1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
12 * This file implements the LEB properties tree (LPT) area. The LPT area
13 * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
14 * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
15 * between the log and the orphan area.
17 * The LPT area is like a miniature self-contained file system. It is required
18 * that it never runs out of space, is fast to access and update, and scales
19 * logarithmically. The LEB properties tree is implemented as a wandering tree
20 * much like the TNC, and the LPT area has its own garbage collection.
22 * The LPT has two slightly different forms called the "small model" and the
23 * "big model". The small model is used when the entire LEB properties table
24 * can be written into a single eraseblock. In that case, garbage collection
25 * consists of just writing the whole table, which therefore makes all other
26 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
27 * selected for garbage collection, which consists of marking the clean nodes in
28 * that LEB as dirty, and then only the dirty nodes are written out. Also, in
29 * the case of the big model, a table of LEB numbers is saved so that the entire
30 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
35 #include <linux/crc16.h>
36 #include <linux/math64.h>
37 #include <linux/slab.h>
40 * do_calc_lpt_geom - calculate sizes for the LPT area.
41 * @c: the UBIFS file-system description object
43 * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
44 * properties of the flash and whether LPT is "big" (c->big_lpt).
46 static void do_calc_lpt_geom(struct ubifs_info
*c
)
48 int i
, n
, bits
, per_leb_wastage
, max_pnode_cnt
;
49 long long sz
, tot_wastage
;
51 n
= c
->main_lebs
+ c
->max_leb_cnt
- c
->leb_cnt
;
52 max_pnode_cnt
= DIV_ROUND_UP(n
, UBIFS_LPT_FANOUT
);
56 while (n
< max_pnode_cnt
) {
58 n
<<= UBIFS_LPT_FANOUT_SHIFT
;
61 c
->pnode_cnt
= DIV_ROUND_UP(c
->main_lebs
, UBIFS_LPT_FANOUT
);
63 n
= DIV_ROUND_UP(c
->pnode_cnt
, UBIFS_LPT_FANOUT
);
65 for (i
= 1; i
< c
->lpt_hght
; i
++) {
66 n
= DIV_ROUND_UP(n
, UBIFS_LPT_FANOUT
);
70 c
->space_bits
= fls(c
->leb_size
) - 3;
71 c
->lpt_lnum_bits
= fls(c
->lpt_lebs
);
72 c
->lpt_offs_bits
= fls(c
->leb_size
- 1);
73 c
->lpt_spc_bits
= fls(c
->leb_size
);
75 n
= DIV_ROUND_UP(c
->max_leb_cnt
, UBIFS_LPT_FANOUT
);
76 c
->pcnt_bits
= fls(n
- 1);
78 c
->lnum_bits
= fls(c
->max_leb_cnt
- 1);
80 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
81 (c
->big_lpt
? c
->pcnt_bits
: 0) +
82 (c
->space_bits
* 2 + 1) * UBIFS_LPT_FANOUT
;
83 c
->pnode_sz
= (bits
+ 7) / 8;
85 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
86 (c
->big_lpt
? c
->pcnt_bits
: 0) +
87 (c
->lpt_lnum_bits
+ c
->lpt_offs_bits
) * UBIFS_LPT_FANOUT
;
88 c
->nnode_sz
= (bits
+ 7) / 8;
90 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
91 c
->lpt_lebs
* c
->lpt_spc_bits
* 2;
92 c
->ltab_sz
= (bits
+ 7) / 8;
94 bits
= UBIFS_LPT_CRC_BITS
+ UBIFS_LPT_TYPE_BITS
+
95 c
->lnum_bits
* c
->lsave_cnt
;
96 c
->lsave_sz
= (bits
+ 7) / 8;
98 /* Calculate the minimum LPT size */
99 c
->lpt_sz
= (long long)c
->pnode_cnt
* c
->pnode_sz
;
100 c
->lpt_sz
+= (long long)c
->nnode_cnt
* c
->nnode_sz
;
101 c
->lpt_sz
+= c
->ltab_sz
;
103 c
->lpt_sz
+= c
->lsave_sz
;
107 per_leb_wastage
= max_t(int, c
->pnode_sz
, c
->nnode_sz
);
108 sz
+= per_leb_wastage
;
109 tot_wastage
= per_leb_wastage
;
110 while (sz
> c
->leb_size
) {
111 sz
+= per_leb_wastage
;
113 tot_wastage
+= per_leb_wastage
;
115 tot_wastage
+= ALIGN(sz
, c
->min_io_size
) - sz
;
116 c
->lpt_sz
+= tot_wastage
;
120 * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
121 * @c: the UBIFS file-system description object
123 * This function returns %0 on success and a negative error code on failure.
125 int ubifs_calc_lpt_geom(struct ubifs_info
*c
)
132 /* Verify that lpt_lebs is big enough */
133 sz
= c
->lpt_sz
* 2; /* Must have at least 2 times the size */
134 lebs_needed
= div_u64(sz
+ c
->leb_size
- 1, c
->leb_size
);
135 if (lebs_needed
> c
->lpt_lebs
) {
136 ubifs_err(c
, "too few LPT LEBs");
140 /* Verify that ltab fits in a single LEB (since ltab is a single node */
141 if (c
->ltab_sz
> c
->leb_size
) {
142 ubifs_err(c
, "LPT ltab too big");
146 c
->check_lpt_free
= c
->big_lpt
;
151 * calc_dflt_lpt_geom - calculate default LPT geometry.
152 * @c: the UBIFS file-system description object
153 * @main_lebs: number of main area LEBs is passed and returned here
154 * @big_lpt: whether the LPT area is "big" is returned here
156 * The size of the LPT area depends on parameters that themselves are dependent
157 * on the size of the LPT area. This function, successively recalculates the LPT
158 * area geometry until the parameters and resultant geometry are consistent.
160 * This function returns %0 on success and a negative error code on failure.
162 static int calc_dflt_lpt_geom(struct ubifs_info
*c
, int *main_lebs
,
168 /* Start by assuming the minimum number of LPT LEBs */
169 c
->lpt_lebs
= UBIFS_MIN_LPT_LEBS
;
170 c
->main_lebs
= *main_lebs
- c
->lpt_lebs
;
171 if (c
->main_lebs
<= 0)
174 /* And assume we will use the small LPT model */
178 * Calculate the geometry based on assumptions above and then see if it
183 /* Small LPT model must have lpt_sz < leb_size */
184 if (c
->lpt_sz
> c
->leb_size
) {
185 /* Nope, so try again using big LPT model */
190 /* Now check there are enough LPT LEBs */
191 for (i
= 0; i
< 64 ; i
++) {
192 sz
= c
->lpt_sz
* 4; /* Allow 4 times the size */
193 lebs_needed
= div_u64(sz
+ c
->leb_size
- 1, c
->leb_size
);
194 if (lebs_needed
> c
->lpt_lebs
) {
195 /* Not enough LPT LEBs so try again with more */
196 c
->lpt_lebs
= lebs_needed
;
197 c
->main_lebs
= *main_lebs
- c
->lpt_lebs
;
198 if (c
->main_lebs
<= 0)
203 if (c
->ltab_sz
> c
->leb_size
) {
204 ubifs_err(c
, "LPT ltab too big");
207 *main_lebs
= c
->main_lebs
;
208 *big_lpt
= c
->big_lpt
;
215 * pack_bits - pack bit fields end-to-end.
216 * @c: UBIFS file-system description object
217 * @addr: address at which to pack (passed and next address returned)
218 * @pos: bit position at which to pack (passed and next position returned)
219 * @val: value to pack
220 * @nrbits: number of bits of value to pack (1-32)
222 static void pack_bits(const struct ubifs_info
*c
, uint8_t **addr
, int *pos
, uint32_t val
, int nrbits
)
227 ubifs_assert(c
, nrbits
> 0);
228 ubifs_assert(c
, nrbits
<= 32);
229 ubifs_assert(c
, *pos
>= 0);
230 ubifs_assert(c
, *pos
< 8);
231 ubifs_assert(c
, (val
>> nrbits
) == 0 || nrbits
== 32);
233 *p
|= ((uint8_t)val
) << b
;
236 *++p
= (uint8_t)(val
>>= (8 - b
));
238 *++p
= (uint8_t)(val
>>= 8);
240 *++p
= (uint8_t)(val
>>= 8);
242 *++p
= (uint8_t)(val
>>= 8);
249 *++p
= (uint8_t)(val
>>= 8);
251 *++p
= (uint8_t)(val
>>= 8);
253 *++p
= (uint8_t)(val
>>= 8);
265 * ubifs_unpack_bits - unpack bit fields.
266 * @c: UBIFS file-system description object
267 * @addr: address at which to unpack (passed and next address returned)
268 * @pos: bit position at which to unpack (passed and next position returned)
269 * @nrbits: number of bits of value to unpack (1-32)
271 * This functions returns the value unpacked.
273 uint32_t ubifs_unpack_bits(const struct ubifs_info
*c
, uint8_t **addr
, int *pos
, int nrbits
)
275 const int k
= 32 - nrbits
;
278 uint32_t uninitialized_var(val
);
279 const int bytes
= (nrbits
+ b
+ 7) >> 3;
281 ubifs_assert(c
, nrbits
> 0);
282 ubifs_assert(c
, nrbits
<= 32);
283 ubifs_assert(c
, *pos
>= 0);
284 ubifs_assert(c
, *pos
< 8);
291 val
= p
[1] | ((uint32_t)p
[2] << 8);
294 val
= p
[1] | ((uint32_t)p
[2] << 8) |
295 ((uint32_t)p
[3] << 16);
298 val
= p
[1] | ((uint32_t)p
[2] << 8) |
299 ((uint32_t)p
[3] << 16) |
300 ((uint32_t)p
[4] << 24);
311 val
= p
[0] | ((uint32_t)p
[1] << 8);
314 val
= p
[0] | ((uint32_t)p
[1] << 8) |
315 ((uint32_t)p
[2] << 16);
318 val
= p
[0] | ((uint32_t)p
[1] << 8) |
319 ((uint32_t)p
[2] << 16) |
320 ((uint32_t)p
[3] << 24);
330 ubifs_assert(c
, (val
>> nrbits
) == 0 || nrbits
- b
== 32);
335 * ubifs_pack_pnode - pack all the bit fields of a pnode.
336 * @c: UBIFS file-system description object
337 * @buf: buffer into which to pack
338 * @pnode: pnode to pack
340 void ubifs_pack_pnode(struct ubifs_info
*c
, void *buf
,
341 struct ubifs_pnode
*pnode
)
343 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
347 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_PNODE
, UBIFS_LPT_TYPE_BITS
);
349 pack_bits(c
, &addr
, &pos
, pnode
->num
, c
->pcnt_bits
);
350 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
351 pack_bits(c
, &addr
, &pos
, pnode
->lprops
[i
].free
>> 3,
353 pack_bits(c
, &addr
, &pos
, pnode
->lprops
[i
].dirty
>> 3,
355 if (pnode
->lprops
[i
].flags
& LPROPS_INDEX
)
356 pack_bits(c
, &addr
, &pos
, 1, 1);
358 pack_bits(c
, &addr
, &pos
, 0, 1);
360 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
361 c
->pnode_sz
- UBIFS_LPT_CRC_BYTES
);
364 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
368 * ubifs_pack_nnode - pack all the bit fields of a nnode.
369 * @c: UBIFS file-system description object
370 * @buf: buffer into which to pack
371 * @nnode: nnode to pack
373 void ubifs_pack_nnode(struct ubifs_info
*c
, void *buf
,
374 struct ubifs_nnode
*nnode
)
376 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
380 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_NNODE
, UBIFS_LPT_TYPE_BITS
);
382 pack_bits(c
, &addr
, &pos
, nnode
->num
, c
->pcnt_bits
);
383 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
384 int lnum
= nnode
->nbranch
[i
].lnum
;
387 lnum
= c
->lpt_last
+ 1;
388 pack_bits(c
, &addr
, &pos
, lnum
- c
->lpt_first
, c
->lpt_lnum_bits
);
389 pack_bits(c
, &addr
, &pos
, nnode
->nbranch
[i
].offs
,
392 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
393 c
->nnode_sz
- UBIFS_LPT_CRC_BYTES
);
396 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
400 * ubifs_pack_ltab - pack the LPT's own lprops table.
401 * @c: UBIFS file-system description object
402 * @buf: buffer into which to pack
403 * @ltab: LPT's own lprops table to pack
405 void ubifs_pack_ltab(struct ubifs_info
*c
, void *buf
,
406 struct ubifs_lpt_lprops
*ltab
)
408 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
412 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_LTAB
, UBIFS_LPT_TYPE_BITS
);
413 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
414 pack_bits(c
, &addr
, &pos
, ltab
[i
].free
, c
->lpt_spc_bits
);
415 pack_bits(c
, &addr
, &pos
, ltab
[i
].dirty
, c
->lpt_spc_bits
);
417 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
418 c
->ltab_sz
- UBIFS_LPT_CRC_BYTES
);
421 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
425 * ubifs_pack_lsave - pack the LPT's save table.
426 * @c: UBIFS file-system description object
427 * @buf: buffer into which to pack
428 * @lsave: LPT's save table to pack
430 void ubifs_pack_lsave(struct ubifs_info
*c
, void *buf
, int *lsave
)
432 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
436 pack_bits(c
, &addr
, &pos
, UBIFS_LPT_LSAVE
, UBIFS_LPT_TYPE_BITS
);
437 for (i
= 0; i
< c
->lsave_cnt
; i
++)
438 pack_bits(c
, &addr
, &pos
, lsave
[i
], c
->lnum_bits
);
439 crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
440 c
->lsave_sz
- UBIFS_LPT_CRC_BYTES
);
443 pack_bits(c
, &addr
, &pos
, crc
, UBIFS_LPT_CRC_BITS
);
447 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
448 * @c: UBIFS file-system description object
449 * @lnum: LEB number to which to add dirty space
450 * @dirty: amount of dirty space to add
452 void ubifs_add_lpt_dirt(struct ubifs_info
*c
, int lnum
, int dirty
)
456 dbg_lp("LEB %d add %d to %d",
457 lnum
, dirty
, c
->ltab
[lnum
- c
->lpt_first
].dirty
);
458 ubifs_assert(c
, lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
459 c
->ltab
[lnum
- c
->lpt_first
].dirty
+= dirty
;
463 * set_ltab - set LPT LEB properties.
464 * @c: UBIFS file-system description object
466 * @free: amount of free space
467 * @dirty: amount of dirty space
469 static void set_ltab(struct ubifs_info
*c
, int lnum
, int free
, int dirty
)
471 dbg_lp("LEB %d free %d dirty %d to %d %d",
472 lnum
, c
->ltab
[lnum
- c
->lpt_first
].free
,
473 c
->ltab
[lnum
- c
->lpt_first
].dirty
, free
, dirty
);
474 ubifs_assert(c
, lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
);
475 c
->ltab
[lnum
- c
->lpt_first
].free
= free
;
476 c
->ltab
[lnum
- c
->lpt_first
].dirty
= dirty
;
480 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
481 * @c: UBIFS file-system description object
482 * @nnode: nnode for which to add dirt
484 void ubifs_add_nnode_dirt(struct ubifs_info
*c
, struct ubifs_nnode
*nnode
)
486 struct ubifs_nnode
*np
= nnode
->parent
;
489 ubifs_add_lpt_dirt(c
, np
->nbranch
[nnode
->iip
].lnum
,
492 ubifs_add_lpt_dirt(c
, c
->lpt_lnum
, c
->nnode_sz
);
493 if (!(c
->lpt_drty_flgs
& LTAB_DIRTY
)) {
494 c
->lpt_drty_flgs
|= LTAB_DIRTY
;
495 ubifs_add_lpt_dirt(c
, c
->ltab_lnum
, c
->ltab_sz
);
501 * add_pnode_dirt - add dirty space to LPT LEB properties.
502 * @c: UBIFS file-system description object
503 * @pnode: pnode for which to add dirt
505 static void add_pnode_dirt(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
507 ubifs_add_lpt_dirt(c
, pnode
->parent
->nbranch
[pnode
->iip
].lnum
,
512 * calc_nnode_num - calculate nnode number.
513 * @row: the row in the tree (root is zero)
514 * @col: the column in the row (leftmost is zero)
516 * The nnode number is a number that uniquely identifies a nnode and can be used
517 * easily to traverse the tree from the root to that nnode.
519 * This function calculates and returns the nnode number for the nnode at @row
522 static int calc_nnode_num(int row
, int col
)
528 bits
= (col
& (UBIFS_LPT_FANOUT
- 1));
529 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
530 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
537 * calc_nnode_num_from_parent - calculate nnode number.
538 * @c: UBIFS file-system description object
539 * @parent: parent nnode
540 * @iip: index in parent
542 * The nnode number is a number that uniquely identifies a nnode and can be used
543 * easily to traverse the tree from the root to that nnode.
545 * This function calculates and returns the nnode number based on the parent's
546 * nnode number and the index in parent.
548 static int calc_nnode_num_from_parent(const struct ubifs_info
*c
,
549 struct ubifs_nnode
*parent
, int iip
)
555 shft
= (c
->lpt_hght
- parent
->level
) * UBIFS_LPT_FANOUT_SHIFT
;
556 num
= parent
->num
^ (1 << shft
);
557 num
|= (UBIFS_LPT_FANOUT
+ iip
) << shft
;
562 * calc_pnode_num_from_parent - calculate pnode number.
563 * @c: UBIFS file-system description object
564 * @parent: parent nnode
565 * @iip: index in parent
567 * The pnode number is a number that uniquely identifies a pnode and can be used
568 * easily to traverse the tree from the root to that pnode.
570 * This function calculates and returns the pnode number based on the parent's
571 * nnode number and the index in parent.
573 static int calc_pnode_num_from_parent(const struct ubifs_info
*c
,
574 struct ubifs_nnode
*parent
, int iip
)
576 int i
, n
= c
->lpt_hght
- 1, pnum
= parent
->num
, num
= 0;
578 for (i
= 0; i
< n
; i
++) {
579 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
580 num
|= pnum
& (UBIFS_LPT_FANOUT
- 1);
581 pnum
>>= UBIFS_LPT_FANOUT_SHIFT
;
583 num
<<= UBIFS_LPT_FANOUT_SHIFT
;
589 * ubifs_create_dflt_lpt - create default LPT.
590 * @c: UBIFS file-system description object
591 * @main_lebs: number of main area LEBs is passed and returned here
592 * @lpt_first: LEB number of first LPT LEB
593 * @lpt_lebs: number of LEBs for LPT is passed and returned here
594 * @big_lpt: use big LPT model is passed and returned here
595 * @hash: hash of the LPT is returned here
597 * This function returns %0 on success and a negative error code on failure.
599 int ubifs_create_dflt_lpt(struct ubifs_info
*c
, int *main_lebs
, int lpt_first
,
600 int *lpt_lebs
, int *big_lpt
, u8
*hash
)
602 int lnum
, err
= 0, node_sz
, iopos
, i
, j
, cnt
, len
, alen
, row
;
603 int blnum
, boffs
, bsz
, bcnt
;
604 struct ubifs_pnode
*pnode
= NULL
;
605 struct ubifs_nnode
*nnode
= NULL
;
606 void *buf
= NULL
, *p
;
607 struct ubifs_lpt_lprops
*ltab
= NULL
;
609 struct shash_desc
*desc
;
611 err
= calc_dflt_lpt_geom(c
, main_lebs
, big_lpt
);
614 *lpt_lebs
= c
->lpt_lebs
;
616 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
617 c
->lpt_first
= lpt_first
;
618 /* Needed by 'set_ltab()' */
619 c
->lpt_last
= lpt_first
+ c
->lpt_lebs
- 1;
620 /* Needed by 'ubifs_pack_lsave()' */
621 c
->main_first
= c
->leb_cnt
- *main_lebs
;
623 desc
= ubifs_hash_get_desc(c
);
625 return PTR_ERR(desc
);
627 lsave
= kmalloc_array(c
->lsave_cnt
, sizeof(int), GFP_KERNEL
);
628 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_KERNEL
);
629 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_KERNEL
);
630 buf
= vmalloc(c
->leb_size
);
631 ltab
= vmalloc(array_size(sizeof(struct ubifs_lpt_lprops
),
633 if (!pnode
|| !nnode
|| !buf
|| !ltab
|| !lsave
) {
638 ubifs_assert(c
, !c
->ltab
);
639 c
->ltab
= ltab
; /* Needed by set_ltab */
641 /* Initialize LPT's own lprops */
642 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
643 ltab
[i
].free
= c
->leb_size
;
651 /* Number of leaf nodes (pnodes) */
655 * The first pnode contains the LEB properties for the LEBs that contain
656 * the root inode node and the root index node of the index tree.
658 node_sz
= ALIGN(ubifs_idx_node_sz(c
, 1), 8);
659 iopos
= ALIGN(node_sz
, c
->min_io_size
);
660 pnode
->lprops
[0].free
= c
->leb_size
- iopos
;
661 pnode
->lprops
[0].dirty
= iopos
- node_sz
;
662 pnode
->lprops
[0].flags
= LPROPS_INDEX
;
664 node_sz
= UBIFS_INO_NODE_SZ
;
665 iopos
= ALIGN(node_sz
, c
->min_io_size
);
666 pnode
->lprops
[1].free
= c
->leb_size
- iopos
;
667 pnode
->lprops
[1].dirty
= iopos
- node_sz
;
669 for (i
= 2; i
< UBIFS_LPT_FANOUT
; i
++)
670 pnode
->lprops
[i
].free
= c
->leb_size
;
672 /* Add first pnode */
673 ubifs_pack_pnode(c
, p
, pnode
);
674 err
= ubifs_shash_update(c
, desc
, p
, c
->pnode_sz
);
682 /* Reset pnode values for remaining pnodes */
683 pnode
->lprops
[0].free
= c
->leb_size
;
684 pnode
->lprops
[0].dirty
= 0;
685 pnode
->lprops
[0].flags
= 0;
687 pnode
->lprops
[1].free
= c
->leb_size
;
688 pnode
->lprops
[1].dirty
= 0;
691 * To calculate the internal node branches, we keep information about
694 blnum
= lnum
; /* LEB number of level below */
695 boffs
= 0; /* Offset of level below */
696 bcnt
= cnt
; /* Number of nodes in level below */
697 bsz
= c
->pnode_sz
; /* Size of nodes in level below */
699 /* Add all remaining pnodes */
700 for (i
= 1; i
< cnt
; i
++) {
701 if (len
+ c
->pnode_sz
> c
->leb_size
) {
702 alen
= ALIGN(len
, c
->min_io_size
);
703 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
704 memset(p
, 0xff, alen
- len
);
705 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
711 ubifs_pack_pnode(c
, p
, pnode
);
712 err
= ubifs_shash_update(c
, desc
, p
, c
->pnode_sz
);
719 * pnodes are simply numbered left to right starting at zero,
720 * which means the pnode number can be used easily to traverse
721 * down the tree to the corresponding pnode.
727 for (i
= UBIFS_LPT_FANOUT
; cnt
> i
; i
<<= UBIFS_LPT_FANOUT_SHIFT
)
729 /* Add all nnodes, one level at a time */
731 /* Number of internal nodes (nnodes) at next level */
732 cnt
= DIV_ROUND_UP(cnt
, UBIFS_LPT_FANOUT
);
733 for (i
= 0; i
< cnt
; i
++) {
734 if (len
+ c
->nnode_sz
> c
->leb_size
) {
735 alen
= ALIGN(len
, c
->min_io_size
);
736 set_ltab(c
, lnum
, c
->leb_size
- alen
,
738 memset(p
, 0xff, alen
- len
);
739 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
745 /* Only 1 nnode at this level, so it is the root */
750 /* Set branches to the level below */
751 for (j
= 0; j
< UBIFS_LPT_FANOUT
; j
++) {
753 if (boffs
+ bsz
> c
->leb_size
) {
757 nnode
->nbranch
[j
].lnum
= blnum
;
758 nnode
->nbranch
[j
].offs
= boffs
;
762 nnode
->nbranch
[j
].lnum
= 0;
763 nnode
->nbranch
[j
].offs
= 0;
766 nnode
->num
= calc_nnode_num(row
, i
);
767 ubifs_pack_nnode(c
, p
, nnode
);
771 /* Only 1 nnode at this level, so it is the root */
774 /* Update the information about the level below */
781 /* Need to add LPT's save table */
782 if (len
+ c
->lsave_sz
> c
->leb_size
) {
783 alen
= ALIGN(len
, c
->min_io_size
);
784 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
785 memset(p
, 0xff, alen
- len
);
786 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
793 c
->lsave_lnum
= lnum
;
796 for (i
= 0; i
< c
->lsave_cnt
&& i
< *main_lebs
; i
++)
797 lsave
[i
] = c
->main_first
+ i
;
798 for (; i
< c
->lsave_cnt
; i
++)
799 lsave
[i
] = c
->main_first
;
801 ubifs_pack_lsave(c
, p
, lsave
);
806 /* Need to add LPT's own LEB properties table */
807 if (len
+ c
->ltab_sz
> c
->leb_size
) {
808 alen
= ALIGN(len
, c
->min_io_size
);
809 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
810 memset(p
, 0xff, alen
- len
);
811 err
= ubifs_leb_change(c
, lnum
++, buf
, alen
);
821 /* Update ltab before packing it */
823 alen
= ALIGN(len
, c
->min_io_size
);
824 set_ltab(c
, lnum
, c
->leb_size
- alen
, alen
- len
);
826 ubifs_pack_ltab(c
, p
, ltab
);
829 /* Write remaining buffer */
830 memset(p
, 0xff, alen
- len
);
831 err
= ubifs_leb_change(c
, lnum
, buf
, alen
);
835 err
= ubifs_shash_final(c
, desc
, hash
);
839 c
->nhead_lnum
= lnum
;
840 c
->nhead_offs
= ALIGN(len
, c
->min_io_size
);
842 dbg_lp("space_bits %d", c
->space_bits
);
843 dbg_lp("lpt_lnum_bits %d", c
->lpt_lnum_bits
);
844 dbg_lp("lpt_offs_bits %d", c
->lpt_offs_bits
);
845 dbg_lp("lpt_spc_bits %d", c
->lpt_spc_bits
);
846 dbg_lp("pcnt_bits %d", c
->pcnt_bits
);
847 dbg_lp("lnum_bits %d", c
->lnum_bits
);
848 dbg_lp("pnode_sz %d", c
->pnode_sz
);
849 dbg_lp("nnode_sz %d", c
->nnode_sz
);
850 dbg_lp("ltab_sz %d", c
->ltab_sz
);
851 dbg_lp("lsave_sz %d", c
->lsave_sz
);
852 dbg_lp("lsave_cnt %d", c
->lsave_cnt
);
853 dbg_lp("lpt_hght %d", c
->lpt_hght
);
854 dbg_lp("big_lpt %d", c
->big_lpt
);
855 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
856 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
857 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
859 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
872 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
873 * @c: UBIFS file-system description object
876 * When a pnode is loaded into memory, the LEB properties it contains are added,
877 * by this function, to the LEB category lists and heaps.
879 static void update_cats(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
)
883 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
884 int cat
= pnode
->lprops
[i
].flags
& LPROPS_CAT_MASK
;
885 int lnum
= pnode
->lprops
[i
].lnum
;
889 ubifs_add_to_cat(c
, &pnode
->lprops
[i
], cat
);
894 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
895 * @c: UBIFS file-system description object
896 * @old_pnode: pnode copied
897 * @new_pnode: pnode copy
899 * During commit it is sometimes necessary to copy a pnode
900 * (see dirty_cow_pnode). When that happens, references in
901 * category lists and heaps must be replaced. This function does that.
903 static void replace_cats(struct ubifs_info
*c
, struct ubifs_pnode
*old_pnode
,
904 struct ubifs_pnode
*new_pnode
)
908 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
909 if (!new_pnode
->lprops
[i
].lnum
)
911 ubifs_replace_cat(c
, &old_pnode
->lprops
[i
],
912 &new_pnode
->lprops
[i
]);
917 * check_lpt_crc - check LPT node crc is correct.
918 * @c: UBIFS file-system description object
919 * @buf: buffer containing node
920 * @len: length of node
922 * This function returns %0 on success and a negative error code on failure.
924 static int check_lpt_crc(const struct ubifs_info
*c
, void *buf
, int len
)
928 uint16_t crc
, calc_crc
;
930 crc
= ubifs_unpack_bits(c
, &addr
, &pos
, UBIFS_LPT_CRC_BITS
);
931 calc_crc
= crc16(-1, buf
+ UBIFS_LPT_CRC_BYTES
,
932 len
- UBIFS_LPT_CRC_BYTES
);
933 if (crc
!= calc_crc
) {
934 ubifs_err(c
, "invalid crc in LPT node: crc %hx calc %hx",
943 * check_lpt_type - check LPT node type is correct.
944 * @c: UBIFS file-system description object
945 * @addr: address of type bit field is passed and returned updated here
946 * @pos: position of type bit field is passed and returned updated here
947 * @type: expected type
949 * This function returns %0 on success and a negative error code on failure.
951 static int check_lpt_type(const struct ubifs_info
*c
, uint8_t **addr
,
956 node_type
= ubifs_unpack_bits(c
, addr
, pos
, UBIFS_LPT_TYPE_BITS
);
957 if (node_type
!= type
) {
958 ubifs_err(c
, "invalid type (%d) in LPT node type %d",
967 * unpack_pnode - unpack a pnode.
968 * @c: UBIFS file-system description object
969 * @buf: buffer containing packed pnode to unpack
970 * @pnode: pnode structure to fill
972 * This function returns %0 on success and a negative error code on failure.
974 static int unpack_pnode(const struct ubifs_info
*c
, void *buf
,
975 struct ubifs_pnode
*pnode
)
977 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
980 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_PNODE
);
984 pnode
->num
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->pcnt_bits
);
985 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
986 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
988 lprops
->free
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->space_bits
);
990 lprops
->dirty
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->space_bits
);
993 if (ubifs_unpack_bits(c
, &addr
, &pos
, 1))
994 lprops
->flags
= LPROPS_INDEX
;
997 lprops
->flags
|= ubifs_categorize_lprops(c
, lprops
);
999 err
= check_lpt_crc(c
, buf
, c
->pnode_sz
);
1004 * ubifs_unpack_nnode - unpack a nnode.
1005 * @c: UBIFS file-system description object
1006 * @buf: buffer containing packed nnode to unpack
1007 * @nnode: nnode structure to fill
1009 * This function returns %0 on success and a negative error code on failure.
1011 int ubifs_unpack_nnode(const struct ubifs_info
*c
, void *buf
,
1012 struct ubifs_nnode
*nnode
)
1014 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1015 int i
, pos
= 0, err
;
1017 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_NNODE
);
1021 nnode
->num
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->pcnt_bits
);
1022 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1025 lnum
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_lnum_bits
) +
1027 if (lnum
== c
->lpt_last
+ 1)
1029 nnode
->nbranch
[i
].lnum
= lnum
;
1030 nnode
->nbranch
[i
].offs
= ubifs_unpack_bits(c
, &addr
, &pos
,
1033 err
= check_lpt_crc(c
, buf
, c
->nnode_sz
);
1038 * unpack_ltab - unpack the LPT's own lprops table.
1039 * @c: UBIFS file-system description object
1040 * @buf: buffer from which to unpack
1042 * This function returns %0 on success and a negative error code on failure.
1044 static int unpack_ltab(const struct ubifs_info
*c
, void *buf
)
1046 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1047 int i
, pos
= 0, err
;
1049 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_LTAB
);
1052 for (i
= 0; i
< c
->lpt_lebs
; i
++) {
1053 int free
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_spc_bits
);
1054 int dirty
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lpt_spc_bits
);
1056 if (free
< 0 || free
> c
->leb_size
|| dirty
< 0 ||
1057 dirty
> c
->leb_size
|| free
+ dirty
> c
->leb_size
)
1060 c
->ltab
[i
].free
= free
;
1061 c
->ltab
[i
].dirty
= dirty
;
1065 err
= check_lpt_crc(c
, buf
, c
->ltab_sz
);
1070 * unpack_lsave - unpack the LPT's save table.
1071 * @c: UBIFS file-system description object
1072 * @buf: buffer from which to unpack
1074 * This function returns %0 on success and a negative error code on failure.
1076 static int unpack_lsave(const struct ubifs_info
*c
, void *buf
)
1078 uint8_t *addr
= buf
+ UBIFS_LPT_CRC_BYTES
;
1079 int i
, pos
= 0, err
;
1081 err
= check_lpt_type(c
, &addr
, &pos
, UBIFS_LPT_LSAVE
);
1084 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1085 int lnum
= ubifs_unpack_bits(c
, &addr
, &pos
, c
->lnum_bits
);
1087 if (lnum
< c
->main_first
|| lnum
>= c
->leb_cnt
)
1091 err
= check_lpt_crc(c
, buf
, c
->lsave_sz
);
1096 * validate_nnode - validate a nnode.
1097 * @c: UBIFS file-system description object
1098 * @nnode: nnode to validate
1099 * @parent: parent nnode (or NULL for the root nnode)
1100 * @iip: index in parent
1102 * This function returns %0 on success and a negative error code on failure.
1104 static int validate_nnode(const struct ubifs_info
*c
, struct ubifs_nnode
*nnode
,
1105 struct ubifs_nnode
*parent
, int iip
)
1107 int i
, lvl
, max_offs
;
1110 int num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1112 if (nnode
->num
!= num
)
1115 lvl
= parent
? parent
->level
- 1 : c
->lpt_hght
;
1119 max_offs
= c
->leb_size
- c
->pnode_sz
;
1121 max_offs
= c
->leb_size
- c
->nnode_sz
;
1122 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1123 int lnum
= nnode
->nbranch
[i
].lnum
;
1124 int offs
= nnode
->nbranch
[i
].offs
;
1131 if (lnum
< c
->lpt_first
|| lnum
> c
->lpt_last
)
1133 if (offs
< 0 || offs
> max_offs
)
1140 * validate_pnode - validate a pnode.
1141 * @c: UBIFS file-system description object
1142 * @pnode: pnode to validate
1143 * @parent: parent nnode
1144 * @iip: index in parent
1146 * This function returns %0 on success and a negative error code on failure.
1148 static int validate_pnode(const struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
1149 struct ubifs_nnode
*parent
, int iip
)
1154 int num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1156 if (pnode
->num
!= num
)
1159 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1160 int free
= pnode
->lprops
[i
].free
;
1161 int dirty
= pnode
->lprops
[i
].dirty
;
1163 if (free
< 0 || free
> c
->leb_size
|| free
% c
->min_io_size
||
1166 if (dirty
< 0 || dirty
> c
->leb_size
|| (dirty
& 7))
1168 if (dirty
+ free
> c
->leb_size
)
1175 * set_pnode_lnum - set LEB numbers on a pnode.
1176 * @c: UBIFS file-system description object
1177 * @pnode: pnode to update
1179 * This function calculates the LEB numbers for the LEB properties it contains
1180 * based on the pnode number.
1182 static void set_pnode_lnum(const struct ubifs_info
*c
,
1183 struct ubifs_pnode
*pnode
)
1187 lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + c
->main_first
;
1188 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1189 if (lnum
>= c
->leb_cnt
)
1191 pnode
->lprops
[i
].lnum
= lnum
++;
1196 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1197 * @c: UBIFS file-system description object
1198 * @parent: parent nnode (or NULL for the root)
1199 * @iip: index in parent
1201 * This function returns %0 on success and a negative error code on failure.
1203 int ubifs_read_nnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1205 struct ubifs_nbranch
*branch
= NULL
;
1206 struct ubifs_nnode
*nnode
= NULL
;
1207 void *buf
= c
->lpt_nod_buf
;
1208 int err
, lnum
, offs
;
1211 branch
= &parent
->nbranch
[iip
];
1212 lnum
= branch
->lnum
;
1213 offs
= branch
->offs
;
1218 nnode
= kzalloc(sizeof(struct ubifs_nnode
), GFP_NOFS
);
1225 * This nnode was not written which just means that the LEB
1226 * properties in the subtree below it describe empty LEBs. We
1227 * make the nnode as though we had read it, which in fact means
1228 * doing almost nothing.
1231 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1233 err
= ubifs_leb_read(c
, lnum
, buf
, offs
, c
->nnode_sz
, 1);
1236 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1240 err
= validate_nnode(c
, nnode
, parent
, iip
);
1244 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1246 branch
->nnode
= nnode
;
1247 nnode
->level
= parent
->level
- 1;
1250 nnode
->level
= c
->lpt_hght
;
1252 nnode
->parent
= parent
;
1257 ubifs_err(c
, "error %d reading nnode at %d:%d", err
, lnum
, offs
);
1264 * read_pnode - read a pnode from flash and link it to the tree in memory.
1265 * @c: UBIFS file-system description object
1266 * @parent: parent nnode
1267 * @iip: index in parent
1269 * This function returns %0 on success and a negative error code on failure.
1271 static int read_pnode(struct ubifs_info
*c
, struct ubifs_nnode
*parent
, int iip
)
1273 struct ubifs_nbranch
*branch
;
1274 struct ubifs_pnode
*pnode
= NULL
;
1275 void *buf
= c
->lpt_nod_buf
;
1276 int err
, lnum
, offs
;
1278 branch
= &parent
->nbranch
[iip
];
1279 lnum
= branch
->lnum
;
1280 offs
= branch
->offs
;
1281 pnode
= kzalloc(sizeof(struct ubifs_pnode
), GFP_NOFS
);
1287 * This pnode was not written which just means that the LEB
1288 * properties in it describe empty LEBs. We make the pnode as
1289 * though we had read it.
1294 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1295 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1296 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
1298 lprops
->free
= c
->leb_size
;
1299 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
1302 err
= ubifs_leb_read(c
, lnum
, buf
, offs
, c
->pnode_sz
, 1);
1305 err
= unpack_pnode(c
, buf
, pnode
);
1309 err
= validate_pnode(c
, pnode
, parent
, iip
);
1313 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
1314 branch
->pnode
= pnode
;
1315 pnode
->parent
= parent
;
1317 set_pnode_lnum(c
, pnode
);
1318 c
->pnodes_have
+= 1;
1322 ubifs_err(c
, "error %d reading pnode at %d:%d", err
, lnum
, offs
);
1323 ubifs_dump_pnode(c
, pnode
, parent
, iip
);
1325 ubifs_err(c
, "calc num: %d", calc_pnode_num_from_parent(c
, parent
, iip
));
1331 * read_ltab - read LPT's own lprops table.
1332 * @c: UBIFS file-system description object
1334 * This function returns %0 on success and a negative error code on failure.
1336 static int read_ltab(struct ubifs_info
*c
)
1341 buf
= vmalloc(c
->ltab_sz
);
1344 err
= ubifs_leb_read(c
, c
->ltab_lnum
, buf
, c
->ltab_offs
, c
->ltab_sz
, 1);
1347 err
= unpack_ltab(c
, buf
);
1354 * read_lsave - read LPT's save table.
1355 * @c: UBIFS file-system description object
1357 * This function returns %0 on success and a negative error code on failure.
1359 static int read_lsave(struct ubifs_info
*c
)
1364 buf
= vmalloc(c
->lsave_sz
);
1367 err
= ubifs_leb_read(c
, c
->lsave_lnum
, buf
, c
->lsave_offs
,
1371 err
= unpack_lsave(c
, buf
);
1374 for (i
= 0; i
< c
->lsave_cnt
; i
++) {
1375 int lnum
= c
->lsave
[i
];
1376 struct ubifs_lprops
*lprops
;
1379 * Due to automatic resizing, the values in the lsave table
1380 * could be beyond the volume size - just ignore them.
1382 if (lnum
>= c
->leb_cnt
)
1384 lprops
= ubifs_lpt_lookup(c
, lnum
);
1385 if (IS_ERR(lprops
)) {
1386 err
= PTR_ERR(lprops
);
1396 * ubifs_get_nnode - get a nnode.
1397 * @c: UBIFS file-system description object
1398 * @parent: parent nnode (or NULL for the root)
1399 * @iip: index in parent
1401 * This function returns a pointer to the nnode on success or a negative error
1404 struct ubifs_nnode
*ubifs_get_nnode(struct ubifs_info
*c
,
1405 struct ubifs_nnode
*parent
, int iip
)
1407 struct ubifs_nbranch
*branch
;
1408 struct ubifs_nnode
*nnode
;
1411 branch
= &parent
->nbranch
[iip
];
1412 nnode
= branch
->nnode
;
1415 err
= ubifs_read_nnode(c
, parent
, iip
);
1417 return ERR_PTR(err
);
1418 return branch
->nnode
;
1422 * ubifs_get_pnode - get a pnode.
1423 * @c: UBIFS file-system description object
1424 * @parent: parent nnode
1425 * @iip: index in parent
1427 * This function returns a pointer to the pnode on success or a negative error
1430 struct ubifs_pnode
*ubifs_get_pnode(struct ubifs_info
*c
,
1431 struct ubifs_nnode
*parent
, int iip
)
1433 struct ubifs_nbranch
*branch
;
1434 struct ubifs_pnode
*pnode
;
1437 branch
= &parent
->nbranch
[iip
];
1438 pnode
= branch
->pnode
;
1441 err
= read_pnode(c
, parent
, iip
);
1443 return ERR_PTR(err
);
1444 update_cats(c
, branch
->pnode
);
1445 return branch
->pnode
;
1449 * ubifs_pnode_lookup - lookup a pnode in the LPT.
1450 * @c: UBIFS file-system description object
1451 * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT)
1453 * This function returns a pointer to the pnode on success or a negative
1454 * error code on failure.
1456 struct ubifs_pnode
*ubifs_pnode_lookup(struct ubifs_info
*c
, int i
)
1458 int err
, h
, iip
, shft
;
1459 struct ubifs_nnode
*nnode
;
1462 err
= ubifs_read_nnode(c
, NULL
, 0);
1464 return ERR_PTR(err
);
1466 i
<<= UBIFS_LPT_FANOUT_SHIFT
;
1468 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1469 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1470 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1471 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1472 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1474 return ERR_CAST(nnode
);
1476 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1477 return ubifs_get_pnode(c
, nnode
, iip
);
1481 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1482 * @c: UBIFS file-system description object
1483 * @lnum: LEB number to lookup
1485 * This function returns a pointer to the LEB properties on success or a
1486 * negative error code on failure.
1488 struct ubifs_lprops
*ubifs_lpt_lookup(struct ubifs_info
*c
, int lnum
)
1491 struct ubifs_pnode
*pnode
;
1493 i
= lnum
- c
->main_first
;
1494 pnode
= ubifs_pnode_lookup(c
, i
>> UBIFS_LPT_FANOUT_SHIFT
);
1496 return ERR_CAST(pnode
);
1497 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1498 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum
,
1499 pnode
->lprops
[iip
].free
, pnode
->lprops
[iip
].dirty
,
1500 pnode
->lprops
[iip
].flags
);
1501 return &pnode
->lprops
[iip
];
1505 * dirty_cow_nnode - ensure a nnode is not being committed.
1506 * @c: UBIFS file-system description object
1507 * @nnode: nnode to check
1509 * Returns dirtied nnode on success or negative error code on failure.
1511 static struct ubifs_nnode
*dirty_cow_nnode(struct ubifs_info
*c
,
1512 struct ubifs_nnode
*nnode
)
1514 struct ubifs_nnode
*n
;
1517 if (!test_bit(COW_CNODE
, &nnode
->flags
)) {
1518 /* nnode is not being committed */
1519 if (!test_and_set_bit(DIRTY_CNODE
, &nnode
->flags
)) {
1520 c
->dirty_nn_cnt
+= 1;
1521 ubifs_add_nnode_dirt(c
, nnode
);
1526 /* nnode is being committed, so copy it */
1527 n
= kmemdup(nnode
, sizeof(struct ubifs_nnode
), GFP_NOFS
);
1529 return ERR_PTR(-ENOMEM
);
1532 __set_bit(DIRTY_CNODE
, &n
->flags
);
1533 __clear_bit(COW_CNODE
, &n
->flags
);
1535 /* The children now have new parent */
1536 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1537 struct ubifs_nbranch
*branch
= &n
->nbranch
[i
];
1540 branch
->cnode
->parent
= n
;
1543 ubifs_assert(c
, !test_bit(OBSOLETE_CNODE
, &nnode
->flags
));
1544 __set_bit(OBSOLETE_CNODE
, &nnode
->flags
);
1546 c
->dirty_nn_cnt
+= 1;
1547 ubifs_add_nnode_dirt(c
, nnode
);
1549 nnode
->parent
->nbranch
[n
->iip
].nnode
= n
;
1556 * dirty_cow_pnode - ensure a pnode is not being committed.
1557 * @c: UBIFS file-system description object
1558 * @pnode: pnode to check
1560 * Returns dirtied pnode on success or negative error code on failure.
1562 static struct ubifs_pnode
*dirty_cow_pnode(struct ubifs_info
*c
,
1563 struct ubifs_pnode
*pnode
)
1565 struct ubifs_pnode
*p
;
1567 if (!test_bit(COW_CNODE
, &pnode
->flags
)) {
1568 /* pnode is not being committed */
1569 if (!test_and_set_bit(DIRTY_CNODE
, &pnode
->flags
)) {
1570 c
->dirty_pn_cnt
+= 1;
1571 add_pnode_dirt(c
, pnode
);
1576 /* pnode is being committed, so copy it */
1577 p
= kmemdup(pnode
, sizeof(struct ubifs_pnode
), GFP_NOFS
);
1579 return ERR_PTR(-ENOMEM
);
1582 __set_bit(DIRTY_CNODE
, &p
->flags
);
1583 __clear_bit(COW_CNODE
, &p
->flags
);
1584 replace_cats(c
, pnode
, p
);
1586 ubifs_assert(c
, !test_bit(OBSOLETE_CNODE
, &pnode
->flags
));
1587 __set_bit(OBSOLETE_CNODE
, &pnode
->flags
);
1589 c
->dirty_pn_cnt
+= 1;
1590 add_pnode_dirt(c
, pnode
);
1591 pnode
->parent
->nbranch
[p
->iip
].pnode
= p
;
1596 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1597 * @c: UBIFS file-system description object
1598 * @lnum: LEB number to lookup
1600 * This function returns a pointer to the LEB properties on success or a
1601 * negative error code on failure.
1603 struct ubifs_lprops
*ubifs_lpt_lookup_dirty(struct ubifs_info
*c
, int lnum
)
1605 int err
, i
, h
, iip
, shft
;
1606 struct ubifs_nnode
*nnode
;
1607 struct ubifs_pnode
*pnode
;
1610 err
= ubifs_read_nnode(c
, NULL
, 0);
1612 return ERR_PTR(err
);
1615 nnode
= dirty_cow_nnode(c
, nnode
);
1617 return ERR_CAST(nnode
);
1618 i
= lnum
- c
->main_first
;
1619 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
1620 for (h
= 1; h
< c
->lpt_hght
; h
++) {
1621 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1622 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
1623 nnode
= ubifs_get_nnode(c
, nnode
, iip
);
1625 return ERR_CAST(nnode
);
1626 nnode
= dirty_cow_nnode(c
, nnode
);
1628 return ERR_CAST(nnode
);
1630 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
1631 pnode
= ubifs_get_pnode(c
, nnode
, iip
);
1633 return ERR_CAST(pnode
);
1634 pnode
= dirty_cow_pnode(c
, pnode
);
1636 return ERR_CAST(pnode
);
1637 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
1638 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum
,
1639 pnode
->lprops
[iip
].free
, pnode
->lprops
[iip
].dirty
,
1640 pnode
->lprops
[iip
].flags
);
1641 ubifs_assert(c
, test_bit(DIRTY_CNODE
, &pnode
->flags
));
1642 return &pnode
->lprops
[iip
];
1646 * ubifs_lpt_calc_hash - Calculate hash of the LPT pnodes
1647 * @c: UBIFS file-system description object
1648 * @hash: the returned hash of the LPT pnodes
1650 * This function iterates over the LPT pnodes and creates a hash over them.
1651 * Returns 0 for success or a negative error code otherwise.
1653 int ubifs_lpt_calc_hash(struct ubifs_info
*c
, u8
*hash
)
1655 struct ubifs_nnode
*nnode
, *nn
;
1656 struct ubifs_cnode
*cnode
;
1657 struct shash_desc
*desc
;
1659 int bufsiz
= max_t(int, c
->nnode_sz
, c
->pnode_sz
);
1663 if (!ubifs_authenticated(c
))
1667 err
= ubifs_read_nnode(c
, NULL
, 0);
1672 desc
= ubifs_hash_get_desc(c
);
1674 return PTR_ERR(desc
);
1676 buf
= kmalloc(bufsiz
, GFP_NOFS
);
1682 cnode
= (struct ubifs_cnode
*)c
->nroot
;
1685 nnode
= cnode
->parent
;
1686 nn
= (struct ubifs_nnode
*)cnode
;
1687 if (cnode
->level
> 1) {
1688 while (iip
< UBIFS_LPT_FANOUT
) {
1689 if (nn
->nbranch
[iip
].lnum
== 0) {
1695 nnode
= ubifs_get_nnode(c
, nn
, iip
);
1696 if (IS_ERR(nnode
)) {
1697 err
= PTR_ERR(nnode
);
1703 cnode
= (struct ubifs_cnode
*)nnode
;
1706 if (iip
< UBIFS_LPT_FANOUT
)
1709 struct ubifs_pnode
*pnode
;
1711 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
1712 if (nn
->nbranch
[i
].lnum
== 0)
1714 pnode
= ubifs_get_pnode(c
, nn
, i
);
1715 if (IS_ERR(pnode
)) {
1716 err
= PTR_ERR(pnode
);
1720 ubifs_pack_pnode(c
, buf
, pnode
);
1721 err
= ubifs_shash_update(c
, desc
, buf
,
1727 /* Go up and to the right */
1728 iip
= cnode
->iip
+ 1;
1729 cnode
= (struct ubifs_cnode
*)nnode
;
1732 err
= ubifs_shash_final(c
, desc
, hash
);
1741 * lpt_check_hash - check the hash of the LPT.
1742 * @c: UBIFS file-system description object
1744 * This function calculates a hash over all pnodes in the LPT and compares it with
1745 * the hash stored in the master node. Returns %0 on success and a negative error
1748 static int lpt_check_hash(struct ubifs_info
*c
)
1751 u8 hash
[UBIFS_HASH_ARR_SZ
];
1753 if (!ubifs_authenticated(c
))
1756 err
= ubifs_lpt_calc_hash(c
, hash
);
1760 if (ubifs_check_hash(c
, c
->mst_node
->hash_lpt
, hash
)) {
1762 ubifs_err(c
, "Failed to authenticate LPT");
1771 * lpt_init_rd - initialize the LPT for reading.
1772 * @c: UBIFS file-system description object
1774 * This function returns %0 on success and a negative error code on failure.
1776 static int lpt_init_rd(struct ubifs_info
*c
)
1780 c
->ltab
= vmalloc(array_size(sizeof(struct ubifs_lpt_lprops
),
1785 i
= max_t(int, c
->nnode_sz
, c
->pnode_sz
);
1786 c
->lpt_nod_buf
= kmalloc(i
, GFP_KERNEL
);
1787 if (!c
->lpt_nod_buf
)
1790 for (i
= 0; i
< LPROPS_HEAP_CNT
; i
++) {
1791 c
->lpt_heap
[i
].arr
= kmalloc_array(LPT_HEAP_SZ
,
1794 if (!c
->lpt_heap
[i
].arr
)
1796 c
->lpt_heap
[i
].cnt
= 0;
1797 c
->lpt_heap
[i
].max_cnt
= LPT_HEAP_SZ
;
1800 c
->dirty_idx
.arr
= kmalloc_array(LPT_HEAP_SZ
, sizeof(void *),
1802 if (!c
->dirty_idx
.arr
)
1804 c
->dirty_idx
.cnt
= 0;
1805 c
->dirty_idx
.max_cnt
= LPT_HEAP_SZ
;
1811 err
= lpt_check_hash(c
);
1815 dbg_lp("space_bits %d", c
->space_bits
);
1816 dbg_lp("lpt_lnum_bits %d", c
->lpt_lnum_bits
);
1817 dbg_lp("lpt_offs_bits %d", c
->lpt_offs_bits
);
1818 dbg_lp("lpt_spc_bits %d", c
->lpt_spc_bits
);
1819 dbg_lp("pcnt_bits %d", c
->pcnt_bits
);
1820 dbg_lp("lnum_bits %d", c
->lnum_bits
);
1821 dbg_lp("pnode_sz %d", c
->pnode_sz
);
1822 dbg_lp("nnode_sz %d", c
->nnode_sz
);
1823 dbg_lp("ltab_sz %d", c
->ltab_sz
);
1824 dbg_lp("lsave_sz %d", c
->lsave_sz
);
1825 dbg_lp("lsave_cnt %d", c
->lsave_cnt
);
1826 dbg_lp("lpt_hght %d", c
->lpt_hght
);
1827 dbg_lp("big_lpt %d", c
->big_lpt
);
1828 dbg_lp("LPT root is at %d:%d", c
->lpt_lnum
, c
->lpt_offs
);
1829 dbg_lp("LPT head is at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
1830 dbg_lp("LPT ltab is at %d:%d", c
->ltab_lnum
, c
->ltab_offs
);
1832 dbg_lp("LPT lsave is at %d:%d", c
->lsave_lnum
, c
->lsave_offs
);
1838 * lpt_init_wr - initialize the LPT for writing.
1839 * @c: UBIFS file-system description object
1841 * 'lpt_init_rd()' must have been called already.
1843 * This function returns %0 on success and a negative error code on failure.
1845 static int lpt_init_wr(struct ubifs_info
*c
)
1849 c
->ltab_cmt
= vmalloc(array_size(sizeof(struct ubifs_lpt_lprops
),
1854 c
->lpt_buf
= vmalloc(c
->leb_size
);
1859 c
->lsave
= kmalloc_array(c
->lsave_cnt
, sizeof(int), GFP_NOFS
);
1862 err
= read_lsave(c
);
1867 for (i
= 0; i
< c
->lpt_lebs
; i
++)
1868 if (c
->ltab
[i
].free
== c
->leb_size
) {
1869 err
= ubifs_leb_unmap(c
, i
+ c
->lpt_first
);
1878 * ubifs_lpt_init - initialize the LPT.
1879 * @c: UBIFS file-system description object
1880 * @rd: whether to initialize lpt for reading
1881 * @wr: whether to initialize lpt for writing
1883 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1884 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1887 * This function returns %0 on success and a negative error code on failure.
1889 int ubifs_lpt_init(struct ubifs_info
*c
, int rd
, int wr
)
1894 err
= lpt_init_rd(c
);
1900 err
= lpt_init_wr(c
);
1909 ubifs_lpt_free(c
, 1);
1911 ubifs_lpt_free(c
, 0);
1916 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1917 * @nnode: where to keep a nnode
1918 * @pnode: where to keep a pnode
1919 * @cnode: where to keep a cnode
1920 * @in_tree: is the node in the tree in memory
1921 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1923 * @ptr.pnode: ditto for pnode
1924 * @ptr.cnode: ditto for cnode
1926 struct lpt_scan_node
{
1928 struct ubifs_nnode nnode
;
1929 struct ubifs_pnode pnode
;
1930 struct ubifs_cnode cnode
;
1934 struct ubifs_nnode
*nnode
;
1935 struct ubifs_pnode
*pnode
;
1936 struct ubifs_cnode
*cnode
;
1941 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1942 * @c: the UBIFS file-system description object
1943 * @path: where to put the nnode
1944 * @parent: parent of the nnode
1945 * @iip: index in parent of the nnode
1947 * This function returns a pointer to the nnode on success or a negative error
1950 static struct ubifs_nnode
*scan_get_nnode(struct ubifs_info
*c
,
1951 struct lpt_scan_node
*path
,
1952 struct ubifs_nnode
*parent
, int iip
)
1954 struct ubifs_nbranch
*branch
;
1955 struct ubifs_nnode
*nnode
;
1956 void *buf
= c
->lpt_nod_buf
;
1959 branch
= &parent
->nbranch
[iip
];
1960 nnode
= branch
->nnode
;
1963 path
->ptr
.nnode
= nnode
;
1966 nnode
= &path
->nnode
;
1968 path
->ptr
.nnode
= nnode
;
1969 memset(nnode
, 0, sizeof(struct ubifs_nnode
));
1970 if (branch
->lnum
== 0) {
1972 * This nnode was not written which just means that the LEB
1973 * properties in the subtree below it describe empty LEBs. We
1974 * make the nnode as though we had read it, which in fact means
1975 * doing almost nothing.
1978 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1980 err
= ubifs_leb_read(c
, branch
->lnum
, buf
, branch
->offs
,
1983 return ERR_PTR(err
);
1984 err
= ubifs_unpack_nnode(c
, buf
, nnode
);
1986 return ERR_PTR(err
);
1988 err
= validate_nnode(c
, nnode
, parent
, iip
);
1990 return ERR_PTR(err
);
1992 nnode
->num
= calc_nnode_num_from_parent(c
, parent
, iip
);
1993 nnode
->level
= parent
->level
- 1;
1994 nnode
->parent
= parent
;
2000 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
2001 * @c: the UBIFS file-system description object
2002 * @path: where to put the pnode
2003 * @parent: parent of the pnode
2004 * @iip: index in parent of the pnode
2006 * This function returns a pointer to the pnode on success or a negative error
2009 static struct ubifs_pnode
*scan_get_pnode(struct ubifs_info
*c
,
2010 struct lpt_scan_node
*path
,
2011 struct ubifs_nnode
*parent
, int iip
)
2013 struct ubifs_nbranch
*branch
;
2014 struct ubifs_pnode
*pnode
;
2015 void *buf
= c
->lpt_nod_buf
;
2018 branch
= &parent
->nbranch
[iip
];
2019 pnode
= branch
->pnode
;
2022 path
->ptr
.pnode
= pnode
;
2025 pnode
= &path
->pnode
;
2027 path
->ptr
.pnode
= pnode
;
2028 memset(pnode
, 0, sizeof(struct ubifs_pnode
));
2029 if (branch
->lnum
== 0) {
2031 * This pnode was not written which just means that the LEB
2032 * properties in it describe empty LEBs. We make the pnode as
2033 * though we had read it.
2038 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
2039 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
2040 struct ubifs_lprops
* const lprops
= &pnode
->lprops
[i
];
2042 lprops
->free
= c
->leb_size
;
2043 lprops
->flags
= ubifs_categorize_lprops(c
, lprops
);
2046 ubifs_assert(c
, branch
->lnum
>= c
->lpt_first
&&
2047 branch
->lnum
<= c
->lpt_last
);
2048 ubifs_assert(c
, branch
->offs
>= 0 && branch
->offs
< c
->leb_size
);
2049 err
= ubifs_leb_read(c
, branch
->lnum
, buf
, branch
->offs
,
2052 return ERR_PTR(err
);
2053 err
= unpack_pnode(c
, buf
, pnode
);
2055 return ERR_PTR(err
);
2057 err
= validate_pnode(c
, pnode
, parent
, iip
);
2059 return ERR_PTR(err
);
2061 pnode
->num
= calc_pnode_num_from_parent(c
, parent
, iip
);
2062 pnode
->parent
= parent
;
2064 set_pnode_lnum(c
, pnode
);
2069 * ubifs_lpt_scan_nolock - scan the LPT.
2070 * @c: the UBIFS file-system description object
2071 * @start_lnum: LEB number from which to start scanning
2072 * @end_lnum: LEB number at which to stop scanning
2073 * @scan_cb: callback function called for each lprops
2074 * @data: data to be passed to the callback function
2076 * This function returns %0 on success and a negative error code on failure.
2078 int ubifs_lpt_scan_nolock(struct ubifs_info
*c
, int start_lnum
, int end_lnum
,
2079 ubifs_lpt_scan_callback scan_cb
, void *data
)
2081 int err
= 0, i
, h
, iip
, shft
;
2082 struct ubifs_nnode
*nnode
;
2083 struct ubifs_pnode
*pnode
;
2084 struct lpt_scan_node
*path
;
2086 if (start_lnum
== -1) {
2087 start_lnum
= end_lnum
+ 1;
2088 if (start_lnum
>= c
->leb_cnt
)
2089 start_lnum
= c
->main_first
;
2092 ubifs_assert(c
, start_lnum
>= c
->main_first
&& start_lnum
< c
->leb_cnt
);
2093 ubifs_assert(c
, end_lnum
>= c
->main_first
&& end_lnum
< c
->leb_cnt
);
2096 err
= ubifs_read_nnode(c
, NULL
, 0);
2101 path
= kmalloc_array(c
->lpt_hght
+ 1, sizeof(struct lpt_scan_node
),
2106 path
[0].ptr
.nnode
= c
->nroot
;
2107 path
[0].in_tree
= 1;
2109 /* Descend to the pnode containing start_lnum */
2111 i
= start_lnum
- c
->main_first
;
2112 shft
= c
->lpt_hght
* UBIFS_LPT_FANOUT_SHIFT
;
2113 for (h
= 1; h
< c
->lpt_hght
; h
++) {
2114 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
2115 shft
-= UBIFS_LPT_FANOUT_SHIFT
;
2116 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
2117 if (IS_ERR(nnode
)) {
2118 err
= PTR_ERR(nnode
);
2122 iip
= ((i
>> shft
) & (UBIFS_LPT_FANOUT
- 1));
2123 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
2124 if (IS_ERR(pnode
)) {
2125 err
= PTR_ERR(pnode
);
2128 iip
= (i
& (UBIFS_LPT_FANOUT
- 1));
2130 /* Loop for each lprops */
2132 struct ubifs_lprops
*lprops
= &pnode
->lprops
[iip
];
2133 int ret
, lnum
= lprops
->lnum
;
2135 ret
= scan_cb(c
, lprops
, path
[h
].in_tree
, data
);
2140 if (ret
& LPT_SCAN_ADD
) {
2141 /* Add all the nodes in path to the tree in memory */
2142 for (h
= 1; h
< c
->lpt_hght
; h
++) {
2143 const size_t sz
= sizeof(struct ubifs_nnode
);
2144 struct ubifs_nnode
*parent
;
2146 if (path
[h
].in_tree
)
2148 nnode
= kmemdup(&path
[h
].nnode
, sz
, GFP_NOFS
);
2153 parent
= nnode
->parent
;
2154 parent
->nbranch
[nnode
->iip
].nnode
= nnode
;
2155 path
[h
].ptr
.nnode
= nnode
;
2156 path
[h
].in_tree
= 1;
2157 path
[h
+ 1].cnode
.parent
= nnode
;
2159 if (path
[h
].in_tree
)
2160 ubifs_ensure_cat(c
, lprops
);
2162 const size_t sz
= sizeof(struct ubifs_pnode
);
2163 struct ubifs_nnode
*parent
;
2165 pnode
= kmemdup(&path
[h
].pnode
, sz
, GFP_NOFS
);
2170 parent
= pnode
->parent
;
2171 parent
->nbranch
[pnode
->iip
].pnode
= pnode
;
2172 path
[h
].ptr
.pnode
= pnode
;
2173 path
[h
].in_tree
= 1;
2174 update_cats(c
, pnode
);
2175 c
->pnodes_have
+= 1;
2177 err
= dbg_check_lpt_nodes(c
, (struct ubifs_cnode
*)
2181 err
= dbg_check_cats(c
);
2185 if (ret
& LPT_SCAN_STOP
) {
2189 /* Get the next lprops */
2190 if (lnum
== end_lnum
) {
2192 * We got to the end without finding what we were
2198 if (lnum
+ 1 >= c
->leb_cnt
) {
2199 /* Wrap-around to the beginning */
2200 start_lnum
= c
->main_first
;
2203 if (iip
+ 1 < UBIFS_LPT_FANOUT
) {
2204 /* Next lprops is in the same pnode */
2208 /* We need to get the next pnode. Go up until we can go right */
2212 ubifs_assert(c
, h
>= 0);
2213 nnode
= path
[h
].ptr
.nnode
;
2214 if (iip
+ 1 < UBIFS_LPT_FANOUT
)
2220 /* Descend to the pnode */
2222 for (; h
< c
->lpt_hght
; h
++) {
2223 nnode
= scan_get_nnode(c
, path
+ h
, nnode
, iip
);
2224 if (IS_ERR(nnode
)) {
2225 err
= PTR_ERR(nnode
);
2230 pnode
= scan_get_pnode(c
, path
+ h
, nnode
, iip
);
2231 if (IS_ERR(pnode
)) {
2232 err
= PTR_ERR(pnode
);
2243 * dbg_chk_pnode - check a pnode.
2244 * @c: the UBIFS file-system description object
2245 * @pnode: pnode to check
2246 * @col: pnode column
2248 * This function returns %0 on success and a negative error code on failure.
2250 static int dbg_chk_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
2255 if (pnode
->num
!= col
) {
2256 ubifs_err(c
, "pnode num %d expected %d parent num %d iip %d",
2257 pnode
->num
, col
, pnode
->parent
->num
, pnode
->iip
);
2260 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
2261 struct ubifs_lprops
*lp
, *lprops
= &pnode
->lprops
[i
];
2262 int lnum
= (pnode
->num
<< UBIFS_LPT_FANOUT_SHIFT
) + i
+
2264 int found
, cat
= lprops
->flags
& LPROPS_CAT_MASK
;
2265 struct ubifs_lpt_heap
*heap
;
2266 struct list_head
*list
= NULL
;
2268 if (lnum
>= c
->leb_cnt
)
2270 if (lprops
->lnum
!= lnum
) {
2271 ubifs_err(c
, "bad LEB number %d expected %d",
2272 lprops
->lnum
, lnum
);
2275 if (lprops
->flags
& LPROPS_TAKEN
) {
2276 if (cat
!= LPROPS_UNCAT
) {
2277 ubifs_err(c
, "LEB %d taken but not uncat %d",
2283 if (lprops
->flags
& LPROPS_INDEX
) {
2286 case LPROPS_DIRTY_IDX
:
2287 case LPROPS_FRDI_IDX
:
2290 ubifs_err(c
, "LEB %d index but cat %d",
2300 case LPROPS_FREEABLE
:
2303 ubifs_err(c
, "LEB %d not index but cat %d",
2310 list
= &c
->uncat_list
;
2313 list
= &c
->empty_list
;
2315 case LPROPS_FREEABLE
:
2316 list
= &c
->freeable_list
;
2318 case LPROPS_FRDI_IDX
:
2319 list
= &c
->frdi_idx_list
;
2325 case LPROPS_DIRTY_IDX
:
2327 heap
= &c
->lpt_heap
[cat
- 1];
2328 if (lprops
->hpos
< heap
->cnt
&&
2329 heap
->arr
[lprops
->hpos
] == lprops
)
2334 case LPROPS_FREEABLE
:
2335 case LPROPS_FRDI_IDX
:
2336 list_for_each_entry(lp
, list
, list
)
2344 ubifs_err(c
, "LEB %d cat %d not found in cat heap/list",
2350 if (lprops
->free
!= c
->leb_size
) {
2351 ubifs_err(c
, "LEB %d cat %d free %d dirty %d",
2352 lprops
->lnum
, cat
, lprops
->free
,
2357 case LPROPS_FREEABLE
:
2358 case LPROPS_FRDI_IDX
:
2359 if (lprops
->free
+ lprops
->dirty
!= c
->leb_size
) {
2360 ubifs_err(c
, "LEB %d cat %d free %d dirty %d",
2361 lprops
->lnum
, cat
, lprops
->free
,
2372 * dbg_check_lpt_nodes - check nnodes and pnodes.
2373 * @c: the UBIFS file-system description object
2374 * @cnode: next cnode (nnode or pnode) to check
2375 * @row: row of cnode (root is zero)
2376 * @col: column of cnode (leftmost is zero)
2378 * This function returns %0 on success and a negative error code on failure.
2380 int dbg_check_lpt_nodes(struct ubifs_info
*c
, struct ubifs_cnode
*cnode
,
2383 struct ubifs_nnode
*nnode
, *nn
;
2384 struct ubifs_cnode
*cn
;
2385 int num
, iip
= 0, err
;
2387 if (!dbg_is_chk_lprops(c
))
2391 ubifs_assert(c
, row
>= 0);
2392 nnode
= cnode
->parent
;
2394 /* cnode is a nnode */
2395 num
= calc_nnode_num(row
, col
);
2396 if (cnode
->num
!= num
) {
2397 ubifs_err(c
, "nnode num %d expected %d parent num %d iip %d",
2399 (nnode
? nnode
->num
: 0), cnode
->iip
);
2402 nn
= (struct ubifs_nnode
*)cnode
;
2403 while (iip
< UBIFS_LPT_FANOUT
) {
2404 cn
= nn
->nbranch
[iip
].cnode
;
2408 col
<<= UBIFS_LPT_FANOUT_SHIFT
;
2417 if (iip
< UBIFS_LPT_FANOUT
)
2420 struct ubifs_pnode
*pnode
;
2422 /* cnode is a pnode */
2423 pnode
= (struct ubifs_pnode
*)cnode
;
2424 err
= dbg_chk_pnode(c
, pnode
, col
);
2428 /* Go up and to the right */
2430 col
>>= UBIFS_LPT_FANOUT_SHIFT
;
2431 iip
= cnode
->iip
+ 1;
2432 cnode
= (struct ubifs_cnode
*)nnode
;