2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
28 #include <linux/crc16.h>
29 #include <linux/slab.h>
32 #ifdef CONFIG_UBIFS_FS_DEBUG
33 static int dbg_populate_lsave(struct ubifs_info
*c
);
35 #define dbg_populate_lsave(c) 0
39 * first_dirty_cnode - find first dirty cnode.
40 * @c: UBIFS file-system description object
41 * @nnode: nnode at which to start
43 * This function returns the first dirty cnode or %NULL if there is not one.
45 static struct ubifs_cnode
*first_dirty_cnode(struct ubifs_nnode
*nnode
)
51 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
52 struct ubifs_cnode
*cnode
;
54 cnode
= nnode
->nbranch
[i
].cnode
;
56 test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
57 if (cnode
->level
== 0)
59 nnode
= (struct ubifs_nnode
*)cnode
;
65 return (struct ubifs_cnode
*)nnode
;
70 * next_dirty_cnode - find next dirty cnode.
71 * @cnode: cnode from which to begin searching
73 * This function returns the next dirty cnode or %NULL if there is not one.
75 static struct ubifs_cnode
*next_dirty_cnode(struct ubifs_cnode
*cnode
)
77 struct ubifs_nnode
*nnode
;
81 nnode
= cnode
->parent
;
84 for (i
= cnode
->iip
+ 1; i
< UBIFS_LPT_FANOUT
; i
++) {
85 cnode
= nnode
->nbranch
[i
].cnode
;
86 if (cnode
&& test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
87 if (cnode
->level
== 0)
88 return cnode
; /* cnode is a pnode */
89 /* cnode is a nnode */
90 return first_dirty_cnode((struct ubifs_nnode
*)cnode
);
93 return (struct ubifs_cnode
*)nnode
;
97 * get_cnodes_to_commit - create list of dirty cnodes to commit.
98 * @c: UBIFS file-system description object
100 * This function returns the number of cnodes to commit.
102 static int get_cnodes_to_commit(struct ubifs_info
*c
)
104 struct ubifs_cnode
*cnode
, *cnext
;
110 if (!test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
113 c
->lpt_cnext
= first_dirty_cnode(c
->nroot
);
114 cnode
= c
->lpt_cnext
;
119 ubifs_assert(!test_bit(COW_ZNODE
, &cnode
->flags
));
120 __set_bit(COW_ZNODE
, &cnode
->flags
);
121 cnext
= next_dirty_cnode(cnode
);
123 cnode
->cnext
= c
->lpt_cnext
;
126 cnode
->cnext
= cnext
;
130 dbg_cmt("committing %d cnodes", cnt
);
131 dbg_lp("committing %d cnodes", cnt
);
132 ubifs_assert(cnt
== c
->dirty_nn_cnt
+ c
->dirty_pn_cnt
);
137 * upd_ltab - update LPT LEB properties.
138 * @c: UBIFS file-system description object
140 * @free: amount of free space
141 * @dirty: amount of dirty space to add
143 static void upd_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
145 dbg_lp("LEB %d free %d dirty %d to %d +%d",
146 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
147 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
148 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
149 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
150 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
154 * alloc_lpt_leb - allocate an LPT LEB that is empty.
155 * @c: UBIFS file-system description object
156 * @lnum: LEB number is passed and returned here
158 * This function finds the next empty LEB in the ltab starting from @lnum. If a
159 * an empty LEB is found it is returned in @lnum and the function returns %0.
160 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
161 * never to run out of space.
163 static int alloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
167 n
= *lnum
- c
->lpt_first
+ 1;
168 for (i
= n
; i
< c
->lpt_lebs
; i
++) {
169 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
171 if (c
->ltab
[i
].free
== c
->leb_size
) {
173 *lnum
= i
+ c
->lpt_first
;
178 for (i
= 0; i
< n
; i
++) {
179 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
181 if (c
->ltab
[i
].free
== c
->leb_size
) {
183 *lnum
= i
+ c
->lpt_first
;
191 * layout_cnodes - layout cnodes for commit.
192 * @c: UBIFS file-system description object
194 * This function returns %0 on success and a negative error code on failure.
196 static int layout_cnodes(struct ubifs_info
*c
)
198 int lnum
, offs
, len
, alen
, done_lsave
, done_ltab
, err
;
199 struct ubifs_cnode
*cnode
;
201 err
= dbg_chk_lpt_sz(c
, 0, 0);
204 cnode
= c
->lpt_cnext
;
207 lnum
= c
->nhead_lnum
;
208 offs
= c
->nhead_offs
;
209 /* Try to place lsave and ltab nicely */
210 done_lsave
= !c
->big_lpt
;
212 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
214 c
->lsave_lnum
= lnum
;
215 c
->lsave_offs
= offs
;
217 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
220 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
225 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
231 c
->dirty_nn_cnt
-= 1;
234 c
->dirty_pn_cnt
-= 1;
236 while (offs
+ len
> c
->leb_size
) {
237 alen
= ALIGN(offs
, c
->min_io_size
);
238 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
239 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
240 err
= alloc_lpt_leb(c
, &lnum
);
244 ubifs_assert(lnum
>= c
->lpt_first
&&
245 lnum
<= c
->lpt_last
);
246 /* Try to place lsave and ltab nicely */
249 c
->lsave_lnum
= lnum
;
250 c
->lsave_offs
= offs
;
252 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
260 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
266 cnode
->parent
->nbranch
[cnode
->iip
].lnum
= lnum
;
267 cnode
->parent
->nbranch
[cnode
->iip
].offs
= offs
;
273 dbg_chk_lpt_sz(c
, 1, len
);
274 cnode
= cnode
->cnext
;
275 } while (cnode
&& cnode
!= c
->lpt_cnext
);
277 /* Make sure to place LPT's save table */
279 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
280 alen
= ALIGN(offs
, c
->min_io_size
);
281 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
282 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
283 err
= alloc_lpt_leb(c
, &lnum
);
287 ubifs_assert(lnum
>= c
->lpt_first
&&
288 lnum
<= c
->lpt_last
);
291 c
->lsave_lnum
= lnum
;
292 c
->lsave_offs
= offs
;
294 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
297 /* Make sure to place LPT's own lprops table */
299 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
300 alen
= ALIGN(offs
, c
->min_io_size
);
301 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
302 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
303 err
= alloc_lpt_leb(c
, &lnum
);
307 ubifs_assert(lnum
>= c
->lpt_first
&&
308 lnum
<= c
->lpt_last
);
314 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
317 alen
= ALIGN(offs
, c
->min_io_size
);
318 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
319 dbg_chk_lpt_sz(c
, 4, alen
- offs
);
320 err
= dbg_chk_lpt_sz(c
, 3, alen
);
326 ubifs_err("LPT out of space");
327 dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
328 "done_lsave %d", lnum
, offs
, len
, done_ltab
, done_lsave
);
329 dbg_dump_lpt_info(c
);
330 dbg_dump_lpt_lebs(c
);
336 * realloc_lpt_leb - allocate an LPT LEB that is empty.
337 * @c: UBIFS file-system description object
338 * @lnum: LEB number is passed and returned here
340 * This function duplicates exactly the results of the function alloc_lpt_leb.
341 * It is used during end commit to reallocate the same LEB numbers that were
342 * allocated by alloc_lpt_leb during start commit.
344 * This function finds the next LEB that was allocated by the alloc_lpt_leb
345 * function starting from @lnum. If a LEB is found it is returned in @lnum and
346 * the function returns %0. Otherwise the function returns -ENOSPC.
347 * Note however, that LPT is designed never to run out of space.
349 static int realloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
353 n
= *lnum
- c
->lpt_first
+ 1;
354 for (i
= n
; i
< c
->lpt_lebs
; i
++)
355 if (c
->ltab
[i
].cmt
) {
357 *lnum
= i
+ c
->lpt_first
;
361 for (i
= 0; i
< n
; i
++)
362 if (c
->ltab
[i
].cmt
) {
364 *lnum
= i
+ c
->lpt_first
;
371 * write_cnodes - write cnodes for commit.
372 * @c: UBIFS file-system description object
374 * This function returns %0 on success and a negative error code on failure.
376 static int write_cnodes(struct ubifs_info
*c
)
378 int lnum
, offs
, len
, from
, err
, wlen
, alen
, done_ltab
, done_lsave
;
379 struct ubifs_cnode
*cnode
;
380 void *buf
= c
->lpt_buf
;
382 cnode
= c
->lpt_cnext
;
385 lnum
= c
->nhead_lnum
;
386 offs
= c
->nhead_offs
;
388 /* Ensure empty LEB is unmapped */
390 err
= ubifs_leb_unmap(c
, lnum
);
394 /* Try to place lsave and ltab nicely */
395 done_lsave
= !c
->big_lpt
;
397 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
399 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
401 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
404 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
406 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
408 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
411 /* Loop for each cnode */
417 while (offs
+ len
> c
->leb_size
) {
420 alen
= ALIGN(wlen
, c
->min_io_size
);
421 memset(buf
+ offs
, 0xff, alen
- wlen
);
422 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
,
423 alen
, UBI_SHORTTERM
);
427 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
428 err
= realloc_lpt_leb(c
, &lnum
);
432 ubifs_assert(lnum
>= c
->lpt_first
&&
433 lnum
<= c
->lpt_last
);
434 err
= ubifs_leb_unmap(c
, lnum
);
437 /* Try to place lsave and ltab nicely */
440 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
442 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
447 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
449 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
455 ubifs_pack_nnode(c
, buf
+ offs
,
456 (struct ubifs_nnode
*)cnode
);
458 ubifs_pack_pnode(c
, buf
+ offs
,
459 (struct ubifs_pnode
*)cnode
);
461 * The reason for the barriers is the same as in case of TNC.
462 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
463 * 'dirty_cow_pnode()' are the functions for which this is
466 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
467 smp_mb__before_clear_bit();
468 clear_bit(COW_ZNODE
, &cnode
->flags
);
469 smp_mb__after_clear_bit();
471 dbg_chk_lpt_sz(c
, 1, len
);
472 cnode
= cnode
->cnext
;
473 } while (cnode
&& cnode
!= c
->lpt_cnext
);
475 /* Make sure to place LPT's save table */
477 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
479 alen
= ALIGN(wlen
, c
->min_io_size
);
480 memset(buf
+ offs
, 0xff, alen
- wlen
);
481 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
,
485 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
486 err
= realloc_lpt_leb(c
, &lnum
);
490 ubifs_assert(lnum
>= c
->lpt_first
&&
491 lnum
<= c
->lpt_last
);
492 err
= ubifs_leb_unmap(c
, lnum
);
497 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
499 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
502 /* Make sure to place LPT's own lprops table */
504 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
506 alen
= ALIGN(wlen
, c
->min_io_size
);
507 memset(buf
+ offs
, 0xff, alen
- wlen
);
508 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
,
512 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
513 err
= realloc_lpt_leb(c
, &lnum
);
517 ubifs_assert(lnum
>= c
->lpt_first
&&
518 lnum
<= c
->lpt_last
);
519 err
= ubifs_leb_unmap(c
, lnum
);
524 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
526 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
529 /* Write remaining data in buffer */
531 alen
= ALIGN(wlen
, c
->min_io_size
);
532 memset(buf
+ offs
, 0xff, alen
- wlen
);
533 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
, UBI_SHORTTERM
);
537 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
538 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
542 c
->nhead_lnum
= lnum
;
543 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
545 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
546 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
547 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
549 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
554 ubifs_err("LPT out of space mismatch");
555 dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
556 "%d, done_lsave %d", lnum
, offs
, len
, done_ltab
, done_lsave
);
557 dbg_dump_lpt_info(c
);
558 dbg_dump_lpt_lebs(c
);
564 * next_pnode_to_dirty - find next pnode to dirty.
565 * @c: UBIFS file-system description object
568 * This function returns the next pnode to dirty or %NULL if there are no more
569 * pnodes. Note that pnodes that have never been written (lnum == 0) are
572 static struct ubifs_pnode
*next_pnode_to_dirty(struct ubifs_info
*c
,
573 struct ubifs_pnode
*pnode
)
575 struct ubifs_nnode
*nnode
;
578 /* Try to go right */
579 nnode
= pnode
->parent
;
580 for (iip
= pnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
581 if (nnode
->nbranch
[iip
].lnum
)
582 return ubifs_get_pnode(c
, nnode
, iip
);
585 /* Go up while can't go right */
587 iip
= nnode
->iip
+ 1;
588 nnode
= nnode
->parent
;
591 for (; iip
< UBIFS_LPT_FANOUT
; iip
++) {
592 if (nnode
->nbranch
[iip
].lnum
)
595 } while (iip
>= UBIFS_LPT_FANOUT
);
598 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
600 return (void *)nnode
;
602 /* Go down to level 1 */
603 while (nnode
->level
> 1) {
604 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++) {
605 if (nnode
->nbranch
[iip
].lnum
)
608 if (iip
>= UBIFS_LPT_FANOUT
) {
610 * Should not happen, but we need to keep going
615 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
617 return (void *)nnode
;
620 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++)
621 if (nnode
->nbranch
[iip
].lnum
)
623 if (iip
>= UBIFS_LPT_FANOUT
)
624 /* Should not happen, but we need to keep going if it does */
626 return ubifs_get_pnode(c
, nnode
, iip
);
630 * pnode_lookup - lookup a pnode in the LPT.
631 * @c: UBIFS file-system description object
632 * @i: pnode number (0 to main_lebs - 1)
634 * This function returns a pointer to the pnode on success or a negative
635 * error code on failure.
637 static struct ubifs_pnode
*pnode_lookup(struct ubifs_info
*c
, int i
)
639 int err
, h
, iip
, shft
;
640 struct ubifs_nnode
*nnode
;
643 err
= ubifs_read_nnode(c
, NULL
, 0);
647 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
649 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
650 for (h
= 1; h
< c
->lpt_hght
; h
++) {
651 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
652 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
653 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
655 return ERR_CAST(nnode
);
657 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
658 return ubifs_get_pnode(c
, nnode
, iip
);
662 * add_pnode_dirt - add dirty space to LPT LEB properties.
663 * @c: UBIFS file-system description object
664 * @pnode: pnode for which to add dirt
666 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
668 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
673 * do_make_pnode_dirty - mark a pnode dirty.
674 * @c: UBIFS file-system description object
675 * @pnode: pnode to mark dirty
677 static void do_make_pnode_dirty(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
679 /* Assumes cnext list is empty i.e. not called during commit */
680 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
681 struct ubifs_nnode
*nnode
;
683 c
->dirty_pn_cnt
+= 1;
684 add_pnode_dirt(c
, pnode
);
685 /* Mark parent and ancestors dirty too */
686 nnode
= pnode
->parent
;
688 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
689 c
->dirty_nn_cnt
+= 1;
690 ubifs_add_nnode_dirt(c
, nnode
);
691 nnode
= nnode
->parent
;
699 * make_tree_dirty - mark the entire LEB properties tree dirty.
700 * @c: UBIFS file-system description object
702 * This function is used by the "small" LPT model to cause the entire LEB
703 * properties tree to be written. The "small" LPT model does not use LPT
704 * garbage collection because it is more efficient to write the entire tree
705 * (because it is small).
707 * This function returns %0 on success and a negative error code on failure.
709 static int make_tree_dirty(struct ubifs_info
*c
)
711 struct ubifs_pnode
*pnode
;
713 pnode
= pnode_lookup(c
, 0);
715 return PTR_ERR(pnode
);
718 do_make_pnode_dirty(c
, pnode
);
719 pnode
= next_pnode_to_dirty(c
, pnode
);
721 return PTR_ERR(pnode
);
727 * need_write_all - determine if the LPT area is running out of free space.
728 * @c: UBIFS file-system description object
730 * This function returns %1 if the LPT area is running out of free space and %0
733 static int need_write_all(struct ubifs_info
*c
)
738 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
739 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
740 free
+= c
->leb_size
- c
->nhead_offs
;
741 else if (c
->ltab
[i
].free
== c
->leb_size
)
743 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
746 /* Less than twice the size left */
747 if (free
<= c
->lpt_sz
* 2)
753 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
754 * @c: UBIFS file-system description object
756 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
757 * free space and so may be reused as soon as the next commit is completed.
758 * This function is called during start commit to mark LPT LEBs for trivial GC.
760 static void lpt_tgc_start(struct ubifs_info
*c
)
764 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
765 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
767 if (c
->ltab
[i
].dirty
> 0 &&
768 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
770 c
->ltab
[i
].free
= c
->leb_size
;
771 c
->ltab
[i
].dirty
= 0;
772 dbg_lp("LEB %d", i
+ c
->lpt_first
);
778 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
779 * @c: UBIFS file-system description object
781 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
782 * free space and so may be reused as soon as the next commit is completed.
783 * This function is called after the commit is completed (master node has been
784 * written) and un-maps LPT LEBs that were marked for trivial GC.
786 static int lpt_tgc_end(struct ubifs_info
*c
)
790 for (i
= 0; i
< c
->lpt_lebs
; i
++)
791 if (c
->ltab
[i
].tgc
) {
792 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
796 dbg_lp("LEB %d", i
+ c
->lpt_first
);
802 * populate_lsave - fill the lsave array with important LEB numbers.
803 * @c: the UBIFS file-system description object
805 * This function is only called for the "big" model. It records a small number
806 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
807 * most important to least important): empty, freeable, freeable index, dirty
808 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
809 * their pnodes into memory. That will stop us from having to scan the LPT
810 * straight away. For the "small" model we assume that scanning the LPT is no
813 static void populate_lsave(struct ubifs_info
*c
)
815 struct ubifs_lprops
*lprops
;
816 struct ubifs_lpt_heap
*heap
;
819 ubifs_assert(c
->big_lpt
);
820 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
821 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
822 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
825 if (dbg_populate_lsave(c
))
828 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
829 c
->lsave
[cnt
++] = lprops
->lnum
;
830 if (cnt
>= c
->lsave_cnt
)
833 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
834 c
->lsave
[cnt
++] = lprops
->lnum
;
835 if (cnt
>= c
->lsave_cnt
)
838 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
839 c
->lsave
[cnt
++] = lprops
->lnum
;
840 if (cnt
>= c
->lsave_cnt
)
843 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
844 for (i
= 0; i
< heap
->cnt
; i
++) {
845 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
846 if (cnt
>= c
->lsave_cnt
)
849 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
850 for (i
= 0; i
< heap
->cnt
; i
++) {
851 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
852 if (cnt
>= c
->lsave_cnt
)
855 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
856 for (i
= 0; i
< heap
->cnt
; i
++) {
857 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
858 if (cnt
>= c
->lsave_cnt
)
861 /* Fill it up completely */
862 while (cnt
< c
->lsave_cnt
)
863 c
->lsave
[cnt
++] = c
->main_first
;
867 * nnode_lookup - lookup a nnode in the LPT.
868 * @c: UBIFS file-system description object
871 * This function returns a pointer to the nnode on success or a negative
872 * error code on failure.
874 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
877 struct ubifs_nnode
*nnode
;
880 err
= ubifs_read_nnode(c
, NULL
, 0);
886 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
887 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
890 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
898 * make_nnode_dirty - find a nnode and, if found, make it dirty.
899 * @c: UBIFS file-system description object
900 * @node_num: nnode number of nnode to make dirty
901 * @lnum: LEB number where nnode was written
902 * @offs: offset where nnode was written
904 * This function is used by LPT garbage collection. LPT garbage collection is
905 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
906 * simply involves marking all the nodes in the LEB being garbage-collected as
907 * dirty. The dirty nodes are written next commit, after which the LEB is free
910 * This function returns %0 on success and a negative error code on failure.
912 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
915 struct ubifs_nnode
*nnode
;
917 nnode
= nnode_lookup(c
, node_num
);
919 return PTR_ERR(nnode
);
921 struct ubifs_nbranch
*branch
;
923 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
924 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
925 return 0; /* nnode is obsolete */
926 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
927 return 0; /* nnode is obsolete */
928 /* Assumes cnext list is empty i.e. not called during commit */
929 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
930 c
->dirty_nn_cnt
+= 1;
931 ubifs_add_nnode_dirt(c
, nnode
);
932 /* Mark parent and ancestors dirty too */
933 nnode
= nnode
->parent
;
935 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
936 c
->dirty_nn_cnt
+= 1;
937 ubifs_add_nnode_dirt(c
, nnode
);
938 nnode
= nnode
->parent
;
947 * make_pnode_dirty - find a pnode and, if found, make it dirty.
948 * @c: UBIFS file-system description object
949 * @node_num: pnode number of pnode to make dirty
950 * @lnum: LEB number where pnode was written
951 * @offs: offset where pnode was written
953 * This function is used by LPT garbage collection. LPT garbage collection is
954 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
955 * simply involves marking all the nodes in the LEB being garbage-collected as
956 * dirty. The dirty nodes are written next commit, after which the LEB is free
959 * This function returns %0 on success and a negative error code on failure.
961 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
964 struct ubifs_pnode
*pnode
;
965 struct ubifs_nbranch
*branch
;
967 pnode
= pnode_lookup(c
, node_num
);
969 return PTR_ERR(pnode
);
970 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
971 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
973 do_make_pnode_dirty(c
, pnode
);
978 * make_ltab_dirty - make ltab node dirty.
979 * @c: UBIFS file-system description object
980 * @lnum: LEB number where ltab was written
981 * @offs: offset where ltab was written
983 * This function is used by LPT garbage collection. LPT garbage collection is
984 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
985 * simply involves marking all the nodes in the LEB being garbage-collected as
986 * dirty. The dirty nodes are written next commit, after which the LEB is free
989 * This function returns %0 on success and a negative error code on failure.
991 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
993 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
994 return 0; /* This ltab node is obsolete */
995 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
996 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
997 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
1003 * make_lsave_dirty - make lsave node dirty.
1004 * @c: UBIFS file-system description object
1005 * @lnum: LEB number where lsave was written
1006 * @offs: offset where lsave was written
1008 * This function is used by LPT garbage collection. LPT garbage collection is
1009 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1010 * simply involves marking all the nodes in the LEB being garbage-collected as
1011 * dirty. The dirty nodes are written next commit, after which the LEB is free
1014 * This function returns %0 on success and a negative error code on failure.
1016 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1018 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1019 return 0; /* This lsave node is obsolete */
1020 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1021 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1022 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1028 * make_node_dirty - make node dirty.
1029 * @c: UBIFS file-system description object
1030 * @node_type: LPT node type
1031 * @node_num: node number
1032 * @lnum: LEB number where node was written
1033 * @offs: offset where node was written
1035 * This function is used by LPT garbage collection. LPT garbage collection is
1036 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1037 * simply involves marking all the nodes in the LEB being garbage-collected as
1038 * dirty. The dirty nodes are written next commit, after which the LEB is free
1041 * This function returns %0 on success and a negative error code on failure.
1043 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1046 switch (node_type
) {
1047 case UBIFS_LPT_NNODE
:
1048 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1049 case UBIFS_LPT_PNODE
:
1050 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1051 case UBIFS_LPT_LTAB
:
1052 return make_ltab_dirty(c
, lnum
, offs
);
1053 case UBIFS_LPT_LSAVE
:
1054 return make_lsave_dirty(c
, lnum
, offs
);
1060 * get_lpt_node_len - return the length of a node based on its type.
1061 * @c: UBIFS file-system description object
1062 * @node_type: LPT node type
1064 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1066 switch (node_type
) {
1067 case UBIFS_LPT_NNODE
:
1069 case UBIFS_LPT_PNODE
:
1071 case UBIFS_LPT_LTAB
:
1073 case UBIFS_LPT_LSAVE
:
1080 * get_pad_len - return the length of padding in a buffer.
1081 * @c: UBIFS file-system description object
1083 * @len: length of buffer
1085 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1089 if (c
->min_io_size
== 1)
1091 offs
= c
->leb_size
- len
;
1092 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1097 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1098 * @c: UBIFS file-system description object
1100 * @node_num: node number is returned here
1102 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1105 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1106 int pos
= 0, node_type
;
1108 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1109 *node_num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1114 * is_a_node - determine if a buffer contains a node.
1115 * @c: UBIFS file-system description object
1117 * @len: length of buffer
1119 * This function returns %1 if the buffer contains a node or %0 if it does not.
1121 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1123 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1124 int pos
= 0, node_type
, node_len
;
1125 uint16_t crc
, calc_crc
;
1127 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1129 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1130 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1132 node_len
= get_lpt_node_len(c
, node_type
);
1133 if (!node_len
|| node_len
> len
)
1137 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1138 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1139 node_len
- UBIFS_LPT_CRC_BYTES
);
1140 if (crc
!= calc_crc
)
1146 * lpt_gc_lnum - garbage collect a LPT LEB.
1147 * @c: UBIFS file-system description object
1148 * @lnum: LEB number to garbage collect
1150 * LPT garbage collection is used only for the "big" LPT model
1151 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1152 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1153 * next commit, after which the LEB is free to be reused.
1155 * This function returns %0 on success and a negative error code on failure.
1157 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1159 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1160 void *buf
= c
->lpt_buf
;
1162 dbg_lp("LEB %d", lnum
);
1163 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1165 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1169 if (!is_a_node(c
, buf
, len
)) {
1172 pad_len
= get_pad_len(c
, buf
, len
);
1180 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1181 node_len
= get_lpt_node_len(c
, node_type
);
1182 offs
= c
->leb_size
- len
;
1183 ubifs_assert(node_len
!= 0);
1184 mutex_lock(&c
->lp_mutex
);
1185 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1186 mutex_unlock(&c
->lp_mutex
);
1196 * lpt_gc - LPT garbage collection.
1197 * @c: UBIFS file-system description object
1199 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1200 * Returns %0 on success and a negative error code on failure.
1202 static int lpt_gc(struct ubifs_info
*c
)
1204 int i
, lnum
= -1, dirty
= 0;
1206 mutex_lock(&c
->lp_mutex
);
1207 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1208 ubifs_assert(!c
->ltab
[i
].tgc
);
1209 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1210 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1212 if (c
->ltab
[i
].dirty
> dirty
) {
1213 dirty
= c
->ltab
[i
].dirty
;
1214 lnum
= i
+ c
->lpt_first
;
1217 mutex_unlock(&c
->lp_mutex
);
1220 return lpt_gc_lnum(c
, lnum
);
1224 * ubifs_lpt_start_commit - UBIFS commit starts.
1225 * @c: the UBIFS file-system description object
1227 * This function has to be called when UBIFS starts the commit operation.
1228 * This function "freezes" all currently dirty LEB properties and does not
1229 * change them anymore. Further changes are saved and tracked separately
1230 * because they are not part of this commit. This function returns zero in case
1231 * of success and a negative error code in case of failure.
1233 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1239 mutex_lock(&c
->lp_mutex
);
1240 err
= dbg_chk_lpt_free_spc(c
);
1243 err
= dbg_check_ltab(c
);
1247 if (c
->check_lpt_free
) {
1249 * We ensure there is enough free space in
1250 * ubifs_lpt_post_commit() by marking nodes dirty. That
1251 * information is lost when we unmount, so we also need
1252 * to check free space once after mounting also.
1254 c
->check_lpt_free
= 0;
1255 while (need_write_all(c
)) {
1256 mutex_unlock(&c
->lp_mutex
);
1260 mutex_lock(&c
->lp_mutex
);
1266 if (!c
->dirty_pn_cnt
) {
1267 dbg_cmt("no cnodes to commit");
1272 if (!c
->big_lpt
&& need_write_all(c
)) {
1273 /* If needed, write everything */
1274 err
= make_tree_dirty(c
);
1283 cnt
= get_cnodes_to_commit(c
);
1284 ubifs_assert(cnt
!= 0);
1286 err
= layout_cnodes(c
);
1290 /* Copy the LPT's own lprops for end commit to write */
1291 memcpy(c
->ltab_cmt
, c
->ltab
,
1292 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1293 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1296 mutex_unlock(&c
->lp_mutex
);
1301 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1302 * @c: UBIFS file-system description object
1304 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1306 struct ubifs_cnode
*cnode
, *cnext
;
1308 cnext
= c
->lpt_cnext
;
1313 cnext
= cnode
->cnext
;
1314 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1317 cnode
->cnext
= NULL
;
1318 } while (cnext
!= c
->lpt_cnext
);
1319 c
->lpt_cnext
= NULL
;
1323 * ubifs_lpt_end_commit - finish the commit operation.
1324 * @c: the UBIFS file-system description object
1326 * This function has to be called when the commit operation finishes. It
1327 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1328 * the media. Returns zero in case of success and a negative error code in case
1331 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1340 err
= write_cnodes(c
);
1344 mutex_lock(&c
->lp_mutex
);
1345 free_obsolete_cnodes(c
);
1346 mutex_unlock(&c
->lp_mutex
);
1352 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1353 * @c: UBIFS file-system description object
1355 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1356 * commit for the "big" LPT model.
1358 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1362 mutex_lock(&c
->lp_mutex
);
1363 err
= lpt_tgc_end(c
);
1367 while (need_write_all(c
)) {
1368 mutex_unlock(&c
->lp_mutex
);
1372 mutex_lock(&c
->lp_mutex
);
1375 mutex_unlock(&c
->lp_mutex
);
1380 * first_nnode - find the first nnode in memory.
1381 * @c: UBIFS file-system description object
1382 * @hght: height of tree where nnode found is returned here
1384 * This function returns a pointer to the nnode found or %NULL if no nnode is
1385 * found. This function is a helper to 'ubifs_lpt_free()'.
1387 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1389 struct ubifs_nnode
*nnode
;
1396 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1398 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1399 if (nnode
->nbranch
[i
].nnode
) {
1401 nnode
= nnode
->nbranch
[i
].nnode
;
1413 * next_nnode - find the next nnode in memory.
1414 * @c: UBIFS file-system description object
1415 * @nnode: nnode from which to start.
1416 * @hght: height of tree where nnode is, is passed and returned here
1418 * This function returns a pointer to the nnode found or %NULL if no nnode is
1419 * found. This function is a helper to 'ubifs_lpt_free()'.
1421 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1422 struct ubifs_nnode
*nnode
, int *hght
)
1424 struct ubifs_nnode
*parent
;
1425 int iip
, h
, i
, found
;
1427 parent
= nnode
->parent
;
1430 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1434 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1435 nnode
= parent
->nbranch
[iip
].nnode
;
1443 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1445 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1446 if (nnode
->nbranch
[i
].nnode
) {
1448 nnode
= nnode
->nbranch
[i
].nnode
;
1460 * ubifs_lpt_free - free resources owned by the LPT.
1461 * @c: UBIFS file-system description object
1462 * @wr_only: free only resources used for writing
1464 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1466 struct ubifs_nnode
*nnode
;
1469 /* Free write-only things first */
1471 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1483 /* Now free the rest */
1485 nnode
= first_nnode(c
, &hght
);
1487 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1488 kfree(nnode
->nbranch
[i
].nnode
);
1489 nnode
= next_nnode(c
, nnode
, &hght
);
1491 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1492 kfree(c
->lpt_heap
[i
].arr
);
1493 kfree(c
->dirty_idx
.arr
);
1496 kfree(c
->lpt_nod_buf
);
1499 #ifdef CONFIG_UBIFS_FS_DEBUG
1502 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1504 * @len: buffer length
1506 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1510 for (i
= 0; i
< len
; i
++)
1517 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1518 * @c: the UBIFS file-system description object
1519 * @lnum: LEB number where nnode was written
1520 * @offs: offset where nnode was written
1522 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1524 struct ubifs_nnode
*nnode
;
1527 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1528 nnode
= first_nnode(c
, &hght
);
1529 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1530 struct ubifs_nbranch
*branch
;
1533 if (nnode
->parent
) {
1534 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1535 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1537 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1541 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1543 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1552 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1553 * @c: the UBIFS file-system description object
1554 * @lnum: LEB number where pnode was written
1555 * @offs: offset where pnode was written
1557 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1561 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1562 for (i
= 0; i
< cnt
; i
++) {
1563 struct ubifs_pnode
*pnode
;
1564 struct ubifs_nbranch
*branch
;
1567 pnode
= pnode_lookup(c
, i
);
1569 return PTR_ERR(pnode
);
1570 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1571 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1573 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1581 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1582 * @c: the UBIFS file-system description object
1583 * @lnum: LEB number where ltab node was written
1584 * @offs: offset where ltab node was written
1586 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1588 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1590 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1594 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1595 * @c: the UBIFS file-system description object
1596 * @lnum: LEB number where lsave node was written
1597 * @offs: offset where lsave node was written
1599 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1601 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1603 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1607 * dbg_is_node_dirty - determine if a node is dirty.
1608 * @c: the UBIFS file-system description object
1609 * @node_type: node type
1610 * @lnum: LEB number where node was written
1611 * @offs: offset where node was written
1613 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1616 switch (node_type
) {
1617 case UBIFS_LPT_NNODE
:
1618 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1619 case UBIFS_LPT_PNODE
:
1620 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1621 case UBIFS_LPT_LTAB
:
1622 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1623 case UBIFS_LPT_LSAVE
:
1624 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1630 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1631 * @c: the UBIFS file-system description object
1632 * @lnum: LEB number where node was written
1633 * @offs: offset where node was written
1635 * This function returns %0 on success and a negative error code on failure.
1637 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1639 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1643 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1646 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1648 ubifs_err("cannot allocate memory for ltab checking");
1652 dbg_lp("LEB %d", lnum
);
1653 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1655 dbg_msg("ubi_read failed, LEB %d, error %d", lnum
, err
);
1659 if (!is_a_node(c
, p
, len
)) {
1662 pad_len
= get_pad_len(c
, p
, len
);
1669 if (!dbg_is_all_ff(p
, len
)) {
1670 dbg_msg("invalid empty space in LEB %d at %d",
1671 lnum
, c
->leb_size
- len
);
1674 i
= lnum
- c
->lpt_first
;
1675 if (len
!= c
->ltab
[i
].free
) {
1676 dbg_msg("invalid free space in LEB %d "
1677 "(free %d, expected %d)",
1678 lnum
, len
, c
->ltab
[i
].free
);
1681 if (dirty
!= c
->ltab
[i
].dirty
) {
1682 dbg_msg("invalid dirty space in LEB %d "
1683 "(dirty %d, expected %d)",
1684 lnum
, dirty
, c
->ltab
[i
].dirty
);
1689 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1690 node_len
= get_lpt_node_len(c
, node_type
);
1691 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1705 * dbg_check_ltab - check the free and dirty space in the ltab.
1706 * @c: the UBIFS file-system description object
1708 * This function returns %0 on success and a negative error code on failure.
1710 int dbg_check_ltab(struct ubifs_info
*c
)
1712 int lnum
, err
, i
, cnt
;
1714 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1717 /* Bring the entire tree into memory */
1718 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1719 for (i
= 0; i
< cnt
; i
++) {
1720 struct ubifs_pnode
*pnode
;
1722 pnode
= pnode_lookup(c
, i
);
1724 return PTR_ERR(pnode
);
1729 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1733 /* Check each LEB */
1734 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1735 err
= dbg_check_ltab_lnum(c
, lnum
);
1737 dbg_err("failed at LEB %d", lnum
);
1742 dbg_lp("succeeded");
1747 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1748 * @c: the UBIFS file-system description object
1750 * This function returns %0 on success and a negative error code on failure.
1752 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1757 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1760 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1761 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1763 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1764 free
+= c
->leb_size
- c
->nhead_offs
;
1765 else if (c
->ltab
[i
].free
== c
->leb_size
)
1766 free
+= c
->leb_size
;
1768 if (free
< c
->lpt_sz
) {
1769 dbg_err("LPT space error: free %lld lpt_sz %lld",
1771 dbg_dump_lpt_info(c
);
1772 dbg_dump_lpt_lebs(c
);
1780 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1781 * @c: the UBIFS file-system description object
1782 * @action: what to do
1783 * @len: length written
1785 * This function returns %0 on success and a negative error code on failure.
1786 * The @action argument may be one of:
1787 * o %0 - LPT debugging checking starts, initialize debugging variables;
1788 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1789 * o %2 - switched to a different LEB and wasted @len bytes;
1790 * o %3 - check that we've written the right number of bytes.
1791 * o %4 - wasted @len bytes;
1793 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1795 struct ubifs_debug_info
*d
= c
->dbg
;
1796 long long chk_lpt_sz
, lpt_sz
;
1799 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
1806 d
->chk_lpt_lebs
= 0;
1807 d
->chk_lpt_wastage
= 0;
1808 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1809 dbg_err("dirty pnodes %d exceed max %d",
1810 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1813 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1814 dbg_err("dirty nnodes %d exceed max %d",
1815 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1820 d
->chk_lpt_sz
+= len
;
1823 d
->chk_lpt_sz
+= len
;
1824 d
->chk_lpt_wastage
+= len
;
1825 d
->chk_lpt_lebs
+= 1;
1828 chk_lpt_sz
= c
->leb_size
;
1829 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1830 chk_lpt_sz
+= len
- c
->nhead_offs
;
1831 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1832 dbg_err("LPT wrote %lld but space used was %lld",
1833 d
->chk_lpt_sz
, chk_lpt_sz
);
1836 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1837 dbg_err("LPT wrote %lld but lpt_sz is %lld",
1838 d
->chk_lpt_sz
, c
->lpt_sz
);
1841 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1842 dbg_err("LPT layout size %lld but wrote %lld",
1843 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1846 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1847 dbg_err("LPT new nhead offs: expected %d was %d",
1848 d
->new_nhead_offs
, len
);
1851 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1852 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1853 lpt_sz
+= c
->ltab_sz
;
1855 lpt_sz
+= c
->lsave_sz
;
1856 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1857 dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1858 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1862 dbg_dump_lpt_info(c
);
1863 dbg_dump_lpt_lebs(c
);
1866 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1868 d
->chk_lpt_wastage
= 0;
1869 d
->chk_lpt_lebs
= 0;
1870 d
->new_nhead_offs
= len
;
1873 d
->chk_lpt_sz
+= len
;
1874 d
->chk_lpt_wastage
+= len
;
1882 * dbg_dump_lpt_leb - dump an LPT LEB.
1883 * @c: UBIFS file-system description object
1884 * @lnum: LEB number to dump
1886 * This function dumps an LEB from LPT area. Nodes in this area are very
1887 * different to nodes in the main area (e.g., they do not have common headers,
1888 * they do not have 8-byte alignments, etc), so we have a separate function to
1889 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1891 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1893 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1896 printk(KERN_DEBUG
"(pid %d) start dumping LEB %d\n",
1897 current
->pid
, lnum
);
1898 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1900 ubifs_err("cannot allocate memory to dump LPT");
1904 err
= ubi_read(c
->ubi
, lnum
, buf
, 0, c
->leb_size
);
1906 ubifs_err("cannot read LEB %d, error %d", lnum
, err
);
1910 offs
= c
->leb_size
- len
;
1911 if (!is_a_node(c
, p
, len
)) {
1914 pad_len
= get_pad_len(c
, p
, len
);
1916 printk(KERN_DEBUG
"LEB %d:%d, pad %d bytes\n",
1917 lnum
, offs
, pad_len
);
1923 printk(KERN_DEBUG
"LEB %d:%d, free %d bytes\n",
1928 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1929 switch (node_type
) {
1930 case UBIFS_LPT_PNODE
:
1932 node_len
= c
->pnode_sz
;
1934 printk(KERN_DEBUG
"LEB %d:%d, pnode num %d\n",
1935 lnum
, offs
, node_num
);
1937 printk(KERN_DEBUG
"LEB %d:%d, pnode\n",
1941 case UBIFS_LPT_NNODE
:
1944 struct ubifs_nnode nnode
;
1946 node_len
= c
->nnode_sz
;
1948 printk(KERN_DEBUG
"LEB %d:%d, nnode num %d, ",
1949 lnum
, offs
, node_num
);
1951 printk(KERN_DEBUG
"LEB %d:%d, nnode, ",
1953 err
= ubifs_unpack_nnode(c
, p
, &nnode
);
1954 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1955 printk(KERN_CONT
"%d:%d", nnode
.nbranch
[i
].lnum
,
1956 nnode
.nbranch
[i
].offs
);
1957 if (i
!= UBIFS_LPT_FANOUT
- 1)
1958 printk(KERN_CONT
", ");
1960 printk(KERN_CONT
"\n");
1963 case UBIFS_LPT_LTAB
:
1964 node_len
= c
->ltab_sz
;
1965 printk(KERN_DEBUG
"LEB %d:%d, ltab\n",
1968 case UBIFS_LPT_LSAVE
:
1969 node_len
= c
->lsave_sz
;
1970 printk(KERN_DEBUG
"LEB %d:%d, lsave len\n", lnum
, offs
);
1973 ubifs_err("LPT node type %d not recognized", node_type
);
1981 printk(KERN_DEBUG
"(pid %d) finish dumping LEB %d\n",
1982 current
->pid
, lnum
);
1989 * dbg_dump_lpt_lebs - dump LPT lebs.
1990 * @c: UBIFS file-system description object
1992 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1995 void dbg_dump_lpt_lebs(const struct ubifs_info
*c
)
1999 printk(KERN_DEBUG
"(pid %d) start dumping all LPT LEBs\n",
2001 for (i
= 0; i
< c
->lpt_lebs
; i
++)
2002 dump_lpt_leb(c
, i
+ c
->lpt_first
);
2003 printk(KERN_DEBUG
"(pid %d) finish dumping all LPT LEBs\n",
2008 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2009 * @c: UBIFS file-system description object
2011 * This is a debugging version for 'populate_lsave()' which populates lsave
2012 * with random LEBs instead of useful LEBs, which is good for test coverage.
2013 * Returns zero if lsave has not been populated (this debugging feature is
2014 * disabled) an non-zero if lsave has been populated.
2016 static int dbg_populate_lsave(struct ubifs_info
*c
)
2018 struct ubifs_lprops
*lprops
;
2019 struct ubifs_lpt_heap
*heap
;
2022 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
2027 for (i
= 0; i
< c
->lsave_cnt
; i
++)
2028 c
->lsave
[i
] = c
->main_first
;
2030 list_for_each_entry(lprops
, &c
->empty_list
, list
)
2031 c
->lsave
[random32() % c
->lsave_cnt
] = lprops
->lnum
;
2032 list_for_each_entry(lprops
, &c
->freeable_list
, list
)
2033 c
->lsave
[random32() % c
->lsave_cnt
] = lprops
->lnum
;
2034 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
)
2035 c
->lsave
[random32() % c
->lsave_cnt
] = lprops
->lnum
;
2037 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
2038 for (i
= 0; i
< heap
->cnt
; i
++)
2039 c
->lsave
[random32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2040 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
2041 for (i
= 0; i
< heap
->cnt
; i
++)
2042 c
->lsave
[random32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2043 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
2044 for (i
= 0; i
< heap
->cnt
; i
++)
2045 c
->lsave
[random32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2050 #endif /* CONFIG_UBIFS_FS_DEBUG */