2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
13 * This file implements commit-related functionality of the LEB properties
18 #include <linux/crc16.h>
19 #include <linux/slab.h>
20 #include <linux/random.h>
22 #include <linux/compat.h>
23 #include <linux/err.h>
29 static int dbg_populate_lsave(struct ubifs_info *c);
33 * first_dirty_cnode - find first dirty cnode.
34 * @c: UBIFS file-system description object
35 * @nnode: nnode at which to start
37 * This function returns the first dirty cnode or %NULL if there is not one.
39 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
45 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
46 struct ubifs_cnode *cnode;
48 cnode = nnode->nbranch[i].cnode;
50 test_bit(DIRTY_CNODE, &cnode->flags)) {
51 if (cnode->level == 0)
53 nnode = (struct ubifs_nnode *)cnode;
59 return (struct ubifs_cnode *)nnode;
64 * next_dirty_cnode - find next dirty cnode.
65 * @cnode: cnode from which to begin searching
67 * This function returns the next dirty cnode or %NULL if there is not one.
69 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
71 struct ubifs_nnode *nnode;
75 nnode = cnode->parent;
78 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
79 cnode = nnode->nbranch[i].cnode;
80 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
81 if (cnode->level == 0)
82 return cnode; /* cnode is a pnode */
83 /* cnode is a nnode */
84 return first_dirty_cnode((struct ubifs_nnode *)cnode);
87 return (struct ubifs_cnode *)nnode;
91 * get_cnodes_to_commit - create list of dirty cnodes to commit.
92 * @c: UBIFS file-system description object
94 * This function returns the number of cnodes to commit.
96 static int get_cnodes_to_commit(struct ubifs_info *c)
98 struct ubifs_cnode *cnode, *cnext;
104 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
107 c->lpt_cnext = first_dirty_cnode(c->nroot);
108 cnode = c->lpt_cnext;
113 ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
114 __set_bit(COW_CNODE, &cnode->flags);
115 cnext = next_dirty_cnode(cnode);
117 cnode->cnext = c->lpt_cnext;
120 cnode->cnext = cnext;
124 dbg_cmt("committing %d cnodes", cnt);
125 dbg_lp("committing %d cnodes", cnt);
126 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
131 * upd_ltab - update LPT LEB properties.
132 * @c: UBIFS file-system description object
134 * @free: amount of free space
135 * @dirty: amount of dirty space to add
137 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
139 dbg_lp("LEB %d free %d dirty %d to %d +%d",
140 lnum, c->ltab[lnum - c->lpt_first].free,
141 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
142 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
143 c->ltab[lnum - c->lpt_first].free = free;
144 c->ltab[lnum - c->lpt_first].dirty += dirty;
148 * alloc_lpt_leb - allocate an LPT LEB that is empty.
149 * @c: UBIFS file-system description object
150 * @lnum: LEB number is passed and returned here
152 * This function finds the next empty LEB in the ltab starting from @lnum. If a
153 * an empty LEB is found it is returned in @lnum and the function returns %0.
154 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
155 * never to run out of space.
157 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
161 n = *lnum - c->lpt_first + 1;
162 for (i = n; i < c->lpt_lebs; i++) {
163 if (c->ltab[i].tgc || c->ltab[i].cmt)
165 if (c->ltab[i].free == c->leb_size) {
167 *lnum = i + c->lpt_first;
172 for (i = 0; i < n; i++) {
173 if (c->ltab[i].tgc || c->ltab[i].cmt)
175 if (c->ltab[i].free == c->leb_size) {
177 *lnum = i + c->lpt_first;
185 * layout_cnodes - layout cnodes for commit.
186 * @c: UBIFS file-system description object
188 * This function returns %0 on success and a negative error code on failure.
190 static int layout_cnodes(struct ubifs_info *c)
192 int lnum, offs, len, alen, done_lsave, done_ltab, err;
193 struct ubifs_cnode *cnode;
195 err = dbg_chk_lpt_sz(c, 0, 0);
198 cnode = c->lpt_cnext;
201 lnum = c->nhead_lnum;
202 offs = c->nhead_offs;
203 /* Try to place lsave and ltab nicely */
204 done_lsave = !c->big_lpt;
206 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
208 c->lsave_lnum = lnum;
209 c->lsave_offs = offs;
211 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
214 if (offs + c->ltab_sz <= c->leb_size) {
219 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
225 c->dirty_nn_cnt -= 1;
228 c->dirty_pn_cnt -= 1;
230 while (offs + len > c->leb_size) {
231 alen = ALIGN(offs, c->min_io_size);
232 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
233 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
234 err = alloc_lpt_leb(c, &lnum);
238 ubifs_assert(lnum >= c->lpt_first &&
239 lnum <= c->lpt_last);
240 /* Try to place lsave and ltab nicely */
243 c->lsave_lnum = lnum;
244 c->lsave_offs = offs;
246 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
254 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
260 cnode->parent->nbranch[cnode->iip].lnum = lnum;
261 cnode->parent->nbranch[cnode->iip].offs = offs;
267 dbg_chk_lpt_sz(c, 1, len);
268 cnode = cnode->cnext;
269 } while (cnode && cnode != c->lpt_cnext);
271 /* Make sure to place LPT's save table */
273 if (offs + c->lsave_sz > c->leb_size) {
274 alen = ALIGN(offs, c->min_io_size);
275 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
276 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
277 err = alloc_lpt_leb(c, &lnum);
281 ubifs_assert(lnum >= c->lpt_first &&
282 lnum <= c->lpt_last);
285 c->lsave_lnum = lnum;
286 c->lsave_offs = offs;
288 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
291 /* Make sure to place LPT's own lprops table */
293 if (offs + c->ltab_sz > c->leb_size) {
294 alen = ALIGN(offs, c->min_io_size);
295 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
296 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
297 err = alloc_lpt_leb(c, &lnum);
301 ubifs_assert(lnum >= c->lpt_first &&
302 lnum <= c->lpt_last);
308 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
311 alen = ALIGN(offs, c->min_io_size);
312 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
313 dbg_chk_lpt_sz(c, 4, alen - offs);
314 err = dbg_chk_lpt_sz(c, 3, alen);
320 ubifs_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
321 lnum, offs, len, done_ltab, done_lsave);
322 ubifs_dump_lpt_info(c);
323 ubifs_dump_lpt_lebs(c);
330 * realloc_lpt_leb - allocate an LPT LEB that is empty.
331 * @c: UBIFS file-system description object
332 * @lnum: LEB number is passed and returned here
334 * This function duplicates exactly the results of the function alloc_lpt_leb.
335 * It is used during end commit to reallocate the same LEB numbers that were
336 * allocated by alloc_lpt_leb during start commit.
338 * This function finds the next LEB that was allocated by the alloc_lpt_leb
339 * function starting from @lnum. If a LEB is found it is returned in @lnum and
340 * the function returns %0. Otherwise the function returns -ENOSPC.
341 * Note however, that LPT is designed never to run out of space.
343 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
347 n = *lnum - c->lpt_first + 1;
348 for (i = n; i < c->lpt_lebs; i++)
349 if (c->ltab[i].cmt) {
351 *lnum = i + c->lpt_first;
355 for (i = 0; i < n; i++)
356 if (c->ltab[i].cmt) {
358 *lnum = i + c->lpt_first;
365 * write_cnodes - write cnodes for commit.
366 * @c: UBIFS file-system description object
368 * This function returns %0 on success and a negative error code on failure.
370 static int write_cnodes(struct ubifs_info *c)
372 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
373 struct ubifs_cnode *cnode;
374 void *buf = c->lpt_buf;
376 cnode = c->lpt_cnext;
379 lnum = c->nhead_lnum;
380 offs = c->nhead_offs;
382 /* Ensure empty LEB is unmapped */
384 err = ubifs_leb_unmap(c, lnum);
388 /* Try to place lsave and ltab nicely */
389 done_lsave = !c->big_lpt;
391 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
393 ubifs_pack_lsave(c, buf + offs, c->lsave);
395 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
398 if (offs + c->ltab_sz <= c->leb_size) {
400 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
402 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
405 /* Loop for each cnode */
411 while (offs + len > c->leb_size) {
414 alen = ALIGN(wlen, c->min_io_size);
415 memset(buf + offs, 0xff, alen - wlen);
416 err = ubifs_leb_write(c, lnum, buf + from, from,
421 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
422 err = realloc_lpt_leb(c, &lnum);
426 ubifs_assert(lnum >= c->lpt_first &&
427 lnum <= c->lpt_last);
428 err = ubifs_leb_unmap(c, lnum);
431 /* Try to place lsave and ltab nicely */
434 ubifs_pack_lsave(c, buf + offs, c->lsave);
436 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
441 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
443 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
449 ubifs_pack_nnode(c, buf + offs,
450 (struct ubifs_nnode *)cnode);
452 ubifs_pack_pnode(c, buf + offs,
453 (struct ubifs_pnode *)cnode);
455 * The reason for the barriers is the same as in case of TNC.
456 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
457 * 'dirty_cow_pnode()' are the functions for which this is
460 clear_bit(DIRTY_CNODE, &cnode->flags);
461 smp_mb__before_clear_bit();
462 clear_bit(COW_CNODE, &cnode->flags);
463 smp_mb__after_clear_bit();
465 dbg_chk_lpt_sz(c, 1, len);
466 cnode = cnode->cnext;
467 } while (cnode && cnode != c->lpt_cnext);
469 /* Make sure to place LPT's save table */
471 if (offs + c->lsave_sz > c->leb_size) {
473 alen = ALIGN(wlen, c->min_io_size);
474 memset(buf + offs, 0xff, alen - wlen);
475 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
478 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
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);
504 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
505 err = realloc_lpt_leb(c, &lnum);
509 ubifs_assert(lnum >= c->lpt_first &&
510 lnum <= c->lpt_last);
511 err = ubifs_leb_unmap(c, lnum);
516 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
518 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
521 /* Write remaining data in buffer */
523 alen = ALIGN(wlen, c->min_io_size);
524 memset(buf + offs, 0xff, alen - wlen);
525 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
529 dbg_chk_lpt_sz(c, 4, alen - wlen);
530 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
534 c->nhead_lnum = lnum;
535 c->nhead_offs = ALIGN(offs, c->min_io_size);
537 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
538 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
539 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
541 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
546 ubifs_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
547 lnum, offs, len, done_ltab, done_lsave);
548 ubifs_dump_lpt_info(c);
549 ubifs_dump_lpt_lebs(c);
556 * next_pnode_to_dirty - find next pnode to dirty.
557 * @c: UBIFS file-system description object
560 * This function returns the next pnode to dirty or %NULL if there are no more
561 * pnodes. Note that pnodes that have never been written (lnum == 0) are
564 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
565 struct ubifs_pnode *pnode)
567 struct ubifs_nnode *nnode;
570 /* Try to go right */
571 nnode = pnode->parent;
572 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
573 if (nnode->nbranch[iip].lnum)
574 return ubifs_get_pnode(c, nnode, iip);
577 /* Go up while can't go right */
579 iip = nnode->iip + 1;
580 nnode = nnode->parent;
583 for (; iip < UBIFS_LPT_FANOUT; iip++) {
584 if (nnode->nbranch[iip].lnum)
587 } while (iip >= UBIFS_LPT_FANOUT);
590 nnode = ubifs_get_nnode(c, nnode, iip);
592 return (void *)nnode;
594 /* Go down to level 1 */
595 while (nnode->level > 1) {
596 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
597 if (nnode->nbranch[iip].lnum)
600 if (iip >= UBIFS_LPT_FANOUT) {
602 * Should not happen, but we need to keep going
607 nnode = ubifs_get_nnode(c, nnode, iip);
609 return (void *)nnode;
612 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
613 if (nnode->nbranch[iip].lnum)
615 if (iip >= UBIFS_LPT_FANOUT)
616 /* Should not happen, but we need to keep going if it does */
618 return ubifs_get_pnode(c, nnode, iip);
622 * pnode_lookup - lookup a pnode in the LPT.
623 * @c: UBIFS file-system description object
624 * @i: pnode number (0 to main_lebs - 1)
626 * This function returns a pointer to the pnode on success or a negative
627 * error code on failure.
629 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
631 int err, h, iip, shft;
632 struct ubifs_nnode *nnode;
635 err = ubifs_read_nnode(c, NULL, 0);
639 i <<= UBIFS_LPT_FANOUT_SHIFT;
641 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
642 for (h = 1; h < c->lpt_hght; h++) {
643 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
644 shft -= UBIFS_LPT_FANOUT_SHIFT;
645 nnode = ubifs_get_nnode(c, nnode, iip);
647 return ERR_CAST(nnode);
649 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
650 return ubifs_get_pnode(c, nnode, iip);
654 * add_pnode_dirt - add dirty space to LPT LEB properties.
655 * @c: UBIFS file-system description object
656 * @pnode: pnode for which to add dirt
658 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
660 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
665 * do_make_pnode_dirty - mark a pnode dirty.
666 * @c: UBIFS file-system description object
667 * @pnode: pnode to mark dirty
669 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
671 /* Assumes cnext list is empty i.e. not called during commit */
672 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
673 struct ubifs_nnode *nnode;
675 c->dirty_pn_cnt += 1;
676 add_pnode_dirt(c, pnode);
677 /* Mark parent and ancestors dirty too */
678 nnode = pnode->parent;
680 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
681 c->dirty_nn_cnt += 1;
682 ubifs_add_nnode_dirt(c, nnode);
683 nnode = nnode->parent;
691 * make_tree_dirty - mark the entire LEB properties tree dirty.
692 * @c: UBIFS file-system description object
694 * This function is used by the "small" LPT model to cause the entire LEB
695 * properties tree to be written. The "small" LPT model does not use LPT
696 * garbage collection because it is more efficient to write the entire tree
697 * (because it is small).
699 * This function returns %0 on success and a negative error code on failure.
701 static int make_tree_dirty(struct ubifs_info *c)
703 struct ubifs_pnode *pnode;
705 pnode = pnode_lookup(c, 0);
707 return PTR_ERR(pnode);
710 do_make_pnode_dirty(c, pnode);
711 pnode = next_pnode_to_dirty(c, pnode);
713 return PTR_ERR(pnode);
719 * need_write_all - determine if the LPT area is running out of free space.
720 * @c: UBIFS file-system description object
722 * This function returns %1 if the LPT area is running out of free space and %0
725 static int need_write_all(struct ubifs_info *c)
730 for (i = 0; i < c->lpt_lebs; i++) {
731 if (i + c->lpt_first == c->nhead_lnum)
732 free += c->leb_size - c->nhead_offs;
733 else if (c->ltab[i].free == c->leb_size)
735 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
738 /* Less than twice the size left */
739 if (free <= c->lpt_sz * 2)
745 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
746 * @c: UBIFS file-system description object
748 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
749 * free space and so may be reused as soon as the next commit is completed.
750 * This function is called during start commit to mark LPT LEBs for trivial GC.
752 static void lpt_tgc_start(struct ubifs_info *c)
756 for (i = 0; i < c->lpt_lebs; i++) {
757 if (i + c->lpt_first == c->nhead_lnum)
759 if (c->ltab[i].dirty > 0 &&
760 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
762 c->ltab[i].free = c->leb_size;
763 c->ltab[i].dirty = 0;
764 dbg_lp("LEB %d", i + c->lpt_first);
770 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
771 * @c: UBIFS file-system description object
773 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
774 * free space and so may be reused as soon as the next commit is completed.
775 * This function is called after the commit is completed (master node has been
776 * written) and un-maps LPT LEBs that were marked for trivial GC.
778 static int lpt_tgc_end(struct ubifs_info *c)
782 for (i = 0; i < c->lpt_lebs; i++)
783 if (c->ltab[i].tgc) {
784 err = ubifs_leb_unmap(c, i + c->lpt_first);
788 dbg_lp("LEB %d", i + c->lpt_first);
794 * populate_lsave - fill the lsave array with important LEB numbers.
795 * @c: the UBIFS file-system description object
797 * This function is only called for the "big" model. It records a small number
798 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
799 * most important to least important): empty, freeable, freeable index, dirty
800 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
801 * their pnodes into memory. That will stop us from having to scan the LPT
802 * straight away. For the "small" model we assume that scanning the LPT is no
805 static void populate_lsave(struct ubifs_info *c)
807 struct ubifs_lprops *lprops;
808 struct ubifs_lpt_heap *heap;
811 ubifs_assert(c->big_lpt);
812 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
813 c->lpt_drty_flgs |= LSAVE_DIRTY;
814 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
818 if (dbg_populate_lsave(c))
822 list_for_each_entry(lprops, &c->empty_list, list) {
823 c->lsave[cnt++] = lprops->lnum;
824 if (cnt >= c->lsave_cnt)
827 list_for_each_entry(lprops, &c->freeable_list, list) {
828 c->lsave[cnt++] = lprops->lnum;
829 if (cnt >= c->lsave_cnt)
832 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
833 c->lsave[cnt++] = lprops->lnum;
834 if (cnt >= c->lsave_cnt)
837 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
838 for (i = 0; i < heap->cnt; i++) {
839 c->lsave[cnt++] = heap->arr[i]->lnum;
840 if (cnt >= c->lsave_cnt)
843 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
844 for (i = 0; i < heap->cnt; i++) {
845 c->lsave[cnt++] = heap->arr[i]->lnum;
846 if (cnt >= c->lsave_cnt)
849 heap = &c->lpt_heap[LPROPS_FREE - 1];
850 for (i = 0; i < heap->cnt; i++) {
851 c->lsave[cnt++] = heap->arr[i]->lnum;
852 if (cnt >= c->lsave_cnt)
855 /* Fill it up completely */
856 while (cnt < c->lsave_cnt)
857 c->lsave[cnt++] = c->main_first;
861 * nnode_lookup - lookup a nnode in the LPT.
862 * @c: UBIFS file-system description object
865 * This function returns a pointer to the nnode on success or a negative
866 * error code on failure.
868 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
871 struct ubifs_nnode *nnode;
874 err = ubifs_read_nnode(c, NULL, 0);
880 iip = i & (UBIFS_LPT_FANOUT - 1);
881 i >>= UBIFS_LPT_FANOUT_SHIFT;
884 nnode = ubifs_get_nnode(c, nnode, iip);
892 * make_nnode_dirty - find a nnode and, if found, make it dirty.
893 * @c: UBIFS file-system description object
894 * @node_num: nnode number of nnode to make dirty
895 * @lnum: LEB number where nnode was written
896 * @offs: offset where nnode was written
898 * This function is used by LPT garbage collection. LPT garbage collection is
899 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
900 * simply involves marking all the nodes in the LEB being garbage-collected as
901 * dirty. The dirty nodes are written next commit, after which the LEB is free
904 * This function returns %0 on success and a negative error code on failure.
906 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
909 struct ubifs_nnode *nnode;
911 nnode = nnode_lookup(c, node_num);
913 return PTR_ERR(nnode);
915 struct ubifs_nbranch *branch;
917 branch = &nnode->parent->nbranch[nnode->iip];
918 if (branch->lnum != lnum || branch->offs != offs)
919 return 0; /* nnode is obsolete */
920 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
921 return 0; /* nnode is obsolete */
922 /* Assumes cnext list is empty i.e. not called during commit */
923 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
924 c->dirty_nn_cnt += 1;
925 ubifs_add_nnode_dirt(c, nnode);
926 /* Mark parent and ancestors dirty too */
927 nnode = nnode->parent;
929 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
930 c->dirty_nn_cnt += 1;
931 ubifs_add_nnode_dirt(c, nnode);
932 nnode = nnode->parent;
941 * make_pnode_dirty - find a pnode and, if found, make it dirty.
942 * @c: UBIFS file-system description object
943 * @node_num: pnode number of pnode to make dirty
944 * @lnum: LEB number where pnode was written
945 * @offs: offset where pnode was written
947 * This function is used by LPT garbage collection. LPT garbage collection is
948 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
949 * simply involves marking all the nodes in the LEB being garbage-collected as
950 * dirty. The dirty nodes are written next commit, after which the LEB is free
953 * This function returns %0 on success and a negative error code on failure.
955 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
958 struct ubifs_pnode *pnode;
959 struct ubifs_nbranch *branch;
961 pnode = pnode_lookup(c, node_num);
963 return PTR_ERR(pnode);
964 branch = &pnode->parent->nbranch[pnode->iip];
965 if (branch->lnum != lnum || branch->offs != offs)
967 do_make_pnode_dirty(c, pnode);
972 * make_ltab_dirty - make ltab node dirty.
973 * @c: UBIFS file-system description object
974 * @lnum: LEB number where ltab was written
975 * @offs: offset where ltab was written
977 * This function is used by LPT garbage collection. LPT garbage collection is
978 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
979 * simply involves marking all the nodes in the LEB being garbage-collected as
980 * dirty. The dirty nodes are written next commit, after which the LEB is free
983 * This function returns %0 on success and a negative error code on failure.
985 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
987 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
988 return 0; /* This ltab node is obsolete */
989 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
990 c->lpt_drty_flgs |= LTAB_DIRTY;
991 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
997 * make_lsave_dirty - make lsave node dirty.
998 * @c: UBIFS file-system description object
999 * @lnum: LEB number where lsave was written
1000 * @offs: offset where lsave was written
1002 * This function is used by LPT garbage collection. LPT garbage collection is
1003 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1004 * simply involves marking all the nodes in the LEB being garbage-collected as
1005 * dirty. The dirty nodes are written next commit, after which the LEB is free
1008 * This function returns %0 on success and a negative error code on failure.
1010 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1012 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1013 return 0; /* This lsave node is obsolete */
1014 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
1015 c->lpt_drty_flgs |= LSAVE_DIRTY;
1016 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
1022 * make_node_dirty - make node dirty.
1023 * @c: UBIFS file-system description object
1024 * @node_type: LPT node type
1025 * @node_num: node number
1026 * @lnum: LEB number where node was written
1027 * @offs: offset where node was written
1029 * This function is used by LPT garbage collection. LPT garbage collection is
1030 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1031 * simply involves marking all the nodes in the LEB being garbage-collected as
1032 * dirty. The dirty nodes are written next commit, after which the LEB is free
1035 * This function returns %0 on success and a negative error code on failure.
1037 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1040 switch (node_type) {
1041 case UBIFS_LPT_NNODE:
1042 return make_nnode_dirty(c, node_num, lnum, offs);
1043 case UBIFS_LPT_PNODE:
1044 return make_pnode_dirty(c, node_num, lnum, offs);
1045 case UBIFS_LPT_LTAB:
1046 return make_ltab_dirty(c, lnum, offs);
1047 case UBIFS_LPT_LSAVE:
1048 return make_lsave_dirty(c, lnum, offs);
1054 * get_lpt_node_len - return the length of a node based on its type.
1055 * @c: UBIFS file-system description object
1056 * @node_type: LPT node type
1058 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1060 switch (node_type) {
1061 case UBIFS_LPT_NNODE:
1063 case UBIFS_LPT_PNODE:
1065 case UBIFS_LPT_LTAB:
1067 case UBIFS_LPT_LSAVE:
1074 * get_pad_len - return the length of padding in a buffer.
1075 * @c: UBIFS file-system description object
1077 * @len: length of buffer
1079 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1083 if (c->min_io_size == 1)
1085 offs = c->leb_size - len;
1086 pad_len = ALIGN(offs, c->min_io_size) - offs;
1091 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1092 * @c: UBIFS file-system description object
1094 * @node_num: node number is returned here
1096 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1099 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1100 int pos = 0, node_type;
1102 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1103 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1108 * is_a_node - determine if a buffer contains a node.
1109 * @c: UBIFS file-system description object
1111 * @len: length of buffer
1113 * This function returns %1 if the buffer contains a node or %0 if it does not.
1115 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1117 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1118 int pos = 0, node_type, node_len;
1119 uint16_t crc, calc_crc;
1121 if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1123 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1124 if (node_type == UBIFS_LPT_NOT_A_NODE)
1126 node_len = get_lpt_node_len(c, node_type);
1127 if (!node_len || node_len > len)
1131 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1132 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1133 node_len - UBIFS_LPT_CRC_BYTES);
1134 if (crc != calc_crc)
1140 * lpt_gc_lnum - garbage collect a LPT LEB.
1141 * @c: UBIFS file-system description object
1142 * @lnum: LEB number to garbage collect
1144 * LPT garbage collection is used only for the "big" LPT model
1145 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1146 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1147 * next commit, after which the LEB is free to be reused.
1149 * This function returns %0 on success and a negative error code on failure.
1151 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1153 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1154 void *buf = c->lpt_buf;
1156 dbg_lp("LEB %d", lnum);
1158 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1163 if (!is_a_node(c, buf, len)) {
1166 pad_len = get_pad_len(c, buf, len);
1174 node_type = get_lpt_node_type(c, buf, &node_num);
1175 node_len = get_lpt_node_len(c, node_type);
1176 offs = c->leb_size - len;
1177 ubifs_assert(node_len != 0);
1178 mutex_lock(&c->lp_mutex);
1179 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1180 mutex_unlock(&c->lp_mutex);
1190 * lpt_gc - LPT garbage collection.
1191 * @c: UBIFS file-system description object
1193 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1194 * Returns %0 on success and a negative error code on failure.
1196 static int lpt_gc(struct ubifs_info *c)
1198 int i, lnum = -1, dirty = 0;
1200 mutex_lock(&c->lp_mutex);
1201 for (i = 0; i < c->lpt_lebs; i++) {
1202 ubifs_assert(!c->ltab[i].tgc);
1203 if (i + c->lpt_first == c->nhead_lnum ||
1204 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1206 if (c->ltab[i].dirty > dirty) {
1207 dirty = c->ltab[i].dirty;
1208 lnum = i + c->lpt_first;
1211 mutex_unlock(&c->lp_mutex);
1214 return lpt_gc_lnum(c, lnum);
1218 * ubifs_lpt_start_commit - UBIFS commit starts.
1219 * @c: the UBIFS file-system description object
1221 * This function has to be called when UBIFS starts the commit operation.
1222 * This function "freezes" all currently dirty LEB properties and does not
1223 * change them anymore. Further changes are saved and tracked separately
1224 * because they are not part of this commit. This function returns zero in case
1225 * of success and a negative error code in case of failure.
1227 int ubifs_lpt_start_commit(struct ubifs_info *c)
1233 mutex_lock(&c->lp_mutex);
1234 err = dbg_chk_lpt_free_spc(c);
1237 err = dbg_check_ltab(c);
1241 if (c->check_lpt_free) {
1243 * We ensure there is enough free space in
1244 * ubifs_lpt_post_commit() by marking nodes dirty. That
1245 * information is lost when we unmount, so we also need
1246 * to check free space once after mounting also.
1248 c->check_lpt_free = 0;
1249 while (need_write_all(c)) {
1250 mutex_unlock(&c->lp_mutex);
1254 mutex_lock(&c->lp_mutex);
1260 if (!c->dirty_pn_cnt) {
1261 dbg_cmt("no cnodes to commit");
1266 if (!c->big_lpt && need_write_all(c)) {
1267 /* If needed, write everything */
1268 err = make_tree_dirty(c);
1277 cnt = get_cnodes_to_commit(c);
1278 ubifs_assert(cnt != 0);
1280 err = layout_cnodes(c);
1284 /* Copy the LPT's own lprops for end commit to write */
1285 memcpy(c->ltab_cmt, c->ltab,
1286 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1287 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1290 mutex_unlock(&c->lp_mutex);
1295 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1296 * @c: UBIFS file-system description object
1298 static void free_obsolete_cnodes(struct ubifs_info *c)
1300 struct ubifs_cnode *cnode, *cnext;
1302 cnext = c->lpt_cnext;
1307 cnext = cnode->cnext;
1308 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1311 cnode->cnext = NULL;
1312 } while (cnext != c->lpt_cnext);
1313 c->lpt_cnext = NULL;
1318 * ubifs_lpt_end_commit - finish the commit operation.
1319 * @c: the UBIFS file-system description object
1321 * This function has to be called when the commit operation finishes. It
1322 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1323 * the media. Returns zero in case of success and a negative error code in case
1326 int ubifs_lpt_end_commit(struct ubifs_info *c)
1335 err = write_cnodes(c);
1339 mutex_lock(&c->lp_mutex);
1340 free_obsolete_cnodes(c);
1341 mutex_unlock(&c->lp_mutex);
1348 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1349 * @c: UBIFS file-system description object
1351 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1352 * commit for the "big" LPT model.
1354 int ubifs_lpt_post_commit(struct ubifs_info *c)
1358 mutex_lock(&c->lp_mutex);
1359 err = lpt_tgc_end(c);
1363 while (need_write_all(c)) {
1364 mutex_unlock(&c->lp_mutex);
1368 mutex_lock(&c->lp_mutex);
1371 mutex_unlock(&c->lp_mutex);
1376 * first_nnode - find the first nnode in memory.
1377 * @c: UBIFS file-system description object
1378 * @hght: height of tree where nnode found is returned here
1380 * This function returns a pointer to the nnode found or %NULL if no nnode is
1381 * found. This function is a helper to 'ubifs_lpt_free()'.
1383 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1385 struct ubifs_nnode *nnode;
1392 for (h = 1; h < c->lpt_hght; h++) {
1394 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1395 if (nnode->nbranch[i].nnode) {
1397 nnode = nnode->nbranch[i].nnode;
1409 * next_nnode - find the next nnode in memory.
1410 * @c: UBIFS file-system description object
1411 * @nnode: nnode from which to start.
1412 * @hght: height of tree where nnode is, is passed and returned here
1414 * This function returns a pointer to the nnode found or %NULL if no nnode is
1415 * found. This function is a helper to 'ubifs_lpt_free()'.
1417 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1418 struct ubifs_nnode *nnode, int *hght)
1420 struct ubifs_nnode *parent;
1421 int iip, h, i, found;
1423 parent = nnode->parent;
1426 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1430 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1431 nnode = parent->nbranch[iip].nnode;
1439 for (h = *hght + 1; h < c->lpt_hght; h++) {
1441 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1442 if (nnode->nbranch[i].nnode) {
1444 nnode = nnode->nbranch[i].nnode;
1456 * ubifs_lpt_free - free resources owned by the LPT.
1457 * @c: UBIFS file-system description object
1458 * @wr_only: free only resources used for writing
1460 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1462 struct ubifs_nnode *nnode;
1465 /* Free write-only things first */
1467 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1479 /* Now free the rest */
1481 nnode = first_nnode(c, &hght);
1483 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1484 kfree(nnode->nbranch[i].nnode);
1485 nnode = next_nnode(c, nnode, &hght);
1487 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1488 kfree(c->lpt_heap[i].arr);
1489 kfree(c->dirty_idx.arr);
1492 kfree(c->lpt_nod_buf);
1497 * Everything below is related to debugging.
1501 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1503 * @len: buffer length
1505 static int dbg_is_all_ff(uint8_t *buf, int len)
1509 for (i = 0; i < len; i++)
1516 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1517 * @c: the UBIFS file-system description object
1518 * @lnum: LEB number where nnode was written
1519 * @offs: offset where nnode was written
1521 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1523 struct ubifs_nnode *nnode;
1526 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1527 nnode = first_nnode(c, &hght);
1528 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1529 struct ubifs_nbranch *branch;
1532 if (nnode->parent) {
1533 branch = &nnode->parent->nbranch[nnode->iip];
1534 if (branch->lnum != lnum || branch->offs != offs)
1536 if (test_bit(DIRTY_CNODE, &nnode->flags))
1540 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1542 if (test_bit(DIRTY_CNODE, &nnode->flags))
1551 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1552 * @c: the UBIFS file-system description object
1553 * @lnum: LEB number where pnode was written
1554 * @offs: offset where pnode was written
1556 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1560 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1561 for (i = 0; i < cnt; i++) {
1562 struct ubifs_pnode *pnode;
1563 struct ubifs_nbranch *branch;
1566 pnode = pnode_lookup(c, i);
1568 return PTR_ERR(pnode);
1569 branch = &pnode->parent->nbranch[pnode->iip];
1570 if (branch->lnum != lnum || branch->offs != offs)
1572 if (test_bit(DIRTY_CNODE, &pnode->flags))
1580 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1581 * @c: the UBIFS file-system description object
1582 * @lnum: LEB number where ltab node was written
1583 * @offs: offset where ltab node was written
1585 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1587 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1589 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1593 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1594 * @c: the UBIFS file-system description object
1595 * @lnum: LEB number where lsave node was written
1596 * @offs: offset where lsave node was written
1598 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1600 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1602 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1606 * dbg_is_node_dirty - determine if a node is dirty.
1607 * @c: the UBIFS file-system description object
1608 * @node_type: node type
1609 * @lnum: LEB number where node was written
1610 * @offs: offset where node was written
1612 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1615 switch (node_type) {
1616 case UBIFS_LPT_NNODE:
1617 return dbg_is_nnode_dirty(c, lnum, offs);
1618 case UBIFS_LPT_PNODE:
1619 return dbg_is_pnode_dirty(c, lnum, offs);
1620 case UBIFS_LPT_LTAB:
1621 return dbg_is_ltab_dirty(c, lnum, offs);
1622 case UBIFS_LPT_LSAVE:
1623 return dbg_is_lsave_dirty(c, lnum, offs);
1629 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1630 * @c: the UBIFS file-system description object
1631 * @lnum: LEB number where node was written
1632 * @offs: offset where node was written
1634 * This function returns %0 on success and a negative error code on failure.
1636 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1638 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1642 if (!dbg_is_chk_lprops(c))
1645 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1647 ubifs_err("cannot allocate memory for ltab checking");
1651 dbg_lp("LEB %d", lnum);
1653 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1658 if (!is_a_node(c, p, len)) {
1661 pad_len = get_pad_len(c, p, len);
1668 if (!dbg_is_all_ff(p, len)) {
1669 ubifs_err("invalid empty space in LEB %d at %d",
1670 lnum, c->leb_size - len);
1673 i = lnum - c->lpt_first;
1674 if (len != c->ltab[i].free) {
1675 ubifs_err("invalid free space in LEB %d (free %d, expected %d)",
1676 lnum, len, c->ltab[i].free);
1679 if (dirty != c->ltab[i].dirty) {
1680 ubifs_err("invalid dirty space in LEB %d (dirty %d, expected %d)",
1681 lnum, dirty, c->ltab[i].dirty);
1686 node_type = get_lpt_node_type(c, p, &node_num);
1687 node_len = get_lpt_node_len(c, node_type);
1688 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1702 * dbg_check_ltab - check the free and dirty space in the ltab.
1703 * @c: the UBIFS file-system description object
1705 * This function returns %0 on success and a negative error code on failure.
1707 int dbg_check_ltab(struct ubifs_info *c)
1709 int lnum, err, i, cnt;
1711 if (!dbg_is_chk_lprops(c))
1714 /* Bring the entire tree into memory */
1715 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1716 for (i = 0; i < cnt; i++) {
1717 struct ubifs_pnode *pnode;
1719 pnode = pnode_lookup(c, i);
1721 return PTR_ERR(pnode);
1726 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1730 /* Check each LEB */
1731 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1732 err = dbg_check_ltab_lnum(c, lnum);
1734 ubifs_err("failed at LEB %d", lnum);
1739 dbg_lp("succeeded");
1744 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1745 * @c: the UBIFS file-system description object
1747 * This function returns %0 on success and a negative error code on failure.
1749 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1754 if (!dbg_is_chk_lprops(c))
1757 for (i = 0; i < c->lpt_lebs; i++) {
1758 if (c->ltab[i].tgc || c->ltab[i].cmt)
1760 if (i + c->lpt_first == c->nhead_lnum)
1761 free += c->leb_size - c->nhead_offs;
1762 else if (c->ltab[i].free == c->leb_size)
1763 free += c->leb_size;
1765 if (free < c->lpt_sz) {
1766 ubifs_err("LPT space error: free %lld lpt_sz %lld",
1768 ubifs_dump_lpt_info(c);
1769 ubifs_dump_lpt_lebs(c);
1777 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1778 * @c: the UBIFS file-system description object
1779 * @action: what to do
1780 * @len: length written
1782 * This function returns %0 on success and a negative error code on failure.
1783 * The @action argument may be one of:
1784 * o %0 - LPT debugging checking starts, initialize debugging variables;
1785 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1786 * o %2 - switched to a different LEB and wasted @len bytes;
1787 * o %3 - check that we've written the right number of bytes.
1788 * o %4 - wasted @len bytes;
1790 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1792 struct ubifs_debug_info *d = c->dbg;
1793 long long chk_lpt_sz, lpt_sz;
1796 if (!dbg_is_chk_lprops(c))
1803 d->chk_lpt_lebs = 0;
1804 d->chk_lpt_wastage = 0;
1805 if (c->dirty_pn_cnt > c->pnode_cnt) {
1806 ubifs_err("dirty pnodes %d exceed max %d",
1807 c->dirty_pn_cnt, c->pnode_cnt);
1810 if (c->dirty_nn_cnt > c->nnode_cnt) {
1811 ubifs_err("dirty nnodes %d exceed max %d",
1812 c->dirty_nn_cnt, c->nnode_cnt);
1817 d->chk_lpt_sz += len;
1820 d->chk_lpt_sz += len;
1821 d->chk_lpt_wastage += len;
1822 d->chk_lpt_lebs += 1;
1825 chk_lpt_sz = c->leb_size;
1826 chk_lpt_sz *= d->chk_lpt_lebs;
1827 chk_lpt_sz += len - c->nhead_offs;
1828 if (d->chk_lpt_sz != chk_lpt_sz) {
1829 ubifs_err("LPT wrote %lld but space used was %lld",
1830 d->chk_lpt_sz, chk_lpt_sz);
1833 if (d->chk_lpt_sz > c->lpt_sz) {
1834 ubifs_err("LPT wrote %lld but lpt_sz is %lld",
1835 d->chk_lpt_sz, c->lpt_sz);
1838 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1839 ubifs_err("LPT layout size %lld but wrote %lld",
1840 d->chk_lpt_sz, d->chk_lpt_sz2);
1843 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1844 ubifs_err("LPT new nhead offs: expected %d was %d",
1845 d->new_nhead_offs, len);
1848 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1849 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1850 lpt_sz += c->ltab_sz;
1852 lpt_sz += c->lsave_sz;
1853 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1854 ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1855 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1859 ubifs_dump_lpt_info(c);
1860 ubifs_dump_lpt_lebs(c);
1863 d->chk_lpt_sz2 = d->chk_lpt_sz;
1865 d->chk_lpt_wastage = 0;
1866 d->chk_lpt_lebs = 0;
1867 d->new_nhead_offs = len;
1870 d->chk_lpt_sz += len;
1871 d->chk_lpt_wastage += len;
1879 * ubifs_dump_lpt_leb - dump an LPT LEB.
1880 * @c: UBIFS file-system description object
1881 * @lnum: LEB number to dump
1883 * This function dumps an LEB from LPT area. Nodes in this area are very
1884 * different to nodes in the main area (e.g., they do not have common headers,
1885 * they do not have 8-byte alignments, etc), so we have a separate function to
1886 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1888 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1890 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1893 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
1894 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1896 ubifs_err("cannot allocate memory to dump LPT");
1900 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1905 offs = c->leb_size - len;
1906 if (!is_a_node(c, p, len)) {
1909 pad_len = get_pad_len(c, p, len);
1911 pr_err("LEB %d:%d, pad %d bytes\n",
1912 lnum, offs, pad_len);
1918 pr_err("LEB %d:%d, free %d bytes\n",
1923 node_type = get_lpt_node_type(c, p, &node_num);
1924 switch (node_type) {
1925 case UBIFS_LPT_PNODE:
1927 node_len = c->pnode_sz;
1929 pr_err("LEB %d:%d, pnode num %d\n",
1930 lnum, offs, node_num);
1932 pr_err("LEB %d:%d, pnode\n", lnum, offs);
1935 case UBIFS_LPT_NNODE:
1938 struct ubifs_nnode nnode;
1940 node_len = c->nnode_sz;
1942 pr_err("LEB %d:%d, nnode num %d, ",
1943 lnum, offs, node_num);
1945 pr_err("LEB %d:%d, nnode, ",
1947 err = ubifs_unpack_nnode(c, p, &nnode);
1948 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1949 pr_cont("%d:%d", nnode.nbranch[i].lnum,
1950 nnode.nbranch[i].offs);
1951 if (i != UBIFS_LPT_FANOUT - 1)
1957 case UBIFS_LPT_LTAB:
1958 node_len = c->ltab_sz;
1959 pr_err("LEB %d:%d, ltab\n", lnum, offs);
1961 case UBIFS_LPT_LSAVE:
1962 node_len = c->lsave_sz;
1963 pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1966 ubifs_err("LPT node type %d not recognized", node_type);
1974 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1981 * ubifs_dump_lpt_lebs - dump LPT lebs.
1982 * @c: UBIFS file-system description object
1984 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1987 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1991 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1992 for (i = 0; i < c->lpt_lebs; i++)
1993 dump_lpt_leb(c, i + c->lpt_first);
1994 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
1998 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1999 * @c: UBIFS file-system description object
2001 * This is a debugging version for 'populate_lsave()' which populates lsave
2002 * with random LEBs instead of useful LEBs, which is good for test coverage.
2003 * Returns zero if lsave has not been populated (this debugging feature is
2004 * disabled) an non-zero if lsave has been populated.
2006 static int dbg_populate_lsave(struct ubifs_info *c)
2008 struct ubifs_lprops *lprops;
2009 struct ubifs_lpt_heap *heap;
2012 if (!dbg_is_chk_gen(c))
2014 if (prandom_u32() & 3)
2017 for (i = 0; i < c->lsave_cnt; i++)
2018 c->lsave[i] = c->main_first;
2020 list_for_each_entry(lprops, &c->empty_list, list)
2021 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2022 list_for_each_entry(lprops, &c->freeable_list, list)
2023 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2024 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2025 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2027 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2028 for (i = 0; i < heap->cnt; i++)
2029 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2030 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2031 for (i = 0; i < heap->cnt; i++)
2032 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2033 heap = &c->lpt_heap[LPROPS_FREE - 1];
2034 for (i = 0; i < heap->cnt; i++)
2035 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;