2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
28 #include <linux/crc16.h>
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_ZNODE, &cnode->flags));
113 __set_bit(COW_ZNODE, &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, alen - 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, alen - 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, alen - offs);
296 err = alloc_lpt_leb(c, &lnum);
300 ubifs_assert(lnum >= c->lpt_first &&
301 lnum <= c->lpt_last);
307 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
310 alen = ALIGN(offs, c->min_io_size);
311 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
312 dbg_chk_lpt_sz(c, 4, alen - offs);
313 err = dbg_chk_lpt_sz(c, 3, alen);
319 ubifs_err("LPT out of space");
320 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
321 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
322 dbg_dump_lpt_info(c);
323 dbg_dump_lpt_lebs(c);
329 * realloc_lpt_leb - allocate an LPT LEB that is empty.
330 * @c: UBIFS file-system description object
331 * @lnum: LEB number is passed and returned here
333 * This function duplicates exactly the results of the function alloc_lpt_leb.
334 * It is used during end commit to reallocate the same LEB numbers that were
335 * allocated by alloc_lpt_leb during start commit.
337 * This function finds the next LEB that was allocated by the alloc_lpt_leb
338 * function starting from @lnum. If a LEB is found it is returned in @lnum and
339 * the function returns %0. Otherwise the function returns -ENOSPC.
340 * Note however, that LPT is designed never to run out of space.
342 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
346 n = *lnum - c->lpt_first + 1;
347 for (i = n; i < c->lpt_lebs; i++)
348 if (c->ltab[i].cmt) {
350 *lnum = i + c->lpt_first;
354 for (i = 0; i < n; i++)
355 if (c->ltab[i].cmt) {
357 *lnum = i + c->lpt_first;
364 * write_cnodes - write cnodes for commit.
365 * @c: UBIFS file-system description object
367 * This function returns %0 on success and a negative error code on failure.
369 static int write_cnodes(struct ubifs_info *c)
371 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
372 struct ubifs_cnode *cnode;
373 void *buf = c->lpt_buf;
375 cnode = c->lpt_cnext;
378 lnum = c->nhead_lnum;
379 offs = c->nhead_offs;
381 /* Ensure empty LEB is unmapped */
383 err = ubifs_leb_unmap(c, lnum);
387 /* Try to place lsave and ltab nicely */
388 done_lsave = !c->big_lpt;
390 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
392 ubifs_pack_lsave(c, buf + offs, c->lsave);
394 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
397 if (offs + c->ltab_sz <= c->leb_size) {
399 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
401 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
404 /* Loop for each cnode */
410 while (offs + len > c->leb_size) {
413 alen = ALIGN(wlen, c->min_io_size);
414 memset(buf + offs, 0xff, alen - wlen);
415 err = ubifs_leb_write(c, lnum, buf + from, from,
416 alen, UBI_SHORTTERM);
419 dbg_chk_lpt_sz(c, 4, alen - wlen);
421 dbg_chk_lpt_sz(c, 2, 0);
422 err = realloc_lpt_leb(c, &lnum);
427 ubifs_assert(lnum >= c->lpt_first &&
428 lnum <= c->lpt_last);
429 err = ubifs_leb_unmap(c, lnum);
432 /* Try to place lsave and ltab nicely */
435 ubifs_pack_lsave(c, buf + offs, c->lsave);
437 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
442 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
444 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
450 ubifs_pack_nnode(c, buf + offs,
451 (struct ubifs_nnode *)cnode);
453 ubifs_pack_pnode(c, buf + offs,
454 (struct ubifs_pnode *)cnode);
456 * The reason for the barriers is the same as in case of TNC.
457 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
458 * 'dirty_cow_pnode()' are the functions for which this is
461 clear_bit(DIRTY_CNODE, &cnode->flags);
462 smp_mb__before_clear_bit();
463 clear_bit(COW_ZNODE, &cnode->flags);
464 smp_mb__after_clear_bit();
466 dbg_chk_lpt_sz(c, 1, len);
467 cnode = cnode->cnext;
468 } while (cnode && cnode != c->lpt_cnext);
470 /* Make sure to place LPT's save table */
472 if (offs + c->lsave_sz > c->leb_size) {
474 alen = ALIGN(wlen, c->min_io_size);
475 memset(buf + offs, 0xff, alen - wlen);
476 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
480 dbg_chk_lpt_sz(c, 2, alen - wlen);
481 err = realloc_lpt_leb(c, &lnum);
485 ubifs_assert(lnum >= c->lpt_first &&
486 lnum <= c->lpt_last);
487 err = ubifs_leb_unmap(c, lnum);
492 ubifs_pack_lsave(c, buf + offs, c->lsave);
494 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
497 /* Make sure to place LPT's own lprops table */
499 if (offs + c->ltab_sz > c->leb_size) {
501 alen = ALIGN(wlen, c->min_io_size);
502 memset(buf + offs, 0xff, alen - wlen);
503 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
507 dbg_chk_lpt_sz(c, 2, alen - wlen);
508 err = realloc_lpt_leb(c, &lnum);
512 ubifs_assert(lnum >= c->lpt_first &&
513 lnum <= c->lpt_last);
514 err = ubifs_leb_unmap(c, lnum);
519 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
521 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
524 /* Write remaining data in buffer */
526 alen = ALIGN(wlen, c->min_io_size);
527 memset(buf + offs, 0xff, alen - wlen);
528 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
532 dbg_chk_lpt_sz(c, 4, alen - wlen);
533 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
537 c->nhead_lnum = lnum;
538 c->nhead_offs = ALIGN(offs, c->min_io_size);
540 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
541 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
542 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
544 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
549 ubifs_err("LPT out of space mismatch");
550 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
551 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
552 dbg_dump_lpt_info(c);
553 dbg_dump_lpt_lebs(c);
559 * next_pnode - find next pnode.
560 * @c: UBIFS file-system description object
563 * This function returns the next pnode or %NULL if there are no more pnodes.
565 static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
566 struct ubifs_pnode *pnode)
568 struct ubifs_nnode *nnode;
571 /* Try to go right */
572 nnode = pnode->parent;
573 iip = pnode->iip + 1;
574 if (iip < UBIFS_LPT_FANOUT) {
575 /* We assume here that LEB zero is never an LPT LEB */
576 if (nnode->nbranch[iip].lnum)
577 return ubifs_get_pnode(c, nnode, iip);
580 /* Go up while can't go right */
582 iip = nnode->iip + 1;
583 nnode = nnode->parent;
586 /* We assume here that LEB zero is never an LPT LEB */
587 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
590 nnode = ubifs_get_nnode(c, nnode, iip);
592 return (void *)nnode;
594 /* Go down to level 1 */
595 while (nnode->level > 1) {
596 nnode = ubifs_get_nnode(c, nnode, 0);
598 return (void *)nnode;
601 return ubifs_get_pnode(c, nnode, 0);
605 * pnode_lookup - lookup a pnode in the LPT.
606 * @c: UBIFS file-system description object
607 * @i: pnode number (0 to main_lebs - 1)
609 * This function returns a pointer to the pnode on success or a negative
610 * error code on failure.
612 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
614 int err, h, iip, shft;
615 struct ubifs_nnode *nnode;
618 err = ubifs_read_nnode(c, NULL, 0);
622 i <<= UBIFS_LPT_FANOUT_SHIFT;
624 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
625 for (h = 1; h < c->lpt_hght; h++) {
626 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
627 shft -= UBIFS_LPT_FANOUT_SHIFT;
628 nnode = ubifs_get_nnode(c, nnode, iip);
630 return ERR_PTR(PTR_ERR(nnode));
632 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
633 return ubifs_get_pnode(c, nnode, iip);
637 * add_pnode_dirt - add dirty space to LPT LEB properties.
638 * @c: UBIFS file-system description object
639 * @pnode: pnode for which to add dirt
641 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
643 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
648 * do_make_pnode_dirty - mark a pnode dirty.
649 * @c: UBIFS file-system description object
650 * @pnode: pnode to mark dirty
652 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
654 /* Assumes cnext list is empty i.e. not called during commit */
655 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
656 struct ubifs_nnode *nnode;
658 c->dirty_pn_cnt += 1;
659 add_pnode_dirt(c, pnode);
660 /* Mark parent and ancestors dirty too */
661 nnode = pnode->parent;
663 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
664 c->dirty_nn_cnt += 1;
665 ubifs_add_nnode_dirt(c, nnode);
666 nnode = nnode->parent;
674 * make_tree_dirty - mark the entire LEB properties tree dirty.
675 * @c: UBIFS file-system description object
677 * This function is used by the "small" LPT model to cause the entire LEB
678 * properties tree to be written. The "small" LPT model does not use LPT
679 * garbage collection because it is more efficient to write the entire tree
680 * (because it is small).
682 * This function returns %0 on success and a negative error code on failure.
684 static int make_tree_dirty(struct ubifs_info *c)
686 struct ubifs_pnode *pnode;
688 pnode = pnode_lookup(c, 0);
690 do_make_pnode_dirty(c, pnode);
691 pnode = next_pnode(c, pnode);
693 return PTR_ERR(pnode);
699 * need_write_all - determine if the LPT area is running out of free space.
700 * @c: UBIFS file-system description object
702 * This function returns %1 if the LPT area is running out of free space and %0
705 static int need_write_all(struct ubifs_info *c)
710 for (i = 0; i < c->lpt_lebs; i++) {
711 if (i + c->lpt_first == c->nhead_lnum)
712 free += c->leb_size - c->nhead_offs;
713 else if (c->ltab[i].free == c->leb_size)
715 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
718 /* Less than twice the size left */
719 if (free <= c->lpt_sz * 2)
725 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
726 * @c: UBIFS file-system description object
728 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
729 * free space and so may be reused as soon as the next commit is completed.
730 * This function is called during start commit to mark LPT LEBs for trivial GC.
732 static void lpt_tgc_start(struct ubifs_info *c)
736 for (i = 0; i < c->lpt_lebs; i++) {
737 if (i + c->lpt_first == c->nhead_lnum)
739 if (c->ltab[i].dirty > 0 &&
740 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
742 c->ltab[i].free = c->leb_size;
743 c->ltab[i].dirty = 0;
744 dbg_lp("LEB %d", i + c->lpt_first);
750 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
751 * @c: UBIFS file-system description object
753 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
754 * free space and so may be reused as soon as the next commit is completed.
755 * This function is called after the commit is completed (master node has been
756 * written) and un-maps LPT LEBs that were marked for trivial GC.
758 static int lpt_tgc_end(struct ubifs_info *c)
762 for (i = 0; i < c->lpt_lebs; i++)
763 if (c->ltab[i].tgc) {
764 err = ubifs_leb_unmap(c, i + c->lpt_first);
768 dbg_lp("LEB %d", i + c->lpt_first);
774 * populate_lsave - fill the lsave array with important LEB numbers.
775 * @c: the UBIFS file-system description object
777 * This function is only called for the "big" model. It records a small number
778 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
779 * most important to least important): empty, freeable, freeable index, dirty
780 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
781 * their pnodes into memory. That will stop us from having to scan the LPT
782 * straight away. For the "small" model we assume that scanning the LPT is no
785 static void populate_lsave(struct ubifs_info *c)
787 struct ubifs_lprops *lprops;
788 struct ubifs_lpt_heap *heap;
791 ubifs_assert(c->big_lpt);
792 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
793 c->lpt_drty_flgs |= LSAVE_DIRTY;
794 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
796 list_for_each_entry(lprops, &c->empty_list, list) {
797 c->lsave[cnt++] = lprops->lnum;
798 if (cnt >= c->lsave_cnt)
801 list_for_each_entry(lprops, &c->freeable_list, list) {
802 c->lsave[cnt++] = lprops->lnum;
803 if (cnt >= c->lsave_cnt)
806 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
807 c->lsave[cnt++] = lprops->lnum;
808 if (cnt >= c->lsave_cnt)
811 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
812 for (i = 0; i < heap->cnt; i++) {
813 c->lsave[cnt++] = heap->arr[i]->lnum;
814 if (cnt >= c->lsave_cnt)
817 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
818 for (i = 0; i < heap->cnt; i++) {
819 c->lsave[cnt++] = heap->arr[i]->lnum;
820 if (cnt >= c->lsave_cnt)
823 heap = &c->lpt_heap[LPROPS_FREE - 1];
824 for (i = 0; i < heap->cnt; i++) {
825 c->lsave[cnt++] = heap->arr[i]->lnum;
826 if (cnt >= c->lsave_cnt)
829 /* Fill it up completely */
830 while (cnt < c->lsave_cnt)
831 c->lsave[cnt++] = c->main_first;
835 * nnode_lookup - lookup a nnode in the LPT.
836 * @c: UBIFS file-system description object
839 * This function returns a pointer to the nnode on success or a negative
840 * error code on failure.
842 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
845 struct ubifs_nnode *nnode;
848 err = ubifs_read_nnode(c, NULL, 0);
854 iip = i & (UBIFS_LPT_FANOUT - 1);
855 i >>= UBIFS_LPT_FANOUT_SHIFT;
858 nnode = ubifs_get_nnode(c, nnode, iip);
866 * make_nnode_dirty - find a nnode and, if found, make it dirty.
867 * @c: UBIFS file-system description object
868 * @node_num: nnode number of nnode to make dirty
869 * @lnum: LEB number where nnode was written
870 * @offs: offset where nnode was written
872 * This function is used by LPT garbage collection. LPT garbage collection is
873 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
874 * simply involves marking all the nodes in the LEB being garbage-collected as
875 * dirty. The dirty nodes are written next commit, after which the LEB is free
878 * This function returns %0 on success and a negative error code on failure.
880 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
883 struct ubifs_nnode *nnode;
885 nnode = nnode_lookup(c, node_num);
887 return PTR_ERR(nnode);
889 struct ubifs_nbranch *branch;
891 branch = &nnode->parent->nbranch[nnode->iip];
892 if (branch->lnum != lnum || branch->offs != offs)
893 return 0; /* nnode is obsolete */
894 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
895 return 0; /* nnode is obsolete */
896 /* Assumes cnext list is empty i.e. not called during commit */
897 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
898 c->dirty_nn_cnt += 1;
899 ubifs_add_nnode_dirt(c, nnode);
900 /* Mark parent and ancestors dirty too */
901 nnode = nnode->parent;
903 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
904 c->dirty_nn_cnt += 1;
905 ubifs_add_nnode_dirt(c, nnode);
906 nnode = nnode->parent;
915 * make_pnode_dirty - find a pnode and, if found, make it dirty.
916 * @c: UBIFS file-system description object
917 * @node_num: pnode number of pnode to make dirty
918 * @lnum: LEB number where pnode was written
919 * @offs: offset where pnode was written
921 * This function is used by LPT garbage collection. LPT garbage collection is
922 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
923 * simply involves marking all the nodes in the LEB being garbage-collected as
924 * dirty. The dirty nodes are written next commit, after which the LEB is free
927 * This function returns %0 on success and a negative error code on failure.
929 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
932 struct ubifs_pnode *pnode;
933 struct ubifs_nbranch *branch;
935 pnode = pnode_lookup(c, node_num);
937 return PTR_ERR(pnode);
938 branch = &pnode->parent->nbranch[pnode->iip];
939 if (branch->lnum != lnum || branch->offs != offs)
941 do_make_pnode_dirty(c, pnode);
946 * make_ltab_dirty - make ltab node dirty.
947 * @c: UBIFS file-system description object
948 * @lnum: LEB number where ltab was written
949 * @offs: offset where ltab was written
951 * This function is used by LPT garbage collection. LPT garbage collection is
952 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
953 * simply involves marking all the nodes in the LEB being garbage-collected as
954 * dirty. The dirty nodes are written next commit, after which the LEB is free
957 * This function returns %0 on success and a negative error code on failure.
959 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
961 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
962 return 0; /* This ltab node is obsolete */
963 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
964 c->lpt_drty_flgs |= LTAB_DIRTY;
965 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
971 * make_lsave_dirty - make lsave node dirty.
972 * @c: UBIFS file-system description object
973 * @lnum: LEB number where lsave was written
974 * @offs: offset where lsave was written
976 * This function is used by LPT garbage collection. LPT garbage collection is
977 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
978 * simply involves marking all the nodes in the LEB being garbage-collected as
979 * dirty. The dirty nodes are written next commit, after which the LEB is free
982 * This function returns %0 on success and a negative error code on failure.
984 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
986 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
987 return 0; /* This lsave node is obsolete */
988 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
989 c->lpt_drty_flgs |= LSAVE_DIRTY;
990 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
996 * make_node_dirty - make node dirty.
997 * @c: UBIFS file-system description object
998 * @node_type: LPT node type
999 * @node_num: node number
1000 * @lnum: LEB number where node was written
1001 * @offs: offset where node was written
1003 * This function is used by LPT garbage collection. LPT garbage collection is
1004 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1005 * simply involves marking all the nodes in the LEB being garbage-collected as
1006 * dirty. The dirty nodes are written next commit, after which the LEB is free
1009 * This function returns %0 on success and a negative error code on failure.
1011 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1014 switch (node_type) {
1015 case UBIFS_LPT_NNODE:
1016 return make_nnode_dirty(c, node_num, lnum, offs);
1017 case UBIFS_LPT_PNODE:
1018 return make_pnode_dirty(c, node_num, lnum, offs);
1019 case UBIFS_LPT_LTAB:
1020 return make_ltab_dirty(c, lnum, offs);
1021 case UBIFS_LPT_LSAVE:
1022 return make_lsave_dirty(c, lnum, offs);
1028 * get_lpt_node_len - return the length of a node based on its type.
1029 * @c: UBIFS file-system description object
1030 * @node_type: LPT node type
1032 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1034 switch (node_type) {
1035 case UBIFS_LPT_NNODE:
1037 case UBIFS_LPT_PNODE:
1039 case UBIFS_LPT_LTAB:
1041 case UBIFS_LPT_LSAVE:
1048 * get_pad_len - return the length of padding in a buffer.
1049 * @c: UBIFS file-system description object
1051 * @len: length of buffer
1053 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1057 if (c->min_io_size == 1)
1059 offs = c->leb_size - len;
1060 pad_len = ALIGN(offs, c->min_io_size) - offs;
1065 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1066 * @c: UBIFS file-system description object
1068 * @node_num: node number is returned here
1070 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1073 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1074 int pos = 0, node_type;
1076 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1077 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1082 * is_a_node - determine if a buffer contains a node.
1083 * @c: UBIFS file-system description object
1085 * @len: length of buffer
1087 * This function returns %1 if the buffer contains a node or %0 if it does not.
1089 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1091 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1092 int pos = 0, node_type, node_len;
1093 uint16_t crc, calc_crc;
1095 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1097 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1098 if (node_type == UBIFS_LPT_NOT_A_NODE)
1100 node_len = get_lpt_node_len(c, node_type);
1101 if (!node_len || node_len > len)
1105 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1106 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1107 node_len - UBIFS_LPT_CRC_BYTES);
1108 if (crc != calc_crc)
1114 * lpt_gc_lnum - garbage collect a LPT LEB.
1115 * @c: UBIFS file-system description object
1116 * @lnum: LEB number to garbage collect
1118 * LPT garbage collection is used only for the "big" LPT model
1119 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1120 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1121 * next commit, after which the LEB is free to be reused.
1123 * This function returns %0 on success and a negative error code on failure.
1125 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1127 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1128 void *buf = c->lpt_buf;
1130 dbg_lp("LEB %d", lnum);
1131 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1133 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1137 if (!is_a_node(c, buf, len)) {
1140 pad_len = get_pad_len(c, buf, len);
1148 node_type = get_lpt_node_type(c, buf, &node_num);
1149 node_len = get_lpt_node_len(c, node_type);
1150 offs = c->leb_size - len;
1151 ubifs_assert(node_len != 0);
1152 mutex_lock(&c->lp_mutex);
1153 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1154 mutex_unlock(&c->lp_mutex);
1164 * lpt_gc - LPT garbage collection.
1165 * @c: UBIFS file-system description object
1167 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1168 * Returns %0 on success and a negative error code on failure.
1170 static int lpt_gc(struct ubifs_info *c)
1172 int i, lnum = -1, dirty = 0;
1174 mutex_lock(&c->lp_mutex);
1175 for (i = 0; i < c->lpt_lebs; i++) {
1176 ubifs_assert(!c->ltab[i].tgc);
1177 if (i + c->lpt_first == c->nhead_lnum ||
1178 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1180 if (c->ltab[i].dirty > dirty) {
1181 dirty = c->ltab[i].dirty;
1182 lnum = i + c->lpt_first;
1185 mutex_unlock(&c->lp_mutex);
1188 return lpt_gc_lnum(c, lnum);
1192 * ubifs_lpt_start_commit - UBIFS commit starts.
1193 * @c: the UBIFS file-system description object
1195 * This function has to be called when UBIFS starts the commit operation.
1196 * This function "freezes" all currently dirty LEB properties and does not
1197 * change them anymore. Further changes are saved and tracked separately
1198 * because they are not part of this commit. This function returns zero in case
1199 * of success and a negative error code in case of failure.
1201 int ubifs_lpt_start_commit(struct ubifs_info *c)
1207 mutex_lock(&c->lp_mutex);
1208 err = dbg_chk_lpt_free_spc(c);
1211 err = dbg_check_ltab(c);
1215 if (c->check_lpt_free) {
1217 * We ensure there is enough free space in
1218 * ubifs_lpt_post_commit() by marking nodes dirty. That
1219 * information is lost when we unmount, so we also need
1220 * to check free space once after mounting also.
1222 c->check_lpt_free = 0;
1223 while (need_write_all(c)) {
1224 mutex_unlock(&c->lp_mutex);
1228 mutex_lock(&c->lp_mutex);
1234 if (!c->dirty_pn_cnt) {
1235 dbg_cmt("no cnodes to commit");
1240 if (!c->big_lpt && need_write_all(c)) {
1241 /* If needed, write everything */
1242 err = make_tree_dirty(c);
1251 cnt = get_cnodes_to_commit(c);
1252 ubifs_assert(cnt != 0);
1254 err = layout_cnodes(c);
1258 /* Copy the LPT's own lprops for end commit to write */
1259 memcpy(c->ltab_cmt, c->ltab,
1260 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1261 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1264 mutex_unlock(&c->lp_mutex);
1269 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1270 * @c: UBIFS file-system description object
1272 static void free_obsolete_cnodes(struct ubifs_info *c)
1274 struct ubifs_cnode *cnode, *cnext;
1276 cnext = c->lpt_cnext;
1281 cnext = cnode->cnext;
1282 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1285 cnode->cnext = NULL;
1286 } while (cnext != c->lpt_cnext);
1287 c->lpt_cnext = NULL;
1291 * ubifs_lpt_end_commit - finish the commit operation.
1292 * @c: the UBIFS file-system description object
1294 * This function has to be called when the commit operation finishes. It
1295 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1296 * the media. Returns zero in case of success and a negative error code in case
1299 int ubifs_lpt_end_commit(struct ubifs_info *c)
1308 err = write_cnodes(c);
1312 mutex_lock(&c->lp_mutex);
1313 free_obsolete_cnodes(c);
1314 mutex_unlock(&c->lp_mutex);
1320 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1321 * @c: UBIFS file-system description object
1323 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1324 * commit for the "big" LPT model.
1326 int ubifs_lpt_post_commit(struct ubifs_info *c)
1330 mutex_lock(&c->lp_mutex);
1331 err = lpt_tgc_end(c);
1335 while (need_write_all(c)) {
1336 mutex_unlock(&c->lp_mutex);
1340 mutex_lock(&c->lp_mutex);
1343 mutex_unlock(&c->lp_mutex);
1348 * first_nnode - find the first nnode in memory.
1349 * @c: UBIFS file-system description object
1350 * @hght: height of tree where nnode found is returned here
1352 * This function returns a pointer to the nnode found or %NULL if no nnode is
1353 * found. This function is a helper to 'ubifs_lpt_free()'.
1355 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1357 struct ubifs_nnode *nnode;
1364 for (h = 1; h < c->lpt_hght; h++) {
1366 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1367 if (nnode->nbranch[i].nnode) {
1369 nnode = nnode->nbranch[i].nnode;
1381 * next_nnode - find the next nnode in memory.
1382 * @c: UBIFS file-system description object
1383 * @nnode: nnode from which to start.
1384 * @hght: height of tree where nnode is, is passed and returned here
1386 * This function returns a pointer to the nnode found or %NULL if no nnode is
1387 * found. This function is a helper to 'ubifs_lpt_free()'.
1389 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1390 struct ubifs_nnode *nnode, int *hght)
1392 struct ubifs_nnode *parent;
1393 int iip, h, i, found;
1395 parent = nnode->parent;
1398 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1402 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1403 nnode = parent->nbranch[iip].nnode;
1411 for (h = *hght + 1; h < c->lpt_hght; h++) {
1413 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1414 if (nnode->nbranch[i].nnode) {
1416 nnode = nnode->nbranch[i].nnode;
1428 * ubifs_lpt_free - free resources owned by the LPT.
1429 * @c: UBIFS file-system description object
1430 * @wr_only: free only resources used for writing
1432 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1434 struct ubifs_nnode *nnode;
1437 /* Free write-only things first */
1439 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1451 /* Now free the rest */
1453 nnode = first_nnode(c, &hght);
1455 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1456 kfree(nnode->nbranch[i].nnode);
1457 nnode = next_nnode(c, nnode, &hght);
1459 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1460 kfree(c->lpt_heap[i].arr);
1461 kfree(c->dirty_idx.arr);
1464 kfree(c->lpt_nod_buf);
1467 #ifdef CONFIG_UBIFS_FS_DEBUG
1470 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1472 * @len: buffer length
1474 static int dbg_is_all_ff(uint8_t *buf, int len)
1478 for (i = 0; i < len; i++)
1485 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1486 * @c: the UBIFS file-system description object
1487 * @lnum: LEB number where nnode was written
1488 * @offs: offset where nnode was written
1490 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1492 struct ubifs_nnode *nnode;
1495 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1496 nnode = first_nnode(c, &hght);
1497 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1498 struct ubifs_nbranch *branch;
1501 if (nnode->parent) {
1502 branch = &nnode->parent->nbranch[nnode->iip];
1503 if (branch->lnum != lnum || branch->offs != offs)
1505 if (test_bit(DIRTY_CNODE, &nnode->flags))
1509 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1511 if (test_bit(DIRTY_CNODE, &nnode->flags))
1520 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1521 * @c: the UBIFS file-system description object
1522 * @lnum: LEB number where pnode was written
1523 * @offs: offset where pnode was written
1525 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1529 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1530 for (i = 0; i < cnt; i++) {
1531 struct ubifs_pnode *pnode;
1532 struct ubifs_nbranch *branch;
1535 pnode = pnode_lookup(c, i);
1537 return PTR_ERR(pnode);
1538 branch = &pnode->parent->nbranch[pnode->iip];
1539 if (branch->lnum != lnum || branch->offs != offs)
1541 if (test_bit(DIRTY_CNODE, &pnode->flags))
1549 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1550 * @c: the UBIFS file-system description object
1551 * @lnum: LEB number where ltab node was written
1552 * @offs: offset where ltab node was written
1554 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1556 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1558 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1562 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1563 * @c: the UBIFS file-system description object
1564 * @lnum: LEB number where lsave node was written
1565 * @offs: offset where lsave node was written
1567 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1569 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1571 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1575 * dbg_is_node_dirty - determine if a node is dirty.
1576 * @c: the UBIFS file-system description object
1577 * @node_type: node type
1578 * @lnum: LEB number where node was written
1579 * @offs: offset where node was written
1581 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1584 switch (node_type) {
1585 case UBIFS_LPT_NNODE:
1586 return dbg_is_nnode_dirty(c, lnum, offs);
1587 case UBIFS_LPT_PNODE:
1588 return dbg_is_pnode_dirty(c, lnum, offs);
1589 case UBIFS_LPT_LTAB:
1590 return dbg_is_ltab_dirty(c, lnum, offs);
1591 case UBIFS_LPT_LSAVE:
1592 return dbg_is_lsave_dirty(c, lnum, offs);
1598 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1599 * @c: the UBIFS file-system description object
1600 * @lnum: LEB number where node was written
1601 * @offs: offset where node was written
1603 * This function returns %0 on success and a negative error code on failure.
1605 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1607 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1609 void *buf = c->dbg->buf;
1611 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1614 dbg_lp("LEB %d", lnum);
1615 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1617 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1621 if (!is_a_node(c, buf, len)) {
1624 pad_len = get_pad_len(c, buf, len);
1631 if (!dbg_is_all_ff(buf, len)) {
1632 dbg_msg("invalid empty space in LEB %d at %d",
1633 lnum, c->leb_size - len);
1636 i = lnum - c->lpt_first;
1637 if (len != c->ltab[i].free) {
1638 dbg_msg("invalid free space in LEB %d "
1639 "(free %d, expected %d)",
1640 lnum, len, c->ltab[i].free);
1643 if (dirty != c->ltab[i].dirty) {
1644 dbg_msg("invalid dirty space in LEB %d "
1645 "(dirty %d, expected %d)",
1646 lnum, dirty, c->ltab[i].dirty);
1651 node_type = get_lpt_node_type(c, buf, &node_num);
1652 node_len = get_lpt_node_len(c, node_type);
1653 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1662 * dbg_check_ltab - check the free and dirty space in the ltab.
1663 * @c: the UBIFS file-system description object
1665 * This function returns %0 on success and a negative error code on failure.
1667 int dbg_check_ltab(struct ubifs_info *c)
1669 int lnum, err, i, cnt;
1671 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1674 /* Bring the entire tree into memory */
1675 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1676 for (i = 0; i < cnt; i++) {
1677 struct ubifs_pnode *pnode;
1679 pnode = pnode_lookup(c, i);
1681 return PTR_ERR(pnode);
1686 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1690 /* Check each LEB */
1691 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1692 err = dbg_check_ltab_lnum(c, lnum);
1694 dbg_err("failed at LEB %d", lnum);
1699 dbg_lp("succeeded");
1704 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1705 * @c: the UBIFS file-system description object
1707 * This function returns %0 on success and a negative error code on failure.
1709 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1714 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1717 for (i = 0; i < c->lpt_lebs; i++) {
1718 if (c->ltab[i].tgc || c->ltab[i].cmt)
1720 if (i + c->lpt_first == c->nhead_lnum)
1721 free += c->leb_size - c->nhead_offs;
1722 else if (c->ltab[i].free == c->leb_size)
1723 free += c->leb_size;
1725 if (free < c->lpt_sz) {
1726 dbg_err("LPT space error: free %lld lpt_sz %lld",
1728 dbg_dump_lpt_info(c);
1729 dbg_dump_lpt_lebs(c);
1737 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1738 * @c: the UBIFS file-system description object
1740 * @len: length written
1742 * This function returns %0 on success and a negative error code on failure.
1744 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1746 struct ubifs_debug_info *d = c->dbg;
1747 long long chk_lpt_sz, lpt_sz;
1750 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1757 d->chk_lpt_lebs = 0;
1758 d->chk_lpt_wastage = 0;
1759 if (c->dirty_pn_cnt > c->pnode_cnt) {
1760 dbg_err("dirty pnodes %d exceed max %d",
1761 c->dirty_pn_cnt, c->pnode_cnt);
1764 if (c->dirty_nn_cnt > c->nnode_cnt) {
1765 dbg_err("dirty nnodes %d exceed max %d",
1766 c->dirty_nn_cnt, c->nnode_cnt);
1771 d->chk_lpt_sz += len;
1774 d->chk_lpt_sz += len;
1775 d->chk_lpt_wastage += len;
1776 d->chk_lpt_lebs += 1;
1779 chk_lpt_sz = c->leb_size;
1780 chk_lpt_sz *= d->chk_lpt_lebs;
1781 chk_lpt_sz += len - c->nhead_offs;
1782 if (d->chk_lpt_sz != chk_lpt_sz) {
1783 dbg_err("LPT wrote %lld but space used was %lld",
1784 d->chk_lpt_sz, chk_lpt_sz);
1787 if (d->chk_lpt_sz > c->lpt_sz) {
1788 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1789 d->chk_lpt_sz, c->lpt_sz);
1792 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1793 dbg_err("LPT layout size %lld but wrote %lld",
1794 d->chk_lpt_sz, d->chk_lpt_sz2);
1797 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1798 dbg_err("LPT new nhead offs: expected %d was %d",
1799 d->new_nhead_offs, len);
1802 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1803 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1804 lpt_sz += c->ltab_sz;
1806 lpt_sz += c->lsave_sz;
1807 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1808 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1809 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1813 dbg_dump_lpt_info(c);
1814 dbg_dump_lpt_lebs(c);
1817 d->chk_lpt_sz2 = d->chk_lpt_sz;
1819 d->chk_lpt_wastage = 0;
1820 d->chk_lpt_lebs = 0;
1821 d->new_nhead_offs = len;
1824 d->chk_lpt_sz += len;
1825 d->chk_lpt_wastage += len;
1833 * dbg_dump_lpt_leb - dump an LPT LEB.
1834 * @c: UBIFS file-system description object
1835 * @lnum: LEB number to dump
1837 * This function dumps an LEB from LPT area. Nodes in this area are very
1838 * different to nodes in the main area (e.g., they do not have common headers,
1839 * they do not have 8-byte alignments, etc), so we have a separate function to
1840 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1842 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1844 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1845 void *buf = c->dbg->buf;
1847 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1848 current->pid, lnum);
1849 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1851 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1855 offs = c->leb_size - len;
1856 if (!is_a_node(c, buf, len)) {
1859 pad_len = get_pad_len(c, buf, len);
1861 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1862 lnum, offs, pad_len);
1868 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1873 node_type = get_lpt_node_type(c, buf, &node_num);
1874 switch (node_type) {
1875 case UBIFS_LPT_PNODE:
1877 node_len = c->pnode_sz;
1879 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1880 lnum, offs, node_num);
1882 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1886 case UBIFS_LPT_NNODE:
1889 struct ubifs_nnode nnode;
1891 node_len = c->nnode_sz;
1893 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1894 lnum, offs, node_num);
1896 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1898 err = ubifs_unpack_nnode(c, buf, &nnode);
1899 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1900 printk("%d:%d", nnode.nbranch[i].lnum,
1901 nnode.nbranch[i].offs);
1902 if (i != UBIFS_LPT_FANOUT - 1)
1908 case UBIFS_LPT_LTAB:
1909 node_len = c->ltab_sz;
1910 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1913 case UBIFS_LPT_LSAVE:
1914 node_len = c->lsave_sz;
1915 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1918 ubifs_err("LPT node type %d not recognized", node_type);
1926 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1927 current->pid, lnum);
1931 * dbg_dump_lpt_lebs - dump LPT lebs.
1932 * @c: UBIFS file-system description object
1934 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1937 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1941 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
1943 for (i = 0; i < c->lpt_lebs; i++)
1944 dump_lpt_leb(c, i + c->lpt_first);
1945 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
1949 #endif /* CONFIG_UBIFS_FS_DEBUG */