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);
327 * realloc_lpt_leb - allocate an LPT LEB that is empty.
328 * @c: UBIFS file-system description object
329 * @lnum: LEB number is passed and returned here
331 * This function duplicates exactly the results of the function alloc_lpt_leb.
332 * It is used during end commit to reallocate the same LEB numbers that were
333 * allocated by alloc_lpt_leb during start commit.
335 * This function finds the next LEB that was allocated by the alloc_lpt_leb
336 * function starting from @lnum. If a LEB is found it is returned in @lnum and
337 * the function returns %0. Otherwise the function returns -ENOSPC.
338 * Note however, that LPT is designed never to run out of space.
340 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
344 n = *lnum - c->lpt_first + 1;
345 for (i = n; i < c->lpt_lebs; i++)
346 if (c->ltab[i].cmt) {
348 *lnum = i + c->lpt_first;
352 for (i = 0; i < n; i++)
353 if (c->ltab[i].cmt) {
355 *lnum = i + c->lpt_first;
362 * write_cnodes - write cnodes for commit.
363 * @c: UBIFS file-system description object
365 * This function returns %0 on success and a negative error code on failure.
367 static int write_cnodes(struct ubifs_info *c)
369 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
370 struct ubifs_cnode *cnode;
371 void *buf = c->lpt_buf;
373 cnode = c->lpt_cnext;
376 lnum = c->nhead_lnum;
377 offs = c->nhead_offs;
379 /* Ensure empty LEB is unmapped */
381 err = ubifs_leb_unmap(c, lnum);
385 /* Try to place lsave and ltab nicely */
386 done_lsave = !c->big_lpt;
388 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
390 ubifs_pack_lsave(c, buf + offs, c->lsave);
392 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
395 if (offs + c->ltab_sz <= c->leb_size) {
397 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
399 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
402 /* Loop for each cnode */
408 while (offs + len > c->leb_size) {
411 alen = ALIGN(wlen, c->min_io_size);
412 memset(buf + offs, 0xff, alen - wlen);
413 err = ubifs_leb_write(c, lnum, buf + from, from,
414 alen, UBI_SHORTTERM);
417 dbg_chk_lpt_sz(c, 4, alen - wlen);
419 dbg_chk_lpt_sz(c, 2, 0);
420 err = realloc_lpt_leb(c, &lnum);
425 ubifs_assert(lnum >= c->lpt_first &&
426 lnum <= c->lpt_last);
427 err = ubifs_leb_unmap(c, lnum);
430 /* Try to place lsave and ltab nicely */
433 ubifs_pack_lsave(c, buf + offs, c->lsave);
435 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
440 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
442 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
448 ubifs_pack_nnode(c, buf + offs,
449 (struct ubifs_nnode *)cnode);
451 ubifs_pack_pnode(c, buf + offs,
452 (struct ubifs_pnode *)cnode);
454 * The reason for the barriers is the same as in case of TNC.
455 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
456 * 'dirty_cow_pnode()' are the functions for which this is
459 clear_bit(DIRTY_CNODE, &cnode->flags);
460 smp_mb__before_clear_bit();
461 clear_bit(COW_ZNODE, &cnode->flags);
462 smp_mb__after_clear_bit();
464 dbg_chk_lpt_sz(c, 1, len);
465 cnode = cnode->cnext;
466 } while (cnode && cnode != c->lpt_cnext);
468 /* Make sure to place LPT's save table */
470 if (offs + c->lsave_sz > c->leb_size) {
472 alen = ALIGN(wlen, c->min_io_size);
473 memset(buf + offs, 0xff, alen - wlen);
474 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
478 dbg_chk_lpt_sz(c, 2, alen - wlen);
479 err = realloc_lpt_leb(c, &lnum);
483 ubifs_assert(lnum >= c->lpt_first &&
484 lnum <= c->lpt_last);
485 err = ubifs_leb_unmap(c, lnum);
490 ubifs_pack_lsave(c, buf + offs, c->lsave);
492 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
495 /* Make sure to place LPT's own lprops table */
497 if (offs + c->ltab_sz > c->leb_size) {
499 alen = ALIGN(wlen, c->min_io_size);
500 memset(buf + offs, 0xff, alen - wlen);
501 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
505 dbg_chk_lpt_sz(c, 2, alen - wlen);
506 err = realloc_lpt_leb(c, &lnum);
510 ubifs_assert(lnum >= c->lpt_first &&
511 lnum <= c->lpt_last);
512 err = ubifs_leb_unmap(c, lnum);
517 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
519 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
522 /* Write remaining data in buffer */
524 alen = ALIGN(wlen, c->min_io_size);
525 memset(buf + offs, 0xff, alen - wlen);
526 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
530 dbg_chk_lpt_sz(c, 4, alen - wlen);
531 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
535 c->nhead_lnum = lnum;
536 c->nhead_offs = ALIGN(offs, c->min_io_size);
538 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
539 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
540 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
542 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
547 ubifs_err("LPT out of space mismatch");
548 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
549 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
550 dbg_dump_lpt_info(c);
555 * next_pnode - find next pnode.
556 * @c: UBIFS file-system description object
559 * This function returns the next pnode or %NULL if there are no more pnodes.
561 static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
562 struct ubifs_pnode *pnode)
564 struct ubifs_nnode *nnode;
567 /* Try to go right */
568 nnode = pnode->parent;
569 iip = pnode->iip + 1;
570 if (iip < UBIFS_LPT_FANOUT) {
571 /* We assume here that LEB zero is never an LPT LEB */
572 if (nnode->nbranch[iip].lnum)
573 return ubifs_get_pnode(c, nnode, iip);
578 /* Go up while can't go right */
580 iip = nnode->iip + 1;
581 nnode = nnode->parent;
584 /* We assume here that LEB zero is never an LPT LEB */
585 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
588 nnode = ubifs_get_nnode(c, nnode, iip);
590 return (void *)nnode;
592 /* Go down to level 1 */
593 while (nnode->level > 1) {
594 nnode = ubifs_get_nnode(c, nnode, 0);
596 return (void *)nnode;
599 return ubifs_get_pnode(c, nnode, 0);
603 * pnode_lookup - lookup a pnode in the LPT.
604 * @c: UBIFS file-system description object
605 * @i: pnode number (0 to main_lebs - 1)
607 * This function returns a pointer to the pnode on success or a negative
608 * error code on failure.
610 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
612 int err, h, iip, shft;
613 struct ubifs_nnode *nnode;
616 err = ubifs_read_nnode(c, NULL, 0);
620 i <<= UBIFS_LPT_FANOUT_SHIFT;
622 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
623 for (h = 1; h < c->lpt_hght; h++) {
624 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
625 shft -= UBIFS_LPT_FANOUT_SHIFT;
626 nnode = ubifs_get_nnode(c, nnode, iip);
628 return ERR_PTR(PTR_ERR(nnode));
630 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
631 return ubifs_get_pnode(c, nnode, iip);
635 * add_pnode_dirt - add dirty space to LPT LEB properties.
636 * @c: UBIFS file-system description object
637 * @pnode: pnode for which to add dirt
639 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
641 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
646 * do_make_pnode_dirty - mark a pnode dirty.
647 * @c: UBIFS file-system description object
648 * @pnode: pnode to mark dirty
650 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
652 /* Assumes cnext list is empty i.e. not called during commit */
653 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
654 struct ubifs_nnode *nnode;
656 c->dirty_pn_cnt += 1;
657 add_pnode_dirt(c, pnode);
658 /* Mark parent and ancestors dirty too */
659 nnode = pnode->parent;
661 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
662 c->dirty_nn_cnt += 1;
663 ubifs_add_nnode_dirt(c, nnode);
664 nnode = nnode->parent;
672 * make_tree_dirty - mark the entire LEB properties tree dirty.
673 * @c: UBIFS file-system description object
675 * This function is used by the "small" LPT model to cause the entire LEB
676 * properties tree to be written. The "small" LPT model does not use LPT
677 * garbage collection because it is more efficient to write the entire tree
678 * (because it is small).
680 * This function returns %0 on success and a negative error code on failure.
682 static int make_tree_dirty(struct ubifs_info *c)
684 struct ubifs_pnode *pnode;
686 pnode = pnode_lookup(c, 0);
688 do_make_pnode_dirty(c, pnode);
689 pnode = next_pnode(c, pnode);
691 return PTR_ERR(pnode);
697 * need_write_all - determine if the LPT area is running out of free space.
698 * @c: UBIFS file-system description object
700 * This function returns %1 if the LPT area is running out of free space and %0
703 static int need_write_all(struct ubifs_info *c)
708 for (i = 0; i < c->lpt_lebs; i++) {
709 if (i + c->lpt_first == c->nhead_lnum)
710 free += c->leb_size - c->nhead_offs;
711 else if (c->ltab[i].free == c->leb_size)
713 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
716 /* Less than twice the size left */
717 if (free <= c->lpt_sz * 2)
723 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
724 * @c: UBIFS file-system description object
726 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
727 * free space and so may be reused as soon as the next commit is completed.
728 * This function is called during start commit to mark LPT LEBs for trivial GC.
730 static void lpt_tgc_start(struct ubifs_info *c)
734 for (i = 0; i < c->lpt_lebs; i++) {
735 if (i + c->lpt_first == c->nhead_lnum)
737 if (c->ltab[i].dirty > 0 &&
738 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
740 c->ltab[i].free = c->leb_size;
741 c->ltab[i].dirty = 0;
742 dbg_lp("LEB %d", i + c->lpt_first);
748 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
749 * @c: UBIFS file-system description object
751 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
752 * free space and so may be reused as soon as the next commit is completed.
753 * This function is called after the commit is completed (master node has been
754 * written) and unmaps LPT LEBs that were marked for trivial GC.
756 static int lpt_tgc_end(struct ubifs_info *c)
760 for (i = 0; i < c->lpt_lebs; i++)
761 if (c->ltab[i].tgc) {
762 err = ubifs_leb_unmap(c, i + c->lpt_first);
766 dbg_lp("LEB %d", i + c->lpt_first);
772 * populate_lsave - fill the lsave array with important LEB numbers.
773 * @c: the UBIFS file-system description object
775 * This function is only called for the "big" model. It records a small number
776 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
777 * most important to least important): empty, freeable, freeable index, dirty
778 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
779 * their pnodes into memory. That will stop us from having to scan the LPT
780 * straight away. For the "small" model we assume that scanning the LPT is no
783 static void populate_lsave(struct ubifs_info *c)
785 struct ubifs_lprops *lprops;
786 struct ubifs_lpt_heap *heap;
789 ubifs_assert(c->big_lpt);
790 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
791 c->lpt_drty_flgs |= LSAVE_DIRTY;
792 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
794 list_for_each_entry(lprops, &c->empty_list, list) {
795 c->lsave[cnt++] = lprops->lnum;
796 if (cnt >= c->lsave_cnt)
799 list_for_each_entry(lprops, &c->freeable_list, list) {
800 c->lsave[cnt++] = lprops->lnum;
801 if (cnt >= c->lsave_cnt)
804 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
805 c->lsave[cnt++] = lprops->lnum;
806 if (cnt >= c->lsave_cnt)
809 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
810 for (i = 0; i < heap->cnt; i++) {
811 c->lsave[cnt++] = heap->arr[i]->lnum;
812 if (cnt >= c->lsave_cnt)
815 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
816 for (i = 0; i < heap->cnt; i++) {
817 c->lsave[cnt++] = heap->arr[i]->lnum;
818 if (cnt >= c->lsave_cnt)
821 heap = &c->lpt_heap[LPROPS_FREE - 1];
822 for (i = 0; i < heap->cnt; i++) {
823 c->lsave[cnt++] = heap->arr[i]->lnum;
824 if (cnt >= c->lsave_cnt)
827 /* Fill it up completely */
828 while (cnt < c->lsave_cnt)
829 c->lsave[cnt++] = c->main_first;
833 * nnode_lookup - lookup a nnode in the LPT.
834 * @c: UBIFS file-system description object
837 * This function returns a pointer to the nnode on success or a negative
838 * error code on failure.
840 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
843 struct ubifs_nnode *nnode;
846 err = ubifs_read_nnode(c, NULL, 0);
852 iip = i & (UBIFS_LPT_FANOUT - 1);
853 i >>= UBIFS_LPT_FANOUT_SHIFT;
856 nnode = ubifs_get_nnode(c, nnode, iip);
864 * make_nnode_dirty - find a nnode and, if found, make it dirty.
865 * @c: UBIFS file-system description object
866 * @node_num: nnode number of nnode to make dirty
867 * @lnum: LEB number where nnode was written
868 * @offs: offset where nnode was written
870 * This function is used by LPT garbage collection. LPT garbage collection is
871 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
872 * simply involves marking all the nodes in the LEB being garbage-collected as
873 * dirty. The dirty nodes are written next commit, after which the LEB is free
876 * This function returns %0 on success and a negative error code on failure.
878 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
881 struct ubifs_nnode *nnode;
883 nnode = nnode_lookup(c, node_num);
885 return PTR_ERR(nnode);
887 struct ubifs_nbranch *branch;
889 branch = &nnode->parent->nbranch[nnode->iip];
890 if (branch->lnum != lnum || branch->offs != offs)
891 return 0; /* nnode is obsolete */
892 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
893 return 0; /* nnode is obsolete */
894 /* Assumes cnext list is empty i.e. not called during commit */
895 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
896 c->dirty_nn_cnt += 1;
897 ubifs_add_nnode_dirt(c, nnode);
898 /* Mark parent and ancestors dirty too */
899 nnode = nnode->parent;
901 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
902 c->dirty_nn_cnt += 1;
903 ubifs_add_nnode_dirt(c, nnode);
904 nnode = nnode->parent;
913 * make_pnode_dirty - find a pnode and, if found, make it dirty.
914 * @c: UBIFS file-system description object
915 * @node_num: pnode number of pnode to make dirty
916 * @lnum: LEB number where pnode was written
917 * @offs: offset where pnode was written
919 * This function is used by LPT garbage collection. LPT garbage collection is
920 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
921 * simply involves marking all the nodes in the LEB being garbage-collected as
922 * dirty. The dirty nodes are written next commit, after which the LEB is free
925 * This function returns %0 on success and a negative error code on failure.
927 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
930 struct ubifs_pnode *pnode;
931 struct ubifs_nbranch *branch;
933 pnode = pnode_lookup(c, node_num);
935 return PTR_ERR(pnode);
936 branch = &pnode->parent->nbranch[pnode->iip];
937 if (branch->lnum != lnum || branch->offs != offs)
939 do_make_pnode_dirty(c, pnode);
944 * make_ltab_dirty - make ltab node dirty.
945 * @c: UBIFS file-system description object
946 * @lnum: LEB number where ltab was written
947 * @offs: offset where ltab was written
949 * This function is used by LPT garbage collection. LPT garbage collection is
950 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
951 * simply involves marking all the nodes in the LEB being garbage-collected as
952 * dirty. The dirty nodes are written next commit, after which the LEB is free
955 * This function returns %0 on success and a negative error code on failure.
957 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
959 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
960 return 0; /* This ltab node is obsolete */
961 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
962 c->lpt_drty_flgs |= LTAB_DIRTY;
963 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
969 * make_lsave_dirty - make lsave node dirty.
970 * @c: UBIFS file-system description object
971 * @lnum: LEB number where lsave was written
972 * @offs: offset where lsave 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_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
984 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
985 return 0; /* This lsave node is obsolete */
986 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
987 c->lpt_drty_flgs |= LSAVE_DIRTY;
988 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
994 * make_node_dirty - make node dirty.
995 * @c: UBIFS file-system description object
996 * @node_type: LPT node type
997 * @node_num: node number
998 * @lnum: LEB number where node was written
999 * @offs: offset where node was written
1001 * This function is used by LPT garbage collection. LPT garbage collection is
1002 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1003 * simply involves marking all the nodes in the LEB being garbage-collected as
1004 * dirty. The dirty nodes are written next commit, after which the LEB is free
1007 * This function returns %0 on success and a negative error code on failure.
1009 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1012 switch (node_type) {
1013 case UBIFS_LPT_NNODE:
1014 return make_nnode_dirty(c, node_num, lnum, offs);
1015 case UBIFS_LPT_PNODE:
1016 return make_pnode_dirty(c, node_num, lnum, offs);
1017 case UBIFS_LPT_LTAB:
1018 return make_ltab_dirty(c, lnum, offs);
1019 case UBIFS_LPT_LSAVE:
1020 return make_lsave_dirty(c, lnum, offs);
1026 * get_lpt_node_len - return the length of a node based on its type.
1027 * @c: UBIFS file-system description object
1028 * @node_type: LPT node type
1030 static int get_lpt_node_len(struct ubifs_info *c, int node_type)
1032 switch (node_type) {
1033 case UBIFS_LPT_NNODE:
1035 case UBIFS_LPT_PNODE:
1037 case UBIFS_LPT_LTAB:
1039 case UBIFS_LPT_LSAVE:
1046 * get_pad_len - return the length of padding in a buffer.
1047 * @c: UBIFS file-system description object
1049 * @len: length of buffer
1051 static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
1055 if (c->min_io_size == 1)
1057 offs = c->leb_size - len;
1058 pad_len = ALIGN(offs, c->min_io_size) - offs;
1063 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1064 * @c: UBIFS file-system description object
1066 * @node_num: node number is returned here
1068 static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
1070 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1071 int pos = 0, node_type;
1073 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1074 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1079 * is_a_node - determine if a buffer contains a node.
1080 * @c: UBIFS file-system description object
1082 * @len: length of buffer
1084 * This function returns %1 if the buffer contains a node or %0 if it does not.
1086 static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
1088 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1089 int pos = 0, node_type, node_len;
1090 uint16_t crc, calc_crc;
1092 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1094 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1095 if (node_type == UBIFS_LPT_NOT_A_NODE)
1097 node_len = get_lpt_node_len(c, node_type);
1098 if (!node_len || node_len > len)
1102 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1103 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1104 node_len - UBIFS_LPT_CRC_BYTES);
1105 if (crc != calc_crc)
1112 * lpt_gc_lnum - garbage collect a LPT LEB.
1113 * @c: UBIFS file-system description object
1114 * @lnum: LEB number to garbage collect
1116 * LPT garbage collection is used only for the "big" LPT model
1117 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1118 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1119 * next commit, after which the LEB is free to be reused.
1121 * This function returns %0 on success and a negative error code on failure.
1123 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1125 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1126 void *buf = c->lpt_buf;
1128 dbg_lp("LEB %d", lnum);
1129 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1131 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1135 if (!is_a_node(c, buf, len)) {
1138 pad_len = get_pad_len(c, buf, len);
1146 node_type = get_lpt_node_type(c, buf, &node_num);
1147 node_len = get_lpt_node_len(c, node_type);
1148 offs = c->leb_size - len;
1149 ubifs_assert(node_len != 0);
1150 mutex_lock(&c->lp_mutex);
1151 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1152 mutex_unlock(&c->lp_mutex);
1162 * lpt_gc - LPT garbage collection.
1163 * @c: UBIFS file-system description object
1165 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1166 * Returns %0 on success and a negative error code on failure.
1168 static int lpt_gc(struct ubifs_info *c)
1170 int i, lnum = -1, dirty = 0;
1172 mutex_lock(&c->lp_mutex);
1173 for (i = 0; i < c->lpt_lebs; i++) {
1174 ubifs_assert(!c->ltab[i].tgc);
1175 if (i + c->lpt_first == c->nhead_lnum ||
1176 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1178 if (c->ltab[i].dirty > dirty) {
1179 dirty = c->ltab[i].dirty;
1180 lnum = i + c->lpt_first;
1183 mutex_unlock(&c->lp_mutex);
1186 return lpt_gc_lnum(c, lnum);
1190 * ubifs_lpt_start_commit - UBIFS commit starts.
1191 * @c: the UBIFS file-system description object
1193 * This function has to be called when UBIFS starts the commit operation.
1194 * This function "freezes" all currently dirty LEB properties and does not
1195 * change them anymore. Further changes are saved and tracked separately
1196 * because they are not part of this commit. This function returns zero in case
1197 * of success and a negative error code in case of failure.
1199 int ubifs_lpt_start_commit(struct ubifs_info *c)
1205 mutex_lock(&c->lp_mutex);
1206 err = dbg_chk_lpt_free_spc(c);
1209 err = dbg_check_ltab(c);
1213 if (c->check_lpt_free) {
1215 * We ensure there is enough free space in
1216 * ubifs_lpt_post_commit() by marking nodes dirty. That
1217 * information is lost when we unmount, so we also need
1218 * to check free space once after mounting also.
1220 c->check_lpt_free = 0;
1221 while (need_write_all(c)) {
1222 mutex_unlock(&c->lp_mutex);
1226 mutex_lock(&c->lp_mutex);
1232 if (!c->dirty_pn_cnt) {
1233 dbg_cmt("no cnodes to commit");
1238 if (!c->big_lpt && need_write_all(c)) {
1239 /* If needed, write everything */
1240 err = make_tree_dirty(c);
1249 cnt = get_cnodes_to_commit(c);
1250 ubifs_assert(cnt != 0);
1252 err = layout_cnodes(c);
1256 /* Copy the LPT's own lprops for end commit to write */
1257 memcpy(c->ltab_cmt, c->ltab,
1258 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1259 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1262 mutex_unlock(&c->lp_mutex);
1267 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1268 * @c: UBIFS file-system description object
1270 static void free_obsolete_cnodes(struct ubifs_info *c)
1272 struct ubifs_cnode *cnode, *cnext;
1274 cnext = c->lpt_cnext;
1279 cnext = cnode->cnext;
1280 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1283 cnode->cnext = NULL;
1284 } while (cnext != c->lpt_cnext);
1285 c->lpt_cnext = NULL;
1289 * ubifs_lpt_end_commit - finish the commit operation.
1290 * @c: the UBIFS file-system description object
1292 * This function has to be called when the commit operation finishes. It
1293 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1294 * the media. Returns zero in case of success and a negative error code in case
1297 int ubifs_lpt_end_commit(struct ubifs_info *c)
1306 err = write_cnodes(c);
1310 mutex_lock(&c->lp_mutex);
1311 free_obsolete_cnodes(c);
1312 mutex_unlock(&c->lp_mutex);
1318 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1319 * @c: UBIFS file-system description object
1321 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1322 * commit for the "big" LPT model.
1324 int ubifs_lpt_post_commit(struct ubifs_info *c)
1328 mutex_lock(&c->lp_mutex);
1329 err = lpt_tgc_end(c);
1333 while (need_write_all(c)) {
1334 mutex_unlock(&c->lp_mutex);
1338 mutex_lock(&c->lp_mutex);
1341 mutex_unlock(&c->lp_mutex);
1346 * first_nnode - find the first nnode in memory.
1347 * @c: UBIFS file-system description object
1348 * @hght: height of tree where nnode found is returned here
1350 * This function returns a pointer to the nnode found or %NULL if no nnode is
1351 * found. This function is a helper to 'ubifs_lpt_free()'.
1353 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1355 struct ubifs_nnode *nnode;
1362 for (h = 1; h < c->lpt_hght; h++) {
1364 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1365 if (nnode->nbranch[i].nnode) {
1367 nnode = nnode->nbranch[i].nnode;
1379 * next_nnode - find the next nnode in memory.
1380 * @c: UBIFS file-system description object
1381 * @nnode: nnode from which to start.
1382 * @hght: height of tree where nnode is, is passed and returned here
1384 * This function returns a pointer to the nnode found or %NULL if no nnode is
1385 * found. This function is a helper to 'ubifs_lpt_free()'.
1387 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1388 struct ubifs_nnode *nnode, int *hght)
1390 struct ubifs_nnode *parent;
1391 int iip, h, i, found;
1393 parent = nnode->parent;
1396 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1400 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1401 nnode = parent->nbranch[iip].nnode;
1409 for (h = *hght + 1; h < c->lpt_hght; h++) {
1411 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1412 if (nnode->nbranch[i].nnode) {
1414 nnode = nnode->nbranch[i].nnode;
1426 * ubifs_lpt_free - free resources owned by the LPT.
1427 * @c: UBIFS file-system description object
1428 * @wr_only: free only resources used for writing
1430 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1432 struct ubifs_nnode *nnode;
1435 /* Free write-only things first */
1437 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1449 /* Now free the rest */
1451 nnode = first_nnode(c, &hght);
1453 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1454 kfree(nnode->nbranch[i].nnode);
1455 nnode = next_nnode(c, nnode, &hght);
1457 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1458 kfree(c->lpt_heap[i].arr);
1459 kfree(c->dirty_idx.arr);
1462 kfree(c->lpt_nod_buf);
1465 #ifdef CONFIG_UBIFS_FS_DEBUG
1468 * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
1470 * @len: buffer length
1472 static int dbg_is_all_ff(uint8_t *buf, int len)
1476 for (i = 0; i < len; i++)
1483 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1484 * @c: the UBIFS file-system description object
1485 * @lnum: LEB number where nnode was written
1486 * @offs: offset where nnode was written
1488 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1490 struct ubifs_nnode *nnode;
1493 /* Entire tree is in memory so first_nnode / next_nnode are ok */
1494 nnode = first_nnode(c, &hght);
1495 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1496 struct ubifs_nbranch *branch;
1499 if (nnode->parent) {
1500 branch = &nnode->parent->nbranch[nnode->iip];
1501 if (branch->lnum != lnum || branch->offs != offs)
1503 if (test_bit(DIRTY_CNODE, &nnode->flags))
1507 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1509 if (test_bit(DIRTY_CNODE, &nnode->flags))
1518 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1519 * @c: the UBIFS file-system description object
1520 * @lnum: LEB number where pnode was written
1521 * @offs: offset where pnode was written
1523 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1527 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1528 for (i = 0; i < cnt; i++) {
1529 struct ubifs_pnode *pnode;
1530 struct ubifs_nbranch *branch;
1533 pnode = pnode_lookup(c, i);
1535 return PTR_ERR(pnode);
1536 branch = &pnode->parent->nbranch[pnode->iip];
1537 if (branch->lnum != lnum || branch->offs != offs)
1539 if (test_bit(DIRTY_CNODE, &pnode->flags))
1547 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1548 * @c: the UBIFS file-system description object
1549 * @lnum: LEB number where ltab node was written
1550 * @offs: offset where ltab node was written
1552 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1554 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1556 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1560 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1561 * @c: the UBIFS file-system description object
1562 * @lnum: LEB number where lsave node was written
1563 * @offs: offset where lsave node was written
1565 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1567 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1569 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1573 * dbg_is_node_dirty - determine if a node is dirty.
1574 * @c: the UBIFS file-system description object
1575 * @node_type: node type
1576 * @lnum: LEB number where node was written
1577 * @offs: offset where node was written
1579 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1582 switch (node_type) {
1583 case UBIFS_LPT_NNODE:
1584 return dbg_is_nnode_dirty(c, lnum, offs);
1585 case UBIFS_LPT_PNODE:
1586 return dbg_is_pnode_dirty(c, lnum, offs);
1587 case UBIFS_LPT_LTAB:
1588 return dbg_is_ltab_dirty(c, lnum, offs);
1589 case UBIFS_LPT_LSAVE:
1590 return dbg_is_lsave_dirty(c, lnum, offs);
1596 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1597 * @c: the UBIFS file-system description object
1598 * @lnum: LEB number where node was written
1599 * @offs: offset where node was written
1601 * This function returns %0 on success and a negative error code on failure.
1603 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1605 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1607 void *buf = c->dbg_buf;
1609 dbg_lp("LEB %d", lnum);
1610 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1612 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1616 if (!is_a_node(c, buf, len)) {
1619 pad_len = get_pad_len(c, buf, len);
1626 if (!dbg_is_all_ff(buf, len)) {
1627 dbg_msg("invalid empty space in LEB %d at %d",
1628 lnum, c->leb_size - len);
1631 i = lnum - c->lpt_first;
1632 if (len != c->ltab[i].free) {
1633 dbg_msg("invalid free space in LEB %d "
1634 "(free %d, expected %d)",
1635 lnum, len, c->ltab[i].free);
1638 if (dirty != c->ltab[i].dirty) {
1639 dbg_msg("invalid dirty space in LEB %d "
1640 "(dirty %d, expected %d)",
1641 lnum, dirty, c->ltab[i].dirty);
1646 node_type = get_lpt_node_type(c, buf, &node_num);
1647 node_len = get_lpt_node_len(c, node_type);
1648 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1657 * dbg_check_ltab - check the free and dirty space in the ltab.
1658 * @c: the UBIFS file-system description object
1660 * This function returns %0 on success and a negative error code on failure.
1662 int dbg_check_ltab(struct ubifs_info *c)
1664 int lnum, err, i, cnt;
1666 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1669 /* Bring the entire tree into memory */
1670 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1671 for (i = 0; i < cnt; i++) {
1672 struct ubifs_pnode *pnode;
1674 pnode = pnode_lookup(c, i);
1676 return PTR_ERR(pnode);
1681 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1685 /* Check each LEB */
1686 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1687 err = dbg_check_ltab_lnum(c, lnum);
1689 dbg_err("failed at LEB %d", lnum);
1694 dbg_lp("succeeded");
1699 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
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_chk_lpt_free_spc(struct ubifs_info *c)
1709 for (i = 0; i < c->lpt_lebs; i++) {
1710 if (c->ltab[i].tgc || c->ltab[i].cmt)
1712 if (i + c->lpt_first == c->nhead_lnum)
1713 free += c->leb_size - c->nhead_offs;
1714 else if (c->ltab[i].free == c->leb_size)
1715 free += c->leb_size;
1717 if (free < c->lpt_sz) {
1718 dbg_err("LPT space error: free %lld lpt_sz %lld",
1720 dbg_dump_lpt_info(c);
1727 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1728 * @c: the UBIFS file-system description object
1730 * @len: length written
1732 * This function returns %0 on success and a negative error code on failure.
1734 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1736 long long chk_lpt_sz, lpt_sz;
1743 c->chk_lpt_lebs = 0;
1744 c->chk_lpt_wastage = 0;
1745 if (c->dirty_pn_cnt > c->pnode_cnt) {
1746 dbg_err("dirty pnodes %d exceed max %d",
1747 c->dirty_pn_cnt, c->pnode_cnt);
1750 if (c->dirty_nn_cnt > c->nnode_cnt) {
1751 dbg_err("dirty nnodes %d exceed max %d",
1752 c->dirty_nn_cnt, c->nnode_cnt);
1757 c->chk_lpt_sz += len;
1760 c->chk_lpt_sz += len;
1761 c->chk_lpt_wastage += len;
1762 c->chk_lpt_lebs += 1;
1765 chk_lpt_sz = c->leb_size;
1766 chk_lpt_sz *= c->chk_lpt_lebs;
1767 chk_lpt_sz += len - c->nhead_offs;
1768 if (c->chk_lpt_sz != chk_lpt_sz) {
1769 dbg_err("LPT wrote %lld but space used was %lld",
1770 c->chk_lpt_sz, chk_lpt_sz);
1773 if (c->chk_lpt_sz > c->lpt_sz) {
1774 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1775 c->chk_lpt_sz, c->lpt_sz);
1778 if (c->chk_lpt_sz2 && c->chk_lpt_sz != c->chk_lpt_sz2) {
1779 dbg_err("LPT layout size %lld but wrote %lld",
1780 c->chk_lpt_sz, c->chk_lpt_sz2);
1783 if (c->chk_lpt_sz2 && c->new_nhead_offs != len) {
1784 dbg_err("LPT new nhead offs: expected %d was %d",
1785 c->new_nhead_offs, len);
1788 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1789 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1790 lpt_sz += c->ltab_sz;
1792 lpt_sz += c->lsave_sz;
1793 if (c->chk_lpt_sz - c->chk_lpt_wastage > lpt_sz) {
1794 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1795 c->chk_lpt_sz, c->chk_lpt_wastage, lpt_sz);
1799 dbg_dump_lpt_info(c);
1800 c->chk_lpt_sz2 = c->chk_lpt_sz;
1802 c->chk_lpt_wastage = 0;
1803 c->chk_lpt_lebs = 0;
1804 c->new_nhead_offs = len;
1807 c->chk_lpt_sz += len;
1808 c->chk_lpt_wastage += len;
1815 #endif /* CONFIG_UBIFS_FS_DEBUG */