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 static int dbg_populate_lsave(struct ubifs_info
*c
);
36 * first_dirty_cnode - find first dirty cnode.
37 * @c: UBIFS file-system description object
38 * @nnode: nnode at which to start
40 * This function returns the first dirty cnode or %NULL if there is not one.
42 static struct ubifs_cnode
*first_dirty_cnode(struct ubifs_nnode
*nnode
)
48 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
49 struct ubifs_cnode
*cnode
;
51 cnode
= nnode
->nbranch
[i
].cnode
;
53 test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
54 if (cnode
->level
== 0)
56 nnode
= (struct ubifs_nnode
*)cnode
;
62 return (struct ubifs_cnode
*)nnode
;
67 * next_dirty_cnode - find next dirty cnode.
68 * @cnode: cnode from which to begin searching
70 * This function returns the next dirty cnode or %NULL if there is not one.
72 static struct ubifs_cnode
*next_dirty_cnode(struct ubifs_cnode
*cnode
)
74 struct ubifs_nnode
*nnode
;
78 nnode
= cnode
->parent
;
81 for (i
= cnode
->iip
+ 1; i
< UBIFS_LPT_FANOUT
; i
++) {
82 cnode
= nnode
->nbranch
[i
].cnode
;
83 if (cnode
&& test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
84 if (cnode
->level
== 0)
85 return cnode
; /* cnode is a pnode */
86 /* cnode is a nnode */
87 return first_dirty_cnode((struct ubifs_nnode
*)cnode
);
90 return (struct ubifs_cnode
*)nnode
;
94 * get_cnodes_to_commit - create list of dirty cnodes to commit.
95 * @c: UBIFS file-system description object
97 * This function returns the number of cnodes to commit.
99 static int get_cnodes_to_commit(struct ubifs_info
*c
)
101 struct ubifs_cnode
*cnode
, *cnext
;
107 if (!test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
110 c
->lpt_cnext
= first_dirty_cnode(c
->nroot
);
111 cnode
= c
->lpt_cnext
;
116 ubifs_assert(!test_bit(COW_CNODE
, &cnode
->flags
));
117 __set_bit(COW_CNODE
, &cnode
->flags
);
118 cnext
= next_dirty_cnode(cnode
);
120 cnode
->cnext
= c
->lpt_cnext
;
123 cnode
->cnext
= cnext
;
127 dbg_cmt("committing %d cnodes", cnt
);
128 dbg_lp("committing %d cnodes", cnt
);
129 ubifs_assert(cnt
== c
->dirty_nn_cnt
+ c
->dirty_pn_cnt
);
134 * upd_ltab - update LPT LEB properties.
135 * @c: UBIFS file-system description object
137 * @free: amount of free space
138 * @dirty: amount of dirty space to add
140 static void upd_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
142 dbg_lp("LEB %d free %d dirty %d to %d +%d",
143 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
144 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
145 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
146 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
147 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
151 * alloc_lpt_leb - allocate an LPT LEB that is empty.
152 * @c: UBIFS file-system description object
153 * @lnum: LEB number is passed and returned here
155 * This function finds the next empty LEB in the ltab starting from @lnum. If a
156 * an empty LEB is found it is returned in @lnum and the function returns %0.
157 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
158 * never to run out of space.
160 static int alloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
164 n
= *lnum
- c
->lpt_first
+ 1;
165 for (i
= n
; i
< c
->lpt_lebs
; i
++) {
166 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
168 if (c
->ltab
[i
].free
== c
->leb_size
) {
170 *lnum
= i
+ c
->lpt_first
;
175 for (i
= 0; i
< n
; i
++) {
176 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
178 if (c
->ltab
[i
].free
== c
->leb_size
) {
180 *lnum
= i
+ c
->lpt_first
;
188 * layout_cnodes - layout cnodes for commit.
189 * @c: UBIFS file-system description object
191 * This function returns %0 on success and a negative error code on failure.
193 static int layout_cnodes(struct ubifs_info
*c
)
195 int lnum
, offs
, len
, alen
, done_lsave
, done_ltab
, err
;
196 struct ubifs_cnode
*cnode
;
198 err
= dbg_chk_lpt_sz(c
, 0, 0);
201 cnode
= c
->lpt_cnext
;
204 lnum
= c
->nhead_lnum
;
205 offs
= c
->nhead_offs
;
206 /* Try to place lsave and ltab nicely */
207 done_lsave
= !c
->big_lpt
;
209 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
211 c
->lsave_lnum
= lnum
;
212 c
->lsave_offs
= offs
;
214 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
217 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
222 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
228 c
->dirty_nn_cnt
-= 1;
231 c
->dirty_pn_cnt
-= 1;
233 while (offs
+ len
> c
->leb_size
) {
234 alen
= ALIGN(offs
, c
->min_io_size
);
235 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
236 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
237 err
= alloc_lpt_leb(c
, &lnum
);
241 ubifs_assert(lnum
>= c
->lpt_first
&&
242 lnum
<= c
->lpt_last
);
243 /* Try to place lsave and ltab nicely */
246 c
->lsave_lnum
= lnum
;
247 c
->lsave_offs
= offs
;
249 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
257 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
263 cnode
->parent
->nbranch
[cnode
->iip
].lnum
= lnum
;
264 cnode
->parent
->nbranch
[cnode
->iip
].offs
= offs
;
270 dbg_chk_lpt_sz(c
, 1, len
);
271 cnode
= cnode
->cnext
;
272 } while (cnode
&& cnode
!= c
->lpt_cnext
);
274 /* Make sure to place LPT's save table */
276 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
277 alen
= ALIGN(offs
, c
->min_io_size
);
278 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
279 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
280 err
= alloc_lpt_leb(c
, &lnum
);
284 ubifs_assert(lnum
>= c
->lpt_first
&&
285 lnum
<= c
->lpt_last
);
288 c
->lsave_lnum
= lnum
;
289 c
->lsave_offs
= offs
;
291 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
294 /* Make sure to place LPT's own lprops table */
296 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
297 alen
= ALIGN(offs
, c
->min_io_size
);
298 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
299 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
300 err
= alloc_lpt_leb(c
, &lnum
);
304 ubifs_assert(lnum
>= c
->lpt_first
&&
305 lnum
<= c
->lpt_last
);
311 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
314 alen
= ALIGN(offs
, c
->min_io_size
);
315 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
316 dbg_chk_lpt_sz(c
, 4, alen
- offs
);
317 err
= dbg_chk_lpt_sz(c
, 3, alen
);
323 ubifs_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
324 lnum
, offs
, len
, done_ltab
, done_lsave
);
325 ubifs_dump_lpt_info(c
);
326 ubifs_dump_lpt_lebs(c
);
332 * realloc_lpt_leb - allocate an LPT LEB that is empty.
333 * @c: UBIFS file-system description object
334 * @lnum: LEB number is passed and returned here
336 * This function duplicates exactly the results of the function alloc_lpt_leb.
337 * It is used during end commit to reallocate the same LEB numbers that were
338 * allocated by alloc_lpt_leb during start commit.
340 * This function finds the next LEB that was allocated by the alloc_lpt_leb
341 * function starting from @lnum. If a LEB is found it is returned in @lnum and
342 * the function returns %0. Otherwise the function returns -ENOSPC.
343 * Note however, that LPT is designed never to run out of space.
345 static int realloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
349 n
= *lnum
- c
->lpt_first
+ 1;
350 for (i
= n
; i
< c
->lpt_lebs
; i
++)
351 if (c
->ltab
[i
].cmt
) {
353 *lnum
= i
+ c
->lpt_first
;
357 for (i
= 0; i
< n
; i
++)
358 if (c
->ltab
[i
].cmt
) {
360 *lnum
= i
+ c
->lpt_first
;
367 * write_cnodes - write cnodes for commit.
368 * @c: UBIFS file-system description object
370 * This function returns %0 on success and a negative error code on failure.
372 static int write_cnodes(struct ubifs_info
*c
)
374 int lnum
, offs
, len
, from
, err
, wlen
, alen
, done_ltab
, done_lsave
;
375 struct ubifs_cnode
*cnode
;
376 void *buf
= c
->lpt_buf
;
378 cnode
= c
->lpt_cnext
;
381 lnum
= c
->nhead_lnum
;
382 offs
= c
->nhead_offs
;
384 /* Ensure empty LEB is unmapped */
386 err
= ubifs_leb_unmap(c
, lnum
);
390 /* Try to place lsave and ltab nicely */
391 done_lsave
= !c
->big_lpt
;
393 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
395 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
397 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
400 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
402 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
404 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
407 /* Loop for each cnode */
413 while (offs
+ len
> c
->leb_size
) {
416 alen
= ALIGN(wlen
, c
->min_io_size
);
417 memset(buf
+ offs
, 0xff, alen
- wlen
);
418 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
,
423 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
424 err
= realloc_lpt_leb(c
, &lnum
);
428 ubifs_assert(lnum
>= c
->lpt_first
&&
429 lnum
<= c
->lpt_last
);
430 err
= ubifs_leb_unmap(c
, lnum
);
433 /* Try to place lsave and ltab nicely */
436 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
438 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
443 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
445 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
451 ubifs_pack_nnode(c
, buf
+ offs
,
452 (struct ubifs_nnode
*)cnode
);
454 ubifs_pack_pnode(c
, buf
+ offs
,
455 (struct ubifs_pnode
*)cnode
);
457 * The reason for the barriers is the same as in case of TNC.
458 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
459 * 'dirty_cow_pnode()' are the functions for which this is
462 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
463 smp_mb__before_clear_bit();
464 clear_bit(COW_CNODE
, &cnode
->flags
);
465 smp_mb__after_clear_bit();
467 dbg_chk_lpt_sz(c
, 1, len
);
468 cnode
= cnode
->cnext
;
469 } while (cnode
&& cnode
!= c
->lpt_cnext
);
471 /* Make sure to place LPT's save table */
473 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
475 alen
= ALIGN(wlen
, c
->min_io_size
);
476 memset(buf
+ offs
, 0xff, alen
- wlen
);
477 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
480 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
481 err
= realloc_lpt_leb(c
, &lnum
);
485 ubifs_assert(lnum
>= c
->lpt_first
&&
486 lnum
<= c
->lpt_last
);
487 err
= ubifs_leb_unmap(c
, lnum
);
492 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
494 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
497 /* Make sure to place LPT's own lprops table */
499 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
501 alen
= ALIGN(wlen
, c
->min_io_size
);
502 memset(buf
+ offs
, 0xff, alen
- wlen
);
503 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
506 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
507 err
= realloc_lpt_leb(c
, &lnum
);
511 ubifs_assert(lnum
>= c
->lpt_first
&&
512 lnum
<= c
->lpt_last
);
513 err
= ubifs_leb_unmap(c
, lnum
);
518 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
520 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
523 /* Write remaining data in buffer */
525 alen
= ALIGN(wlen
, c
->min_io_size
);
526 memset(buf
+ offs
, 0xff, alen
- wlen
);
527 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
531 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
532 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
536 c
->nhead_lnum
= lnum
;
537 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
539 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
540 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
541 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
543 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
548 ubifs_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
549 lnum
, offs
, len
, done_ltab
, done_lsave
);
550 ubifs_dump_lpt_info(c
);
551 ubifs_dump_lpt_lebs(c
);
557 * next_pnode_to_dirty - find next pnode to dirty.
558 * @c: UBIFS file-system description object
561 * This function returns the next pnode to dirty or %NULL if there are no more
562 * pnodes. Note that pnodes that have never been written (lnum == 0) are
565 static struct ubifs_pnode
*next_pnode_to_dirty(struct ubifs_info
*c
,
566 struct ubifs_pnode
*pnode
)
568 struct ubifs_nnode
*nnode
;
571 /* Try to go right */
572 nnode
= pnode
->parent
;
573 for (iip
= pnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
574 if (nnode
->nbranch
[iip
].lnum
)
575 return ubifs_get_pnode(c
, nnode
, iip
);
578 /* Go up while can't go right */
580 iip
= nnode
->iip
+ 1;
581 nnode
= nnode
->parent
;
584 for (; iip
< UBIFS_LPT_FANOUT
; iip
++) {
585 if (nnode
->nbranch
[iip
].lnum
)
588 } while (iip
>= UBIFS_LPT_FANOUT
);
591 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
593 return (void *)nnode
;
595 /* Go down to level 1 */
596 while (nnode
->level
> 1) {
597 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++) {
598 if (nnode
->nbranch
[iip
].lnum
)
601 if (iip
>= UBIFS_LPT_FANOUT
) {
603 * Should not happen, but we need to keep going
608 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
610 return (void *)nnode
;
613 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++)
614 if (nnode
->nbranch
[iip
].lnum
)
616 if (iip
>= UBIFS_LPT_FANOUT
)
617 /* Should not happen, but we need to keep going if it does */
619 return ubifs_get_pnode(c
, nnode
, iip
);
623 * pnode_lookup - lookup a pnode in the LPT.
624 * @c: UBIFS file-system description object
625 * @i: pnode number (0 to main_lebs - 1)
627 * This function returns a pointer to the pnode on success or a negative
628 * error code on failure.
630 static struct ubifs_pnode
*pnode_lookup(struct ubifs_info
*c
, int i
)
632 int err
, h
, iip
, shft
;
633 struct ubifs_nnode
*nnode
;
636 err
= ubifs_read_nnode(c
, NULL
, 0);
640 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
642 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
643 for (h
= 1; h
< c
->lpt_hght
; h
++) {
644 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
645 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
646 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
648 return ERR_CAST(nnode
);
650 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
651 return ubifs_get_pnode(c
, nnode
, iip
);
655 * add_pnode_dirt - add dirty space to LPT LEB properties.
656 * @c: UBIFS file-system description object
657 * @pnode: pnode for which to add dirt
659 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
661 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
666 * do_make_pnode_dirty - mark a pnode dirty.
667 * @c: UBIFS file-system description object
668 * @pnode: pnode to mark dirty
670 static void do_make_pnode_dirty(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
672 /* Assumes cnext list is empty i.e. not called during commit */
673 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
674 struct ubifs_nnode
*nnode
;
676 c
->dirty_pn_cnt
+= 1;
677 add_pnode_dirt(c
, pnode
);
678 /* Mark parent and ancestors dirty too */
679 nnode
= pnode
->parent
;
681 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
682 c
->dirty_nn_cnt
+= 1;
683 ubifs_add_nnode_dirt(c
, nnode
);
684 nnode
= nnode
->parent
;
692 * make_tree_dirty - mark the entire LEB properties tree dirty.
693 * @c: UBIFS file-system description object
695 * This function is used by the "small" LPT model to cause the entire LEB
696 * properties tree to be written. The "small" LPT model does not use LPT
697 * garbage collection because it is more efficient to write the entire tree
698 * (because it is small).
700 * This function returns %0 on success and a negative error code on failure.
702 static int make_tree_dirty(struct ubifs_info
*c
)
704 struct ubifs_pnode
*pnode
;
706 pnode
= pnode_lookup(c
, 0);
708 return PTR_ERR(pnode
);
711 do_make_pnode_dirty(c
, pnode
);
712 pnode
= next_pnode_to_dirty(c
, pnode
);
714 return PTR_ERR(pnode
);
720 * need_write_all - determine if the LPT area is running out of free space.
721 * @c: UBIFS file-system description object
723 * This function returns %1 if the LPT area is running out of free space and %0
726 static int need_write_all(struct ubifs_info
*c
)
731 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
732 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
733 free
+= c
->leb_size
- c
->nhead_offs
;
734 else if (c
->ltab
[i
].free
== c
->leb_size
)
736 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
739 /* Less than twice the size left */
740 if (free
<= c
->lpt_sz
* 2)
746 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
747 * @c: UBIFS file-system description object
749 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
750 * free space and so may be reused as soon as the next commit is completed.
751 * This function is called during start commit to mark LPT LEBs for trivial GC.
753 static void lpt_tgc_start(struct ubifs_info
*c
)
757 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
758 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
760 if (c
->ltab
[i
].dirty
> 0 &&
761 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
763 c
->ltab
[i
].free
= c
->leb_size
;
764 c
->ltab
[i
].dirty
= 0;
765 dbg_lp("LEB %d", i
+ c
->lpt_first
);
771 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
772 * @c: UBIFS file-system description object
774 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
775 * free space and so may be reused as soon as the next commit is completed.
776 * This function is called after the commit is completed (master node has been
777 * written) and un-maps LPT LEBs that were marked for trivial GC.
779 static int lpt_tgc_end(struct ubifs_info
*c
)
783 for (i
= 0; i
< c
->lpt_lebs
; i
++)
784 if (c
->ltab
[i
].tgc
) {
785 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
789 dbg_lp("LEB %d", i
+ c
->lpt_first
);
795 * populate_lsave - fill the lsave array with important LEB numbers.
796 * @c: the UBIFS file-system description object
798 * This function is only called for the "big" model. It records a small number
799 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
800 * most important to least important): empty, freeable, freeable index, dirty
801 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
802 * their pnodes into memory. That will stop us from having to scan the LPT
803 * straight away. For the "small" model we assume that scanning the LPT is no
806 static void populate_lsave(struct ubifs_info
*c
)
808 struct ubifs_lprops
*lprops
;
809 struct ubifs_lpt_heap
*heap
;
812 ubifs_assert(c
->big_lpt
);
813 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
814 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
815 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
818 if (dbg_populate_lsave(c
))
821 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
822 c
->lsave
[cnt
++] = lprops
->lnum
;
823 if (cnt
>= c
->lsave_cnt
)
826 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
827 c
->lsave
[cnt
++] = lprops
->lnum
;
828 if (cnt
>= c
->lsave_cnt
)
831 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
832 c
->lsave
[cnt
++] = lprops
->lnum
;
833 if (cnt
>= c
->lsave_cnt
)
836 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
837 for (i
= 0; i
< heap
->cnt
; i
++) {
838 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
839 if (cnt
>= c
->lsave_cnt
)
842 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
843 for (i
= 0; i
< heap
->cnt
; i
++) {
844 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
845 if (cnt
>= c
->lsave_cnt
)
848 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
849 for (i
= 0; i
< heap
->cnt
; i
++) {
850 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
851 if (cnt
>= c
->lsave_cnt
)
854 /* Fill it up completely */
855 while (cnt
< c
->lsave_cnt
)
856 c
->lsave
[cnt
++] = c
->main_first
;
860 * nnode_lookup - lookup a nnode in the LPT.
861 * @c: UBIFS file-system description object
864 * This function returns a pointer to the nnode on success or a negative
865 * error code on failure.
867 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
870 struct ubifs_nnode
*nnode
;
873 err
= ubifs_read_nnode(c
, NULL
, 0);
879 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
880 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
883 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
891 * make_nnode_dirty - find a nnode and, if found, make it dirty.
892 * @c: UBIFS file-system description object
893 * @node_num: nnode number of nnode to make dirty
894 * @lnum: LEB number where nnode was written
895 * @offs: offset where nnode was written
897 * This function is used by LPT garbage collection. LPT garbage collection is
898 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
899 * simply involves marking all the nodes in the LEB being garbage-collected as
900 * dirty. The dirty nodes are written next commit, after which the LEB is free
903 * This function returns %0 on success and a negative error code on failure.
905 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
908 struct ubifs_nnode
*nnode
;
910 nnode
= nnode_lookup(c
, node_num
);
912 return PTR_ERR(nnode
);
914 struct ubifs_nbranch
*branch
;
916 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
917 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
918 return 0; /* nnode is obsolete */
919 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
920 return 0; /* nnode is obsolete */
921 /* Assumes cnext list is empty i.e. not called during commit */
922 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
923 c
->dirty_nn_cnt
+= 1;
924 ubifs_add_nnode_dirt(c
, nnode
);
925 /* Mark parent and ancestors dirty too */
926 nnode
= nnode
->parent
;
928 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
929 c
->dirty_nn_cnt
+= 1;
930 ubifs_add_nnode_dirt(c
, nnode
);
931 nnode
= nnode
->parent
;
940 * make_pnode_dirty - find a pnode and, if found, make it dirty.
941 * @c: UBIFS file-system description object
942 * @node_num: pnode number of pnode to make dirty
943 * @lnum: LEB number where pnode was written
944 * @offs: offset where pnode was written
946 * This function is used by LPT garbage collection. LPT garbage collection is
947 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
948 * simply involves marking all the nodes in the LEB being garbage-collected as
949 * dirty. The dirty nodes are written next commit, after which the LEB is free
952 * This function returns %0 on success and a negative error code on failure.
954 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
957 struct ubifs_pnode
*pnode
;
958 struct ubifs_nbranch
*branch
;
960 pnode
= pnode_lookup(c
, node_num
);
962 return PTR_ERR(pnode
);
963 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
964 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
966 do_make_pnode_dirty(c
, pnode
);
971 * make_ltab_dirty - make ltab node dirty.
972 * @c: UBIFS file-system description object
973 * @lnum: LEB number where ltab was written
974 * @offs: offset where ltab was written
976 * This function is used by LPT garbage collection. LPT garbage collection is
977 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
978 * simply involves marking all the nodes in the LEB being garbage-collected as
979 * dirty. The dirty nodes are written next commit, after which the LEB is free
982 * This function returns %0 on success and a negative error code on failure.
984 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
986 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
987 return 0; /* This ltab node is obsolete */
988 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
989 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
990 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
996 * make_lsave_dirty - make lsave node dirty.
997 * @c: UBIFS file-system description object
998 * @lnum: LEB number where lsave was written
999 * @offs: offset where lsave was written
1001 * This function is used by LPT garbage collection. LPT garbage collection is
1002 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1003 * simply involves marking all the nodes in the LEB being garbage-collected as
1004 * dirty. The dirty nodes are written next commit, after which the LEB is free
1007 * This function returns %0 on success and a negative error code on failure.
1009 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1011 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1012 return 0; /* This lsave node is obsolete */
1013 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1014 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1015 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1021 * make_node_dirty - make node dirty.
1022 * @c: UBIFS file-system description object
1023 * @node_type: LPT node type
1024 * @node_num: node number
1025 * @lnum: LEB number where node was written
1026 * @offs: offset where node was written
1028 * This function is used by LPT garbage collection. LPT garbage collection is
1029 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1030 * simply involves marking all the nodes in the LEB being garbage-collected as
1031 * dirty. The dirty nodes are written next commit, after which the LEB is free
1034 * This function returns %0 on success and a negative error code on failure.
1036 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1039 switch (node_type
) {
1040 case UBIFS_LPT_NNODE
:
1041 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1042 case UBIFS_LPT_PNODE
:
1043 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1044 case UBIFS_LPT_LTAB
:
1045 return make_ltab_dirty(c
, lnum
, offs
);
1046 case UBIFS_LPT_LSAVE
:
1047 return make_lsave_dirty(c
, lnum
, offs
);
1053 * get_lpt_node_len - return the length of a node based on its type.
1054 * @c: UBIFS file-system description object
1055 * @node_type: LPT node type
1057 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1059 switch (node_type
) {
1060 case UBIFS_LPT_NNODE
:
1062 case UBIFS_LPT_PNODE
:
1064 case UBIFS_LPT_LTAB
:
1066 case UBIFS_LPT_LSAVE
:
1073 * get_pad_len - return the length of padding in a buffer.
1074 * @c: UBIFS file-system description object
1076 * @len: length of buffer
1078 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1082 if (c
->min_io_size
== 1)
1084 offs
= c
->leb_size
- len
;
1085 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1090 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1091 * @c: UBIFS file-system description object
1093 * @node_num: node number is returned here
1095 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1098 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1099 int pos
= 0, node_type
;
1101 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1102 *node_num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1107 * is_a_node - determine if a buffer contains a node.
1108 * @c: UBIFS file-system description object
1110 * @len: length of buffer
1112 * This function returns %1 if the buffer contains a node or %0 if it does not.
1114 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1116 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1117 int pos
= 0, node_type
, node_len
;
1118 uint16_t crc
, calc_crc
;
1120 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1122 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1123 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1125 node_len
= get_lpt_node_len(c
, node_type
);
1126 if (!node_len
|| node_len
> len
)
1130 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1131 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1132 node_len
- UBIFS_LPT_CRC_BYTES
);
1133 if (crc
!= calc_crc
)
1139 * lpt_gc_lnum - garbage collect a LPT LEB.
1140 * @c: UBIFS file-system description object
1141 * @lnum: LEB number to garbage collect
1143 * LPT garbage collection is used only for the "big" LPT model
1144 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1145 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1146 * next commit, after which the LEB is free to be reused.
1148 * This function returns %0 on success and a negative error code on failure.
1150 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1152 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1153 void *buf
= c
->lpt_buf
;
1155 dbg_lp("LEB %d", lnum
);
1157 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1162 if (!is_a_node(c
, buf
, len
)) {
1165 pad_len
= get_pad_len(c
, buf
, len
);
1173 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1174 node_len
= get_lpt_node_len(c
, node_type
);
1175 offs
= c
->leb_size
- len
;
1176 ubifs_assert(node_len
!= 0);
1177 mutex_lock(&c
->lp_mutex
);
1178 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1179 mutex_unlock(&c
->lp_mutex
);
1189 * lpt_gc - LPT garbage collection.
1190 * @c: UBIFS file-system description object
1192 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1193 * Returns %0 on success and a negative error code on failure.
1195 static int lpt_gc(struct ubifs_info
*c
)
1197 int i
, lnum
= -1, dirty
= 0;
1199 mutex_lock(&c
->lp_mutex
);
1200 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1201 ubifs_assert(!c
->ltab
[i
].tgc
);
1202 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1203 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1205 if (c
->ltab
[i
].dirty
> dirty
) {
1206 dirty
= c
->ltab
[i
].dirty
;
1207 lnum
= i
+ c
->lpt_first
;
1210 mutex_unlock(&c
->lp_mutex
);
1213 return lpt_gc_lnum(c
, lnum
);
1217 * ubifs_lpt_start_commit - UBIFS commit starts.
1218 * @c: the UBIFS file-system description object
1220 * This function has to be called when UBIFS starts the commit operation.
1221 * This function "freezes" all currently dirty LEB properties and does not
1222 * change them anymore. Further changes are saved and tracked separately
1223 * because they are not part of this commit. This function returns zero in case
1224 * of success and a negative error code in case of failure.
1226 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1232 mutex_lock(&c
->lp_mutex
);
1233 err
= dbg_chk_lpt_free_spc(c
);
1236 err
= dbg_check_ltab(c
);
1240 if (c
->check_lpt_free
) {
1242 * We ensure there is enough free space in
1243 * ubifs_lpt_post_commit() by marking nodes dirty. That
1244 * information is lost when we unmount, so we also need
1245 * to check free space once after mounting also.
1247 c
->check_lpt_free
= 0;
1248 while (need_write_all(c
)) {
1249 mutex_unlock(&c
->lp_mutex
);
1253 mutex_lock(&c
->lp_mutex
);
1259 if (!c
->dirty_pn_cnt
) {
1260 dbg_cmt("no cnodes to commit");
1265 if (!c
->big_lpt
&& need_write_all(c
)) {
1266 /* If needed, write everything */
1267 err
= make_tree_dirty(c
);
1276 cnt
= get_cnodes_to_commit(c
);
1277 ubifs_assert(cnt
!= 0);
1279 err
= layout_cnodes(c
);
1283 /* Copy the LPT's own lprops for end commit to write */
1284 memcpy(c
->ltab_cmt
, c
->ltab
,
1285 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1286 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1289 mutex_unlock(&c
->lp_mutex
);
1294 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1295 * @c: UBIFS file-system description object
1297 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1299 struct ubifs_cnode
*cnode
, *cnext
;
1301 cnext
= c
->lpt_cnext
;
1306 cnext
= cnode
->cnext
;
1307 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1310 cnode
->cnext
= NULL
;
1311 } while (cnext
!= c
->lpt_cnext
);
1312 c
->lpt_cnext
= NULL
;
1316 * ubifs_lpt_end_commit - finish the commit operation.
1317 * @c: the UBIFS file-system description object
1319 * This function has to be called when the commit operation finishes. It
1320 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1321 * the media. Returns zero in case of success and a negative error code in case
1324 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1333 err
= write_cnodes(c
);
1337 mutex_lock(&c
->lp_mutex
);
1338 free_obsolete_cnodes(c
);
1339 mutex_unlock(&c
->lp_mutex
);
1345 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1346 * @c: UBIFS file-system description object
1348 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1349 * commit for the "big" LPT model.
1351 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1355 mutex_lock(&c
->lp_mutex
);
1356 err
= lpt_tgc_end(c
);
1360 while (need_write_all(c
)) {
1361 mutex_unlock(&c
->lp_mutex
);
1365 mutex_lock(&c
->lp_mutex
);
1368 mutex_unlock(&c
->lp_mutex
);
1373 * first_nnode - find the first nnode in memory.
1374 * @c: UBIFS file-system description object
1375 * @hght: height of tree where nnode found is returned here
1377 * This function returns a pointer to the nnode found or %NULL if no nnode is
1378 * found. This function is a helper to 'ubifs_lpt_free()'.
1380 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1382 struct ubifs_nnode
*nnode
;
1389 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1391 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1392 if (nnode
->nbranch
[i
].nnode
) {
1394 nnode
= nnode
->nbranch
[i
].nnode
;
1406 * next_nnode - find the next nnode in memory.
1407 * @c: UBIFS file-system description object
1408 * @nnode: nnode from which to start.
1409 * @hght: height of tree where nnode is, is passed and returned here
1411 * This function returns a pointer to the nnode found or %NULL if no nnode is
1412 * found. This function is a helper to 'ubifs_lpt_free()'.
1414 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1415 struct ubifs_nnode
*nnode
, int *hght
)
1417 struct ubifs_nnode
*parent
;
1418 int iip
, h
, i
, found
;
1420 parent
= nnode
->parent
;
1423 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1427 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1428 nnode
= parent
->nbranch
[iip
].nnode
;
1436 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1438 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1439 if (nnode
->nbranch
[i
].nnode
) {
1441 nnode
= nnode
->nbranch
[i
].nnode
;
1453 * ubifs_lpt_free - free resources owned by the LPT.
1454 * @c: UBIFS file-system description object
1455 * @wr_only: free only resources used for writing
1457 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1459 struct ubifs_nnode
*nnode
;
1462 /* Free write-only things first */
1464 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1476 /* Now free the rest */
1478 nnode
= first_nnode(c
, &hght
);
1480 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1481 kfree(nnode
->nbranch
[i
].nnode
);
1482 nnode
= next_nnode(c
, nnode
, &hght
);
1484 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1485 kfree(c
->lpt_heap
[i
].arr
);
1486 kfree(c
->dirty_idx
.arr
);
1489 kfree(c
->lpt_nod_buf
);
1493 * Everything below is related to debugging.
1497 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1499 * @len: buffer length
1501 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1505 for (i
= 0; i
< len
; i
++)
1512 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1513 * @c: the UBIFS file-system description object
1514 * @lnum: LEB number where nnode was written
1515 * @offs: offset where nnode was written
1517 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1519 struct ubifs_nnode
*nnode
;
1522 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1523 nnode
= first_nnode(c
, &hght
);
1524 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1525 struct ubifs_nbranch
*branch
;
1528 if (nnode
->parent
) {
1529 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1530 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1532 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1536 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1538 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1547 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1548 * @c: the UBIFS file-system description object
1549 * @lnum: LEB number where pnode was written
1550 * @offs: offset where pnode was written
1552 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1556 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1557 for (i
= 0; i
< cnt
; i
++) {
1558 struct ubifs_pnode
*pnode
;
1559 struct ubifs_nbranch
*branch
;
1562 pnode
= pnode_lookup(c
, i
);
1564 return PTR_ERR(pnode
);
1565 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1566 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1568 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1576 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1577 * @c: the UBIFS file-system description object
1578 * @lnum: LEB number where ltab node was written
1579 * @offs: offset where ltab node was written
1581 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1583 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1585 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1589 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1590 * @c: the UBIFS file-system description object
1591 * @lnum: LEB number where lsave node was written
1592 * @offs: offset where lsave node was written
1594 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1596 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1598 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1602 * dbg_is_node_dirty - determine if a node is dirty.
1603 * @c: the UBIFS file-system description object
1604 * @node_type: node type
1605 * @lnum: LEB number where node was written
1606 * @offs: offset where node was written
1608 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1611 switch (node_type
) {
1612 case UBIFS_LPT_NNODE
:
1613 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1614 case UBIFS_LPT_PNODE
:
1615 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1616 case UBIFS_LPT_LTAB
:
1617 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1618 case UBIFS_LPT_LSAVE
:
1619 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1625 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1626 * @c: the UBIFS file-system description object
1627 * @lnum: LEB number where node was written
1628 * @offs: offset where node was written
1630 * This function returns %0 on success and a negative error code on failure.
1632 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1634 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1638 if (!dbg_is_chk_lprops(c
))
1641 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1643 ubifs_err("cannot allocate memory for ltab checking");
1647 dbg_lp("LEB %d", lnum
);
1649 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1654 if (!is_a_node(c
, p
, len
)) {
1657 pad_len
= get_pad_len(c
, p
, len
);
1664 if (!dbg_is_all_ff(p
, len
)) {
1665 ubifs_err("invalid empty space in LEB %d at %d",
1666 lnum
, c
->leb_size
- len
);
1669 i
= lnum
- c
->lpt_first
;
1670 if (len
!= c
->ltab
[i
].free
) {
1671 ubifs_err("invalid free space in LEB %d (free %d, expected %d)",
1672 lnum
, len
, c
->ltab
[i
].free
);
1675 if (dirty
!= c
->ltab
[i
].dirty
) {
1676 ubifs_err("invalid dirty space in LEB %d (dirty %d, expected %d)",
1677 lnum
, dirty
, c
->ltab
[i
].dirty
);
1682 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1683 node_len
= get_lpt_node_len(c
, node_type
);
1684 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1698 * dbg_check_ltab - check the free and dirty space in the ltab.
1699 * @c: the UBIFS file-system description object
1701 * This function returns %0 on success and a negative error code on failure.
1703 int dbg_check_ltab(struct ubifs_info
*c
)
1705 int lnum
, err
, i
, cnt
;
1707 if (!dbg_is_chk_lprops(c
))
1710 /* Bring the entire tree into memory */
1711 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1712 for (i
= 0; i
< cnt
; i
++) {
1713 struct ubifs_pnode
*pnode
;
1715 pnode
= pnode_lookup(c
, i
);
1717 return PTR_ERR(pnode
);
1722 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1726 /* Check each LEB */
1727 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1728 err
= dbg_check_ltab_lnum(c
, lnum
);
1730 ubifs_err("failed at LEB %d", lnum
);
1735 dbg_lp("succeeded");
1740 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1741 * @c: the UBIFS file-system description object
1743 * This function returns %0 on success and a negative error code on failure.
1745 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1750 if (!dbg_is_chk_lprops(c
))
1753 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1754 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1756 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1757 free
+= c
->leb_size
- c
->nhead_offs
;
1758 else if (c
->ltab
[i
].free
== c
->leb_size
)
1759 free
+= c
->leb_size
;
1761 if (free
< c
->lpt_sz
) {
1762 ubifs_err("LPT space error: free %lld lpt_sz %lld",
1764 ubifs_dump_lpt_info(c
);
1765 ubifs_dump_lpt_lebs(c
);
1773 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1774 * @c: the UBIFS file-system description object
1775 * @action: what to do
1776 * @len: length written
1778 * This function returns %0 on success and a negative error code on failure.
1779 * The @action argument may be one of:
1780 * o %0 - LPT debugging checking starts, initialize debugging variables;
1781 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1782 * o %2 - switched to a different LEB and wasted @len bytes;
1783 * o %3 - check that we've written the right number of bytes.
1784 * o %4 - wasted @len bytes;
1786 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1788 struct ubifs_debug_info
*d
= c
->dbg
;
1789 long long chk_lpt_sz
, lpt_sz
;
1792 if (!dbg_is_chk_lprops(c
))
1799 d
->chk_lpt_lebs
= 0;
1800 d
->chk_lpt_wastage
= 0;
1801 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1802 ubifs_err("dirty pnodes %d exceed max %d",
1803 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1806 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1807 ubifs_err("dirty nnodes %d exceed max %d",
1808 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1813 d
->chk_lpt_sz
+= len
;
1816 d
->chk_lpt_sz
+= len
;
1817 d
->chk_lpt_wastage
+= len
;
1818 d
->chk_lpt_lebs
+= 1;
1821 chk_lpt_sz
= c
->leb_size
;
1822 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1823 chk_lpt_sz
+= len
- c
->nhead_offs
;
1824 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1825 ubifs_err("LPT wrote %lld but space used was %lld",
1826 d
->chk_lpt_sz
, chk_lpt_sz
);
1829 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1830 ubifs_err("LPT wrote %lld but lpt_sz is %lld",
1831 d
->chk_lpt_sz
, c
->lpt_sz
);
1834 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1835 ubifs_err("LPT layout size %lld but wrote %lld",
1836 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1839 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1840 ubifs_err("LPT new nhead offs: expected %d was %d",
1841 d
->new_nhead_offs
, len
);
1844 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1845 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1846 lpt_sz
+= c
->ltab_sz
;
1848 lpt_sz
+= c
->lsave_sz
;
1849 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1850 ubifs_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1851 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1855 ubifs_dump_lpt_info(c
);
1856 ubifs_dump_lpt_lebs(c
);
1859 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1861 d
->chk_lpt_wastage
= 0;
1862 d
->chk_lpt_lebs
= 0;
1863 d
->new_nhead_offs
= len
;
1866 d
->chk_lpt_sz
+= len
;
1867 d
->chk_lpt_wastage
+= len
;
1875 * ubifs_dump_lpt_leb - dump an LPT LEB.
1876 * @c: UBIFS file-system description object
1877 * @lnum: LEB number to dump
1879 * This function dumps an LEB from LPT area. Nodes in this area are very
1880 * different to nodes in the main area (e.g., they do not have common headers,
1881 * they do not have 8-byte alignments, etc), so we have a separate function to
1882 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1884 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1886 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1889 pr_err("(pid %d) start dumping LEB %d\n", current
->pid
, lnum
);
1890 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1892 ubifs_err("cannot allocate memory to dump LPT");
1896 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1901 offs
= c
->leb_size
- len
;
1902 if (!is_a_node(c
, p
, len
)) {
1905 pad_len
= get_pad_len(c
, p
, len
);
1907 pr_err("LEB %d:%d, pad %d bytes\n",
1908 lnum
, offs
, pad_len
);
1914 pr_err("LEB %d:%d, free %d bytes\n",
1919 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1920 switch (node_type
) {
1921 case UBIFS_LPT_PNODE
:
1923 node_len
= c
->pnode_sz
;
1925 pr_err("LEB %d:%d, pnode num %d\n",
1926 lnum
, offs
, node_num
);
1928 pr_err("LEB %d:%d, pnode\n", lnum
, offs
);
1931 case UBIFS_LPT_NNODE
:
1934 struct ubifs_nnode nnode
;
1936 node_len
= c
->nnode_sz
;
1938 pr_err("LEB %d:%d, nnode num %d, ",
1939 lnum
, offs
, node_num
);
1941 pr_err("LEB %d:%d, nnode, ",
1943 err
= ubifs_unpack_nnode(c
, p
, &nnode
);
1944 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1945 pr_cont("%d:%d", nnode
.nbranch
[i
].lnum
,
1946 nnode
.nbranch
[i
].offs
);
1947 if (i
!= UBIFS_LPT_FANOUT
- 1)
1953 case UBIFS_LPT_LTAB
:
1954 node_len
= c
->ltab_sz
;
1955 pr_err("LEB %d:%d, ltab\n", lnum
, offs
);
1957 case UBIFS_LPT_LSAVE
:
1958 node_len
= c
->lsave_sz
;
1959 pr_err("LEB %d:%d, lsave len\n", lnum
, offs
);
1962 ubifs_err("LPT node type %d not recognized", node_type
);
1970 pr_err("(pid %d) finish dumping LEB %d\n", current
->pid
, lnum
);
1977 * ubifs_dump_lpt_lebs - dump LPT lebs.
1978 * @c: UBIFS file-system description object
1980 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1983 void ubifs_dump_lpt_lebs(const struct ubifs_info
*c
)
1987 pr_err("(pid %d) start dumping all LPT LEBs\n", current
->pid
);
1988 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1989 dump_lpt_leb(c
, i
+ c
->lpt_first
);
1990 pr_err("(pid %d) finish dumping all LPT LEBs\n", current
->pid
);
1994 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1995 * @c: UBIFS file-system description object
1997 * This is a debugging version for 'populate_lsave()' which populates lsave
1998 * with random LEBs instead of useful LEBs, which is good for test coverage.
1999 * Returns zero if lsave has not been populated (this debugging feature is
2000 * disabled) an non-zero if lsave has been populated.
2002 static int dbg_populate_lsave(struct ubifs_info
*c
)
2004 struct ubifs_lprops
*lprops
;
2005 struct ubifs_lpt_heap
*heap
;
2008 if (!dbg_is_chk_gen(c
))
2010 if (prandom_u32() & 3)
2013 for (i
= 0; i
< c
->lsave_cnt
; i
++)
2014 c
->lsave
[i
] = c
->main_first
;
2016 list_for_each_entry(lprops
, &c
->empty_list
, list
)
2017 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2018 list_for_each_entry(lprops
, &c
->freeable_list
, list
)
2019 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2020 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
)
2021 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2023 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
2024 for (i
= 0; i
< heap
->cnt
; i
++)
2025 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2026 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
2027 for (i
= 0; i
< heap
->cnt
; i
++)
2028 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2029 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
2030 for (i
= 0; i
< heap
->cnt
; i
++)
2031 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;