1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
12 * This file implements the functions that access LEB properties and their
13 * categories. LEBs are categorized based on the needs of UBIFS, and the
14 * categories are stored as either heaps or lists to provide a fast way of
15 * finding a LEB in a particular category. For example, UBIFS may need to find
16 * an empty LEB for the journal, or a very dirty LEB for garbage collection.
20 #include <linux/err.h>
25 * get_heap_comp_val - get the LEB properties value for heap comparisons.
26 * @lprops: LEB properties
29 static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
34 case LPROPS_DIRTY_IDX:
35 return lprops->free + lprops->dirty;
42 * move_up_lpt_heap - move a new heap entry up as far as possible.
43 * @c: UBIFS file-system description object
44 * @heap: LEB category heap
45 * @lprops: LEB properties to move
48 * New entries to a heap are added at the bottom and then moved up until the
49 * parent's value is greater. In the case of LPT's category heaps, the value
50 * is either the amount of free space or the amount of dirty space, depending
53 static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
54 struct ubifs_lprops *lprops, int cat)
60 return; /* Already top of the heap */
61 val1 = get_heap_comp_val(lprops, cat);
62 /* Compare to parent and, if greater, move up the heap */
64 int ppos = (hpos - 1) / 2;
66 val2 = get_heap_comp_val(heap->arr[ppos], cat);
69 /* Greater than parent so move up */
70 heap->arr[ppos]->hpos = hpos;
71 heap->arr[hpos] = heap->arr[ppos];
72 heap->arr[ppos] = lprops;
79 * adjust_lpt_heap - move a changed heap entry up or down the heap.
80 * @c: UBIFS file-system description object
81 * @heap: LEB category heap
82 * @lprops: LEB properties to move
83 * @hpos: heap position of @lprops
86 * Changed entries in a heap are moved up or down until the parent's value is
87 * greater. In the case of LPT's category heaps, the value is either the amount
88 * of free space or the amount of dirty space, depending on the category.
90 static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
91 struct ubifs_lprops *lprops, int hpos, int cat)
93 int val1, val2, val3, cpos;
95 val1 = get_heap_comp_val(lprops, cat);
96 /* Compare to parent and, if greater than parent, move up the heap */
98 int ppos = (hpos - 1) / 2;
100 val2 = get_heap_comp_val(heap->arr[ppos], cat);
102 /* Greater than parent so move up */
104 heap->arr[ppos]->hpos = hpos;
105 heap->arr[hpos] = heap->arr[ppos];
106 heap->arr[ppos] = lprops;
111 ppos = (hpos - 1) / 2;
112 val2 = get_heap_comp_val(heap->arr[ppos], cat);
115 /* Still greater than parent so keep going */
120 /* Not greater than parent, so compare to children */
122 /* Compare to left child */
124 if (cpos >= heap->cnt)
126 val2 = get_heap_comp_val(heap->arr[cpos], cat);
128 /* Less than left child, so promote biggest child */
129 if (cpos + 1 < heap->cnt) {
130 val3 = get_heap_comp_val(heap->arr[cpos + 1],
133 cpos += 1; /* Right child is bigger */
135 heap->arr[cpos]->hpos = hpos;
136 heap->arr[hpos] = heap->arr[cpos];
137 heap->arr[cpos] = lprops;
142 /* Compare to right child */
144 if (cpos >= heap->cnt)
146 val3 = get_heap_comp_val(heap->arr[cpos], cat);
148 /* Less than right child, so promote right child */
149 heap->arr[cpos]->hpos = hpos;
150 heap->arr[hpos] = heap->arr[cpos];
151 heap->arr[cpos] = lprops;
161 * add_to_lpt_heap - add LEB properties to a LEB category heap.
162 * @c: UBIFS file-system description object
163 * @lprops: LEB properties to add
166 * This function returns %1 if @lprops is added to the heap for LEB category
167 * @cat, otherwise %0 is returned because the heap is full.
169 static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
172 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
174 if (heap->cnt >= heap->max_cnt) {
175 const int b = LPT_HEAP_SZ / 2 - 1;
176 int cpos, val1, val2;
178 /* Compare to some other LEB on the bottom of heap */
179 /* Pick a position kind of randomly */
180 cpos = (((size_t)lprops >> 4) & b) + b;
181 ubifs_assert(cpos >= b);
182 ubifs_assert(cpos < LPT_HEAP_SZ);
183 ubifs_assert(cpos < heap->cnt);
185 val1 = get_heap_comp_val(lprops, cat);
186 val2 = get_heap_comp_val(heap->arr[cpos], cat);
188 struct ubifs_lprops *lp;
190 lp = heap->arr[cpos];
191 lp->flags &= ~LPROPS_CAT_MASK;
192 lp->flags |= LPROPS_UNCAT;
193 list_add(&lp->list, &c->uncat_list);
195 heap->arr[cpos] = lprops;
196 move_up_lpt_heap(c, heap, lprops, cat);
197 dbg_check_heap(c, heap, cat, lprops->hpos);
198 return 1; /* Added to heap */
200 dbg_check_heap(c, heap, cat, -1);
201 return 0; /* Not added to heap */
203 lprops->hpos = heap->cnt++;
204 heap->arr[lprops->hpos] = lprops;
205 move_up_lpt_heap(c, heap, lprops, cat);
206 dbg_check_heap(c, heap, cat, lprops->hpos);
207 return 1; /* Added to heap */
212 * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
213 * @c: UBIFS file-system description object
214 * @lprops: LEB properties to remove
217 static void remove_from_lpt_heap(struct ubifs_info *c,
218 struct ubifs_lprops *lprops, int cat)
220 struct ubifs_lpt_heap *heap;
221 int hpos = lprops->hpos;
223 heap = &c->lpt_heap[cat - 1];
224 ubifs_assert(hpos >= 0 && hpos < heap->cnt);
225 ubifs_assert(heap->arr[hpos] == lprops);
227 if (hpos < heap->cnt) {
228 heap->arr[hpos] = heap->arr[heap->cnt];
229 heap->arr[hpos]->hpos = hpos;
230 adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
232 dbg_check_heap(c, heap, cat, -1);
236 * lpt_heap_replace - replace lprops in a category heap.
237 * @c: UBIFS file-system description object
238 * @old_lprops: LEB properties to replace
239 * @new_lprops: LEB properties with which to replace
242 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
243 * and the lprops that the pnode contains. When that happens, references in
244 * the category heaps to those lprops must be updated to point to the new
245 * lprops. This function does that.
247 static void lpt_heap_replace(struct ubifs_info *c,
248 struct ubifs_lprops *old_lprops,
249 struct ubifs_lprops *new_lprops, int cat)
251 struct ubifs_lpt_heap *heap;
252 int hpos = new_lprops->hpos;
254 heap = &c->lpt_heap[cat - 1];
255 heap->arr[hpos] = new_lprops;
259 * ubifs_add_to_cat - add LEB properties to a category list or heap.
260 * @c: UBIFS file-system description object
261 * @lprops: LEB properties to add
262 * @cat: LEB category to which to add
264 * LEB properties are categorized to enable fast find operations.
266 void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
271 case LPROPS_DIRTY_IDX:
273 if (add_to_lpt_heap(c, lprops, cat))
275 /* No more room on heap so make it un-categorized */
279 list_add(&lprops->list, &c->uncat_list);
282 list_add(&lprops->list, &c->empty_list);
284 case LPROPS_FREEABLE:
285 list_add(&lprops->list, &c->freeable_list);
286 c->freeable_cnt += 1;
288 case LPROPS_FRDI_IDX:
289 list_add(&lprops->list, &c->frdi_idx_list);
295 lprops->flags &= ~LPROPS_CAT_MASK;
296 lprops->flags |= cat;
297 c->in_a_category_cnt += 1;
298 ubifs_assert(c->in_a_category_cnt <= c->main_lebs);
302 * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
303 * @c: UBIFS file-system description object
304 * @lprops: LEB properties to remove
305 * @cat: LEB category from which to remove
307 * LEB properties are categorized to enable fast find operations.
309 static void ubifs_remove_from_cat(struct ubifs_info *c,
310 struct ubifs_lprops *lprops, int cat)
314 case LPROPS_DIRTY_IDX:
316 remove_from_lpt_heap(c, lprops, cat);
318 case LPROPS_FREEABLE:
319 c->freeable_cnt -= 1;
320 ubifs_assert(c->freeable_cnt >= 0);
324 case LPROPS_FRDI_IDX:
325 ubifs_assert(!list_empty(&lprops->list));
326 list_del(&lprops->list);
332 c->in_a_category_cnt -= 1;
333 ubifs_assert(c->in_a_category_cnt >= 0);
337 * ubifs_replace_cat - replace lprops in a category list or heap.
338 * @c: UBIFS file-system description object
339 * @old_lprops: LEB properties to replace
340 * @new_lprops: LEB properties with which to replace
342 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
343 * and the lprops that the pnode contains. When that happens, references in
344 * category lists and heaps must be replaced. This function does that.
346 void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
347 struct ubifs_lprops *new_lprops)
351 cat = new_lprops->flags & LPROPS_CAT_MASK;
354 case LPROPS_DIRTY_IDX:
356 lpt_heap_replace(c, old_lprops, new_lprops, cat);
360 case LPROPS_FREEABLE:
361 case LPROPS_FRDI_IDX:
362 list_replace(&old_lprops->list, &new_lprops->list);
370 * ubifs_ensure_cat - ensure LEB properties are categorized.
371 * @c: UBIFS file-system description object
372 * @lprops: LEB properties
374 * A LEB may have fallen off of the bottom of a heap, and ended up as
375 * un-categorized even though it has enough space for us now. If that is the
376 * case this function will put the LEB back onto a heap.
378 void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
380 int cat = lprops->flags & LPROPS_CAT_MASK;
382 if (cat != LPROPS_UNCAT)
384 cat = ubifs_categorize_lprops(c, lprops);
385 if (cat == LPROPS_UNCAT)
387 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
388 ubifs_add_to_cat(c, lprops, cat);
392 * ubifs_categorize_lprops - categorize LEB properties.
393 * @c: UBIFS file-system description object
394 * @lprops: LEB properties to categorize
396 * LEB properties are categorized to enable fast find operations. This function
397 * returns the LEB category to which the LEB properties belong. Note however
398 * that if the LEB category is stored as a heap and the heap is full, the
399 * LEB properties may have their category changed to %LPROPS_UNCAT.
401 int ubifs_categorize_lprops(const struct ubifs_info *c,
402 const struct ubifs_lprops *lprops)
404 if (lprops->flags & LPROPS_TAKEN)
407 if (lprops->free == c->leb_size) {
408 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
412 if (lprops->free + lprops->dirty == c->leb_size) {
413 if (lprops->flags & LPROPS_INDEX)
414 return LPROPS_FRDI_IDX;
416 return LPROPS_FREEABLE;
419 if (lprops->flags & LPROPS_INDEX) {
420 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
421 return LPROPS_DIRTY_IDX;
423 if (lprops->dirty >= c->dead_wm &&
424 lprops->dirty > lprops->free)
426 if (lprops->free > 0)
434 * change_category - change LEB properties category.
435 * @c: UBIFS file-system description object
436 * @lprops: LEB properties to re-categorize
438 * LEB properties are categorized to enable fast find operations. When the LEB
439 * properties change they must be re-categorized.
441 static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
443 int old_cat = lprops->flags & LPROPS_CAT_MASK;
444 int new_cat = ubifs_categorize_lprops(c, lprops);
446 if (old_cat == new_cat) {
447 struct ubifs_lpt_heap *heap;
449 /* lprops on a heap now must be moved up or down */
450 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
451 return; /* Not on a heap */
452 heap = &c->lpt_heap[new_cat - 1];
453 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
455 ubifs_remove_from_cat(c, lprops, old_cat);
456 ubifs_add_to_cat(c, lprops, new_cat);
461 * ubifs_calc_dark - calculate LEB dark space size.
462 * @c: the UBIFS file-system description object
463 * @spc: amount of free and dirty space in the LEB
465 * This function calculates and returns amount of dark space in an LEB which
466 * has @spc bytes of free and dirty space.
468 * UBIFS is trying to account the space which might not be usable, and this
469 * space is called "dark space". For example, if an LEB has only %512 free
470 * bytes, it is dark space, because it cannot fit a large data node.
472 int ubifs_calc_dark(const struct ubifs_info *c, int spc)
474 ubifs_assert(!(spc & 7));
476 if (spc < c->dark_wm)
480 * If we have slightly more space then the dark space watermark, we can
481 * anyway safely assume it we'll be able to write a node of the
482 * smallest size there.
484 if (spc - c->dark_wm < MIN_WRITE_SZ)
485 return spc - MIN_WRITE_SZ;
491 * is_lprops_dirty - determine if LEB properties are dirty.
492 * @c: the UBIFS file-system description object
493 * @lprops: LEB properties to test
495 static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
497 struct ubifs_pnode *pnode;
500 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
501 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
504 return !test_bit(COW_CNODE, &pnode->flags) &&
505 test_bit(DIRTY_CNODE, &pnode->flags);
509 * ubifs_change_lp - change LEB properties.
510 * @c: the UBIFS file-system description object
511 * @lp: LEB properties to change
512 * @free: new free space amount
513 * @dirty: new dirty space amount
515 * @idx_gc_cnt: change to the count of @idx_gc list
517 * This function changes LEB properties (@free, @dirty or @flag). However, the
518 * property which has the %LPROPS_NC value is not changed. Returns a pointer to
519 * the updated LEB properties on success and a negative error code on failure.
521 * Note, the LEB properties may have had to be copied (due to COW) and
522 * consequently the pointer returned may not be the same as the pointer
525 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
526 const struct ubifs_lprops *lp,
527 int free, int dirty, int flags,
531 * This is the only function that is allowed to change lprops, so we
532 * discard the "const" qualifier.
534 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
536 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
537 lprops->lnum, free, dirty, flags);
539 ubifs_assert(mutex_is_locked(&c->lp_mutex));
540 ubifs_assert(c->lst.empty_lebs >= 0 &&
541 c->lst.empty_lebs <= c->main_lebs);
542 ubifs_assert(c->freeable_cnt >= 0);
543 ubifs_assert(c->freeable_cnt <= c->main_lebs);
544 ubifs_assert(c->lst.taken_empty_lebs >= 0);
545 ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
546 ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
547 ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
548 ubifs_assert(!(c->lst.total_used & 7));
549 ubifs_assert(free == LPROPS_NC || free >= 0);
550 ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
552 if (!is_lprops_dirty(c, lprops)) {
553 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
557 ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
559 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
561 spin_lock(&c->space_lock);
562 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
563 c->lst.taken_empty_lebs -= 1;
565 if (!(lprops->flags & LPROPS_INDEX)) {
568 old_spc = lprops->free + lprops->dirty;
569 if (old_spc < c->dead_wm)
570 c->lst.total_dead -= old_spc;
572 c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
574 c->lst.total_used -= c->leb_size - old_spc;
577 if (free != LPROPS_NC) {
578 free = ALIGN(free, 8);
579 c->lst.total_free += free - lprops->free;
581 /* Increase or decrease empty LEBs counter if needed */
582 if (free == c->leb_size) {
583 if (lprops->free != c->leb_size)
584 c->lst.empty_lebs += 1;
585 } else if (lprops->free == c->leb_size)
586 c->lst.empty_lebs -= 1;
590 if (dirty != LPROPS_NC) {
591 dirty = ALIGN(dirty, 8);
592 c->lst.total_dirty += dirty - lprops->dirty;
593 lprops->dirty = dirty;
596 if (flags != LPROPS_NC) {
597 /* Take care about indexing LEBs counter if needed */
598 if ((lprops->flags & LPROPS_INDEX)) {
599 if (!(flags & LPROPS_INDEX))
600 c->lst.idx_lebs -= 1;
601 } else if (flags & LPROPS_INDEX)
602 c->lst.idx_lebs += 1;
603 lprops->flags = flags;
606 if (!(lprops->flags & LPROPS_INDEX)) {
609 new_spc = lprops->free + lprops->dirty;
610 if (new_spc < c->dead_wm)
611 c->lst.total_dead += new_spc;
613 c->lst.total_dark += ubifs_calc_dark(c, new_spc);
615 c->lst.total_used += c->leb_size - new_spc;
618 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
619 c->lst.taken_empty_lebs += 1;
621 change_category(c, lprops);
622 c->idx_gc_cnt += idx_gc_cnt;
623 spin_unlock(&c->space_lock);
628 * ubifs_get_lp_stats - get lprops statistics.
629 * @c: UBIFS file-system description object
630 * @st: return statistics
632 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
634 spin_lock(&c->space_lock);
635 memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
636 spin_unlock(&c->space_lock);
640 * ubifs_change_one_lp - change LEB properties.
641 * @c: the UBIFS file-system description object
642 * @lnum: LEB to change properties for
643 * @free: amount of free space
644 * @dirty: amount of dirty space
645 * @flags_set: flags to set
646 * @flags_clean: flags to clean
647 * @idx_gc_cnt: change to the count of idx_gc list
649 * This function changes properties of LEB @lnum. It is a helper wrapper over
650 * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
651 * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
652 * a negative error code in case of failure.
654 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
655 int flags_set, int flags_clean, int idx_gc_cnt)
658 const struct ubifs_lprops *lp;
662 lp = ubifs_lpt_lookup_dirty(c, lnum);
668 flags = (lp->flags | flags_set) & ~flags_clean;
669 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
674 ubifs_release_lprops(c);
676 ubifs_err(c, "cannot change properties of LEB %d, error %d",
682 * ubifs_update_one_lp - update LEB properties.
683 * @c: the UBIFS file-system description object
684 * @lnum: LEB to change properties for
685 * @free: amount of free space
686 * @dirty: amount of dirty space to add
687 * @flags_set: flags to set
688 * @flags_clean: flags to clean
690 * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
691 * current dirty space, not substitutes it.
693 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
694 int flags_set, int flags_clean)
697 const struct ubifs_lprops *lp;
701 lp = ubifs_lpt_lookup_dirty(c, lnum);
707 flags = (lp->flags | flags_set) & ~flags_clean;
708 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
713 ubifs_release_lprops(c);
715 ubifs_err(c, "cannot update properties of LEB %d, error %d",
721 * ubifs_read_one_lp - read LEB properties.
722 * @c: the UBIFS file-system description object
723 * @lnum: LEB to read properties for
724 * @lp: where to store read properties
726 * This helper function reads properties of a LEB @lnum and stores them in @lp.
727 * Returns zero in case of success and a negative error code in case of
730 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
733 const struct ubifs_lprops *lpp;
737 lpp = ubifs_lpt_lookup(c, lnum);
740 ubifs_err(c, "cannot read properties of LEB %d, error %d",
745 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
748 ubifs_release_lprops(c);
753 * ubifs_fast_find_free - try to find a LEB with free space quickly.
754 * @c: the UBIFS file-system description object
756 * This function returns LEB properties for a LEB with free space or %NULL if
757 * the function is unable to find a LEB quickly.
759 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
761 struct ubifs_lprops *lprops;
762 struct ubifs_lpt_heap *heap;
764 ubifs_assert(mutex_is_locked(&c->lp_mutex));
766 heap = &c->lpt_heap[LPROPS_FREE - 1];
770 lprops = heap->arr[0];
771 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
772 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
777 * ubifs_fast_find_empty - try to find an empty LEB quickly.
778 * @c: the UBIFS file-system description object
780 * This function returns LEB properties for an empty LEB or %NULL if the
781 * function is unable to find an empty LEB quickly.
783 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
785 struct ubifs_lprops *lprops;
787 ubifs_assert(mutex_is_locked(&c->lp_mutex));
789 if (list_empty(&c->empty_list))
792 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
793 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
794 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
795 ubifs_assert(lprops->free == c->leb_size);
800 * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
801 * @c: the UBIFS file-system description object
803 * This function returns LEB properties for a freeable LEB or %NULL if the
804 * function is unable to find a freeable LEB quickly.
806 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
808 struct ubifs_lprops *lprops;
810 ubifs_assert(mutex_is_locked(&c->lp_mutex));
812 if (list_empty(&c->freeable_list))
815 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
816 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
817 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
818 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
819 ubifs_assert(c->freeable_cnt > 0);
824 * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
825 * @c: the UBIFS file-system description object
827 * This function returns LEB properties for a freeable index LEB or %NULL if the
828 * function is unable to find a freeable index LEB quickly.
830 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
832 struct ubifs_lprops *lprops;
834 ubifs_assert(mutex_is_locked(&c->lp_mutex));
836 if (list_empty(&c->frdi_idx_list))
839 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
840 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
841 ubifs_assert((lprops->flags & LPROPS_INDEX));
842 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
847 * Everything below is related to debugging.
851 * dbg_check_cats - check category heaps and lists.
852 * @c: UBIFS file-system description object
854 * This function returns %0 on success and a negative error code on failure.
856 int dbg_check_cats(struct ubifs_info *c)
858 struct ubifs_lprops *lprops;
859 struct list_head *pos;
862 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
865 list_for_each_entry(lprops, &c->empty_list, list) {
866 if (lprops->free != c->leb_size) {
867 ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
868 lprops->lnum, lprops->free, lprops->dirty,
872 if (lprops->flags & LPROPS_TAKEN) {
873 ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
874 lprops->lnum, lprops->free, lprops->dirty,
881 list_for_each_entry(lprops, &c->freeable_list, list) {
882 if (lprops->free + lprops->dirty != c->leb_size) {
883 ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
884 lprops->lnum, lprops->free, lprops->dirty,
888 if (lprops->flags & LPROPS_TAKEN) {
889 ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
890 lprops->lnum, lprops->free, lprops->dirty,
896 if (i != c->freeable_cnt) {
897 ubifs_err(c, "freeable list count %d expected %d", i,
903 list_for_each(pos, &c->idx_gc)
905 if (i != c->idx_gc_cnt) {
906 ubifs_err(c, "idx_gc list count %d expected %d", i,
911 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
912 if (lprops->free + lprops->dirty != c->leb_size) {
913 ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
914 lprops->lnum, lprops->free, lprops->dirty,
918 if (lprops->flags & LPROPS_TAKEN) {
919 ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
920 lprops->lnum, lprops->free, lprops->dirty,
924 if (!(lprops->flags & LPROPS_INDEX)) {
925 ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
926 lprops->lnum, lprops->free, lprops->dirty,
932 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
933 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
935 for (i = 0; i < heap->cnt; i++) {
936 lprops = heap->arr[i];
938 ubifs_err(c, "null ptr in LPT heap cat %d", cat);
941 if (lprops->hpos != i) {
942 ubifs_err(c, "bad ptr in LPT heap cat %d", cat);
945 if (lprops->flags & LPROPS_TAKEN) {
946 ubifs_err(c, "taken LEB in LPT heap cat %d", cat);
955 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
958 int i = 0, j, err = 0;
960 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
963 for (i = 0; i < heap->cnt; i++) {
964 struct ubifs_lprops *lprops = heap->arr[i];
965 struct ubifs_lprops *lp;
968 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
972 if (lprops->hpos != i) {
976 lp = ubifs_lpt_lookup(c, lprops->lnum);
982 ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
983 (size_t)lprops, (size_t)lp, lprops->lnum,
988 for (j = 0; j < i; j++) {
994 if (lp->lnum == lprops->lnum) {
1002 ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err);
1004 ubifs_dump_heap(c, heap, cat);
1009 * scan_check_cb - scan callback.
1010 * @c: the UBIFS file-system description object
1011 * @lp: LEB properties to scan
1012 * @in_tree: whether the LEB properties are in main memory
1013 * @lst: lprops statistics to update
1015 * This function returns a code that indicates whether the scan should continue
1016 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1017 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1020 static int scan_check_cb(struct ubifs_info *c,
1021 const struct ubifs_lprops *lp, int in_tree,
1022 struct ubifs_lp_stats *lst)
1024 struct ubifs_scan_leb *sleb;
1025 struct ubifs_scan_node *snod;
1026 int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret;
1029 cat = lp->flags & LPROPS_CAT_MASK;
1030 if (cat != LPROPS_UNCAT) {
1031 cat = ubifs_categorize_lprops(c, lp);
1032 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1033 ubifs_err(c, "bad LEB category %d expected %d",
1034 (lp->flags & LPROPS_CAT_MASK), cat);
1039 /* Check lp is on its category list (if it has one) */
1041 struct list_head *list = NULL;
1045 list = &c->empty_list;
1047 case LPROPS_FREEABLE:
1048 list = &c->freeable_list;
1050 case LPROPS_FRDI_IDX:
1051 list = &c->frdi_idx_list;
1054 list = &c->uncat_list;
1058 struct ubifs_lprops *lprops;
1061 list_for_each_entry(lprops, list, list) {
1068 ubifs_err(c, "bad LPT list (category %d)", cat);
1074 /* Check lp is on its category heap (if it has one) */
1075 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1076 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1078 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1079 lp != heap->arr[lp->hpos]) {
1080 ubifs_err(c, "bad LPT heap (category %d)", cat);
1085 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1090 * After an unclean unmount, empty and freeable LEBs
1091 * may contain garbage - do not scan them.
1093 if (lp->free == c->leb_size) {
1094 lst->empty_lebs += 1;
1095 lst->total_free += c->leb_size;
1096 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1097 return LPT_SCAN_CONTINUE;
1099 if (lp->free + lp->dirty == c->leb_size &&
1100 !(lp->flags & LPROPS_INDEX)) {
1101 lst->total_free += lp->free;
1102 lst->total_dirty += lp->dirty;
1103 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1104 return LPT_SCAN_CONTINUE;
1107 sleb = ubifs_scan(c, lnum, 0, buf, 0);
1109 ret = PTR_ERR(sleb);
1110 if (ret == -EUCLEAN) {
1111 ubifs_dump_lprops(c);
1112 ubifs_dump_budg(c, &c->bi);
1118 list_for_each_entry(snod, &sleb->nodes, list) {
1119 int found, level = 0;
1124 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1126 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1127 ubifs_err(c, "indexing node in data LEB %d:%d",
1132 if (snod->type == UBIFS_IDX_NODE) {
1133 struct ubifs_idx_node *idx = snod->node;
1135 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1136 level = le16_to_cpu(idx->level);
1139 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1140 snod->offs, is_idx);
1144 used += ALIGN(snod->len, 8);
1148 free = c->leb_size - sleb->endpt;
1149 dirty = sleb->endpt - used;
1151 if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1153 ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d",
1158 if (lp->free + lp->dirty == c->leb_size &&
1159 free + dirty == c->leb_size)
1160 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1161 (!is_idx && free == c->leb_size) ||
1162 lp->free == c->leb_size) {
1164 * Empty or freeable LEBs could contain index
1165 * nodes from an uncompleted commit due to an
1166 * unclean unmount. Or they could be empty for
1167 * the same reason. Or it may simply not have been
1175 if (is_idx && lp->free + lp->dirty == free + dirty &&
1176 lnum != c->ihead_lnum) {
1178 * After an unclean unmount, an index LEB could have a different
1179 * amount of free space than the value recorded by lprops. That
1180 * is because the in-the-gaps method may use free space or
1181 * create free space (as a side-effect of using ubi_leb_change
1182 * and not writing the whole LEB). The incorrect free space
1183 * value is not a problem because the index is only ever
1184 * allocated empty LEBs, so there will never be an attempt to
1185 * write to the free space at the end of an index LEB - except
1186 * by the in-the-gaps method for which it is not a problem.
1192 if (lp->free != free || lp->dirty != dirty)
1195 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1196 if (free == c->leb_size)
1197 /* Free but not unmapped LEB, it's fine */
1200 ubifs_err(c, "indexing node without indexing flag");
1205 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1206 ubifs_err(c, "data node with indexing flag");
1210 if (free == c->leb_size)
1211 lst->empty_lebs += 1;
1216 if (!(lp->flags & LPROPS_INDEX))
1217 lst->total_used += c->leb_size - free - dirty;
1218 lst->total_free += free;
1219 lst->total_dirty += dirty;
1221 if (!(lp->flags & LPROPS_INDEX)) {
1222 int spc = free + dirty;
1224 if (spc < c->dead_wm)
1225 lst->total_dead += spc;
1227 lst->total_dark += ubifs_calc_dark(c, spc);
1230 ubifs_scan_destroy(sleb);
1232 return LPT_SCAN_CONTINUE;
1235 ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1236 lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1237 ubifs_dump_leb(c, lnum);
1239 ubifs_scan_destroy(sleb);
1247 * dbg_check_lprops - check all LEB properties.
1248 * @c: UBIFS file-system description object
1250 * This function checks all LEB properties and makes sure they are all correct.
1251 * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1252 * and other negative error codes in case of other errors. This function is
1253 * called while the file system is locked (because of commit start), so no
1254 * additional locking is required. Note that locking the LPT mutex would cause
1255 * a circular lock dependency with the TNC mutex.
1257 int dbg_check_lprops(struct ubifs_info *c)
1260 struct ubifs_lp_stats lst;
1262 if (!dbg_is_chk_lprops(c))
1266 * As we are going to scan the media, the write buffers have to be
1269 for (i = 0; i < c->jhead_cnt; i++) {
1270 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1275 memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1276 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1277 (ubifs_lpt_scan_callback)scan_check_cb,
1279 if (err && err != -ENOSPC)
1282 if (lst.empty_lebs != c->lst.empty_lebs ||
1283 lst.idx_lebs != c->lst.idx_lebs ||
1284 lst.total_free != c->lst.total_free ||
1285 lst.total_dirty != c->lst.total_dirty ||
1286 lst.total_used != c->lst.total_used) {
1287 ubifs_err(c, "bad overall accounting");
1288 ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1289 lst.empty_lebs, lst.idx_lebs, lst.total_free,
1290 lst.total_dirty, lst.total_used);
1291 ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1292 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1293 c->lst.total_dirty, c->lst.total_used);
1298 if (lst.total_dead != c->lst.total_dead ||
1299 lst.total_dark != c->lst.total_dark) {
1300 ubifs_err(c, "bad dead/dark space accounting");
1301 ubifs_err(c, "calculated: total_dead %lld, total_dark %lld",
1302 lst.total_dead, lst.total_dark);
1303 ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld",
1304 c->lst.total_dead, c->lst.total_dark);
1309 err = dbg_check_cats(c);