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 * @nnode: nnode at which to start
39 * This function returns the first dirty cnode or %NULL if there is not one.
41 static struct ubifs_cnode
*first_dirty_cnode(struct ubifs_nnode
*nnode
)
47 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
48 struct ubifs_cnode
*cnode
;
50 cnode
= nnode
->nbranch
[i
].cnode
;
52 test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
53 if (cnode
->level
== 0)
55 nnode
= (struct ubifs_nnode
*)cnode
;
61 return (struct ubifs_cnode
*)nnode
;
66 * next_dirty_cnode - find next dirty cnode.
67 * @cnode: cnode from which to begin searching
69 * This function returns the next dirty cnode or %NULL if there is not one.
71 static struct ubifs_cnode
*next_dirty_cnode(struct ubifs_cnode
*cnode
)
73 struct ubifs_nnode
*nnode
;
77 nnode
= cnode
->parent
;
80 for (i
= cnode
->iip
+ 1; i
< UBIFS_LPT_FANOUT
; i
++) {
81 cnode
= nnode
->nbranch
[i
].cnode
;
82 if (cnode
&& test_bit(DIRTY_CNODE
, &cnode
->flags
)) {
83 if (cnode
->level
== 0)
84 return cnode
; /* cnode is a pnode */
85 /* cnode is a nnode */
86 return first_dirty_cnode((struct ubifs_nnode
*)cnode
);
89 return (struct ubifs_cnode
*)nnode
;
93 * get_cnodes_to_commit - create list of dirty cnodes to commit.
94 * @c: UBIFS file-system description object
96 * This function returns the number of cnodes to commit.
98 static int get_cnodes_to_commit(struct ubifs_info
*c
)
100 struct ubifs_cnode
*cnode
, *cnext
;
106 if (!test_bit(DIRTY_CNODE
, &c
->nroot
->flags
))
109 c
->lpt_cnext
= first_dirty_cnode(c
->nroot
);
110 cnode
= c
->lpt_cnext
;
115 ubifs_assert(!test_bit(COW_CNODE
, &cnode
->flags
));
116 __set_bit(COW_CNODE
, &cnode
->flags
);
117 cnext
= next_dirty_cnode(cnode
);
119 cnode
->cnext
= c
->lpt_cnext
;
122 cnode
->cnext
= cnext
;
126 dbg_cmt("committing %d cnodes", cnt
);
127 dbg_lp("committing %d cnodes", cnt
);
128 ubifs_assert(cnt
== c
->dirty_nn_cnt
+ c
->dirty_pn_cnt
);
133 * upd_ltab - update LPT LEB properties.
134 * @c: UBIFS file-system description object
136 * @free: amount of free space
137 * @dirty: amount of dirty space to add
139 static void upd_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
141 dbg_lp("LEB %d free %d dirty %d to %d +%d",
142 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
143 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
144 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
145 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
146 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
150 * alloc_lpt_leb - allocate an LPT LEB that is empty.
151 * @c: UBIFS file-system description object
152 * @lnum: LEB number is passed and returned here
154 * This function finds the next empty LEB in the ltab starting from @lnum. If a
155 * an empty LEB is found it is returned in @lnum and the function returns %0.
156 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
157 * never to run out of space.
159 static int alloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
163 n
= *lnum
- c
->lpt_first
+ 1;
164 for (i
= n
; i
< c
->lpt_lebs
; i
++) {
165 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
167 if (c
->ltab
[i
].free
== c
->leb_size
) {
169 *lnum
= i
+ c
->lpt_first
;
174 for (i
= 0; i
< n
; i
++) {
175 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
177 if (c
->ltab
[i
].free
== c
->leb_size
) {
179 *lnum
= i
+ c
->lpt_first
;
187 * layout_cnodes - layout cnodes for commit.
188 * @c: UBIFS file-system description object
190 * This function returns %0 on success and a negative error code on failure.
192 static int layout_cnodes(struct ubifs_info
*c
)
194 int lnum
, offs
, len
, alen
, done_lsave
, done_ltab
, err
;
195 struct ubifs_cnode
*cnode
;
197 err
= dbg_chk_lpt_sz(c
, 0, 0);
200 cnode
= c
->lpt_cnext
;
203 lnum
= c
->nhead_lnum
;
204 offs
= c
->nhead_offs
;
205 /* Try to place lsave and ltab nicely */
206 done_lsave
= !c
->big_lpt
;
208 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
210 c
->lsave_lnum
= lnum
;
211 c
->lsave_offs
= offs
;
213 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
216 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
221 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
227 c
->dirty_nn_cnt
-= 1;
230 c
->dirty_pn_cnt
-= 1;
232 while (offs
+ len
> c
->leb_size
) {
233 alen
= ALIGN(offs
, c
->min_io_size
);
234 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
235 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
236 err
= alloc_lpt_leb(c
, &lnum
);
240 ubifs_assert(lnum
>= c
->lpt_first
&&
241 lnum
<= c
->lpt_last
);
242 /* Try to place lsave and ltab nicely */
245 c
->lsave_lnum
= lnum
;
246 c
->lsave_offs
= offs
;
248 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
256 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
262 cnode
->parent
->nbranch
[cnode
->iip
].lnum
= lnum
;
263 cnode
->parent
->nbranch
[cnode
->iip
].offs
= offs
;
269 dbg_chk_lpt_sz(c
, 1, len
);
270 cnode
= cnode
->cnext
;
271 } while (cnode
&& cnode
!= c
->lpt_cnext
);
273 /* Make sure to place LPT's save table */
275 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
276 alen
= ALIGN(offs
, c
->min_io_size
);
277 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
278 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
279 err
= alloc_lpt_leb(c
, &lnum
);
283 ubifs_assert(lnum
>= c
->lpt_first
&&
284 lnum
<= c
->lpt_last
);
287 c
->lsave_lnum
= lnum
;
288 c
->lsave_offs
= offs
;
290 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
293 /* Make sure to place LPT's own lprops table */
295 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
296 alen
= ALIGN(offs
, c
->min_io_size
);
297 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
298 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
299 err
= alloc_lpt_leb(c
, &lnum
);
303 ubifs_assert(lnum
>= c
->lpt_first
&&
304 lnum
<= c
->lpt_last
);
309 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
312 alen
= ALIGN(offs
, c
->min_io_size
);
313 upd_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- offs
);
314 dbg_chk_lpt_sz(c
, 4, alen
- offs
);
315 err
= dbg_chk_lpt_sz(c
, 3, alen
);
321 ubifs_err(c
, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
322 lnum
, offs
, len
, done_ltab
, done_lsave
);
323 ubifs_dump_lpt_info(c
);
324 ubifs_dump_lpt_lebs(c
);
330 * realloc_lpt_leb - allocate an LPT LEB that is empty.
331 * @c: UBIFS file-system description object
332 * @lnum: LEB number is passed and returned here
334 * This function duplicates exactly the results of the function alloc_lpt_leb.
335 * It is used during end commit to reallocate the same LEB numbers that were
336 * allocated by alloc_lpt_leb during start commit.
338 * This function finds the next LEB that was allocated by the alloc_lpt_leb
339 * function starting from @lnum. If a LEB is found it is returned in @lnum and
340 * the function returns %0. Otherwise the function returns -ENOSPC.
341 * Note however, that LPT is designed never to run out of space.
343 static int realloc_lpt_leb(struct ubifs_info
*c
, int *lnum
)
347 n
= *lnum
- c
->lpt_first
+ 1;
348 for (i
= n
; i
< c
->lpt_lebs
; i
++)
349 if (c
->ltab
[i
].cmt
) {
351 *lnum
= i
+ c
->lpt_first
;
355 for (i
= 0; i
< n
; i
++)
356 if (c
->ltab
[i
].cmt
) {
358 *lnum
= i
+ c
->lpt_first
;
365 * write_cnodes - write cnodes for commit.
366 * @c: UBIFS file-system description object
368 * This function returns %0 on success and a negative error code on failure.
370 static int write_cnodes(struct ubifs_info
*c
)
372 int lnum
, offs
, len
, from
, err
, wlen
, alen
, done_ltab
, done_lsave
;
373 struct ubifs_cnode
*cnode
;
374 void *buf
= c
->lpt_buf
;
376 cnode
= c
->lpt_cnext
;
379 lnum
= c
->nhead_lnum
;
380 offs
= c
->nhead_offs
;
382 /* Ensure empty LEB is unmapped */
384 err
= ubifs_leb_unmap(c
, lnum
);
388 /* Try to place lsave and ltab nicely */
389 done_lsave
= !c
->big_lpt
;
391 if (!done_lsave
&& offs
+ c
->lsave_sz
<= c
->leb_size
) {
393 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
395 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
398 if (offs
+ c
->ltab_sz
<= c
->leb_size
) {
400 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
402 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
405 /* Loop for each cnode */
411 while (offs
+ len
> c
->leb_size
) {
414 alen
= ALIGN(wlen
, c
->min_io_size
);
415 memset(buf
+ offs
, 0xff, alen
- wlen
);
416 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
,
421 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
422 err
= realloc_lpt_leb(c
, &lnum
);
426 ubifs_assert(lnum
>= c
->lpt_first
&&
427 lnum
<= c
->lpt_last
);
428 err
= ubifs_leb_unmap(c
, lnum
);
431 /* Try to place lsave and ltab nicely */
434 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
436 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
441 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
443 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
449 ubifs_pack_nnode(c
, buf
+ offs
,
450 (struct ubifs_nnode
*)cnode
);
452 ubifs_pack_pnode(c
, buf
+ offs
,
453 (struct ubifs_pnode
*)cnode
);
455 * The reason for the barriers is the same as in case of TNC.
456 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
457 * 'dirty_cow_pnode()' are the functions for which this is
460 clear_bit(DIRTY_CNODE
, &cnode
->flags
);
461 smp_mb__before_atomic();
462 clear_bit(COW_CNODE
, &cnode
->flags
);
463 smp_mb__after_atomic();
465 dbg_chk_lpt_sz(c
, 1, len
);
466 cnode
= cnode
->cnext
;
467 } while (cnode
&& cnode
!= c
->lpt_cnext
);
469 /* Make sure to place LPT's save table */
471 if (offs
+ c
->lsave_sz
> c
->leb_size
) {
473 alen
= ALIGN(wlen
, c
->min_io_size
);
474 memset(buf
+ offs
, 0xff, alen
- wlen
);
475 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
478 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
479 err
= realloc_lpt_leb(c
, &lnum
);
483 ubifs_assert(lnum
>= c
->lpt_first
&&
484 lnum
<= c
->lpt_last
);
485 err
= ubifs_leb_unmap(c
, lnum
);
490 ubifs_pack_lsave(c
, buf
+ offs
, c
->lsave
);
492 dbg_chk_lpt_sz(c
, 1, c
->lsave_sz
);
495 /* Make sure to place LPT's own lprops table */
497 if (offs
+ c
->ltab_sz
> c
->leb_size
) {
499 alen
= ALIGN(wlen
, c
->min_io_size
);
500 memset(buf
+ offs
, 0xff, alen
- wlen
);
501 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
504 dbg_chk_lpt_sz(c
, 2, c
->leb_size
- offs
);
505 err
= realloc_lpt_leb(c
, &lnum
);
509 ubifs_assert(lnum
>= c
->lpt_first
&&
510 lnum
<= c
->lpt_last
);
511 err
= ubifs_leb_unmap(c
, lnum
);
515 ubifs_pack_ltab(c
, buf
+ offs
, c
->ltab_cmt
);
517 dbg_chk_lpt_sz(c
, 1, c
->ltab_sz
);
520 /* Write remaining data in buffer */
522 alen
= ALIGN(wlen
, c
->min_io_size
);
523 memset(buf
+ offs
, 0xff, alen
- wlen
);
524 err
= ubifs_leb_write(c
, lnum
, buf
+ from
, from
, alen
);
528 dbg_chk_lpt_sz(c
, 4, alen
- wlen
);
529 err
= dbg_chk_lpt_sz(c
, 3, ALIGN(offs
, c
->min_io_size
));
533 c
->nhead_lnum
= lnum
;
534 c
->nhead_offs
= ALIGN(offs
, c
->min_io_size
);
536 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
537 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
538 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
540 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
545 ubifs_err(c
, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
546 lnum
, offs
, len
, done_ltab
, done_lsave
);
547 ubifs_dump_lpt_info(c
);
548 ubifs_dump_lpt_lebs(c
);
554 * next_pnode_to_dirty - find next pnode to dirty.
555 * @c: UBIFS file-system description object
558 * This function returns the next pnode to dirty or %NULL if there are no more
559 * pnodes. Note that pnodes that have never been written (lnum == 0) are
562 static struct ubifs_pnode
*next_pnode_to_dirty(struct ubifs_info
*c
,
563 struct ubifs_pnode
*pnode
)
565 struct ubifs_nnode
*nnode
;
568 /* Try to go right */
569 nnode
= pnode
->parent
;
570 for (iip
= pnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
571 if (nnode
->nbranch
[iip
].lnum
)
572 return ubifs_get_pnode(c
, nnode
, iip
);
575 /* Go up while can't go right */
577 iip
= nnode
->iip
+ 1;
578 nnode
= nnode
->parent
;
581 for (; iip
< UBIFS_LPT_FANOUT
; iip
++) {
582 if (nnode
->nbranch
[iip
].lnum
)
585 } while (iip
>= UBIFS_LPT_FANOUT
);
588 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
590 return (void *)nnode
;
592 /* Go down to level 1 */
593 while (nnode
->level
> 1) {
594 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++) {
595 if (nnode
->nbranch
[iip
].lnum
)
598 if (iip
>= UBIFS_LPT_FANOUT
) {
600 * Should not happen, but we need to keep going
605 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
607 return (void *)nnode
;
610 for (iip
= 0; iip
< UBIFS_LPT_FANOUT
; iip
++)
611 if (nnode
->nbranch
[iip
].lnum
)
613 if (iip
>= UBIFS_LPT_FANOUT
)
614 /* Should not happen, but we need to keep going if it does */
616 return ubifs_get_pnode(c
, nnode
, iip
);
620 * pnode_lookup - lookup a pnode in the LPT.
621 * @c: UBIFS file-system description object
622 * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT))
624 * This function returns a pointer to the pnode on success or a negative
625 * error code on failure.
627 static struct ubifs_pnode
*pnode_lookup(struct ubifs_info
*c
, int i
)
629 int err
, h
, iip
, shft
;
630 struct ubifs_nnode
*nnode
;
633 err
= ubifs_read_nnode(c
, NULL
, 0);
637 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
639 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
640 for (h
= 1; h
< c
->lpt_hght
; h
++) {
641 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
642 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
643 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
645 return ERR_CAST(nnode
);
647 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
648 return ubifs_get_pnode(c
, nnode
, iip
);
652 * add_pnode_dirt - add dirty space to LPT LEB properties.
653 * @c: UBIFS file-system description object
654 * @pnode: pnode for which to add dirt
656 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
658 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
663 * do_make_pnode_dirty - mark a pnode dirty.
664 * @c: UBIFS file-system description object
665 * @pnode: pnode to mark dirty
667 static void do_make_pnode_dirty(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
669 /* Assumes cnext list is empty i.e. not called during commit */
670 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
671 struct ubifs_nnode
*nnode
;
673 c
->dirty_pn_cnt
+= 1;
674 add_pnode_dirt(c
, pnode
);
675 /* Mark parent and ancestors dirty too */
676 nnode
= pnode
->parent
;
678 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
679 c
->dirty_nn_cnt
+= 1;
680 ubifs_add_nnode_dirt(c
, nnode
);
681 nnode
= nnode
->parent
;
689 * make_tree_dirty - mark the entire LEB properties tree dirty.
690 * @c: UBIFS file-system description object
692 * This function is used by the "small" LPT model to cause the entire LEB
693 * properties tree to be written. The "small" LPT model does not use LPT
694 * garbage collection because it is more efficient to write the entire tree
695 * (because it is small).
697 * This function returns %0 on success and a negative error code on failure.
699 static int make_tree_dirty(struct ubifs_info
*c
)
701 struct ubifs_pnode
*pnode
;
703 pnode
= pnode_lookup(c
, 0);
705 return PTR_ERR(pnode
);
708 do_make_pnode_dirty(c
, pnode
);
709 pnode
= next_pnode_to_dirty(c
, pnode
);
711 return PTR_ERR(pnode
);
717 * need_write_all - determine if the LPT area is running out of free space.
718 * @c: UBIFS file-system description object
720 * This function returns %1 if the LPT area is running out of free space and %0
723 static int need_write_all(struct ubifs_info
*c
)
728 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
729 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
730 free
+= c
->leb_size
- c
->nhead_offs
;
731 else if (c
->ltab
[i
].free
== c
->leb_size
)
733 else if (c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
736 /* Less than twice the size left */
737 if (free
<= c
->lpt_sz
* 2)
743 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
744 * @c: UBIFS file-system description object
746 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
747 * free space and so may be reused as soon as the next commit is completed.
748 * This function is called during start commit to mark LPT LEBs for trivial GC.
750 static void lpt_tgc_start(struct ubifs_info
*c
)
754 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
755 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
757 if (c
->ltab
[i
].dirty
> 0 &&
758 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
) {
760 c
->ltab
[i
].free
= c
->leb_size
;
761 c
->ltab
[i
].dirty
= 0;
762 dbg_lp("LEB %d", i
+ c
->lpt_first
);
768 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
769 * @c: UBIFS file-system description object
771 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
772 * free space and so may be reused as soon as the next commit is completed.
773 * This function is called after the commit is completed (master node has been
774 * written) and un-maps LPT LEBs that were marked for trivial GC.
776 static int lpt_tgc_end(struct ubifs_info
*c
)
780 for (i
= 0; i
< c
->lpt_lebs
; i
++)
781 if (c
->ltab
[i
].tgc
) {
782 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
786 dbg_lp("LEB %d", i
+ c
->lpt_first
);
792 * populate_lsave - fill the lsave array with important LEB numbers.
793 * @c: the UBIFS file-system description object
795 * This function is only called for the "big" model. It records a small number
796 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
797 * most important to least important): empty, freeable, freeable index, dirty
798 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
799 * their pnodes into memory. That will stop us from having to scan the LPT
800 * straight away. For the "small" model we assume that scanning the LPT is no
803 static void populate_lsave(struct ubifs_info
*c
)
805 struct ubifs_lprops
*lprops
;
806 struct ubifs_lpt_heap
*heap
;
809 ubifs_assert(c
->big_lpt
);
810 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
811 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
812 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
815 if (dbg_populate_lsave(c
))
818 list_for_each_entry(lprops
, &c
->empty_list
, list
) {
819 c
->lsave
[cnt
++] = lprops
->lnum
;
820 if (cnt
>= c
->lsave_cnt
)
823 list_for_each_entry(lprops
, &c
->freeable_list
, list
) {
824 c
->lsave
[cnt
++] = lprops
->lnum
;
825 if (cnt
>= c
->lsave_cnt
)
828 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
) {
829 c
->lsave
[cnt
++] = lprops
->lnum
;
830 if (cnt
>= c
->lsave_cnt
)
833 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
834 for (i
= 0; i
< heap
->cnt
; i
++) {
835 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
836 if (cnt
>= c
->lsave_cnt
)
839 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
840 for (i
= 0; i
< heap
->cnt
; i
++) {
841 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
842 if (cnt
>= c
->lsave_cnt
)
845 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
846 for (i
= 0; i
< heap
->cnt
; i
++) {
847 c
->lsave
[cnt
++] = heap
->arr
[i
]->lnum
;
848 if (cnt
>= c
->lsave_cnt
)
851 /* Fill it up completely */
852 while (cnt
< c
->lsave_cnt
)
853 c
->lsave
[cnt
++] = c
->main_first
;
857 * nnode_lookup - lookup a nnode in the LPT.
858 * @c: UBIFS file-system description object
861 * This function returns a pointer to the nnode on success or a negative
862 * error code on failure.
864 static struct ubifs_nnode
*nnode_lookup(struct ubifs_info
*c
, int i
)
867 struct ubifs_nnode
*nnode
;
870 err
= ubifs_read_nnode(c
, NULL
, 0);
876 iip
= i
& (UBIFS_LPT_FANOUT
- 1);
877 i
>>= UBIFS_LPT_FANOUT_SHIFT
;
880 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
888 * make_nnode_dirty - find a nnode and, if found, make it dirty.
889 * @c: UBIFS file-system description object
890 * @node_num: nnode number of nnode to make dirty
891 * @lnum: LEB number where nnode was written
892 * @offs: offset where nnode was written
894 * This function is used by LPT garbage collection. LPT garbage collection is
895 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
896 * simply involves marking all the nodes in the LEB being garbage-collected as
897 * dirty. The dirty nodes are written next commit, after which the LEB is free
900 * This function returns %0 on success and a negative error code on failure.
902 static int make_nnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
905 struct ubifs_nnode
*nnode
;
907 nnode
= nnode_lookup(c
, node_num
);
909 return PTR_ERR(nnode
);
911 struct ubifs_nbranch
*branch
;
913 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
914 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
915 return 0; /* nnode is obsolete */
916 } else if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
917 return 0; /* nnode is obsolete */
918 /* Assumes cnext list is empty i.e. not called during commit */
919 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
920 c
->dirty_nn_cnt
+= 1;
921 ubifs_add_nnode_dirt(c
, nnode
);
922 /* Mark parent and ancestors dirty too */
923 nnode
= nnode
->parent
;
925 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
926 c
->dirty_nn_cnt
+= 1;
927 ubifs_add_nnode_dirt(c
, nnode
);
928 nnode
= nnode
->parent
;
937 * make_pnode_dirty - find a pnode and, if found, make it dirty.
938 * @c: UBIFS file-system description object
939 * @node_num: pnode number of pnode to make dirty
940 * @lnum: LEB number where pnode was written
941 * @offs: offset where pnode was written
943 * This function is used by LPT garbage collection. LPT garbage collection is
944 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
945 * simply involves marking all the nodes in the LEB being garbage-collected as
946 * dirty. The dirty nodes are written next commit, after which the LEB is free
949 * This function returns %0 on success and a negative error code on failure.
951 static int make_pnode_dirty(struct ubifs_info
*c
, int node_num
, int lnum
,
954 struct ubifs_pnode
*pnode
;
955 struct ubifs_nbranch
*branch
;
957 pnode
= pnode_lookup(c
, node_num
);
959 return PTR_ERR(pnode
);
960 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
961 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
963 do_make_pnode_dirty(c
, pnode
);
968 * make_ltab_dirty - make ltab node dirty.
969 * @c: UBIFS file-system description object
970 * @lnum: LEB number where ltab was written
971 * @offs: offset where ltab was written
973 * This function is used by LPT garbage collection. LPT garbage collection is
974 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
975 * simply involves marking all the nodes in the LEB being garbage-collected as
976 * dirty. The dirty nodes are written next commit, after which the LEB is free
979 * This function returns %0 on success and a negative error code on failure.
981 static int make_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
983 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
984 return 0; /* This ltab node is obsolete */
985 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
986 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
987 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
993 * make_lsave_dirty - make lsave node dirty.
994 * @c: UBIFS file-system description object
995 * @lnum: LEB number where lsave was written
996 * @offs: offset where lsave was written
998 * This function is used by LPT garbage collection. LPT garbage collection is
999 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1000 * simply involves marking all the nodes in the LEB being garbage-collected as
1001 * dirty. The dirty nodes are written next commit, after which the LEB is free
1004 * This function returns %0 on success and a negative error code on failure.
1006 static int make_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1008 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1009 return 0; /* This lsave node is obsolete */
1010 if (!(c
->lpt_drty_flgs
& LSAVE_DIRTY
)) {
1011 c
->lpt_drty_flgs
|= LSAVE_DIRTY
;
1012 ubifs_add_lpt_dirt(c
, c
->lsave_lnum
, c
->lsave_sz
);
1018 * make_node_dirty - make node dirty.
1019 * @c: UBIFS file-system description object
1020 * @node_type: LPT node type
1021 * @node_num: node number
1022 * @lnum: LEB number where node was written
1023 * @offs: offset where node was written
1025 * This function is used by LPT garbage collection. LPT garbage collection is
1026 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1027 * simply involves marking all the nodes in the LEB being garbage-collected as
1028 * dirty. The dirty nodes are written next commit, after which the LEB is free
1031 * This function returns %0 on success and a negative error code on failure.
1033 static int make_node_dirty(struct ubifs_info
*c
, int node_type
, int node_num
,
1036 switch (node_type
) {
1037 case UBIFS_LPT_NNODE
:
1038 return make_nnode_dirty(c
, node_num
, lnum
, offs
);
1039 case UBIFS_LPT_PNODE
:
1040 return make_pnode_dirty(c
, node_num
, lnum
, offs
);
1041 case UBIFS_LPT_LTAB
:
1042 return make_ltab_dirty(c
, lnum
, offs
);
1043 case UBIFS_LPT_LSAVE
:
1044 return make_lsave_dirty(c
, lnum
, offs
);
1050 * get_lpt_node_len - return the length of a node based on its type.
1051 * @c: UBIFS file-system description object
1052 * @node_type: LPT node type
1054 static int get_lpt_node_len(const struct ubifs_info
*c
, int node_type
)
1056 switch (node_type
) {
1057 case UBIFS_LPT_NNODE
:
1059 case UBIFS_LPT_PNODE
:
1061 case UBIFS_LPT_LTAB
:
1063 case UBIFS_LPT_LSAVE
:
1070 * get_pad_len - return the length of padding in a buffer.
1071 * @c: UBIFS file-system description object
1073 * @len: length of buffer
1075 static int get_pad_len(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1079 if (c
->min_io_size
== 1)
1081 offs
= c
->leb_size
- len
;
1082 pad_len
= ALIGN(offs
, c
->min_io_size
) - offs
;
1087 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1088 * @c: UBIFS file-system description object
1090 * @node_num: node number is returned here
1092 static int get_lpt_node_type(const struct ubifs_info
*c
, uint8_t *buf
,
1095 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1096 int pos
= 0, node_type
;
1098 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1099 *node_num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1104 * is_a_node - determine if a buffer contains a node.
1105 * @c: UBIFS file-system description object
1107 * @len: length of buffer
1109 * This function returns %1 if the buffer contains a node or %0 if it does not.
1111 static int is_a_node(const struct ubifs_info
*c
, uint8_t *buf
, int len
)
1113 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1114 int pos
= 0, node_type
, node_len
;
1115 uint16_t crc
, calc_crc
;
1117 if (len
< UBIFS_LPT_CRC_BYTES
+ (UBIFS_LPT_TYPE_BITS
+ 7) / 8)
1119 node_type
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_TYPE_BITS
);
1120 if (node_type
== UBIFS_LPT_NOT_A_NODE
)
1122 node_len
= get_lpt_node_len(c
, node_type
);
1123 if (!node_len
|| node_len
> len
)
1127 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
1128 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
1129 node_len
- UBIFS_LPT_CRC_BYTES
);
1130 if (crc
!= calc_crc
)
1136 * lpt_gc_lnum - garbage collect a LPT LEB.
1137 * @c: UBIFS file-system description object
1138 * @lnum: LEB number to garbage collect
1140 * LPT garbage collection is used only for the "big" LPT model
1141 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1142 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1143 * next commit, after which the LEB is free to be reused.
1145 * This function returns %0 on success and a negative error code on failure.
1147 static int lpt_gc_lnum(struct ubifs_info
*c
, int lnum
)
1149 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1150 void *buf
= c
->lpt_buf
;
1152 dbg_lp("LEB %d", lnum
);
1154 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1159 if (!is_a_node(c
, buf
, len
)) {
1162 pad_len
= get_pad_len(c
, buf
, len
);
1170 node_type
= get_lpt_node_type(c
, buf
, &node_num
);
1171 node_len
= get_lpt_node_len(c
, node_type
);
1172 offs
= c
->leb_size
- len
;
1173 ubifs_assert(node_len
!= 0);
1174 mutex_lock(&c
->lp_mutex
);
1175 err
= make_node_dirty(c
, node_type
, node_num
, lnum
, offs
);
1176 mutex_unlock(&c
->lp_mutex
);
1186 * lpt_gc - LPT garbage collection.
1187 * @c: UBIFS file-system description object
1189 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1190 * Returns %0 on success and a negative error code on failure.
1192 static int lpt_gc(struct ubifs_info
*c
)
1194 int i
, lnum
= -1, dirty
= 0;
1196 mutex_lock(&c
->lp_mutex
);
1197 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1198 ubifs_assert(!c
->ltab
[i
].tgc
);
1199 if (i
+ c
->lpt_first
== c
->nhead_lnum
||
1200 c
->ltab
[i
].free
+ c
->ltab
[i
].dirty
== c
->leb_size
)
1202 if (c
->ltab
[i
].dirty
> dirty
) {
1203 dirty
= c
->ltab
[i
].dirty
;
1204 lnum
= i
+ c
->lpt_first
;
1207 mutex_unlock(&c
->lp_mutex
);
1210 return lpt_gc_lnum(c
, lnum
);
1214 * ubifs_lpt_start_commit - UBIFS commit starts.
1215 * @c: the UBIFS file-system description object
1217 * This function has to be called when UBIFS starts the commit operation.
1218 * This function "freezes" all currently dirty LEB properties and does not
1219 * change them anymore. Further changes are saved and tracked separately
1220 * because they are not part of this commit. This function returns zero in case
1221 * of success and a negative error code in case of failure.
1223 int ubifs_lpt_start_commit(struct ubifs_info
*c
)
1229 mutex_lock(&c
->lp_mutex
);
1230 err
= dbg_chk_lpt_free_spc(c
);
1233 err
= dbg_check_ltab(c
);
1237 if (c
->check_lpt_free
) {
1239 * We ensure there is enough free space in
1240 * ubifs_lpt_post_commit() by marking nodes dirty. That
1241 * information is lost when we unmount, so we also need
1242 * to check free space once after mounting also.
1244 c
->check_lpt_free
= 0;
1245 while (need_write_all(c
)) {
1246 mutex_unlock(&c
->lp_mutex
);
1250 mutex_lock(&c
->lp_mutex
);
1256 if (!c
->dirty_pn_cnt
) {
1257 dbg_cmt("no cnodes to commit");
1262 if (!c
->big_lpt
&& need_write_all(c
)) {
1263 /* If needed, write everything */
1264 err
= make_tree_dirty(c
);
1273 cnt
= get_cnodes_to_commit(c
);
1274 ubifs_assert(cnt
!= 0);
1276 err
= layout_cnodes(c
);
1280 /* Copy the LPT's own lprops for end commit to write */
1281 memcpy(c
->ltab_cmt
, c
->ltab
,
1282 sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1283 c
->lpt_drty_flgs
&= ~(LTAB_DIRTY
| LSAVE_DIRTY
);
1286 mutex_unlock(&c
->lp_mutex
);
1291 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1292 * @c: UBIFS file-system description object
1294 static void free_obsolete_cnodes(struct ubifs_info
*c
)
1296 struct ubifs_cnode
*cnode
, *cnext
;
1298 cnext
= c
->lpt_cnext
;
1303 cnext
= cnode
->cnext
;
1304 if (test_bit(OBSOLETE_CNODE
, &cnode
->flags
))
1307 cnode
->cnext
= NULL
;
1308 } while (cnext
!= c
->lpt_cnext
);
1309 c
->lpt_cnext
= NULL
;
1313 * ubifs_lpt_end_commit - finish the commit operation.
1314 * @c: the UBIFS file-system description object
1316 * This function has to be called when the commit operation finishes. It
1317 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1318 * the media. Returns zero in case of success and a negative error code in case
1321 int ubifs_lpt_end_commit(struct ubifs_info
*c
)
1330 err
= write_cnodes(c
);
1334 mutex_lock(&c
->lp_mutex
);
1335 free_obsolete_cnodes(c
);
1336 mutex_unlock(&c
->lp_mutex
);
1342 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1343 * @c: UBIFS file-system description object
1345 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1346 * commit for the "big" LPT model.
1348 int ubifs_lpt_post_commit(struct ubifs_info
*c
)
1352 mutex_lock(&c
->lp_mutex
);
1353 err
= lpt_tgc_end(c
);
1357 while (need_write_all(c
)) {
1358 mutex_unlock(&c
->lp_mutex
);
1362 mutex_lock(&c
->lp_mutex
);
1365 mutex_unlock(&c
->lp_mutex
);
1370 * first_nnode - find the first nnode in memory.
1371 * @c: UBIFS file-system description object
1372 * @hght: height of tree where nnode found is returned here
1374 * This function returns a pointer to the nnode found or %NULL if no nnode is
1375 * found. This function is a helper to 'ubifs_lpt_free()'.
1377 static struct ubifs_nnode
*first_nnode(struct ubifs_info
*c
, int *hght
)
1379 struct ubifs_nnode
*nnode
;
1386 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1388 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1389 if (nnode
->nbranch
[i
].nnode
) {
1391 nnode
= nnode
->nbranch
[i
].nnode
;
1403 * next_nnode - find the next nnode in memory.
1404 * @c: UBIFS file-system description object
1405 * @nnode: nnode from which to start.
1406 * @hght: height of tree where nnode is, is passed and returned here
1408 * This function returns a pointer to the nnode found or %NULL if no nnode is
1409 * found. This function is a helper to 'ubifs_lpt_free()'.
1411 static struct ubifs_nnode
*next_nnode(struct ubifs_info
*c
,
1412 struct ubifs_nnode
*nnode
, int *hght
)
1414 struct ubifs_nnode
*parent
;
1415 int iip
, h
, i
, found
;
1417 parent
= nnode
->parent
;
1420 if (nnode
->iip
== UBIFS_LPT_FANOUT
- 1) {
1424 for (iip
= nnode
->iip
+ 1; iip
< UBIFS_LPT_FANOUT
; iip
++) {
1425 nnode
= parent
->nbranch
[iip
].nnode
;
1433 for (h
= *hght
+ 1; h
< c
->lpt_hght
; h
++) {
1435 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1436 if (nnode
->nbranch
[i
].nnode
) {
1438 nnode
= nnode
->nbranch
[i
].nnode
;
1450 * ubifs_lpt_free - free resources owned by the LPT.
1451 * @c: UBIFS file-system description object
1452 * @wr_only: free only resources used for writing
1454 void ubifs_lpt_free(struct ubifs_info
*c
, int wr_only
)
1456 struct ubifs_nnode
*nnode
;
1459 /* Free write-only things first */
1461 free_obsolete_cnodes(c
); /* Leftover from a failed commit */
1473 /* Now free the rest */
1475 nnode
= first_nnode(c
, &hght
);
1477 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++)
1478 kfree(nnode
->nbranch
[i
].nnode
);
1479 nnode
= next_nnode(c
, nnode
, &hght
);
1481 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++)
1482 kfree(c
->lpt_heap
[i
].arr
);
1483 kfree(c
->dirty_idx
.arr
);
1486 kfree(c
->lpt_nod_buf
);
1490 * Everything below is related to debugging.
1494 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1496 * @len: buffer length
1498 static int dbg_is_all_ff(uint8_t *buf
, int len
)
1502 for (i
= 0; i
< len
; i
++)
1509 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1510 * @c: the UBIFS file-system description object
1511 * @lnum: LEB number where nnode was written
1512 * @offs: offset where nnode was written
1514 static int dbg_is_nnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1516 struct ubifs_nnode
*nnode
;
1519 /* Entire tree is in memory so first_nnode / next_nnode are OK */
1520 nnode
= first_nnode(c
, &hght
);
1521 for (; nnode
; nnode
= next_nnode(c
, nnode
, &hght
)) {
1522 struct ubifs_nbranch
*branch
;
1525 if (nnode
->parent
) {
1526 branch
= &nnode
->parent
->nbranch
[nnode
->iip
];
1527 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1529 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1533 if (c
->lpt_lnum
!= lnum
|| c
->lpt_offs
!= offs
)
1535 if (test_bit(DIRTY_CNODE
, &nnode
->flags
))
1544 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1545 * @c: the UBIFS file-system description object
1546 * @lnum: LEB number where pnode was written
1547 * @offs: offset where pnode was written
1549 static int dbg_is_pnode_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1553 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1554 for (i
= 0; i
< cnt
; i
++) {
1555 struct ubifs_pnode
*pnode
;
1556 struct ubifs_nbranch
*branch
;
1559 pnode
= pnode_lookup(c
, i
);
1561 return PTR_ERR(pnode
);
1562 branch
= &pnode
->parent
->nbranch
[pnode
->iip
];
1563 if (branch
->lnum
!= lnum
|| branch
->offs
!= offs
)
1565 if (test_bit(DIRTY_CNODE
, &pnode
->flags
))
1573 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1574 * @c: the UBIFS file-system description object
1575 * @lnum: LEB number where ltab node was written
1576 * @offs: offset where ltab node was written
1578 static int dbg_is_ltab_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1580 if (lnum
!= c
->ltab_lnum
|| offs
!= c
->ltab_offs
)
1582 return (c
->lpt_drty_flgs
& LTAB_DIRTY
) != 0;
1586 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1587 * @c: the UBIFS file-system description object
1588 * @lnum: LEB number where lsave node was written
1589 * @offs: offset where lsave node was written
1591 static int dbg_is_lsave_dirty(struct ubifs_info
*c
, int lnum
, int offs
)
1593 if (lnum
!= c
->lsave_lnum
|| offs
!= c
->lsave_offs
)
1595 return (c
->lpt_drty_flgs
& LSAVE_DIRTY
) != 0;
1599 * dbg_is_node_dirty - determine if a node is dirty.
1600 * @c: the UBIFS file-system description object
1601 * @node_type: node type
1602 * @lnum: LEB number where node was written
1603 * @offs: offset where node was written
1605 static int dbg_is_node_dirty(struct ubifs_info
*c
, int node_type
, int lnum
,
1608 switch (node_type
) {
1609 case UBIFS_LPT_NNODE
:
1610 return dbg_is_nnode_dirty(c
, lnum
, offs
);
1611 case UBIFS_LPT_PNODE
:
1612 return dbg_is_pnode_dirty(c
, lnum
, offs
);
1613 case UBIFS_LPT_LTAB
:
1614 return dbg_is_ltab_dirty(c
, lnum
, offs
);
1615 case UBIFS_LPT_LSAVE
:
1616 return dbg_is_lsave_dirty(c
, lnum
, offs
);
1622 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1623 * @c: the UBIFS file-system description object
1624 * @lnum: LEB number where node was written
1626 * This function returns %0 on success and a negative error code on failure.
1628 static int dbg_check_ltab_lnum(struct ubifs_info
*c
, int lnum
)
1630 int err
, len
= c
->leb_size
, dirty
= 0, node_type
, node_num
, node_len
;
1634 if (!dbg_is_chk_lprops(c
))
1637 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1639 ubifs_err(c
, "cannot allocate memory for ltab checking");
1643 dbg_lp("LEB %d", lnum
);
1645 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1650 if (!is_a_node(c
, p
, len
)) {
1653 pad_len
= get_pad_len(c
, p
, len
);
1660 if (!dbg_is_all_ff(p
, len
)) {
1661 ubifs_err(c
, "invalid empty space in LEB %d at %d",
1662 lnum
, c
->leb_size
- len
);
1665 i
= lnum
- c
->lpt_first
;
1666 if (len
!= c
->ltab
[i
].free
) {
1667 ubifs_err(c
, "invalid free space in LEB %d (free %d, expected %d)",
1668 lnum
, len
, c
->ltab
[i
].free
);
1671 if (dirty
!= c
->ltab
[i
].dirty
) {
1672 ubifs_err(c
, "invalid dirty space in LEB %d (dirty %d, expected %d)",
1673 lnum
, dirty
, c
->ltab
[i
].dirty
);
1678 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1679 node_len
= get_lpt_node_len(c
, node_type
);
1680 ret
= dbg_is_node_dirty(c
, node_type
, lnum
, c
->leb_size
- len
);
1694 * dbg_check_ltab - check the free and dirty space in the ltab.
1695 * @c: the UBIFS file-system description object
1697 * This function returns %0 on success and a negative error code on failure.
1699 int dbg_check_ltab(struct ubifs_info
*c
)
1701 int lnum
, err
, i
, cnt
;
1703 if (!dbg_is_chk_lprops(c
))
1706 /* Bring the entire tree into memory */
1707 cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
1708 for (i
= 0; i
< cnt
; i
++) {
1709 struct ubifs_pnode
*pnode
;
1711 pnode
= pnode_lookup(c
, i
);
1713 return PTR_ERR(pnode
);
1718 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)c
->nroot
, 0, 0);
1722 /* Check each LEB */
1723 for (lnum
= c
->lpt_first
; lnum
<= c
->lpt_last
; lnum
++) {
1724 err
= dbg_check_ltab_lnum(c
, lnum
);
1726 ubifs_err(c
, "failed at LEB %d", lnum
);
1731 dbg_lp("succeeded");
1736 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1737 * @c: the UBIFS file-system description object
1739 * This function returns %0 on success and a negative error code on failure.
1741 int dbg_chk_lpt_free_spc(struct ubifs_info
*c
)
1746 if (!dbg_is_chk_lprops(c
))
1749 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1750 if (c
->ltab
[i
].tgc
|| c
->ltab
[i
].cmt
)
1752 if (i
+ c
->lpt_first
== c
->nhead_lnum
)
1753 free
+= c
->leb_size
- c
->nhead_offs
;
1754 else if (c
->ltab
[i
].free
== c
->leb_size
)
1755 free
+= c
->leb_size
;
1757 if (free
< c
->lpt_sz
) {
1758 ubifs_err(c
, "LPT space error: free %lld lpt_sz %lld",
1760 ubifs_dump_lpt_info(c
);
1761 ubifs_dump_lpt_lebs(c
);
1769 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1770 * @c: the UBIFS file-system description object
1771 * @action: what to do
1772 * @len: length written
1774 * This function returns %0 on success and a negative error code on failure.
1775 * The @action argument may be one of:
1776 * o %0 - LPT debugging checking starts, initialize debugging variables;
1777 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1778 * o %2 - switched to a different LEB and wasted @len bytes;
1779 * o %3 - check that we've written the right number of bytes.
1780 * o %4 - wasted @len bytes;
1782 int dbg_chk_lpt_sz(struct ubifs_info
*c
, int action
, int len
)
1784 struct ubifs_debug_info
*d
= c
->dbg
;
1785 long long chk_lpt_sz
, lpt_sz
;
1788 if (!dbg_is_chk_lprops(c
))
1795 d
->chk_lpt_lebs
= 0;
1796 d
->chk_lpt_wastage
= 0;
1797 if (c
->dirty_pn_cnt
> c
->pnode_cnt
) {
1798 ubifs_err(c
, "dirty pnodes %d exceed max %d",
1799 c
->dirty_pn_cnt
, c
->pnode_cnt
);
1802 if (c
->dirty_nn_cnt
> c
->nnode_cnt
) {
1803 ubifs_err(c
, "dirty nnodes %d exceed max %d",
1804 c
->dirty_nn_cnt
, c
->nnode_cnt
);
1809 d
->chk_lpt_sz
+= len
;
1812 d
->chk_lpt_sz
+= len
;
1813 d
->chk_lpt_wastage
+= len
;
1814 d
->chk_lpt_lebs
+= 1;
1817 chk_lpt_sz
= c
->leb_size
;
1818 chk_lpt_sz
*= d
->chk_lpt_lebs
;
1819 chk_lpt_sz
+= len
- c
->nhead_offs
;
1820 if (d
->chk_lpt_sz
!= chk_lpt_sz
) {
1821 ubifs_err(c
, "LPT wrote %lld but space used was %lld",
1822 d
->chk_lpt_sz
, chk_lpt_sz
);
1825 if (d
->chk_lpt_sz
> c
->lpt_sz
) {
1826 ubifs_err(c
, "LPT wrote %lld but lpt_sz is %lld",
1827 d
->chk_lpt_sz
, c
->lpt_sz
);
1830 if (d
->chk_lpt_sz2
&& d
->chk_lpt_sz
!= d
->chk_lpt_sz2
) {
1831 ubifs_err(c
, "LPT layout size %lld but wrote %lld",
1832 d
->chk_lpt_sz
, d
->chk_lpt_sz2
);
1835 if (d
->chk_lpt_sz2
&& d
->new_nhead_offs
!= len
) {
1836 ubifs_err(c
, "LPT new nhead offs: expected %d was %d",
1837 d
->new_nhead_offs
, len
);
1840 lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
1841 lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
1842 lpt_sz
+= c
->ltab_sz
;
1844 lpt_sz
+= c
->lsave_sz
;
1845 if (d
->chk_lpt_sz
- d
->chk_lpt_wastage
> lpt_sz
) {
1846 ubifs_err(c
, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1847 d
->chk_lpt_sz
, d
->chk_lpt_wastage
, lpt_sz
);
1851 ubifs_dump_lpt_info(c
);
1852 ubifs_dump_lpt_lebs(c
);
1855 d
->chk_lpt_sz2
= d
->chk_lpt_sz
;
1857 d
->chk_lpt_wastage
= 0;
1858 d
->chk_lpt_lebs
= 0;
1859 d
->new_nhead_offs
= len
;
1862 d
->chk_lpt_sz
+= len
;
1863 d
->chk_lpt_wastage
+= len
;
1871 * dump_lpt_leb - dump an LPT LEB.
1872 * @c: UBIFS file-system description object
1873 * @lnum: LEB number to dump
1875 * This function dumps an LEB from LPT area. Nodes in this area are very
1876 * different to nodes in the main area (e.g., they do not have common headers,
1877 * they do not have 8-byte alignments, etc), so we have a separate function to
1878 * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1880 static void dump_lpt_leb(const struct ubifs_info
*c
, int lnum
)
1882 int err
, len
= c
->leb_size
, node_type
, node_num
, node_len
, offs
;
1885 pr_err("(pid %d) start dumping LEB %d\n", current
->pid
, lnum
);
1886 buf
= p
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
1888 ubifs_err(c
, "cannot allocate memory to dump LPT");
1892 err
= ubifs_leb_read(c
, lnum
, buf
, 0, c
->leb_size
, 1);
1897 offs
= c
->leb_size
- len
;
1898 if (!is_a_node(c
, p
, len
)) {
1901 pad_len
= get_pad_len(c
, p
, len
);
1903 pr_err("LEB %d:%d, pad %d bytes\n",
1904 lnum
, offs
, pad_len
);
1910 pr_err("LEB %d:%d, free %d bytes\n",
1915 node_type
= get_lpt_node_type(c
, p
, &node_num
);
1916 switch (node_type
) {
1917 case UBIFS_LPT_PNODE
:
1919 node_len
= c
->pnode_sz
;
1921 pr_err("LEB %d:%d, pnode num %d\n",
1922 lnum
, offs
, node_num
);
1924 pr_err("LEB %d:%d, pnode\n", lnum
, offs
);
1927 case UBIFS_LPT_NNODE
:
1930 struct ubifs_nnode nnode
;
1932 node_len
= c
->nnode_sz
;
1934 pr_err("LEB %d:%d, nnode num %d, ",
1935 lnum
, offs
, node_num
);
1937 pr_err("LEB %d:%d, nnode, ",
1939 err
= ubifs_unpack_nnode(c
, p
, &nnode
);
1941 pr_err("failed to unpack_node, error %d\n",
1945 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1946 pr_cont("%d:%d", nnode
.nbranch
[i
].lnum
,
1947 nnode
.nbranch
[i
].offs
);
1948 if (i
!= UBIFS_LPT_FANOUT
- 1)
1954 case UBIFS_LPT_LTAB
:
1955 node_len
= c
->ltab_sz
;
1956 pr_err("LEB %d:%d, ltab\n", lnum
, offs
);
1958 case UBIFS_LPT_LSAVE
:
1959 node_len
= c
->lsave_sz
;
1960 pr_err("LEB %d:%d, lsave len\n", lnum
, offs
);
1963 ubifs_err(c
, "LPT node type %d not recognized", node_type
);
1971 pr_err("(pid %d) finish dumping LEB %d\n", current
->pid
, lnum
);
1978 * ubifs_dump_lpt_lebs - dump LPT lebs.
1979 * @c: UBIFS file-system description object
1981 * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1984 void ubifs_dump_lpt_lebs(const struct ubifs_info
*c
)
1988 pr_err("(pid %d) start dumping all LPT LEBs\n", current
->pid
);
1989 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1990 dump_lpt_leb(c
, i
+ c
->lpt_first
);
1991 pr_err("(pid %d) finish dumping all LPT LEBs\n", current
->pid
);
1995 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1996 * @c: UBIFS file-system description object
1998 * This is a debugging version for 'populate_lsave()' which populates lsave
1999 * with random LEBs instead of useful LEBs, which is good for test coverage.
2000 * Returns zero if lsave has not been populated (this debugging feature is
2001 * disabled) an non-zero if lsave has been populated.
2003 static int dbg_populate_lsave(struct ubifs_info
*c
)
2005 struct ubifs_lprops
*lprops
;
2006 struct ubifs_lpt_heap
*heap
;
2009 if (!dbg_is_chk_gen(c
))
2011 if (prandom_u32() & 3)
2014 for (i
= 0; i
< c
->lsave_cnt
; i
++)
2015 c
->lsave
[i
] = c
->main_first
;
2017 list_for_each_entry(lprops
, &c
->empty_list
, list
)
2018 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2019 list_for_each_entry(lprops
, &c
->freeable_list
, list
)
2020 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2021 list_for_each_entry(lprops
, &c
->frdi_idx_list
, list
)
2022 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = lprops
->lnum
;
2024 heap
= &c
->lpt_heap
[LPROPS_DIRTY_IDX
- 1];
2025 for (i
= 0; i
< heap
->cnt
; i
++)
2026 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2027 heap
= &c
->lpt_heap
[LPROPS_DIRTY
- 1];
2028 for (i
= 0; i
< heap
->cnt
; i
++)
2029 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;
2030 heap
= &c
->lpt_heap
[LPROPS_FREE
- 1];
2031 for (i
= 0; i
< heap
->cnt
; i
++)
2032 c
->lsave
[prandom_u32() % c
->lsave_cnt
] = heap
->arr
[i
]->lnum
;