2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements commit-related functionality of the LEB properties
28 #include <linux/crc16.h>
29 #include <linux/slab.h>
30 #include <linux/random.h>
33 static int dbg_populate_lsave(struct ubifs_info
*c
);
36 * first_dirty_cnode - find first dirty cnode.
37 * @c: UBIFS file-system description object
38 * @nnode: nnode at which to start
40 * This function returns the first dirty cnode or %NULL if there is not one.
42 static struct ubifs_cnode
*first_dirty_cnode(const struct ubifs_info
*c
, struct ubifs_nnode
*nnode
)
44 ubifs_assert(c
, nnode
);
48 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
49 struct ubifs_cnode
*cnode
;
51 cnode
= nnode
->nbranch
[i
].cnode
;
53 test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
54 if (cnode
->level
== 0)
56 nnode
= (struct ubifs_nnode
*)cnode
;
62 return (struct ubifs_cnode
*)nnode
;
67 * next_dirty_cnode - find next dirty cnode.
68 * @c: UBIFS file-system description object
69 * @cnode: cnode from which to begin searching
71 * This function returns the next dirty cnode or %NULL if there is not one.
73 static struct ubifs_cnode
*next_dirty_cnode(const struct ubifs_info
*c
, struct ubifs_cnode
*cnode
)
75 struct ubifs_nnode
*nnode
;
78 ubifs_assert(c
, cnode
);
79 nnode
= cnode
->parent
;
82 for (i
= cnode
->iip
+ 1; i
< UBIFS_LPT_FANOUT
; i
++) {
83 cnode
= nnode
->nbranch
[i
].cnode
;
84 if (cnode
&& test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
85 if (cnode
->level
== 0)
86 return cnode
; /* cnode is a pnode */
87 /* cnode is a nnode */
88 return first_dirty_cnode(c
, (struct ubifs_nnode
*)cnode
);
91 return (struct ubifs_cnode
*)nnode
;
95 * get_cnodes_to_commit - create list of dirty cnodes to commit.
96 * @c: UBIFS file-system description object
98 * This function returns the number of cnodes to commit.
100 static int get_cnodes_to_commit(struct ubifs_info
*c
)
102 struct ubifs_cnode
*cnode
, *cnext
;
108 if (!test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
111 c
->lpt_cnext
= first_dirty_cnode(c
, c
->nroot
);
112 cnode
= c
->lpt_cnext
;
117 ubifs_assert(c
, !test_bit(COW_CNODE
, &cnode
->flags
));
118 __set_bit(COW_CNODE
, &cnode
->flags
);
119 cnext
= next_dirty_cnode(c
, cnode
);
121 cnode
->cnext
= c
->lpt_cnext
;
124 cnode
->cnext
= cnext
;
128 dbg_cmt("committing %d cnodes", cnt
);
129 dbg_lp("committing %d cnodes", cnt
);
130 ubifs_assert(c
, cnt
== c
->dirty_nn_cnt
+ c
->dirty_pn_cnt
);
135 * upd_ltab - update LPT LEB properties.
136 * @c: UBIFS file-system description object
138 * @free: amount of free space
139 * @dirty: amount of dirty space to add
141 static void upd_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
143 dbg_lp("LEB %d free %d dirty %d to %d +%d",
144 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
145 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
146 ubifs_assert(c
, lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
147 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
148 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
152 * alloc_lpt_leb - allocate an LPT LEB that is empty.
153 * @c: UBIFS file-system description object
154 * @lnum: LEB number is passed and returned here
156 * This function finds the next empty LEB in the ltab starting from @lnum. If a
157 * an empty LEB is found it is returned in @lnum and the function returns %0.
158 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
159 * never to run out of space.
161 static int alloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
165 n
= *lnum
- c
->lpt_first
+ 1;
166 for (i
= n
; i
< c
->lpt_lebs
; i
++) {
167 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
169 if (c
->ltab
[i
].free
== c
->leb_size
) {
171 *lnum
= i
+ c
->lpt_first
;
176 for (i
= 0; i
< n
; i
++) {
177 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
179 if (c
->ltab
[i
].free
== c
->leb_size
) {
181 *lnum
= i
+ c
->lpt_first
;
189 * layout_cnodes - layout cnodes for commit.
190 * @c: UBIFS file-system description object
192 * This function returns %0 on success and a negative error code on failure.
194 static int layout_cnodes(struct ubifs_info
*c
)
196 int lnum
, offs
, len
, alen
, done_lsave
, done_ltab
, err
;
197 struct ubifs_cnode
*cnode
;
199 err
= dbg_chk_lpt_sz(c
, 0, 0);
202 cnode
= c
->lpt_cnext
;
205 lnum
= c
->nhead_lnum
;
206 offs
= c
->nhead_offs
;
207 /* Try to place lsave and ltab nicely */
208 done_lsave
= !c
->big_lpt
;
210 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
212 c
->lsave_lnum
= lnum
;
213 c
->lsave_offs
= offs
;
215 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
218 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
223 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
229 c
->dirty_nn_cnt
-= 1;
232 c
->dirty_pn_cnt
-= 1;
234 while (offs
+ len
> c
->leb_size
) {
235 alen
= ALIGN(offs
, c
->min_io_size
);
236 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
237 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
238 err
= alloc_lpt_leb(c
, &lnum
);
242 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
243 lnum
<= c
->lpt_last
);
244 /* Try to place lsave and ltab nicely */
247 c
->lsave_lnum
= lnum
;
248 c
->lsave_offs
= offs
;
250 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
258 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
264 cnode
->parent
->nbranch
[cnode
->iip
].lnum
= lnum
;
265 cnode
->parent
->nbranch
[cnode
->iip
].offs
= offs
;
271 dbg_chk_lpt_sz(c
, 1, len
);
272 cnode
= cnode
->cnext
;
273 } while (cnode
&& cnode
!= c
->lpt_cnext
);
275 /* Make sure to place LPT's save table */
277 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
278 alen
= ALIGN(offs
, c
->min_io_size
);
279 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
280 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
281 err
= alloc_lpt_leb(c
, &lnum
);
285 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
286 lnum
<= c
->lpt_last
);
289 c
->lsave_lnum
= lnum
;
290 c
->lsave_offs
= offs
;
292 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
295 /* Make sure to place LPT's own lprops table */
297 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
298 alen
= ALIGN(offs
, c
->min_io_size
);
299 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
300 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
301 err
= alloc_lpt_leb(c
, &lnum
);
305 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
306 lnum
<= c
->lpt_last
);
311 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
314 alen
= ALIGN(offs
, c
->min_io_size
);
315 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
316 dbg_chk_lpt_sz(c
, 4, alen
- offs
);
317 err
= dbg_chk_lpt_sz(c
, 3, alen
);
323 ubifs_err(c
, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
324 lnum
, offs
, len
, done_ltab
, done_lsave
);
325 ubifs_dump_lpt_info(c
);
326 ubifs_dump_lpt_lebs(c
);
332 * realloc_lpt_leb - allocate an LPT LEB that is empty.
333 * @c: UBIFS file-system description object
334 * @lnum: LEB number is passed and returned here
336 * This function duplicates exactly the results of the function alloc_lpt_leb.
337 * It is used during end commit to reallocate the same LEB numbers that were
338 * allocated by alloc_lpt_leb during start commit.
340 * This function finds the next LEB that was allocated by the alloc_lpt_leb
341 * function starting from @lnum. If a LEB is found it is returned in @lnum and
342 * the function returns %0. Otherwise the function returns -ENOSPC.
343 * Note however, that LPT is designed never to run out of space.
345 static int realloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
349 n
= *lnum
- c
->lpt_first
+ 1;
350 for (i
= n
; i
< c
->lpt_lebs
; i
++)
351 if (c
->ltab
[i
].cmt
) {
353 *lnum
= i
+ c
->lpt_first
;
357 for (i
= 0; i
< n
; i
++)
358 if (c
->ltab
[i
].cmt
) {
360 *lnum
= i
+ c
->lpt_first
;
367 * write_cnodes - write cnodes for commit.
368 * @c: UBIFS file-system description object
370 * This function returns %0 on success and a negative error code on failure.
372 static int write_cnodes(struct ubifs_info
*c
)
374 int lnum
, offs
, len
, from
, err
, wlen
, alen
, done_ltab
, done_lsave
;
375 struct ubifs_cnode
*cnode
;
376 void *buf
= c
->lpt_buf
;
378 cnode
= c
->lpt_cnext
;
381 lnum
= c
->nhead_lnum
;
382 offs
= c
->nhead_offs
;
384 /* Ensure empty LEB is unmapped */
386 err
= ubifs_leb_unmap(c
, lnum
);
390 /* Try to place lsave and ltab nicely */
391 done_lsave
= !c
->big_lpt
;
393 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
395 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
397 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
400 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
402 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
404 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
407 /* Loop for each cnode */
413 while (offs
+ len
> c
->leb_size
) {
416 alen
= ALIGN(wlen
, c
->min_io_size
);
417 memset(buf
+ offs
, 0xff, alen
- wlen
);
418 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
,
423 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
424 err
= realloc_lpt_leb(c
, &lnum
);
428 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
429 lnum
<= c
->lpt_last
);
430 err
= ubifs_leb_unmap(c
, lnum
);
433 /* Try to place lsave and ltab nicely */
436 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
438 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
443 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
445 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
451 ubifs_pack_nnode(c
, buf
+ offs
,
452 (struct ubifs_nnode
*)cnode
);
454 ubifs_pack_pnode(c
, buf
+ offs
,
455 (struct ubifs_pnode
*)cnode
);
457 * The reason for the barriers is the same as in case of TNC.
458 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
459 * 'dirty_cow_pnode()' are the functions for which this is
462 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
463 smp_mb__before_atomic();
464 clear_bit(COW_CNODE
, &cnode
->flags
);
465 smp_mb__after_atomic();
467 dbg_chk_lpt_sz(c
, 1, len
);
468 cnode
= cnode
->cnext
;
469 } while (cnode
&& cnode
!= c
->lpt_cnext
);
471 /* Make sure to place LPT's save table */
473 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
475 alen
= ALIGN(wlen
, c
->min_io_size
);
476 memset(buf
+ offs
, 0xff, alen
- wlen
);
477 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
480 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
481 err
= realloc_lpt_leb(c
, &lnum
);
485 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
486 lnum
<= c
->lpt_last
);
487 err
= ubifs_leb_unmap(c
, lnum
);
492 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
494 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
497 /* Make sure to place LPT's own lprops table */
499 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
501 alen
= ALIGN(wlen
, c
->min_io_size
);
502 memset(buf
+ offs
, 0xff, alen
- wlen
);
503 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
506 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
507 err
= realloc_lpt_leb(c
, &lnum
);
511 ubifs_assert(c
, lnum
>= c
->lpt_first
&&
512 lnum
<= c
->lpt_last
);
513 err
= ubifs_leb_unmap(c
, lnum
);
517 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
519 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
522 /* Write remaining data in buffer */
524 alen
= ALIGN(wlen
, c
->min_io_size
);
525 memset(buf
+ offs
, 0xff, alen
- wlen
);
526 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
530 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
531 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
535 c
->nhead_lnum
= lnum
;
536 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
538 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
539 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
540 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
542 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
547 ubifs_err(c
, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
548 lnum
, offs
, len
, done_ltab
, done_lsave
);
549 ubifs_dump_lpt_info(c
);
550 ubifs_dump_lpt_lebs(c
);
556 * next_pnode_to_dirty - find next pnode to dirty.
557 * @c: UBIFS file-system description object
560 * This function returns the next pnode to dirty or %NULL if there are no more
561 * pnodes. Note that pnodes that have never been written (lnum == 0) are
564 static struct ubifs_pnode
*next_pnode_to_dirty(struct ubifs_info
*c
,
565 struct ubifs_pnode
*pnode
)
567 struct ubifs_nnode
*nnode
;
570 /* Try to go right */
571 nnode
= pnode
->parent
;
572 for (iip
= pnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
573 if (nnode
->nbranch
[iip
].lnum
)
574 return ubifs_get_pnode(c
, nnode
, iip
);
577 /* Go up while can't go right */
579 iip
= nnode
->iip
+ 1;
580 nnode
= nnode
->parent
;
583 for (; iip
< UBIFS_LPT_FANOUT
; iip
++) {
584 if (nnode
->nbranch
[iip
].lnum
)
587 } while (iip
>= UBIFS_LPT_FANOUT
);
590 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
592 return (void *)nnode
;
594 /* Go down to level 1 */
595 while (nnode
->level
> 1) {
596 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++) {
597 if (nnode
->nbranch
[iip
].lnum
)
600 if (iip
>= UBIFS_LPT_FANOUT
) {
602 * Should not happen, but we need to keep going
607 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
609 return (void *)nnode
;
612 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++)
613 if (nnode
->nbranch
[iip
].lnum
)
615 if (iip
>= UBIFS_LPT_FANOUT
)
616 /* Should not happen, but we need to keep going if it does */
618 return ubifs_get_pnode(c
, nnode
, iip
);
622 * pnode_lookup - lookup a pnode in the LPT.
623 * @c: UBIFS file-system description object
624 * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT))
626 * This function returns a pointer to the pnode on success or a negative
627 * error code on failure.
629 static struct ubifs_pnode
*pnode_lookup(struct ubifs_info
*c
, int i
)
631 int err
, h
, iip
, shft
;
632 struct ubifs_nnode
*nnode
;
635 err
= ubifs_read_nnode(c
, NULL
, 0);
639 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
641 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
642 for (h
= 1; h
< c
->lpt_hght
; h
++) {
643 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
644 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
645 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
647 return ERR_CAST(nnode
);
649 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
650 return ubifs_get_pnode(c
, nnode
, iip
);
654 * add_pnode_dirt - add dirty space to LPT LEB properties.
655 * @c: UBIFS file-system description object
656 * @pnode: pnode for which to add dirt
658 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
660 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
665 * do_make_pnode_dirty - mark a pnode dirty.
666 * @c: UBIFS file-system description object
667 * @pnode: pnode to mark dirty
669 static void do_make_pnode_dirty(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
671 /* Assumes cnext list is empty i.e. not called during commit */
672 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
673 struct ubifs_nnode
*nnode
;
675 c
->dirty_pn_cnt
+= 1;
676 add_pnode_dirt(c
, pnode
);
677 /* Mark parent and ancestors dirty too */
678 nnode
= pnode
->parent
;
680 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
681 c
->dirty_nn_cnt
+= 1;
682 ubifs_add_nnode_dirt(c
, nnode
);
683 nnode
= nnode
->parent
;
691 * make_tree_dirty - mark the entire LEB properties tree dirty.
692 * @c: UBIFS file-system description object
694 * This function is used by the "small" LPT model to cause the entire LEB
695 * properties tree to be written. The "small" LPT model does not use LPT
696 * garbage collection because it is more efficient to write the entire tree
697 * (because it is small).
699 * This function returns %0 on success and a negative error code on failure.
701 static int make_tree_dirty(struct ubifs_info
*c
)
703 struct ubifs_pnode
*pnode
;
705 pnode
= pnode_lookup(c
, 0);
707 return PTR_ERR(pnode
);
710 do_make_pnode_dirty(c
, pnode
);
711 pnode
= next_pnode_to_dirty(c
, pnode
);
713 return PTR_ERR(pnode
);
719 * need_write_all - determine if the LPT area is running out of free space.
720 * @c: UBIFS file-system description object
722 * This function returns %1 if the LPT area is running out of free space and %0
725 static int need_write_all(struct ubifs_info
*c
)
730 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
731 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
732 free
+= c
->leb_size
- c
->nhead_offs
;
733 else if (c
->ltab
[i
].free
== c
->leb_size
)
735 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
738 /* Less than twice the size left */
739 if (free
<= c
->lpt_sz
* 2)
745 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
746 * @c: UBIFS file-system description object
748 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
749 * free space and so may be reused as soon as the next commit is completed.
750 * This function is called during start commit to mark LPT LEBs for trivial GC.
752 static void lpt_tgc_start(struct ubifs_info
*c
)
756 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
757 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
759 if (c
->ltab
[i
].dirty
> 0 &&
760 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
762 c
->ltab
[i
].free
= c
->leb_size
;
763 c
->ltab
[i
].dirty
= 0;
764 dbg_lp("LEB %d", i
+ c
->lpt_first
);
770 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
771 * @c: UBIFS file-system description object
773 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
774 * free space and so may be reused as soon as the next commit is completed.
775 * This function is called after the commit is completed (master node has been
776 * written) and un-maps LPT LEBs that were marked for trivial GC.
778 static int lpt_tgc_end(struct ubifs_info
*c
)
782 for (i
= 0; i
< c
->lpt_lebs
; i
++)
783 if (c
->ltab
[i
].tgc
) {
784 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
788 dbg_lp("LEB %d", i
+ c
->lpt_first
);
794 * populate_lsave - fill the lsave array with important LEB numbers.
795 * @c: the UBIFS file-system description object
797 * This function is only called for the "big" model. It records a small number
798 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
799 * most important to least important): empty, freeable, freeable index, dirty
800 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
801 * their pnodes into memory. That will stop us from having to scan the LPT
802 * straight away. For the "small" model we assume that scanning the LPT is no
805 static void populate_lsave(struct ubifs_info
*c
)
807 struct ubifs_lprops
*lprops
;
808 struct ubifs_lpt_heap
*heap
;
811 ubifs_assert(c
, 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
);
817 if (dbg_populate_lsave(c
))
820 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
821 c
->lsave
[cnt
++] = lprops
->lnum
;
822 if (cnt
>= c
->lsave_cnt
)
825 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
826 c
->lsave
[cnt
++] = lprops
->lnum
;
827 if (cnt
>= c
->lsave_cnt
)
830 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
831 c
->lsave
[cnt
++] = lprops
->lnum
;
832 if (cnt
>= c
->lsave_cnt
)
835 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
836 for (i
= 0; i
< heap
->cnt
; i
++) {
837 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
838 if (cnt
>= c
->lsave_cnt
)
841 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
842 for (i
= 0; i
< heap
->cnt
; i
++) {
843 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
844 if (cnt
>= c
->lsave_cnt
)
847 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
848 for (i
= 0; i
< heap
->cnt
; i
++) {
849 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
850 if (cnt
>= c
->lsave_cnt
)
853 /* Fill it up completely */
854 while (cnt
< c
->lsave_cnt
)
855 c
->lsave
[cnt
++] = c
->main_first
;
859 * nnode_lookup - lookup a nnode in the LPT.
860 * @c: UBIFS file-system description object
863 * This function returns a pointer to the nnode on success or a negative
864 * error code on failure.
866 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
869 struct ubifs_nnode
*nnode
;
872 err
= ubifs_read_nnode(c
, NULL
, 0);
878 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
879 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
882 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
890 * make_nnode_dirty - find a nnode and, if found, make it dirty.
891 * @c: UBIFS file-system description object
892 * @node_num: nnode number of nnode to make dirty
893 * @lnum: LEB number where nnode was written
894 * @offs: offset where nnode was written
896 * This function is used by LPT garbage collection. LPT garbage collection is
897 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
898 * simply involves marking all the nodes in the LEB being garbage-collected as
899 * dirty. The dirty nodes are written next commit, after which the LEB is free
902 * This function returns %0 on success and a negative error code on failure.
904 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
907 struct ubifs_nnode
*nnode
;
909 nnode
= nnode_lookup(c
, node_num
);
911 return PTR_ERR(nnode
);
913 struct ubifs_nbranch
*branch
;
915 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
916 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
917 return 0; /* nnode is obsolete */
918 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
919 return 0; /* nnode is obsolete */
920 /* Assumes cnext list is empty i.e. not called during commit */
921 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
922 c
->dirty_nn_cnt
+= 1;
923 ubifs_add_nnode_dirt(c
, nnode
);
924 /* Mark parent and ancestors dirty too */
925 nnode
= nnode
->parent
;
927 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
928 c
->dirty_nn_cnt
+= 1;
929 ubifs_add_nnode_dirt(c
, nnode
);
930 nnode
= nnode
->parent
;
939 * make_pnode_dirty - find a pnode and, if found, make it dirty.
940 * @c: UBIFS file-system description object
941 * @node_num: pnode number of pnode to make dirty
942 * @lnum: LEB number where pnode was written
943 * @offs: offset where pnode was written
945 * This function is used by LPT garbage collection. LPT garbage collection is
946 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
947 * simply involves marking all the nodes in the LEB being garbage-collected as
948 * dirty. The dirty nodes are written next commit, after which the LEB is free
951 * This function returns %0 on success and a negative error code on failure.
953 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
956 struct ubifs_pnode
*pnode
;
957 struct ubifs_nbranch
*branch
;
959 pnode
= pnode_lookup(c
, node_num
);
961 return PTR_ERR(pnode
);
962 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
963 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
965 do_make_pnode_dirty(c
, pnode
);
970 * make_ltab_dirty - make ltab node dirty.
971 * @c: UBIFS file-system description object
972 * @lnum: LEB number where ltab was written
973 * @offs: offset where ltab was written
975 * This function is used by LPT garbage collection. LPT garbage collection is
976 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
977 * simply involves marking all the nodes in the LEB being garbage-collected as
978 * dirty. The dirty nodes are written next commit, after which the LEB is free
981 * This function returns %0 on success and a negative error code on failure.
983 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
985 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
986 return 0; /* This ltab node is obsolete */
987 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
988 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
989 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
995 * make_lsave_dirty - make lsave node dirty.
996 * @c: UBIFS file-system description object
997 * @lnum: LEB number where lsave was written
998 * @offs: offset where lsave was written
1000 * This function is used by LPT garbage collection. LPT garbage collection is
1001 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1002 * simply involves marking all the nodes in the LEB being garbage-collected as
1003 * dirty. The dirty nodes are written next commit, after which the LEB is free
1006 * This function returns %0 on success and a negative error code on failure.
1008 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1010 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1011 return 0; /* This lsave node is obsolete */
1012 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1013 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1014 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1020 * make_node_dirty - make node dirty.
1021 * @c: UBIFS file-system description object
1022 * @node_type: LPT node type
1023 * @node_num: node number
1024 * @lnum: LEB number where node was written
1025 * @offs: offset where node was written
1027 * This function is used by LPT garbage collection. LPT garbage collection is
1028 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1029 * simply involves marking all the nodes in the LEB being garbage-collected as
1030 * dirty. The dirty nodes are written next commit, after which the LEB is free
1033 * This function returns %0 on success and a negative error code on failure.
1035 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1038 switch (node_type
) {
1039 case UBIFS_LPT_NNODE
:
1040 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1041 case UBIFS_LPT_PNODE
:
1042 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1043 case UBIFS_LPT_LTAB
:
1044 return make_ltab_dirty(c
, lnum
, offs
);
1045 case UBIFS_LPT_LSAVE
:
1046 return make_lsave_dirty(c
, lnum
, offs
);
1052 * get_lpt_node_len - return the length of a node based on its type.
1053 * @c: UBIFS file-system description object
1054 * @node_type: LPT node type
1056 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1058 switch (node_type
) {
1059 case UBIFS_LPT_NNODE
:
1061 case UBIFS_LPT_PNODE
:
1063 case UBIFS_LPT_LTAB
:
1065 case UBIFS_LPT_LSAVE
:
1072 * get_pad_len - return the length of padding in a buffer.
1073 * @c: UBIFS file-system description object
1075 * @len: length of buffer
1077 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1081 if (c
->min_io_size
== 1)
1083 offs
= c
->leb_size
- len
;
1084 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1089 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1090 * @c: UBIFS file-system description object
1092 * @node_num: node number is returned here
1094 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1097 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1098 int pos
= 0, node_type
;
1100 node_type
= ubifs_unpack_bits(c
, &addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1101 *node_num
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->pcnt_bits
);
1106 * is_a_node - determine if a buffer contains a node.
1107 * @c: UBIFS file-system description object
1109 * @len: length of buffer
1111 * This function returns %1 if the buffer contains a node or %0 if it does not.
1113 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1115 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1116 int pos
= 0, node_type
, node_len
;
1117 uint16_t crc
, calc_crc
;
1119 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1121 node_type
= ubifs_unpack_bits(c
, &addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1122 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1124 node_len
= get_lpt_node_len(c
, node_type
);
1125 if (!node_len
|| node_len
> len
)
1129 crc
= ubifs_unpack_bits(c
, &addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1130 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1131 node_len
- UBIFS_LPT_CRC_BYTES
);
1132 if (crc
!= calc_crc
)
1138 * lpt_gc_lnum - garbage collect a LPT LEB.
1139 * @c: UBIFS file-system description object
1140 * @lnum: LEB number to garbage collect
1142 * LPT garbage collection is used only for the "big" LPT model
1143 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1144 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1145 * next commit, after which the LEB is free to be reused.
1147 * This function returns %0 on success and a negative error code on failure.
1149 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1151 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1152 void *buf
= c
->lpt_buf
;
1154 dbg_lp("LEB %d", lnum
);
1156 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1161 if (!is_a_node(c
, buf
, len
)) {
1164 pad_len
= get_pad_len(c
, buf
, len
);
1172 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1173 node_len
= get_lpt_node_len(c
, node_type
);
1174 offs
= c
->leb_size
- len
;
1175 ubifs_assert(c
, node_len
!= 0);
1176 mutex_lock(&c
->lp_mutex
);
1177 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1178 mutex_unlock(&c
->lp_mutex
);
1188 * lpt_gc - LPT garbage collection.
1189 * @c: UBIFS file-system description object
1191 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1192 * Returns %0 on success and a negative error code on failure.
1194 static int lpt_gc(struct ubifs_info
*c
)
1196 int i
, lnum
= -1, dirty
= 0;
1198 mutex_lock(&c
->lp_mutex
);
1199 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1200 ubifs_assert(c
, !c
->ltab
[i
].tgc
);
1201 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1202 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1204 if (c
->ltab
[i
].dirty
> dirty
) {
1205 dirty
= c
->ltab
[i
].dirty
;
1206 lnum
= i
+ c
->lpt_first
;
1209 mutex_unlock(&c
->lp_mutex
);
1212 return lpt_gc_lnum(c
, lnum
);
1216 * ubifs_lpt_start_commit - UBIFS commit starts.
1217 * @c: the UBIFS file-system description object
1219 * This function has to be called when UBIFS starts the commit operation.
1220 * This function "freezes" all currently dirty LEB properties and does not
1221 * change them anymore. Further changes are saved and tracked separately
1222 * because they are not part of this commit. This function returns zero in case
1223 * of success and a negative error code in case of failure.
1225 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1231 mutex_lock(&c
->lp_mutex
);
1232 err
= dbg_chk_lpt_free_spc(c
);
1235 err
= dbg_check_ltab(c
);
1239 if (c
->check_lpt_free
) {
1241 * We ensure there is enough free space in
1242 * ubifs_lpt_post_commit() by marking nodes dirty. That
1243 * information is lost when we unmount, so we also need
1244 * to check free space once after mounting also.
1246 c
->check_lpt_free
= 0;
1247 while (need_write_all(c
)) {
1248 mutex_unlock(&c
->lp_mutex
);
1252 mutex_lock(&c
->lp_mutex
);
1258 if (!c
->dirty_pn_cnt
) {
1259 dbg_cmt("no cnodes to commit");
1264 if (!c
->big_lpt
&& need_write_all(c
)) {
1265 /* If needed, write everything */
1266 err
= make_tree_dirty(c
);
1275 cnt
= get_cnodes_to_commit(c
);
1276 ubifs_assert(c
, cnt
!= 0);
1278 err
= layout_cnodes(c
);
1282 /* Copy the LPT's own lprops for end commit to write */
1283 memcpy(c
->ltab_cmt
, c
->ltab
,
1284 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1285 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1288 mutex_unlock(&c
->lp_mutex
);
1293 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1294 * @c: UBIFS file-system description object
1296 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1298 struct ubifs_cnode
*cnode
, *cnext
;
1300 cnext
= c
->lpt_cnext
;
1305 cnext
= cnode
->cnext
;
1306 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1309 cnode
->cnext
= NULL
;
1310 } while (cnext
!= c
->lpt_cnext
);
1311 c
->lpt_cnext
= NULL
;
1315 * ubifs_lpt_end_commit - finish the commit operation.
1316 * @c: the UBIFS file-system description object
1318 * This function has to be called when the commit operation finishes. It
1319 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1320 * the media. Returns zero in case of success and a negative error code in case
1323 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1332 err
= write_cnodes(c
);
1336 mutex_lock(&c
->lp_mutex
);
1337 free_obsolete_cnodes(c
);
1338 mutex_unlock(&c
->lp_mutex
);
1344 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1345 * @c: UBIFS file-system description object
1347 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1348 * commit for the "big" LPT model.
1350 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1354 mutex_lock(&c
->lp_mutex
);
1355 err
= lpt_tgc_end(c
);
1359 while (need_write_all(c
)) {
1360 mutex_unlock(&c
->lp_mutex
);
1364 mutex_lock(&c
->lp_mutex
);
1367 mutex_unlock(&c
->lp_mutex
);
1372 * first_nnode - find the first nnode in memory.
1373 * @c: UBIFS file-system description object
1374 * @hght: height of tree where nnode found is returned here
1376 * This function returns a pointer to the nnode found or %NULL if no nnode is
1377 * found. This function is a helper to 'ubifs_lpt_free()'.
1379 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1381 struct ubifs_nnode
*nnode
;
1388 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1390 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1391 if (nnode
->nbranch
[i
].nnode
) {
1393 nnode
= nnode
->nbranch
[i
].nnode
;
1405 * next_nnode - find the next nnode in memory.
1406 * @c: UBIFS file-system description object
1407 * @nnode: nnode from which to start.
1408 * @hght: height of tree where nnode is, is passed and returned here
1410 * This function returns a pointer to the nnode found or %NULL if no nnode is
1411 * found. This function is a helper to 'ubifs_lpt_free()'.
1413 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1414 struct ubifs_nnode
*nnode
, int *hght
)
1416 struct ubifs_nnode
*parent
;
1417 int iip
, h
, i
, found
;
1419 parent
= nnode
->parent
;
1422 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1426 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1427 nnode
= parent
->nbranch
[iip
].nnode
;
1435 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1437 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1438 if (nnode
->nbranch
[i
].nnode
) {
1440 nnode
= nnode
->nbranch
[i
].nnode
;
1452 * ubifs_lpt_free - free resources owned by the LPT.
1453 * @c: UBIFS file-system description object
1454 * @wr_only: free only resources used for writing
1456 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1458 struct ubifs_nnode
*nnode
;
1461 /* Free write-only things first */
1463 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1475 /* Now free the rest */
1477 nnode
= first_nnode(c
, &hght
);
1479 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1480 kfree(nnode
->nbranch
[i
].nnode
);
1481 nnode
= next_nnode(c
, nnode
, &hght
);
1483 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1484 kfree(c
->lpt_heap
[i
].arr
);
1485 kfree(c
->dirty_idx
.arr
);
1488 kfree(c
->lpt_nod_buf
);
1492 * Everything below is related to debugging.
1496 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1498 * @len: buffer length
1500 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1504 for (i
= 0; i
< len
; i
++)
1511 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1512 * @c: the UBIFS file-system description object
1513 * @lnum: LEB number where nnode was written
1514 * @offs: offset where nnode was written
1516 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1518 struct ubifs_nnode
*nnode
;
1521 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1522 nnode
= first_nnode(c
, &hght
);
1523 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1524 struct ubifs_nbranch
*branch
;
1527 if (nnode
->parent
) {
1528 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1529 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1531 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1535 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1537 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1546 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1547 * @c: the UBIFS file-system description object
1548 * @lnum: LEB number where pnode was written
1549 * @offs: offset where pnode was written
1551 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1555 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1556 for (i
= 0; i
< cnt
; i
++) {
1557 struct ubifs_pnode
*pnode
;
1558 struct ubifs_nbranch
*branch
;
1561 pnode
= pnode_lookup(c
, i
);
1563 return PTR_ERR(pnode
);
1564 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1565 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1567 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1575 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1576 * @c: the UBIFS file-system description object
1577 * @lnum: LEB number where ltab node was written
1578 * @offs: offset where ltab node was written
1580 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1582 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1584 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1588 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1589 * @c: the UBIFS file-system description object
1590 * @lnum: LEB number where lsave node was written
1591 * @offs: offset where lsave node was written
1593 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1595 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1597 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1601 * dbg_is_node_dirty - determine if a node is dirty.
1602 * @c: the UBIFS file-system description object
1603 * @node_type: node type
1604 * @lnum: LEB number where node was written
1605 * @offs: offset where node was written
1607 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1610 switch (node_type
) {
1611 case UBIFS_LPT_NNODE
:
1612 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1613 case UBIFS_LPT_PNODE
:
1614 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1615 case UBIFS_LPT_LTAB
:
1616 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1617 case UBIFS_LPT_LSAVE
:
1618 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1624 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1625 * @c: the UBIFS file-system description object
1626 * @lnum: LEB number where node was written
1628 * This function returns %0 on success and a negative error code on failure.
1630 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1632 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1636 if (!dbg_is_chk_lprops(c
))
1639 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1641 ubifs_err(c
, "cannot allocate memory for ltab checking");
1645 dbg_lp("LEB %d", lnum
);
1647 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1652 if (!is_a_node(c
, p
, len
)) {
1655 pad_len
= get_pad_len(c
, p
, len
);
1662 if (!dbg_is_all_ff(p
, len
)) {
1663 ubifs_err(c
, "invalid empty space in LEB %d at %d",
1664 lnum
, c
->leb_size
- len
);
1667 i
= lnum
- c
->lpt_first
;
1668 if (len
!= c
->ltab
[i
].free
) {
1669 ubifs_err(c
, "invalid free space in LEB %d (free %d, expected %d)",
1670 lnum
, len
, c
->ltab
[i
].free
);
1673 if (dirty
!= c
->ltab
[i
].dirty
) {
1674 ubifs_err(c
, "invalid dirty space in LEB %d (dirty %d, expected %d)",
1675 lnum
, dirty
, c
->ltab
[i
].dirty
);
1680 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1681 node_len
= get_lpt_node_len(c
, node_type
);
1682 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1696 * dbg_check_ltab - check the free and dirty space in the ltab.
1697 * @c: the UBIFS file-system description object
1699 * This function returns %0 on success and a negative error code on failure.
1701 int dbg_check_ltab(struct ubifs_info
*c
)
1703 int lnum
, err
, i
, cnt
;
1705 if (!dbg_is_chk_lprops(c
))
1708 /* Bring the entire tree into memory */
1709 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1710 for (i
= 0; i
< cnt
; i
++) {
1711 struct ubifs_pnode
*pnode
;
1713 pnode
= pnode_lookup(c
, i
);
1715 return PTR_ERR(pnode
);
1720 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1724 /* Check each LEB */
1725 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1726 err
= dbg_check_ltab_lnum(c
, lnum
);
1728 ubifs_err(c
, "failed at LEB %d", lnum
);
1733 dbg_lp("succeeded");
1738 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1739 * @c: the UBIFS file-system description object
1741 * This function returns %0 on success and a negative error code on failure.
1743 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1748 if (!dbg_is_chk_lprops(c
))
1751 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1752 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1754 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1755 free
+= c
->leb_size
- c
->nhead_offs
;
1756 else if (c
->ltab
[i
].free
== c
->leb_size
)
1757 free
+= c
->leb_size
;
1759 if (free
< c
->lpt_sz
) {
1760 ubifs_err(c
, "LPT space error: free %lld lpt_sz %lld",
1762 ubifs_dump_lpt_info(c
);
1763 ubifs_dump_lpt_lebs(c
);
1771 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1772 * @c: the UBIFS file-system description object
1773 * @action: what to do
1774 * @len: length written
1776 * This function returns %0 on success and a negative error code on failure.
1777 * The @action argument may be one of:
1778 * o %0 - LPT debugging checking starts, initialize debugging variables;
1779 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1780 * o %2 - switched to a different LEB and wasted @len bytes;
1781 * o %3 - check that we've written the right number of bytes.
1782 * o %4 - wasted @len bytes;
1784 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1786 struct ubifs_debug_info
*d
= c
->dbg
;
1787 long long chk_lpt_sz
, lpt_sz
;
1790 if (!dbg_is_chk_lprops(c
))
1797 d
->chk_lpt_lebs
= 0;
1798 d
->chk_lpt_wastage
= 0;
1799 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1800 ubifs_err(c
, "dirty pnodes %d exceed max %d",
1801 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1804 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1805 ubifs_err(c
, "dirty nnodes %d exceed max %d",
1806 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1811 d
->chk_lpt_sz
+= len
;
1814 d
->chk_lpt_sz
+= len
;
1815 d
->chk_lpt_wastage
+= len
;
1816 d
->chk_lpt_lebs
+= 1;
1819 chk_lpt_sz
= c
->leb_size
;
1820 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1821 chk_lpt_sz
+= len
- c
->nhead_offs
;
1822 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1823 ubifs_err(c
, "LPT wrote %lld but space used was %lld",
1824 d
->chk_lpt_sz
, chk_lpt_sz
);
1827 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1828 ubifs_err(c
, "LPT wrote %lld but lpt_sz is %lld",
1829 d
->chk_lpt_sz
, c
->lpt_sz
);
1832 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1833 ubifs_err(c
, "LPT layout size %lld but wrote %lld",
1834 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1837 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1838 ubifs_err(c
, "LPT new nhead offs: expected %d was %d",
1839 d
->new_nhead_offs
, len
);
1842 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1843 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1844 lpt_sz
+= c
->ltab_sz
;
1846 lpt_sz
+= c
->lsave_sz
;
1847 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1848 ubifs_err(c
, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1849 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1853 ubifs_dump_lpt_info(c
);
1854 ubifs_dump_lpt_lebs(c
);
1857 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1859 d
->chk_lpt_wastage
= 0;
1860 d
->chk_lpt_lebs
= 0;
1861 d
->new_nhead_offs
= len
;
1864 d
->chk_lpt_sz
+= len
;
1865 d
->chk_lpt_wastage
+= len
;
1873 * dump_lpt_leb - dump an LPT LEB.
1874 * @c: UBIFS file-system description object
1875 * @lnum: LEB number to dump
1877 * This function dumps an LEB from LPT area. Nodes in this area are very
1878 * different to nodes in the main area (e.g., they do not have common headers,
1879 * they do not have 8-byte alignments, etc), so we have a separate function to
1880 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1882 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1884 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1887 pr_err("(pid %d) start dumping LEB %d\n", current
->pid
, lnum
);
1888 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1890 ubifs_err(c
, "cannot allocate memory to dump LPT");
1894 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1899 offs
= c
->leb_size
- len
;
1900 if (!is_a_node(c
, p
, len
)) {
1903 pad_len
= get_pad_len(c
, p
, len
);
1905 pr_err("LEB %d:%d, pad %d bytes\n",
1906 lnum
, offs
, pad_len
);
1912 pr_err("LEB %d:%d, free %d bytes\n",
1917 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1918 switch (node_type
) {
1919 case UBIFS_LPT_PNODE
:
1921 node_len
= c
->pnode_sz
;
1923 pr_err("LEB %d:%d, pnode num %d\n",
1924 lnum
, offs
, node_num
);
1926 pr_err("LEB %d:%d, pnode\n", lnum
, offs
);
1929 case UBIFS_LPT_NNODE
:
1932 struct ubifs_nnode nnode
;
1934 node_len
= c
->nnode_sz
;
1936 pr_err("LEB %d:%d, nnode num %d, ",
1937 lnum
, offs
, node_num
);
1939 pr_err("LEB %d:%d, nnode, ",
1941 err
= ubifs_unpack_nnode(c
, p
, &nnode
);
1943 pr_err("failed to unpack_node, error %d\n",
1947 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1948 pr_cont("%d:%d", nnode
.nbranch
[i
].lnum
,
1949 nnode
.nbranch
[i
].offs
);
1950 if (i
!= UBIFS_LPT_FANOUT
- 1)
1956 case UBIFS_LPT_LTAB
:
1957 node_len
= c
->ltab_sz
;
1958 pr_err("LEB %d:%d, ltab\n", lnum
, offs
);
1960 case UBIFS_LPT_LSAVE
:
1961 node_len
= c
->lsave_sz
;
1962 pr_err("LEB %d:%d, lsave len\n", lnum
, offs
);
1965 ubifs_err(c
, "LPT node type %d not recognized", node_type
);
1973 pr_err("(pid %d) finish dumping LEB %d\n", current
->pid
, lnum
);
1980 * ubifs_dump_lpt_lebs - dump LPT lebs.
1981 * @c: UBIFS file-system description object
1983 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1986 void ubifs_dump_lpt_lebs(const struct ubifs_info
*c
)
1990 pr_err("(pid %d) start dumping all LPT LEBs\n", current
->pid
);
1991 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1992 dump_lpt_leb(c
, i
+ c
->lpt_first
);
1993 pr_err("(pid %d) finish dumping all LPT LEBs\n", current
->pid
);
1997 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1998 * @c: UBIFS file-system description object
2000 * This is a debugging version for 'populate_lsave()' which populates lsave
2001 * with random LEBs instead of useful LEBs, which is good for test coverage.
2002 * Returns zero if lsave has not been populated (this debugging feature is
2003 * disabled) an non-zero if lsave has been populated.
2005 static int dbg_populate_lsave(struct ubifs_info
*c
)
2007 struct ubifs_lprops
*lprops
;
2008 struct ubifs_lpt_heap
*heap
;
2011 if (!dbg_is_chk_gen(c
))
2013 if (prandom_u32() & 3)
2016 for (i
= 0; i
< c
->lsave_cnt
; i
++)
2017 c
->lsave
[i
] = c
->main_first
;
2019 list_for_each_entry(lprops
, &c
->empty_list
, list
)
2020 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2021 list_for_each_entry(lprops
, &c
->freeable_list
, list
)
2022 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2023 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
)
2024 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2026 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
2027 for (i
= 0; i
< heap
->cnt
; i
++)
2028 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2029 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
2030 for (i
= 0; i
< heap
->cnt
; i
++)
2031 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2032 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
2033 for (i
= 0; i
< heap
->cnt
; i
++)
2034 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;