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 the LEB properties tree (LPT) area. The LPT area
25 * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
26 * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
27 * between the log and the orphan area.
29 * The LPT area is like a miniature self-contained file system. It is required
30 * that it never runs out of space, is fast to access and update, and scales
31 * logarithmically. The LEB properties tree is implemented as a wandering tree
32 * much like the TNC, and the LPT area has its own garbage collection.
34 * The LPT has two slightly different forms called the "small model" and the
35 * "big model". The small model is used when the entire LEB properties table
36 * can be written into a single eraseblock. In that case, garbage collection
37 * consists of just writing the whole table, which therefore makes all other
38 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
39 * selected for garbage collection, which consists of marking the clean nodes in
40 * that LEB as dirty, and then only the dirty nodes are written out. Also, in
41 * the case of the big model, a table of LEB numbers is saved so that the entire
42 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
47 #include <linux/crc16.h>
48 #include <linux/math64.h>
49 #include <linux/slab.h>
52 * do_calc_lpt_geom - calculate sizes for the LPT area.
53 * @c: the UBIFS file-system description object
55 * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
56 * properties of the flash and whether LPT is "big" (c->big_lpt).
58 static void do_calc_lpt_geom(struct ubifs_info
*c
)
60 int i
, n
, bits
, per_leb_wastage
, max_pnode_cnt
;
61 long long sz
, tot_wastage
;
63 n
= c
->main_lebs
+ c
->max_leb_cnt
- c
->leb_cnt
;
64 max_pnode_cnt
= DIV_ROUND_UP(n
, UBIFS_LPT_FANOUT
);
68 while (n
< max_pnode_cnt
) {
70 n
<<= UBIFS_LPT_FANOUT_SHIFT
;
73 c
->pnode_cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
75 n
= DIV_ROUND_UP(c
->pnode_cnt
, UBIFS_LPT_FANOUT
);
77 for (i
= 1; i
< c
->lpt_hght
; i
++) {
78 n
= DIV_ROUND_UP(n
, UBIFS_LPT_FANOUT
);
82 c
->space_bits
= fls(c
->leb_size
) - 3;
83 c
->lpt_lnum_bits
= fls(c
->lpt_lebs
);
84 c
->lpt_offs_bits
= fls(c
->leb_size
- 1);
85 c
->lpt_spc_bits
= fls(c
->leb_size
);
87 n
= DIV_ROUND_UP(c
->max_leb_cnt
, UBIFS_LPT_FANOUT
);
88 c
->pcnt_bits
= fls(n
- 1);
90 c
->lnum_bits
= fls(c
->max_leb_cnt
- 1);
92 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
93 (c
->big_lpt
? c
->pcnt_bits
: 0) +
94 (c
->space_bits
* 2 + 1) * UBIFS_LPT_FANOUT
;
95 c
->pnode_sz
= (bits
+ 7) / 8;
97 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
98 (c
->big_lpt
? c
->pcnt_bits
: 0) +
99 (c
->lpt_lnum_bits
+ c
->lpt_offs_bits
) * UBIFS_LPT_FANOUT
;
100 c
->nnode_sz
= (bits
+ 7) / 8;
102 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
103 c
->lpt_lebs
* c
->lpt_spc_bits
* 2;
104 c
->ltab_sz
= (bits
+ 7) / 8;
106 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
107 c
->lnum_bits
* c
->lsave_cnt
;
108 c
->lsave_sz
= (bits
+ 7) / 8;
110 /* Calculate the minimum LPT size */
111 c
->lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
112 c
->lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
113 c
->lpt_sz
+= c
->ltab_sz
;
115 c
->lpt_sz
+= c
->lsave_sz
;
119 per_leb_wastage
= max_t(int, c
->pnode_sz
, c
->nnode_sz
);
120 sz
+= per_leb_wastage
;
121 tot_wastage
= per_leb_wastage
;
122 while (sz
> c
->leb_size
) {
123 sz
+= per_leb_wastage
;
125 tot_wastage
+= per_leb_wastage
;
127 tot_wastage
+= ALIGN(sz
, c
->min_io_size
) - sz
;
128 c
->lpt_sz
+= tot_wastage
;
132 * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
133 * @c: the UBIFS file-system description object
135 * This function returns %0 on success and a negative error code on failure.
137 int ubifs_calc_lpt_geom(struct ubifs_info
*c
)
144 /* Verify that lpt_lebs is big enough */
145 sz
= c
->lpt_sz
* 2; /* Must have at least 2 times the size */
146 lebs_needed
= div_u64(sz
+ c
->leb_size
- 1, c
->leb_size
);
147 if (lebs_needed
> c
->lpt_lebs
) {
148 ubifs_err("too few LPT LEBs");
152 /* Verify that ltab fits in a single LEB (since ltab is a single node */
153 if (c
->ltab_sz
> c
->leb_size
) {
154 ubifs_err("LPT ltab too big");
158 c
->check_lpt_free
= c
->big_lpt
;
163 * calc_dflt_lpt_geom - calculate default LPT geometry.
164 * @c: the UBIFS file-system description object
165 * @main_lebs: number of main area LEBs is passed and returned here
166 * @big_lpt: whether the LPT area is "big" is returned here
168 * The size of the LPT area depends on parameters that themselves are dependent
169 * on the size of the LPT area. This function, successively recalculates the LPT
170 * area geometry until the parameters and resultant geometry are consistent.
172 * This function returns %0 on success and a negative error code on failure.
174 static int calc_dflt_lpt_geom(struct ubifs_info
*c
, int *main_lebs
,
180 /* Start by assuming the minimum number of LPT LEBs */
181 c
->lpt_lebs
= UBIFS_MIN_LPT_LEBS
;
182 c
->main_lebs
= *main_lebs
- c
->lpt_lebs
;
183 if (c
->main_lebs
<= 0)
186 /* And assume we will use the small LPT model */
190 * Calculate the geometry based on assumptions above and then see if it
195 /* Small LPT model must have lpt_sz < leb_size */
196 if (c
->lpt_sz
> c
->leb_size
) {
197 /* Nope, so try again using big LPT model */
202 /* Now check there are enough LPT LEBs */
203 for (i
= 0; i
< 64 ; i
++) {
204 sz
= c
->lpt_sz
* 4; /* Allow 4 times the size */
205 lebs_needed
= div_u64(sz
+ c
->leb_size
- 1, c
->leb_size
);
206 if (lebs_needed
> c
->lpt_lebs
) {
207 /* Not enough LPT LEBs so try again with more */
208 c
->lpt_lebs
= lebs_needed
;
209 c
->main_lebs
= *main_lebs
- c
->lpt_lebs
;
210 if (c
->main_lebs
<= 0)
215 if (c
->ltab_sz
> c
->leb_size
) {
216 ubifs_err("LPT ltab too big");
219 *main_lebs
= c
->main_lebs
;
220 *big_lpt
= c
->big_lpt
;
227 * pack_bits - pack bit fields end-to-end.
228 * @addr: address at which to pack (passed and next address returned)
229 * @pos: bit position at which to pack (passed and next position returned)
230 * @val: value to pack
231 * @nrbits: number of bits of value to pack (1-32)
233 static void pack_bits(uint8_t **addr
, int *pos
, uint32_t val
, int nrbits
)
238 ubifs_assert(nrbits
> 0);
239 ubifs_assert(nrbits
<= 32);
240 ubifs_assert(*pos
>= 0);
241 ubifs_assert(*pos
< 8);
242 ubifs_assert((val
>> nrbits
) == 0 || nrbits
== 32);
244 *p
|= ((uint8_t)val
) << b
;
247 *++p
= (uint8_t)(val
>>= (8 - b
));
249 *++p
= (uint8_t)(val
>>= 8);
251 *++p
= (uint8_t)(val
>>= 8);
253 *++p
= (uint8_t)(val
>>= 8);
260 *++p
= (uint8_t)(val
>>= 8);
262 *++p
= (uint8_t)(val
>>= 8);
264 *++p
= (uint8_t)(val
>>= 8);
276 * ubifs_unpack_bits - unpack bit fields.
277 * @addr: address at which to unpack (passed and next address returned)
278 * @pos: bit position at which to unpack (passed and next position returned)
279 * @nrbits: number of bits of value to unpack (1-32)
281 * This functions returns the value unpacked.
283 uint32_t ubifs_unpack_bits(uint8_t **addr
, int *pos
, int nrbits
)
285 const int k
= 32 - nrbits
;
288 uint32_t uninitialized_var(val
);
289 const int bytes
= (nrbits
+ b
+ 7) >> 3;
291 ubifs_assert(nrbits
> 0);
292 ubifs_assert(nrbits
<= 32);
293 ubifs_assert(*pos
>= 0);
294 ubifs_assert(*pos
< 8);
301 val
= p
[1] | ((uint32_t)p
[2] << 8);
304 val
= p
[1] | ((uint32_t)p
[2] << 8) |
305 ((uint32_t)p
[3] << 16);
308 val
= p
[1] | ((uint32_t)p
[2] << 8) |
309 ((uint32_t)p
[3] << 16) |
310 ((uint32_t)p
[4] << 24);
321 val
= p
[0] | ((uint32_t)p
[1] << 8);
324 val
= p
[0] | ((uint32_t)p
[1] << 8) |
325 ((uint32_t)p
[2] << 16);
328 val
= p
[0] | ((uint32_t)p
[1] << 8) |
329 ((uint32_t)p
[2] << 16) |
330 ((uint32_t)p
[3] << 24);
340 ubifs_assert((val
>> nrbits
) == 0 || nrbits
- b
== 32);
345 * ubifs_pack_pnode - pack all the bit fields of a pnode.
346 * @c: UBIFS file-system description object
347 * @buf: buffer into which to pack
348 * @pnode: pnode to pack
350 void ubifs_pack_pnode(struct ubifs_info
*c
, void *buf
,
351 struct ubifs_pnode
*pnode
)
353 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
357 pack_bits(&addr
, &pos
, UBIFS_LPT_PNODE
, UBIFS_LPT_TYPE_BITS
);
359 pack_bits(&addr
, &pos
, pnode
->num
, c
->pcnt_bits
);
360 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
361 pack_bits(&addr
, &pos
, pnode
->lprops
[i
].free
>> 3,
363 pack_bits(&addr
, &pos
, pnode
->lprops
[i
].dirty
>> 3,
365 if (pnode
->lprops
[i
].flags
& LPROPS_INDEX
)
366 pack_bits(&addr
, &pos
, 1, 1);
368 pack_bits(&addr
, &pos
, 0, 1);
370 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
371 c
->pnode_sz
- UBIFS_LPT_CRC_BYTES
);
374 pack_bits(&addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
378 * ubifs_pack_nnode - pack all the bit fields of a nnode.
379 * @c: UBIFS file-system description object
380 * @buf: buffer into which to pack
381 * @nnode: nnode to pack
383 void ubifs_pack_nnode(struct ubifs_info
*c
, void *buf
,
384 struct ubifs_nnode
*nnode
)
386 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
390 pack_bits(&addr
, &pos
, UBIFS_LPT_NNODE
, UBIFS_LPT_TYPE_BITS
);
392 pack_bits(&addr
, &pos
, nnode
->num
, c
->pcnt_bits
);
393 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
394 int lnum
= nnode
->nbranch
[i
].lnum
;
397 lnum
= c
->lpt_last
+ 1;
398 pack_bits(&addr
, &pos
, lnum
- c
->lpt_first
, c
->lpt_lnum_bits
);
399 pack_bits(&addr
, &pos
, nnode
->nbranch
[i
].offs
,
402 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
403 c
->nnode_sz
- UBIFS_LPT_CRC_BYTES
);
406 pack_bits(&addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
410 * ubifs_pack_ltab - pack the LPT's own lprops table.
411 * @c: UBIFS file-system description object
412 * @buf: buffer into which to pack
413 * @ltab: LPT's own lprops table to pack
415 void ubifs_pack_ltab(struct ubifs_info
*c
, void *buf
,
416 struct ubifs_lpt_lprops
*ltab
)
418 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
422 pack_bits(&addr
, &pos
, UBIFS_LPT_LTAB
, UBIFS_LPT_TYPE_BITS
);
423 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
424 pack_bits(&addr
, &pos
, ltab
[i
].free
, c
->lpt_spc_bits
);
425 pack_bits(&addr
, &pos
, ltab
[i
].dirty
, c
->lpt_spc_bits
);
427 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
428 c
->ltab_sz
- UBIFS_LPT_CRC_BYTES
);
431 pack_bits(&addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
435 * ubifs_pack_lsave - pack the LPT's save table.
436 * @c: UBIFS file-system description object
437 * @buf: buffer into which to pack
438 * @lsave: LPT's save table to pack
440 void ubifs_pack_lsave(struct ubifs_info
*c
, void *buf
, int *lsave
)
442 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
446 pack_bits(&addr
, &pos
, UBIFS_LPT_LSAVE
, UBIFS_LPT_TYPE_BITS
);
447 for (i
= 0; i
< c
->lsave_cnt
; i
++)
448 pack_bits(&addr
, &pos
, lsave
[i
], c
->lnum_bits
);
449 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
450 c
->lsave_sz
- UBIFS_LPT_CRC_BYTES
);
453 pack_bits(&addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
457 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
458 * @c: UBIFS file-system description object
459 * @lnum: LEB number to which to add dirty space
460 * @dirty: amount of dirty space to add
462 void ubifs_add_lpt_dirt(struct ubifs_info
*c
, int lnum
, int dirty
)
466 dbg_lp("LEB %d add %d to %d",
467 lnum
, dirty
, c
->ltab
[lnum
- c
->lpt_first
].dirty
);
468 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
469 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
473 * set_ltab - set LPT LEB properties.
474 * @c: UBIFS file-system description object
476 * @free: amount of free space
477 * @dirty: amount of dirty space
479 static void set_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
481 dbg_lp("LEB %d free %d dirty %d to %d %d",
482 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
483 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
484 ubifs_assert(lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
485 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
486 c
->ltab
[lnum
- c
->lpt_first
].dirty
= dirty
;
490 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
491 * @c: UBIFS file-system description object
492 * @nnode: nnode for which to add dirt
494 void ubifs_add_nnode_dirt(struct ubifs_info
*c
, struct ubifs_nnode
*nnode
)
496 struct ubifs_nnode
*np
= nnode
->parent
;
499 ubifs_add_lpt_dirt(c
, np
->nbranch
[nnode
->iip
].lnum
,
502 ubifs_add_lpt_dirt(c
, c
->lpt_lnum
, c
->nnode_sz
);
503 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
504 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
505 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
511 * add_pnode_dirt - add dirty space to LPT LEB properties.
512 * @c: UBIFS file-system description object
513 * @pnode: pnode for which to add dirt
515 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
517 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
522 * calc_nnode_num - calculate nnode number.
523 * @row: the row in the tree (root is zero)
524 * @col: the column in the row (leftmost is zero)
526 * The nnode number is a number that uniquely identifies a nnode and can be used
527 * easily to traverse the tree from the root to that nnode.
529 * This function calculates and returns the nnode number for the nnode at @row
532 static int calc_nnode_num(int row
, int col
)
538 bits
= (col
& (UBIFS_LPT_FANOUT
- 1));
539 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
540 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
547 * calc_nnode_num_from_parent - calculate nnode number.
548 * @c: UBIFS file-system description object
549 * @parent: parent nnode
550 * @iip: index in parent
552 * The nnode number is a number that uniquely identifies a nnode and can be used
553 * easily to traverse the tree from the root to that nnode.
555 * This function calculates and returns the nnode number based on the parent's
556 * nnode number and the index in parent.
558 static int calc_nnode_num_from_parent(const struct ubifs_info
*c
,
559 struct ubifs_nnode
*parent
, int iip
)
565 shft
= (c
->lpt_hght
- parent
->level
) * UBIFS_LPT_FANOUT_SHIFT
;
566 num
= parent
->num
^ (1 << shft
);
567 num
|= (UBIFS_LPT_FANOUT
+ iip
) << shft
;
572 * calc_pnode_num_from_parent - calculate pnode number.
573 * @c: UBIFS file-system description object
574 * @parent: parent nnode
575 * @iip: index in parent
577 * The pnode number is a number that uniquely identifies a pnode and can be used
578 * easily to traverse the tree from the root to that pnode.
580 * This function calculates and returns the pnode number based on the parent's
581 * nnode number and the index in parent.
583 static int calc_pnode_num_from_parent(const struct ubifs_info
*c
,
584 struct ubifs_nnode
*parent
, int iip
)
586 int i
, n
= c
->lpt_hght
- 1, pnum
= parent
->num
, num
= 0;
588 for (i
= 0; i
< n
; i
++) {
589 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
590 num
|= pnum
& (UBIFS_LPT_FANOUT
- 1);
591 pnum
>>= UBIFS_LPT_FANOUT_SHIFT
;
593 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
599 * ubifs_create_dflt_lpt - create default LPT.
600 * @c: UBIFS file-system description object
601 * @main_lebs: number of main area LEBs is passed and returned here
602 * @lpt_first: LEB number of first LPT LEB
603 * @lpt_lebs: number of LEBs for LPT is passed and returned here
604 * @big_lpt: use big LPT model is passed and returned here
606 * This function returns %0 on success and a negative error code on failure.
608 int ubifs_create_dflt_lpt(struct ubifs_info
*c
, int *main_lebs
, int lpt_first
,
609 int *lpt_lebs
, int *big_lpt
)
611 int lnum
, err
= 0, node_sz
, iopos
, i
, j
, cnt
, len
, alen
, row
;
612 int blnum
, boffs
, bsz
, bcnt
;
613 struct ubifs_pnode
*pnode
= NULL
;
614 struct ubifs_nnode
*nnode
= NULL
;
615 void *buf
= NULL
, *p
;
616 struct ubifs_lpt_lprops
*ltab
= NULL
;
619 err
= calc_dflt_lpt_geom(c
, main_lebs
, big_lpt
);
622 *lpt_lebs
= c
->lpt_lebs
;
624 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
625 c
->lpt_first
= lpt_first
;
626 /* Needed by 'set_ltab()' */
627 c
->lpt_last
= lpt_first
+ c
->lpt_lebs
- 1;
628 /* Needed by 'ubifs_pack_lsave()' */
629 c
->main_first
= c
->leb_cnt
- *main_lebs
;
631 lsave
= kmalloc(sizeof(int) * c
->lsave_cnt
, GFP_KERNEL
);
632 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_KERNEL
);
633 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_KERNEL
);
634 buf
= vmalloc(c
->leb_size
);
635 ltab
= vmalloc(sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
636 if (!pnode
|| !nnode
|| !buf
|| !ltab
|| !lsave
) {
641 ubifs_assert(!c
->ltab
);
642 c
->ltab
= ltab
; /* Needed by set_ltab */
644 /* Initialize LPT's own lprops */
645 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
646 ltab
[i
].free
= c
->leb_size
;
654 /* Number of leaf nodes (pnodes) */
658 * The first pnode contains the LEB properties for the LEBs that contain
659 * the root inode node and the root index node of the index tree.
661 node_sz
= ALIGN(ubifs_idx_node_sz(c
, 1), 8);
662 iopos
= ALIGN(node_sz
, c
->min_io_size
);
663 pnode
->lprops
[0].free
= c
->leb_size
- iopos
;
664 pnode
->lprops
[0].dirty
= iopos
- node_sz
;
665 pnode
->lprops
[0].flags
= LPROPS_INDEX
;
667 node_sz
= UBIFS_INO_NODE_SZ
;
668 iopos
= ALIGN(node_sz
, c
->min_io_size
);
669 pnode
->lprops
[1].free
= c
->leb_size
- iopos
;
670 pnode
->lprops
[1].dirty
= iopos
- node_sz
;
672 for (i
= 2; i
< UBIFS_LPT_FANOUT
; i
++)
673 pnode
->lprops
[i
].free
= c
->leb_size
;
675 /* Add first pnode */
676 ubifs_pack_pnode(c
, p
, pnode
);
681 /* Reset pnode values for remaining pnodes */
682 pnode
->lprops
[0].free
= c
->leb_size
;
683 pnode
->lprops
[0].dirty
= 0;
684 pnode
->lprops
[0].flags
= 0;
686 pnode
->lprops
[1].free
= c
->leb_size
;
687 pnode
->lprops
[1].dirty
= 0;
690 * To calculate the internal node branches, we keep information about
693 blnum
= lnum
; /* LEB number of level below */
694 boffs
= 0; /* Offset of level below */
695 bcnt
= cnt
; /* Number of nodes in level below */
696 bsz
= c
->pnode_sz
; /* Size of nodes in level below */
698 /* Add all remaining pnodes */
699 for (i
= 1; i
< cnt
; i
++) {
700 if (len
+ c
->pnode_sz
> c
->leb_size
) {
701 alen
= ALIGN(len
, c
->min_io_size
);
702 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
703 memset(p
, 0xff, alen
- len
);
704 err
= ubi_leb_change(c
->ubi
, lnum
++, buf
, alen
,
711 ubifs_pack_pnode(c
, p
, pnode
);
715 * pnodes are simply numbered left to right starting at zero,
716 * which means the pnode number can be used easily to traverse
717 * down the tree to the corresponding pnode.
723 for (i
= UBIFS_LPT_FANOUT
; cnt
> i
; i
<<= UBIFS_LPT_FANOUT_SHIFT
)
725 /* Add all nnodes, one level at a time */
727 /* Number of internal nodes (nnodes) at next level */
728 cnt
= DIV_ROUND_UP(cnt
, UBIFS_LPT_FANOUT
);
729 for (i
= 0; i
< cnt
; i
++) {
730 if (len
+ c
->nnode_sz
> c
->leb_size
) {
731 alen
= ALIGN(len
, c
->min_io_size
);
732 set_ltab(c
, lnum
, c
->leb_size
- alen
,
734 memset(p
, 0xff, alen
- len
);
735 err
= ubi_leb_change(c
->ubi
, lnum
++, buf
, alen
,
742 /* Only 1 nnode at this level, so it is the root */
747 /* Set branches to the level below */
748 for (j
= 0; j
< UBIFS_LPT_FANOUT
; j
++) {
750 if (boffs
+ bsz
> c
->leb_size
) {
754 nnode
->nbranch
[j
].lnum
= blnum
;
755 nnode
->nbranch
[j
].offs
= boffs
;
759 nnode
->nbranch
[j
].lnum
= 0;
760 nnode
->nbranch
[j
].offs
= 0;
763 nnode
->num
= calc_nnode_num(row
, i
);
764 ubifs_pack_nnode(c
, p
, nnode
);
768 /* Only 1 nnode at this level, so it is the root */
771 /* Update the information about the level below */
778 /* Need to add LPT's save table */
779 if (len
+ c
->lsave_sz
> c
->leb_size
) {
780 alen
= ALIGN(len
, c
->min_io_size
);
781 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
782 memset(p
, 0xff, alen
- len
);
783 err
= ubi_leb_change(c
->ubi
, lnum
++, buf
, alen
,
791 c
->lsave_lnum
= lnum
;
794 for (i
= 0; i
< c
->lsave_cnt
&& i
< *main_lebs
; i
++)
795 lsave
[i
] = c
->main_first
+ i
;
796 for (; i
< c
->lsave_cnt
; i
++)
797 lsave
[i
] = c
->main_first
;
799 ubifs_pack_lsave(c
, p
, lsave
);
804 /* Need to add LPT's own LEB properties table */
805 if (len
+ c
->ltab_sz
> c
->leb_size
) {
806 alen
= ALIGN(len
, c
->min_io_size
);
807 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
808 memset(p
, 0xff, alen
- len
);
809 err
= ubi_leb_change(c
->ubi
, lnum
++, buf
, alen
, UBI_SHORTTERM
);
819 /* Update ltab before packing it */
821 alen
= ALIGN(len
, c
->min_io_size
);
822 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
824 ubifs_pack_ltab(c
, p
, ltab
);
827 /* Write remaining buffer */
828 memset(p
, 0xff, alen
- len
);
829 err
= ubi_leb_change(c
->ubi
, lnum
, buf
, alen
, UBI_SHORTTERM
);
833 c
->nhead_lnum
= lnum
;
834 c
->nhead_offs
= ALIGN(len
, c
->min_io_size
);
836 dbg_lp("space_bits %d", c
->space_bits
);
837 dbg_lp("lpt_lnum_bits %d", c
->lpt_lnum_bits
);
838 dbg_lp("lpt_offs_bits %d", c
->lpt_offs_bits
);
839 dbg_lp("lpt_spc_bits %d", c
->lpt_spc_bits
);
840 dbg_lp("pcnt_bits %d", c
->pcnt_bits
);
841 dbg_lp("lnum_bits %d", c
->lnum_bits
);
842 dbg_lp("pnode_sz %d", c
->pnode_sz
);
843 dbg_lp("nnode_sz %d", c
->nnode_sz
);
844 dbg_lp("ltab_sz %d", c
->ltab_sz
);
845 dbg_lp("lsave_sz %d", c
->lsave_sz
);
846 dbg_lp("lsave_cnt %d", c
->lsave_cnt
);
847 dbg_lp("lpt_hght %d", c
->lpt_hght
);
848 dbg_lp("big_lpt %d", c
->big_lpt
);
849 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
850 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
851 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
853 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
865 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
866 * @c: UBIFS file-system description object
869 * When a pnode is loaded into memory, the LEB properties it contains are added,
870 * by this function, to the LEB category lists and heaps.
872 static void update_cats(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
876 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
877 int cat
= pnode
->lprops
[i
].flags
& LPROPS_CAT_MASK
;
878 int lnum
= pnode
->lprops
[i
].lnum
;
882 ubifs_add_to_cat(c
, &pnode
->lprops
[i
], cat
);
887 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
888 * @c: UBIFS file-system description object
889 * @old_pnode: pnode copied
890 * @new_pnode: pnode copy
892 * During commit it is sometimes necessary to copy a pnode
893 * (see dirty_cow_pnode). When that happens, references in
894 * category lists and heaps must be replaced. This function does that.
896 static void replace_cats(struct ubifs_info
*c
, struct ubifs_pnode
*old_pnode
,
897 struct ubifs_pnode
*new_pnode
)
901 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
902 if (!new_pnode
->lprops
[i
].lnum
)
904 ubifs_replace_cat(c
, &old_pnode
->lprops
[i
],
905 &new_pnode
->lprops
[i
]);
910 * check_lpt_crc - check LPT node crc is correct.
911 * @c: UBIFS file-system description object
912 * @buf: buffer containing node
913 * @len: length of node
915 * This function returns %0 on success and a negative error code on failure.
917 static int check_lpt_crc(void *buf
, int len
)
921 uint16_t crc
, calc_crc
;
923 crc
= ubifs_unpack_bits(&addr
, &pos
, UBIFS_LPT_CRC_BITS
);
924 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
925 len
- UBIFS_LPT_CRC_BYTES
);
926 if (crc
!= calc_crc
) {
927 ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc
,
936 * check_lpt_type - check LPT node type is correct.
937 * @c: UBIFS file-system description object
938 * @addr: address of type bit field is passed and returned updated here
939 * @pos: position of type bit field is passed and returned updated here
940 * @type: expected type
942 * This function returns %0 on success and a negative error code on failure.
944 static int check_lpt_type(uint8_t **addr
, int *pos
, int type
)
948 node_type
= ubifs_unpack_bits(addr
, pos
, UBIFS_LPT_TYPE_BITS
);
949 if (node_type
!= type
) {
950 ubifs_err("invalid type (%d) in LPT node type %d", node_type
,
959 * unpack_pnode - unpack a pnode.
960 * @c: UBIFS file-system description object
961 * @buf: buffer containing packed pnode to unpack
962 * @pnode: pnode structure to fill
964 * This function returns %0 on success and a negative error code on failure.
966 static int unpack_pnode(const struct ubifs_info
*c
, void *buf
,
967 struct ubifs_pnode
*pnode
)
969 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
972 err
= check_lpt_type(&addr
, &pos
, UBIFS_LPT_PNODE
);
976 pnode
->num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
977 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
978 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
980 lprops
->free
= ubifs_unpack_bits(&addr
, &pos
, c
->space_bits
);
982 lprops
->dirty
= ubifs_unpack_bits(&addr
, &pos
, c
->space_bits
);
985 if (ubifs_unpack_bits(&addr
, &pos
, 1))
986 lprops
->flags
= LPROPS_INDEX
;
989 lprops
->flags
|= ubifs_categorize_lprops(c
, lprops
);
991 err
= check_lpt_crc(buf
, c
->pnode_sz
);
996 * ubifs_unpack_nnode - unpack a nnode.
997 * @c: UBIFS file-system description object
998 * @buf: buffer containing packed nnode to unpack
999 * @nnode: nnode structure to fill
1001 * This function returns %0 on success and a negative error code on failure.
1003 int ubifs_unpack_nnode(const struct ubifs_info
*c
, void *buf
,
1004 struct ubifs_nnode
*nnode
)
1006 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1007 int i
, pos
= 0, err
;
1009 err
= check_lpt_type(&addr
, &pos
, UBIFS_LPT_NNODE
);
1013 nnode
->num
= ubifs_unpack_bits(&addr
, &pos
, c
->pcnt_bits
);
1014 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1017 lnum
= ubifs_unpack_bits(&addr
, &pos
, c
->lpt_lnum_bits
) +
1019 if (lnum
== c
->lpt_last
+ 1)
1021 nnode
->nbranch
[i
].lnum
= lnum
;
1022 nnode
->nbranch
[i
].offs
= ubifs_unpack_bits(&addr
, &pos
,
1025 err
= check_lpt_crc(buf
, c
->nnode_sz
);
1030 * unpack_ltab - unpack the LPT's own lprops table.
1031 * @c: UBIFS file-system description object
1032 * @buf: buffer from which to unpack
1034 * This function returns %0 on success and a negative error code on failure.
1036 static int unpack_ltab(const struct ubifs_info
*c
, void *buf
)
1038 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1039 int i
, pos
= 0, err
;
1041 err
= check_lpt_type(&addr
, &pos
, UBIFS_LPT_LTAB
);
1044 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1045 int free
= ubifs_unpack_bits(&addr
, &pos
, c
->lpt_spc_bits
);
1046 int dirty
= ubifs_unpack_bits(&addr
, &pos
, c
->lpt_spc_bits
);
1048 if (free
< 0 || free
> c
->leb_size
|| dirty
< 0 ||
1049 dirty
> c
->leb_size
|| free
+ dirty
> c
->leb_size
)
1052 c
->ltab
[i
].free
= free
;
1053 c
->ltab
[i
].dirty
= dirty
;
1057 err
= check_lpt_crc(buf
, c
->ltab_sz
);
1062 * unpack_lsave - unpack the LPT's save table.
1063 * @c: UBIFS file-system description object
1064 * @buf: buffer from which to unpack
1066 * This function returns %0 on success and a negative error code on failure.
1068 static int unpack_lsave(const struct ubifs_info
*c
, void *buf
)
1070 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1071 int i
, pos
= 0, err
;
1073 err
= check_lpt_type(&addr
, &pos
, UBIFS_LPT_LSAVE
);
1076 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1077 int lnum
= ubifs_unpack_bits(&addr
, &pos
, c
->lnum_bits
);
1079 if (lnum
< c
->main_first
|| lnum
>= c
->leb_cnt
)
1083 err
= check_lpt_crc(buf
, c
->lsave_sz
);
1088 * validate_nnode - validate a nnode.
1089 * @c: UBIFS file-system description object
1090 * @nnode: nnode to validate
1091 * @parent: parent nnode (or NULL for the root nnode)
1092 * @iip: index in parent
1094 * This function returns %0 on success and a negative error code on failure.
1096 static int validate_nnode(const struct ubifs_info
*c
, struct ubifs_nnode
*nnode
,
1097 struct ubifs_nnode
*parent
, int iip
)
1099 int i
, lvl
, max_offs
;
1102 int num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1104 if (nnode
->num
!= num
)
1107 lvl
= parent
? parent
->level
- 1 : c
->lpt_hght
;
1111 max_offs
= c
->leb_size
- c
->pnode_sz
;
1113 max_offs
= c
->leb_size
- c
->nnode_sz
;
1114 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1115 int lnum
= nnode
->nbranch
[i
].lnum
;
1116 int offs
= nnode
->nbranch
[i
].offs
;
1123 if (lnum
< c
->lpt_first
|| lnum
> c
->lpt_last
)
1125 if (offs
< 0 || offs
> max_offs
)
1132 * validate_pnode - validate a pnode.
1133 * @c: UBIFS file-system description object
1134 * @pnode: pnode to validate
1135 * @parent: parent nnode
1136 * @iip: index in parent
1138 * This function returns %0 on success and a negative error code on failure.
1140 static int validate_pnode(const struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
1141 struct ubifs_nnode
*parent
, int iip
)
1146 int num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1148 if (pnode
->num
!= num
)
1151 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1152 int free
= pnode
->lprops
[i
].free
;
1153 int dirty
= pnode
->lprops
[i
].dirty
;
1155 if (free
< 0 || free
> c
->leb_size
|| free
% c
->min_io_size
||
1158 if (dirty
< 0 || dirty
> c
->leb_size
|| (dirty
& 7))
1160 if (dirty
+ free
> c
->leb_size
)
1167 * set_pnode_lnum - set LEB numbers on a pnode.
1168 * @c: UBIFS file-system description object
1169 * @pnode: pnode to update
1171 * This function calculates the LEB numbers for the LEB properties it contains
1172 * based on the pnode number.
1174 static void set_pnode_lnum(const struct ubifs_info
*c
,
1175 struct ubifs_pnode
*pnode
)
1179 lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + c
->main_first
;
1180 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1181 if (lnum
>= c
->leb_cnt
)
1183 pnode
->lprops
[i
].lnum
= lnum
++;
1188 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1189 * @c: UBIFS file-system description object
1190 * @parent: parent nnode (or NULL for the root)
1191 * @iip: index in parent
1193 * This function returns %0 on success and a negative error code on failure.
1195 int ubifs_read_nnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1197 struct ubifs_nbranch
*branch
= NULL
;
1198 struct ubifs_nnode
*nnode
= NULL
;
1199 void *buf
= c
->lpt_nod_buf
;
1200 int err
, lnum
, offs
;
1203 branch
= &parent
->nbranch
[iip
];
1204 lnum
= branch
->lnum
;
1205 offs
= branch
->offs
;
1210 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_NOFS
);
1217 * This nnode was not written which just means that the LEB
1218 * properties in the subtree below it describe empty LEBs. We
1219 * make the nnode as though we had read it, which in fact means
1220 * doing almost nothing.
1223 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1225 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, c
->nnode_sz
);
1228 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1232 err
= validate_nnode(c
, nnode
, parent
, iip
);
1236 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1238 branch
->nnode
= nnode
;
1239 nnode
->level
= parent
->level
- 1;
1242 nnode
->level
= c
->lpt_hght
;
1244 nnode
->parent
= parent
;
1249 ubifs_err("error %d reading nnode at %d:%d", err
, lnum
, offs
);
1255 * read_pnode - read a pnode from flash and link it to the tree in memory.
1256 * @c: UBIFS file-system description object
1257 * @parent: parent nnode
1258 * @iip: index in parent
1260 * This function returns %0 on success and a negative error code on failure.
1262 static int read_pnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1264 struct ubifs_nbranch
*branch
;
1265 struct ubifs_pnode
*pnode
= NULL
;
1266 void *buf
= c
->lpt_nod_buf
;
1267 int err
, lnum
, offs
;
1269 branch
= &parent
->nbranch
[iip
];
1270 lnum
= branch
->lnum
;
1271 offs
= branch
->offs
;
1272 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_NOFS
);
1278 * This pnode was not written which just means that the LEB
1279 * properties in it describe empty LEBs. We make the pnode as
1280 * though we had read it.
1285 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1286 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1287 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
1289 lprops
->free
= c
->leb_size
;
1290 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
1293 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, c
->pnode_sz
);
1296 err
= unpack_pnode(c
, buf
, pnode
);
1300 err
= validate_pnode(c
, pnode
, parent
, iip
);
1304 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1305 branch
->pnode
= pnode
;
1306 pnode
->parent
= parent
;
1308 set_pnode_lnum(c
, pnode
);
1309 c
->pnodes_have
+= 1;
1313 ubifs_err("error %d reading pnode at %d:%d", err
, lnum
, offs
);
1314 dbg_dump_pnode(c
, pnode
, parent
, iip
);
1315 dbg_msg("calc num: %d", calc_pnode_num_from_parent(c
, parent
, iip
));
1321 * read_ltab - read LPT's own lprops table.
1322 * @c: UBIFS file-system description object
1324 * This function returns %0 on success and a negative error code on failure.
1326 static int read_ltab(struct ubifs_info
*c
)
1331 buf
= vmalloc(c
->ltab_sz
);
1334 err
= ubi_read(c
->ubi
, c
->ltab_lnum
, buf
, c
->ltab_offs
, c
->ltab_sz
);
1337 err
= unpack_ltab(c
, buf
);
1344 * read_lsave - read LPT's save table.
1345 * @c: UBIFS file-system description object
1347 * This function returns %0 on success and a negative error code on failure.
1349 static int read_lsave(struct ubifs_info
*c
)
1354 buf
= vmalloc(c
->lsave_sz
);
1357 err
= ubi_read(c
->ubi
, c
->lsave_lnum
, buf
, c
->lsave_offs
, c
->lsave_sz
);
1360 err
= unpack_lsave(c
, buf
);
1363 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1364 int lnum
= c
->lsave
[i
];
1365 struct ubifs_lprops
*lprops
;
1368 * Due to automatic resizing, the values in the lsave table
1369 * could be beyond the volume size - just ignore them.
1371 if (lnum
>= c
->leb_cnt
)
1373 lprops
= ubifs_lpt_lookup(c
, lnum
);
1374 if (IS_ERR(lprops
)) {
1375 err
= PTR_ERR(lprops
);
1385 * ubifs_get_nnode - get a nnode.
1386 * @c: UBIFS file-system description object
1387 * @parent: parent nnode (or NULL for the root)
1388 * @iip: index in parent
1390 * This function returns a pointer to the nnode on success or a negative error
1393 struct ubifs_nnode
*ubifs_get_nnode(struct ubifs_info
*c
,
1394 struct ubifs_nnode
*parent
, int iip
)
1396 struct ubifs_nbranch
*branch
;
1397 struct ubifs_nnode
*nnode
;
1400 branch
= &parent
->nbranch
[iip
];
1401 nnode
= branch
->nnode
;
1404 err
= ubifs_read_nnode(c
, parent
, iip
);
1406 return ERR_PTR(err
);
1407 return branch
->nnode
;
1411 * ubifs_get_pnode - get a pnode.
1412 * @c: UBIFS file-system description object
1413 * @parent: parent nnode
1414 * @iip: index in parent
1416 * This function returns a pointer to the pnode on success or a negative error
1419 struct ubifs_pnode
*ubifs_get_pnode(struct ubifs_info
*c
,
1420 struct ubifs_nnode
*parent
, int iip
)
1422 struct ubifs_nbranch
*branch
;
1423 struct ubifs_pnode
*pnode
;
1426 branch
= &parent
->nbranch
[iip
];
1427 pnode
= branch
->pnode
;
1430 err
= read_pnode(c
, parent
, iip
);
1432 return ERR_PTR(err
);
1433 update_cats(c
, branch
->pnode
);
1434 return branch
->pnode
;
1438 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1439 * @c: UBIFS file-system description object
1440 * @lnum: LEB number to lookup
1442 * This function returns a pointer to the LEB properties on success or a
1443 * negative error code on failure.
1445 struct ubifs_lprops
*ubifs_lpt_lookup(struct ubifs_info
*c
, int lnum
)
1447 int err
, i
, h
, iip
, shft
;
1448 struct ubifs_nnode
*nnode
;
1449 struct ubifs_pnode
*pnode
;
1452 err
= ubifs_read_nnode(c
, NULL
, 0);
1454 return ERR_PTR(err
);
1457 i
= lnum
- c
->main_first
;
1458 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1459 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1460 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1461 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1462 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1464 return ERR_CAST(nnode
);
1466 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1467 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1468 pnode
= ubifs_get_pnode(c
, nnode
, iip
);
1470 return ERR_CAST(pnode
);
1471 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1472 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum
,
1473 pnode
->lprops
[iip
].free
, pnode
->lprops
[iip
].dirty
,
1474 pnode
->lprops
[iip
].flags
);
1475 return &pnode
->lprops
[iip
];
1479 * dirty_cow_nnode - ensure a nnode is not being committed.
1480 * @c: UBIFS file-system description object
1481 * @nnode: nnode to check
1483 * Returns dirtied nnode on success or negative error code on failure.
1485 static struct ubifs_nnode
*dirty_cow_nnode(struct ubifs_info
*c
,
1486 struct ubifs_nnode
*nnode
)
1488 struct ubifs_nnode
*n
;
1491 if (!test_bit(COW_CNODE
, &nnode
->flags
)) {
1492 /* nnode is not being committed */
1493 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
1494 c
->dirty_nn_cnt
+= 1;
1495 ubifs_add_nnode_dirt(c
, nnode
);
1500 /* nnode is being committed, so copy it */
1501 n
= kmalloc(sizeof(struct ubifs_nnode
), GFP_NOFS
);
1503 return ERR_PTR(-ENOMEM
);
1505 memcpy(n
, nnode
, sizeof(struct ubifs_nnode
));
1507 __set_bit(DIRTY_CNODE
, &n
->flags
);
1508 __clear_bit(COW_CNODE
, &n
->flags
);
1510 /* The children now have new parent */
1511 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1512 struct ubifs_nbranch
*branch
= &n
->nbranch
[i
];
1515 branch
->cnode
->parent
= n
;
1518 ubifs_assert(!test_bit(OBSOLETE_CNODE
, &nnode
->flags
));
1519 __set_bit(OBSOLETE_CNODE
, &nnode
->flags
);
1521 c
->dirty_nn_cnt
+= 1;
1522 ubifs_add_nnode_dirt(c
, nnode
);
1524 nnode
->parent
->nbranch
[n
->iip
].nnode
= n
;
1531 * dirty_cow_pnode - ensure a pnode is not being committed.
1532 * @c: UBIFS file-system description object
1533 * @pnode: pnode to check
1535 * Returns dirtied pnode on success or negative error code on failure.
1537 static struct ubifs_pnode
*dirty_cow_pnode(struct ubifs_info
*c
,
1538 struct ubifs_pnode
*pnode
)
1540 struct ubifs_pnode
*p
;
1542 if (!test_bit(COW_CNODE
, &pnode
->flags
)) {
1543 /* pnode is not being committed */
1544 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
1545 c
->dirty_pn_cnt
+= 1;
1546 add_pnode_dirt(c
, pnode
);
1551 /* pnode is being committed, so copy it */
1552 p
= kmalloc(sizeof(struct ubifs_pnode
), GFP_NOFS
);
1554 return ERR_PTR(-ENOMEM
);
1556 memcpy(p
, pnode
, sizeof(struct ubifs_pnode
));
1558 __set_bit(DIRTY_CNODE
, &p
->flags
);
1559 __clear_bit(COW_CNODE
, &p
->flags
);
1560 replace_cats(c
, pnode
, p
);
1562 ubifs_assert(!test_bit(OBSOLETE_CNODE
, &pnode
->flags
));
1563 __set_bit(OBSOLETE_CNODE
, &pnode
->flags
);
1565 c
->dirty_pn_cnt
+= 1;
1566 add_pnode_dirt(c
, pnode
);
1567 pnode
->parent
->nbranch
[p
->iip
].pnode
= p
;
1572 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1573 * @c: UBIFS file-system description object
1574 * @lnum: LEB number to lookup
1576 * This function returns a pointer to the LEB properties on success or a
1577 * negative error code on failure.
1579 struct ubifs_lprops
*ubifs_lpt_lookup_dirty(struct ubifs_info
*c
, int lnum
)
1581 int err
, i
, h
, iip
, shft
;
1582 struct ubifs_nnode
*nnode
;
1583 struct ubifs_pnode
*pnode
;
1586 err
= ubifs_read_nnode(c
, NULL
, 0);
1588 return ERR_PTR(err
);
1591 nnode
= dirty_cow_nnode(c
, nnode
);
1593 return ERR_CAST(nnode
);
1594 i
= lnum
- c
->main_first
;
1595 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1596 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1597 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1598 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1599 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1601 return ERR_CAST(nnode
);
1602 nnode
= dirty_cow_nnode(c
, nnode
);
1604 return ERR_CAST(nnode
);
1606 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1607 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1608 pnode
= ubifs_get_pnode(c
, nnode
, iip
);
1610 return ERR_CAST(pnode
);
1611 pnode
= dirty_cow_pnode(c
, pnode
);
1613 return ERR_CAST(pnode
);
1614 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1615 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum
,
1616 pnode
->lprops
[iip
].free
, pnode
->lprops
[iip
].dirty
,
1617 pnode
->lprops
[iip
].flags
);
1618 ubifs_assert(test_bit(DIRTY_CNODE
, &pnode
->flags
));
1619 return &pnode
->lprops
[iip
];
1623 * lpt_init_rd - initialize the LPT for reading.
1624 * @c: UBIFS file-system description object
1626 * This function returns %0 on success and a negative error code on failure.
1628 static int lpt_init_rd(struct ubifs_info
*c
)
1632 c
->ltab
= vmalloc(sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1636 i
= max_t(int, c
->nnode_sz
, c
->pnode_sz
);
1637 c
->lpt_nod_buf
= kmalloc(i
, GFP_KERNEL
);
1638 if (!c
->lpt_nod_buf
)
1641 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++) {
1642 c
->lpt_heap
[i
].arr
= kmalloc(sizeof(void *) * LPT_HEAP_SZ
,
1644 if (!c
->lpt_heap
[i
].arr
)
1646 c
->lpt_heap
[i
].cnt
= 0;
1647 c
->lpt_heap
[i
].max_cnt
= LPT_HEAP_SZ
;
1650 c
->dirty_idx
.arr
= kmalloc(sizeof(void *) * LPT_HEAP_SZ
, GFP_KERNEL
);
1651 if (!c
->dirty_idx
.arr
)
1653 c
->dirty_idx
.cnt
= 0;
1654 c
->dirty_idx
.max_cnt
= LPT_HEAP_SZ
;
1660 dbg_lp("space_bits %d", c
->space_bits
);
1661 dbg_lp("lpt_lnum_bits %d", c
->lpt_lnum_bits
);
1662 dbg_lp("lpt_offs_bits %d", c
->lpt_offs_bits
);
1663 dbg_lp("lpt_spc_bits %d", c
->lpt_spc_bits
);
1664 dbg_lp("pcnt_bits %d", c
->pcnt_bits
);
1665 dbg_lp("lnum_bits %d", c
->lnum_bits
);
1666 dbg_lp("pnode_sz %d", c
->pnode_sz
);
1667 dbg_lp("nnode_sz %d", c
->nnode_sz
);
1668 dbg_lp("ltab_sz %d", c
->ltab_sz
);
1669 dbg_lp("lsave_sz %d", c
->lsave_sz
);
1670 dbg_lp("lsave_cnt %d", c
->lsave_cnt
);
1671 dbg_lp("lpt_hght %d", c
->lpt_hght
);
1672 dbg_lp("big_lpt %d", c
->big_lpt
);
1673 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
1674 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
1675 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
1677 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
1683 * lpt_init_wr - initialize the LPT for writing.
1684 * @c: UBIFS file-system description object
1686 * 'lpt_init_rd()' must have been called already.
1688 * This function returns %0 on success and a negative error code on failure.
1690 static int lpt_init_wr(struct ubifs_info
*c
)
1694 c
->ltab_cmt
= vmalloc(sizeof(struct ubifs_lpt_lprops
) * c
->lpt_lebs
);
1698 c
->lpt_buf
= vmalloc(c
->leb_size
);
1703 c
->lsave
= kmalloc(sizeof(int) * c
->lsave_cnt
, GFP_NOFS
);
1706 err
= read_lsave(c
);
1711 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1712 if (c
->ltab
[i
].free
== c
->leb_size
) {
1713 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
1722 * ubifs_lpt_init - initialize the LPT.
1723 * @c: UBIFS file-system description object
1724 * @rd: whether to initialize lpt for reading
1725 * @wr: whether to initialize lpt for writing
1727 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1728 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1731 * This function returns %0 on success and a negative error code on failure.
1733 int ubifs_lpt_init(struct ubifs_info
*c
, int rd
, int wr
)
1738 err
= lpt_init_rd(c
);
1744 err
= lpt_init_wr(c
);
1753 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1754 * @nnode: where to keep a nnode
1755 * @pnode: where to keep a pnode
1756 * @cnode: where to keep a cnode
1757 * @in_tree: is the node in the tree in memory
1758 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1760 * @ptr.pnode: ditto for pnode
1761 * @ptr.cnode: ditto for cnode
1763 struct lpt_scan_node
{
1765 struct ubifs_nnode nnode
;
1766 struct ubifs_pnode pnode
;
1767 struct ubifs_cnode cnode
;
1771 struct ubifs_nnode
*nnode
;
1772 struct ubifs_pnode
*pnode
;
1773 struct ubifs_cnode
*cnode
;
1778 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1779 * @c: the UBIFS file-system description object
1780 * @path: where to put the nnode
1781 * @parent: parent of the nnode
1782 * @iip: index in parent of the nnode
1784 * This function returns a pointer to the nnode on success or a negative error
1787 static struct ubifs_nnode
*scan_get_nnode(struct ubifs_info
*c
,
1788 struct lpt_scan_node
*path
,
1789 struct ubifs_nnode
*parent
, int iip
)
1791 struct ubifs_nbranch
*branch
;
1792 struct ubifs_nnode
*nnode
;
1793 void *buf
= c
->lpt_nod_buf
;
1796 branch
= &parent
->nbranch
[iip
];
1797 nnode
= branch
->nnode
;
1800 path
->ptr
.nnode
= nnode
;
1803 nnode
= &path
->nnode
;
1805 path
->ptr
.nnode
= nnode
;
1806 memset(nnode
, 0, sizeof(struct ubifs_nnode
));
1807 if (branch
->lnum
== 0) {
1809 * This nnode was not written which just means that the LEB
1810 * properties in the subtree below it describe empty LEBs. We
1811 * make the nnode as though we had read it, which in fact means
1812 * doing almost nothing.
1815 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1817 err
= ubi_read(c
->ubi
, branch
->lnum
, buf
, branch
->offs
,
1820 return ERR_PTR(err
);
1821 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1823 return ERR_PTR(err
);
1825 err
= validate_nnode(c
, nnode
, parent
, iip
);
1827 return ERR_PTR(err
);
1829 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1830 nnode
->level
= parent
->level
- 1;
1831 nnode
->parent
= parent
;
1837 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1838 * @c: the UBIFS file-system description object
1839 * @path: where to put the pnode
1840 * @parent: parent of the pnode
1841 * @iip: index in parent of the pnode
1843 * This function returns a pointer to the pnode on success or a negative error
1846 static struct ubifs_pnode
*scan_get_pnode(struct ubifs_info
*c
,
1847 struct lpt_scan_node
*path
,
1848 struct ubifs_nnode
*parent
, int iip
)
1850 struct ubifs_nbranch
*branch
;
1851 struct ubifs_pnode
*pnode
;
1852 void *buf
= c
->lpt_nod_buf
;
1855 branch
= &parent
->nbranch
[iip
];
1856 pnode
= branch
->pnode
;
1859 path
->ptr
.pnode
= pnode
;
1862 pnode
= &path
->pnode
;
1864 path
->ptr
.pnode
= pnode
;
1865 memset(pnode
, 0, sizeof(struct ubifs_pnode
));
1866 if (branch
->lnum
== 0) {
1868 * This pnode was not written which just means that the LEB
1869 * properties in it describe empty LEBs. We make the pnode as
1870 * though we had read it.
1875 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1876 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1877 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
1879 lprops
->free
= c
->leb_size
;
1880 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
1883 ubifs_assert(branch
->lnum
>= c
->lpt_first
&&
1884 branch
->lnum
<= c
->lpt_last
);
1885 ubifs_assert(branch
->offs
>= 0 && branch
->offs
< c
->leb_size
);
1886 err
= ubi_read(c
->ubi
, branch
->lnum
, buf
, branch
->offs
,
1889 return ERR_PTR(err
);
1890 err
= unpack_pnode(c
, buf
, pnode
);
1892 return ERR_PTR(err
);
1894 err
= validate_pnode(c
, pnode
, parent
, iip
);
1896 return ERR_PTR(err
);
1898 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1899 pnode
->parent
= parent
;
1901 set_pnode_lnum(c
, pnode
);
1906 * ubifs_lpt_scan_nolock - scan the LPT.
1907 * @c: the UBIFS file-system description object
1908 * @start_lnum: LEB number from which to start scanning
1909 * @end_lnum: LEB number at which to stop scanning
1910 * @scan_cb: callback function called for each lprops
1911 * @data: data to be passed to the callback function
1913 * This function returns %0 on success and a negative error code on failure.
1915 int ubifs_lpt_scan_nolock(struct ubifs_info
*c
, int start_lnum
, int end_lnum
,
1916 ubifs_lpt_scan_callback scan_cb
, void *data
)
1918 int err
= 0, i
, h
, iip
, shft
;
1919 struct ubifs_nnode
*nnode
;
1920 struct ubifs_pnode
*pnode
;
1921 struct lpt_scan_node
*path
;
1923 if (start_lnum
== -1) {
1924 start_lnum
= end_lnum
+ 1;
1925 if (start_lnum
>= c
->leb_cnt
)
1926 start_lnum
= c
->main_first
;
1929 ubifs_assert(start_lnum
>= c
->main_first
&& start_lnum
< c
->leb_cnt
);
1930 ubifs_assert(end_lnum
>= c
->main_first
&& end_lnum
< c
->leb_cnt
);
1933 err
= ubifs_read_nnode(c
, NULL
, 0);
1938 path
= kmalloc(sizeof(struct lpt_scan_node
) * (c
->lpt_hght
+ 1),
1943 path
[0].ptr
.nnode
= c
->nroot
;
1944 path
[0].in_tree
= 1;
1946 /* Descend to the pnode containing start_lnum */
1948 i
= start_lnum
- c
->main_first
;
1949 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1950 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1951 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1952 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1953 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
1954 if (IS_ERR(nnode
)) {
1955 err
= PTR_ERR(nnode
);
1959 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1960 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1961 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
1962 if (IS_ERR(pnode
)) {
1963 err
= PTR_ERR(pnode
);
1966 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1968 /* Loop for each lprops */
1970 struct ubifs_lprops
*lprops
= &pnode
->lprops
[iip
];
1971 int ret
, lnum
= lprops
->lnum
;
1973 ret
= scan_cb(c
, lprops
, path
[h
].in_tree
, data
);
1978 if (ret
& LPT_SCAN_ADD
) {
1979 /* Add all the nodes in path to the tree in memory */
1980 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1981 const size_t sz
= sizeof(struct ubifs_nnode
);
1982 struct ubifs_nnode
*parent
;
1984 if (path
[h
].in_tree
)
1986 nnode
= kmalloc(sz
, GFP_NOFS
);
1991 memcpy(nnode
, &path
[h
].nnode
, sz
);
1992 parent
= nnode
->parent
;
1993 parent
->nbranch
[nnode
->iip
].nnode
= nnode
;
1994 path
[h
].ptr
.nnode
= nnode
;
1995 path
[h
].in_tree
= 1;
1996 path
[h
+ 1].cnode
.parent
= nnode
;
1998 if (path
[h
].in_tree
)
1999 ubifs_ensure_cat(c
, lprops
);
2001 const size_t sz
= sizeof(struct ubifs_pnode
);
2002 struct ubifs_nnode
*parent
;
2004 pnode
= kmalloc(sz
, GFP_NOFS
);
2009 memcpy(pnode
, &path
[h
].pnode
, sz
);
2010 parent
= pnode
->parent
;
2011 parent
->nbranch
[pnode
->iip
].pnode
= pnode
;
2012 path
[h
].ptr
.pnode
= pnode
;
2013 path
[h
].in_tree
= 1;
2014 update_cats(c
, pnode
);
2015 c
->pnodes_have
+= 1;
2017 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)
2021 err
= dbg_check_cats(c
);
2025 if (ret
& LPT_SCAN_STOP
) {
2029 /* Get the next lprops */
2030 if (lnum
== end_lnum
) {
2032 * We got to the end without finding what we were
2038 if (lnum
+ 1 >= c
->leb_cnt
) {
2039 /* Wrap-around to the beginning */
2040 start_lnum
= c
->main_first
;
2043 if (iip
+ 1 < UBIFS_LPT_FANOUT
) {
2044 /* Next lprops is in the same pnode */
2048 /* We need to get the next pnode. Go up until we can go right */
2052 ubifs_assert(h
>= 0);
2053 nnode
= path
[h
].ptr
.nnode
;
2054 if (iip
+ 1 < UBIFS_LPT_FANOUT
)
2060 /* Descend to the pnode */
2062 for (; h
< c
->lpt_hght
; h
++) {
2063 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
2064 if (IS_ERR(nnode
)) {
2065 err
= PTR_ERR(nnode
);
2070 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
2071 if (IS_ERR(pnode
)) {
2072 err
= PTR_ERR(pnode
);
2082 #ifdef CONFIG_UBIFS_FS_DEBUG
2085 * dbg_chk_pnode - check a pnode.
2086 * @c: the UBIFS file-system description object
2087 * @pnode: pnode to check
2088 * @col: pnode column
2090 * This function returns %0 on success and a negative error code on failure.
2092 static int dbg_chk_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
2097 if (pnode
->num
!= col
) {
2098 dbg_err("pnode num %d expected %d parent num %d iip %d",
2099 pnode
->num
, col
, pnode
->parent
->num
, pnode
->iip
);
2102 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
2103 struct ubifs_lprops
*lp
, *lprops
= &pnode
->lprops
[i
];
2104 int lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + i
+
2106 int found
, cat
= lprops
->flags
& LPROPS_CAT_MASK
;
2107 struct ubifs_lpt_heap
*heap
;
2108 struct list_head
*list
= NULL
;
2110 if (lnum
>= c
->leb_cnt
)
2112 if (lprops
->lnum
!= lnum
) {
2113 dbg_err("bad LEB number %d expected %d",
2114 lprops
->lnum
, lnum
);
2117 if (lprops
->flags
& LPROPS_TAKEN
) {
2118 if (cat
!= LPROPS_UNCAT
) {
2119 dbg_err("LEB %d taken but not uncat %d",
2125 if (lprops
->flags
& LPROPS_INDEX
) {
2128 case LPROPS_DIRTY_IDX
:
2129 case LPROPS_FRDI_IDX
:
2132 dbg_err("LEB %d index but cat %d",
2142 case LPROPS_FREEABLE
:
2145 dbg_err("LEB %d not index but cat %d",
2152 list
= &c
->uncat_list
;
2155 list
= &c
->empty_list
;
2157 case LPROPS_FREEABLE
:
2158 list
= &c
->freeable_list
;
2160 case LPROPS_FRDI_IDX
:
2161 list
= &c
->frdi_idx_list
;
2167 case LPROPS_DIRTY_IDX
:
2169 heap
= &c
->lpt_heap
[cat
- 1];
2170 if (lprops
->hpos
< heap
->cnt
&&
2171 heap
->arr
[lprops
->hpos
] == lprops
)
2176 case LPROPS_FREEABLE
:
2177 case LPROPS_FRDI_IDX
:
2178 list_for_each_entry(lp
, list
, list
)
2186 dbg_err("LEB %d cat %d not found in cat heap/list",
2192 if (lprops
->free
!= c
->leb_size
) {
2193 dbg_err("LEB %d cat %d free %d dirty %d",
2194 lprops
->lnum
, cat
, lprops
->free
,
2198 case LPROPS_FREEABLE
:
2199 case LPROPS_FRDI_IDX
:
2200 if (lprops
->free
+ lprops
->dirty
!= c
->leb_size
) {
2201 dbg_err("LEB %d cat %d free %d dirty %d",
2202 lprops
->lnum
, cat
, lprops
->free
,
2212 * dbg_check_lpt_nodes - check nnodes and pnodes.
2213 * @c: the UBIFS file-system description object
2214 * @cnode: next cnode (nnode or pnode) to check
2215 * @row: row of cnode (root is zero)
2216 * @col: column of cnode (leftmost is zero)
2218 * This function returns %0 on success and a negative error code on failure.
2220 int dbg_check_lpt_nodes(struct ubifs_info
*c
, struct ubifs_cnode
*cnode
,
2223 struct ubifs_nnode
*nnode
, *nn
;
2224 struct ubifs_cnode
*cn
;
2225 int num
, iip
= 0, err
;
2227 if (!(ubifs_chk_flags
& UBIFS_CHK_LPROPS
))
2231 ubifs_assert(row
>= 0);
2232 nnode
= cnode
->parent
;
2234 /* cnode is a nnode */
2235 num
= calc_nnode_num(row
, col
);
2236 if (cnode
->num
!= num
) {
2237 dbg_err("nnode num %d expected %d "
2238 "parent num %d iip %d", cnode
->num
, num
,
2239 (nnode
? nnode
->num
: 0), cnode
->iip
);
2242 nn
= (struct ubifs_nnode
*)cnode
;
2243 while (iip
< UBIFS_LPT_FANOUT
) {
2244 cn
= nn
->nbranch
[iip
].cnode
;
2248 col
<<= UBIFS_LPT_FANOUT_SHIFT
;
2257 if (iip
< UBIFS_LPT_FANOUT
)
2260 struct ubifs_pnode
*pnode
;
2262 /* cnode is a pnode */
2263 pnode
= (struct ubifs_pnode
*)cnode
;
2264 err
= dbg_chk_pnode(c
, pnode
, col
);
2268 /* Go up and to the right */
2270 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
2271 iip
= cnode
->iip
+ 1;
2272 cnode
= (struct ubifs_cnode
*)nnode
;
2277 #endif /* CONFIG_UBIFS_FS_DEBUG */