Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Authors: Adrian Hunter
0008  *          Artem Bityutskiy (Битюцкий Артём)
0009  */
0010 
0011 /*
0012  * This file implements the functions that access LEB properties and their
0013  * categories. LEBs are categorized based on the needs of UBIFS, and the
0014  * categories are stored as either heaps or lists to provide a fast way of
0015  * finding a LEB in a particular category. For example, UBIFS may need to find
0016  * an empty LEB for the journal, or a very dirty LEB for garbage collection.
0017  */
0018 
0019 #include "ubifs.h"
0020 
0021 /**
0022  * get_heap_comp_val - get the LEB properties value for heap comparisons.
0023  * @lprops: LEB properties
0024  * @cat: LEB category
0025  */
0026 static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
0027 {
0028     switch (cat) {
0029     case LPROPS_FREE:
0030         return lprops->free;
0031     case LPROPS_DIRTY_IDX:
0032         return lprops->free + lprops->dirty;
0033     default:
0034         return lprops->dirty;
0035     }
0036 }
0037 
0038 /**
0039  * move_up_lpt_heap - move a new heap entry up as far as possible.
0040  * @c: UBIFS file-system description object
0041  * @heap: LEB category heap
0042  * @lprops: LEB properties to move
0043  * @cat: LEB category
0044  *
0045  * New entries to a heap are added at the bottom and then moved up until the
0046  * parent's value is greater.  In the case of LPT's category heaps, the value
0047  * is either the amount of free space or the amount of dirty space, depending
0048  * on the category.
0049  */
0050 static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
0051                  struct ubifs_lprops *lprops, int cat)
0052 {
0053     int val1, val2, hpos;
0054 
0055     hpos = lprops->hpos;
0056     if (!hpos)
0057         return; /* Already top of the heap */
0058     val1 = get_heap_comp_val(lprops, cat);
0059     /* Compare to parent and, if greater, move up the heap */
0060     do {
0061         int ppos = (hpos - 1) / 2;
0062 
0063         val2 = get_heap_comp_val(heap->arr[ppos], cat);
0064         if (val2 >= val1)
0065             return;
0066         /* Greater than parent so move up */
0067         heap->arr[ppos]->hpos = hpos;
0068         heap->arr[hpos] = heap->arr[ppos];
0069         heap->arr[ppos] = lprops;
0070         lprops->hpos = ppos;
0071         hpos = ppos;
0072     } while (hpos);
0073 }
0074 
0075 /**
0076  * adjust_lpt_heap - move a changed heap entry up or down the heap.
0077  * @c: UBIFS file-system description object
0078  * @heap: LEB category heap
0079  * @lprops: LEB properties to move
0080  * @hpos: heap position of @lprops
0081  * @cat: LEB category
0082  *
0083  * Changed entries in a heap are moved up or down until the parent's value is
0084  * greater.  In the case of LPT's category heaps, the value is either the amount
0085  * of free space or the amount of dirty space, depending on the category.
0086  */
0087 static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
0088                 struct ubifs_lprops *lprops, int hpos, int cat)
0089 {
0090     int val1, val2, val3, cpos;
0091 
0092     val1 = get_heap_comp_val(lprops, cat);
0093     /* Compare to parent and, if greater than parent, move up the heap */
0094     if (hpos) {
0095         int ppos = (hpos - 1) / 2;
0096 
0097         val2 = get_heap_comp_val(heap->arr[ppos], cat);
0098         if (val1 > val2) {
0099             /* Greater than parent so move up */
0100             while (1) {
0101                 heap->arr[ppos]->hpos = hpos;
0102                 heap->arr[hpos] = heap->arr[ppos];
0103                 heap->arr[ppos] = lprops;
0104                 lprops->hpos = ppos;
0105                 hpos = ppos;
0106                 if (!hpos)
0107                     return;
0108                 ppos = (hpos - 1) / 2;
0109                 val2 = get_heap_comp_val(heap->arr[ppos], cat);
0110                 if (val1 <= val2)
0111                     return;
0112                 /* Still greater than parent so keep going */
0113             }
0114         }
0115     }
0116 
0117     /* Not greater than parent, so compare to children */
0118     while (1) {
0119         /* Compare to left child */
0120         cpos = hpos * 2 + 1;
0121         if (cpos >= heap->cnt)
0122             return;
0123         val2 = get_heap_comp_val(heap->arr[cpos], cat);
0124         if (val1 < val2) {
0125             /* Less than left child, so promote biggest child */
0126             if (cpos + 1 < heap->cnt) {
0127                 val3 = get_heap_comp_val(heap->arr[cpos + 1],
0128                              cat);
0129                 if (val3 > val2)
0130                     cpos += 1; /* Right child is bigger */
0131             }
0132             heap->arr[cpos]->hpos = hpos;
0133             heap->arr[hpos] = heap->arr[cpos];
0134             heap->arr[cpos] = lprops;
0135             lprops->hpos = cpos;
0136             hpos = cpos;
0137             continue;
0138         }
0139         /* Compare to right child */
0140         cpos += 1;
0141         if (cpos >= heap->cnt)
0142             return;
0143         val3 = get_heap_comp_val(heap->arr[cpos], cat);
0144         if (val1 < val3) {
0145             /* Less than right child, so promote right child */
0146             heap->arr[cpos]->hpos = hpos;
0147             heap->arr[hpos] = heap->arr[cpos];
0148             heap->arr[cpos] = lprops;
0149             lprops->hpos = cpos;
0150             hpos = cpos;
0151             continue;
0152         }
0153         return;
0154     }
0155 }
0156 
0157 /**
0158  * add_to_lpt_heap - add LEB properties to a LEB category heap.
0159  * @c: UBIFS file-system description object
0160  * @lprops: LEB properties to add
0161  * @cat: LEB category
0162  *
0163  * This function returns %1 if @lprops is added to the heap for LEB category
0164  * @cat, otherwise %0 is returned because the heap is full.
0165  */
0166 static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
0167                int cat)
0168 {
0169     struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
0170 
0171     if (heap->cnt >= heap->max_cnt) {
0172         const int b = LPT_HEAP_SZ / 2 - 1;
0173         int cpos, val1, val2;
0174 
0175         /* Compare to some other LEB on the bottom of heap */
0176         /* Pick a position kind of randomly */
0177         cpos = (((size_t)lprops >> 4) & b) + b;
0178         ubifs_assert(c, cpos >= b);
0179         ubifs_assert(c, cpos < LPT_HEAP_SZ);
0180         ubifs_assert(c, cpos < heap->cnt);
0181 
0182         val1 = get_heap_comp_val(lprops, cat);
0183         val2 = get_heap_comp_val(heap->arr[cpos], cat);
0184         if (val1 > val2) {
0185             struct ubifs_lprops *lp;
0186 
0187             lp = heap->arr[cpos];
0188             lp->flags &= ~LPROPS_CAT_MASK;
0189             lp->flags |= LPROPS_UNCAT;
0190             list_add(&lp->list, &c->uncat_list);
0191             lprops->hpos = cpos;
0192             heap->arr[cpos] = lprops;
0193             move_up_lpt_heap(c, heap, lprops, cat);
0194             dbg_check_heap(c, heap, cat, lprops->hpos);
0195             return 1; /* Added to heap */
0196         }
0197         dbg_check_heap(c, heap, cat, -1);
0198         return 0; /* Not added to heap */
0199     } else {
0200         lprops->hpos = heap->cnt++;
0201         heap->arr[lprops->hpos] = lprops;
0202         move_up_lpt_heap(c, heap, lprops, cat);
0203         dbg_check_heap(c, heap, cat, lprops->hpos);
0204         return 1; /* Added to heap */
0205     }
0206 }
0207 
0208 /**
0209  * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
0210  * @c: UBIFS file-system description object
0211  * @lprops: LEB properties to remove
0212  * @cat: LEB category
0213  */
0214 static void remove_from_lpt_heap(struct ubifs_info *c,
0215                  struct ubifs_lprops *lprops, int cat)
0216 {
0217     struct ubifs_lpt_heap *heap;
0218     int hpos = lprops->hpos;
0219 
0220     heap = &c->lpt_heap[cat - 1];
0221     ubifs_assert(c, hpos >= 0 && hpos < heap->cnt);
0222     ubifs_assert(c, heap->arr[hpos] == lprops);
0223     heap->cnt -= 1;
0224     if (hpos < heap->cnt) {
0225         heap->arr[hpos] = heap->arr[heap->cnt];
0226         heap->arr[hpos]->hpos = hpos;
0227         adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
0228     }
0229     dbg_check_heap(c, heap, cat, -1);
0230 }
0231 
0232 /**
0233  * lpt_heap_replace - replace lprops in a category heap.
0234  * @c: UBIFS file-system description object
0235  * @new_lprops: LEB properties with which to replace
0236  * @cat: LEB category
0237  *
0238  * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
0239  * and the lprops that the pnode contains.  When that happens, references in
0240  * the category heaps to those lprops must be updated to point to the new
0241  * lprops.  This function does that.
0242  */
0243 static void lpt_heap_replace(struct ubifs_info *c,
0244                  struct ubifs_lprops *new_lprops, int cat)
0245 {
0246     struct ubifs_lpt_heap *heap;
0247     int hpos = new_lprops->hpos;
0248 
0249     heap = &c->lpt_heap[cat - 1];
0250     heap->arr[hpos] = new_lprops;
0251 }
0252 
0253 /**
0254  * ubifs_add_to_cat - add LEB properties to a category list or heap.
0255  * @c: UBIFS file-system description object
0256  * @lprops: LEB properties to add
0257  * @cat: LEB category to which to add
0258  *
0259  * LEB properties are categorized to enable fast find operations.
0260  */
0261 void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
0262               int cat)
0263 {
0264     switch (cat) {
0265     case LPROPS_DIRTY:
0266     case LPROPS_DIRTY_IDX:
0267     case LPROPS_FREE:
0268         if (add_to_lpt_heap(c, lprops, cat))
0269             break;
0270         /* No more room on heap so make it un-categorized */
0271         cat = LPROPS_UNCAT;
0272         fallthrough;
0273     case LPROPS_UNCAT:
0274         list_add(&lprops->list, &c->uncat_list);
0275         break;
0276     case LPROPS_EMPTY:
0277         list_add(&lprops->list, &c->empty_list);
0278         break;
0279     case LPROPS_FREEABLE:
0280         list_add(&lprops->list, &c->freeable_list);
0281         c->freeable_cnt += 1;
0282         break;
0283     case LPROPS_FRDI_IDX:
0284         list_add(&lprops->list, &c->frdi_idx_list);
0285         break;
0286     default:
0287         ubifs_assert(c, 0);
0288     }
0289 
0290     lprops->flags &= ~LPROPS_CAT_MASK;
0291     lprops->flags |= cat;
0292     c->in_a_category_cnt += 1;
0293     ubifs_assert(c, c->in_a_category_cnt <= c->main_lebs);
0294 }
0295 
0296 /**
0297  * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
0298  * @c: UBIFS file-system description object
0299  * @lprops: LEB properties to remove
0300  * @cat: LEB category from which to remove
0301  *
0302  * LEB properties are categorized to enable fast find operations.
0303  */
0304 static void ubifs_remove_from_cat(struct ubifs_info *c,
0305                   struct ubifs_lprops *lprops, int cat)
0306 {
0307     switch (cat) {
0308     case LPROPS_DIRTY:
0309     case LPROPS_DIRTY_IDX:
0310     case LPROPS_FREE:
0311         remove_from_lpt_heap(c, lprops, cat);
0312         break;
0313     case LPROPS_FREEABLE:
0314         c->freeable_cnt -= 1;
0315         ubifs_assert(c, c->freeable_cnt >= 0);
0316         fallthrough;
0317     case LPROPS_UNCAT:
0318     case LPROPS_EMPTY:
0319     case LPROPS_FRDI_IDX:
0320         ubifs_assert(c, !list_empty(&lprops->list));
0321         list_del(&lprops->list);
0322         break;
0323     default:
0324         ubifs_assert(c, 0);
0325     }
0326 
0327     c->in_a_category_cnt -= 1;
0328     ubifs_assert(c, c->in_a_category_cnt >= 0);
0329 }
0330 
0331 /**
0332  * ubifs_replace_cat - replace lprops in a category list or heap.
0333  * @c: UBIFS file-system description object
0334  * @old_lprops: LEB properties to replace
0335  * @new_lprops: LEB properties with which to replace
0336  *
0337  * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
0338  * and the lprops that the pnode contains. When that happens, references in
0339  * category lists and heaps must be replaced. This function does that.
0340  */
0341 void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
0342                struct ubifs_lprops *new_lprops)
0343 {
0344     int cat;
0345 
0346     cat = new_lprops->flags & LPROPS_CAT_MASK;
0347     switch (cat) {
0348     case LPROPS_DIRTY:
0349     case LPROPS_DIRTY_IDX:
0350     case LPROPS_FREE:
0351         lpt_heap_replace(c, new_lprops, cat);
0352         break;
0353     case LPROPS_UNCAT:
0354     case LPROPS_EMPTY:
0355     case LPROPS_FREEABLE:
0356     case LPROPS_FRDI_IDX:
0357         list_replace(&old_lprops->list, &new_lprops->list);
0358         break;
0359     default:
0360         ubifs_assert(c, 0);
0361     }
0362 }
0363 
0364 /**
0365  * ubifs_ensure_cat - ensure LEB properties are categorized.
0366  * @c: UBIFS file-system description object
0367  * @lprops: LEB properties
0368  *
0369  * A LEB may have fallen off of the bottom of a heap, and ended up as
0370  * un-categorized even though it has enough space for us now. If that is the
0371  * case this function will put the LEB back onto a heap.
0372  */
0373 void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
0374 {
0375     int cat = lprops->flags & LPROPS_CAT_MASK;
0376 
0377     if (cat != LPROPS_UNCAT)
0378         return;
0379     cat = ubifs_categorize_lprops(c, lprops);
0380     if (cat == LPROPS_UNCAT)
0381         return;
0382     ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
0383     ubifs_add_to_cat(c, lprops, cat);
0384 }
0385 
0386 /**
0387  * ubifs_categorize_lprops - categorize LEB properties.
0388  * @c: UBIFS file-system description object
0389  * @lprops: LEB properties to categorize
0390  *
0391  * LEB properties are categorized to enable fast find operations. This function
0392  * returns the LEB category to which the LEB properties belong. Note however
0393  * that if the LEB category is stored as a heap and the heap is full, the
0394  * LEB properties may have their category changed to %LPROPS_UNCAT.
0395  */
0396 int ubifs_categorize_lprops(const struct ubifs_info *c,
0397                 const struct ubifs_lprops *lprops)
0398 {
0399     if (lprops->flags & LPROPS_TAKEN)
0400         return LPROPS_UNCAT;
0401 
0402     if (lprops->free == c->leb_size) {
0403         ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0404         return LPROPS_EMPTY;
0405     }
0406 
0407     if (lprops->free + lprops->dirty == c->leb_size) {
0408         if (lprops->flags & LPROPS_INDEX)
0409             return LPROPS_FRDI_IDX;
0410         else
0411             return LPROPS_FREEABLE;
0412     }
0413 
0414     if (lprops->flags & LPROPS_INDEX) {
0415         if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
0416             return LPROPS_DIRTY_IDX;
0417     } else {
0418         if (lprops->dirty >= c->dead_wm &&
0419             lprops->dirty > lprops->free)
0420             return LPROPS_DIRTY;
0421         if (lprops->free > 0)
0422             return LPROPS_FREE;
0423     }
0424 
0425     return LPROPS_UNCAT;
0426 }
0427 
0428 /**
0429  * change_category - change LEB properties category.
0430  * @c: UBIFS file-system description object
0431  * @lprops: LEB properties to re-categorize
0432  *
0433  * LEB properties are categorized to enable fast find operations. When the LEB
0434  * properties change they must be re-categorized.
0435  */
0436 static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
0437 {
0438     int old_cat = lprops->flags & LPROPS_CAT_MASK;
0439     int new_cat = ubifs_categorize_lprops(c, lprops);
0440 
0441     if (old_cat == new_cat) {
0442         struct ubifs_lpt_heap *heap;
0443 
0444         /* lprops on a heap now must be moved up or down */
0445         if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
0446             return; /* Not on a heap */
0447         heap = &c->lpt_heap[new_cat - 1];
0448         adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
0449     } else {
0450         ubifs_remove_from_cat(c, lprops, old_cat);
0451         ubifs_add_to_cat(c, lprops, new_cat);
0452     }
0453 }
0454 
0455 /**
0456  * ubifs_calc_dark - calculate LEB dark space size.
0457  * @c: the UBIFS file-system description object
0458  * @spc: amount of free and dirty space in the LEB
0459  *
0460  * This function calculates and returns amount of dark space in an LEB which
0461  * has @spc bytes of free and dirty space.
0462  *
0463  * UBIFS is trying to account the space which might not be usable, and this
0464  * space is called "dark space". For example, if an LEB has only %512 free
0465  * bytes, it is dark space, because it cannot fit a large data node.
0466  */
0467 int ubifs_calc_dark(const struct ubifs_info *c, int spc)
0468 {
0469     ubifs_assert(c, !(spc & 7));
0470 
0471     if (spc < c->dark_wm)
0472         return spc;
0473 
0474     /*
0475      * If we have slightly more space then the dark space watermark, we can
0476      * anyway safely assume it we'll be able to write a node of the
0477      * smallest size there.
0478      */
0479     if (spc - c->dark_wm < MIN_WRITE_SZ)
0480         return spc - MIN_WRITE_SZ;
0481 
0482     return c->dark_wm;
0483 }
0484 
0485 /**
0486  * is_lprops_dirty - determine if LEB properties are dirty.
0487  * @c: the UBIFS file-system description object
0488  * @lprops: LEB properties to test
0489  */
0490 static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
0491 {
0492     struct ubifs_pnode *pnode;
0493     int pos;
0494 
0495     pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
0496     pnode = (struct ubifs_pnode *)container_of(lprops - pos,
0497                            struct ubifs_pnode,
0498                            lprops[0]);
0499     return !test_bit(COW_CNODE, &pnode->flags) &&
0500            test_bit(DIRTY_CNODE, &pnode->flags);
0501 }
0502 
0503 /**
0504  * ubifs_change_lp - change LEB properties.
0505  * @c: the UBIFS file-system description object
0506  * @lp: LEB properties to change
0507  * @free: new free space amount
0508  * @dirty: new dirty space amount
0509  * @flags: new flags
0510  * @idx_gc_cnt: change to the count of @idx_gc list
0511  *
0512  * This function changes LEB properties (@free, @dirty or @flag). However, the
0513  * property which has the %LPROPS_NC value is not changed. Returns a pointer to
0514  * the updated LEB properties on success and a negative error code on failure.
0515  *
0516  * Note, the LEB properties may have had to be copied (due to COW) and
0517  * consequently the pointer returned may not be the same as the pointer
0518  * passed.
0519  */
0520 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
0521                        const struct ubifs_lprops *lp,
0522                        int free, int dirty, int flags,
0523                        int idx_gc_cnt)
0524 {
0525     /*
0526      * This is the only function that is allowed to change lprops, so we
0527      * discard the "const" qualifier.
0528      */
0529     struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
0530 
0531     dbg_lp("LEB %d, free %d, dirty %d, flags %d",
0532            lprops->lnum, free, dirty, flags);
0533 
0534     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0535     ubifs_assert(c, c->lst.empty_lebs >= 0 &&
0536              c->lst.empty_lebs <= c->main_lebs);
0537     ubifs_assert(c, c->freeable_cnt >= 0);
0538     ubifs_assert(c, c->freeable_cnt <= c->main_lebs);
0539     ubifs_assert(c, c->lst.taken_empty_lebs >= 0);
0540     ubifs_assert(c, c->lst.taken_empty_lebs <= c->lst.empty_lebs);
0541     ubifs_assert(c, !(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
0542     ubifs_assert(c, !(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
0543     ubifs_assert(c, !(c->lst.total_used & 7));
0544     ubifs_assert(c, free == LPROPS_NC || free >= 0);
0545     ubifs_assert(c, dirty == LPROPS_NC || dirty >= 0);
0546 
0547     if (!is_lprops_dirty(c, lprops)) {
0548         lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
0549         if (IS_ERR(lprops))
0550             return lprops;
0551     } else
0552         ubifs_assert(c, lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
0553 
0554     ubifs_assert(c, !(lprops->free & 7) && !(lprops->dirty & 7));
0555 
0556     spin_lock(&c->space_lock);
0557     if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
0558         c->lst.taken_empty_lebs -= 1;
0559 
0560     if (!(lprops->flags & LPROPS_INDEX)) {
0561         int old_spc;
0562 
0563         old_spc = lprops->free + lprops->dirty;
0564         if (old_spc < c->dead_wm)
0565             c->lst.total_dead -= old_spc;
0566         else
0567             c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
0568 
0569         c->lst.total_used -= c->leb_size - old_spc;
0570     }
0571 
0572     if (free != LPROPS_NC) {
0573         free = ALIGN(free, 8);
0574         c->lst.total_free += free - lprops->free;
0575 
0576         /* Increase or decrease empty LEBs counter if needed */
0577         if (free == c->leb_size) {
0578             if (lprops->free != c->leb_size)
0579                 c->lst.empty_lebs += 1;
0580         } else if (lprops->free == c->leb_size)
0581             c->lst.empty_lebs -= 1;
0582         lprops->free = free;
0583     }
0584 
0585     if (dirty != LPROPS_NC) {
0586         dirty = ALIGN(dirty, 8);
0587         c->lst.total_dirty += dirty - lprops->dirty;
0588         lprops->dirty = dirty;
0589     }
0590 
0591     if (flags != LPROPS_NC) {
0592         /* Take care about indexing LEBs counter if needed */
0593         if ((lprops->flags & LPROPS_INDEX)) {
0594             if (!(flags & LPROPS_INDEX))
0595                 c->lst.idx_lebs -= 1;
0596         } else if (flags & LPROPS_INDEX)
0597             c->lst.idx_lebs += 1;
0598         lprops->flags = flags;
0599     }
0600 
0601     if (!(lprops->flags & LPROPS_INDEX)) {
0602         int new_spc;
0603 
0604         new_spc = lprops->free + lprops->dirty;
0605         if (new_spc < c->dead_wm)
0606             c->lst.total_dead += new_spc;
0607         else
0608             c->lst.total_dark += ubifs_calc_dark(c, new_spc);
0609 
0610         c->lst.total_used += c->leb_size - new_spc;
0611     }
0612 
0613     if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
0614         c->lst.taken_empty_lebs += 1;
0615 
0616     change_category(c, lprops);
0617     c->idx_gc_cnt += idx_gc_cnt;
0618     spin_unlock(&c->space_lock);
0619     return lprops;
0620 }
0621 
0622 /**
0623  * ubifs_get_lp_stats - get lprops statistics.
0624  * @c: UBIFS file-system description object
0625  * @lst: return statistics
0626  */
0627 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
0628 {
0629     spin_lock(&c->space_lock);
0630     memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
0631     spin_unlock(&c->space_lock);
0632 }
0633 
0634 /**
0635  * ubifs_change_one_lp - change LEB properties.
0636  * @c: the UBIFS file-system description object
0637  * @lnum: LEB to change properties for
0638  * @free: amount of free space
0639  * @dirty: amount of dirty space
0640  * @flags_set: flags to set
0641  * @flags_clean: flags to clean
0642  * @idx_gc_cnt: change to the count of idx_gc list
0643  *
0644  * This function changes properties of LEB @lnum. It is a helper wrapper over
0645  * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
0646  * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
0647  * a negative error code in case of failure.
0648  */
0649 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
0650             int flags_set, int flags_clean, int idx_gc_cnt)
0651 {
0652     int err = 0, flags;
0653     const struct ubifs_lprops *lp;
0654 
0655     ubifs_get_lprops(c);
0656 
0657     lp = ubifs_lpt_lookup_dirty(c, lnum);
0658     if (IS_ERR(lp)) {
0659         err = PTR_ERR(lp);
0660         goto out;
0661     }
0662 
0663     flags = (lp->flags | flags_set) & ~flags_clean;
0664     lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
0665     if (IS_ERR(lp))
0666         err = PTR_ERR(lp);
0667 
0668 out:
0669     ubifs_release_lprops(c);
0670     if (err)
0671         ubifs_err(c, "cannot change properties of LEB %d, error %d",
0672               lnum, err);
0673     return err;
0674 }
0675 
0676 /**
0677  * ubifs_update_one_lp - update LEB properties.
0678  * @c: the UBIFS file-system description object
0679  * @lnum: LEB to change properties for
0680  * @free: amount of free space
0681  * @dirty: amount of dirty space to add
0682  * @flags_set: flags to set
0683  * @flags_clean: flags to clean
0684  *
0685  * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
0686  * current dirty space, not substitutes it.
0687  */
0688 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
0689             int flags_set, int flags_clean)
0690 {
0691     int err = 0, flags;
0692     const struct ubifs_lprops *lp;
0693 
0694     ubifs_get_lprops(c);
0695 
0696     lp = ubifs_lpt_lookup_dirty(c, lnum);
0697     if (IS_ERR(lp)) {
0698         err = PTR_ERR(lp);
0699         goto out;
0700     }
0701 
0702     flags = (lp->flags | flags_set) & ~flags_clean;
0703     lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
0704     if (IS_ERR(lp))
0705         err = PTR_ERR(lp);
0706 
0707 out:
0708     ubifs_release_lprops(c);
0709     if (err)
0710         ubifs_err(c, "cannot update properties of LEB %d, error %d",
0711               lnum, err);
0712     return err;
0713 }
0714 
0715 /**
0716  * ubifs_read_one_lp - read LEB properties.
0717  * @c: the UBIFS file-system description object
0718  * @lnum: LEB to read properties for
0719  * @lp: where to store read properties
0720  *
0721  * This helper function reads properties of a LEB @lnum and stores them in @lp.
0722  * Returns zero in case of success and a negative error code in case of
0723  * failure.
0724  */
0725 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
0726 {
0727     int err = 0;
0728     const struct ubifs_lprops *lpp;
0729 
0730     ubifs_get_lprops(c);
0731 
0732     lpp = ubifs_lpt_lookup(c, lnum);
0733     if (IS_ERR(lpp)) {
0734         err = PTR_ERR(lpp);
0735         ubifs_err(c, "cannot read properties of LEB %d, error %d",
0736               lnum, err);
0737         goto out;
0738     }
0739 
0740     memcpy(lp, lpp, sizeof(struct ubifs_lprops));
0741 
0742 out:
0743     ubifs_release_lprops(c);
0744     return err;
0745 }
0746 
0747 /**
0748  * ubifs_fast_find_free - try to find a LEB with free space quickly.
0749  * @c: the UBIFS file-system description object
0750  *
0751  * This function returns LEB properties for a LEB with free space or %NULL if
0752  * the function is unable to find a LEB quickly.
0753  */
0754 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
0755 {
0756     struct ubifs_lprops *lprops;
0757     struct ubifs_lpt_heap *heap;
0758 
0759     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0760 
0761     heap = &c->lpt_heap[LPROPS_FREE - 1];
0762     if (heap->cnt == 0)
0763         return NULL;
0764 
0765     lprops = heap->arr[0];
0766     ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0767     ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0768     return lprops;
0769 }
0770 
0771 /**
0772  * ubifs_fast_find_empty - try to find an empty LEB quickly.
0773  * @c: the UBIFS file-system description object
0774  *
0775  * This function returns LEB properties for an empty LEB or %NULL if the
0776  * function is unable to find an empty LEB quickly.
0777  */
0778 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
0779 {
0780     struct ubifs_lprops *lprops;
0781 
0782     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0783 
0784     if (list_empty(&c->empty_list))
0785         return NULL;
0786 
0787     lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
0788     ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0789     ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0790     ubifs_assert(c, lprops->free == c->leb_size);
0791     return lprops;
0792 }
0793 
0794 /**
0795  * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
0796  * @c: the UBIFS file-system description object
0797  *
0798  * This function returns LEB properties for a freeable LEB or %NULL if the
0799  * function is unable to find a freeable LEB quickly.
0800  */
0801 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
0802 {
0803     struct ubifs_lprops *lprops;
0804 
0805     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0806 
0807     if (list_empty(&c->freeable_list))
0808         return NULL;
0809 
0810     lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
0811     ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0812     ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0813     ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
0814     ubifs_assert(c, c->freeable_cnt > 0);
0815     return lprops;
0816 }
0817 
0818 /**
0819  * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
0820  * @c: the UBIFS file-system description object
0821  *
0822  * This function returns LEB properties for a freeable index LEB or %NULL if the
0823  * function is unable to find a freeable index LEB quickly.
0824  */
0825 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
0826 {
0827     struct ubifs_lprops *lprops;
0828 
0829     ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0830 
0831     if (list_empty(&c->frdi_idx_list))
0832         return NULL;
0833 
0834     lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
0835     ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0836     ubifs_assert(c, (lprops->flags & LPROPS_INDEX));
0837     ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
0838     return lprops;
0839 }
0840 
0841 /*
0842  * Everything below is related to debugging.
0843  */
0844 
0845 /**
0846  * dbg_check_cats - check category heaps and lists.
0847  * @c: UBIFS file-system description object
0848  *
0849  * This function returns %0 on success and a negative error code on failure.
0850  */
0851 int dbg_check_cats(struct ubifs_info *c)
0852 {
0853     struct ubifs_lprops *lprops;
0854     struct list_head *pos;
0855     int i, cat;
0856 
0857     if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
0858         return 0;
0859 
0860     list_for_each_entry(lprops, &c->empty_list, list) {
0861         if (lprops->free != c->leb_size) {
0862             ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
0863                   lprops->lnum, lprops->free, lprops->dirty,
0864                   lprops->flags);
0865             return -EINVAL;
0866         }
0867         if (lprops->flags & LPROPS_TAKEN) {
0868             ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
0869                   lprops->lnum, lprops->free, lprops->dirty,
0870                   lprops->flags);
0871             return -EINVAL;
0872         }
0873     }
0874 
0875     i = 0;
0876     list_for_each_entry(lprops, &c->freeable_list, list) {
0877         if (lprops->free + lprops->dirty != c->leb_size) {
0878             ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
0879                   lprops->lnum, lprops->free, lprops->dirty,
0880                   lprops->flags);
0881             return -EINVAL;
0882         }
0883         if (lprops->flags & LPROPS_TAKEN) {
0884             ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
0885                   lprops->lnum, lprops->free, lprops->dirty,
0886                   lprops->flags);
0887             return -EINVAL;
0888         }
0889         i += 1;
0890     }
0891     if (i != c->freeable_cnt) {
0892         ubifs_err(c, "freeable list count %d expected %d", i,
0893               c->freeable_cnt);
0894         return -EINVAL;
0895     }
0896 
0897     i = 0;
0898     list_for_each(pos, &c->idx_gc)
0899         i += 1;
0900     if (i != c->idx_gc_cnt) {
0901         ubifs_err(c, "idx_gc list count %d expected %d", i,
0902               c->idx_gc_cnt);
0903         return -EINVAL;
0904     }
0905 
0906     list_for_each_entry(lprops, &c->frdi_idx_list, list) {
0907         if (lprops->free + lprops->dirty != c->leb_size) {
0908             ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0909                   lprops->lnum, lprops->free, lprops->dirty,
0910                   lprops->flags);
0911             return -EINVAL;
0912         }
0913         if (lprops->flags & LPROPS_TAKEN) {
0914             ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0915                   lprops->lnum, lprops->free, lprops->dirty,
0916                   lprops->flags);
0917             return -EINVAL;
0918         }
0919         if (!(lprops->flags & LPROPS_INDEX)) {
0920             ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0921                   lprops->lnum, lprops->free, lprops->dirty,
0922                   lprops->flags);
0923             return -EINVAL;
0924         }
0925     }
0926 
0927     for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
0928         struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
0929 
0930         for (i = 0; i < heap->cnt; i++) {
0931             lprops = heap->arr[i];
0932             if (!lprops) {
0933                 ubifs_err(c, "null ptr in LPT heap cat %d", cat);
0934                 return -EINVAL;
0935             }
0936             if (lprops->hpos != i) {
0937                 ubifs_err(c, "bad ptr in LPT heap cat %d", cat);
0938                 return -EINVAL;
0939             }
0940             if (lprops->flags & LPROPS_TAKEN) {
0941                 ubifs_err(c, "taken LEB in LPT heap cat %d", cat);
0942                 return -EINVAL;
0943             }
0944         }
0945     }
0946 
0947     return 0;
0948 }
0949 
0950 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
0951             int add_pos)
0952 {
0953     int i = 0, j, err = 0;
0954 
0955     if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
0956         return;
0957 
0958     for (i = 0; i < heap->cnt; i++) {
0959         struct ubifs_lprops *lprops = heap->arr[i];
0960         struct ubifs_lprops *lp;
0961 
0962         if (i != add_pos)
0963             if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
0964                 err = 1;
0965                 goto out;
0966             }
0967         if (lprops->hpos != i) {
0968             err = 2;
0969             goto out;
0970         }
0971         lp = ubifs_lpt_lookup(c, lprops->lnum);
0972         if (IS_ERR(lp)) {
0973             err = 3;
0974             goto out;
0975         }
0976         if (lprops != lp) {
0977             ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
0978                   (size_t)lprops, (size_t)lp, lprops->lnum,
0979                   lp->lnum);
0980             err = 4;
0981             goto out;
0982         }
0983         for (j = 0; j < i; j++) {
0984             lp = heap->arr[j];
0985             if (lp == lprops) {
0986                 err = 5;
0987                 goto out;
0988             }
0989             if (lp->lnum == lprops->lnum) {
0990                 err = 6;
0991                 goto out;
0992             }
0993         }
0994     }
0995 out:
0996     if (err) {
0997         ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err);
0998         dump_stack();
0999         ubifs_dump_heap(c, heap, cat);
1000     }
1001 }
1002 
1003 /**
1004  * scan_check_cb - scan callback.
1005  * @c: the UBIFS file-system description object
1006  * @lp: LEB properties to scan
1007  * @in_tree: whether the LEB properties are in main memory
1008  * @lst: lprops statistics to update
1009  *
1010  * This function returns a code that indicates whether the scan should continue
1011  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1012  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1013  * (%LPT_SCAN_STOP).
1014  */
1015 static int scan_check_cb(struct ubifs_info *c,
1016              const struct ubifs_lprops *lp, int in_tree,
1017              struct ubifs_lp_stats *lst)
1018 {
1019     struct ubifs_scan_leb *sleb;
1020     struct ubifs_scan_node *snod;
1021     int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret;
1022     void *buf = NULL;
1023 
1024     cat = lp->flags & LPROPS_CAT_MASK;
1025     if (cat != LPROPS_UNCAT) {
1026         cat = ubifs_categorize_lprops(c, lp);
1027         if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1028             ubifs_err(c, "bad LEB category %d expected %d",
1029                   (lp->flags & LPROPS_CAT_MASK), cat);
1030             return -EINVAL;
1031         }
1032     }
1033 
1034     /* Check lp is on its category list (if it has one) */
1035     if (in_tree) {
1036         struct list_head *list = NULL;
1037 
1038         switch (cat) {
1039         case LPROPS_EMPTY:
1040             list = &c->empty_list;
1041             break;
1042         case LPROPS_FREEABLE:
1043             list = &c->freeable_list;
1044             break;
1045         case LPROPS_FRDI_IDX:
1046             list = &c->frdi_idx_list;
1047             break;
1048         case LPROPS_UNCAT:
1049             list = &c->uncat_list;
1050             break;
1051         }
1052         if (list) {
1053             struct ubifs_lprops *lprops;
1054             int found = 0;
1055 
1056             list_for_each_entry(lprops, list, list) {
1057                 if (lprops == lp) {
1058                     found = 1;
1059                     break;
1060                 }
1061             }
1062             if (!found) {
1063                 ubifs_err(c, "bad LPT list (category %d)", cat);
1064                 return -EINVAL;
1065             }
1066         }
1067     }
1068 
1069     /* Check lp is on its category heap (if it has one) */
1070     if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1071         struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1072 
1073         if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1074             lp != heap->arr[lp->hpos]) {
1075             ubifs_err(c, "bad LPT heap (category %d)", cat);
1076             return -EINVAL;
1077         }
1078     }
1079 
1080     /*
1081      * After an unclean unmount, empty and freeable LEBs
1082      * may contain garbage - do not scan them.
1083      */
1084     if (lp->free == c->leb_size) {
1085         lst->empty_lebs += 1;
1086         lst->total_free += c->leb_size;
1087         lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1088         return LPT_SCAN_CONTINUE;
1089     }
1090     if (lp->free + lp->dirty == c->leb_size &&
1091         !(lp->flags & LPROPS_INDEX)) {
1092         lst->total_free  += lp->free;
1093         lst->total_dirty += lp->dirty;
1094         lst->total_dark  +=  ubifs_calc_dark(c, c->leb_size);
1095         return LPT_SCAN_CONTINUE;
1096     }
1097 
1098     buf = __vmalloc(c->leb_size, GFP_NOFS);
1099     if (!buf)
1100         return -ENOMEM;
1101 
1102     sleb = ubifs_scan(c, lnum, 0, buf, 0);
1103     if (IS_ERR(sleb)) {
1104         ret = PTR_ERR(sleb);
1105         if (ret == -EUCLEAN) {
1106             ubifs_dump_lprops(c);
1107             ubifs_dump_budg(c, &c->bi);
1108         }
1109         goto out;
1110     }
1111 
1112     is_idx = -1;
1113     list_for_each_entry(snod, &sleb->nodes, list) {
1114         int found, level = 0;
1115 
1116         cond_resched();
1117 
1118         if (is_idx == -1)
1119             is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1120 
1121         if (is_idx && snod->type != UBIFS_IDX_NODE) {
1122             ubifs_err(c, "indexing node in data LEB %d:%d",
1123                   lnum, snod->offs);
1124             goto out_destroy;
1125         }
1126 
1127         if (snod->type == UBIFS_IDX_NODE) {
1128             struct ubifs_idx_node *idx = snod->node;
1129 
1130             key_read(c, ubifs_idx_key(c, idx), &snod->key);
1131             level = le16_to_cpu(idx->level);
1132         }
1133 
1134         found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1135                        snod->offs, is_idx);
1136         if (found) {
1137             if (found < 0)
1138                 goto out_destroy;
1139             used += ALIGN(snod->len, 8);
1140         }
1141     }
1142 
1143     free = c->leb_size - sleb->endpt;
1144     dirty = sleb->endpt - used;
1145 
1146     if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1147         dirty < 0) {
1148         ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d",
1149               lnum, free, dirty);
1150         goto out_destroy;
1151     }
1152 
1153     if (lp->free + lp->dirty == c->leb_size &&
1154         free + dirty == c->leb_size)
1155         if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1156             (!is_idx && free == c->leb_size) ||
1157             lp->free == c->leb_size) {
1158             /*
1159              * Empty or freeable LEBs could contain index
1160              * nodes from an uncompleted commit due to an
1161              * unclean unmount. Or they could be empty for
1162              * the same reason. Or it may simply not have been
1163              * unmapped.
1164              */
1165             free = lp->free;
1166             dirty = lp->dirty;
1167             is_idx = 0;
1168             }
1169 
1170     if (is_idx && lp->free + lp->dirty == free + dirty &&
1171         lnum != c->ihead_lnum) {
1172         /*
1173          * After an unclean unmount, an index LEB could have a different
1174          * amount of free space than the value recorded by lprops. That
1175          * is because the in-the-gaps method may use free space or
1176          * create free space (as a side-effect of using ubi_leb_change
1177          * and not writing the whole LEB). The incorrect free space
1178          * value is not a problem because the index is only ever
1179          * allocated empty LEBs, so there will never be an attempt to
1180          * write to the free space at the end of an index LEB - except
1181          * by the in-the-gaps method for which it is not a problem.
1182          */
1183         free = lp->free;
1184         dirty = lp->dirty;
1185     }
1186 
1187     if (lp->free != free || lp->dirty != dirty)
1188         goto out_print;
1189 
1190     if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1191         if (free == c->leb_size)
1192             /* Free but not unmapped LEB, it's fine */
1193             is_idx = 0;
1194         else {
1195             ubifs_err(c, "indexing node without indexing flag");
1196             goto out_print;
1197         }
1198     }
1199 
1200     if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1201         ubifs_err(c, "data node with indexing flag");
1202         goto out_print;
1203     }
1204 
1205     if (free == c->leb_size)
1206         lst->empty_lebs += 1;
1207 
1208     if (is_idx)
1209         lst->idx_lebs += 1;
1210 
1211     if (!(lp->flags & LPROPS_INDEX))
1212         lst->total_used += c->leb_size - free - dirty;
1213     lst->total_free += free;
1214     lst->total_dirty += dirty;
1215 
1216     if (!(lp->flags & LPROPS_INDEX)) {
1217         int spc = free + dirty;
1218 
1219         if (spc < c->dead_wm)
1220             lst->total_dead += spc;
1221         else
1222             lst->total_dark += ubifs_calc_dark(c, spc);
1223     }
1224 
1225     ubifs_scan_destroy(sleb);
1226     vfree(buf);
1227     return LPT_SCAN_CONTINUE;
1228 
1229 out_print:
1230     ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1231           lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1232     ubifs_dump_leb(c, lnum);
1233 out_destroy:
1234     ubifs_scan_destroy(sleb);
1235     ret = -EINVAL;
1236 out:
1237     vfree(buf);
1238     return ret;
1239 }
1240 
1241 /**
1242  * dbg_check_lprops - check all LEB properties.
1243  * @c: UBIFS file-system description object
1244  *
1245  * This function checks all LEB properties and makes sure they are all correct.
1246  * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1247  * and other negative error codes in case of other errors. This function is
1248  * called while the file system is locked (because of commit start), so no
1249  * additional locking is required. Note that locking the LPT mutex would cause
1250  * a circular lock dependency with the TNC mutex.
1251  */
1252 int dbg_check_lprops(struct ubifs_info *c)
1253 {
1254     int i, err;
1255     struct ubifs_lp_stats lst;
1256 
1257     if (!dbg_is_chk_lprops(c))
1258         return 0;
1259 
1260     /*
1261      * As we are going to scan the media, the write buffers have to be
1262      * synchronized.
1263      */
1264     for (i = 0; i < c->jhead_cnt; i++) {
1265         err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1266         if (err)
1267             return err;
1268     }
1269 
1270     memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1271     err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1272                     (ubifs_lpt_scan_callback)scan_check_cb,
1273                     &lst);
1274     if (err && err != -ENOSPC)
1275         goto out;
1276 
1277     if (lst.empty_lebs != c->lst.empty_lebs ||
1278         lst.idx_lebs != c->lst.idx_lebs ||
1279         lst.total_free != c->lst.total_free ||
1280         lst.total_dirty != c->lst.total_dirty ||
1281         lst.total_used != c->lst.total_used) {
1282         ubifs_err(c, "bad overall accounting");
1283         ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1284               lst.empty_lebs, lst.idx_lebs, lst.total_free,
1285               lst.total_dirty, lst.total_used);
1286         ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1287               c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1288               c->lst.total_dirty, c->lst.total_used);
1289         err = -EINVAL;
1290         goto out;
1291     }
1292 
1293     if (lst.total_dead != c->lst.total_dead ||
1294         lst.total_dark != c->lst.total_dark) {
1295         ubifs_err(c, "bad dead/dark space accounting");
1296         ubifs_err(c, "calculated: total_dead %lld, total_dark %lld",
1297               lst.total_dead, lst.total_dark);
1298         ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld",
1299               c->lst.total_dead, c->lst.total_dark);
1300         err = -EINVAL;
1301         goto out;
1302     }
1303 
1304     err = dbg_check_cats(c);
1305 out:
1306     return err;
1307 }