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 commit-related functionality of the LEB properties
17 #include <linux/crc16.h>
18 #include <linux/slab.h>
19 #include <linux/random.h>
21 #include <linux/compat.h>
22 #include <linux/err.h>
28 static int dbg_populate_lsave(struct ubifs_info *c);
32 * first_dirty_cnode - find first dirty cnode.
33 * @c: UBIFS file-system description object
34 * @nnode: nnode at which to start
36 * This function returns the first dirty cnode or %NULL if there is not one.
38 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
44 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
45 struct ubifs_cnode *cnode;
47 cnode = nnode->nbranch[i].cnode;
49 test_bit(DIRTY_CNODE, &cnode->flags)) {
50 if (cnode->level == 0)
52 nnode = (struct ubifs_nnode *)cnode;
58 return (struct ubifs_cnode *)nnode;
63 * next_dirty_cnode - find next dirty cnode.
64 * @cnode: cnode from which to begin searching
66 * This function returns the next dirty cnode or %NULL if there is not one.
68 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
70 struct ubifs_nnode *nnode;
74 nnode = cnode->parent;
77 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
78 cnode = nnode->nbranch[i].cnode;
79 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
80 if (cnode->level == 0)
81 return cnode; /* cnode is a pnode */
82 /* cnode is a nnode */
83 return first_dirty_cnode((struct ubifs_nnode *)cnode);
86 return (struct ubifs_cnode *)nnode;
90 * get_cnodes_to_commit - create list of dirty cnodes to commit.
91 * @c: UBIFS file-system description object
93 * This function returns the number of cnodes to commit.
95 static int get_cnodes_to_commit(struct ubifs_info *c)
97 struct ubifs_cnode *cnode, *cnext;
103 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
106 c->lpt_cnext = first_dirty_cnode(c->nroot);
107 cnode = c->lpt_cnext;
112 ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
113 __set_bit(COW_CNODE, &cnode->flags);
114 cnext = next_dirty_cnode(cnode);
116 cnode->cnext = c->lpt_cnext;
119 cnode->cnext = cnext;
123 dbg_cmt("committing %d cnodes", cnt);
124 dbg_lp("committing %d cnodes", cnt);
125 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
130 * upd_ltab - update LPT LEB properties.
131 * @c: UBIFS file-system description object
133 * @free: amount of free space
134 * @dirty: amount of dirty space to add
136 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
138 dbg_lp("LEB %d free %d dirty %d to %d +%d",
139 lnum, c->ltab[lnum - c->lpt_first].free,
140 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
141 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
142 c->ltab[lnum - c->lpt_first].free = free;
143 c->ltab[lnum - c->lpt_first].dirty += dirty;
147 * alloc_lpt_leb - allocate an LPT LEB that is empty.
148 * @c: UBIFS file-system description object
149 * @lnum: LEB number is passed and returned here
151 * This function finds the next empty LEB in the ltab starting from @lnum. If a
152 * an empty LEB is found it is returned in @lnum and the function returns %0.
153 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
154 * never to run out of space.
156 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
160 n = *lnum - c->lpt_first + 1;
161 for (i = n; i < c->lpt_lebs; i++) {
162 if (c->ltab[i].tgc || c->ltab[i].cmt)
164 if (c->ltab[i].free == c->leb_size) {
166 *lnum = i + c->lpt_first;
171 for (i = 0; i < n; i++) {
172 if (c->ltab[i].tgc || c->ltab[i].cmt)
174 if (c->ltab[i].free == c->leb_size) {
176 *lnum = i + c->lpt_first;
184 * layout_cnodes - layout cnodes for commit.
185 * @c: UBIFS file-system description object
187 * This function returns %0 on success and a negative error code on failure.
189 static int layout_cnodes(struct ubifs_info *c)
191 int lnum, offs, len, alen, done_lsave, done_ltab, err;
192 struct ubifs_cnode *cnode;
194 err = dbg_chk_lpt_sz(c, 0, 0);
197 cnode = c->lpt_cnext;
200 lnum = c->nhead_lnum;
201 offs = c->nhead_offs;
202 /* Try to place lsave and ltab nicely */
203 done_lsave = !c->big_lpt;
205 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
207 c->lsave_lnum = lnum;
208 c->lsave_offs = offs;
210 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
213 if (offs + c->ltab_sz <= c->leb_size) {
218 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
224 c->dirty_nn_cnt -= 1;
227 c->dirty_pn_cnt -= 1;
229 while (offs + len > c->leb_size) {
230 alen = ALIGN(offs, c->min_io_size);
231 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
232 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
233 err = alloc_lpt_leb(c, &lnum);
237 ubifs_assert(lnum >= c->lpt_first &&
238 lnum <= c->lpt_last);
239 /* Try to place lsave and ltab nicely */
242 c->lsave_lnum = lnum;
243 c->lsave_offs = offs;
245 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
253 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
259 cnode->parent->nbranch[cnode->iip].lnum = lnum;
260 cnode->parent->nbranch[cnode->iip].offs = offs;
266 dbg_chk_lpt_sz(c, 1, len);
267 cnode = cnode->cnext;
268 } while (cnode && cnode != c->lpt_cnext);
270 /* Make sure to place LPT's save table */
272 if (offs + c->lsave_sz > c->leb_size) {
273 alen = ALIGN(offs, c->min_io_size);
274 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
275 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
276 err = alloc_lpt_leb(c, &lnum);
280 ubifs_assert(lnum >= c->lpt_first &&
281 lnum <= c->lpt_last);
284 c->lsave_lnum = lnum;
285 c->lsave_offs = offs;
287 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
290 /* Make sure to place LPT's own lprops table */
292 if (offs + c->ltab_sz > c->leb_size) {
293 alen = ALIGN(offs, c->min_io_size);
294 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
295 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
296 err = alloc_lpt_leb(c, &lnum);
300 ubifs_assert(lnum >= c->lpt_first &&
301 lnum <= c->lpt_last);
306 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
309 alen = ALIGN(offs, c->min_io_size);
310 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
311 dbg_chk_lpt_sz(c, 4, alen - offs);
312 err = dbg_chk_lpt_sz(c, 3, alen);
318 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
319 lnum, offs, len, done_ltab, done_lsave);
320 ubifs_dump_lpt_info(c);
321 ubifs_dump_lpt_lebs(c);
328 * realloc_lpt_leb - allocate an LPT LEB that is empty.
329 * @c: UBIFS file-system description object
330 * @lnum: LEB number is passed and returned here
332 * This function duplicates exactly the results of the function alloc_lpt_leb.
333 * It is used during end commit to reallocate the same LEB numbers that were
334 * allocated by alloc_lpt_leb during start commit.
336 * This function finds the next LEB that was allocated by the alloc_lpt_leb
337 * function starting from @lnum. If a LEB is found it is returned in @lnum and
338 * the function returns %0. Otherwise the function returns -ENOSPC.
339 * Note however, that LPT is designed never to run out of space.
341 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
345 n = *lnum - c->lpt_first + 1;
346 for (i = n; i < c->lpt_lebs; i++)
347 if (c->ltab[i].cmt) {
349 *lnum = i + c->lpt_first;
353 for (i = 0; i < n; i++)
354 if (c->ltab[i].cmt) {
356 *lnum = i + c->lpt_first;
363 * write_cnodes - write cnodes for commit.
364 * @c: UBIFS file-system description object
366 * This function returns %0 on success and a negative error code on failure.
368 static int write_cnodes(struct ubifs_info *c)
370 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
371 struct ubifs_cnode *cnode;
372 void *buf = c->lpt_buf;
374 cnode = c->lpt_cnext;
377 lnum = c->nhead_lnum;
378 offs = c->nhead_offs;
380 /* Ensure empty LEB is unmapped */
382 err = ubifs_leb_unmap(c, lnum);
386 /* Try to place lsave and ltab nicely */
387 done_lsave = !c->big_lpt;
389 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
391 ubifs_pack_lsave(c, buf + offs, c->lsave);
393 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
396 if (offs + c->ltab_sz <= c->leb_size) {
398 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
400 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
403 /* Loop for each cnode */
409 while (offs + len > c->leb_size) {
412 alen = ALIGN(wlen, c->min_io_size);
413 memset(buf + offs, 0xff, alen - wlen);
414 err = ubifs_leb_write(c, lnum, buf + from, from,
419 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
420 err = realloc_lpt_leb(c, &lnum);
424 ubifs_assert(lnum >= c->lpt_first &&
425 lnum <= c->lpt_last);
426 err = ubifs_leb_unmap(c, lnum);
429 /* Try to place lsave and ltab nicely */
432 ubifs_pack_lsave(c, buf + offs, c->lsave);
434 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
439 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
441 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
447 ubifs_pack_nnode(c, buf + offs,
448 (struct ubifs_nnode *)cnode);
450 ubifs_pack_pnode(c, buf + offs,
451 (struct ubifs_pnode *)cnode);
453 * The reason for the barriers is the same as in case of TNC.
454 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
455 * 'dirty_cow_pnode()' are the functions for which this is
458 clear_bit(DIRTY_CNODE, &cnode->flags);
459 smp_mb__before_atomic();
460 clear_bit(COW_CNODE, &cnode->flags);
461 smp_mb__after_atomic();
463 dbg_chk_lpt_sz(c, 1, len);
464 cnode = cnode->cnext;
465 } while (cnode && cnode != c->lpt_cnext);
467 /* Make sure to place LPT's save table */
469 if (offs + c->lsave_sz > c->leb_size) {
471 alen = ALIGN(wlen, c->min_io_size);
472 memset(buf + offs, 0xff, alen - wlen);
473 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
476 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
477 err = realloc_lpt_leb(c, &lnum);
481 ubifs_assert(lnum >= c->lpt_first &&
482 lnum <= c->lpt_last);
483 err = ubifs_leb_unmap(c, lnum);
488 ubifs_pack_lsave(c, buf + offs, c->lsave);
490 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
493 /* Make sure to place LPT's own lprops table */
495 if (offs + c->ltab_sz > c->leb_size) {
497 alen = ALIGN(wlen, c->min_io_size);
498 memset(buf + offs, 0xff, alen - wlen);
499 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
502 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
503 err = realloc_lpt_leb(c, &lnum);
507 ubifs_assert(lnum >= c->lpt_first &&
508 lnum <= c->lpt_last);
509 err = ubifs_leb_unmap(c, lnum);
513 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
515 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
518 /* Write remaining data in buffer */
520 alen = ALIGN(wlen, c->min_io_size);
521 memset(buf + offs, 0xff, alen - wlen);
522 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
526 dbg_chk_lpt_sz(c, 4, alen - wlen);
527 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
531 c->nhead_lnum = lnum;
532 c->nhead_offs = ALIGN(offs, c->min_io_size);
534 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
535 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
536 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
538 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
543 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
544 lnum, offs, len, done_ltab, done_lsave);
545 ubifs_dump_lpt_info(c);
546 ubifs_dump_lpt_lebs(c);
553 * next_pnode_to_dirty - find next pnode to dirty.
554 * @c: UBIFS file-system description object
557 * This function returns the next pnode to dirty or %NULL if there are no more
558 * pnodes. Note that pnodes that have never been written (lnum == 0) are
561 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
562 struct ubifs_pnode *pnode)
564 struct ubifs_nnode *nnode;
567 /* Try to go right */
568 nnode = pnode->parent;
569 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
570 if (nnode->nbranch[iip].lnum)
571 return ubifs_get_pnode(c, nnode, iip);
574 /* Go up while can't go right */
576 iip = nnode->iip + 1;
577 nnode = nnode->parent;
580 for (; iip < UBIFS_LPT_FANOUT; iip++) {
581 if (nnode->nbranch[iip].lnum)
584 } while (iip >= UBIFS_LPT_FANOUT);
587 nnode = ubifs_get_nnode(c, nnode, iip);
589 return (void *)nnode;
591 /* Go down to level 1 */
592 while (nnode->level > 1) {
593 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
594 if (nnode->nbranch[iip].lnum)
597 if (iip >= UBIFS_LPT_FANOUT) {
599 * Should not happen, but we need to keep going
604 nnode = ubifs_get_nnode(c, nnode, iip);
606 return (void *)nnode;
609 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
610 if (nnode->nbranch[iip].lnum)
612 if (iip >= UBIFS_LPT_FANOUT)
613 /* Should not happen, but we need to keep going if it does */
615 return ubifs_get_pnode(c, nnode, iip);
619 * pnode_lookup - lookup a pnode in the LPT.
620 * @c: UBIFS file-system description object
621 * @i: pnode number (0 to main_lebs - 1)
623 * This function returns a pointer to the pnode on success or a negative
624 * error code on failure.
626 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
628 int err, h, iip, shft;
629 struct ubifs_nnode *nnode;
632 err = ubifs_read_nnode(c, NULL, 0);
636 i <<= UBIFS_LPT_FANOUT_SHIFT;
638 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
639 for (h = 1; h < c->lpt_hght; h++) {
640 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
641 shft -= UBIFS_LPT_FANOUT_SHIFT;
642 nnode = ubifs_get_nnode(c, nnode, iip);
644 return ERR_CAST(nnode);
646 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
647 return ubifs_get_pnode(c, nnode, iip);
651 * add_pnode_dirt - add dirty space to LPT LEB properties.
652 * @c: UBIFS file-system description object
653 * @pnode: pnode for which to add dirt
655 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
657 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
662 * do_make_pnode_dirty - mark a pnode dirty.
663 * @c: UBIFS file-system description object
664 * @pnode: pnode to mark dirty
666 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
668 /* Assumes cnext list is empty i.e. not called during commit */
669 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
670 struct ubifs_nnode *nnode;
672 c->dirty_pn_cnt += 1;
673 add_pnode_dirt(c, pnode);
674 /* Mark parent and ancestors dirty too */
675 nnode = pnode->parent;
677 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
678 c->dirty_nn_cnt += 1;
679 ubifs_add_nnode_dirt(c, nnode);
680 nnode = nnode->parent;
688 * make_tree_dirty - mark the entire LEB properties tree dirty.
689 * @c: UBIFS file-system description object
691 * This function is used by the "small" LPT model to cause the entire LEB
692 * properties tree to be written. The "small" LPT model does not use LPT
693 * garbage collection because it is more efficient to write the entire tree
694 * (because it is small).
696 * This function returns %0 on success and a negative error code on failure.
698 static int make_tree_dirty(struct ubifs_info *c)
700 struct ubifs_pnode *pnode;
702 pnode = pnode_lookup(c, 0);
704 return PTR_ERR(pnode);
707 do_make_pnode_dirty(c, pnode);
708 pnode = next_pnode_to_dirty(c, pnode);
710 return PTR_ERR(pnode);
716 * need_write_all - determine if the LPT area is running out of free space.
717 * @c: UBIFS file-system description object
719 * This function returns %1 if the LPT area is running out of free space and %0
722 static int need_write_all(struct ubifs_info *c)
727 for (i = 0; i < c->lpt_lebs; i++) {
728 if (i + c->lpt_first == c->nhead_lnum)
729 free += c->leb_size - c->nhead_offs;
730 else if (c->ltab[i].free == c->leb_size)
732 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
735 /* Less than twice the size left */
736 if (free <= c->lpt_sz * 2)
742 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
743 * @c: UBIFS file-system description object
745 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
746 * free space and so may be reused as soon as the next commit is completed.
747 * This function is called during start commit to mark LPT LEBs for trivial GC.
749 static void lpt_tgc_start(struct ubifs_info *c)
753 for (i = 0; i < c->lpt_lebs; i++) {
754 if (i + c->lpt_first == c->nhead_lnum)
756 if (c->ltab[i].dirty > 0 &&
757 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
759 c->ltab[i].free = c->leb_size;
760 c->ltab[i].dirty = 0;
761 dbg_lp("LEB %d", i + c->lpt_first);
767 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
768 * @c: UBIFS file-system description object
770 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
771 * free space and so may be reused as soon as the next commit is completed.
772 * This function is called after the commit is completed (master node has been
773 * written) and un-maps LPT LEBs that were marked for trivial GC.
775 static int lpt_tgc_end(struct ubifs_info *c)
779 for (i = 0; i < c->lpt_lebs; i++)
780 if (c->ltab[i].tgc) {
781 err = ubifs_leb_unmap(c, i + c->lpt_first);
785 dbg_lp("LEB %d", i + c->lpt_first);
791 * populate_lsave - fill the lsave array with important LEB numbers.
792 * @c: the UBIFS file-system description object
794 * This function is only called for the "big" model. It records a small number
795 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
796 * most important to least important): empty, freeable, freeable index, dirty
797 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
798 * their pnodes into memory. That will stop us from having to scan the LPT
799 * straight away. For the "small" model we assume that scanning the LPT is no
802 static void populate_lsave(struct ubifs_info *c)
804 struct ubifs_lprops *lprops;
805 struct ubifs_lpt_heap *heap;
808 ubifs_assert(c->big_lpt);
809 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
810 c->lpt_drty_flgs |= LSAVE_DIRTY;
811 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
815 if (dbg_populate_lsave(c))
819 list_for_each_entry(lprops, &c->empty_list, list) {
820 c->lsave[cnt++] = lprops->lnum;
821 if (cnt >= c->lsave_cnt)
824 list_for_each_entry(lprops, &c->freeable_list, list) {
825 c->lsave[cnt++] = lprops->lnum;
826 if (cnt >= c->lsave_cnt)
829 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
830 c->lsave[cnt++] = lprops->lnum;
831 if (cnt >= c->lsave_cnt)
834 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
835 for (i = 0; i < heap->cnt; i++) {
836 c->lsave[cnt++] = heap->arr[i]->lnum;
837 if (cnt >= c->lsave_cnt)
840 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
841 for (i = 0; i < heap->cnt; i++) {
842 c->lsave[cnt++] = heap->arr[i]->lnum;
843 if (cnt >= c->lsave_cnt)
846 heap = &c->lpt_heap[LPROPS_FREE - 1];
847 for (i = 0; i < heap->cnt; i++) {
848 c->lsave[cnt++] = heap->arr[i]->lnum;
849 if (cnt >= c->lsave_cnt)
852 /* Fill it up completely */
853 while (cnt < c->lsave_cnt)
854 c->lsave[cnt++] = c->main_first;
858 * nnode_lookup - lookup a nnode in the LPT.
859 * @c: UBIFS file-system description object
862 * This function returns a pointer to the nnode on success or a negative
863 * error code on failure.
865 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
868 struct ubifs_nnode *nnode;
871 err = ubifs_read_nnode(c, NULL, 0);
877 iip = i & (UBIFS_LPT_FANOUT - 1);
878 i >>= UBIFS_LPT_FANOUT_SHIFT;
881 nnode = ubifs_get_nnode(c, nnode, iip);
889 * make_nnode_dirty - find a nnode and, if found, make it dirty.
890 * @c: UBIFS file-system description object
891 * @node_num: nnode number of nnode to make dirty
892 * @lnum: LEB number where nnode was written
893 * @offs: offset where nnode was written
895 * This function is used by LPT garbage collection. LPT garbage collection is
896 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
897 * simply involves marking all the nodes in the LEB being garbage-collected as
898 * dirty. The dirty nodes are written next commit, after which the LEB is free
901 * This function returns %0 on success and a negative error code on failure.
903 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
906 struct ubifs_nnode *nnode;
908 nnode = nnode_lookup(c, node_num);
910 return PTR_ERR(nnode);
912 struct ubifs_nbranch *branch;
914 branch = &nnode->parent->nbranch[nnode->iip];
915 if (branch->lnum != lnum || branch->offs != offs)
916 return 0; /* nnode is obsolete */
917 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
918 return 0; /* nnode is obsolete */
919 /* Assumes cnext list is empty i.e. not called during commit */
920 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
921 c->dirty_nn_cnt += 1;
922 ubifs_add_nnode_dirt(c, nnode);
923 /* Mark parent and ancestors dirty too */
924 nnode = nnode->parent;
926 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
927 c->dirty_nn_cnt += 1;
928 ubifs_add_nnode_dirt(c, nnode);
929 nnode = nnode->parent;
938 * make_pnode_dirty - find a pnode and, if found, make it dirty.
939 * @c: UBIFS file-system description object
940 * @node_num: pnode number of pnode to make dirty
941 * @lnum: LEB number where pnode was written
942 * @offs: offset where pnode was written
944 * This function is used by LPT garbage collection. LPT garbage collection is
945 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
946 * simply involves marking all the nodes in the LEB being garbage-collected as
947 * dirty. The dirty nodes are written next commit, after which the LEB is free
950 * This function returns %0 on success and a negative error code on failure.
952 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
955 struct ubifs_pnode *pnode;
956 struct ubifs_nbranch *branch;
958 pnode = pnode_lookup(c, node_num);
960 return PTR_ERR(pnode);
961 branch = &pnode->parent->nbranch[pnode->iip];
962 if (branch->lnum != lnum || branch->offs != offs)
964 do_make_pnode_dirty(c, pnode);
969 * make_ltab_dirty - make ltab node dirty.
970 * @c: UBIFS file-system description object
971 * @lnum: LEB number where ltab was written
972 * @offs: offset where ltab was written
974 * This function is used by LPT garbage collection. LPT garbage collection is
975 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
976 * simply involves marking all the nodes in the LEB being garbage-collected as
977 * dirty. The dirty nodes are written next commit, after which the LEB is free
980 * This function returns %0 on success and a negative error code on failure.
982 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
984 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
985 return 0; /* This ltab node is obsolete */
986 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
987 c->lpt_drty_flgs |= LTAB_DIRTY;
988 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
994 * make_lsave_dirty - make lsave node dirty.
995 * @c: UBIFS file-system description object
996 * @lnum: LEB number where lsave was written
997 * @offs: offset where lsave was written
999 * This function is used by LPT garbage collection. LPT garbage collection is
1000 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1001 * simply involves marking all the nodes in the LEB being garbage-collected as
1002 * dirty. The dirty nodes are written next commit, after which the LEB is free
1005 * This function returns %0 on success and a negative error code on failure.
1007 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1009 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1010 return 0; /* This lsave node is obsolete */
1011 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1012 c->lpt_drty_flgs |= LSAVE_DIRTY;
1013 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1019 * make_node_dirty - make node dirty.
1020 * @c: UBIFS file-system description object
1021 * @node_type: LPT node type
1022 * @node_num: node number
1023 * @lnum: LEB number where node was written
1024 * @offs: offset where node was written
1026 * This function is used by LPT garbage collection. LPT garbage collection is
1027 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1028 * simply involves marking all the nodes in the LEB being garbage-collected as
1029 * dirty. The dirty nodes are written next commit, after which the LEB is free
1032 * This function returns %0 on success and a negative error code on failure.
1034 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1037 switch (node_type) {
1038 case UBIFS_LPT_NNODE:
1039 return make_nnode_dirty(c, node_num, lnum, offs);
1040 case UBIFS_LPT_PNODE:
1041 return make_pnode_dirty(c, node_num, lnum, offs);
1042 case UBIFS_LPT_LTAB:
1043 return make_ltab_dirty(c, lnum, offs);
1044 case UBIFS_LPT_LSAVE:
1045 return make_lsave_dirty(c, lnum, offs);
1051 * get_lpt_node_len - return the length of a node based on its type.
1052 * @c: UBIFS file-system description object
1053 * @node_type: LPT node type
1055 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1057 switch (node_type) {
1058 case UBIFS_LPT_NNODE:
1060 case UBIFS_LPT_PNODE:
1062 case UBIFS_LPT_LTAB:
1064 case UBIFS_LPT_LSAVE:
1071 * get_pad_len - return the length of padding in a buffer.
1072 * @c: UBIFS file-system description object
1074 * @len: length of buffer
1076 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1080 if (c->min_io_size == 1)
1082 offs = c->leb_size - len;
1083 pad_len = ALIGN(offs, c->min_io_size) - offs;
1088 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1089 * @c: UBIFS file-system description object
1091 * @node_num: node number is returned here
1093 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1096 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1097 int pos = 0, node_type;
1099 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1100 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1105 * is_a_node - determine if a buffer contains a node.
1106 * @c: UBIFS file-system description object
1108 * @len: length of buffer
1110 * This function returns %1 if the buffer contains a node or %0 if it does not.
1112 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1114 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1115 int pos = 0, node_type, node_len;
1116 uint16_t crc, calc_crc;
1118 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1120 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1121 if (node_type == UBIFS_LPT_NOT_A_NODE)
1123 node_len = get_lpt_node_len(c, node_type);
1124 if (!node_len || node_len > len)
1128 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1129 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1130 node_len - UBIFS_LPT_CRC_BYTES);
1131 if (crc != calc_crc)
1137 * lpt_gc_lnum - garbage collect a LPT LEB.
1138 * @c: UBIFS file-system description object
1139 * @lnum: LEB number to garbage collect
1141 * LPT garbage collection is used only for the "big" LPT model
1142 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1143 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1144 * next commit, after which the LEB is free to be reused.
1146 * This function returns %0 on success and a negative error code on failure.
1148 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1150 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1151 void *buf = c->lpt_buf;
1153 dbg_lp("LEB %d", lnum);
1155 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1160 if (!is_a_node(c, buf, len)) {
1163 pad_len = get_pad_len(c, buf, len);
1171 node_type = get_lpt_node_type(c, buf, &node_num);
1172 node_len = get_lpt_node_len(c, node_type);
1173 offs = c->leb_size - len;
1174 ubifs_assert(node_len != 0);
1175 mutex_lock(&c->lp_mutex);
1176 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1177 mutex_unlock(&c->lp_mutex);
1187 * lpt_gc - LPT garbage collection.
1188 * @c: UBIFS file-system description object
1190 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1191 * Returns %0 on success and a negative error code on failure.
1193 static int lpt_gc(struct ubifs_info *c)
1195 int i, lnum = -1, dirty = 0;
1197 mutex_lock(&c->lp_mutex);
1198 for (i = 0; i < c->lpt_lebs; i++) {
1199 ubifs_assert(!c->ltab[i].tgc);
1200 if (i + c->lpt_first == c->nhead_lnum ||
1201 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1203 if (c->ltab[i].dirty > dirty) {
1204 dirty = c->ltab[i].dirty;
1205 lnum = i + c->lpt_first;
1208 mutex_unlock(&c->lp_mutex);
1211 return lpt_gc_lnum(c, lnum);
1215 * ubifs_lpt_start_commit - UBIFS commit starts.
1216 * @c: the UBIFS file-system description object
1218 * This function has to be called when UBIFS starts the commit operation.
1219 * This function "freezes" all currently dirty LEB properties and does not
1220 * change them anymore. Further changes are saved and tracked separately
1221 * because they are not part of this commit. This function returns zero in case
1222 * of success and a negative error code in case of failure.
1224 int ubifs_lpt_start_commit(struct ubifs_info *c)
1230 mutex_lock(&c->lp_mutex);
1231 err = dbg_chk_lpt_free_spc(c);
1234 err = dbg_check_ltab(c);
1238 if (c->check_lpt_free) {
1240 * We ensure there is enough free space in
1241 * ubifs_lpt_post_commit() by marking nodes dirty. That
1242 * information is lost when we unmount, so we also need
1243 * to check free space once after mounting also.
1245 c->check_lpt_free = 0;
1246 while (need_write_all(c)) {
1247 mutex_unlock(&c->lp_mutex);
1251 mutex_lock(&c->lp_mutex);
1257 if (!c->dirty_pn_cnt) {
1258 dbg_cmt("no cnodes to commit");
1263 if (!c->big_lpt && need_write_all(c)) {
1264 /* If needed, write everything */
1265 err = make_tree_dirty(c);
1274 cnt = get_cnodes_to_commit(c);
1275 ubifs_assert(cnt != 0);
1277 err = layout_cnodes(c);
1281 /* Copy the LPT's own lprops for end commit to write */
1282 memcpy(c->ltab_cmt, c->ltab,
1283 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1284 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1287 mutex_unlock(&c->lp_mutex);
1292 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1293 * @c: UBIFS file-system description object
1295 static void free_obsolete_cnodes(struct ubifs_info *c)
1297 struct ubifs_cnode *cnode, *cnext;
1299 cnext = c->lpt_cnext;
1304 cnext = cnode->cnext;
1305 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1308 cnode->cnext = NULL;
1309 } while (cnext != c->lpt_cnext);
1310 c->lpt_cnext = NULL;
1315 * ubifs_lpt_end_commit - finish the commit operation.
1316 * @c: the UBIFS file-system description object
1318 * This function has to be called when the commit operation finishes. It
1319 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1320 * the media. Returns zero in case of success and a negative error code in case
1323 int ubifs_lpt_end_commit(struct ubifs_info *c)
1332 err = write_cnodes(c);
1336 mutex_lock(&c->lp_mutex);
1337 free_obsolete_cnodes(c);
1338 mutex_unlock(&c->lp_mutex);
1345 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1346 * @c: UBIFS file-system description object
1348 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1349 * commit for the "big" LPT model.
1351 int ubifs_lpt_post_commit(struct ubifs_info *c)
1355 mutex_lock(&c->lp_mutex);
1356 err = lpt_tgc_end(c);
1360 while (need_write_all(c)) {
1361 mutex_unlock(&c->lp_mutex);
1365 mutex_lock(&c->lp_mutex);
1368 mutex_unlock(&c->lp_mutex);
1373 * first_nnode - find the first nnode in memory.
1374 * @c: UBIFS file-system description object
1375 * @hght: height of tree where nnode found is returned here
1377 * This function returns a pointer to the nnode found or %NULL if no nnode is
1378 * found. This function is a helper to 'ubifs_lpt_free()'.
1380 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1382 struct ubifs_nnode *nnode;
1389 for (h = 1; h < c->lpt_hght; h++) {
1391 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1392 if (nnode->nbranch[i].nnode) {
1394 nnode = nnode->nbranch[i].nnode;
1406 * next_nnode - find the next nnode in memory.
1407 * @c: UBIFS file-system description object
1408 * @nnode: nnode from which to start.
1409 * @hght: height of tree where nnode is, is passed and returned here
1411 * This function returns a pointer to the nnode found or %NULL if no nnode is
1412 * found. This function is a helper to 'ubifs_lpt_free()'.
1414 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1415 struct ubifs_nnode *nnode, int *hght)
1417 struct ubifs_nnode *parent;
1418 int iip, h, i, found;
1420 parent = nnode->parent;
1423 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1427 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1428 nnode = parent->nbranch[iip].nnode;
1436 for (h = *hght + 1; h < c->lpt_hght; h++) {
1438 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1439 if (nnode->nbranch[i].nnode) {
1441 nnode = nnode->nbranch[i].nnode;
1453 * ubifs_lpt_free - free resources owned by the LPT.
1454 * @c: UBIFS file-system description object
1455 * @wr_only: free only resources used for writing
1457 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1459 struct ubifs_nnode *nnode;
1462 /* Free write-only things first */
1464 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1476 /* Now free the rest */
1478 nnode = first_nnode(c, &hght);
1480 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1481 kfree(nnode->nbranch[i].nnode);
1482 nnode = next_nnode(c, nnode, &hght);
1484 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1485 kfree(c->lpt_heap[i].arr);
1486 kfree(c->dirty_idx.arr);
1489 kfree(c->lpt_nod_buf);
1494 * Everything below is related to debugging.
1498 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1500 * @len: buffer length
1502 static int dbg_is_all_ff(uint8_t *buf, int len)
1506 for (i = 0; i < len; i++)
1513 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1514 * @c: the UBIFS file-system description object
1515 * @lnum: LEB number where nnode was written
1516 * @offs: offset where nnode was written
1518 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1520 struct ubifs_nnode *nnode;
1523 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1524 nnode = first_nnode(c, &hght);
1525 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1526 struct ubifs_nbranch *branch;
1529 if (nnode->parent) {
1530 branch = &nnode->parent->nbranch[nnode->iip];
1531 if (branch->lnum != lnum || branch->offs != offs)
1533 if (test_bit(DIRTY_CNODE, &nnode->flags))
1537 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1539 if (test_bit(DIRTY_CNODE, &nnode->flags))
1548 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1549 * @c: the UBIFS file-system description object
1550 * @lnum: LEB number where pnode was written
1551 * @offs: offset where pnode was written
1553 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1557 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1558 for (i = 0; i < cnt; i++) {
1559 struct ubifs_pnode *pnode;
1560 struct ubifs_nbranch *branch;
1563 pnode = pnode_lookup(c, i);
1565 return PTR_ERR(pnode);
1566 branch = &pnode->parent->nbranch[pnode->iip];
1567 if (branch->lnum != lnum || branch->offs != offs)
1569 if (test_bit(DIRTY_CNODE, &pnode->flags))
1577 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1578 * @c: the UBIFS file-system description object
1579 * @lnum: LEB number where ltab node was written
1580 * @offs: offset where ltab node was written
1582 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1584 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1586 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1590 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1591 * @c: the UBIFS file-system description object
1592 * @lnum: LEB number where lsave node was written
1593 * @offs: offset where lsave node was written
1595 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1597 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1599 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1603 * dbg_is_node_dirty - determine if a node is dirty.
1604 * @c: the UBIFS file-system description object
1605 * @node_type: node type
1606 * @lnum: LEB number where node was written
1607 * @offs: offset where node was written
1609 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1612 switch (node_type) {
1613 case UBIFS_LPT_NNODE:
1614 return dbg_is_nnode_dirty(c, lnum, offs);
1615 case UBIFS_LPT_PNODE:
1616 return dbg_is_pnode_dirty(c, lnum, offs);
1617 case UBIFS_LPT_LTAB:
1618 return dbg_is_ltab_dirty(c, lnum, offs);
1619 case UBIFS_LPT_LSAVE:
1620 return dbg_is_lsave_dirty(c, lnum, offs);
1626 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1627 * @c: the UBIFS file-system description object
1628 * @lnum: LEB number where node was written
1629 * @offs: offset where node was written
1631 * This function returns %0 on success and a negative error code on failure.
1633 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1635 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1639 if (!dbg_is_chk_lprops(c))
1642 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1644 ubifs_err(c, "cannot allocate memory for ltab checking");
1648 dbg_lp("LEB %d", lnum);
1650 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1655 if (!is_a_node(c, p, len)) {
1658 pad_len = get_pad_len(c, p, len);
1665 if (!dbg_is_all_ff(p, len)) {
1666 ubifs_err(c, "invalid empty space in LEB %d at %d",
1667 lnum, c->leb_size - len);
1670 i = lnum - c->lpt_first;
1671 if (len != c->ltab[i].free) {
1672 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)",
1673 lnum, len, c->ltab[i].free);
1676 if (dirty != c->ltab[i].dirty) {
1677 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)",
1678 lnum, dirty, c->ltab[i].dirty);
1683 node_type = get_lpt_node_type(c, p, &node_num);
1684 node_len = get_lpt_node_len(c, node_type);
1685 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1699 * dbg_check_ltab - check the free and dirty space in the ltab.
1700 * @c: the UBIFS file-system description object
1702 * This function returns %0 on success and a negative error code on failure.
1704 int dbg_check_ltab(struct ubifs_info *c)
1706 int lnum, err, i, cnt;
1708 if (!dbg_is_chk_lprops(c))
1711 /* Bring the entire tree into memory */
1712 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1713 for (i = 0; i < cnt; i++) {
1714 struct ubifs_pnode *pnode;
1716 pnode = pnode_lookup(c, i);
1718 return PTR_ERR(pnode);
1723 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1727 /* Check each LEB */
1728 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1729 err = dbg_check_ltab_lnum(c, lnum);
1731 ubifs_err(c, "failed at LEB %d", lnum);
1736 dbg_lp("succeeded");
1741 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1742 * @c: the UBIFS file-system description object
1744 * This function returns %0 on success and a negative error code on failure.
1746 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1751 if (!dbg_is_chk_lprops(c))
1754 for (i = 0; i < c->lpt_lebs; i++) {
1755 if (c->ltab[i].tgc || c->ltab[i].cmt)
1757 if (i + c->lpt_first == c->nhead_lnum)
1758 free += c->leb_size - c->nhead_offs;
1759 else if (c->ltab[i].free == c->leb_size)
1760 free += c->leb_size;
1762 if (free < c->lpt_sz) {
1763 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld",
1765 ubifs_dump_lpt_info(c);
1766 ubifs_dump_lpt_lebs(c);
1774 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1775 * @c: the UBIFS file-system description object
1776 * @action: what to do
1777 * @len: length written
1779 * This function returns %0 on success and a negative error code on failure.
1780 * The @action argument may be one of:
1781 * o %0 - LPT debugging checking starts, initialize debugging variables;
1782 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1783 * o %2 - switched to a different LEB and wasted @len bytes;
1784 * o %3 - check that we've written the right number of bytes.
1785 * o %4 - wasted @len bytes;
1787 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1789 struct ubifs_debug_info *d = c->dbg;
1790 long long chk_lpt_sz, lpt_sz;
1793 if (!dbg_is_chk_lprops(c))
1800 d->chk_lpt_lebs = 0;
1801 d->chk_lpt_wastage = 0;
1802 if (c->dirty_pn_cnt > c->pnode_cnt) {
1803 ubifs_err(c, "dirty pnodes %d exceed max %d",
1804 c->dirty_pn_cnt, c->pnode_cnt);
1807 if (c->dirty_nn_cnt > c->nnode_cnt) {
1808 ubifs_err(c, "dirty nnodes %d exceed max %d",
1809 c->dirty_nn_cnt, c->nnode_cnt);
1814 d->chk_lpt_sz += len;
1817 d->chk_lpt_sz += len;
1818 d->chk_lpt_wastage += len;
1819 d->chk_lpt_lebs += 1;
1822 chk_lpt_sz = c->leb_size;
1823 chk_lpt_sz *= d->chk_lpt_lebs;
1824 chk_lpt_sz += len - c->nhead_offs;
1825 if (d->chk_lpt_sz != chk_lpt_sz) {
1826 ubifs_err(c, "LPT wrote %lld but space used was %lld",
1827 d->chk_lpt_sz, chk_lpt_sz);
1830 if (d->chk_lpt_sz > c->lpt_sz) {
1831 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld",
1832 d->chk_lpt_sz, c->lpt_sz);
1835 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1836 ubifs_err(c, "LPT layout size %lld but wrote %lld",
1837 d->chk_lpt_sz, d->chk_lpt_sz2);
1840 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1841 ubifs_err(c, "LPT new nhead offs: expected %d was %d",
1842 d->new_nhead_offs, len);
1845 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1846 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1847 lpt_sz += c->ltab_sz;
1849 lpt_sz += c->lsave_sz;
1850 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1851 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1852 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1856 ubifs_dump_lpt_info(c);
1857 ubifs_dump_lpt_lebs(c);
1860 d->chk_lpt_sz2 = d->chk_lpt_sz;
1862 d->chk_lpt_wastage = 0;
1863 d->chk_lpt_lebs = 0;
1864 d->new_nhead_offs = len;
1867 d->chk_lpt_sz += len;
1868 d->chk_lpt_wastage += len;
1876 * ubifs_dump_lpt_leb - dump an LPT LEB.
1877 * @c: UBIFS file-system description object
1878 * @lnum: LEB number to dump
1880 * This function dumps an LEB from LPT area. Nodes in this area are very
1881 * different to nodes in the main area (e.g., they do not have common headers,
1882 * they do not have 8-byte alignments, etc), so we have a separate function to
1883 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1885 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1887 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1890 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
1891 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1893 ubifs_err(c, "cannot allocate memory to dump LPT");
1897 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1902 offs = c->leb_size - len;
1903 if (!is_a_node(c, p, len)) {
1906 pad_len = get_pad_len(c, p, len);
1908 pr_err("LEB %d:%d, pad %d bytes\n",
1909 lnum, offs, pad_len);
1915 pr_err("LEB %d:%d, free %d bytes\n",
1920 node_type = get_lpt_node_type(c, p, &node_num);
1921 switch (node_type) {
1922 case UBIFS_LPT_PNODE:
1924 node_len = c->pnode_sz;
1926 pr_err("LEB %d:%d, pnode num %d\n",
1927 lnum, offs, node_num);
1929 pr_err("LEB %d:%d, pnode\n", lnum, offs);
1932 case UBIFS_LPT_NNODE:
1935 struct ubifs_nnode nnode;
1937 node_len = c->nnode_sz;
1939 pr_err("LEB %d:%d, nnode num %d, ",
1940 lnum, offs, node_num);
1942 pr_err("LEB %d:%d, nnode, ",
1944 err = ubifs_unpack_nnode(c, p, &nnode);
1946 pr_err("failed to unpack_node, error %d\n",
1950 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1951 pr_cont("%d:%d", nnode.nbranch[i].lnum,
1952 nnode.nbranch[i].offs);
1953 if (i != UBIFS_LPT_FANOUT - 1)
1959 case UBIFS_LPT_LTAB:
1960 node_len = c->ltab_sz;
1961 pr_err("LEB %d:%d, ltab\n", lnum, offs);
1963 case UBIFS_LPT_LSAVE:
1964 node_len = c->lsave_sz;
1965 pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1968 ubifs_err(c, "LPT node type %d not recognized", node_type);
1976 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1983 * ubifs_dump_lpt_lebs - dump LPT lebs.
1984 * @c: UBIFS file-system description object
1986 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1989 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1993 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1994 for (i = 0; i < c->lpt_lebs; i++)
1995 dump_lpt_leb(c, i + c->lpt_first);
1996 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
2000 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2001 * @c: UBIFS file-system description object
2003 * This is a debugging version for 'populate_lsave()' which populates lsave
2004 * with random LEBs instead of useful LEBs, which is good for test coverage.
2005 * Returns zero if lsave has not been populated (this debugging feature is
2006 * disabled) an non-zero if lsave has been populated.
2008 static int dbg_populate_lsave(struct ubifs_info *c)
2010 struct ubifs_lprops *lprops;
2011 struct ubifs_lpt_heap *heap;
2014 if (!dbg_is_chk_gen(c))
2016 if (prandom_u32() & 3)
2019 for (i = 0; i < c->lsave_cnt; i++)
2020 c->lsave[i] = c->main_first;
2022 list_for_each_entry(lprops, &c->empty_list, list)
2023 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2024 list_for_each_entry(lprops, &c->freeable_list, list)
2025 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2026 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2027 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2029 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2030 for (i = 0; i < heap->cnt; i++)
2031 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2032 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2033 for (i = 0; i < heap->cnt; i++)
2034 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2035 heap = &c->lpt_heap[LPROPS_FREE - 1];
2036 for (i = 0; i < heap->cnt; i++)
2037 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;