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>
29 #include <linux/slab.h>
30 #include <linux/random.h>
33 #ifdef CONFIG_UBIFS_FS_DEBUG
34 static int dbg_populate_lsave(struct ubifs_info *c);
36 #define dbg_populate_lsave(c) 0
40 * first_dirty_cnode - find first dirty cnode.
41 * @c: UBIFS file-system description object
42 * @nnode: nnode at which to start
44 * This function returns the first dirty cnode or %NULL if there is not one.
46 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
52 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
53 struct ubifs_cnode *cnode;
55 cnode = nnode->nbranch[i].cnode;
57 test_bit(DIRTY_CNODE, &cnode->flags)) {
58 if (cnode->level == 0)
60 nnode = (struct ubifs_nnode *)cnode;
66 return (struct ubifs_cnode *)nnode;
71 * next_dirty_cnode - find next dirty cnode.
72 * @cnode: cnode from which to begin searching
74 * This function returns the next dirty cnode or %NULL if there is not one.
76 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
78 struct ubifs_nnode *nnode;
82 nnode = cnode->parent;
85 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
86 cnode = nnode->nbranch[i].cnode;
87 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
88 if (cnode->level == 0)
89 return cnode; /* cnode is a pnode */
90 /* cnode is a nnode */
91 return first_dirty_cnode((struct ubifs_nnode *)cnode);
94 return (struct ubifs_cnode *)nnode;
98 * get_cnodes_to_commit - create list of dirty cnodes to commit.
99 * @c: UBIFS file-system description object
101 * This function returns the number of cnodes to commit.
103 static int get_cnodes_to_commit(struct ubifs_info *c)
105 struct ubifs_cnode *cnode, *cnext;
111 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
114 c->lpt_cnext = first_dirty_cnode(c->nroot);
115 cnode = c->lpt_cnext;
120 ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
121 __set_bit(COW_CNODE, &cnode->flags);
122 cnext = next_dirty_cnode(cnode);
124 cnode->cnext = c->lpt_cnext;
127 cnode->cnext = cnext;
131 dbg_cmt("committing %d cnodes", cnt);
132 dbg_lp("committing %d cnodes", cnt);
133 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
138 * upd_ltab - update LPT LEB properties.
139 * @c: UBIFS file-system description object
141 * @free: amount of free space
142 * @dirty: amount of dirty space to add
144 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
146 dbg_lp("LEB %d free %d dirty %d to %d +%d",
147 lnum, c->ltab[lnum - c->lpt_first].free,
148 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
149 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
150 c->ltab[lnum - c->lpt_first].free = free;
151 c->ltab[lnum - c->lpt_first].dirty += dirty;
155 * alloc_lpt_leb - allocate an LPT LEB that is empty.
156 * @c: UBIFS file-system description object
157 * @lnum: LEB number is passed and returned here
159 * This function finds the next empty LEB in the ltab starting from @lnum. If a
160 * an empty LEB is found it is returned in @lnum and the function returns %0.
161 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
162 * never to run out of space.
164 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
168 n = *lnum - c->lpt_first + 1;
169 for (i = n; i < c->lpt_lebs; i++) {
170 if (c->ltab[i].tgc || c->ltab[i].cmt)
172 if (c->ltab[i].free == c->leb_size) {
174 *lnum = i + c->lpt_first;
179 for (i = 0; i < n; i++) {
180 if (c->ltab[i].tgc || c->ltab[i].cmt)
182 if (c->ltab[i].free == c->leb_size) {
184 *lnum = i + c->lpt_first;
192 * layout_cnodes - layout cnodes for commit.
193 * @c: UBIFS file-system description object
195 * This function returns %0 on success and a negative error code on failure.
197 static int layout_cnodes(struct ubifs_info *c)
199 int lnum, offs, len, alen, done_lsave, done_ltab, err;
200 struct ubifs_cnode *cnode;
202 err = dbg_chk_lpt_sz(c, 0, 0);
205 cnode = c->lpt_cnext;
208 lnum = c->nhead_lnum;
209 offs = c->nhead_offs;
210 /* Try to place lsave and ltab nicely */
211 done_lsave = !c->big_lpt;
213 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
215 c->lsave_lnum = lnum;
216 c->lsave_offs = offs;
218 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
221 if (offs + c->ltab_sz <= c->leb_size) {
226 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
232 c->dirty_nn_cnt -= 1;
235 c->dirty_pn_cnt -= 1;
237 while (offs + len > c->leb_size) {
238 alen = ALIGN(offs, c->min_io_size);
239 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
240 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
241 err = alloc_lpt_leb(c, &lnum);
245 ubifs_assert(lnum >= c->lpt_first &&
246 lnum <= c->lpt_last);
247 /* Try to place lsave and ltab nicely */
250 c->lsave_lnum = lnum;
251 c->lsave_offs = offs;
253 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
261 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
267 cnode->parent->nbranch[cnode->iip].lnum = lnum;
268 cnode->parent->nbranch[cnode->iip].offs = offs;
274 dbg_chk_lpt_sz(c, 1, len);
275 cnode = cnode->cnext;
276 } while (cnode && cnode != c->lpt_cnext);
278 /* Make sure to place LPT's save table */
280 if (offs + c->lsave_sz > c->leb_size) {
281 alen = ALIGN(offs, c->min_io_size);
282 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
283 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
284 err = alloc_lpt_leb(c, &lnum);
288 ubifs_assert(lnum >= c->lpt_first &&
289 lnum <= c->lpt_last);
292 c->lsave_lnum = lnum;
293 c->lsave_offs = offs;
295 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
298 /* Make sure to place LPT's own lprops table */
300 if (offs + c->ltab_sz > c->leb_size) {
301 alen = ALIGN(offs, c->min_io_size);
302 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
303 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
304 err = alloc_lpt_leb(c, &lnum);
308 ubifs_assert(lnum >= c->lpt_first &&
309 lnum <= c->lpt_last);
315 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
318 alen = ALIGN(offs, c->min_io_size);
319 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
320 dbg_chk_lpt_sz(c, 4, alen - offs);
321 err = dbg_chk_lpt_sz(c, 3, alen);
327 ubifs_err("LPT out of space");
328 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
329 "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
330 dbg_dump_lpt_info(c);
331 dbg_dump_lpt_lebs(c);
337 * realloc_lpt_leb - allocate an LPT LEB that is empty.
338 * @c: UBIFS file-system description object
339 * @lnum: LEB number is passed and returned here
341 * This function duplicates exactly the results of the function alloc_lpt_leb.
342 * It is used during end commit to reallocate the same LEB numbers that were
343 * allocated by alloc_lpt_leb during start commit.
345 * This function finds the next LEB that was allocated by the alloc_lpt_leb
346 * function starting from @lnum. If a LEB is found it is returned in @lnum and
347 * the function returns %0. Otherwise the function returns -ENOSPC.
348 * Note however, that LPT is designed never to run out of space.
350 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
354 n = *lnum - c->lpt_first + 1;
355 for (i = n; i < c->lpt_lebs; i++)
356 if (c->ltab[i].cmt) {
358 *lnum = i + c->lpt_first;
362 for (i = 0; i < n; i++)
363 if (c->ltab[i].cmt) {
365 *lnum = i + c->lpt_first;
372 * write_cnodes - write cnodes for commit.
373 * @c: UBIFS file-system description object
375 * This function returns %0 on success and a negative error code on failure.
377 static int write_cnodes(struct ubifs_info *c)
379 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
380 struct ubifs_cnode *cnode;
381 void *buf = c->lpt_buf;
383 cnode = c->lpt_cnext;
386 lnum = c->nhead_lnum;
387 offs = c->nhead_offs;
389 /* Ensure empty LEB is unmapped */
391 err = ubifs_leb_unmap(c, lnum);
395 /* Try to place lsave and ltab nicely */
396 done_lsave = !c->big_lpt;
398 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
400 ubifs_pack_lsave(c, buf + offs, c->lsave);
402 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
405 if (offs + c->ltab_sz <= c->leb_size) {
407 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
409 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
412 /* Loop for each cnode */
418 while (offs + len > c->leb_size) {
421 alen = ALIGN(wlen, c->min_io_size);
422 memset(buf + offs, 0xff, alen - wlen);
423 err = ubifs_leb_write(c, lnum, buf + from, from,
424 alen, UBI_SHORTTERM);
428 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
429 err = realloc_lpt_leb(c, &lnum);
433 ubifs_assert(lnum >= c->lpt_first &&
434 lnum <= c->lpt_last);
435 err = ubifs_leb_unmap(c, lnum);
438 /* Try to place lsave and ltab nicely */
441 ubifs_pack_lsave(c, buf + offs, c->lsave);
443 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
448 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
450 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
456 ubifs_pack_nnode(c, buf + offs,
457 (struct ubifs_nnode *)cnode);
459 ubifs_pack_pnode(c, buf + offs,
460 (struct ubifs_pnode *)cnode);
462 * The reason for the barriers is the same as in case of TNC.
463 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
464 * 'dirty_cow_pnode()' are the functions for which this is
467 clear_bit(DIRTY_CNODE, &cnode->flags);
468 smp_mb__before_clear_bit();
469 clear_bit(COW_CNODE, &cnode->flags);
470 smp_mb__after_clear_bit();
472 dbg_chk_lpt_sz(c, 1, len);
473 cnode = cnode->cnext;
474 } while (cnode && cnode != c->lpt_cnext);
476 /* Make sure to place LPT's save table */
478 if (offs + c->lsave_sz > c->leb_size) {
480 alen = ALIGN(wlen, c->min_io_size);
481 memset(buf + offs, 0xff, alen - wlen);
482 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
486 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
487 err = realloc_lpt_leb(c, &lnum);
491 ubifs_assert(lnum >= c->lpt_first &&
492 lnum <= c->lpt_last);
493 err = ubifs_leb_unmap(c, lnum);
498 ubifs_pack_lsave(c, buf + offs, c->lsave);
500 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
503 /* Make sure to place LPT's own lprops table */
505 if (offs + c->ltab_sz > c->leb_size) {
507 alen = ALIGN(wlen, c->min_io_size);
508 memset(buf + offs, 0xff, alen - wlen);
509 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
513 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
514 err = realloc_lpt_leb(c, &lnum);
518 ubifs_assert(lnum >= c->lpt_first &&
519 lnum <= c->lpt_last);
520 err = ubifs_leb_unmap(c, lnum);
525 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
527 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
530 /* Write remaining data in buffer */
532 alen = ALIGN(wlen, c->min_io_size);
533 memset(buf + offs, 0xff, alen - wlen);
534 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
538 dbg_chk_lpt_sz(c, 4, alen - wlen);
539 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
543 c->nhead_lnum = lnum;
544 c->nhead_offs = ALIGN(offs, c->min_io_size);
546 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
547 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
548 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
550 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
555 ubifs_err("LPT out of space mismatch");
556 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
557 "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
558 dbg_dump_lpt_info(c);
559 dbg_dump_lpt_lebs(c);
565 * next_pnode_to_dirty - find next pnode to dirty.
566 * @c: UBIFS file-system description object
569 * This function returns the next pnode to dirty or %NULL if there are no more
570 * pnodes. Note that pnodes that have never been written (lnum == 0) are
573 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
574 struct ubifs_pnode *pnode)
576 struct ubifs_nnode *nnode;
579 /* Try to go right */
580 nnode = pnode->parent;
581 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
582 if (nnode->nbranch[iip].lnum)
583 return ubifs_get_pnode(c, nnode, iip);
586 /* Go up while can't go right */
588 iip = nnode->iip + 1;
589 nnode = nnode->parent;
592 for (; iip < UBIFS_LPT_FANOUT; iip++) {
593 if (nnode->nbranch[iip].lnum)
596 } while (iip >= UBIFS_LPT_FANOUT);
599 nnode = ubifs_get_nnode(c, nnode, iip);
601 return (void *)nnode;
603 /* Go down to level 1 */
604 while (nnode->level > 1) {
605 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
606 if (nnode->nbranch[iip].lnum)
609 if (iip >= UBIFS_LPT_FANOUT) {
611 * Should not happen, but we need to keep going
616 nnode = ubifs_get_nnode(c, nnode, iip);
618 return (void *)nnode;
621 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
622 if (nnode->nbranch[iip].lnum)
624 if (iip >= UBIFS_LPT_FANOUT)
625 /* Should not happen, but we need to keep going if it does */
627 return ubifs_get_pnode(c, nnode, iip);
631 * pnode_lookup - lookup a pnode in the LPT.
632 * @c: UBIFS file-system description object
633 * @i: pnode number (0 to main_lebs - 1)
635 * This function returns a pointer to the pnode on success or a negative
636 * error code on failure.
638 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
640 int err, h, iip, shft;
641 struct ubifs_nnode *nnode;
644 err = ubifs_read_nnode(c, NULL, 0);
648 i <<= UBIFS_LPT_FANOUT_SHIFT;
650 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
651 for (h = 1; h < c->lpt_hght; h++) {
652 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
653 shft -= UBIFS_LPT_FANOUT_SHIFT;
654 nnode = ubifs_get_nnode(c, nnode, iip);
656 return ERR_CAST(nnode);
658 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
659 return ubifs_get_pnode(c, nnode, iip);
663 * add_pnode_dirt - add dirty space to LPT LEB properties.
664 * @c: UBIFS file-system description object
665 * @pnode: pnode for which to add dirt
667 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
669 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
674 * do_make_pnode_dirty - mark a pnode dirty.
675 * @c: UBIFS file-system description object
676 * @pnode: pnode to mark dirty
678 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
680 /* Assumes cnext list is empty i.e. not called during commit */
681 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
682 struct ubifs_nnode *nnode;
684 c->dirty_pn_cnt += 1;
685 add_pnode_dirt(c, pnode);
686 /* Mark parent and ancestors dirty too */
687 nnode = pnode->parent;
689 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
690 c->dirty_nn_cnt += 1;
691 ubifs_add_nnode_dirt(c, nnode);
692 nnode = nnode->parent;
700 * make_tree_dirty - mark the entire LEB properties tree dirty.
701 * @c: UBIFS file-system description object
703 * This function is used by the "small" LPT model to cause the entire LEB
704 * properties tree to be written. The "small" LPT model does not use LPT
705 * garbage collection because it is more efficient to write the entire tree
706 * (because it is small).
708 * This function returns %0 on success and a negative error code on failure.
710 static int make_tree_dirty(struct ubifs_info *c)
712 struct ubifs_pnode *pnode;
714 pnode = pnode_lookup(c, 0);
716 return PTR_ERR(pnode);
719 do_make_pnode_dirty(c, pnode);
720 pnode = next_pnode_to_dirty(c, pnode);
722 return PTR_ERR(pnode);
728 * need_write_all - determine if the LPT area is running out of free space.
729 * @c: UBIFS file-system description object
731 * This function returns %1 if the LPT area is running out of free space and %0
734 static int need_write_all(struct ubifs_info *c)
739 for (i = 0; i < c->lpt_lebs; i++) {
740 if (i + c->lpt_first == c->nhead_lnum)
741 free += c->leb_size - c->nhead_offs;
742 else if (c->ltab[i].free == c->leb_size)
744 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
747 /* Less than twice the size left */
748 if (free <= c->lpt_sz * 2)
754 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
755 * @c: UBIFS file-system description object
757 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
758 * free space and so may be reused as soon as the next commit is completed.
759 * This function is called during start commit to mark LPT LEBs for trivial GC.
761 static void lpt_tgc_start(struct ubifs_info *c)
765 for (i = 0; i < c->lpt_lebs; i++) {
766 if (i + c->lpt_first == c->nhead_lnum)
768 if (c->ltab[i].dirty > 0 &&
769 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
771 c->ltab[i].free = c->leb_size;
772 c->ltab[i].dirty = 0;
773 dbg_lp("LEB %d", i + c->lpt_first);
779 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
780 * @c: UBIFS file-system description object
782 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
783 * free space and so may be reused as soon as the next commit is completed.
784 * This function is called after the commit is completed (master node has been
785 * written) and un-maps LPT LEBs that were marked for trivial GC.
787 static int lpt_tgc_end(struct ubifs_info *c)
791 for (i = 0; i < c->lpt_lebs; i++)
792 if (c->ltab[i].tgc) {
793 err = ubifs_leb_unmap(c, i + c->lpt_first);
797 dbg_lp("LEB %d", i + c->lpt_first);
803 * populate_lsave - fill the lsave array with important LEB numbers.
804 * @c: the UBIFS file-system description object
806 * This function is only called for the "big" model. It records a small number
807 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
808 * most important to least important): empty, freeable, freeable index, dirty
809 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
810 * their pnodes into memory. That will stop us from having to scan the LPT
811 * straight away. For the "small" model we assume that scanning the LPT is no
814 static void populate_lsave(struct ubifs_info *c)
816 struct ubifs_lprops *lprops;
817 struct ubifs_lpt_heap *heap;
820 ubifs_assert(c->big_lpt);
821 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
822 c->lpt_drty_flgs |= LSAVE_DIRTY;
823 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
826 if (dbg_populate_lsave(c))
829 list_for_each_entry(lprops, &c->empty_list, list) {
830 c->lsave[cnt++] = lprops->lnum;
831 if (cnt >= c->lsave_cnt)
834 list_for_each_entry(lprops, &c->freeable_list, list) {
835 c->lsave[cnt++] = lprops->lnum;
836 if (cnt >= c->lsave_cnt)
839 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
840 c->lsave[cnt++] = lprops->lnum;
841 if (cnt >= c->lsave_cnt)
844 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
845 for (i = 0; i < heap->cnt; i++) {
846 c->lsave[cnt++] = heap->arr[i]->lnum;
847 if (cnt >= c->lsave_cnt)
850 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
851 for (i = 0; i < heap->cnt; i++) {
852 c->lsave[cnt++] = heap->arr[i]->lnum;
853 if (cnt >= c->lsave_cnt)
856 heap = &c->lpt_heap[LPROPS_FREE - 1];
857 for (i = 0; i < heap->cnt; i++) {
858 c->lsave[cnt++] = heap->arr[i]->lnum;
859 if (cnt >= c->lsave_cnt)
862 /* Fill it up completely */
863 while (cnt < c->lsave_cnt)
864 c->lsave[cnt++] = c->main_first;
868 * nnode_lookup - lookup a nnode in the LPT.
869 * @c: UBIFS file-system description object
872 * This function returns a pointer to the nnode on success or a negative
873 * error code on failure.
875 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
878 struct ubifs_nnode *nnode;
881 err = ubifs_read_nnode(c, NULL, 0);
887 iip = i & (UBIFS_LPT_FANOUT - 1);
888 i >>= UBIFS_LPT_FANOUT_SHIFT;
891 nnode = ubifs_get_nnode(c, nnode, iip);
899 * make_nnode_dirty - find a nnode and, if found, make it dirty.
900 * @c: UBIFS file-system description object
901 * @node_num: nnode number of nnode to make dirty
902 * @lnum: LEB number where nnode was written
903 * @offs: offset where nnode was written
905 * This function is used by LPT garbage collection. LPT garbage collection is
906 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
907 * simply involves marking all the nodes in the LEB being garbage-collected as
908 * dirty. The dirty nodes are written next commit, after which the LEB is free
911 * This function returns %0 on success and a negative error code on failure.
913 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
916 struct ubifs_nnode *nnode;
918 nnode = nnode_lookup(c, node_num);
920 return PTR_ERR(nnode);
922 struct ubifs_nbranch *branch;
924 branch = &nnode->parent->nbranch[nnode->iip];
925 if (branch->lnum != lnum || branch->offs != offs)
926 return 0; /* nnode is obsolete */
927 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
928 return 0; /* nnode is obsolete */
929 /* Assumes cnext list is empty i.e. not called during commit */
930 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
931 c->dirty_nn_cnt += 1;
932 ubifs_add_nnode_dirt(c, nnode);
933 /* Mark parent and ancestors dirty too */
934 nnode = nnode->parent;
936 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
937 c->dirty_nn_cnt += 1;
938 ubifs_add_nnode_dirt(c, nnode);
939 nnode = nnode->parent;
948 * make_pnode_dirty - find a pnode and, if found, make it dirty.
949 * @c: UBIFS file-system description object
950 * @node_num: pnode number of pnode to make dirty
951 * @lnum: LEB number where pnode was written
952 * @offs: offset where pnode was written
954 * This function is used by LPT garbage collection. LPT garbage collection is
955 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
956 * simply involves marking all the nodes in the LEB being garbage-collected as
957 * dirty. The dirty nodes are written next commit, after which the LEB is free
960 * This function returns %0 on success and a negative error code on failure.
962 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
965 struct ubifs_pnode *pnode;
966 struct ubifs_nbranch *branch;
968 pnode = pnode_lookup(c, node_num);
970 return PTR_ERR(pnode);
971 branch = &pnode->parent->nbranch[pnode->iip];
972 if (branch->lnum != lnum || branch->offs != offs)
974 do_make_pnode_dirty(c, pnode);
979 * make_ltab_dirty - make ltab node dirty.
980 * @c: UBIFS file-system description object
981 * @lnum: LEB number where ltab was written
982 * @offs: offset where ltab was written
984 * This function is used by LPT garbage collection. LPT garbage collection is
985 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
986 * simply involves marking all the nodes in the LEB being garbage-collected as
987 * dirty. The dirty nodes are written next commit, after which the LEB is free
990 * This function returns %0 on success and a negative error code on failure.
992 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
994 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
995 return 0; /* This ltab node is obsolete */
996 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
997 c->lpt_drty_flgs |= LTAB_DIRTY;
998 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
1004 * make_lsave_dirty - make lsave node dirty.
1005 * @c: UBIFS file-system description object
1006 * @lnum: LEB number where lsave was written
1007 * @offs: offset where lsave was written
1009 * This function is used by LPT garbage collection. LPT garbage collection is
1010 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1011 * simply involves marking all the nodes in the LEB being garbage-collected as
1012 * dirty. The dirty nodes are written next commit, after which the LEB is free
1015 * This function returns %0 on success and a negative error code on failure.
1017 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1019 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1020 return 0; /* This lsave node is obsolete */
1021 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1022 c->lpt_drty_flgs |= LSAVE_DIRTY;
1023 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1029 * make_node_dirty - make node dirty.
1030 * @c: UBIFS file-system description object
1031 * @node_type: LPT node type
1032 * @node_num: node number
1033 * @lnum: LEB number where node was written
1034 * @offs: offset where node was written
1036 * This function is used by LPT garbage collection. LPT garbage collection is
1037 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1038 * simply involves marking all the nodes in the LEB being garbage-collected as
1039 * dirty. The dirty nodes are written next commit, after which the LEB is free
1042 * This function returns %0 on success and a negative error code on failure.
1044 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1047 switch (node_type) {
1048 case UBIFS_LPT_NNODE:
1049 return make_nnode_dirty(c, node_num, lnum, offs);
1050 case UBIFS_LPT_PNODE:
1051 return make_pnode_dirty(c, node_num, lnum, offs);
1052 case UBIFS_LPT_LTAB:
1053 return make_ltab_dirty(c, lnum, offs);
1054 case UBIFS_LPT_LSAVE:
1055 return make_lsave_dirty(c, lnum, offs);
1061 * get_lpt_node_len - return the length of a node based on its type.
1062 * @c: UBIFS file-system description object
1063 * @node_type: LPT node type
1065 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1067 switch (node_type) {
1068 case UBIFS_LPT_NNODE:
1070 case UBIFS_LPT_PNODE:
1072 case UBIFS_LPT_LTAB:
1074 case UBIFS_LPT_LSAVE:
1081 * get_pad_len - return the length of padding in a buffer.
1082 * @c: UBIFS file-system description object
1084 * @len: length of buffer
1086 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1090 if (c->min_io_size == 1)
1092 offs = c->leb_size - len;
1093 pad_len = ALIGN(offs, c->min_io_size) - offs;
1098 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1099 * @c: UBIFS file-system description object
1101 * @node_num: node number is returned here
1103 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1106 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1107 int pos = 0, node_type;
1109 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1110 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1115 * is_a_node - determine if a buffer contains a node.
1116 * @c: UBIFS file-system description object
1118 * @len: length of buffer
1120 * This function returns %1 if the buffer contains a node or %0 if it does not.
1122 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1124 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1125 int pos = 0, node_type, node_len;
1126 uint16_t crc, calc_crc;
1128 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1130 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1131 if (node_type == UBIFS_LPT_NOT_A_NODE)
1133 node_len = get_lpt_node_len(c, node_type);
1134 if (!node_len || node_len > len)
1138 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1139 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1140 node_len - UBIFS_LPT_CRC_BYTES);
1141 if (crc != calc_crc)
1147 * lpt_gc_lnum - garbage collect a LPT LEB.
1148 * @c: UBIFS file-system description object
1149 * @lnum: LEB number to garbage collect
1151 * LPT garbage collection is used only for the "big" LPT model
1152 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1153 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1154 * next commit, after which the LEB is free to be reused.
1156 * This function returns %0 on success and a negative error code on failure.
1158 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1160 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1161 void *buf = c->lpt_buf;
1163 dbg_lp("LEB %d", lnum);
1165 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1170 if (!is_a_node(c, buf, len)) {
1173 pad_len = get_pad_len(c, buf, len);
1181 node_type = get_lpt_node_type(c, buf, &node_num);
1182 node_len = get_lpt_node_len(c, node_type);
1183 offs = c->leb_size - len;
1184 ubifs_assert(node_len != 0);
1185 mutex_lock(&c->lp_mutex);
1186 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1187 mutex_unlock(&c->lp_mutex);
1197 * lpt_gc - LPT garbage collection.
1198 * @c: UBIFS file-system description object
1200 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1201 * Returns %0 on success and a negative error code on failure.
1203 static int lpt_gc(struct ubifs_info *c)
1205 int i, lnum = -1, dirty = 0;
1207 mutex_lock(&c->lp_mutex);
1208 for (i = 0; i < c->lpt_lebs; i++) {
1209 ubifs_assert(!c->ltab[i].tgc);
1210 if (i + c->lpt_first == c->nhead_lnum ||
1211 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1213 if (c->ltab[i].dirty > dirty) {
1214 dirty = c->ltab[i].dirty;
1215 lnum = i + c->lpt_first;
1218 mutex_unlock(&c->lp_mutex);
1221 return lpt_gc_lnum(c, lnum);
1225 * ubifs_lpt_start_commit - UBIFS commit starts.
1226 * @c: the UBIFS file-system description object
1228 * This function has to be called when UBIFS starts the commit operation.
1229 * This function "freezes" all currently dirty LEB properties and does not
1230 * change them anymore. Further changes are saved and tracked separately
1231 * because they are not part of this commit. This function returns zero in case
1232 * of success and a negative error code in case of failure.
1234 int ubifs_lpt_start_commit(struct ubifs_info *c)
1240 mutex_lock(&c->lp_mutex);
1241 err = dbg_chk_lpt_free_spc(c);
1244 err = dbg_check_ltab(c);
1248 if (c->check_lpt_free) {
1250 * We ensure there is enough free space in
1251 * ubifs_lpt_post_commit() by marking nodes dirty. That
1252 * information is lost when we unmount, so we also need
1253 * to check free space once after mounting also.
1255 c->check_lpt_free = 0;
1256 while (need_write_all(c)) {
1257 mutex_unlock(&c->lp_mutex);
1261 mutex_lock(&c->lp_mutex);
1267 if (!c->dirty_pn_cnt) {
1268 dbg_cmt("no cnodes to commit");
1273 if (!c->big_lpt && need_write_all(c)) {
1274 /* If needed, write everything */
1275 err = make_tree_dirty(c);
1284 cnt = get_cnodes_to_commit(c);
1285 ubifs_assert(cnt != 0);
1287 err = layout_cnodes(c);
1291 /* Copy the LPT's own lprops for end commit to write */
1292 memcpy(c->ltab_cmt, c->ltab,
1293 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1294 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1297 mutex_unlock(&c->lp_mutex);
1302 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1303 * @c: UBIFS file-system description object
1305 static void free_obsolete_cnodes(struct ubifs_info *c)
1307 struct ubifs_cnode *cnode, *cnext;
1309 cnext = c->lpt_cnext;
1314 cnext = cnode->cnext;
1315 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1318 cnode->cnext = NULL;
1319 } while (cnext != c->lpt_cnext);
1320 c->lpt_cnext = NULL;
1324 * ubifs_lpt_end_commit - finish the commit operation.
1325 * @c: the UBIFS file-system description object
1327 * This function has to be called when the commit operation finishes. It
1328 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1329 * the media. Returns zero in case of success and a negative error code in case
1332 int ubifs_lpt_end_commit(struct ubifs_info *c)
1341 err = write_cnodes(c);
1345 mutex_lock(&c->lp_mutex);
1346 free_obsolete_cnodes(c);
1347 mutex_unlock(&c->lp_mutex);
1353 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1354 * @c: UBIFS file-system description object
1356 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1357 * commit for the "big" LPT model.
1359 int ubifs_lpt_post_commit(struct ubifs_info *c)
1363 mutex_lock(&c->lp_mutex);
1364 err = lpt_tgc_end(c);
1368 while (need_write_all(c)) {
1369 mutex_unlock(&c->lp_mutex);
1373 mutex_lock(&c->lp_mutex);
1376 mutex_unlock(&c->lp_mutex);
1381 * first_nnode - find the first nnode in memory.
1382 * @c: UBIFS file-system description object
1383 * @hght: height of tree where nnode found is returned here
1385 * This function returns a pointer to the nnode found or %NULL if no nnode is
1386 * found. This function is a helper to 'ubifs_lpt_free()'.
1388 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1390 struct ubifs_nnode *nnode;
1397 for (h = 1; h < c->lpt_hght; h++) {
1399 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1400 if (nnode->nbranch[i].nnode) {
1402 nnode = nnode->nbranch[i].nnode;
1414 * next_nnode - find the next nnode in memory.
1415 * @c: UBIFS file-system description object
1416 * @nnode: nnode from which to start.
1417 * @hght: height of tree where nnode is, is passed and returned here
1419 * This function returns a pointer to the nnode found or %NULL if no nnode is
1420 * found. This function is a helper to 'ubifs_lpt_free()'.
1422 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1423 struct ubifs_nnode *nnode, int *hght)
1425 struct ubifs_nnode *parent;
1426 int iip, h, i, found;
1428 parent = nnode->parent;
1431 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1435 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1436 nnode = parent->nbranch[iip].nnode;
1444 for (h = *hght + 1; h < c->lpt_hght; h++) {
1446 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1447 if (nnode->nbranch[i].nnode) {
1449 nnode = nnode->nbranch[i].nnode;
1461 * ubifs_lpt_free - free resources owned by the LPT.
1462 * @c: UBIFS file-system description object
1463 * @wr_only: free only resources used for writing
1465 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1467 struct ubifs_nnode *nnode;
1470 /* Free write-only things first */
1472 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1484 /* Now free the rest */
1486 nnode = first_nnode(c, &hght);
1488 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1489 kfree(nnode->nbranch[i].nnode);
1490 nnode = next_nnode(c, nnode, &hght);
1492 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1493 kfree(c->lpt_heap[i].arr);
1494 kfree(c->dirty_idx.arr);
1497 kfree(c->lpt_nod_buf);
1500 #ifdef CONFIG_UBIFS_FS_DEBUG
1503 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1505 * @len: buffer length
1507 static int dbg_is_all_ff(uint8_t *buf, int len)
1511 for (i = 0; i < len; i++)
1518 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1519 * @c: the UBIFS file-system description object
1520 * @lnum: LEB number where nnode was written
1521 * @offs: offset where nnode was written
1523 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1525 struct ubifs_nnode *nnode;
1528 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1529 nnode = first_nnode(c, &hght);
1530 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1531 struct ubifs_nbranch *branch;
1534 if (nnode->parent) {
1535 branch = &nnode->parent->nbranch[nnode->iip];
1536 if (branch->lnum != lnum || branch->offs != offs)
1538 if (test_bit(DIRTY_CNODE, &nnode->flags))
1542 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1544 if (test_bit(DIRTY_CNODE, &nnode->flags))
1553 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1554 * @c: the UBIFS file-system description object
1555 * @lnum: LEB number where pnode was written
1556 * @offs: offset where pnode was written
1558 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1562 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1563 for (i = 0; i < cnt; i++) {
1564 struct ubifs_pnode *pnode;
1565 struct ubifs_nbranch *branch;
1568 pnode = pnode_lookup(c, i);
1570 return PTR_ERR(pnode);
1571 branch = &pnode->parent->nbranch[pnode->iip];
1572 if (branch->lnum != lnum || branch->offs != offs)
1574 if (test_bit(DIRTY_CNODE, &pnode->flags))
1582 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1583 * @c: the UBIFS file-system description object
1584 * @lnum: LEB number where ltab node was written
1585 * @offs: offset where ltab node was written
1587 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1589 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1591 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1595 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1596 * @c: the UBIFS file-system description object
1597 * @lnum: LEB number where lsave node was written
1598 * @offs: offset where lsave node was written
1600 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1602 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1604 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1608 * dbg_is_node_dirty - determine if a node is dirty.
1609 * @c: the UBIFS file-system description object
1610 * @node_type: node type
1611 * @lnum: LEB number where node was written
1612 * @offs: offset where node was written
1614 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1617 switch (node_type) {
1618 case UBIFS_LPT_NNODE:
1619 return dbg_is_nnode_dirty(c, lnum, offs);
1620 case UBIFS_LPT_PNODE:
1621 return dbg_is_pnode_dirty(c, lnum, offs);
1622 case UBIFS_LPT_LTAB:
1623 return dbg_is_ltab_dirty(c, lnum, offs);
1624 case UBIFS_LPT_LSAVE:
1625 return dbg_is_lsave_dirty(c, lnum, offs);
1631 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1632 * @c: the UBIFS file-system description object
1633 * @lnum: LEB number where node was written
1634 * @offs: offset where node was written
1636 * This function returns %0 on success and a negative error code on failure.
1638 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1640 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1644 if (!dbg_is_chk_lprops(c))
1647 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1649 ubifs_err("cannot allocate memory for ltab checking");
1653 dbg_lp("LEB %d", lnum);
1655 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1660 if (!is_a_node(c, p, len)) {
1663 pad_len = get_pad_len(c, p, len);
1670 if (!dbg_is_all_ff(p, len)) {
1671 dbg_msg("invalid empty space in LEB %d at %d",
1672 lnum, c->leb_size - len);
1675 i = lnum - c->lpt_first;
1676 if (len != c->ltab[i].free) {
1677 dbg_msg("invalid free space in LEB %d "
1678 "(free %d, expected %d)",
1679 lnum, len, c->ltab[i].free);
1682 if (dirty != c->ltab[i].dirty) {
1683 dbg_msg("invalid dirty space in LEB %d "
1684 "(dirty %d, expected %d)",
1685 lnum, dirty, c->ltab[i].dirty);
1690 node_type = get_lpt_node_type(c, p, &node_num);
1691 node_len = get_lpt_node_len(c, node_type);
1692 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1706 * dbg_check_ltab - check the free and dirty space in the ltab.
1707 * @c: the UBIFS file-system description object
1709 * This function returns %0 on success and a negative error code on failure.
1711 int dbg_check_ltab(struct ubifs_info *c)
1713 int lnum, err, i, cnt;
1715 if (!dbg_is_chk_lprops(c))
1718 /* Bring the entire tree into memory */
1719 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1720 for (i = 0; i < cnt; i++) {
1721 struct ubifs_pnode *pnode;
1723 pnode = pnode_lookup(c, i);
1725 return PTR_ERR(pnode);
1730 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1734 /* Check each LEB */
1735 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1736 err = dbg_check_ltab_lnum(c, lnum);
1738 dbg_err("failed at LEB %d", lnum);
1743 dbg_lp("succeeded");
1748 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1749 * @c: the UBIFS file-system description object
1751 * This function returns %0 on success and a negative error code on failure.
1753 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1758 if (!dbg_is_chk_lprops(c))
1761 for (i = 0; i < c->lpt_lebs; i++) {
1762 if (c->ltab[i].tgc || c->ltab[i].cmt)
1764 if (i + c->lpt_first == c->nhead_lnum)
1765 free += c->leb_size - c->nhead_offs;
1766 else if (c->ltab[i].free == c->leb_size)
1767 free += c->leb_size;
1769 if (free < c->lpt_sz) {
1770 dbg_err("LPT space error: free %lld lpt_sz %lld",
1772 dbg_dump_lpt_info(c);
1773 dbg_dump_lpt_lebs(c);
1781 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1782 * @c: the UBIFS file-system description object
1783 * @action: what to do
1784 * @len: length written
1786 * This function returns %0 on success and a negative error code on failure.
1787 * The @action argument may be one of:
1788 * o %0 - LPT debugging checking starts, initialize debugging variables;
1789 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1790 * o %2 - switched to a different LEB and wasted @len bytes;
1791 * o %3 - check that we've written the right number of bytes.
1792 * o %4 - wasted @len bytes;
1794 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1796 struct ubifs_debug_info *d = c->dbg;
1797 long long chk_lpt_sz, lpt_sz;
1800 if (!dbg_is_chk_lprops(c))
1807 d->chk_lpt_lebs = 0;
1808 d->chk_lpt_wastage = 0;
1809 if (c->dirty_pn_cnt > c->pnode_cnt) {
1810 dbg_err("dirty pnodes %d exceed max %d",
1811 c->dirty_pn_cnt, c->pnode_cnt);
1814 if (c->dirty_nn_cnt > c->nnode_cnt) {
1815 dbg_err("dirty nnodes %d exceed max %d",
1816 c->dirty_nn_cnt, c->nnode_cnt);
1821 d->chk_lpt_sz += len;
1824 d->chk_lpt_sz += len;
1825 d->chk_lpt_wastage += len;
1826 d->chk_lpt_lebs += 1;
1829 chk_lpt_sz = c->leb_size;
1830 chk_lpt_sz *= d->chk_lpt_lebs;
1831 chk_lpt_sz += len - c->nhead_offs;
1832 if (d->chk_lpt_sz != chk_lpt_sz) {
1833 dbg_err("LPT wrote %lld but space used was %lld",
1834 d->chk_lpt_sz, chk_lpt_sz);
1837 if (d->chk_lpt_sz > c->lpt_sz) {
1838 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1839 d->chk_lpt_sz, c->lpt_sz);
1842 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1843 dbg_err("LPT layout size %lld but wrote %lld",
1844 d->chk_lpt_sz, d->chk_lpt_sz2);
1847 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1848 dbg_err("LPT new nhead offs: expected %d was %d",
1849 d->new_nhead_offs, len);
1852 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1853 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1854 lpt_sz += c->ltab_sz;
1856 lpt_sz += c->lsave_sz;
1857 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1858 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1859 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1863 dbg_dump_lpt_info(c);
1864 dbg_dump_lpt_lebs(c);
1867 d->chk_lpt_sz2 = d->chk_lpt_sz;
1869 d->chk_lpt_wastage = 0;
1870 d->chk_lpt_lebs = 0;
1871 d->new_nhead_offs = len;
1874 d->chk_lpt_sz += len;
1875 d->chk_lpt_wastage += len;
1883 * dbg_dump_lpt_leb - dump an LPT LEB.
1884 * @c: UBIFS file-system description object
1885 * @lnum: LEB number to dump
1887 * This function dumps an LEB from LPT area. Nodes in this area are very
1888 * different to nodes in the main area (e.g., they do not have common headers,
1889 * they do not have 8-byte alignments, etc), so we have a separate function to
1890 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1892 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1894 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1897 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
1898 current->pid, lnum);
1899 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1901 ubifs_err("cannot allocate memory to dump LPT");
1905 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1910 offs = c->leb_size - len;
1911 if (!is_a_node(c, p, len)) {
1914 pad_len = get_pad_len(c, p, len);
1916 printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
1917 lnum, offs, pad_len);
1923 printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
1928 node_type = get_lpt_node_type(c, p, &node_num);
1929 switch (node_type) {
1930 case UBIFS_LPT_PNODE:
1932 node_len = c->pnode_sz;
1934 printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
1935 lnum, offs, node_num);
1937 printk(KERN_DEBUG "LEB %d:%d, pnode\n",
1941 case UBIFS_LPT_NNODE:
1944 struct ubifs_nnode nnode;
1946 node_len = c->nnode_sz;
1948 printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
1949 lnum, offs, node_num);
1951 printk(KERN_DEBUG "LEB %d:%d, nnode, ",
1953 err = ubifs_unpack_nnode(c, p, &nnode);
1954 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1955 printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
1956 nnode.nbranch[i].offs);
1957 if (i != UBIFS_LPT_FANOUT - 1)
1958 printk(KERN_CONT ", ");
1960 printk(KERN_CONT "\n");
1963 case UBIFS_LPT_LTAB:
1964 node_len = c->ltab_sz;
1965 printk(KERN_DEBUG "LEB %d:%d, ltab\n",
1968 case UBIFS_LPT_LSAVE:
1969 node_len = c->lsave_sz;
1970 printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
1973 ubifs_err("LPT node type %d not recognized", node_type);
1981 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
1982 current->pid, lnum);
1989 * dbg_dump_lpt_lebs - dump LPT lebs.
1990 * @c: UBIFS file-system description object
1992 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1995 void dbg_dump_lpt_lebs(const struct ubifs_info *c)
1999 printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
2001 for (i = 0; i < c->lpt_lebs; i++)
2002 dump_lpt_leb(c, i + c->lpt_first);
2003 printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
2008 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2009 * @c: UBIFS file-system description object
2011 * This is a debugging version for 'populate_lsave()' which populates lsave
2012 * with random LEBs instead of useful LEBs, which is good for test coverage.
2013 * Returns zero if lsave has not been populated (this debugging feature is
2014 * disabled) an non-zero if lsave has been populated.
2016 static int dbg_populate_lsave(struct ubifs_info *c)
2018 struct ubifs_lprops *lprops;
2019 struct ubifs_lpt_heap *heap;
2022 if (!dbg_is_chk_gen(c))
2027 for (i = 0; i < c->lsave_cnt; i++)
2028 c->lsave[i] = c->main_first;
2030 list_for_each_entry(lprops, &c->empty_list, list)
2031 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2032 list_for_each_entry(lprops, &c->freeable_list, list)
2033 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2034 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2035 c->lsave[random32() % c->lsave_cnt] = lprops->lnum;
2037 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2038 for (i = 0; i < heap->cnt; i++)
2039 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2040 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2041 for (i = 0; i < heap->cnt; i++)
2042 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2043 heap = &c->lpt_heap[LPROPS_FREE - 1];
2044 for (i = 0; i < heap->cnt; i++)
2045 c->lsave[random32() % c->lsave_cnt] = heap->arr[i]->lnum;
2050 #endif /* CONFIG_UBIFS_FS_DEBUG */