1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
12 * This file implements commit-related functionality of the LEB properties
18 #include <dm/devres.h>
19 #include <linux/crc16.h>
20 #include <linux/slab.h>
21 #include <linux/random.h>
23 #include <linux/bitops.h>
24 #include <linux/compat.h>
25 #include <linux/err.h>
31 static int dbg_populate_lsave(struct ubifs_info *c);
35 * first_dirty_cnode - find first dirty cnode.
36 * @c: UBIFS file-system description object
37 * @nnode: nnode at which to start
39 * This function returns the first dirty cnode or %NULL if there is not one.
41 static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
47 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
48 struct ubifs_cnode *cnode;
50 cnode = nnode->nbranch[i].cnode;
52 test_bit(DIRTY_CNODE, &cnode->flags)) {
53 if (cnode->level == 0)
55 nnode = (struct ubifs_nnode *)cnode;
61 return (struct ubifs_cnode *)nnode;
66 * next_dirty_cnode - find next dirty cnode.
67 * @cnode: cnode from which to begin searching
69 * This function returns the next dirty cnode or %NULL if there is not one.
71 static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
73 struct ubifs_nnode *nnode;
77 nnode = cnode->parent;
80 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
81 cnode = nnode->nbranch[i].cnode;
82 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
83 if (cnode->level == 0)
84 return cnode; /* cnode is a pnode */
85 /* cnode is a nnode */
86 return first_dirty_cnode((struct ubifs_nnode *)cnode);
89 return (struct ubifs_cnode *)nnode;
93 * get_cnodes_to_commit - create list of dirty cnodes to commit.
94 * @c: UBIFS file-system description object
96 * This function returns the number of cnodes to commit.
98 static int get_cnodes_to_commit(struct ubifs_info *c)
100 struct ubifs_cnode *cnode, *cnext;
106 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
109 c->lpt_cnext = first_dirty_cnode(c->nroot);
110 cnode = c->lpt_cnext;
115 ubifs_assert(!test_bit(COW_CNODE, &cnode->flags));
116 __set_bit(COW_CNODE, &cnode->flags);
117 cnext = next_dirty_cnode(cnode);
119 cnode->cnext = c->lpt_cnext;
122 cnode->cnext = cnext;
126 dbg_cmt("committing %d cnodes", cnt);
127 dbg_lp("committing %d cnodes", cnt);
128 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
133 * upd_ltab - update LPT LEB properties.
134 * @c: UBIFS file-system description object
136 * @free: amount of free space
137 * @dirty: amount of dirty space to add
139 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
141 dbg_lp("LEB %d free %d dirty %d to %d +%d",
142 lnum, c->ltab[lnum - c->lpt_first].free,
143 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
144 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
145 c->ltab[lnum - c->lpt_first].free = free;
146 c->ltab[lnum - c->lpt_first].dirty += dirty;
150 * alloc_lpt_leb - allocate an LPT LEB that is empty.
151 * @c: UBIFS file-system description object
152 * @lnum: LEB number is passed and returned here
154 * This function finds the next empty LEB in the ltab starting from @lnum. If a
155 * an empty LEB is found it is returned in @lnum and the function returns %0.
156 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
157 * never to run out of space.
159 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
163 n = *lnum - c->lpt_first + 1;
164 for (i = n; i < c->lpt_lebs; i++) {
165 if (c->ltab[i].tgc || c->ltab[i].cmt)
167 if (c->ltab[i].free == c->leb_size) {
169 *lnum = i + c->lpt_first;
174 for (i = 0; i < n; i++) {
175 if (c->ltab[i].tgc || c->ltab[i].cmt)
177 if (c->ltab[i].free == c->leb_size) {
179 *lnum = i + c->lpt_first;
187 * layout_cnodes - layout cnodes for commit.
188 * @c: UBIFS file-system description object
190 * This function returns %0 on success and a negative error code on failure.
192 static int layout_cnodes(struct ubifs_info *c)
194 int lnum, offs, len, alen, done_lsave, done_ltab, err;
195 struct ubifs_cnode *cnode;
197 err = dbg_chk_lpt_sz(c, 0, 0);
200 cnode = c->lpt_cnext;
203 lnum = c->nhead_lnum;
204 offs = c->nhead_offs;
205 /* Try to place lsave and ltab nicely */
206 done_lsave = !c->big_lpt;
208 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
210 c->lsave_lnum = lnum;
211 c->lsave_offs = offs;
213 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
216 if (offs + c->ltab_sz <= c->leb_size) {
221 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
227 c->dirty_nn_cnt -= 1;
230 c->dirty_pn_cnt -= 1;
232 while (offs + len > c->leb_size) {
233 alen = ALIGN(offs, c->min_io_size);
234 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
235 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
236 err = alloc_lpt_leb(c, &lnum);
240 ubifs_assert(lnum >= c->lpt_first &&
241 lnum <= c->lpt_last);
242 /* Try to place lsave and ltab nicely */
245 c->lsave_lnum = lnum;
246 c->lsave_offs = offs;
248 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
256 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
262 cnode->parent->nbranch[cnode->iip].lnum = lnum;
263 cnode->parent->nbranch[cnode->iip].offs = offs;
269 dbg_chk_lpt_sz(c, 1, len);
270 cnode = cnode->cnext;
271 } while (cnode && cnode != c->lpt_cnext);
273 /* Make sure to place LPT's save table */
275 if (offs + c->lsave_sz > c->leb_size) {
276 alen = ALIGN(offs, c->min_io_size);
277 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
278 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
279 err = alloc_lpt_leb(c, &lnum);
283 ubifs_assert(lnum >= c->lpt_first &&
284 lnum <= c->lpt_last);
287 c->lsave_lnum = lnum;
288 c->lsave_offs = offs;
290 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
293 /* Make sure to place LPT's own lprops table */
295 if (offs + c->ltab_sz > c->leb_size) {
296 alen = ALIGN(offs, c->min_io_size);
297 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
298 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
299 err = alloc_lpt_leb(c, &lnum);
303 ubifs_assert(lnum >= c->lpt_first &&
304 lnum <= c->lpt_last);
309 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
312 alen = ALIGN(offs, c->min_io_size);
313 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
314 dbg_chk_lpt_sz(c, 4, alen - offs);
315 err = dbg_chk_lpt_sz(c, 3, alen);
321 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
322 lnum, offs, len, done_ltab, done_lsave);
323 ubifs_dump_lpt_info(c);
324 ubifs_dump_lpt_lebs(c);
331 * realloc_lpt_leb - allocate an LPT LEB that is empty.
332 * @c: UBIFS file-system description object
333 * @lnum: LEB number is passed and returned here
335 * This function duplicates exactly the results of the function alloc_lpt_leb.
336 * It is used during end commit to reallocate the same LEB numbers that were
337 * allocated by alloc_lpt_leb during start commit.
339 * This function finds the next LEB that was allocated by the alloc_lpt_leb
340 * function starting from @lnum. If a LEB is found it is returned in @lnum and
341 * the function returns %0. Otherwise the function returns -ENOSPC.
342 * Note however, that LPT is designed never to run out of space.
344 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
348 n = *lnum - c->lpt_first + 1;
349 for (i = n; i < c->lpt_lebs; i++)
350 if (c->ltab[i].cmt) {
352 *lnum = i + c->lpt_first;
356 for (i = 0; i < n; i++)
357 if (c->ltab[i].cmt) {
359 *lnum = i + c->lpt_first;
366 * write_cnodes - write cnodes for commit.
367 * @c: UBIFS file-system description object
369 * This function returns %0 on success and a negative error code on failure.
371 static int write_cnodes(struct ubifs_info *c)
373 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
374 struct ubifs_cnode *cnode;
375 void *buf = c->lpt_buf;
377 cnode = c->lpt_cnext;
380 lnum = c->nhead_lnum;
381 offs = c->nhead_offs;
383 /* Ensure empty LEB is unmapped */
385 err = ubifs_leb_unmap(c, lnum);
389 /* Try to place lsave and ltab nicely */
390 done_lsave = !c->big_lpt;
392 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
394 ubifs_pack_lsave(c, buf + offs, c->lsave);
396 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
399 if (offs + c->ltab_sz <= c->leb_size) {
401 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
403 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
406 /* Loop for each cnode */
412 while (offs + len > c->leb_size) {
415 alen = ALIGN(wlen, c->min_io_size);
416 memset(buf + offs, 0xff, alen - wlen);
417 err = ubifs_leb_write(c, lnum, buf + from, from,
422 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
423 err = realloc_lpt_leb(c, &lnum);
427 ubifs_assert(lnum >= c->lpt_first &&
428 lnum <= c->lpt_last);
429 err = ubifs_leb_unmap(c, lnum);
432 /* Try to place lsave and ltab nicely */
435 ubifs_pack_lsave(c, buf + offs, c->lsave);
437 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
442 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
444 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
450 ubifs_pack_nnode(c, buf + offs,
451 (struct ubifs_nnode *)cnode);
453 ubifs_pack_pnode(c, buf + offs,
454 (struct ubifs_pnode *)cnode);
456 * The reason for the barriers is the same as in case of TNC.
457 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
458 * 'dirty_cow_pnode()' are the functions for which this is
461 clear_bit(DIRTY_CNODE, &cnode->flags);
462 smp_mb__before_atomic();
463 clear_bit(COW_CNODE, &cnode->flags);
464 smp_mb__after_atomic();
466 dbg_chk_lpt_sz(c, 1, len);
467 cnode = cnode->cnext;
468 } while (cnode && cnode != c->lpt_cnext);
470 /* Make sure to place LPT's save table */
472 if (offs + c->lsave_sz > c->leb_size) {
474 alen = ALIGN(wlen, c->min_io_size);
475 memset(buf + offs, 0xff, alen - wlen);
476 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
479 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
480 err = realloc_lpt_leb(c, &lnum);
484 ubifs_assert(lnum >= c->lpt_first &&
485 lnum <= c->lpt_last);
486 err = ubifs_leb_unmap(c, lnum);
491 ubifs_pack_lsave(c, buf + offs, c->lsave);
493 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
496 /* Make sure to place LPT's own lprops table */
498 if (offs + c->ltab_sz > c->leb_size) {
500 alen = ALIGN(wlen, c->min_io_size);
501 memset(buf + offs, 0xff, alen - wlen);
502 err = ubifs_leb_write(c, lnum, buf + from, from, alen);
505 dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
506 err = realloc_lpt_leb(c, &lnum);
510 ubifs_assert(lnum >= c->lpt_first &&
511 lnum <= c->lpt_last);
512 err = ubifs_leb_unmap(c, lnum);
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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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(c, "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);
1949 pr_err("failed to unpack_node, error %d\n",
1953 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1954 pr_cont("%d:%d", nnode.nbranch[i].lnum,
1955 nnode.nbranch[i].offs);
1956 if (i != UBIFS_LPT_FANOUT - 1)
1962 case UBIFS_LPT_LTAB:
1963 node_len = c->ltab_sz;
1964 pr_err("LEB %d:%d, ltab\n", lnum, offs);
1966 case UBIFS_LPT_LSAVE:
1967 node_len = c->lsave_sz;
1968 pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1971 ubifs_err(c, "LPT node type %d not recognized", node_type);
1979 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1986 * ubifs_dump_lpt_lebs - dump LPT lebs.
1987 * @c: UBIFS file-system description object
1989 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1992 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1996 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1997 for (i = 0; i < c->lpt_lebs; i++)
1998 dump_lpt_leb(c, i + c->lpt_first);
1999 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
2003 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2004 * @c: UBIFS file-system description object
2006 * This is a debugging version for 'populate_lsave()' which populates lsave
2007 * with random LEBs instead of useful LEBs, which is good for test coverage.
2008 * Returns zero if lsave has not been populated (this debugging feature is
2009 * disabled) an non-zero if lsave has been populated.
2011 static int dbg_populate_lsave(struct ubifs_info *c)
2013 struct ubifs_lprops *lprops;
2014 struct ubifs_lpt_heap *heap;
2017 if (!dbg_is_chk_gen(c))
2019 if (prandom_u32() & 3)
2022 for (i = 0; i < c->lsave_cnt; i++)
2023 c->lsave[i] = c->main_first;
2025 list_for_each_entry(lprops, &c->empty_list, list)
2026 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2027 list_for_each_entry(lprops, &c->freeable_list, list)
2028 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2029 list_for_each_entry(lprops, &c->frdi_idx_list, list)
2030 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
2032 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2033 for (i = 0; i < heap->cnt; i++)
2034 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2035 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2036 for (i = 0; i < heap->cnt; i++)
2037 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2038 heap = &c->lpt_heap[LPROPS_FREE - 1];
2039 for (i = 0; i < heap->cnt; i++)
2040 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;