2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
13 * This file implements the functions that access LEB properties and their
14 * categories. LEBs are categorized based on the needs of UBIFS, and the
15 * categories are stored as either heaps or lists to provide a fast way of
16 * finding a LEB in a particular category. For example, UBIFS may need to find
17 * an empty LEB for the journal, or a very dirty LEB for garbage collection.
22 #include <linux/err.h>
27 * get_heap_comp_val - get the LEB properties value for heap comparisons.
28 * @lprops: LEB properties
31 static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
36 case LPROPS_DIRTY_IDX:
37 return lprops->free + lprops->dirty;
44 * move_up_lpt_heap - move a new heap entry up as far as possible.
45 * @c: UBIFS file-system description object
46 * @heap: LEB category heap
47 * @lprops: LEB properties to move
50 * New entries to a heap are added at the bottom and then moved up until the
51 * parent's value is greater. In the case of LPT's category heaps, the value
52 * is either the amount of free space or the amount of dirty space, depending
55 static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
56 struct ubifs_lprops *lprops, int cat)
62 return; /* Already top of the heap */
63 val1 = get_heap_comp_val(lprops, cat);
64 /* Compare to parent and, if greater, move up the heap */
66 int ppos = (hpos - 1) / 2;
68 val2 = get_heap_comp_val(heap->arr[ppos], cat);
71 /* Greater than parent so move up */
72 heap->arr[ppos]->hpos = hpos;
73 heap->arr[hpos] = heap->arr[ppos];
74 heap->arr[ppos] = lprops;
81 * adjust_lpt_heap - move a changed heap entry up or down the heap.
82 * @c: UBIFS file-system description object
83 * @heap: LEB category heap
84 * @lprops: LEB properties to move
85 * @hpos: heap position of @lprops
88 * Changed entries in a heap are moved up or down until the parent's value is
89 * greater. In the case of LPT's category heaps, the value is either the amount
90 * of free space or the amount of dirty space, depending on the category.
92 static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
93 struct ubifs_lprops *lprops, int hpos, int cat)
95 int val1, val2, val3, cpos;
97 val1 = get_heap_comp_val(lprops, cat);
98 /* Compare to parent and, if greater than parent, move up the heap */
100 int ppos = (hpos - 1) / 2;
102 val2 = get_heap_comp_val(heap->arr[ppos], cat);
104 /* Greater than parent so move up */
106 heap->arr[ppos]->hpos = hpos;
107 heap->arr[hpos] = heap->arr[ppos];
108 heap->arr[ppos] = lprops;
113 ppos = (hpos - 1) / 2;
114 val2 = get_heap_comp_val(heap->arr[ppos], cat);
117 /* Still greater than parent so keep going */
122 /* Not greater than parent, so compare to children */
124 /* Compare to left child */
126 if (cpos >= heap->cnt)
128 val2 = get_heap_comp_val(heap->arr[cpos], cat);
130 /* Less than left child, so promote biggest child */
131 if (cpos + 1 < heap->cnt) {
132 val3 = get_heap_comp_val(heap->arr[cpos + 1],
135 cpos += 1; /* Right child is bigger */
137 heap->arr[cpos]->hpos = hpos;
138 heap->arr[hpos] = heap->arr[cpos];
139 heap->arr[cpos] = lprops;
144 /* Compare to right child */
146 if (cpos >= heap->cnt)
148 val3 = get_heap_comp_val(heap->arr[cpos], cat);
150 /* Less than right child, so promote right child */
151 heap->arr[cpos]->hpos = hpos;
152 heap->arr[hpos] = heap->arr[cpos];
153 heap->arr[cpos] = lprops;
163 * add_to_lpt_heap - add LEB properties to a LEB category heap.
164 * @c: UBIFS file-system description object
165 * @lprops: LEB properties to add
168 * This function returns %1 if @lprops is added to the heap for LEB category
169 * @cat, otherwise %0 is returned because the heap is full.
171 static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
174 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
176 if (heap->cnt >= heap->max_cnt) {
177 const int b = LPT_HEAP_SZ / 2 - 1;
178 int cpos, val1, val2;
180 /* Compare to some other LEB on the bottom of heap */
181 /* Pick a position kind of randomly */
182 cpos = (((size_t)lprops >> 4) & b) + b;
183 ubifs_assert(cpos >= b);
184 ubifs_assert(cpos < LPT_HEAP_SZ);
185 ubifs_assert(cpos < heap->cnt);
187 val1 = get_heap_comp_val(lprops, cat);
188 val2 = get_heap_comp_val(heap->arr[cpos], cat);
190 struct ubifs_lprops *lp;
192 lp = heap->arr[cpos];
193 lp->flags &= ~LPROPS_CAT_MASK;
194 lp->flags |= LPROPS_UNCAT;
195 list_add(&lp->list, &c->uncat_list);
197 heap->arr[cpos] = lprops;
198 move_up_lpt_heap(c, heap, lprops, cat);
199 dbg_check_heap(c, heap, cat, lprops->hpos);
200 return 1; /* Added to heap */
202 dbg_check_heap(c, heap, cat, -1);
203 return 0; /* Not added to heap */
205 lprops->hpos = heap->cnt++;
206 heap->arr[lprops->hpos] = lprops;
207 move_up_lpt_heap(c, heap, lprops, cat);
208 dbg_check_heap(c, heap, cat, lprops->hpos);
209 return 1; /* Added to heap */
214 * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
215 * @c: UBIFS file-system description object
216 * @lprops: LEB properties to remove
219 static void remove_from_lpt_heap(struct ubifs_info *c,
220 struct ubifs_lprops *lprops, int cat)
222 struct ubifs_lpt_heap *heap;
223 int hpos = lprops->hpos;
225 heap = &c->lpt_heap[cat - 1];
226 ubifs_assert(hpos >= 0 && hpos < heap->cnt);
227 ubifs_assert(heap->arr[hpos] == lprops);
229 if (hpos < heap->cnt) {
230 heap->arr[hpos] = heap->arr[heap->cnt];
231 heap->arr[hpos]->hpos = hpos;
232 adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
234 dbg_check_heap(c, heap, cat, -1);
238 * lpt_heap_replace - replace lprops in a category heap.
239 * @c: UBIFS file-system description object
240 * @old_lprops: LEB properties to replace
241 * @new_lprops: LEB properties with which to replace
244 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
245 * and the lprops that the pnode contains. When that happens, references in
246 * the category heaps to those lprops must be updated to point to the new
247 * lprops. This function does that.
249 static void lpt_heap_replace(struct ubifs_info *c,
250 struct ubifs_lprops *old_lprops,
251 struct ubifs_lprops *new_lprops, int cat)
253 struct ubifs_lpt_heap *heap;
254 int hpos = new_lprops->hpos;
256 heap = &c->lpt_heap[cat - 1];
257 heap->arr[hpos] = new_lprops;
261 * ubifs_add_to_cat - add LEB properties to a category list or heap.
262 * @c: UBIFS file-system description object
263 * @lprops: LEB properties to add
264 * @cat: LEB category to which to add
266 * LEB properties are categorized to enable fast find operations.
268 void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
273 case LPROPS_DIRTY_IDX:
275 if (add_to_lpt_heap(c, lprops, cat))
277 /* No more room on heap so make it un-categorized */
281 list_add(&lprops->list, &c->uncat_list);
284 list_add(&lprops->list, &c->empty_list);
286 case LPROPS_FREEABLE:
287 list_add(&lprops->list, &c->freeable_list);
288 c->freeable_cnt += 1;
290 case LPROPS_FRDI_IDX:
291 list_add(&lprops->list, &c->frdi_idx_list);
297 lprops->flags &= ~LPROPS_CAT_MASK;
298 lprops->flags |= cat;
299 c->in_a_category_cnt += 1;
300 ubifs_assert(c->in_a_category_cnt <= c->main_lebs);
304 * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
305 * @c: UBIFS file-system description object
306 * @lprops: LEB properties to remove
307 * @cat: LEB category from which to remove
309 * LEB properties are categorized to enable fast find operations.
311 static void ubifs_remove_from_cat(struct ubifs_info *c,
312 struct ubifs_lprops *lprops, int cat)
316 case LPROPS_DIRTY_IDX:
318 remove_from_lpt_heap(c, lprops, cat);
320 case LPROPS_FREEABLE:
321 c->freeable_cnt -= 1;
322 ubifs_assert(c->freeable_cnt >= 0);
326 case LPROPS_FRDI_IDX:
327 ubifs_assert(!list_empty(&lprops->list));
328 list_del(&lprops->list);
334 c->in_a_category_cnt -= 1;
335 ubifs_assert(c->in_a_category_cnt >= 0);
339 * ubifs_replace_cat - replace lprops in a category list or heap.
340 * @c: UBIFS file-system description object
341 * @old_lprops: LEB properties to replace
342 * @new_lprops: LEB properties with which to replace
344 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
345 * and the lprops that the pnode contains. When that happens, references in
346 * category lists and heaps must be replaced. This function does that.
348 void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
349 struct ubifs_lprops *new_lprops)
353 cat = new_lprops->flags & LPROPS_CAT_MASK;
356 case LPROPS_DIRTY_IDX:
358 lpt_heap_replace(c, old_lprops, new_lprops, cat);
362 case LPROPS_FREEABLE:
363 case LPROPS_FRDI_IDX:
364 list_replace(&old_lprops->list, &new_lprops->list);
372 * ubifs_ensure_cat - ensure LEB properties are categorized.
373 * @c: UBIFS file-system description object
374 * @lprops: LEB properties
376 * A LEB may have fallen off of the bottom of a heap, and ended up as
377 * un-categorized even though it has enough space for us now. If that is the
378 * case this function will put the LEB back onto a heap.
380 void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
382 int cat = lprops->flags & LPROPS_CAT_MASK;
384 if (cat != LPROPS_UNCAT)
386 cat = ubifs_categorize_lprops(c, lprops);
387 if (cat == LPROPS_UNCAT)
389 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
390 ubifs_add_to_cat(c, lprops, cat);
394 * ubifs_categorize_lprops - categorize LEB properties.
395 * @c: UBIFS file-system description object
396 * @lprops: LEB properties to categorize
398 * LEB properties are categorized to enable fast find operations. This function
399 * returns the LEB category to which the LEB properties belong. Note however
400 * that if the LEB category is stored as a heap and the heap is full, the
401 * LEB properties may have their category changed to %LPROPS_UNCAT.
403 int ubifs_categorize_lprops(const struct ubifs_info *c,
404 const struct ubifs_lprops *lprops)
406 if (lprops->flags & LPROPS_TAKEN)
409 if (lprops->free == c->leb_size) {
410 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
414 if (lprops->free + lprops->dirty == c->leb_size) {
415 if (lprops->flags & LPROPS_INDEX)
416 return LPROPS_FRDI_IDX;
418 return LPROPS_FREEABLE;
421 if (lprops->flags & LPROPS_INDEX) {
422 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
423 return LPROPS_DIRTY_IDX;
425 if (lprops->dirty >= c->dead_wm &&
426 lprops->dirty > lprops->free)
428 if (lprops->free > 0)
436 * change_category - change LEB properties category.
437 * @c: UBIFS file-system description object
438 * @lprops: LEB properties to re-categorize
440 * LEB properties are categorized to enable fast find operations. When the LEB
441 * properties change they must be re-categorized.
443 static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
445 int old_cat = lprops->flags & LPROPS_CAT_MASK;
446 int new_cat = ubifs_categorize_lprops(c, lprops);
448 if (old_cat == new_cat) {
449 struct ubifs_lpt_heap *heap;
451 /* lprops on a heap now must be moved up or down */
452 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
453 return; /* Not on a heap */
454 heap = &c->lpt_heap[new_cat - 1];
455 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
457 ubifs_remove_from_cat(c, lprops, old_cat);
458 ubifs_add_to_cat(c, lprops, new_cat);
463 * ubifs_calc_dark - calculate LEB dark space size.
464 * @c: the UBIFS file-system description object
465 * @spc: amount of free and dirty space in the LEB
467 * This function calculates and returns amount of dark space in an LEB which
468 * has @spc bytes of free and dirty space.
470 * UBIFS is trying to account the space which might not be usable, and this
471 * space is called "dark space". For example, if an LEB has only %512 free
472 * bytes, it is dark space, because it cannot fit a large data node.
474 int ubifs_calc_dark(const struct ubifs_info *c, int spc)
476 ubifs_assert(!(spc & 7));
478 if (spc < c->dark_wm)
482 * If we have slightly more space then the dark space watermark, we can
483 * anyway safely assume it we'll be able to write a node of the
484 * smallest size there.
486 if (spc - c->dark_wm < MIN_WRITE_SZ)
487 return spc - MIN_WRITE_SZ;
493 * is_lprops_dirty - determine if LEB properties are dirty.
494 * @c: the UBIFS file-system description object
495 * @lprops: LEB properties to test
497 static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
499 struct ubifs_pnode *pnode;
502 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
503 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
506 return !test_bit(COW_CNODE, &pnode->flags) &&
507 test_bit(DIRTY_CNODE, &pnode->flags);
511 * ubifs_change_lp - change LEB properties.
512 * @c: the UBIFS file-system description object
513 * @lp: LEB properties to change
514 * @free: new free space amount
515 * @dirty: new dirty space amount
517 * @idx_gc_cnt: change to the count of @idx_gc list
519 * This function changes LEB properties (@free, @dirty or @flag). However, the
520 * property which has the %LPROPS_NC value is not changed. Returns a pointer to
521 * the updated LEB properties on success and a negative error code on failure.
523 * Note, the LEB properties may have had to be copied (due to COW) and
524 * consequently the pointer returned may not be the same as the pointer
527 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
528 const struct ubifs_lprops *lp,
529 int free, int dirty, int flags,
533 * This is the only function that is allowed to change lprops, so we
534 * discard the "const" qualifier.
536 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
538 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
539 lprops->lnum, free, dirty, flags);
541 ubifs_assert(mutex_is_locked(&c->lp_mutex));
542 ubifs_assert(c->lst.empty_lebs >= 0 &&
543 c->lst.empty_lebs <= c->main_lebs);
544 ubifs_assert(c->freeable_cnt >= 0);
545 ubifs_assert(c->freeable_cnt <= c->main_lebs);
546 ubifs_assert(c->lst.taken_empty_lebs >= 0);
547 ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
548 ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
549 ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
550 ubifs_assert(!(c->lst.total_used & 7));
551 ubifs_assert(free == LPROPS_NC || free >= 0);
552 ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
554 if (!is_lprops_dirty(c, lprops)) {
555 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
559 ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
561 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
563 spin_lock(&c->space_lock);
564 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
565 c->lst.taken_empty_lebs -= 1;
567 if (!(lprops->flags & LPROPS_INDEX)) {
570 old_spc = lprops->free + lprops->dirty;
571 if (old_spc < c->dead_wm)
572 c->lst.total_dead -= old_spc;
574 c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
576 c->lst.total_used -= c->leb_size - old_spc;
579 if (free != LPROPS_NC) {
580 free = ALIGN(free, 8);
581 c->lst.total_free += free - lprops->free;
583 /* Increase or decrease empty LEBs counter if needed */
584 if (free == c->leb_size) {
585 if (lprops->free != c->leb_size)
586 c->lst.empty_lebs += 1;
587 } else if (lprops->free == c->leb_size)
588 c->lst.empty_lebs -= 1;
592 if (dirty != LPROPS_NC) {
593 dirty = ALIGN(dirty, 8);
594 c->lst.total_dirty += dirty - lprops->dirty;
595 lprops->dirty = dirty;
598 if (flags != LPROPS_NC) {
599 /* Take care about indexing LEBs counter if needed */
600 if ((lprops->flags & LPROPS_INDEX)) {
601 if (!(flags & LPROPS_INDEX))
602 c->lst.idx_lebs -= 1;
603 } else if (flags & LPROPS_INDEX)
604 c->lst.idx_lebs += 1;
605 lprops->flags = flags;
608 if (!(lprops->flags & LPROPS_INDEX)) {
611 new_spc = lprops->free + lprops->dirty;
612 if (new_spc < c->dead_wm)
613 c->lst.total_dead += new_spc;
615 c->lst.total_dark += ubifs_calc_dark(c, new_spc);
617 c->lst.total_used += c->leb_size - new_spc;
620 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
621 c->lst.taken_empty_lebs += 1;
623 change_category(c, lprops);
624 c->idx_gc_cnt += idx_gc_cnt;
625 spin_unlock(&c->space_lock);
630 * ubifs_get_lp_stats - get lprops statistics.
631 * @c: UBIFS file-system description object
632 * @st: return statistics
634 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
636 spin_lock(&c->space_lock);
637 memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
638 spin_unlock(&c->space_lock);
642 * ubifs_change_one_lp - change LEB properties.
643 * @c: the UBIFS file-system description object
644 * @lnum: LEB to change properties for
645 * @free: amount of free space
646 * @dirty: amount of dirty space
647 * @flags_set: flags to set
648 * @flags_clean: flags to clean
649 * @idx_gc_cnt: change to the count of idx_gc list
651 * This function changes properties of LEB @lnum. It is a helper wrapper over
652 * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
653 * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
654 * a negative error code in case of failure.
656 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
657 int flags_set, int flags_clean, int idx_gc_cnt)
660 const struct ubifs_lprops *lp;
664 lp = ubifs_lpt_lookup_dirty(c, lnum);
670 flags = (lp->flags | flags_set) & ~flags_clean;
671 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
676 ubifs_release_lprops(c);
678 ubifs_err("cannot change properties of LEB %d, error %d",
684 * ubifs_update_one_lp - update LEB properties.
685 * @c: the UBIFS file-system description object
686 * @lnum: LEB to change properties for
687 * @free: amount of free space
688 * @dirty: amount of dirty space to add
689 * @flags_set: flags to set
690 * @flags_clean: flags to clean
692 * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
693 * current dirty space, not substitutes it.
695 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
696 int flags_set, int flags_clean)
699 const struct ubifs_lprops *lp;
703 lp = ubifs_lpt_lookup_dirty(c, lnum);
709 flags = (lp->flags | flags_set) & ~flags_clean;
710 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
715 ubifs_release_lprops(c);
717 ubifs_err("cannot update properties of LEB %d, error %d",
723 * ubifs_read_one_lp - read LEB properties.
724 * @c: the UBIFS file-system description object
725 * @lnum: LEB to read properties for
726 * @lp: where to store read properties
728 * This helper function reads properties of a LEB @lnum and stores them in @lp.
729 * Returns zero in case of success and a negative error code in case of
732 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
735 const struct ubifs_lprops *lpp;
739 lpp = ubifs_lpt_lookup(c, lnum);
742 ubifs_err("cannot read properties of LEB %d, error %d",
747 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
750 ubifs_release_lprops(c);
755 * ubifs_fast_find_free - try to find a LEB with free space quickly.
756 * @c: the UBIFS file-system description object
758 * This function returns LEB properties for a LEB with free space or %NULL if
759 * the function is unable to find a LEB quickly.
761 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
763 struct ubifs_lprops *lprops;
764 struct ubifs_lpt_heap *heap;
766 ubifs_assert(mutex_is_locked(&c->lp_mutex));
768 heap = &c->lpt_heap[LPROPS_FREE - 1];
772 lprops = heap->arr[0];
773 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
774 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
779 * ubifs_fast_find_empty - try to find an empty LEB quickly.
780 * @c: the UBIFS file-system description object
782 * This function returns LEB properties for an empty LEB or %NULL if the
783 * function is unable to find an empty LEB quickly.
785 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
787 struct ubifs_lprops *lprops;
789 ubifs_assert(mutex_is_locked(&c->lp_mutex));
791 if (list_empty(&c->empty_list))
794 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
795 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
796 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
797 ubifs_assert(lprops->free == c->leb_size);
802 * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
803 * @c: the UBIFS file-system description object
805 * This function returns LEB properties for a freeable LEB or %NULL if the
806 * function is unable to find a freeable LEB quickly.
808 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
810 struct ubifs_lprops *lprops;
812 ubifs_assert(mutex_is_locked(&c->lp_mutex));
814 if (list_empty(&c->freeable_list))
817 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
818 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
819 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
820 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
821 ubifs_assert(c->freeable_cnt > 0);
826 * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
827 * @c: the UBIFS file-system description object
829 * This function returns LEB properties for a freeable index LEB or %NULL if the
830 * function is unable to find a freeable index LEB quickly.
832 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
834 struct ubifs_lprops *lprops;
836 ubifs_assert(mutex_is_locked(&c->lp_mutex));
838 if (list_empty(&c->frdi_idx_list))
841 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
842 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
843 ubifs_assert((lprops->flags & LPROPS_INDEX));
844 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
849 * Everything below is related to debugging.
853 * dbg_check_cats - check category heaps and lists.
854 * @c: UBIFS file-system description object
856 * This function returns %0 on success and a negative error code on failure.
858 int dbg_check_cats(struct ubifs_info *c)
860 struct ubifs_lprops *lprops;
861 struct list_head *pos;
864 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
867 list_for_each_entry(lprops, &c->empty_list, list) {
868 if (lprops->free != c->leb_size) {
869 ubifs_err("non-empty LEB %d on empty list (free %d dirty %d flags %d)",
870 lprops->lnum, lprops->free, lprops->dirty,
874 if (lprops->flags & LPROPS_TAKEN) {
875 ubifs_err("taken LEB %d on empty list (free %d dirty %d flags %d)",
876 lprops->lnum, lprops->free, lprops->dirty,
883 list_for_each_entry(lprops, &c->freeable_list, list) {
884 if (lprops->free + lprops->dirty != c->leb_size) {
885 ubifs_err("non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
886 lprops->lnum, lprops->free, lprops->dirty,
890 if (lprops->flags & LPROPS_TAKEN) {
891 ubifs_err("taken LEB %d on freeable list (free %d dirty %d flags %d)",
892 lprops->lnum, lprops->free, lprops->dirty,
898 if (i != c->freeable_cnt) {
899 ubifs_err("freeable list count %d expected %d", i,
905 list_for_each(pos, &c->idx_gc)
907 if (i != c->idx_gc_cnt) {
908 ubifs_err("idx_gc list count %d expected %d", i,
913 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
914 if (lprops->free + lprops->dirty != c->leb_size) {
915 ubifs_err("non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
916 lprops->lnum, lprops->free, lprops->dirty,
920 if (lprops->flags & LPROPS_TAKEN) {
921 ubifs_err("taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
922 lprops->lnum, lprops->free, lprops->dirty,
926 if (!(lprops->flags & LPROPS_INDEX)) {
927 ubifs_err("non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
928 lprops->lnum, lprops->free, lprops->dirty,
934 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
935 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
937 for (i = 0; i < heap->cnt; i++) {
938 lprops = heap->arr[i];
940 ubifs_err("null ptr in LPT heap cat %d", cat);
943 if (lprops->hpos != i) {
944 ubifs_err("bad ptr in LPT heap cat %d", cat);
947 if (lprops->flags & LPROPS_TAKEN) {
948 ubifs_err("taken LEB in LPT heap cat %d", cat);
957 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
960 int i = 0, j, err = 0;
962 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
965 for (i = 0; i < heap->cnt; i++) {
966 struct ubifs_lprops *lprops = heap->arr[i];
967 struct ubifs_lprops *lp;
970 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
974 if (lprops->hpos != i) {
978 lp = ubifs_lpt_lookup(c, lprops->lnum);
984 ubifs_err("lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
985 (size_t)lprops, (size_t)lp, lprops->lnum,
990 for (j = 0; j < i; j++) {
996 if (lp->lnum == lprops->lnum) {
1004 ubifs_err("failed cat %d hpos %d err %d", cat, i, err);
1006 ubifs_dump_heap(c, heap, cat);
1011 * scan_check_cb - scan callback.
1012 * @c: the UBIFS file-system description object
1013 * @lp: LEB properties to scan
1014 * @in_tree: whether the LEB properties are in main memory
1015 * @lst: lprops statistics to update
1017 * This function returns a code that indicates whether the scan should continue
1018 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1019 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1022 static int scan_check_cb(struct ubifs_info *c,
1023 const struct ubifs_lprops *lp, int in_tree,
1024 struct ubifs_lp_stats *lst)
1026 struct ubifs_scan_leb *sleb;
1027 struct ubifs_scan_node *snod;
1028 int cat, lnum = lp->lnum, is_idx = 0, used = 0, freef, dirty, ret;
1031 cat = lp->flags & LPROPS_CAT_MASK;
1032 if (cat != LPROPS_UNCAT) {
1033 cat = ubifs_categorize_lprops(c, lp);
1034 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1035 ubifs_err("bad LEB category %d expected %d",
1036 (lp->flags & LPROPS_CAT_MASK), cat);
1041 /* Check lp is on its category list (if it has one) */
1043 struct list_head *list = NULL;
1047 list = &c->empty_list;
1049 case LPROPS_FREEABLE:
1050 list = &c->freeable_list;
1052 case LPROPS_FRDI_IDX:
1053 list = &c->frdi_idx_list;
1056 list = &c->uncat_list;
1060 struct ubifs_lprops *lprops;
1063 list_for_each_entry(lprops, list, list) {
1070 ubifs_err("bad LPT list (category %d)", cat);
1076 /* Check lp is on its category heap (if it has one) */
1077 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1078 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1080 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1081 lp != heap->arr[lp->hpos]) {
1082 ubifs_err("bad LPT heap (category %d)", cat);
1087 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1092 * After an unclean unmount, empty and freeable LEBs
1093 * may contain garbage - do not scan them.
1095 if (lp->free == c->leb_size) {
1096 lst->empty_lebs += 1;
1097 lst->total_free += c->leb_size;
1098 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1099 return LPT_SCAN_CONTINUE;
1101 if (lp->free + lp->dirty == c->leb_size &&
1102 !(lp->flags & LPROPS_INDEX)) {
1103 lst->total_free += lp->free;
1104 lst->total_dirty += lp->dirty;
1105 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1106 return LPT_SCAN_CONTINUE;
1109 sleb = ubifs_scan(c, lnum, 0, buf, 0);
1111 ret = PTR_ERR(sleb);
1112 if (ret == -EUCLEAN) {
1113 ubifs_dump_lprops(c);
1114 ubifs_dump_budg(c, &c->bi);
1120 list_for_each_entry(snod, &sleb->nodes, list) {
1121 int found, level = 0;
1126 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1128 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1129 ubifs_err("indexing node in data LEB %d:%d",
1134 if (snod->type == UBIFS_IDX_NODE) {
1135 struct ubifs_idx_node *idx = snod->node;
1137 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1138 level = le16_to_cpu(idx->level);
1141 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1142 snod->offs, is_idx);
1146 used += ALIGN(snod->len, 8);
1150 freef = c->leb_size - sleb->endpt;
1151 dirty = sleb->endpt - used;
1153 if (freef > c->leb_size || freef < 0 || dirty > c->leb_size ||
1155 ubifs_err("bad calculated accounting for LEB %d: free %d, dirty %d",
1156 lnum, freef, dirty);
1160 if (lp->free + lp->dirty == c->leb_size &&
1161 freef + dirty == c->leb_size)
1162 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1163 (!is_idx && freef == c->leb_size) ||
1164 lp->free == c->leb_size) {
1166 * Empty or freeable LEBs could contain index
1167 * nodes from an uncompleted commit due to an
1168 * unclean unmount. Or they could be empty for
1169 * the same reason. Or it may simply not have been
1177 if (is_idx && lp->free + lp->dirty == freef + dirty &&
1178 lnum != c->ihead_lnum) {
1180 * After an unclean unmount, an index LEB could have a different
1181 * amount of free space than the value recorded by lprops. That
1182 * is because the in-the-gaps method may use free space or
1183 * create free space (as a side-effect of using ubi_leb_change
1184 * and not writing the whole LEB). The incorrect free space
1185 * value is not a problem because the index is only ever
1186 * allocated empty LEBs, so there will never be an attempt to
1187 * write to the free space at the end of an index LEB - except
1188 * by the in-the-gaps method for which it is not a problem.
1194 if (lp->free != freef || lp->dirty != dirty)
1197 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1198 if (freef == c->leb_size)
1199 /* Free but not unmapped LEB, it's fine */
1202 ubifs_err("indexing node without indexing flag");
1207 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1208 ubifs_err("data node with indexing flag");
1212 if (freef == c->leb_size)
1213 lst->empty_lebs += 1;
1218 if (!(lp->flags & LPROPS_INDEX))
1219 lst->total_used += c->leb_size - freef - dirty;
1220 lst->total_free += freef;
1221 lst->total_dirty += dirty;
1223 if (!(lp->flags & LPROPS_INDEX)) {
1224 int spc = freef + dirty;
1226 if (spc < c->dead_wm)
1227 lst->total_dead += spc;
1229 lst->total_dark += ubifs_calc_dark(c, spc);
1232 ubifs_scan_destroy(sleb);
1234 return LPT_SCAN_CONTINUE;
1237 ubifs_err("bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1238 lnum, lp->free, lp->dirty, lp->flags, freef, dirty);
1239 ubifs_dump_leb(c, lnum);
1241 ubifs_scan_destroy(sleb);
1249 * dbg_check_lprops - check all LEB properties.
1250 * @c: UBIFS file-system description object
1252 * This function checks all LEB properties and makes sure they are all correct.
1253 * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1254 * and other negative error codes in case of other errors. This function is
1255 * called while the file system is locked (because of commit start), so no
1256 * additional locking is required. Note that locking the LPT mutex would cause
1257 * a circular lock dependency with the TNC mutex.
1259 int dbg_check_lprops(struct ubifs_info *c)
1262 struct ubifs_lp_stats lst;
1264 if (!dbg_is_chk_lprops(c))
1268 * As we are going to scan the media, the write buffers have to be
1271 for (i = 0; i < c->jhead_cnt; i++) {
1272 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1277 memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1278 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1279 (ubifs_lpt_scan_callback)scan_check_cb,
1281 if (err && err != -ENOSPC)
1284 if (lst.empty_lebs != c->lst.empty_lebs ||
1285 lst.idx_lebs != c->lst.idx_lebs ||
1286 lst.total_free != c->lst.total_free ||
1287 lst.total_dirty != c->lst.total_dirty ||
1288 lst.total_used != c->lst.total_used) {
1289 ubifs_err("bad overall accounting");
1290 ubifs_err("calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1291 lst.empty_lebs, lst.idx_lebs, lst.total_free,
1292 lst.total_dirty, lst.total_used);
1293 ubifs_err("read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1294 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1295 c->lst.total_dirty, c->lst.total_used);
1300 if (lst.total_dead != c->lst.total_dead ||
1301 lst.total_dark != c->lst.total_dark) {
1302 ubifs_err("bad dead/dark space accounting");
1303 ubifs_err("calculated: total_dead %lld, total_dark %lld",
1304 lst.total_dead, lst.total_dark);
1305 ubifs_err("read from lprops: total_dead %lld, total_dark %lld",
1306 c->lst.total_dead, c->lst.total_dark);
1311 err = dbg_check_cats(c);