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(c
, "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(c
, "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(c
, "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 * @c: UBIFS file-system description object
229 * @addr: address at which to pack (passed and next address returned)
230 * @pos: bit position at which to pack (passed and next position returned)
231 * @val: value to pack
232 * @nrbits: number of bits of value to pack (1-32)
234 static void pack_bits(const struct ubifs_info
*c
, uint8_t **addr
, int *pos
, uint32_t val
, int nrbits
)
239 ubifs_assert(c
, nrbits
> 0);
240 ubifs_assert(c
, nrbits
<= 32);
241 ubifs_assert(c
, *pos
>= 0);
242 ubifs_assert(c
, *pos
< 8);
243 ubifs_assert(c
, (val
>> nrbits
) == 0 || nrbits
== 32);
245 *p
|= ((uint8_t)val
) << b
;
248 *++p
= (uint8_t)(val
>>= (8 - b
));
250 *++p
= (uint8_t)(val
>>= 8);
252 *++p
= (uint8_t)(val
>>= 8);
254 *++p
= (uint8_t)(val
>>= 8);
261 *++p
= (uint8_t)(val
>>= 8);
263 *++p
= (uint8_t)(val
>>= 8);
265 *++p
= (uint8_t)(val
>>= 8);
277 * ubifs_unpack_bits - unpack bit fields.
278 * @c: UBIFS file-system description object
279 * @addr: address at which to unpack (passed and next address returned)
280 * @pos: bit position at which to unpack (passed and next position returned)
281 * @nrbits: number of bits of value to unpack (1-32)
283 * This functions returns the value unpacked.
285 uint32_t ubifs_unpack_bits(const struct ubifs_info
*c
, uint8_t **addr
, int *pos
, int nrbits
)
287 const int k
= 32 - nrbits
;
290 uint32_t uninitialized_var(val
);
291 const int bytes
= (nrbits
+ b
+ 7) >> 3;
293 ubifs_assert(c
, nrbits
> 0);
294 ubifs_assert(c
, nrbits
<= 32);
295 ubifs_assert(c
, *pos
>= 0);
296 ubifs_assert(c
, *pos
< 8);
303 val
= p
[1] | ((uint32_t)p
[2] << 8);
306 val
= p
[1] | ((uint32_t)p
[2] << 8) |
307 ((uint32_t)p
[3] << 16);
310 val
= p
[1] | ((uint32_t)p
[2] << 8) |
311 ((uint32_t)p
[3] << 16) |
312 ((uint32_t)p
[4] << 24);
323 val
= p
[0] | ((uint32_t)p
[1] << 8);
326 val
= p
[0] | ((uint32_t)p
[1] << 8) |
327 ((uint32_t)p
[2] << 16);
330 val
= p
[0] | ((uint32_t)p
[1] << 8) |
331 ((uint32_t)p
[2] << 16) |
332 ((uint32_t)p
[3] << 24);
342 ubifs_assert(c
, (val
>> nrbits
) == 0 || nrbits
- b
== 32);
347 * ubifs_pack_pnode - pack all the bit fields of a pnode.
348 * @c: UBIFS file-system description object
349 * @buf: buffer into which to pack
350 * @pnode: pnode to pack
352 void ubifs_pack_pnode(struct ubifs_info
*c
, void *buf
,
353 struct ubifs_pnode
*pnode
)
355 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
359 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_PNODE
, UBIFS_LPT_TYPE_BITS
);
361 pack_bits(c
, &addr
, &pos
, pnode
->num
, c
->pcnt_bits
);
362 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
363 pack_bits(c
, &addr
, &pos
, pnode
->lprops
[i
].free
>> 3,
365 pack_bits(c
, &addr
, &pos
, pnode
->lprops
[i
].dirty
>> 3,
367 if (pnode
->lprops
[i
].flags
& LPROPS_INDEX
)
368 pack_bits(c
, &addr
, &pos
, 1, 1);
370 pack_bits(c
, &addr
, &pos
, 0, 1);
372 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
373 c
->pnode_sz
- UBIFS_LPT_CRC_BYTES
);
376 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
380 * ubifs_pack_nnode - pack all the bit fields of a nnode.
381 * @c: UBIFS file-system description object
382 * @buf: buffer into which to pack
383 * @nnode: nnode to pack
385 void ubifs_pack_nnode(struct ubifs_info
*c
, void *buf
,
386 struct ubifs_nnode
*nnode
)
388 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
392 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_NNODE
, UBIFS_LPT_TYPE_BITS
);
394 pack_bits(c
, &addr
, &pos
, nnode
->num
, c
->pcnt_bits
);
395 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
396 int lnum
= nnode
->nbranch
[i
].lnum
;
399 lnum
= c
->lpt_last
+ 1;
400 pack_bits(c
, &addr
, &pos
, lnum
- c
->lpt_first
, c
->lpt_lnum_bits
);
401 pack_bits(c
, &addr
, &pos
, nnode
->nbranch
[i
].offs
,
404 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
405 c
->nnode_sz
- UBIFS_LPT_CRC_BYTES
);
408 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
412 * ubifs_pack_ltab - pack the LPT's own lprops table.
413 * @c: UBIFS file-system description object
414 * @buf: buffer into which to pack
415 * @ltab: LPT's own lprops table to pack
417 void ubifs_pack_ltab(struct ubifs_info
*c
, void *buf
,
418 struct ubifs_lpt_lprops
*ltab
)
420 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
424 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_LTAB
, UBIFS_LPT_TYPE_BITS
);
425 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
426 pack_bits(c
, &addr
, &pos
, ltab
[i
].free
, c
->lpt_spc_bits
);
427 pack_bits(c
, &addr
, &pos
, ltab
[i
].dirty
, c
->lpt_spc_bits
);
429 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
430 c
->ltab_sz
- UBIFS_LPT_CRC_BYTES
);
433 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
437 * ubifs_pack_lsave - pack the LPT's save table.
438 * @c: UBIFS file-system description object
439 * @buf: buffer into which to pack
440 * @lsave: LPT's save table to pack
442 void ubifs_pack_lsave(struct ubifs_info
*c
, void *buf
, int *lsave
)
444 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
448 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_LSAVE
, UBIFS_LPT_TYPE_BITS
);
449 for (i
= 0; i
< c
->lsave_cnt
; i
++)
450 pack_bits(c
, &addr
, &pos
, lsave
[i
], c
->lnum_bits
);
451 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
452 c
->lsave_sz
- UBIFS_LPT_CRC_BYTES
);
455 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
459 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
460 * @c: UBIFS file-system description object
461 * @lnum: LEB number to which to add dirty space
462 * @dirty: amount of dirty space to add
464 void ubifs_add_lpt_dirt(struct ubifs_info
*c
, int lnum
, int dirty
)
468 dbg_lp("LEB %d add %d to %d",
469 lnum
, dirty
, c
->ltab
[lnum
- c
->lpt_first
].dirty
);
470 ubifs_assert(c
, lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
471 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
475 * set_ltab - set LPT LEB properties.
476 * @c: UBIFS file-system description object
478 * @free: amount of free space
479 * @dirty: amount of dirty space
481 static void set_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
483 dbg_lp("LEB %d free %d dirty %d to %d %d",
484 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
485 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
486 ubifs_assert(c
, lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
487 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
488 c
->ltab
[lnum
- c
->lpt_first
].dirty
= dirty
;
492 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
493 * @c: UBIFS file-system description object
494 * @nnode: nnode for which to add dirt
496 void ubifs_add_nnode_dirt(struct ubifs_info
*c
, struct ubifs_nnode
*nnode
)
498 struct ubifs_nnode
*np
= nnode
->parent
;
501 ubifs_add_lpt_dirt(c
, np
->nbranch
[nnode
->iip
].lnum
,
504 ubifs_add_lpt_dirt(c
, c
->lpt_lnum
, c
->nnode_sz
);
505 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
506 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
507 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
513 * add_pnode_dirt - add dirty space to LPT LEB properties.
514 * @c: UBIFS file-system description object
515 * @pnode: pnode for which to add dirt
517 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
519 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
524 * calc_nnode_num - calculate nnode number.
525 * @row: the row in the tree (root is zero)
526 * @col: the column in the row (leftmost is zero)
528 * The nnode number is a number that uniquely identifies a nnode and can be used
529 * easily to traverse the tree from the root to that nnode.
531 * This function calculates and returns the nnode number for the nnode at @row
534 static int calc_nnode_num(int row
, int col
)
540 bits
= (col
& (UBIFS_LPT_FANOUT
- 1));
541 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
542 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
549 * calc_nnode_num_from_parent - calculate nnode number.
550 * @c: UBIFS file-system description object
551 * @parent: parent nnode
552 * @iip: index in parent
554 * The nnode number is a number that uniquely identifies a nnode and can be used
555 * easily to traverse the tree from the root to that nnode.
557 * This function calculates and returns the nnode number based on the parent's
558 * nnode number and the index in parent.
560 static int calc_nnode_num_from_parent(const struct ubifs_info
*c
,
561 struct ubifs_nnode
*parent
, int iip
)
567 shft
= (c
->lpt_hght
- parent
->level
) * UBIFS_LPT_FANOUT_SHIFT
;
568 num
= parent
->num
^ (1 << shft
);
569 num
|= (UBIFS_LPT_FANOUT
+ iip
) << shft
;
574 * calc_pnode_num_from_parent - calculate pnode number.
575 * @c: UBIFS file-system description object
576 * @parent: parent nnode
577 * @iip: index in parent
579 * The pnode number is a number that uniquely identifies a pnode and can be used
580 * easily to traverse the tree from the root to that pnode.
582 * This function calculates and returns the pnode number based on the parent's
583 * nnode number and the index in parent.
585 static int calc_pnode_num_from_parent(const struct ubifs_info
*c
,
586 struct ubifs_nnode
*parent
, int iip
)
588 int i
, n
= c
->lpt_hght
- 1, pnum
= parent
->num
, num
= 0;
590 for (i
= 0; i
< n
; i
++) {
591 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
592 num
|= pnum
& (UBIFS_LPT_FANOUT
- 1);
593 pnum
>>= UBIFS_LPT_FANOUT_SHIFT
;
595 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
601 * ubifs_create_dflt_lpt - create default LPT.
602 * @c: UBIFS file-system description object
603 * @main_lebs: number of main area LEBs is passed and returned here
604 * @lpt_first: LEB number of first LPT LEB
605 * @lpt_lebs: number of LEBs for LPT is passed and returned here
606 * @big_lpt: use big LPT model is passed and returned here
608 * This function returns %0 on success and a negative error code on failure.
610 int ubifs_create_dflt_lpt(struct ubifs_info
*c
, int *main_lebs
, int lpt_first
,
611 int *lpt_lebs
, int *big_lpt
)
613 int lnum
, err
= 0, node_sz
, iopos
, i
, j
, cnt
, len
, alen
, row
;
614 int blnum
, boffs
, bsz
, bcnt
;
615 struct ubifs_pnode
*pnode
= NULL
;
616 struct ubifs_nnode
*nnode
= NULL
;
617 void *buf
= NULL
, *p
;
618 struct ubifs_lpt_lprops
*ltab
= NULL
;
621 err
= calc_dflt_lpt_geom(c
, main_lebs
, big_lpt
);
624 *lpt_lebs
= c
->lpt_lebs
;
626 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
627 c
->lpt_first
= lpt_first
;
628 /* Needed by 'set_ltab()' */
629 c
->lpt_last
= lpt_first
+ c
->lpt_lebs
- 1;
630 /* Needed by 'ubifs_pack_lsave()' */
631 c
->main_first
= c
->leb_cnt
- *main_lebs
;
633 lsave
= kmalloc_array(c
->lsave_cnt
, sizeof(int), GFP_KERNEL
);
634 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_KERNEL
);
635 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_KERNEL
);
636 buf
= vmalloc(c
->leb_size
);
637 ltab
= vmalloc(array_size(sizeof(struct ubifs_lpt_lprops
),
639 if (!pnode
|| !nnode
|| !buf
|| !ltab
|| !lsave
) {
644 ubifs_assert(c
, !c
->ltab
);
645 c
->ltab
= ltab
; /* Needed by set_ltab */
647 /* Initialize LPT's own lprops */
648 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
649 ltab
[i
].free
= c
->leb_size
;
657 /* Number of leaf nodes (pnodes) */
661 * The first pnode contains the LEB properties for the LEBs that contain
662 * the root inode node and the root index node of the index tree.
664 node_sz
= ALIGN(ubifs_idx_node_sz(c
, 1), 8);
665 iopos
= ALIGN(node_sz
, c
->min_io_size
);
666 pnode
->lprops
[0].free
= c
->leb_size
- iopos
;
667 pnode
->lprops
[0].dirty
= iopos
- node_sz
;
668 pnode
->lprops
[0].flags
= LPROPS_INDEX
;
670 node_sz
= UBIFS_INO_NODE_SZ
;
671 iopos
= ALIGN(node_sz
, c
->min_io_size
);
672 pnode
->lprops
[1].free
= c
->leb_size
- iopos
;
673 pnode
->lprops
[1].dirty
= iopos
- node_sz
;
675 for (i
= 2; i
< UBIFS_LPT_FANOUT
; i
++)
676 pnode
->lprops
[i
].free
= c
->leb_size
;
678 /* Add first pnode */
679 ubifs_pack_pnode(c
, p
, pnode
);
684 /* Reset pnode values for remaining pnodes */
685 pnode
->lprops
[0].free
= c
->leb_size
;
686 pnode
->lprops
[0].dirty
= 0;
687 pnode
->lprops
[0].flags
= 0;
689 pnode
->lprops
[1].free
= c
->leb_size
;
690 pnode
->lprops
[1].dirty
= 0;
693 * To calculate the internal node branches, we keep information about
696 blnum
= lnum
; /* LEB number of level below */
697 boffs
= 0; /* Offset of level below */
698 bcnt
= cnt
; /* Number of nodes in level below */
699 bsz
= c
->pnode_sz
; /* Size of nodes in level below */
701 /* Add all remaining pnodes */
702 for (i
= 1; i
< cnt
; i
++) {
703 if (len
+ c
->pnode_sz
> c
->leb_size
) {
704 alen
= ALIGN(len
, c
->min_io_size
);
705 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
706 memset(p
, 0xff, alen
- len
);
707 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
713 ubifs_pack_pnode(c
, p
, pnode
);
717 * pnodes are simply numbered left to right starting at zero,
718 * which means the pnode number can be used easily to traverse
719 * down the tree to the corresponding pnode.
725 for (i
= UBIFS_LPT_FANOUT
; cnt
> i
; i
<<= UBIFS_LPT_FANOUT_SHIFT
)
727 /* Add all nnodes, one level at a time */
729 /* Number of internal nodes (nnodes) at next level */
730 cnt
= DIV_ROUND_UP(cnt
, UBIFS_LPT_FANOUT
);
731 for (i
= 0; i
< cnt
; i
++) {
732 if (len
+ c
->nnode_sz
> c
->leb_size
) {
733 alen
= ALIGN(len
, c
->min_io_size
);
734 set_ltab(c
, lnum
, c
->leb_size
- alen
,
736 memset(p
, 0xff, alen
- len
);
737 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
743 /* Only 1 nnode at this level, so it is the root */
748 /* Set branches to the level below */
749 for (j
= 0; j
< UBIFS_LPT_FANOUT
; j
++) {
751 if (boffs
+ bsz
> c
->leb_size
) {
755 nnode
->nbranch
[j
].lnum
= blnum
;
756 nnode
->nbranch
[j
].offs
= boffs
;
760 nnode
->nbranch
[j
].lnum
= 0;
761 nnode
->nbranch
[j
].offs
= 0;
764 nnode
->num
= calc_nnode_num(row
, i
);
765 ubifs_pack_nnode(c
, p
, nnode
);
769 /* Only 1 nnode at this level, so it is the root */
772 /* Update the information about the level below */
779 /* Need to add LPT's save table */
780 if (len
+ c
->lsave_sz
> c
->leb_size
) {
781 alen
= ALIGN(len
, c
->min_io_size
);
782 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
783 memset(p
, 0xff, alen
- len
);
784 err
= ubifs_leb_change(c
, 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
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
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
= ubifs_leb_change(c
, lnum
, buf
, alen
);
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(const struct ubifs_info
*c
, void *buf
, int len
)
921 uint16_t crc
, calc_crc
;
923 crc
= ubifs_unpack_bits(c
, &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(c
, "invalid crc in LPT node: crc %hx calc %hx",
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(const struct ubifs_info
*c
, uint8_t **addr
,
949 node_type
= ubifs_unpack_bits(c
, addr
, pos
, UBIFS_LPT_TYPE_BITS
);
950 if (node_type
!= type
) {
951 ubifs_err(c
, "invalid type (%d) in LPT node type %d",
960 * unpack_pnode - unpack a pnode.
961 * @c: UBIFS file-system description object
962 * @buf: buffer containing packed pnode to unpack
963 * @pnode: pnode structure to fill
965 * This function returns %0 on success and a negative error code on failure.
967 static int unpack_pnode(const struct ubifs_info
*c
, void *buf
,
968 struct ubifs_pnode
*pnode
)
970 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
973 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_PNODE
);
977 pnode
->num
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->pcnt_bits
);
978 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
979 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
981 lprops
->free
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->space_bits
);
983 lprops
->dirty
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->space_bits
);
986 if (ubifs_unpack_bits(c
, &addr
, &pos
, 1))
987 lprops
->flags
= LPROPS_INDEX
;
990 lprops
->flags
|= ubifs_categorize_lprops(c
, lprops
);
992 err
= check_lpt_crc(c
, buf
, c
->pnode_sz
);
997 * ubifs_unpack_nnode - unpack a nnode.
998 * @c: UBIFS file-system description object
999 * @buf: buffer containing packed nnode to unpack
1000 * @nnode: nnode structure to fill
1002 * This function returns %0 on success and a negative error code on failure.
1004 int ubifs_unpack_nnode(const struct ubifs_info
*c
, void *buf
,
1005 struct ubifs_nnode
*nnode
)
1007 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1008 int i
, pos
= 0, err
;
1010 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_NNODE
);
1014 nnode
->num
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->pcnt_bits
);
1015 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1018 lnum
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_lnum_bits
) +
1020 if (lnum
== c
->lpt_last
+ 1)
1022 nnode
->nbranch
[i
].lnum
= lnum
;
1023 nnode
->nbranch
[i
].offs
= ubifs_unpack_bits(c
, &addr
, &pos
,
1026 err
= check_lpt_crc(c
, buf
, c
->nnode_sz
);
1031 * unpack_ltab - unpack the LPT's own lprops table.
1032 * @c: UBIFS file-system description object
1033 * @buf: buffer from which to unpack
1035 * This function returns %0 on success and a negative error code on failure.
1037 static int unpack_ltab(const struct ubifs_info
*c
, void *buf
)
1039 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1040 int i
, pos
= 0, err
;
1042 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_LTAB
);
1045 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1046 int free
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_spc_bits
);
1047 int dirty
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_spc_bits
);
1049 if (free
< 0 || free
> c
->leb_size
|| dirty
< 0 ||
1050 dirty
> c
->leb_size
|| free
+ dirty
> c
->leb_size
)
1053 c
->ltab
[i
].free
= free
;
1054 c
->ltab
[i
].dirty
= dirty
;
1058 err
= check_lpt_crc(c
, buf
, c
->ltab_sz
);
1063 * unpack_lsave - unpack the LPT's save table.
1064 * @c: UBIFS file-system description object
1065 * @buf: buffer from which to unpack
1067 * This function returns %0 on success and a negative error code on failure.
1069 static int unpack_lsave(const struct ubifs_info
*c
, void *buf
)
1071 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1072 int i
, pos
= 0, err
;
1074 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_LSAVE
);
1077 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1078 int lnum
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lnum_bits
);
1080 if (lnum
< c
->main_first
|| lnum
>= c
->leb_cnt
)
1084 err
= check_lpt_crc(c
, buf
, c
->lsave_sz
);
1089 * validate_nnode - validate a nnode.
1090 * @c: UBIFS file-system description object
1091 * @nnode: nnode to validate
1092 * @parent: parent nnode (or NULL for the root nnode)
1093 * @iip: index in parent
1095 * This function returns %0 on success and a negative error code on failure.
1097 static int validate_nnode(const struct ubifs_info
*c
, struct ubifs_nnode
*nnode
,
1098 struct ubifs_nnode
*parent
, int iip
)
1100 int i
, lvl
, max_offs
;
1103 int num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1105 if (nnode
->num
!= num
)
1108 lvl
= parent
? parent
->level
- 1 : c
->lpt_hght
;
1112 max_offs
= c
->leb_size
- c
->pnode_sz
;
1114 max_offs
= c
->leb_size
- c
->nnode_sz
;
1115 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1116 int lnum
= nnode
->nbranch
[i
].lnum
;
1117 int offs
= nnode
->nbranch
[i
].offs
;
1124 if (lnum
< c
->lpt_first
|| lnum
> c
->lpt_last
)
1126 if (offs
< 0 || offs
> max_offs
)
1133 * validate_pnode - validate a pnode.
1134 * @c: UBIFS file-system description object
1135 * @pnode: pnode to validate
1136 * @parent: parent nnode
1137 * @iip: index in parent
1139 * This function returns %0 on success and a negative error code on failure.
1141 static int validate_pnode(const struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
1142 struct ubifs_nnode
*parent
, int iip
)
1147 int num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1149 if (pnode
->num
!= num
)
1152 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1153 int free
= pnode
->lprops
[i
].free
;
1154 int dirty
= pnode
->lprops
[i
].dirty
;
1156 if (free
< 0 || free
> c
->leb_size
|| free
% c
->min_io_size
||
1159 if (dirty
< 0 || dirty
> c
->leb_size
|| (dirty
& 7))
1161 if (dirty
+ free
> c
->leb_size
)
1168 * set_pnode_lnum - set LEB numbers on a pnode.
1169 * @c: UBIFS file-system description object
1170 * @pnode: pnode to update
1172 * This function calculates the LEB numbers for the LEB properties it contains
1173 * based on the pnode number.
1175 static void set_pnode_lnum(const struct ubifs_info
*c
,
1176 struct ubifs_pnode
*pnode
)
1180 lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + c
->main_first
;
1181 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1182 if (lnum
>= c
->leb_cnt
)
1184 pnode
->lprops
[i
].lnum
= lnum
++;
1189 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1190 * @c: UBIFS file-system description object
1191 * @parent: parent nnode (or NULL for the root)
1192 * @iip: index in parent
1194 * This function returns %0 on success and a negative error code on failure.
1196 int ubifs_read_nnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1198 struct ubifs_nbranch
*branch
= NULL
;
1199 struct ubifs_nnode
*nnode
= NULL
;
1200 void *buf
= c
->lpt_nod_buf
;
1201 int err
, lnum
, offs
;
1204 branch
= &parent
->nbranch
[iip
];
1205 lnum
= branch
->lnum
;
1206 offs
= branch
->offs
;
1211 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_NOFS
);
1218 * This nnode was not written which just means that the LEB
1219 * properties in the subtree below it describe empty LEBs. We
1220 * make the nnode as though we had read it, which in fact means
1221 * doing almost nothing.
1224 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1226 err
= ubifs_leb_read(c
, lnum
, buf
, offs
, c
->nnode_sz
, 1);
1229 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1233 err
= validate_nnode(c
, nnode
, parent
, iip
);
1237 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1239 branch
->nnode
= nnode
;
1240 nnode
->level
= parent
->level
- 1;
1243 nnode
->level
= c
->lpt_hght
;
1245 nnode
->parent
= parent
;
1250 ubifs_err(c
, "error %d reading nnode at %d:%d", err
, lnum
, offs
);
1257 * read_pnode - read a pnode from flash and link it to the tree in memory.
1258 * @c: UBIFS file-system description object
1259 * @parent: parent nnode
1260 * @iip: index in parent
1262 * This function returns %0 on success and a negative error code on failure.
1264 static int read_pnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1266 struct ubifs_nbranch
*branch
;
1267 struct ubifs_pnode
*pnode
= NULL
;
1268 void *buf
= c
->lpt_nod_buf
;
1269 int err
, lnum
, offs
;
1271 branch
= &parent
->nbranch
[iip
];
1272 lnum
= branch
->lnum
;
1273 offs
= branch
->offs
;
1274 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_NOFS
);
1280 * This pnode was not written which just means that the LEB
1281 * properties in it describe empty LEBs. We make the pnode as
1282 * though we had read it.
1287 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1288 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1289 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
1291 lprops
->free
= c
->leb_size
;
1292 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
1295 err
= ubifs_leb_read(c
, lnum
, buf
, offs
, c
->pnode_sz
, 1);
1298 err
= unpack_pnode(c
, buf
, pnode
);
1302 err
= validate_pnode(c
, pnode
, parent
, iip
);
1306 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1307 branch
->pnode
= pnode
;
1308 pnode
->parent
= parent
;
1310 set_pnode_lnum(c
, pnode
);
1311 c
->pnodes_have
+= 1;
1315 ubifs_err(c
, "error %d reading pnode at %d:%d", err
, lnum
, offs
);
1316 ubifs_dump_pnode(c
, pnode
, parent
, iip
);
1318 ubifs_err(c
, "calc num: %d", calc_pnode_num_from_parent(c
, parent
, iip
));
1324 * read_ltab - read LPT's own lprops table.
1325 * @c: UBIFS file-system description object
1327 * This function returns %0 on success and a negative error code on failure.
1329 static int read_ltab(struct ubifs_info
*c
)
1334 buf
= vmalloc(c
->ltab_sz
);
1337 err
= ubifs_leb_read(c
, c
->ltab_lnum
, buf
, c
->ltab_offs
, c
->ltab_sz
, 1);
1340 err
= unpack_ltab(c
, buf
);
1347 * read_lsave - read LPT's save table.
1348 * @c: UBIFS file-system description object
1350 * This function returns %0 on success and a negative error code on failure.
1352 static int read_lsave(struct ubifs_info
*c
)
1357 buf
= vmalloc(c
->lsave_sz
);
1360 err
= ubifs_leb_read(c
, c
->lsave_lnum
, buf
, c
->lsave_offs
,
1364 err
= unpack_lsave(c
, buf
);
1367 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1368 int lnum
= c
->lsave
[i
];
1369 struct ubifs_lprops
*lprops
;
1372 * Due to automatic resizing, the values in the lsave table
1373 * could be beyond the volume size - just ignore them.
1375 if (lnum
>= c
->leb_cnt
)
1377 lprops
= ubifs_lpt_lookup(c
, lnum
);
1378 if (IS_ERR(lprops
)) {
1379 err
= PTR_ERR(lprops
);
1389 * ubifs_get_nnode - get a nnode.
1390 * @c: UBIFS file-system description object
1391 * @parent: parent nnode (or NULL for the root)
1392 * @iip: index in parent
1394 * This function returns a pointer to the nnode on success or a negative error
1397 struct ubifs_nnode
*ubifs_get_nnode(struct ubifs_info
*c
,
1398 struct ubifs_nnode
*parent
, int iip
)
1400 struct ubifs_nbranch
*branch
;
1401 struct ubifs_nnode
*nnode
;
1404 branch
= &parent
->nbranch
[iip
];
1405 nnode
= branch
->nnode
;
1408 err
= ubifs_read_nnode(c
, parent
, iip
);
1410 return ERR_PTR(err
);
1411 return branch
->nnode
;
1415 * ubifs_get_pnode - get a pnode.
1416 * @c: UBIFS file-system description object
1417 * @parent: parent nnode
1418 * @iip: index in parent
1420 * This function returns a pointer to the pnode on success or a negative error
1423 struct ubifs_pnode
*ubifs_get_pnode(struct ubifs_info
*c
,
1424 struct ubifs_nnode
*parent
, int iip
)
1426 struct ubifs_nbranch
*branch
;
1427 struct ubifs_pnode
*pnode
;
1430 branch
= &parent
->nbranch
[iip
];
1431 pnode
= branch
->pnode
;
1434 err
= read_pnode(c
, parent
, iip
);
1436 return ERR_PTR(err
);
1437 update_cats(c
, branch
->pnode
);
1438 return branch
->pnode
;
1442 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1443 * @c: UBIFS file-system description object
1444 * @lnum: LEB number to lookup
1446 * This function returns a pointer to the LEB properties on success or a
1447 * negative error code on failure.
1449 struct ubifs_lprops
*ubifs_lpt_lookup(struct ubifs_info
*c
, int lnum
)
1451 int err
, i
, h
, iip
, shft
;
1452 struct ubifs_nnode
*nnode
;
1453 struct ubifs_pnode
*pnode
;
1456 err
= ubifs_read_nnode(c
, NULL
, 0);
1458 return ERR_PTR(err
);
1461 i
= lnum
- c
->main_first
;
1462 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1463 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1464 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1465 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1466 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1468 return ERR_CAST(nnode
);
1470 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1471 pnode
= ubifs_get_pnode(c
, nnode
, iip
);
1473 return ERR_CAST(pnode
);
1474 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1475 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum
,
1476 pnode
->lprops
[iip
].free
, pnode
->lprops
[iip
].dirty
,
1477 pnode
->lprops
[iip
].flags
);
1478 return &pnode
->lprops
[iip
];
1482 * dirty_cow_nnode - ensure a nnode is not being committed.
1483 * @c: UBIFS file-system description object
1484 * @nnode: nnode to check
1486 * Returns dirtied nnode on success or negative error code on failure.
1488 static struct ubifs_nnode
*dirty_cow_nnode(struct ubifs_info
*c
,
1489 struct ubifs_nnode
*nnode
)
1491 struct ubifs_nnode
*n
;
1494 if (!test_bit(COW_CNODE
, &nnode
->flags
)) {
1495 /* nnode is not being committed */
1496 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
1497 c
->dirty_nn_cnt
+= 1;
1498 ubifs_add_nnode_dirt(c
, nnode
);
1503 /* nnode is being committed, so copy it */
1504 n
= kmemdup(nnode
, sizeof(struct ubifs_nnode
), GFP_NOFS
);
1506 return ERR_PTR(-ENOMEM
);
1509 __set_bit(DIRTY_CNODE
, &n
->flags
);
1510 __clear_bit(COW_CNODE
, &n
->flags
);
1512 /* The children now have new parent */
1513 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1514 struct ubifs_nbranch
*branch
= &n
->nbranch
[i
];
1517 branch
->cnode
->parent
= n
;
1520 ubifs_assert(c
, !test_bit(OBSOLETE_CNODE
, &nnode
->flags
));
1521 __set_bit(OBSOLETE_CNODE
, &nnode
->flags
);
1523 c
->dirty_nn_cnt
+= 1;
1524 ubifs_add_nnode_dirt(c
, nnode
);
1526 nnode
->parent
->nbranch
[n
->iip
].nnode
= n
;
1533 * dirty_cow_pnode - ensure a pnode is not being committed.
1534 * @c: UBIFS file-system description object
1535 * @pnode: pnode to check
1537 * Returns dirtied pnode on success or negative error code on failure.
1539 static struct ubifs_pnode
*dirty_cow_pnode(struct ubifs_info
*c
,
1540 struct ubifs_pnode
*pnode
)
1542 struct ubifs_pnode
*p
;
1544 if (!test_bit(COW_CNODE
, &pnode
->flags
)) {
1545 /* pnode is not being committed */
1546 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
1547 c
->dirty_pn_cnt
+= 1;
1548 add_pnode_dirt(c
, pnode
);
1553 /* pnode is being committed, so copy it */
1554 p
= kmemdup(pnode
, sizeof(struct ubifs_pnode
), GFP_NOFS
);
1556 return ERR_PTR(-ENOMEM
);
1559 __set_bit(DIRTY_CNODE
, &p
->flags
);
1560 __clear_bit(COW_CNODE
, &p
->flags
);
1561 replace_cats(c
, pnode
, p
);
1563 ubifs_assert(c
, !test_bit(OBSOLETE_CNODE
, &pnode
->flags
));
1564 __set_bit(OBSOLETE_CNODE
, &pnode
->flags
);
1566 c
->dirty_pn_cnt
+= 1;
1567 add_pnode_dirt(c
, pnode
);
1568 pnode
->parent
->nbranch
[p
->iip
].pnode
= p
;
1573 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1574 * @c: UBIFS file-system description object
1575 * @lnum: LEB number to lookup
1577 * This function returns a pointer to the LEB properties on success or a
1578 * negative error code on failure.
1580 struct ubifs_lprops
*ubifs_lpt_lookup_dirty(struct ubifs_info
*c
, int lnum
)
1582 int err
, i
, h
, iip
, shft
;
1583 struct ubifs_nnode
*nnode
;
1584 struct ubifs_pnode
*pnode
;
1587 err
= ubifs_read_nnode(c
, NULL
, 0);
1589 return ERR_PTR(err
);
1592 nnode
= dirty_cow_nnode(c
, nnode
);
1594 return ERR_CAST(nnode
);
1595 i
= lnum
- c
->main_first
;
1596 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1597 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1598 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1599 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1600 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1602 return ERR_CAST(nnode
);
1603 nnode
= dirty_cow_nnode(c
, nnode
);
1605 return ERR_CAST(nnode
);
1607 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
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(c
, 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(array_size(sizeof(struct ubifs_lpt_lprops
),
1637 i
= max_t(int, c
->nnode_sz
, c
->pnode_sz
);
1638 c
->lpt_nod_buf
= kmalloc(i
, GFP_KERNEL
);
1639 if (!c
->lpt_nod_buf
)
1642 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++) {
1643 c
->lpt_heap
[i
].arr
= kmalloc_array(LPT_HEAP_SZ
,
1646 if (!c
->lpt_heap
[i
].arr
)
1648 c
->lpt_heap
[i
].cnt
= 0;
1649 c
->lpt_heap
[i
].max_cnt
= LPT_HEAP_SZ
;
1652 c
->dirty_idx
.arr
= kmalloc_array(LPT_HEAP_SZ
, sizeof(void *),
1654 if (!c
->dirty_idx
.arr
)
1656 c
->dirty_idx
.cnt
= 0;
1657 c
->dirty_idx
.max_cnt
= LPT_HEAP_SZ
;
1663 dbg_lp("space_bits %d", c
->space_bits
);
1664 dbg_lp("lpt_lnum_bits %d", c
->lpt_lnum_bits
);
1665 dbg_lp("lpt_offs_bits %d", c
->lpt_offs_bits
);
1666 dbg_lp("lpt_spc_bits %d", c
->lpt_spc_bits
);
1667 dbg_lp("pcnt_bits %d", c
->pcnt_bits
);
1668 dbg_lp("lnum_bits %d", c
->lnum_bits
);
1669 dbg_lp("pnode_sz %d", c
->pnode_sz
);
1670 dbg_lp("nnode_sz %d", c
->nnode_sz
);
1671 dbg_lp("ltab_sz %d", c
->ltab_sz
);
1672 dbg_lp("lsave_sz %d", c
->lsave_sz
);
1673 dbg_lp("lsave_cnt %d", c
->lsave_cnt
);
1674 dbg_lp("lpt_hght %d", c
->lpt_hght
);
1675 dbg_lp("big_lpt %d", c
->big_lpt
);
1676 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
1677 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
1678 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
1680 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
1686 * lpt_init_wr - initialize the LPT for writing.
1687 * @c: UBIFS file-system description object
1689 * 'lpt_init_rd()' must have been called already.
1691 * This function returns %0 on success and a negative error code on failure.
1693 static int lpt_init_wr(struct ubifs_info
*c
)
1697 c
->ltab_cmt
= vmalloc(array_size(sizeof(struct ubifs_lpt_lprops
),
1702 c
->lpt_buf
= vmalloc(c
->leb_size
);
1707 c
->lsave
= kmalloc_array(c
->lsave_cnt
, sizeof(int), GFP_NOFS
);
1710 err
= read_lsave(c
);
1715 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1716 if (c
->ltab
[i
].free
== c
->leb_size
) {
1717 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
1726 * ubifs_lpt_init - initialize the LPT.
1727 * @c: UBIFS file-system description object
1728 * @rd: whether to initialize lpt for reading
1729 * @wr: whether to initialize lpt for writing
1731 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1732 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1735 * This function returns %0 on success and a negative error code on failure.
1737 int ubifs_lpt_init(struct ubifs_info
*c
, int rd
, int wr
)
1742 err
= lpt_init_rd(c
);
1748 err
= lpt_init_wr(c
);
1757 ubifs_lpt_free(c
, 1);
1759 ubifs_lpt_free(c
, 0);
1764 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1765 * @nnode: where to keep a nnode
1766 * @pnode: where to keep a pnode
1767 * @cnode: where to keep a cnode
1768 * @in_tree: is the node in the tree in memory
1769 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1771 * @ptr.pnode: ditto for pnode
1772 * @ptr.cnode: ditto for cnode
1774 struct lpt_scan_node
{
1776 struct ubifs_nnode nnode
;
1777 struct ubifs_pnode pnode
;
1778 struct ubifs_cnode cnode
;
1782 struct ubifs_nnode
*nnode
;
1783 struct ubifs_pnode
*pnode
;
1784 struct ubifs_cnode
*cnode
;
1789 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1790 * @c: the UBIFS file-system description object
1791 * @path: where to put the nnode
1792 * @parent: parent of the nnode
1793 * @iip: index in parent of the nnode
1795 * This function returns a pointer to the nnode on success or a negative error
1798 static struct ubifs_nnode
*scan_get_nnode(struct ubifs_info
*c
,
1799 struct lpt_scan_node
*path
,
1800 struct ubifs_nnode
*parent
, int iip
)
1802 struct ubifs_nbranch
*branch
;
1803 struct ubifs_nnode
*nnode
;
1804 void *buf
= c
->lpt_nod_buf
;
1807 branch
= &parent
->nbranch
[iip
];
1808 nnode
= branch
->nnode
;
1811 path
->ptr
.nnode
= nnode
;
1814 nnode
= &path
->nnode
;
1816 path
->ptr
.nnode
= nnode
;
1817 memset(nnode
, 0, sizeof(struct ubifs_nnode
));
1818 if (branch
->lnum
== 0) {
1820 * This nnode was not written which just means that the LEB
1821 * properties in the subtree below it describe empty LEBs. We
1822 * make the nnode as though we had read it, which in fact means
1823 * doing almost nothing.
1826 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1828 err
= ubifs_leb_read(c
, branch
->lnum
, buf
, branch
->offs
,
1831 return ERR_PTR(err
);
1832 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1834 return ERR_PTR(err
);
1836 err
= validate_nnode(c
, nnode
, parent
, iip
);
1838 return ERR_PTR(err
);
1840 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1841 nnode
->level
= parent
->level
- 1;
1842 nnode
->parent
= parent
;
1848 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1849 * @c: the UBIFS file-system description object
1850 * @path: where to put the pnode
1851 * @parent: parent of the pnode
1852 * @iip: index in parent of the pnode
1854 * This function returns a pointer to the pnode on success or a negative error
1857 static struct ubifs_pnode
*scan_get_pnode(struct ubifs_info
*c
,
1858 struct lpt_scan_node
*path
,
1859 struct ubifs_nnode
*parent
, int iip
)
1861 struct ubifs_nbranch
*branch
;
1862 struct ubifs_pnode
*pnode
;
1863 void *buf
= c
->lpt_nod_buf
;
1866 branch
= &parent
->nbranch
[iip
];
1867 pnode
= branch
->pnode
;
1870 path
->ptr
.pnode
= pnode
;
1873 pnode
= &path
->pnode
;
1875 path
->ptr
.pnode
= pnode
;
1876 memset(pnode
, 0, sizeof(struct ubifs_pnode
));
1877 if (branch
->lnum
== 0) {
1879 * This pnode was not written which just means that the LEB
1880 * properties in it describe empty LEBs. We make the pnode as
1881 * though we had read it.
1886 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1887 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1888 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
1890 lprops
->free
= c
->leb_size
;
1891 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
1894 ubifs_assert(c
, branch
->lnum
>= c
->lpt_first
&&
1895 branch
->lnum
<= c
->lpt_last
);
1896 ubifs_assert(c
, branch
->offs
>= 0 && branch
->offs
< c
->leb_size
);
1897 err
= ubifs_leb_read(c
, branch
->lnum
, buf
, branch
->offs
,
1900 return ERR_PTR(err
);
1901 err
= unpack_pnode(c
, buf
, pnode
);
1903 return ERR_PTR(err
);
1905 err
= validate_pnode(c
, pnode
, parent
, iip
);
1907 return ERR_PTR(err
);
1909 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1910 pnode
->parent
= parent
;
1912 set_pnode_lnum(c
, pnode
);
1917 * ubifs_lpt_scan_nolock - scan the LPT.
1918 * @c: the UBIFS file-system description object
1919 * @start_lnum: LEB number from which to start scanning
1920 * @end_lnum: LEB number at which to stop scanning
1921 * @scan_cb: callback function called for each lprops
1922 * @data: data to be passed to the callback function
1924 * This function returns %0 on success and a negative error code on failure.
1926 int ubifs_lpt_scan_nolock(struct ubifs_info
*c
, int start_lnum
, int end_lnum
,
1927 ubifs_lpt_scan_callback scan_cb
, void *data
)
1929 int err
= 0, i
, h
, iip
, shft
;
1930 struct ubifs_nnode
*nnode
;
1931 struct ubifs_pnode
*pnode
;
1932 struct lpt_scan_node
*path
;
1934 if (start_lnum
== -1) {
1935 start_lnum
= end_lnum
+ 1;
1936 if (start_lnum
>= c
->leb_cnt
)
1937 start_lnum
= c
->main_first
;
1940 ubifs_assert(c
, start_lnum
>= c
->main_first
&& start_lnum
< c
->leb_cnt
);
1941 ubifs_assert(c
, end_lnum
>= c
->main_first
&& end_lnum
< c
->leb_cnt
);
1944 err
= ubifs_read_nnode(c
, NULL
, 0);
1949 path
= kmalloc_array(c
->lpt_hght
+ 1, sizeof(struct lpt_scan_node
),
1954 path
[0].ptr
.nnode
= c
->nroot
;
1955 path
[0].in_tree
= 1;
1957 /* Descend to the pnode containing start_lnum */
1959 i
= start_lnum
- c
->main_first
;
1960 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1961 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1962 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1963 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1964 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
1965 if (IS_ERR(nnode
)) {
1966 err
= PTR_ERR(nnode
);
1970 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1971 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
1972 if (IS_ERR(pnode
)) {
1973 err
= PTR_ERR(pnode
);
1976 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1978 /* Loop for each lprops */
1980 struct ubifs_lprops
*lprops
= &pnode
->lprops
[iip
];
1981 int ret
, lnum
= lprops
->lnum
;
1983 ret
= scan_cb(c
, lprops
, path
[h
].in_tree
, data
);
1988 if (ret
& LPT_SCAN_ADD
) {
1989 /* Add all the nodes in path to the tree in memory */
1990 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1991 const size_t sz
= sizeof(struct ubifs_nnode
);
1992 struct ubifs_nnode
*parent
;
1994 if (path
[h
].in_tree
)
1996 nnode
= kmemdup(&path
[h
].nnode
, sz
, GFP_NOFS
);
2001 parent
= nnode
->parent
;
2002 parent
->nbranch
[nnode
->iip
].nnode
= nnode
;
2003 path
[h
].ptr
.nnode
= nnode
;
2004 path
[h
].in_tree
= 1;
2005 path
[h
+ 1].cnode
.parent
= nnode
;
2007 if (path
[h
].in_tree
)
2008 ubifs_ensure_cat(c
, lprops
);
2010 const size_t sz
= sizeof(struct ubifs_pnode
);
2011 struct ubifs_nnode
*parent
;
2013 pnode
= kmemdup(&path
[h
].pnode
, sz
, GFP_NOFS
);
2018 parent
= pnode
->parent
;
2019 parent
->nbranch
[pnode
->iip
].pnode
= pnode
;
2020 path
[h
].ptr
.pnode
= pnode
;
2021 path
[h
].in_tree
= 1;
2022 update_cats(c
, pnode
);
2023 c
->pnodes_have
+= 1;
2025 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)
2029 err
= dbg_check_cats(c
);
2033 if (ret
& LPT_SCAN_STOP
) {
2037 /* Get the next lprops */
2038 if (lnum
== end_lnum
) {
2040 * We got to the end without finding what we were
2046 if (lnum
+ 1 >= c
->leb_cnt
) {
2047 /* Wrap-around to the beginning */
2048 start_lnum
= c
->main_first
;
2051 if (iip
+ 1 < UBIFS_LPT_FANOUT
) {
2052 /* Next lprops is in the same pnode */
2056 /* We need to get the next pnode. Go up until we can go right */
2060 ubifs_assert(c
, h
>= 0);
2061 nnode
= path
[h
].ptr
.nnode
;
2062 if (iip
+ 1 < UBIFS_LPT_FANOUT
)
2068 /* Descend to the pnode */
2070 for (; h
< c
->lpt_hght
; h
++) {
2071 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
2072 if (IS_ERR(nnode
)) {
2073 err
= PTR_ERR(nnode
);
2078 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
2079 if (IS_ERR(pnode
)) {
2080 err
= PTR_ERR(pnode
);
2091 * dbg_chk_pnode - check a pnode.
2092 * @c: the UBIFS file-system description object
2093 * @pnode: pnode to check
2094 * @col: pnode column
2096 * This function returns %0 on success and a negative error code on failure.
2098 static int dbg_chk_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
2103 if (pnode
->num
!= col
) {
2104 ubifs_err(c
, "pnode num %d expected %d parent num %d iip %d",
2105 pnode
->num
, col
, pnode
->parent
->num
, pnode
->iip
);
2108 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
2109 struct ubifs_lprops
*lp
, *lprops
= &pnode
->lprops
[i
];
2110 int lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + i
+
2112 int found
, cat
= lprops
->flags
& LPROPS_CAT_MASK
;
2113 struct ubifs_lpt_heap
*heap
;
2114 struct list_head
*list
= NULL
;
2116 if (lnum
>= c
->leb_cnt
)
2118 if (lprops
->lnum
!= lnum
) {
2119 ubifs_err(c
, "bad LEB number %d expected %d",
2120 lprops
->lnum
, lnum
);
2123 if (lprops
->flags
& LPROPS_TAKEN
) {
2124 if (cat
!= LPROPS_UNCAT
) {
2125 ubifs_err(c
, "LEB %d taken but not uncat %d",
2131 if (lprops
->flags
& LPROPS_INDEX
) {
2134 case LPROPS_DIRTY_IDX
:
2135 case LPROPS_FRDI_IDX
:
2138 ubifs_err(c
, "LEB %d index but cat %d",
2148 case LPROPS_FREEABLE
:
2151 ubifs_err(c
, "LEB %d not index but cat %d",
2158 list
= &c
->uncat_list
;
2161 list
= &c
->empty_list
;
2163 case LPROPS_FREEABLE
:
2164 list
= &c
->freeable_list
;
2166 case LPROPS_FRDI_IDX
:
2167 list
= &c
->frdi_idx_list
;
2173 case LPROPS_DIRTY_IDX
:
2175 heap
= &c
->lpt_heap
[cat
- 1];
2176 if (lprops
->hpos
< heap
->cnt
&&
2177 heap
->arr
[lprops
->hpos
] == lprops
)
2182 case LPROPS_FREEABLE
:
2183 case LPROPS_FRDI_IDX
:
2184 list_for_each_entry(lp
, list
, list
)
2192 ubifs_err(c
, "LEB %d cat %d not found in cat heap/list",
2198 if (lprops
->free
!= c
->leb_size
) {
2199 ubifs_err(c
, "LEB %d cat %d free %d dirty %d",
2200 lprops
->lnum
, cat
, lprops
->free
,
2205 case LPROPS_FREEABLE
:
2206 case LPROPS_FRDI_IDX
:
2207 if (lprops
->free
+ lprops
->dirty
!= c
->leb_size
) {
2208 ubifs_err(c
, "LEB %d cat %d free %d dirty %d",
2209 lprops
->lnum
, cat
, lprops
->free
,
2220 * dbg_check_lpt_nodes - check nnodes and pnodes.
2221 * @c: the UBIFS file-system description object
2222 * @cnode: next cnode (nnode or pnode) to check
2223 * @row: row of cnode (root is zero)
2224 * @col: column of cnode (leftmost is zero)
2226 * This function returns %0 on success and a negative error code on failure.
2228 int dbg_check_lpt_nodes(struct ubifs_info
*c
, struct ubifs_cnode
*cnode
,
2231 struct ubifs_nnode
*nnode
, *nn
;
2232 struct ubifs_cnode
*cn
;
2233 int num
, iip
= 0, err
;
2235 if (!dbg_is_chk_lprops(c
))
2239 ubifs_assert(c
, row
>= 0);
2240 nnode
= cnode
->parent
;
2242 /* cnode is a nnode */
2243 num
= calc_nnode_num(row
, col
);
2244 if (cnode
->num
!= num
) {
2245 ubifs_err(c
, "nnode num %d expected %d parent num %d iip %d",
2247 (nnode
? nnode
->num
: 0), cnode
->iip
);
2250 nn
= (struct ubifs_nnode
*)cnode
;
2251 while (iip
< UBIFS_LPT_FANOUT
) {
2252 cn
= nn
->nbranch
[iip
].cnode
;
2256 col
<<= UBIFS_LPT_FANOUT_SHIFT
;
2265 if (iip
< UBIFS_LPT_FANOUT
)
2268 struct ubifs_pnode
*pnode
;
2270 /* cnode is a pnode */
2271 pnode
= (struct ubifs_pnode
*)cnode
;
2272 err
= dbg_chk_pnode(c
, pnode
, col
);
2276 /* Go up and to the right */
2278 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
2279 iip
= cnode
->iip
+ 1;
2280 cnode
= (struct ubifs_cnode
*)nnode
;