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, alen
- 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, alen
- 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, alen
- 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
);
419 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
421 dbg_chk_lpt_sz(c
, 2, 0);
422 err
= realloc_lpt_leb(c
, &lnum
);
427 ubifs_assert(lnum
>= c
->lpt_first
&&
428 lnum
<= c
->lpt_last
);
429 err
= ubifs_leb_unmap(c
, lnum
);
432 /* Try to place lsave and ltab nicely */
435 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
437 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
442 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
444 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
450 ubifs_pack_nnode(c
, buf
+ offs
,
451 (struct ubifs_nnode
*)cnode
);
453 ubifs_pack_pnode(c
, buf
+ offs
,
454 (struct ubifs_pnode
*)cnode
);
456 * The reason for the barriers is the same as in case of TNC.
457 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
458 * 'dirty_cow_pnode()' are the functions for which this is
461 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
462 smp_mb__before_clear_bit();
463 clear_bit(COW_ZNODE
, &cnode
->flags
);
464 smp_mb__after_clear_bit();
466 dbg_chk_lpt_sz(c
, 1, len
);
467 cnode
= cnode
->cnext
;
468 } while (cnode
&& cnode
!= c
->lpt_cnext
);
470 /* Make sure to place LPT's save table */
472 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
474 alen
= ALIGN(wlen
, c
->min_io_size
);
475 memset(buf
+ offs
, 0xff, alen
- wlen
);
476 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
,
480 dbg_chk_lpt_sz(c
, 2, alen
- wlen
);
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
,
507 dbg_chk_lpt_sz(c
, 2, alen
- wlen
);
508 err
= realloc_lpt_leb(c
, &lnum
);
512 ubifs_assert(lnum
>= c
->lpt_first
&&
513 lnum
<= c
->lpt_last
);
514 err
= ubifs_leb_unmap(c
, lnum
);
519 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
521 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
524 /* Write remaining data in buffer */
526 alen
= ALIGN(wlen
, c
->min_io_size
);
527 memset(buf
+ offs
, 0xff, alen
- wlen
);
528 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
, UBI_SHORTTERM
);
532 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
533 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
537 c
->nhead_lnum
= lnum
;
538 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
540 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
541 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
542 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
544 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
549 ubifs_err("LPT out of space mismatch");
550 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
551 "%d, done_lsave %d", lnum
, offs
, len
, done_ltab
, done_lsave
);
552 dbg_dump_lpt_info(c
);
553 dbg_dump_lpt_lebs(c
);
559 * next_pnode_to_dirty - find next pnode to dirty.
560 * @c: UBIFS file-system description object
563 * This function returns the next pnode to dirty or %NULL if there are no more
564 * pnodes. Note that pnodes that have never been written (lnum == 0) are
567 static struct ubifs_pnode
*next_pnode_to_dirty(struct ubifs_info
*c
,
568 struct ubifs_pnode
*pnode
)
570 struct ubifs_nnode
*nnode
;
573 /* Try to go right */
574 nnode
= pnode
->parent
;
575 for (iip
= pnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
576 if (nnode
->nbranch
[iip
].lnum
)
577 return ubifs_get_pnode(c
, nnode
, iip
);
580 /* Go up while can't go right */
582 iip
= nnode
->iip
+ 1;
583 nnode
= nnode
->parent
;
586 for (; iip
< UBIFS_LPT_FANOUT
; iip
++) {
587 if (nnode
->nbranch
[iip
].lnum
)
590 } while (iip
>= UBIFS_LPT_FANOUT
);
593 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
595 return (void *)nnode
;
597 /* Go down to level 1 */
598 while (nnode
->level
> 1) {
599 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++) {
600 if (nnode
->nbranch
[iip
].lnum
)
603 if (iip
>= UBIFS_LPT_FANOUT
) {
605 * Should not happen, but we need to keep going
610 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
612 return (void *)nnode
;
615 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++)
616 if (nnode
->nbranch
[iip
].lnum
)
618 if (iip
>= UBIFS_LPT_FANOUT
)
619 /* Should not happen, but we need to keep going if it does */
621 return ubifs_get_pnode(c
, nnode
, iip
);
625 * pnode_lookup - lookup a pnode in the LPT.
626 * @c: UBIFS file-system description object
627 * @i: pnode number (0 to main_lebs - 1)
629 * This function returns a pointer to the pnode on success or a negative
630 * error code on failure.
632 static struct ubifs_pnode
*pnode_lookup(struct ubifs_info
*c
, int i
)
634 int err
, h
, iip
, shft
;
635 struct ubifs_nnode
*nnode
;
638 err
= ubifs_read_nnode(c
, NULL
, 0);
642 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
644 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
645 for (h
= 1; h
< c
->lpt_hght
; h
++) {
646 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
647 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
648 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
650 return ERR_PTR(PTR_ERR(nnode
));
652 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
653 return ubifs_get_pnode(c
, nnode
, iip
);
657 * add_pnode_dirt - add dirty space to LPT LEB properties.
658 * @c: UBIFS file-system description object
659 * @pnode: pnode for which to add dirt
661 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
663 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
668 * do_make_pnode_dirty - mark a pnode dirty.
669 * @c: UBIFS file-system description object
670 * @pnode: pnode to mark dirty
672 static void do_make_pnode_dirty(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
674 /* Assumes cnext list is empty i.e. not called during commit */
675 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
676 struct ubifs_nnode
*nnode
;
678 c
->dirty_pn_cnt
+= 1;
679 add_pnode_dirt(c
, pnode
);
680 /* Mark parent and ancestors dirty too */
681 nnode
= pnode
->parent
;
683 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
684 c
->dirty_nn_cnt
+= 1;
685 ubifs_add_nnode_dirt(c
, nnode
);
686 nnode
= nnode
->parent
;
694 * make_tree_dirty - mark the entire LEB properties tree dirty.
695 * @c: UBIFS file-system description object
697 * This function is used by the "small" LPT model to cause the entire LEB
698 * properties tree to be written. The "small" LPT model does not use LPT
699 * garbage collection because it is more efficient to write the entire tree
700 * (because it is small).
702 * This function returns %0 on success and a negative error code on failure.
704 static int make_tree_dirty(struct ubifs_info
*c
)
706 struct ubifs_pnode
*pnode
;
708 pnode
= pnode_lookup(c
, 0);
710 do_make_pnode_dirty(c
, pnode
);
711 pnode
= next_pnode_to_dirty(c
, pnode
);
713 return PTR_ERR(pnode
);
719 * need_write_all - determine if the LPT area is running out of free space.
720 * @c: UBIFS file-system description object
722 * This function returns %1 if the LPT area is running out of free space and %0
725 static int need_write_all(struct ubifs_info
*c
)
730 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
731 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
732 free
+= c
->leb_size
- c
->nhead_offs
;
733 else if (c
->ltab
[i
].free
== c
->leb_size
)
735 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
738 /* Less than twice the size left */
739 if (free
<= c
->lpt_sz
* 2)
745 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
746 * @c: UBIFS file-system description object
748 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
749 * free space and so may be reused as soon as the next commit is completed.
750 * This function is called during start commit to mark LPT LEBs for trivial GC.
752 static void lpt_tgc_start(struct ubifs_info
*c
)
756 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
757 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
759 if (c
->ltab
[i
].dirty
> 0 &&
760 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
762 c
->ltab
[i
].free
= c
->leb_size
;
763 c
->ltab
[i
].dirty
= 0;
764 dbg_lp("LEB %d", i
+ c
->lpt_first
);
770 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
771 * @c: UBIFS file-system description object
773 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
774 * free space and so may be reused as soon as the next commit is completed.
775 * This function is called after the commit is completed (master node has been
776 * written) and un-maps LPT LEBs that were marked for trivial GC.
778 static int lpt_tgc_end(struct ubifs_info
*c
)
782 for (i
= 0; i
< c
->lpt_lebs
; i
++)
783 if (c
->ltab
[i
].tgc
) {
784 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
788 dbg_lp("LEB %d", i
+ c
->lpt_first
);
794 * populate_lsave - fill the lsave array with important LEB numbers.
795 * @c: the UBIFS file-system description object
797 * This function is only called for the "big" model. It records a small number
798 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
799 * most important to least important): empty, freeable, freeable index, dirty
800 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
801 * their pnodes into memory. That will stop us from having to scan the LPT
802 * straight away. For the "small" model we assume that scanning the LPT is no
805 static void populate_lsave(struct ubifs_info
*c
)
807 struct ubifs_lprops
*lprops
;
808 struct ubifs_lpt_heap
*heap
;
811 ubifs_assert(c
->big_lpt
);
812 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
813 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
814 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
816 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
817 c
->lsave
[cnt
++] = lprops
->lnum
;
818 if (cnt
>= c
->lsave_cnt
)
821 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
822 c
->lsave
[cnt
++] = lprops
->lnum
;
823 if (cnt
>= c
->lsave_cnt
)
826 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
827 c
->lsave
[cnt
++] = lprops
->lnum
;
828 if (cnt
>= c
->lsave_cnt
)
831 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
832 for (i
= 0; i
< heap
->cnt
; i
++) {
833 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
834 if (cnt
>= c
->lsave_cnt
)
837 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
838 for (i
= 0; i
< heap
->cnt
; i
++) {
839 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
840 if (cnt
>= c
->lsave_cnt
)
843 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
844 for (i
= 0; i
< heap
->cnt
; i
++) {
845 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
846 if (cnt
>= c
->lsave_cnt
)
849 /* Fill it up completely */
850 while (cnt
< c
->lsave_cnt
)
851 c
->lsave
[cnt
++] = c
->main_first
;
855 * nnode_lookup - lookup a nnode in the LPT.
856 * @c: UBIFS file-system description object
859 * This function returns a pointer to the nnode on success or a negative
860 * error code on failure.
862 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
865 struct ubifs_nnode
*nnode
;
868 err
= ubifs_read_nnode(c
, NULL
, 0);
874 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
875 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
878 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
886 * make_nnode_dirty - find a nnode and, if found, make it dirty.
887 * @c: UBIFS file-system description object
888 * @node_num: nnode number of nnode to make dirty
889 * @lnum: LEB number where nnode was written
890 * @offs: offset where nnode was written
892 * This function is used by LPT garbage collection. LPT garbage collection is
893 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
894 * simply involves marking all the nodes in the LEB being garbage-collected as
895 * dirty. The dirty nodes are written next commit, after which the LEB is free
898 * This function returns %0 on success and a negative error code on failure.
900 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
903 struct ubifs_nnode
*nnode
;
905 nnode
= nnode_lookup(c
, node_num
);
907 return PTR_ERR(nnode
);
909 struct ubifs_nbranch
*branch
;
911 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
912 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
913 return 0; /* nnode is obsolete */
914 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
915 return 0; /* nnode is obsolete */
916 /* Assumes cnext list is empty i.e. not called during commit */
917 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
918 c
->dirty_nn_cnt
+= 1;
919 ubifs_add_nnode_dirt(c
, nnode
);
920 /* Mark parent and ancestors dirty too */
921 nnode
= nnode
->parent
;
923 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
924 c
->dirty_nn_cnt
+= 1;
925 ubifs_add_nnode_dirt(c
, nnode
);
926 nnode
= nnode
->parent
;
935 * make_pnode_dirty - find a pnode and, if found, make it dirty.
936 * @c: UBIFS file-system description object
937 * @node_num: pnode number of pnode to make dirty
938 * @lnum: LEB number where pnode was written
939 * @offs: offset where pnode was written
941 * This function is used by LPT garbage collection. LPT garbage collection is
942 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
943 * simply involves marking all the nodes in the LEB being garbage-collected as
944 * dirty. The dirty nodes are written next commit, after which the LEB is free
947 * This function returns %0 on success and a negative error code on failure.
949 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
952 struct ubifs_pnode
*pnode
;
953 struct ubifs_nbranch
*branch
;
955 pnode
= pnode_lookup(c
, node_num
);
957 return PTR_ERR(pnode
);
958 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
959 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
961 do_make_pnode_dirty(c
, pnode
);
966 * make_ltab_dirty - make ltab node dirty.
967 * @c: UBIFS file-system description object
968 * @lnum: LEB number where ltab was written
969 * @offs: offset where ltab was written
971 * This function is used by LPT garbage collection. LPT garbage collection is
972 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
973 * simply involves marking all the nodes in the LEB being garbage-collected as
974 * dirty. The dirty nodes are written next commit, after which the LEB is free
977 * This function returns %0 on success and a negative error code on failure.
979 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
981 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
982 return 0; /* This ltab node is obsolete */
983 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
984 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
985 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
991 * make_lsave_dirty - make lsave node dirty.
992 * @c: UBIFS file-system description object
993 * @lnum: LEB number where lsave was written
994 * @offs: offset where lsave was written
996 * This function is used by LPT garbage collection. LPT garbage collection is
997 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
998 * simply involves marking all the nodes in the LEB being garbage-collected as
999 * dirty. The dirty nodes are written next commit, after which the LEB is free
1002 * This function returns %0 on success and a negative error code on failure.
1004 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1006 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1007 return 0; /* This lsave node is obsolete */
1008 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1009 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1010 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1016 * make_node_dirty - make node dirty.
1017 * @c: UBIFS file-system description object
1018 * @node_type: LPT node type
1019 * @node_num: node number
1020 * @lnum: LEB number where node was written
1021 * @offs: offset where node was written
1023 * This function is used by LPT garbage collection. LPT garbage collection is
1024 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1025 * simply involves marking all the nodes in the LEB being garbage-collected as
1026 * dirty. The dirty nodes are written next commit, after which the LEB is free
1029 * This function returns %0 on success and a negative error code on failure.
1031 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1034 switch (node_type
) {
1035 case UBIFS_LPT_NNODE
:
1036 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1037 case UBIFS_LPT_PNODE
:
1038 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1039 case UBIFS_LPT_LTAB
:
1040 return make_ltab_dirty(c
, lnum
, offs
);
1041 case UBIFS_LPT_LSAVE
:
1042 return make_lsave_dirty(c
, lnum
, offs
);
1048 * get_lpt_node_len - return the length of a node based on its type.
1049 * @c: UBIFS file-system description object
1050 * @node_type: LPT node type
1052 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1054 switch (node_type
) {
1055 case UBIFS_LPT_NNODE
:
1057 case UBIFS_LPT_PNODE
:
1059 case UBIFS_LPT_LTAB
:
1061 case UBIFS_LPT_LSAVE
:
1068 * get_pad_len - return the length of padding in a buffer.
1069 * @c: UBIFS file-system description object
1071 * @len: length of buffer
1073 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1077 if (c
->min_io_size
== 1)
1079 offs
= c
->leb_size
- len
;
1080 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1085 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1086 * @c: UBIFS file-system description object
1088 * @node_num: node number is returned here
1090 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1093 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1094 int pos
= 0, node_type
;
1096 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1097 *node_num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1102 * is_a_node - determine if a buffer contains a node.
1103 * @c: UBIFS file-system description object
1105 * @len: length of buffer
1107 * This function returns %1 if the buffer contains a node or %0 if it does not.
1109 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1111 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1112 int pos
= 0, node_type
, node_len
;
1113 uint16_t crc
, calc_crc
;
1115 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1117 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1118 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1120 node_len
= get_lpt_node_len(c
, node_type
);
1121 if (!node_len
|| node_len
> len
)
1125 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1126 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1127 node_len
- UBIFS_LPT_CRC_BYTES
);
1128 if (crc
!= calc_crc
)
1134 * lpt_gc_lnum - garbage collect a LPT LEB.
1135 * @c: UBIFS file-system description object
1136 * @lnum: LEB number to garbage collect
1138 * LPT garbage collection is used only for the "big" LPT model
1139 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1140 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1141 * next commit, after which the LEB is free to be reused.
1143 * This function returns %0 on success and a negative error code on failure.
1145 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1147 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1148 void *buf
= c
->lpt_buf
;
1150 dbg_lp("LEB %d", lnum
);
1151 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1153 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1157 if (!is_a_node(c
, buf
, len
)) {
1160 pad_len
= get_pad_len(c
, buf
, len
);
1168 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1169 node_len
= get_lpt_node_len(c
, node_type
);
1170 offs
= c
->leb_size
- len
;
1171 ubifs_assert(node_len
!= 0);
1172 mutex_lock(&c
->lp_mutex
);
1173 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1174 mutex_unlock(&c
->lp_mutex
);
1184 * lpt_gc - LPT garbage collection.
1185 * @c: UBIFS file-system description object
1187 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1188 * Returns %0 on success and a negative error code on failure.
1190 static int lpt_gc(struct ubifs_info
*c
)
1192 int i
, lnum
= -1, dirty
= 0;
1194 mutex_lock(&c
->lp_mutex
);
1195 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1196 ubifs_assert(!c
->ltab
[i
].tgc
);
1197 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1198 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1200 if (c
->ltab
[i
].dirty
> dirty
) {
1201 dirty
= c
->ltab
[i
].dirty
;
1202 lnum
= i
+ c
->lpt_first
;
1205 mutex_unlock(&c
->lp_mutex
);
1208 return lpt_gc_lnum(c
, lnum
);
1212 * ubifs_lpt_start_commit - UBIFS commit starts.
1213 * @c: the UBIFS file-system description object
1215 * This function has to be called when UBIFS starts the commit operation.
1216 * This function "freezes" all currently dirty LEB properties and does not
1217 * change them anymore. Further changes are saved and tracked separately
1218 * because they are not part of this commit. This function returns zero in case
1219 * of success and a negative error code in case of failure.
1221 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1227 mutex_lock(&c
->lp_mutex
);
1228 err
= dbg_chk_lpt_free_spc(c
);
1231 err
= dbg_check_ltab(c
);
1235 if (c
->check_lpt_free
) {
1237 * We ensure there is enough free space in
1238 * ubifs_lpt_post_commit() by marking nodes dirty. That
1239 * information is lost when we unmount, so we also need
1240 * to check free space once after mounting also.
1242 c
->check_lpt_free
= 0;
1243 while (need_write_all(c
)) {
1244 mutex_unlock(&c
->lp_mutex
);
1248 mutex_lock(&c
->lp_mutex
);
1254 if (!c
->dirty_pn_cnt
) {
1255 dbg_cmt("no cnodes to commit");
1260 if (!c
->big_lpt
&& need_write_all(c
)) {
1261 /* If needed, write everything */
1262 err
= make_tree_dirty(c
);
1271 cnt
= get_cnodes_to_commit(c
);
1272 ubifs_assert(cnt
!= 0);
1274 err
= layout_cnodes(c
);
1278 /* Copy the LPT's own lprops for end commit to write */
1279 memcpy(c
->ltab_cmt
, c
->ltab
,
1280 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1281 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1284 mutex_unlock(&c
->lp_mutex
);
1289 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1290 * @c: UBIFS file-system description object
1292 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1294 struct ubifs_cnode
*cnode
, *cnext
;
1296 cnext
= c
->lpt_cnext
;
1301 cnext
= cnode
->cnext
;
1302 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1305 cnode
->cnext
= NULL
;
1306 } while (cnext
!= c
->lpt_cnext
);
1307 c
->lpt_cnext
= NULL
;
1311 * ubifs_lpt_end_commit - finish the commit operation.
1312 * @c: the UBIFS file-system description object
1314 * This function has to be called when the commit operation finishes. It
1315 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1316 * the media. Returns zero in case of success and a negative error code in case
1319 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1328 err
= write_cnodes(c
);
1332 mutex_lock(&c
->lp_mutex
);
1333 free_obsolete_cnodes(c
);
1334 mutex_unlock(&c
->lp_mutex
);
1340 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1341 * @c: UBIFS file-system description object
1343 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1344 * commit for the "big" LPT model.
1346 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1350 mutex_lock(&c
->lp_mutex
);
1351 err
= lpt_tgc_end(c
);
1355 while (need_write_all(c
)) {
1356 mutex_unlock(&c
->lp_mutex
);
1360 mutex_lock(&c
->lp_mutex
);
1363 mutex_unlock(&c
->lp_mutex
);
1368 * first_nnode - find the first nnode in memory.
1369 * @c: UBIFS file-system description object
1370 * @hght: height of tree where nnode found is returned here
1372 * This function returns a pointer to the nnode found or %NULL if no nnode is
1373 * found. This function is a helper to 'ubifs_lpt_free()'.
1375 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1377 struct ubifs_nnode
*nnode
;
1384 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1386 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1387 if (nnode
->nbranch
[i
].nnode
) {
1389 nnode
= nnode
->nbranch
[i
].nnode
;
1401 * next_nnode - find the next nnode in memory.
1402 * @c: UBIFS file-system description object
1403 * @nnode: nnode from which to start.
1404 * @hght: height of tree where nnode is, is passed and returned here
1406 * This function returns a pointer to the nnode found or %NULL if no nnode is
1407 * found. This function is a helper to 'ubifs_lpt_free()'.
1409 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1410 struct ubifs_nnode
*nnode
, int *hght
)
1412 struct ubifs_nnode
*parent
;
1413 int iip
, h
, i
, found
;
1415 parent
= nnode
->parent
;
1418 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1422 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1423 nnode
= parent
->nbranch
[iip
].nnode
;
1431 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1433 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1434 if (nnode
->nbranch
[i
].nnode
) {
1436 nnode
= nnode
->nbranch
[i
].nnode
;
1448 * ubifs_lpt_free - free resources owned by the LPT.
1449 * @c: UBIFS file-system description object
1450 * @wr_only: free only resources used for writing
1452 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1454 struct ubifs_nnode
*nnode
;
1457 /* Free write-only things first */
1459 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1471 /* Now free the rest */
1473 nnode
= first_nnode(c
, &hght
);
1475 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1476 kfree(nnode
->nbranch
[i
].nnode
);
1477 nnode
= next_nnode(c
, nnode
, &hght
);
1479 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1480 kfree(c
->lpt_heap
[i
].arr
);
1481 kfree(c
->dirty_idx
.arr
);
1484 kfree(c
->lpt_nod_buf
);
1487 #ifdef CONFIG_UBIFS_FS_DEBUG
1490 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1492 * @len: buffer length
1494 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1498 for (i
= 0; i
< len
; i
++)
1505 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1506 * @c: the UBIFS file-system description object
1507 * @lnum: LEB number where nnode was written
1508 * @offs: offset where nnode was written
1510 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1512 struct ubifs_nnode
*nnode
;
1515 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1516 nnode
= first_nnode(c
, &hght
);
1517 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1518 struct ubifs_nbranch
*branch
;
1521 if (nnode
->parent
) {
1522 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1523 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1525 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1529 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1531 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1540 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1541 * @c: the UBIFS file-system description object
1542 * @lnum: LEB number where pnode was written
1543 * @offs: offset where pnode was written
1545 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1549 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1550 for (i
= 0; i
< cnt
; i
++) {
1551 struct ubifs_pnode
*pnode
;
1552 struct ubifs_nbranch
*branch
;
1555 pnode
= pnode_lookup(c
, i
);
1557 return PTR_ERR(pnode
);
1558 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1559 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1561 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1569 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1570 * @c: the UBIFS file-system description object
1571 * @lnum: LEB number where ltab node was written
1572 * @offs: offset where ltab node was written
1574 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1576 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1578 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1582 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1583 * @c: the UBIFS file-system description object
1584 * @lnum: LEB number where lsave node was written
1585 * @offs: offset where lsave node was written
1587 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1589 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1591 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1595 * dbg_is_node_dirty - determine if a node is dirty.
1596 * @c: the UBIFS file-system description object
1597 * @node_type: node type
1598 * @lnum: LEB number where node was written
1599 * @offs: offset where node was written
1601 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1604 switch (node_type
) {
1605 case UBIFS_LPT_NNODE
:
1606 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1607 case UBIFS_LPT_PNODE
:
1608 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1609 case UBIFS_LPT_LTAB
:
1610 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1611 case UBIFS_LPT_LSAVE
:
1612 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1618 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1619 * @c: the UBIFS file-system description object
1620 * @lnum: LEB number where node was written
1621 * @offs: offset where node was written
1623 * This function returns %0 on success and a negative error code on failure.
1625 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1627 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1629 void *buf
= c
->dbg
->buf
;
1631 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1634 dbg_lp("LEB %d", lnum
);
1635 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1637 dbg_msg("ubi_read failed, LEB %d, error %d", lnum
, err
);
1641 if (!is_a_node(c
, buf
, len
)) {
1644 pad_len
= get_pad_len(c
, buf
, len
);
1651 if (!dbg_is_all_ff(buf
, len
)) {
1652 dbg_msg("invalid empty space in LEB %d at %d",
1653 lnum
, c
->leb_size
- len
);
1656 i
= lnum
- c
->lpt_first
;
1657 if (len
!= c
->ltab
[i
].free
) {
1658 dbg_msg("invalid free space in LEB %d "
1659 "(free %d, expected %d)",
1660 lnum
, len
, c
->ltab
[i
].free
);
1663 if (dirty
!= c
->ltab
[i
].dirty
) {
1664 dbg_msg("invalid dirty space in LEB %d "
1665 "(dirty %d, expected %d)",
1666 lnum
, dirty
, c
->ltab
[i
].dirty
);
1671 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1672 node_len
= get_lpt_node_len(c
, node_type
);
1673 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1682 * dbg_check_ltab - check the free and dirty space in the ltab.
1683 * @c: the UBIFS file-system description object
1685 * This function returns %0 on success and a negative error code on failure.
1687 int dbg_check_ltab(struct ubifs_info
*c
)
1689 int lnum
, err
, i
, cnt
;
1691 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1694 /* Bring the entire tree into memory */
1695 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1696 for (i
= 0; i
< cnt
; i
++) {
1697 struct ubifs_pnode
*pnode
;
1699 pnode
= pnode_lookup(c
, i
);
1701 return PTR_ERR(pnode
);
1706 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1710 /* Check each LEB */
1711 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1712 err
= dbg_check_ltab_lnum(c
, lnum
);
1714 dbg_err("failed at LEB %d", lnum
);
1719 dbg_lp("succeeded");
1724 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1725 * @c: the UBIFS file-system description object
1727 * This function returns %0 on success and a negative error code on failure.
1729 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1734 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1737 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1738 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1740 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1741 free
+= c
->leb_size
- c
->nhead_offs
;
1742 else if (c
->ltab
[i
].free
== c
->leb_size
)
1743 free
+= c
->leb_size
;
1745 if (free
< c
->lpt_sz
) {
1746 dbg_err("LPT space error: free %lld lpt_sz %lld",
1748 dbg_dump_lpt_info(c
);
1749 dbg_dump_lpt_lebs(c
);
1757 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1758 * @c: the UBIFS file-system description object
1760 * @len: length written
1762 * This function returns %0 on success and a negative error code on failure.
1764 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1766 struct ubifs_debug_info
*d
= c
->dbg
;
1767 long long chk_lpt_sz
, lpt_sz
;
1770 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1777 d
->chk_lpt_lebs
= 0;
1778 d
->chk_lpt_wastage
= 0;
1779 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1780 dbg_err("dirty pnodes %d exceed max %d",
1781 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1784 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1785 dbg_err("dirty nnodes %d exceed max %d",
1786 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1791 d
->chk_lpt_sz
+= len
;
1794 d
->chk_lpt_sz
+= len
;
1795 d
->chk_lpt_wastage
+= len
;
1796 d
->chk_lpt_lebs
+= 1;
1799 chk_lpt_sz
= c
->leb_size
;
1800 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1801 chk_lpt_sz
+= len
- c
->nhead_offs
;
1802 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1803 dbg_err("LPT wrote %lld but space used was %lld",
1804 d
->chk_lpt_sz
, chk_lpt_sz
);
1807 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1808 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1809 d
->chk_lpt_sz
, c
->lpt_sz
);
1812 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1813 dbg_err("LPT layout size %lld but wrote %lld",
1814 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1817 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1818 dbg_err("LPT new nhead offs: expected %d was %d",
1819 d
->new_nhead_offs
, len
);
1822 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1823 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1824 lpt_sz
+= c
->ltab_sz
;
1826 lpt_sz
+= c
->lsave_sz
;
1827 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1828 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1829 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1833 dbg_dump_lpt_info(c
);
1834 dbg_dump_lpt_lebs(c
);
1837 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1839 d
->chk_lpt_wastage
= 0;
1840 d
->chk_lpt_lebs
= 0;
1841 d
->new_nhead_offs
= len
;
1844 d
->chk_lpt_sz
+= len
;
1845 d
->chk_lpt_wastage
+= len
;
1853 * dbg_dump_lpt_leb - dump an LPT LEB.
1854 * @c: UBIFS file-system description object
1855 * @lnum: LEB number to dump
1857 * This function dumps an LEB from LPT area. Nodes in this area are very
1858 * different to nodes in the main area (e.g., they do not have common headers,
1859 * they do not have 8-byte alignments, etc), so we have a separate function to
1860 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1862 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1864 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1865 void *buf
= c
->dbg
->buf
;
1867 printk(KERN_DEBUG
"(pid %d) start dumping LEB %d\n",
1868 current
->pid
, lnum
);
1869 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1871 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1875 offs
= c
->leb_size
- len
;
1876 if (!is_a_node(c
, buf
, len
)) {
1879 pad_len
= get_pad_len(c
, buf
, len
);
1881 printk(KERN_DEBUG
"LEB %d:%d, pad %d bytes\n",
1882 lnum
, offs
, pad_len
);
1888 printk(KERN_DEBUG
"LEB %d:%d, free %d bytes\n",
1893 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1894 switch (node_type
) {
1895 case UBIFS_LPT_PNODE
:
1897 node_len
= c
->pnode_sz
;
1899 printk(KERN_DEBUG
"LEB %d:%d, pnode num %d\n",
1900 lnum
, offs
, node_num
);
1902 printk(KERN_DEBUG
"LEB %d:%d, pnode\n",
1906 case UBIFS_LPT_NNODE
:
1909 struct ubifs_nnode nnode
;
1911 node_len
= c
->nnode_sz
;
1913 printk(KERN_DEBUG
"LEB %d:%d, nnode num %d, ",
1914 lnum
, offs
, node_num
);
1916 printk(KERN_DEBUG
"LEB %d:%d, nnode, ",
1918 err
= ubifs_unpack_nnode(c
, buf
, &nnode
);
1919 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1920 printk("%d:%d", nnode
.nbranch
[i
].lnum
,
1921 nnode
.nbranch
[i
].offs
);
1922 if (i
!= UBIFS_LPT_FANOUT
- 1)
1928 case UBIFS_LPT_LTAB
:
1929 node_len
= c
->ltab_sz
;
1930 printk(KERN_DEBUG
"LEB %d:%d, ltab\n",
1933 case UBIFS_LPT_LSAVE
:
1934 node_len
= c
->lsave_sz
;
1935 printk(KERN_DEBUG
"LEB %d:%d, lsave len\n", lnum
, offs
);
1938 ubifs_err("LPT node type %d not recognized", node_type
);
1946 printk(KERN_DEBUG
"(pid %d) finish dumping LEB %d\n",
1947 current
->pid
, lnum
);
1951 * dbg_dump_lpt_lebs - dump LPT lebs.
1952 * @c: UBIFS file-system description object
1954 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1957 void dbg_dump_lpt_lebs(const struct ubifs_info
*c
)
1961 printk(KERN_DEBUG
"(pid %d) start dumping all LPT LEBs\n",
1963 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1964 dump_lpt_leb(c
, i
+ c
->lpt_first
);
1965 printk(KERN_DEBUG
"(pid %d) finish dumping all LPT LEBs\n",
1969 #endif /* CONFIG_UBIFS_FS_DEBUG */