// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation. * * Authors: Adrian Hunter * Artem Bityutskiy (Битюцкий Артём)
*/
/* * This file implements the functions that access LEB properties and their * categories. LEBs are categorized based on the needs of UBIFS, and the * categories are stored as either heaps or lists to provide a fast way of * finding a LEB in a particular category. For example, UBIFS may need to find * an empty LEB for the journal, or a very dirty LEB for garbage collection.
*/
#include"ubifs.h"
/** * get_heap_comp_val - get the LEB properties value for heap comparisons. * @lprops: LEB properties * @cat: LEB category
*/ staticint get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
{ switch (cat) { case LPROPS_FREE: return lprops->free; case LPROPS_DIRTY_IDX: return lprops->free + lprops->dirty; default: return lprops->dirty;
}
}
/** * move_up_lpt_heap - move a new heap entry up as far as possible. * @c: UBIFS file-system description object * @heap: LEB category heap * @lprops: LEB properties to move * @cat: LEB category * * New entries to a heap are added at the bottom and then moved up until the * parent's value is greater. In the case of LPT's category heaps, the value * is either the amount of free space or the amount of dirty space, depending * on the category.
*/ staticvoid move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, struct ubifs_lprops *lprops, int cat)
{ int val1, val2, hpos;
hpos = lprops->hpos; if (!hpos) return; /* Already top of the heap */
val1 = get_heap_comp_val(lprops, cat); /* Compare to parent and, if greater, move up the heap */ do { int ppos = (hpos - 1) / 2;
val2 = get_heap_comp_val(heap->arr[ppos], cat); if (val2 >= val1) return; /* Greater than parent so move up */
heap->arr[ppos]->hpos = hpos;
heap->arr[hpos] = heap->arr[ppos];
heap->arr[ppos] = lprops;
lprops->hpos = ppos;
hpos = ppos;
} while (hpos);
}
/** * adjust_lpt_heap - move a changed heap entry up or down the heap. * @c: UBIFS file-system description object * @heap: LEB category heap * @lprops: LEB properties to move * @hpos: heap position of @lprops * @cat: LEB category * * Changed entries in a heap are moved up or down until the parent's value is * greater. In the case of LPT's category heaps, the value is either the amount * of free space or the amount of dirty space, depending on the category.
*/ staticvoid adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, struct ubifs_lprops *lprops, int hpos, int cat)
{ int val1, val2, val3, cpos;
val1 = get_heap_comp_val(lprops, cat); /* Compare to parent and, if greater than parent, move up the heap */ if (hpos) { int ppos = (hpos - 1) / 2;
val2 = get_heap_comp_val(heap->arr[ppos], cat); if (val1 > val2) { /* Greater than parent so move up */ while (1) {
heap->arr[ppos]->hpos = hpos;
heap->arr[hpos] = heap->arr[ppos];
heap->arr[ppos] = lprops;
lprops->hpos = ppos;
hpos = ppos; if (!hpos) return;
ppos = (hpos - 1) / 2;
val2 = get_heap_comp_val(heap->arr[ppos], cat); if (val1 <= val2) return; /* Still greater than parent so keep going */
}
}
}
/* Not greater than parent, so compare to children */ while (1) { /* Compare to left child */
cpos = hpos * 2 + 1; if (cpos >= heap->cnt) return;
val2 = get_heap_comp_val(heap->arr[cpos], cat); if (val1 < val2) { /* Less than left child, so promote biggest child */ if (cpos + 1 < heap->cnt) {
val3 = get_heap_comp_val(heap->arr[cpos + 1],
cat); if (val3 > val2)
cpos += 1; /* Right child is bigger */
}
heap->arr[cpos]->hpos = hpos;
heap->arr[hpos] = heap->arr[cpos];
heap->arr[cpos] = lprops;
lprops->hpos = cpos;
hpos = cpos; continue;
} /* Compare to right child */
cpos += 1; if (cpos >= heap->cnt) return;
val3 = get_heap_comp_val(heap->arr[cpos], cat); if (val1 < val3) { /* Less than right child, so promote right child */
heap->arr[cpos]->hpos = hpos;
heap->arr[hpos] = heap->arr[cpos];
heap->arr[cpos] = lprops;
lprops->hpos = cpos;
hpos = cpos; continue;
} return;
}
}
/** * add_to_lpt_heap - add LEB properties to a LEB category heap. * @c: UBIFS file-system description object * @lprops: LEB properties to add * @cat: LEB category * * This function returns %1 if @lprops is added to the heap for LEB category * @cat, otherwise %0 is returned because the heap is full.
*/ staticint add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops, int cat)
{ struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
if (heap->cnt >= heap->max_cnt) { constint b = LPT_HEAP_SZ / 2 - 1; int cpos, val1, val2;
/* Compare to some other LEB on the bottom of heap */ /* Pick a position kind of randomly */
cpos = (((size_t)lprops >> 4) & b) + b;
ubifs_assert(c, cpos >= b);
ubifs_assert(c, cpos < LPT_HEAP_SZ);
ubifs_assert(c, cpos < heap->cnt);
/** * lpt_heap_replace - replace lprops in a category heap. * @c: UBIFS file-system description object * @new_lprops: LEB properties with which to replace * @cat: LEB category * * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode) * and the lprops that the pnode contains. When that happens, references in * the category heaps to those lprops must be updated to point to the new * lprops. This function does that.
*/ staticvoid lpt_heap_replace(struct ubifs_info *c, struct ubifs_lprops *new_lprops, int cat)
{ struct ubifs_lpt_heap *heap; int hpos = new_lprops->hpos;
/** * ubifs_add_to_cat - add LEB properties to a category list or heap. * @c: UBIFS file-system description object * @lprops: LEB properties to add * @cat: LEB category to which to add * * LEB properties are categorized to enable fast find operations.
*/ void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops, int cat)
{ switch (cat) { case LPROPS_DIRTY: case LPROPS_DIRTY_IDX: case LPROPS_FREE: if (add_to_lpt_heap(c, lprops, cat)) break; /* No more room on heap so make it un-categorized */
cat = LPROPS_UNCAT;
fallthrough; case LPROPS_UNCAT:
list_add(&lprops->list, &c->uncat_list); break; case LPROPS_EMPTY:
list_add(&lprops->list, &c->empty_list); break; case LPROPS_FREEABLE:
list_add(&lprops->list, &c->freeable_list);
c->freeable_cnt += 1; break; case LPROPS_FRDI_IDX:
list_add(&lprops->list, &c->frdi_idx_list); break; default:
ubifs_assert(c, 0);
}
/** * ubifs_remove_from_cat - remove LEB properties from a category list or heap. * @c: UBIFS file-system description object * @lprops: LEB properties to remove * @cat: LEB category from which to remove * * LEB properties are categorized to enable fast find operations.
*/ staticvoid ubifs_remove_from_cat(struct ubifs_info *c, struct ubifs_lprops *lprops, int cat)
{ switch (cat) { case LPROPS_DIRTY: case LPROPS_DIRTY_IDX: case LPROPS_FREE:
remove_from_lpt_heap(c, lprops, cat); break; case LPROPS_FREEABLE:
c->freeable_cnt -= 1;
ubifs_assert(c, c->freeable_cnt >= 0);
fallthrough; case LPROPS_UNCAT: case LPROPS_EMPTY: case LPROPS_FRDI_IDX:
ubifs_assert(c, !list_empty(&lprops->list));
list_del(&lprops->list); break; default:
ubifs_assert(c, 0);
}
/** * ubifs_replace_cat - replace lprops in a category list or heap. * @c: UBIFS file-system description object * @old_lprops: LEB properties to replace * @new_lprops: LEB properties with which to replace * * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode) * and the lprops that the pnode contains. When that happens, references in * category lists and heaps must be replaced. This function does that.
*/ void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops, struct ubifs_lprops *new_lprops)
{ int cat;
cat = new_lprops->flags & LPROPS_CAT_MASK; switch (cat) { case LPROPS_DIRTY: case LPROPS_DIRTY_IDX: case LPROPS_FREE:
lpt_heap_replace(c, new_lprops, cat); break; case LPROPS_UNCAT: case LPROPS_EMPTY: case LPROPS_FREEABLE: case LPROPS_FRDI_IDX:
list_replace(&old_lprops->list, &new_lprops->list); break; default:
ubifs_assert(c, 0);
}
}
/** * ubifs_ensure_cat - ensure LEB properties are categorized. * @c: UBIFS file-system description object * @lprops: LEB properties * * A LEB may have fallen off of the bottom of a heap, and ended up as * un-categorized even though it has enough space for us now. If that is the * case this function will put the LEB back onto a heap.
*/ void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
{ int cat = lprops->flags & LPROPS_CAT_MASK;
/** * ubifs_categorize_lprops - categorize LEB properties. * @c: UBIFS file-system description object * @lprops: LEB properties to categorize * * LEB properties are categorized to enable fast find operations. This function * returns the LEB category to which the LEB properties belong. Note however * that if the LEB category is stored as a heap and the heap is full, the * LEB properties may have their category changed to %LPROPS_UNCAT.
*/ int ubifs_categorize_lprops(conststruct ubifs_info *c, conststruct ubifs_lprops *lprops)
{ if (lprops->flags & LPROPS_TAKEN) return LPROPS_UNCAT;
if (lprops->free + lprops->dirty == c->leb_size) { if (lprops->flags & LPROPS_INDEX) return LPROPS_FRDI_IDX; else return LPROPS_FREEABLE;
}
if (lprops->flags & LPROPS_INDEX) { if (lprops->dirty + lprops->free >= c->min_idx_node_sz) return LPROPS_DIRTY_IDX;
} else { if (lprops->dirty >= c->dead_wm &&
lprops->dirty > lprops->free) return LPROPS_DIRTY; if (lprops->free > 0) return LPROPS_FREE;
}
return LPROPS_UNCAT;
}
/** * change_category - change LEB properties category. * @c: UBIFS file-system description object * @lprops: LEB properties to re-categorize * * LEB properties are categorized to enable fast find operations. When the LEB * properties change they must be re-categorized.
*/ staticvoid change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
{ int old_cat = lprops->flags & LPROPS_CAT_MASK; int new_cat = ubifs_categorize_lprops(c, lprops);
if (old_cat == new_cat) { struct ubifs_lpt_heap *heap;
/* lprops on a heap now must be moved up or down */ if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT) return; /* Not on a heap */
heap = &c->lpt_heap[new_cat - 1];
adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
} else {
ubifs_remove_from_cat(c, lprops, old_cat);
ubifs_add_to_cat(c, lprops, new_cat);
}
}
/** * ubifs_calc_dark - calculate LEB dark space size. * @c: the UBIFS file-system description object * @spc: amount of free and dirty space in the LEB * * This function calculates and returns amount of dark space in an LEB which * has @spc bytes of free and dirty space. * * UBIFS is trying to account the space which might not be usable, and this * space is called "dark space". For example, if an LEB has only %512 free * bytes, it is dark space, because it cannot fit a large data node.
*/ int ubifs_calc_dark(conststruct ubifs_info *c, int spc)
{
ubifs_assert(c, !(spc & 7));
if (spc < c->dark_wm) return spc;
/* * If we have slightly more space then the dark space watermark, we can * anyway safely assume it we'll be able to write a node of the * smallest size there.
*/ if (spc - c->dark_wm < MIN_WRITE_SZ) return spc - MIN_WRITE_SZ;
return c->dark_wm;
}
/** * is_lprops_dirty - determine if LEB properties are dirty. * @c: the UBIFS file-system description object * @lprops: LEB properties to test
*/ staticint is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
{ struct ubifs_pnode *pnode; int pos;
/** * ubifs_change_lp - change LEB properties. * @c: the UBIFS file-system description object * @lp: LEB properties to change * @free: new free space amount * @dirty: new dirty space amount * @flags: new flags * @idx_gc_cnt: change to the count of @idx_gc list * * This function changes LEB properties (@free, @dirty or @flag). However, the * property which has the %LPROPS_NC value is not changed. Returns a pointer to * the updated LEB properties on success and a negative error code on failure. * * Note, the LEB properties may have had to be copied (due to COW) and * consequently the pointer returned may not be the same as the pointer * passed.
*/ conststruct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c, conststruct ubifs_lprops *lp, int free, int dirty, int flags, int idx_gc_cnt)
{ /* * This is the only function that is allowed to change lprops, so we * discard the "const" qualifier.
*/ struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
/** * ubifs_change_one_lp - change LEB properties. * @c: the UBIFS file-system description object * @lnum: LEB to change properties for * @free: amount of free space * @dirty: amount of dirty space * @flags_set: flags to set * @flags_clean: flags to clean * @idx_gc_cnt: change to the count of idx_gc list * * This function changes properties of LEB @lnum. It is a helper wrapper over * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and * a negative error code in case of failure.
*/ int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty, int flags_set, int flags_clean, int idx_gc_cnt)
{ int err = 0, flags; conststruct ubifs_lprops *lp;
out:
ubifs_release_lprops(c); if (err)
ubifs_err(c, "cannot change properties of LEB %d, error %d",
lnum, err); return err;
}
/** * ubifs_update_one_lp - update LEB properties. * @c: the UBIFS file-system description object * @lnum: LEB to change properties for * @free: amount of free space * @dirty: amount of dirty space to add * @flags_set: flags to set * @flags_clean: flags to clean * * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to * current dirty space, not substitutes it.
*/ int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty, int flags_set, int flags_clean)
{ int err = 0, flags; conststruct ubifs_lprops *lp;
out:
ubifs_release_lprops(c); if (err)
ubifs_err(c, "cannot update properties of LEB %d, error %d",
lnum, err); return err;
}
/** * ubifs_read_one_lp - read LEB properties. * @c: the UBIFS file-system description object * @lnum: LEB to read properties for * @lp: where to store read properties * * This helper function reads properties of a LEB @lnum and stores them in @lp. * Returns zero in case of success and a negative error code in case of * failure.
*/ int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
{ int err = 0; conststruct ubifs_lprops *lpp;
/** * ubifs_fast_find_free - try to find a LEB with free space quickly. * @c: the UBIFS file-system description object * * This function returns LEB properties for a LEB with free space or %NULL if * the function is unable to find a LEB quickly.
*/ conststruct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
{ struct ubifs_lprops *lprops; struct ubifs_lpt_heap *heap;
/** * ubifs_fast_find_empty - try to find an empty LEB quickly. * @c: the UBIFS file-system description object * * This function returns LEB properties for an empty LEB or %NULL if the * function is unable to find an empty LEB quickly.
*/ conststruct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
{ struct ubifs_lprops *lprops;
/** * ubifs_fast_find_freeable - try to find a freeable LEB quickly. * @c: the UBIFS file-system description object * * This function returns LEB properties for a freeable LEB or %NULL if the * function is unable to find a freeable LEB quickly.
*/ conststruct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
{ struct ubifs_lprops *lprops;
/** * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly. * @c: the UBIFS file-system description object * * This function returns LEB properties for a freeable index LEB or %NULL if the * function is unable to find a freeable index LEB quickly.
*/ conststruct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
{ struct ubifs_lprops *lprops;
/** * dbg_check_cats - check category heaps and lists. * @c: UBIFS file-system description object * * This function returns %0 on success and a negative error code on failure.
*/ int dbg_check_cats(struct ubifs_info *c)
{ struct ubifs_lprops *lprops; struct list_head *pos; int i, cat;
if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c)) return 0;
list_for_each_entry(lprops, &c->empty_list, list) { if (lprops->free != c->leb_size) {
ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
} if (lprops->flags & LPROPS_TAKEN) {
ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
}
}
i = 0;
list_for_each_entry(lprops, &c->freeable_list, list) { if (lprops->free + lprops->dirty != c->leb_size) {
ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
} if (lprops->flags & LPROPS_TAKEN) {
ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
}
i += 1;
} if (i != c->freeable_cnt) {
ubifs_err(c, "freeable list count %d expected %d", i,
c->freeable_cnt); return -EINVAL;
}
i = 0;
list_for_each(pos, &c->idx_gc)
i += 1; if (i != c->idx_gc_cnt) {
ubifs_err(c, "idx_gc list count %d expected %d", i,
c->idx_gc_cnt); return -EINVAL;
}
list_for_each_entry(lprops, &c->frdi_idx_list, list) { if (lprops->free + lprops->dirty != c->leb_size) {
ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
} if (lprops->flags & LPROPS_TAKEN) {
ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
} if (!(lprops->flags & LPROPS_INDEX)) {
ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
lprops->lnum, lprops->free, lprops->dirty,
lprops->flags); return -EINVAL;
}
}
/** * scan_check_cb - scan callback. * @c: the UBIFS file-system description object * @lp: LEB properties to scan * @in_tree: whether the LEB properties are in main memory * @arg: lprops statistics to update * * This function returns a code that indicates whether the scan should continue * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree * in main memory (%LPT_SCAN_ADD), or whether the scan should stop * (%LPT_SCAN_STOP).
*/ staticint scan_check_cb(struct ubifs_info *c, conststruct ubifs_lprops *lp, int in_tree, void *arg)
{ struct ubifs_lp_stats *lst = arg; struct ubifs_scan_leb *sleb; struct ubifs_scan_node *snod; int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret; void *buf = NULL;
/* Check lp is on its category list (if it has one) */ if (in_tree) { struct list_head *list = NULL;
switch (cat) { case LPROPS_EMPTY:
list = &c->empty_list; break; case LPROPS_FREEABLE:
list = &c->freeable_list; break; case LPROPS_FRDI_IDX:
list = &c->frdi_idx_list; break; case LPROPS_UNCAT:
list = &c->uncat_list; break;
} if (list) { struct ubifs_lprops *lprops; int found = 0;
list_for_each_entry(lprops, list, list) { if (lprops == lp) {
found = 1; break;
}
} if (!found) {
ubifs_err(c, "bad LPT list (category %d)", cat); return -EINVAL;
}
}
}
/* Check lp is on its category heap (if it has one) */ if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) { struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
if (lp->free + lp->dirty == c->leb_size &&
free + dirty == c->leb_size) if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
(!is_idx && free == c->leb_size) ||
lp->free == c->leb_size) { /* * Empty or freeable LEBs could contain index * nodes from an uncompleted commit due to an * unclean unmount. Or they could be empty for * the same reason. Or it may simply not have been * unmapped.
*/
free = lp->free;
dirty = lp->dirty;
is_idx = 0;
}
if (is_idx && lp->free + lp->dirty == free + dirty &&
lnum != c->ihead_lnum) { /* * After an unclean unmount, an index LEB could have a different * amount of free space than the value recorded by lprops. That * is because the in-the-gaps method may use free space or * create free space (as a side-effect of using ubi_leb_change * and not writing the whole LEB). The incorrect free space * value is not a problem because the index is only ever * allocated empty LEBs, so there will never be an attempt to * write to the free space at the end of an index LEB - except * by the in-the-gaps method for which it is not a problem.
*/
free = lp->free;
dirty = lp->dirty;
}
if (lp->free != free || lp->dirty != dirty) goto out_print;
if (is_idx && !(lp->flags & LPROPS_INDEX)) { if (free == c->leb_size) /* Free but not unmapped LEB, it's fine */
is_idx = 0; else {
ubifs_err(c, "indexing node without indexing flag"); goto out_print;
}
}
if (!is_idx && (lp->flags & LPROPS_INDEX)) {
ubifs_err(c, "data node with indexing flag"); goto out_print;
}
out_print:
ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
lnum, lp->free, lp->dirty, lp->flags, free, dirty);
ubifs_dump_leb(c, lnum);
out_destroy:
ubifs_scan_destroy(sleb);
ret = -EINVAL;
out:
vfree(buf); return ret;
}
/** * dbg_check_lprops - check all LEB properties. * @c: UBIFS file-system description object * * This function checks all LEB properties and makes sure they are all correct. * It returns zero if everything is fine, %-EINVAL if there is an inconsistency * and other negative error codes in case of other errors. This function is * called while the file system is locked (because of commit start), so no * additional locking is required. Note that locking the LPT mutex would cause * a circular lock dependency with the TNC mutex.
*/ int dbg_check_lprops(struct ubifs_info *c)
{ int i, err; struct ubifs_lp_stats lst;
if (!dbg_is_chk_lprops(c)) return 0;
/* * As we are going to scan the media, the write buffers have to be * synchronized.
*/ for (i = 0; i < c->jhead_cnt; i++) {
err = ubifs_wbuf_sync(&c->jheads[i].wbuf); if (err) return err;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.