2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
28 #include <linux/crc16.h>
32 * first_dirty_cnode - find first dirty cnode.
33 * @c: UBIFS file-system description object
34 * @nnode: nnode at which to start
36 * This function returns the first dirty cnode or %NULL if there is not one.
38 static struct ubifs_cnode
*first_dirty_cnode(struct ubifs_nnode
*nnode
)
44 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
45 struct ubifs_cnode
*cnode
;
47 cnode
= nnode
->nbranch
[i
].cnode
;
49 test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
50 if (cnode
->level
== 0)
52 nnode
= (struct ubifs_nnode
*)cnode
;
58 return (struct ubifs_cnode
*)nnode
;
63 * next_dirty_cnode - find next dirty cnode.
64 * @cnode: cnode from which to begin searching
66 * This function returns the next dirty cnode or %NULL if there is not one.
68 static struct ubifs_cnode
*next_dirty_cnode(struct ubifs_cnode
*cnode
)
70 struct ubifs_nnode
*nnode
;
74 nnode
= cnode
->parent
;
77 for (i
= cnode
->iip
+ 1; i
< UBIFS_LPT_FANOUT
; i
++) {
78 cnode
= nnode
->nbranch
[i
].cnode
;
79 if (cnode
&& test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
80 if (cnode
->level
== 0)
81 return cnode
; /* cnode is a pnode */
82 /* cnode is a nnode */
83 return first_dirty_cnode((struct ubifs_nnode
*)cnode
);
86 return (struct ubifs_cnode
*)nnode
;
90 * get_cnodes_to_commit - create list of dirty cnodes to commit.
91 * @c: UBIFS file-system description object
93 * This function returns the number of cnodes to commit.
95 static int get_cnodes_to_commit(struct ubifs_info
*c
)
97 struct ubifs_cnode
*cnode
, *cnext
;
103 if (!test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
106 c
->lpt_cnext
= first_dirty_cnode(c
->nroot
);
107 cnode
= c
->lpt_cnext
;
112 ubifs_assert(!test_bit(COW_ZNODE
, &cnode
->flags
));
113 __set_bit(COW_ZNODE
, &cnode
->flags
);
114 cnext
= next_dirty_cnode(cnode
);
116 cnode
->cnext
= c
->lpt_cnext
;
119 cnode
->cnext
= cnext
;
123 dbg_cmt("committing %d cnodes", cnt
);
124 dbg_lp("committing %d cnodes", cnt
);
125 ubifs_assert(cnt
== c
->dirty_nn_cnt
+ c
->dirty_pn_cnt
);
130 * upd_ltab - update LPT LEB properties.
131 * @c: UBIFS file-system description object
133 * @free: amount of free space
134 * @dirty: amount of dirty space to add
136 static void upd_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
138 dbg_lp("LEB %d free %d dirty %d to %d +%d",
139 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
140 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
141 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
142 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
143 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
147 * alloc_lpt_leb - allocate an LPT LEB that is empty.
148 * @c: UBIFS file-system description object
149 * @lnum: LEB number is passed and returned here
151 * This function finds the next empty LEB in the ltab starting from @lnum. If a
152 * an empty LEB is found it is returned in @lnum and the function returns %0.
153 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
154 * never to run out of space.
156 static int alloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
160 n
= *lnum
- c
->lpt_first
+ 1;
161 for (i
= n
; i
< c
->lpt_lebs
; i
++) {
162 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
164 if (c
->ltab
[i
].free
== c
->leb_size
) {
166 *lnum
= i
+ c
->lpt_first
;
171 for (i
= 0; i
< n
; i
++) {
172 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
174 if (c
->ltab
[i
].free
== c
->leb_size
) {
176 *lnum
= i
+ c
->lpt_first
;
184 * layout_cnodes - layout cnodes for commit.
185 * @c: UBIFS file-system description object
187 * This function returns %0 on success and a negative error code on failure.
189 static int layout_cnodes(struct ubifs_info
*c
)
191 int lnum
, offs
, len
, alen
, done_lsave
, done_ltab
, err
;
192 struct ubifs_cnode
*cnode
;
194 err
= dbg_chk_lpt_sz(c
, 0, 0);
197 cnode
= c
->lpt_cnext
;
200 lnum
= c
->nhead_lnum
;
201 offs
= c
->nhead_offs
;
202 /* Try to place lsave and ltab nicely */
203 done_lsave
= !c
->big_lpt
;
205 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
207 c
->lsave_lnum
= lnum
;
208 c
->lsave_offs
= offs
;
210 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
213 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
218 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
224 c
->dirty_nn_cnt
-= 1;
227 c
->dirty_pn_cnt
-= 1;
229 while (offs
+ len
> c
->leb_size
) {
230 alen
= ALIGN(offs
, c
->min_io_size
);
231 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
232 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
233 err
= alloc_lpt_leb(c
, &lnum
);
237 ubifs_assert(lnum
>= c
->lpt_first
&&
238 lnum
<= c
->lpt_last
);
239 /* Try to place lsave and ltab nicely */
242 c
->lsave_lnum
= lnum
;
243 c
->lsave_offs
= offs
;
245 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
253 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
259 cnode
->parent
->nbranch
[cnode
->iip
].lnum
= lnum
;
260 cnode
->parent
->nbranch
[cnode
->iip
].offs
= offs
;
266 dbg_chk_lpt_sz(c
, 1, len
);
267 cnode
= cnode
->cnext
;
268 } while (cnode
&& cnode
!= c
->lpt_cnext
);
270 /* Make sure to place LPT's save table */
272 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
273 alen
= ALIGN(offs
, c
->min_io_size
);
274 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
275 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
276 err
= alloc_lpt_leb(c
, &lnum
);
280 ubifs_assert(lnum
>= c
->lpt_first
&&
281 lnum
<= c
->lpt_last
);
284 c
->lsave_lnum
= lnum
;
285 c
->lsave_offs
= offs
;
287 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
290 /* Make sure to place LPT's own lprops table */
292 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
293 alen
= ALIGN(offs
, c
->min_io_size
);
294 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
295 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
296 err
= alloc_lpt_leb(c
, &lnum
);
300 ubifs_assert(lnum
>= c
->lpt_first
&&
301 lnum
<= c
->lpt_last
);
307 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
310 alen
= ALIGN(offs
, c
->min_io_size
);
311 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
312 dbg_chk_lpt_sz(c
, 4, alen
- offs
);
313 err
= dbg_chk_lpt_sz(c
, 3, alen
);
319 ubifs_err("LPT out of space");
320 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
321 "done_lsave %d", lnum
, offs
, len
, done_ltab
, done_lsave
);
322 dbg_dump_lpt_info(c
);
323 dbg_dump_lpt_lebs(c
);
329 * realloc_lpt_leb - allocate an LPT LEB that is empty.
330 * @c: UBIFS file-system description object
331 * @lnum: LEB number is passed and returned here
333 * This function duplicates exactly the results of the function alloc_lpt_leb.
334 * It is used during end commit to reallocate the same LEB numbers that were
335 * allocated by alloc_lpt_leb during start commit.
337 * This function finds the next LEB that was allocated by the alloc_lpt_leb
338 * function starting from @lnum. If a LEB is found it is returned in @lnum and
339 * the function returns %0. Otherwise the function returns -ENOSPC.
340 * Note however, that LPT is designed never to run out of space.
342 static int realloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
346 n
= *lnum
- c
->lpt_first
+ 1;
347 for (i
= n
; i
< c
->lpt_lebs
; i
++)
348 if (c
->ltab
[i
].cmt
) {
350 *lnum
= i
+ c
->lpt_first
;
354 for (i
= 0; i
< n
; i
++)
355 if (c
->ltab
[i
].cmt
) {
357 *lnum
= i
+ c
->lpt_first
;
364 * write_cnodes - write cnodes for commit.
365 * @c: UBIFS file-system description object
367 * This function returns %0 on success and a negative error code on failure.
369 static int write_cnodes(struct ubifs_info
*c
)
371 int lnum
, offs
, len
, from
, err
, wlen
, alen
, done_ltab
, done_lsave
;
372 struct ubifs_cnode
*cnode
;
373 void *buf
= c
->lpt_buf
;
375 cnode
= c
->lpt_cnext
;
378 lnum
= c
->nhead_lnum
;
379 offs
= c
->nhead_offs
;
381 /* Ensure empty LEB is unmapped */
383 err
= ubifs_leb_unmap(c
, lnum
);
387 /* Try to place lsave and ltab nicely */
388 done_lsave
= !c
->big_lpt
;
390 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
392 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
394 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
397 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
399 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
401 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
404 /* Loop for each cnode */
410 while (offs
+ len
> c
->leb_size
) {
413 alen
= ALIGN(wlen
, c
->min_io_size
);
414 memset(buf
+ offs
, 0xff, alen
- wlen
);
415 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
,
416 alen
, UBI_SHORTTERM
);
420 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
421 err
= realloc_lpt_leb(c
, &lnum
);
425 ubifs_assert(lnum
>= c
->lpt_first
&&
426 lnum
<= c
->lpt_last
);
427 err
= ubifs_leb_unmap(c
, lnum
);
430 /* Try to place lsave and ltab nicely */
433 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
435 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
440 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
442 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
448 ubifs_pack_nnode(c
, buf
+ offs
,
449 (struct ubifs_nnode
*)cnode
);
451 ubifs_pack_pnode(c
, buf
+ offs
,
452 (struct ubifs_pnode
*)cnode
);
454 * The reason for the barriers is the same as in case of TNC.
455 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
456 * 'dirty_cow_pnode()' are the functions for which this is
459 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
460 smp_mb__before_clear_bit();
461 clear_bit(COW_ZNODE
, &cnode
->flags
);
462 smp_mb__after_clear_bit();
464 dbg_chk_lpt_sz(c
, 1, len
);
465 cnode
= cnode
->cnext
;
466 } while (cnode
&& cnode
!= c
->lpt_cnext
);
468 /* Make sure to place LPT's save table */
470 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
472 alen
= ALIGN(wlen
, c
->min_io_size
);
473 memset(buf
+ offs
, 0xff, alen
- wlen
);
474 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
,
478 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
479 err
= realloc_lpt_leb(c
, &lnum
);
483 ubifs_assert(lnum
>= c
->lpt_first
&&
484 lnum
<= c
->lpt_last
);
485 err
= ubifs_leb_unmap(c
, lnum
);
490 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
492 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
495 /* Make sure to place LPT's own lprops table */
497 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
499 alen
= ALIGN(wlen
, c
->min_io_size
);
500 memset(buf
+ offs
, 0xff, alen
- wlen
);
501 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
,
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
);
517 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
519 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
522 /* Write remaining data in buffer */
524 alen
= ALIGN(wlen
, c
->min_io_size
);
525 memset(buf
+ offs
, 0xff, alen
- wlen
);
526 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
, UBI_SHORTTERM
);
530 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
531 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
535 c
->nhead_lnum
= lnum
;
536 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
538 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
539 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
540 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
542 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
547 ubifs_err("LPT out of space mismatch");
548 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
549 "%d, done_lsave %d", lnum
, offs
, len
, done_ltab
, done_lsave
);
550 dbg_dump_lpt_info(c
);
551 dbg_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_PTR(PTR_ERR(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 do_make_pnode_dirty(c
, pnode
);
709 pnode
= next_pnode_to_dirty(c
, pnode
);
711 return PTR_ERR(pnode
);
717 * need_write_all - determine if the LPT area is running out of free space.
718 * @c: UBIFS file-system description object
720 * This function returns %1 if the LPT area is running out of free space and %0
723 static int need_write_all(struct ubifs_info
*c
)
728 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
729 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
730 free
+= c
->leb_size
- c
->nhead_offs
;
731 else if (c
->ltab
[i
].free
== c
->leb_size
)
733 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
736 /* Less than twice the size left */
737 if (free
<= c
->lpt_sz
* 2)
743 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
744 * @c: UBIFS file-system description object
746 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
747 * free space and so may be reused as soon as the next commit is completed.
748 * This function is called during start commit to mark LPT LEBs for trivial GC.
750 static void lpt_tgc_start(struct ubifs_info
*c
)
754 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
755 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
757 if (c
->ltab
[i
].dirty
> 0 &&
758 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
760 c
->ltab
[i
].free
= c
->leb_size
;
761 c
->ltab
[i
].dirty
= 0;
762 dbg_lp("LEB %d", i
+ c
->lpt_first
);
768 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
769 * @c: UBIFS file-system description object
771 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
772 * free space and so may be reused as soon as the next commit is completed.
773 * This function is called after the commit is completed (master node has been
774 * written) and un-maps LPT LEBs that were marked for trivial GC.
776 static int lpt_tgc_end(struct ubifs_info
*c
)
780 for (i
= 0; i
< c
->lpt_lebs
; i
++)
781 if (c
->ltab
[i
].tgc
) {
782 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
786 dbg_lp("LEB %d", i
+ c
->lpt_first
);
792 * populate_lsave - fill the lsave array with important LEB numbers.
793 * @c: the UBIFS file-system description object
795 * This function is only called for the "big" model. It records a small number
796 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
797 * most important to least important): empty, freeable, freeable index, dirty
798 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
799 * their pnodes into memory. That will stop us from having to scan the LPT
800 * straight away. For the "small" model we assume that scanning the LPT is no
803 static void populate_lsave(struct ubifs_info
*c
)
805 struct ubifs_lprops
*lprops
;
806 struct ubifs_lpt_heap
*heap
;
809 ubifs_assert(c
->big_lpt
);
810 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
811 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
812 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
814 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
815 c
->lsave
[cnt
++] = lprops
->lnum
;
816 if (cnt
>= c
->lsave_cnt
)
819 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
820 c
->lsave
[cnt
++] = lprops
->lnum
;
821 if (cnt
>= c
->lsave_cnt
)
824 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
825 c
->lsave
[cnt
++] = lprops
->lnum
;
826 if (cnt
>= c
->lsave_cnt
)
829 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
830 for (i
= 0; i
< heap
->cnt
; i
++) {
831 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
832 if (cnt
>= c
->lsave_cnt
)
835 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
836 for (i
= 0; i
< heap
->cnt
; i
++) {
837 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
838 if (cnt
>= c
->lsave_cnt
)
841 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
842 for (i
= 0; i
< heap
->cnt
; i
++) {
843 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
844 if (cnt
>= c
->lsave_cnt
)
847 /* Fill it up completely */
848 while (cnt
< c
->lsave_cnt
)
849 c
->lsave
[cnt
++] = c
->main_first
;
853 * nnode_lookup - lookup a nnode in the LPT.
854 * @c: UBIFS file-system description object
857 * This function returns a pointer to the nnode on success or a negative
858 * error code on failure.
860 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
863 struct ubifs_nnode
*nnode
;
866 err
= ubifs_read_nnode(c
, NULL
, 0);
872 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
873 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
876 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
884 * make_nnode_dirty - find a nnode and, if found, make it dirty.
885 * @c: UBIFS file-system description object
886 * @node_num: nnode number of nnode to make dirty
887 * @lnum: LEB number where nnode was written
888 * @offs: offset where nnode was written
890 * This function is used by LPT garbage collection. LPT garbage collection is
891 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
892 * simply involves marking all the nodes in the LEB being garbage-collected as
893 * dirty. The dirty nodes are written next commit, after which the LEB is free
896 * This function returns %0 on success and a negative error code on failure.
898 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
901 struct ubifs_nnode
*nnode
;
903 nnode
= nnode_lookup(c
, node_num
);
905 return PTR_ERR(nnode
);
907 struct ubifs_nbranch
*branch
;
909 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
910 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
911 return 0; /* nnode is obsolete */
912 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
913 return 0; /* nnode is obsolete */
914 /* Assumes cnext list is empty i.e. not called during commit */
915 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
916 c
->dirty_nn_cnt
+= 1;
917 ubifs_add_nnode_dirt(c
, nnode
);
918 /* Mark parent and ancestors dirty too */
919 nnode
= nnode
->parent
;
921 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
922 c
->dirty_nn_cnt
+= 1;
923 ubifs_add_nnode_dirt(c
, nnode
);
924 nnode
= nnode
->parent
;
933 * make_pnode_dirty - find a pnode and, if found, make it dirty.
934 * @c: UBIFS file-system description object
935 * @node_num: pnode number of pnode to make dirty
936 * @lnum: LEB number where pnode was written
937 * @offs: offset where pnode was written
939 * This function is used by LPT garbage collection. LPT garbage collection is
940 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
941 * simply involves marking all the nodes in the LEB being garbage-collected as
942 * dirty. The dirty nodes are written next commit, after which the LEB is free
945 * This function returns %0 on success and a negative error code on failure.
947 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
950 struct ubifs_pnode
*pnode
;
951 struct ubifs_nbranch
*branch
;
953 pnode
= pnode_lookup(c
, node_num
);
955 return PTR_ERR(pnode
);
956 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
957 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
959 do_make_pnode_dirty(c
, pnode
);
964 * make_ltab_dirty - make ltab node dirty.
965 * @c: UBIFS file-system description object
966 * @lnum: LEB number where ltab was written
967 * @offs: offset where ltab was written
969 * This function is used by LPT garbage collection. LPT garbage collection is
970 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
971 * simply involves marking all the nodes in the LEB being garbage-collected as
972 * dirty. The dirty nodes are written next commit, after which the LEB is free
975 * This function returns %0 on success and a negative error code on failure.
977 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
979 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
980 return 0; /* This ltab node is obsolete */
981 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
982 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
983 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
989 * make_lsave_dirty - make lsave node dirty.
990 * @c: UBIFS file-system description object
991 * @lnum: LEB number where lsave was written
992 * @offs: offset where lsave was written
994 * This function is used by LPT garbage collection. LPT garbage collection is
995 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
996 * simply involves marking all the nodes in the LEB being garbage-collected as
997 * dirty. The dirty nodes are written next commit, after which the LEB is free
1000 * This function returns %0 on success and a negative error code on failure.
1002 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1004 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1005 return 0; /* This lsave node is obsolete */
1006 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1007 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1008 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1014 * make_node_dirty - make node dirty.
1015 * @c: UBIFS file-system description object
1016 * @node_type: LPT node type
1017 * @node_num: node number
1018 * @lnum: LEB number where node was written
1019 * @offs: offset where node was written
1021 * This function is used by LPT garbage collection. LPT garbage collection is
1022 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1023 * simply involves marking all the nodes in the LEB being garbage-collected as
1024 * dirty. The dirty nodes are written next commit, after which the LEB is free
1027 * This function returns %0 on success and a negative error code on failure.
1029 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1032 switch (node_type
) {
1033 case UBIFS_LPT_NNODE
:
1034 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1035 case UBIFS_LPT_PNODE
:
1036 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1037 case UBIFS_LPT_LTAB
:
1038 return make_ltab_dirty(c
, lnum
, offs
);
1039 case UBIFS_LPT_LSAVE
:
1040 return make_lsave_dirty(c
, lnum
, offs
);
1046 * get_lpt_node_len - return the length of a node based on its type.
1047 * @c: UBIFS file-system description object
1048 * @node_type: LPT node type
1050 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1052 switch (node_type
) {
1053 case UBIFS_LPT_NNODE
:
1055 case UBIFS_LPT_PNODE
:
1057 case UBIFS_LPT_LTAB
:
1059 case UBIFS_LPT_LSAVE
:
1066 * get_pad_len - return the length of padding in a buffer.
1067 * @c: UBIFS file-system description object
1069 * @len: length of buffer
1071 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1075 if (c
->min_io_size
== 1)
1077 offs
= c
->leb_size
- len
;
1078 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1083 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1084 * @c: UBIFS file-system description object
1086 * @node_num: node number is returned here
1088 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1091 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1092 int pos
= 0, node_type
;
1094 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1095 *node_num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1100 * is_a_node - determine if a buffer contains a node.
1101 * @c: UBIFS file-system description object
1103 * @len: length of buffer
1105 * This function returns %1 if the buffer contains a node or %0 if it does not.
1107 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1109 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1110 int pos
= 0, node_type
, node_len
;
1111 uint16_t crc
, calc_crc
;
1113 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1115 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1116 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1118 node_len
= get_lpt_node_len(c
, node_type
);
1119 if (!node_len
|| node_len
> len
)
1123 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1124 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1125 node_len
- UBIFS_LPT_CRC_BYTES
);
1126 if (crc
!= calc_crc
)
1132 * lpt_gc_lnum - garbage collect a LPT LEB.
1133 * @c: UBIFS file-system description object
1134 * @lnum: LEB number to garbage collect
1136 * LPT garbage collection is used only for the "big" LPT model
1137 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1138 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1139 * next commit, after which the LEB is free to be reused.
1141 * This function returns %0 on success and a negative error code on failure.
1143 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1145 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1146 void *buf
= c
->lpt_buf
;
1148 dbg_lp("LEB %d", lnum
);
1149 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1151 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1155 if (!is_a_node(c
, buf
, len
)) {
1158 pad_len
= get_pad_len(c
, buf
, len
);
1166 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1167 node_len
= get_lpt_node_len(c
, node_type
);
1168 offs
= c
->leb_size
- len
;
1169 ubifs_assert(node_len
!= 0);
1170 mutex_lock(&c
->lp_mutex
);
1171 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1172 mutex_unlock(&c
->lp_mutex
);
1182 * lpt_gc - LPT garbage collection.
1183 * @c: UBIFS file-system description object
1185 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1186 * Returns %0 on success and a negative error code on failure.
1188 static int lpt_gc(struct ubifs_info
*c
)
1190 int i
, lnum
= -1, dirty
= 0;
1192 mutex_lock(&c
->lp_mutex
);
1193 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1194 ubifs_assert(!c
->ltab
[i
].tgc
);
1195 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1196 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1198 if (c
->ltab
[i
].dirty
> dirty
) {
1199 dirty
= c
->ltab
[i
].dirty
;
1200 lnum
= i
+ c
->lpt_first
;
1203 mutex_unlock(&c
->lp_mutex
);
1206 return lpt_gc_lnum(c
, lnum
);
1210 * ubifs_lpt_start_commit - UBIFS commit starts.
1211 * @c: the UBIFS file-system description object
1213 * This function has to be called when UBIFS starts the commit operation.
1214 * This function "freezes" all currently dirty LEB properties and does not
1215 * change them anymore. Further changes are saved and tracked separately
1216 * because they are not part of this commit. This function returns zero in case
1217 * of success and a negative error code in case of failure.
1219 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1225 mutex_lock(&c
->lp_mutex
);
1226 err
= dbg_chk_lpt_free_spc(c
);
1229 err
= dbg_check_ltab(c
);
1233 if (c
->check_lpt_free
) {
1235 * We ensure there is enough free space in
1236 * ubifs_lpt_post_commit() by marking nodes dirty. That
1237 * information is lost when we unmount, so we also need
1238 * to check free space once after mounting also.
1240 c
->check_lpt_free
= 0;
1241 while (need_write_all(c
)) {
1242 mutex_unlock(&c
->lp_mutex
);
1246 mutex_lock(&c
->lp_mutex
);
1252 if (!c
->dirty_pn_cnt
) {
1253 dbg_cmt("no cnodes to commit");
1258 if (!c
->big_lpt
&& need_write_all(c
)) {
1259 /* If needed, write everything */
1260 err
= make_tree_dirty(c
);
1269 cnt
= get_cnodes_to_commit(c
);
1270 ubifs_assert(cnt
!= 0);
1272 err
= layout_cnodes(c
);
1276 /* Copy the LPT's own lprops for end commit to write */
1277 memcpy(c
->ltab_cmt
, c
->ltab
,
1278 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1279 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1282 mutex_unlock(&c
->lp_mutex
);
1287 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1288 * @c: UBIFS file-system description object
1290 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1292 struct ubifs_cnode
*cnode
, *cnext
;
1294 cnext
= c
->lpt_cnext
;
1299 cnext
= cnode
->cnext
;
1300 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1303 cnode
->cnext
= NULL
;
1304 } while (cnext
!= c
->lpt_cnext
);
1305 c
->lpt_cnext
= NULL
;
1309 * ubifs_lpt_end_commit - finish the commit operation.
1310 * @c: the UBIFS file-system description object
1312 * This function has to be called when the commit operation finishes. It
1313 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1314 * the media. Returns zero in case of success and a negative error code in case
1317 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1326 err
= write_cnodes(c
);
1330 mutex_lock(&c
->lp_mutex
);
1331 free_obsolete_cnodes(c
);
1332 mutex_unlock(&c
->lp_mutex
);
1338 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1339 * @c: UBIFS file-system description object
1341 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1342 * commit for the "big" LPT model.
1344 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1348 mutex_lock(&c
->lp_mutex
);
1349 err
= lpt_tgc_end(c
);
1353 while (need_write_all(c
)) {
1354 mutex_unlock(&c
->lp_mutex
);
1358 mutex_lock(&c
->lp_mutex
);
1361 mutex_unlock(&c
->lp_mutex
);
1366 * first_nnode - find the first nnode in memory.
1367 * @c: UBIFS file-system description object
1368 * @hght: height of tree where nnode found is returned here
1370 * This function returns a pointer to the nnode found or %NULL if no nnode is
1371 * found. This function is a helper to 'ubifs_lpt_free()'.
1373 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1375 struct ubifs_nnode
*nnode
;
1382 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1384 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1385 if (nnode
->nbranch
[i
].nnode
) {
1387 nnode
= nnode
->nbranch
[i
].nnode
;
1399 * next_nnode - find the next nnode in memory.
1400 * @c: UBIFS file-system description object
1401 * @nnode: nnode from which to start.
1402 * @hght: height of tree where nnode is, is passed and returned here
1404 * This function returns a pointer to the nnode found or %NULL if no nnode is
1405 * found. This function is a helper to 'ubifs_lpt_free()'.
1407 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1408 struct ubifs_nnode
*nnode
, int *hght
)
1410 struct ubifs_nnode
*parent
;
1411 int iip
, h
, i
, found
;
1413 parent
= nnode
->parent
;
1416 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1420 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1421 nnode
= parent
->nbranch
[iip
].nnode
;
1429 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1431 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1432 if (nnode
->nbranch
[i
].nnode
) {
1434 nnode
= nnode
->nbranch
[i
].nnode
;
1446 * ubifs_lpt_free - free resources owned by the LPT.
1447 * @c: UBIFS file-system description object
1448 * @wr_only: free only resources used for writing
1450 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1452 struct ubifs_nnode
*nnode
;
1455 /* Free write-only things first */
1457 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1469 /* Now free the rest */
1471 nnode
= first_nnode(c
, &hght
);
1473 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1474 kfree(nnode
->nbranch
[i
].nnode
);
1475 nnode
= next_nnode(c
, nnode
, &hght
);
1477 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1478 kfree(c
->lpt_heap
[i
].arr
);
1479 kfree(c
->dirty_idx
.arr
);
1482 kfree(c
->lpt_nod_buf
);
1485 #ifdef CONFIG_UBIFS_FS_DEBUG
1488 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1490 * @len: buffer length
1492 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1496 for (i
= 0; i
< len
; i
++)
1503 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1504 * @c: the UBIFS file-system description object
1505 * @lnum: LEB number where nnode was written
1506 * @offs: offset where nnode was written
1508 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1510 struct ubifs_nnode
*nnode
;
1513 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1514 nnode
= first_nnode(c
, &hght
);
1515 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1516 struct ubifs_nbranch
*branch
;
1519 if (nnode
->parent
) {
1520 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1521 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1523 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1527 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1529 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1538 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1539 * @c: the UBIFS file-system description object
1540 * @lnum: LEB number where pnode was written
1541 * @offs: offset where pnode was written
1543 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1547 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1548 for (i
= 0; i
< cnt
; i
++) {
1549 struct ubifs_pnode
*pnode
;
1550 struct ubifs_nbranch
*branch
;
1553 pnode
= pnode_lookup(c
, i
);
1555 return PTR_ERR(pnode
);
1556 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1557 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1559 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1567 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1568 * @c: the UBIFS file-system description object
1569 * @lnum: LEB number where ltab node was written
1570 * @offs: offset where ltab node was written
1572 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1574 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1576 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1580 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1581 * @c: the UBIFS file-system description object
1582 * @lnum: LEB number where lsave node was written
1583 * @offs: offset where lsave node was written
1585 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1587 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1589 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1593 * dbg_is_node_dirty - determine if a node is dirty.
1594 * @c: the UBIFS file-system description object
1595 * @node_type: node type
1596 * @lnum: LEB number where node was written
1597 * @offs: offset where node was written
1599 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1602 switch (node_type
) {
1603 case UBIFS_LPT_NNODE
:
1604 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1605 case UBIFS_LPT_PNODE
:
1606 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1607 case UBIFS_LPT_LTAB
:
1608 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1609 case UBIFS_LPT_LSAVE
:
1610 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1616 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1617 * @c: the UBIFS file-system description object
1618 * @lnum: LEB number where node was written
1619 * @offs: offset where node was written
1621 * This function returns %0 on success and a negative error code on failure.
1623 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1625 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1627 void *buf
= c
->dbg
->buf
;
1629 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1632 dbg_lp("LEB %d", lnum
);
1633 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1635 dbg_msg("ubi_read failed, LEB %d, error %d", lnum
, err
);
1639 if (!is_a_node(c
, buf
, len
)) {
1642 pad_len
= get_pad_len(c
, buf
, len
);
1649 if (!dbg_is_all_ff(buf
, len
)) {
1650 dbg_msg("invalid empty space in LEB %d at %d",
1651 lnum
, c
->leb_size
- len
);
1654 i
= lnum
- c
->lpt_first
;
1655 if (len
!= c
->ltab
[i
].free
) {
1656 dbg_msg("invalid free space in LEB %d "
1657 "(free %d, expected %d)",
1658 lnum
, len
, c
->ltab
[i
].free
);
1661 if (dirty
!= c
->ltab
[i
].dirty
) {
1662 dbg_msg("invalid dirty space in LEB %d "
1663 "(dirty %d, expected %d)",
1664 lnum
, dirty
, c
->ltab
[i
].dirty
);
1669 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1670 node_len
= get_lpt_node_len(c
, node_type
);
1671 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1680 * dbg_check_ltab - check the free and dirty space in the ltab.
1681 * @c: the UBIFS file-system description object
1683 * This function returns %0 on success and a negative error code on failure.
1685 int dbg_check_ltab(struct ubifs_info
*c
)
1687 int lnum
, err
, i
, cnt
;
1689 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1692 /* Bring the entire tree into memory */
1693 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1694 for (i
= 0; i
< cnt
; i
++) {
1695 struct ubifs_pnode
*pnode
;
1697 pnode
= pnode_lookup(c
, i
);
1699 return PTR_ERR(pnode
);
1704 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1708 /* Check each LEB */
1709 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1710 err
= dbg_check_ltab_lnum(c
, lnum
);
1712 dbg_err("failed at LEB %d", lnum
);
1717 dbg_lp("succeeded");
1722 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1723 * @c: the UBIFS file-system description object
1725 * This function returns %0 on success and a negative error code on failure.
1727 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1732 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1735 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1736 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1738 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1739 free
+= c
->leb_size
- c
->nhead_offs
;
1740 else if (c
->ltab
[i
].free
== c
->leb_size
)
1741 free
+= c
->leb_size
;
1743 if (free
< c
->lpt_sz
) {
1744 dbg_err("LPT space error: free %lld lpt_sz %lld",
1746 dbg_dump_lpt_info(c
);
1747 dbg_dump_lpt_lebs(c
);
1755 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1756 * @c: the UBIFS file-system description object
1757 * @action: what to do
1758 * @len: length written
1760 * This function returns %0 on success and a negative error code on failure.
1761 * The @action argument may be one of:
1762 * o %0 - LPT debugging checking starts, initialize debugging variables;
1763 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1764 * o %2 - switched to a different LEB and wasted @len bytes;
1765 * o %3 - check that we've written the right number of bytes.
1766 * o %4 - wasted @len bytes;
1768 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1770 struct ubifs_debug_info
*d
= c
->dbg
;
1771 long long chk_lpt_sz
, lpt_sz
;
1774 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1781 d
->chk_lpt_lebs
= 0;
1782 d
->chk_lpt_wastage
= 0;
1783 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1784 dbg_err("dirty pnodes %d exceed max %d",
1785 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1788 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1789 dbg_err("dirty nnodes %d exceed max %d",
1790 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1795 d
->chk_lpt_sz
+= len
;
1798 d
->chk_lpt_sz
+= len
;
1799 d
->chk_lpt_wastage
+= len
;
1800 d
->chk_lpt_lebs
+= 1;
1803 chk_lpt_sz
= c
->leb_size
;
1804 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1805 chk_lpt_sz
+= len
- c
->nhead_offs
;
1806 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1807 dbg_err("LPT wrote %lld but space used was %lld",
1808 d
->chk_lpt_sz
, chk_lpt_sz
);
1811 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1812 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1813 d
->chk_lpt_sz
, c
->lpt_sz
);
1816 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1817 dbg_err("LPT layout size %lld but wrote %lld",
1818 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1821 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1822 dbg_err("LPT new nhead offs: expected %d was %d",
1823 d
->new_nhead_offs
, len
);
1826 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1827 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1828 lpt_sz
+= c
->ltab_sz
;
1830 lpt_sz
+= c
->lsave_sz
;
1831 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1832 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1833 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1837 dbg_dump_lpt_info(c
);
1838 dbg_dump_lpt_lebs(c
);
1841 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1843 d
->chk_lpt_wastage
= 0;
1844 d
->chk_lpt_lebs
= 0;
1845 d
->new_nhead_offs
= len
;
1848 d
->chk_lpt_sz
+= len
;
1849 d
->chk_lpt_wastage
+= len
;
1857 * dbg_dump_lpt_leb - dump an LPT LEB.
1858 * @c: UBIFS file-system description object
1859 * @lnum: LEB number to dump
1861 * This function dumps an LEB from LPT area. Nodes in this area are very
1862 * different to nodes in the main area (e.g., they do not have common headers,
1863 * they do not have 8-byte alignments, etc), so we have a separate function to
1864 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1866 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1868 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1869 void *buf
= c
->dbg
->buf
;
1871 printk(KERN_DEBUG
"(pid %d) start dumping LEB %d\n",
1872 current
->pid
, lnum
);
1873 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1875 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1879 offs
= c
->leb_size
- len
;
1880 if (!is_a_node(c
, buf
, len
)) {
1883 pad_len
= get_pad_len(c
, buf
, len
);
1885 printk(KERN_DEBUG
"LEB %d:%d, pad %d bytes\n",
1886 lnum
, offs
, pad_len
);
1892 printk(KERN_DEBUG
"LEB %d:%d, free %d bytes\n",
1897 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1898 switch (node_type
) {
1899 case UBIFS_LPT_PNODE
:
1901 node_len
= c
->pnode_sz
;
1903 printk(KERN_DEBUG
"LEB %d:%d, pnode num %d\n",
1904 lnum
, offs
, node_num
);
1906 printk(KERN_DEBUG
"LEB %d:%d, pnode\n",
1910 case UBIFS_LPT_NNODE
:
1913 struct ubifs_nnode nnode
;
1915 node_len
= c
->nnode_sz
;
1917 printk(KERN_DEBUG
"LEB %d:%d, nnode num %d, ",
1918 lnum
, offs
, node_num
);
1920 printk(KERN_DEBUG
"LEB %d:%d, nnode, ",
1922 err
= ubifs_unpack_nnode(c
, buf
, &nnode
);
1923 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1924 printk(KERN_CONT
"%d:%d", nnode
.nbranch
[i
].lnum
,
1925 nnode
.nbranch
[i
].offs
);
1926 if (i
!= UBIFS_LPT_FANOUT
- 1)
1927 printk(KERN_CONT
", ");
1929 printk(KERN_CONT
"\n");
1932 case UBIFS_LPT_LTAB
:
1933 node_len
= c
->ltab_sz
;
1934 printk(KERN_DEBUG
"LEB %d:%d, ltab\n",
1937 case UBIFS_LPT_LSAVE
:
1938 node_len
= c
->lsave_sz
;
1939 printk(KERN_DEBUG
"LEB %d:%d, lsave len\n", lnum
, offs
);
1942 ubifs_err("LPT node type %d not recognized", node_type
);
1950 printk(KERN_DEBUG
"(pid %d) finish dumping LEB %d\n",
1951 current
->pid
, lnum
);
1955 * dbg_dump_lpt_lebs - dump LPT lebs.
1956 * @c: UBIFS file-system description object
1958 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1961 void dbg_dump_lpt_lebs(const struct ubifs_info
*c
)
1965 printk(KERN_DEBUG
"(pid %d) start dumping all LPT LEBs\n",
1967 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1968 dump_lpt_leb(c
, i
+ c
->lpt_first
);
1969 printk(KERN_DEBUG
"(pid %d) finish dumping all LPT LEBs\n",
1973 #endif /* CONFIG_UBIFS_FS_DEBUG */