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
607 * @hash: hash of the LPT is returned here
609 * This function returns %0 on success and a negative error code on failure.
611 int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
612 int *lpt_lebs, int *big_lpt, u8 *hash)
614 int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
615 int blnum, boffs, bsz, bcnt;
616 struct ubifs_pnode *pnode = NULL;
617 struct ubifs_nnode *nnode = NULL;
618 void *buf = NULL, *p;
619 struct ubifs_lpt_lprops *ltab = NULL;
621 struct shash_desc *desc;
623 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
626 *lpt_lebs = c->lpt_lebs;
628 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
629 c->lpt_first = lpt_first;
630 /* Needed by 'set_ltab()' */
631 c->lpt_last = lpt_first + c->lpt_lebs - 1;
632 /* Needed by 'ubifs_pack_lsave()' */
633 c->main_first = c->leb_cnt - *main_lebs;
635 desc = ubifs_hash_get_desc(c);
637 return PTR_ERR(desc);
639 lsave = kmalloc_array(c->lsave_cnt, sizeof(int), GFP_KERNEL);
640 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
641 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
642 buf = vmalloc(c->leb_size);
643 ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
645 if (!pnode || !nnode || !buf || !ltab || !lsave) {
650 ubifs_assert(c, !c->ltab);
651 c->ltab = ltab; /* Needed by set_ltab */
653 /* Initialize LPT's own lprops */
654 for (i = 0; i < c->lpt_lebs; i++) {
655 ltab[i].free = c->leb_size;
663 /* Number of leaf nodes (pnodes) */
667 * The first pnode contains the LEB properties for the LEBs that contain
668 * the root inode node and the root index node of the index tree.
670 node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
671 iopos = ALIGN(node_sz, c->min_io_size);
672 pnode->lprops[0].free = c->leb_size - iopos;
673 pnode->lprops[0].dirty = iopos - node_sz;
674 pnode->lprops[0].flags = LPROPS_INDEX;
676 node_sz = UBIFS_INO_NODE_SZ;
677 iopos = ALIGN(node_sz, c->min_io_size);
678 pnode->lprops[1].free = c->leb_size - iopos;
679 pnode->lprops[1].dirty = iopos - node_sz;
681 for (i = 2; i < UBIFS_LPT_FANOUT; i++)
682 pnode->lprops[i].free = c->leb_size;
684 /* Add first pnode */
685 ubifs_pack_pnode(c, p, pnode);
686 err = ubifs_shash_update(c, desc, p, c->pnode_sz);
694 /* Reset pnode values for remaining pnodes */
695 pnode->lprops[0].free = c->leb_size;
696 pnode->lprops[0].dirty = 0;
697 pnode->lprops[0].flags = 0;
699 pnode->lprops[1].free = c->leb_size;
700 pnode->lprops[1].dirty = 0;
703 * To calculate the internal node branches, we keep information about
706 blnum = lnum; /* LEB number of level below */
707 boffs = 0; /* Offset of level below */
708 bcnt = cnt; /* Number of nodes in level below */
709 bsz = c->pnode_sz; /* Size of nodes in level below */
711 /* Add all remaining pnodes */
712 for (i = 1; i < cnt; i++) {
713 if (len + c->pnode_sz > c->leb_size) {
714 alen = ALIGN(len, c->min_io_size);
715 set_ltab(c, lnum, c->leb_size - alen, alen - len);
716 memset(p, 0xff, alen - len);
717 err = ubifs_leb_change(c, lnum++, buf, alen);
723 ubifs_pack_pnode(c, p, pnode);
724 err = ubifs_shash_update(c, desc, p, c->pnode_sz);
731 * pnodes are simply numbered left to right starting at zero,
732 * which means the pnode number can be used easily to traverse
733 * down the tree to the corresponding pnode.
739 for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
741 /* Add all nnodes, one level at a time */
743 /* Number of internal nodes (nnodes) at next level */
744 cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
745 for (i = 0; i < cnt; i++) {
746 if (len + c->nnode_sz > c->leb_size) {
747 alen = ALIGN(len, c->min_io_size);
748 set_ltab(c, lnum, c->leb_size - alen,
750 memset(p, 0xff, alen - len);
751 err = ubifs_leb_change(c, lnum++, buf, alen);
757 /* Only 1 nnode at this level, so it is the root */
762 /* Set branches to the level below */
763 for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
765 if (boffs + bsz > c->leb_size) {
769 nnode->nbranch[j].lnum = blnum;
770 nnode->nbranch[j].offs = boffs;
774 nnode->nbranch[j].lnum = 0;
775 nnode->nbranch[j].offs = 0;
778 nnode->num = calc_nnode_num(row, i);
779 ubifs_pack_nnode(c, p, nnode);
783 /* Only 1 nnode at this level, so it is the root */
786 /* Update the information about the level below */
793 /* Need to add LPT's save table */
794 if (len + c->lsave_sz > c->leb_size) {
795 alen = ALIGN(len, c->min_io_size);
796 set_ltab(c, lnum, c->leb_size - alen, alen - len);
797 memset(p, 0xff, alen - len);
798 err = ubifs_leb_change(c, lnum++, buf, alen);
805 c->lsave_lnum = lnum;
808 for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
809 lsave[i] = c->main_first + i;
810 for (; i < c->lsave_cnt; i++)
811 lsave[i] = c->main_first;
813 ubifs_pack_lsave(c, p, lsave);
818 /* Need to add LPT's own LEB properties table */
819 if (len + c->ltab_sz > c->leb_size) {
820 alen = ALIGN(len, c->min_io_size);
821 set_ltab(c, lnum, c->leb_size - alen, alen - len);
822 memset(p, 0xff, alen - len);
823 err = ubifs_leb_change(c, lnum++, buf, alen);
833 /* Update ltab before packing it */
835 alen = ALIGN(len, c->min_io_size);
836 set_ltab(c, lnum, c->leb_size - alen, alen - len);
838 ubifs_pack_ltab(c, p, ltab);
841 /* Write remaining buffer */
842 memset(p, 0xff, alen - len);
843 err = ubifs_leb_change(c, lnum, buf, alen);
847 err = ubifs_shash_final(c, desc, hash);
851 c->nhead_lnum = lnum;
852 c->nhead_offs = ALIGN(len, c->min_io_size);
854 dbg_lp("space_bits %d", c->space_bits);
855 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
856 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
857 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
858 dbg_lp("pcnt_bits %d", c->pcnt_bits);
859 dbg_lp("lnum_bits %d", c->lnum_bits);
860 dbg_lp("pnode_sz %d", c->pnode_sz);
861 dbg_lp("nnode_sz %d", c->nnode_sz);
862 dbg_lp("ltab_sz %d", c->ltab_sz);
863 dbg_lp("lsave_sz %d", c->lsave_sz);
864 dbg_lp("lsave_cnt %d", c->lsave_cnt);
865 dbg_lp("lpt_hght %d", c->lpt_hght);
866 dbg_lp("big_lpt %d", c->big_lpt);
867 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
868 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
869 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
871 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
884 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
885 * @c: UBIFS file-system description object
888 * When a pnode is loaded into memory, the LEB properties it contains are added,
889 * by this function, to the LEB category lists and heaps.
891 static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
895 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
896 int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
897 int lnum = pnode->lprops[i].lnum;
901 ubifs_add_to_cat(c, &pnode->lprops[i], cat);
906 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
907 * @c: UBIFS file-system description object
908 * @old_pnode: pnode copied
909 * @new_pnode: pnode copy
911 * During commit it is sometimes necessary to copy a pnode
912 * (see dirty_cow_pnode). When that happens, references in
913 * category lists and heaps must be replaced. This function does that.
915 static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
916 struct ubifs_pnode *new_pnode)
920 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
921 if (!new_pnode->lprops[i].lnum)
923 ubifs_replace_cat(c, &old_pnode->lprops[i],
924 &new_pnode->lprops[i]);
929 * check_lpt_crc - check LPT node crc is correct.
930 * @c: UBIFS file-system description object
931 * @buf: buffer containing node
932 * @len: length of node
934 * This function returns %0 on success and a negative error code on failure.
936 static int check_lpt_crc(const struct ubifs_info *c, void *buf, int len)
940 uint16_t crc, calc_crc;
942 crc = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_CRC_BITS);
943 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
944 len - UBIFS_LPT_CRC_BYTES);
945 if (crc != calc_crc) {
946 ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx",
955 * check_lpt_type - check LPT node type is correct.
956 * @c: UBIFS file-system description object
957 * @addr: address of type bit field is passed and returned updated here
958 * @pos: position of type bit field is passed and returned updated here
959 * @type: expected type
961 * This function returns %0 on success and a negative error code on failure.
963 static int check_lpt_type(const struct ubifs_info *c, uint8_t **addr,
968 node_type = ubifs_unpack_bits(c, addr, pos, UBIFS_LPT_TYPE_BITS);
969 if (node_type != type) {
970 ubifs_err(c, "invalid type (%d) in LPT node type %d",
979 * unpack_pnode - unpack a pnode.
980 * @c: UBIFS file-system description object
981 * @buf: buffer containing packed pnode to unpack
982 * @pnode: pnode structure to fill
984 * This function returns %0 on success and a negative error code on failure.
986 static int unpack_pnode(const struct ubifs_info *c, void *buf,
987 struct ubifs_pnode *pnode)
989 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
992 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE);
996 pnode->num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits);
997 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
998 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1000 lprops->free = ubifs_unpack_bits(c, &addr, &pos, c->space_bits);
1002 lprops->dirty = ubifs_unpack_bits(c, &addr, &pos, c->space_bits);
1003 lprops->dirty <<= 3;
1005 if (ubifs_unpack_bits(c, &addr, &pos, 1))
1006 lprops->flags = LPROPS_INDEX;
1009 lprops->flags |= ubifs_categorize_lprops(c, lprops);
1011 err = check_lpt_crc(c, buf, c->pnode_sz);
1016 * ubifs_unpack_nnode - unpack a nnode.
1017 * @c: UBIFS file-system description object
1018 * @buf: buffer containing packed nnode to unpack
1019 * @nnode: nnode structure to fill
1021 * This function returns %0 on success and a negative error code on failure.
1023 int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1024 struct ubifs_nnode *nnode)
1026 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1027 int i, pos = 0, err;
1029 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE);
1033 nnode->num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits);
1034 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1037 lnum = ubifs_unpack_bits(c, &addr, &pos, c->lpt_lnum_bits) +
1039 if (lnum == c->lpt_last + 1)
1041 nnode->nbranch[i].lnum = lnum;
1042 nnode->nbranch[i].offs = ubifs_unpack_bits(c, &addr, &pos,
1045 err = check_lpt_crc(c, buf, c->nnode_sz);
1050 * unpack_ltab - unpack the LPT's own lprops table.
1051 * @c: UBIFS file-system description object
1052 * @buf: buffer from which to unpack
1054 * This function returns %0 on success and a negative error code on failure.
1056 static int unpack_ltab(const struct ubifs_info *c, void *buf)
1058 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1059 int i, pos = 0, err;
1061 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB);
1064 for (i = 0; i < c->lpt_lebs; i++) {
1065 int free = ubifs_unpack_bits(c, &addr, &pos, c->lpt_spc_bits);
1066 int dirty = ubifs_unpack_bits(c, &addr, &pos, c->lpt_spc_bits);
1068 if (free < 0 || free > c->leb_size || dirty < 0 ||
1069 dirty > c->leb_size || free + dirty > c->leb_size)
1072 c->ltab[i].free = free;
1073 c->ltab[i].dirty = dirty;
1077 err = check_lpt_crc(c, buf, c->ltab_sz);
1082 * unpack_lsave - unpack the LPT's save table.
1083 * @c: UBIFS file-system description object
1084 * @buf: buffer from which to unpack
1086 * This function returns %0 on success and a negative error code on failure.
1088 static int unpack_lsave(const struct ubifs_info *c, void *buf)
1090 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1091 int i, pos = 0, err;
1093 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LSAVE);
1096 for (i = 0; i < c->lsave_cnt; i++) {
1097 int lnum = ubifs_unpack_bits(c, &addr, &pos, c->lnum_bits);
1099 if (lnum < c->main_first || lnum >= c->leb_cnt)
1103 err = check_lpt_crc(c, buf, c->lsave_sz);
1108 * validate_nnode - validate a nnode.
1109 * @c: UBIFS file-system description object
1110 * @nnode: nnode to validate
1111 * @parent: parent nnode (or NULL for the root nnode)
1112 * @iip: index in parent
1114 * This function returns %0 on success and a negative error code on failure.
1116 static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1117 struct ubifs_nnode *parent, int iip)
1119 int i, lvl, max_offs;
1122 int num = calc_nnode_num_from_parent(c, parent, iip);
1124 if (nnode->num != num)
1127 lvl = parent ? parent->level - 1 : c->lpt_hght;
1131 max_offs = c->leb_size - c->pnode_sz;
1133 max_offs = c->leb_size - c->nnode_sz;
1134 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1135 int lnum = nnode->nbranch[i].lnum;
1136 int offs = nnode->nbranch[i].offs;
1143 if (lnum < c->lpt_first || lnum > c->lpt_last)
1145 if (offs < 0 || offs > max_offs)
1152 * validate_pnode - validate a pnode.
1153 * @c: UBIFS file-system description object
1154 * @pnode: pnode to validate
1155 * @parent: parent nnode
1156 * @iip: index in parent
1158 * This function returns %0 on success and a negative error code on failure.
1160 static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1161 struct ubifs_nnode *parent, int iip)
1166 int num = calc_pnode_num_from_parent(c, parent, iip);
1168 if (pnode->num != num)
1171 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1172 int free = pnode->lprops[i].free;
1173 int dirty = pnode->lprops[i].dirty;
1175 if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1178 if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1180 if (dirty + free > c->leb_size)
1187 * set_pnode_lnum - set LEB numbers on a pnode.
1188 * @c: UBIFS file-system description object
1189 * @pnode: pnode to update
1191 * This function calculates the LEB numbers for the LEB properties it contains
1192 * based on the pnode number.
1194 static void set_pnode_lnum(const struct ubifs_info *c,
1195 struct ubifs_pnode *pnode)
1199 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1200 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1201 if (lnum >= c->leb_cnt)
1203 pnode->lprops[i].lnum = lnum++;
1208 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1209 * @c: UBIFS file-system description object
1210 * @parent: parent nnode (or NULL for the root)
1211 * @iip: index in parent
1213 * This function returns %0 on success and a negative error code on failure.
1215 int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1217 struct ubifs_nbranch *branch = NULL;
1218 struct ubifs_nnode *nnode = NULL;
1219 void *buf = c->lpt_nod_buf;
1220 int err, lnum, offs;
1223 branch = &parent->nbranch[iip];
1224 lnum = branch->lnum;
1225 offs = branch->offs;
1230 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1237 * This nnode was not written which just means that the LEB
1238 * properties in the subtree below it describe empty LEBs. We
1239 * make the nnode as though we had read it, which in fact means
1240 * doing almost nothing.
1243 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1245 err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1248 err = ubifs_unpack_nnode(c, buf, nnode);
1252 err = validate_nnode(c, nnode, parent, iip);
1256 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1258 branch->nnode = nnode;
1259 nnode->level = parent->level - 1;
1262 nnode->level = c->lpt_hght;
1264 nnode->parent = parent;
1269 ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs);
1276 * read_pnode - read a pnode from flash and link it to the tree in memory.
1277 * @c: UBIFS file-system description object
1278 * @parent: parent nnode
1279 * @iip: index in parent
1281 * This function returns %0 on success and a negative error code on failure.
1283 static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1285 struct ubifs_nbranch *branch;
1286 struct ubifs_pnode *pnode = NULL;
1287 void *buf = c->lpt_nod_buf;
1288 int err, lnum, offs;
1290 branch = &parent->nbranch[iip];
1291 lnum = branch->lnum;
1292 offs = branch->offs;
1293 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1299 * This pnode was not written which just means that the LEB
1300 * properties in it describe empty LEBs. We make the pnode as
1301 * though we had read it.
1306 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1307 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1308 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1310 lprops->free = c->leb_size;
1311 lprops->flags = ubifs_categorize_lprops(c, lprops);
1314 err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1317 err = unpack_pnode(c, buf, pnode);
1321 err = validate_pnode(c, pnode, parent, iip);
1325 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1326 branch->pnode = pnode;
1327 pnode->parent = parent;
1329 set_pnode_lnum(c, pnode);
1330 c->pnodes_have += 1;
1334 ubifs_err(c, "error %d reading pnode at %d:%d", err, lnum, offs);
1335 ubifs_dump_pnode(c, pnode, parent, iip);
1337 ubifs_err(c, "calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1343 * read_ltab - read LPT's own lprops table.
1344 * @c: UBIFS file-system description object
1346 * This function returns %0 on success and a negative error code on failure.
1348 static int read_ltab(struct ubifs_info *c)
1353 buf = vmalloc(c->ltab_sz);
1356 err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1359 err = unpack_ltab(c, buf);
1366 * read_lsave - read LPT's save table.
1367 * @c: UBIFS file-system description object
1369 * This function returns %0 on success and a negative error code on failure.
1371 static int read_lsave(struct ubifs_info *c)
1376 buf = vmalloc(c->lsave_sz);
1379 err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1383 err = unpack_lsave(c, buf);
1386 for (i = 0; i < c->lsave_cnt; i++) {
1387 int lnum = c->lsave[i];
1388 struct ubifs_lprops *lprops;
1391 * Due to automatic resizing, the values in the lsave table
1392 * could be beyond the volume size - just ignore them.
1394 if (lnum >= c->leb_cnt)
1396 lprops = ubifs_lpt_lookup(c, lnum);
1397 if (IS_ERR(lprops)) {
1398 err = PTR_ERR(lprops);
1408 * ubifs_get_nnode - get a nnode.
1409 * @c: UBIFS file-system description object
1410 * @parent: parent nnode (or NULL for the root)
1411 * @iip: index in parent
1413 * This function returns a pointer to the nnode on success or a negative error
1416 struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1417 struct ubifs_nnode *parent, int iip)
1419 struct ubifs_nbranch *branch;
1420 struct ubifs_nnode *nnode;
1423 branch = &parent->nbranch[iip];
1424 nnode = branch->nnode;
1427 err = ubifs_read_nnode(c, parent, iip);
1429 return ERR_PTR(err);
1430 return branch->nnode;
1434 * ubifs_get_pnode - get a pnode.
1435 * @c: UBIFS file-system description object
1436 * @parent: parent nnode
1437 * @iip: index in parent
1439 * This function returns a pointer to the pnode on success or a negative error
1442 struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1443 struct ubifs_nnode *parent, int iip)
1445 struct ubifs_nbranch *branch;
1446 struct ubifs_pnode *pnode;
1449 branch = &parent->nbranch[iip];
1450 pnode = branch->pnode;
1453 err = read_pnode(c, parent, iip);
1455 return ERR_PTR(err);
1456 update_cats(c, branch->pnode);
1457 return branch->pnode;
1461 * ubifs_pnode_lookup - lookup a pnode in the LPT.
1462 * @c: UBIFS file-system description object
1463 * @i: pnode number (0 to (main_lebs - 1) / UBIFS_LPT_FANOUT)
1465 * This function returns a pointer to the pnode on success or a negative
1466 * error code on failure.
1468 struct ubifs_pnode *ubifs_pnode_lookup(struct ubifs_info *c, int i)
1470 int err, h, iip, shft;
1471 struct ubifs_nnode *nnode;
1474 err = ubifs_read_nnode(c, NULL, 0);
1476 return ERR_PTR(err);
1478 i <<= UBIFS_LPT_FANOUT_SHIFT;
1480 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1481 for (h = 1; h < c->lpt_hght; h++) {
1482 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1483 shft -= UBIFS_LPT_FANOUT_SHIFT;
1484 nnode = ubifs_get_nnode(c, nnode, iip);
1486 return ERR_CAST(nnode);
1488 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1489 return ubifs_get_pnode(c, nnode, iip);
1493 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1494 * @c: UBIFS file-system description object
1495 * @lnum: LEB number to lookup
1497 * This function returns a pointer to the LEB properties on success or a
1498 * negative error code on failure.
1500 struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1503 struct ubifs_pnode *pnode;
1505 i = lnum - c->main_first;
1506 pnode = ubifs_pnode_lookup(c, i >> UBIFS_LPT_FANOUT_SHIFT);
1508 return ERR_CAST(pnode);
1509 iip = (i & (UBIFS_LPT_FANOUT - 1));
1510 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1511 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1512 pnode->lprops[iip].flags);
1513 return &pnode->lprops[iip];
1517 * dirty_cow_nnode - ensure a nnode is not being committed.
1518 * @c: UBIFS file-system description object
1519 * @nnode: nnode to check
1521 * Returns dirtied nnode on success or negative error code on failure.
1523 static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1524 struct ubifs_nnode *nnode)
1526 struct ubifs_nnode *n;
1529 if (!test_bit(COW_CNODE, &nnode->flags)) {
1530 /* nnode is not being committed */
1531 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1532 c->dirty_nn_cnt += 1;
1533 ubifs_add_nnode_dirt(c, nnode);
1538 /* nnode is being committed, so copy it */
1539 n = kmemdup(nnode, sizeof(struct ubifs_nnode), GFP_NOFS);
1541 return ERR_PTR(-ENOMEM);
1544 __set_bit(DIRTY_CNODE, &n->flags);
1545 __clear_bit(COW_CNODE, &n->flags);
1547 /* The children now have new parent */
1548 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1549 struct ubifs_nbranch *branch = &n->nbranch[i];
1552 branch->cnode->parent = n;
1555 ubifs_assert(c, !test_bit(OBSOLETE_CNODE, &nnode->flags));
1556 __set_bit(OBSOLETE_CNODE, &nnode->flags);
1558 c->dirty_nn_cnt += 1;
1559 ubifs_add_nnode_dirt(c, nnode);
1561 nnode->parent->nbranch[n->iip].nnode = n;
1568 * dirty_cow_pnode - ensure a pnode is not being committed.
1569 * @c: UBIFS file-system description object
1570 * @pnode: pnode to check
1572 * Returns dirtied pnode on success or negative error code on failure.
1574 static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1575 struct ubifs_pnode *pnode)
1577 struct ubifs_pnode *p;
1579 if (!test_bit(COW_CNODE, &pnode->flags)) {
1580 /* pnode is not being committed */
1581 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1582 c->dirty_pn_cnt += 1;
1583 add_pnode_dirt(c, pnode);
1588 /* pnode is being committed, so copy it */
1589 p = kmemdup(pnode, sizeof(struct ubifs_pnode), GFP_NOFS);
1591 return ERR_PTR(-ENOMEM);
1594 __set_bit(DIRTY_CNODE, &p->flags);
1595 __clear_bit(COW_CNODE, &p->flags);
1596 replace_cats(c, pnode, p);
1598 ubifs_assert(c, !test_bit(OBSOLETE_CNODE, &pnode->flags));
1599 __set_bit(OBSOLETE_CNODE, &pnode->flags);
1601 c->dirty_pn_cnt += 1;
1602 add_pnode_dirt(c, pnode);
1603 pnode->parent->nbranch[p->iip].pnode = p;
1608 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1609 * @c: UBIFS file-system description object
1610 * @lnum: LEB number to lookup
1612 * This function returns a pointer to the LEB properties on success or a
1613 * negative error code on failure.
1615 struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1617 int err, i, h, iip, shft;
1618 struct ubifs_nnode *nnode;
1619 struct ubifs_pnode *pnode;
1622 err = ubifs_read_nnode(c, NULL, 0);
1624 return ERR_PTR(err);
1627 nnode = dirty_cow_nnode(c, nnode);
1629 return ERR_CAST(nnode);
1630 i = lnum - c->main_first;
1631 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1632 for (h = 1; h < c->lpt_hght; h++) {
1633 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1634 shft -= UBIFS_LPT_FANOUT_SHIFT;
1635 nnode = ubifs_get_nnode(c, nnode, iip);
1637 return ERR_CAST(nnode);
1638 nnode = dirty_cow_nnode(c, nnode);
1640 return ERR_CAST(nnode);
1642 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1643 pnode = ubifs_get_pnode(c, nnode, iip);
1645 return ERR_CAST(pnode);
1646 pnode = dirty_cow_pnode(c, pnode);
1648 return ERR_CAST(pnode);
1649 iip = (i & (UBIFS_LPT_FANOUT - 1));
1650 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1651 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1652 pnode->lprops[iip].flags);
1653 ubifs_assert(c, test_bit(DIRTY_CNODE, &pnode->flags));
1654 return &pnode->lprops[iip];
1658 * ubifs_lpt_calc_hash - Calculate hash of the LPT pnodes
1659 * @c: UBIFS file-system description object
1660 * @hash: the returned hash of the LPT pnodes
1662 * This function iterates over the LPT pnodes and creates a hash over them.
1663 * Returns 0 for success or a negative error code otherwise.
1665 int ubifs_lpt_calc_hash(struct ubifs_info *c, u8 *hash)
1667 struct ubifs_nnode *nnode, *nn;
1668 struct ubifs_cnode *cnode;
1669 struct shash_desc *desc;
1671 int bufsiz = max_t(int, c->nnode_sz, c->pnode_sz);
1675 if (!ubifs_authenticated(c))
1679 err = ubifs_read_nnode(c, NULL, 0);
1684 desc = ubifs_hash_get_desc(c);
1686 return PTR_ERR(desc);
1688 buf = kmalloc(bufsiz, GFP_NOFS);
1694 cnode = (struct ubifs_cnode *)c->nroot;
1697 nnode = cnode->parent;
1698 nn = (struct ubifs_nnode *)cnode;
1699 if (cnode->level > 1) {
1700 while (iip < UBIFS_LPT_FANOUT) {
1701 if (nn->nbranch[iip].lnum == 0) {
1707 nnode = ubifs_get_nnode(c, nn, iip);
1708 if (IS_ERR(nnode)) {
1709 err = PTR_ERR(nnode);
1715 cnode = (struct ubifs_cnode *)nnode;
1718 if (iip < UBIFS_LPT_FANOUT)
1721 struct ubifs_pnode *pnode;
1723 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1724 if (nn->nbranch[i].lnum == 0)
1726 pnode = ubifs_get_pnode(c, nn, i);
1727 if (IS_ERR(pnode)) {
1728 err = PTR_ERR(pnode);
1732 ubifs_pack_pnode(c, buf, pnode);
1733 err = ubifs_shash_update(c, desc, buf,
1739 /* Go up and to the right */
1740 iip = cnode->iip + 1;
1741 cnode = (struct ubifs_cnode *)nnode;
1744 err = ubifs_shash_final(c, desc, hash);
1753 * lpt_check_hash - check the hash of the LPT.
1754 * @c: UBIFS file-system description object
1756 * This function calculates a hash over all pnodes in the LPT and compares it with
1757 * the hash stored in the master node. Returns %0 on success and a negative error
1760 static int lpt_check_hash(struct ubifs_info *c)
1763 u8 hash[UBIFS_HASH_ARR_SZ];
1765 if (!ubifs_authenticated(c))
1768 err = ubifs_lpt_calc_hash(c, hash);
1772 if (ubifs_check_hash(c, c->mst_node->hash_lpt, hash)) {
1774 ubifs_err(c, "Failed to authenticate LPT");
1783 * lpt_init_rd - initialize the LPT for reading.
1784 * @c: UBIFS file-system description object
1786 * This function returns %0 on success and a negative error code on failure.
1788 static int lpt_init_rd(struct ubifs_info *c)
1792 c->ltab = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
1797 i = max_t(int, c->nnode_sz, c->pnode_sz);
1798 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1799 if (!c->lpt_nod_buf)
1802 for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1803 c->lpt_heap[i].arr = kmalloc_array(LPT_HEAP_SZ,
1806 if (!c->lpt_heap[i].arr)
1808 c->lpt_heap[i].cnt = 0;
1809 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1812 c->dirty_idx.arr = kmalloc_array(LPT_HEAP_SZ, sizeof(void *),
1814 if (!c->dirty_idx.arr)
1816 c->dirty_idx.cnt = 0;
1817 c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1823 err = lpt_check_hash(c);
1827 dbg_lp("space_bits %d", c->space_bits);
1828 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1829 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1830 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1831 dbg_lp("pcnt_bits %d", c->pcnt_bits);
1832 dbg_lp("lnum_bits %d", c->lnum_bits);
1833 dbg_lp("pnode_sz %d", c->pnode_sz);
1834 dbg_lp("nnode_sz %d", c->nnode_sz);
1835 dbg_lp("ltab_sz %d", c->ltab_sz);
1836 dbg_lp("lsave_sz %d", c->lsave_sz);
1837 dbg_lp("lsave_cnt %d", c->lsave_cnt);
1838 dbg_lp("lpt_hght %d", c->lpt_hght);
1839 dbg_lp("big_lpt %d", c->big_lpt);
1840 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1841 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1842 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1844 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1850 * lpt_init_wr - initialize the LPT for writing.
1851 * @c: UBIFS file-system description object
1853 * 'lpt_init_rd()' must have been called already.
1855 * This function returns %0 on success and a negative error code on failure.
1857 static int lpt_init_wr(struct ubifs_info *c)
1861 c->ltab_cmt = vmalloc(array_size(sizeof(struct ubifs_lpt_lprops),
1866 c->lpt_buf = vmalloc(c->leb_size);
1871 c->lsave = kmalloc_array(c->lsave_cnt, sizeof(int), GFP_NOFS);
1874 err = read_lsave(c);
1879 for (i = 0; i < c->lpt_lebs; i++)
1880 if (c->ltab[i].free == c->leb_size) {
1881 err = ubifs_leb_unmap(c, i + c->lpt_first);
1890 * ubifs_lpt_init - initialize the LPT.
1891 * @c: UBIFS file-system description object
1892 * @rd: whether to initialize lpt for reading
1893 * @wr: whether to initialize lpt for writing
1895 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1896 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1899 * This function returns %0 on success and a negative error code on failure.
1901 int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1906 err = lpt_init_rd(c);
1912 err = lpt_init_wr(c);
1921 ubifs_lpt_free(c, 1);
1923 ubifs_lpt_free(c, 0);
1928 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1929 * @nnode: where to keep a nnode
1930 * @pnode: where to keep a pnode
1931 * @cnode: where to keep a cnode
1932 * @in_tree: is the node in the tree in memory
1933 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1935 * @ptr.pnode: ditto for pnode
1936 * @ptr.cnode: ditto for cnode
1938 struct lpt_scan_node {
1940 struct ubifs_nnode nnode;
1941 struct ubifs_pnode pnode;
1942 struct ubifs_cnode cnode;
1946 struct ubifs_nnode *nnode;
1947 struct ubifs_pnode *pnode;
1948 struct ubifs_cnode *cnode;
1953 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1954 * @c: the UBIFS file-system description object
1955 * @path: where to put the nnode
1956 * @parent: parent of the nnode
1957 * @iip: index in parent of the nnode
1959 * This function returns a pointer to the nnode on success or a negative error
1962 static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1963 struct lpt_scan_node *path,
1964 struct ubifs_nnode *parent, int iip)
1966 struct ubifs_nbranch *branch;
1967 struct ubifs_nnode *nnode;
1968 void *buf = c->lpt_nod_buf;
1971 branch = &parent->nbranch[iip];
1972 nnode = branch->nnode;
1975 path->ptr.nnode = nnode;
1978 nnode = &path->nnode;
1980 path->ptr.nnode = nnode;
1981 memset(nnode, 0, sizeof(struct ubifs_nnode));
1982 if (branch->lnum == 0) {
1984 * This nnode was not written which just means that the LEB
1985 * properties in the subtree below it describe empty LEBs. We
1986 * make the nnode as though we had read it, which in fact means
1987 * doing almost nothing.
1990 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1992 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1995 return ERR_PTR(err);
1996 err = ubifs_unpack_nnode(c, buf, nnode);
1998 return ERR_PTR(err);
2000 err = validate_nnode(c, nnode, parent, iip);
2002 return ERR_PTR(err);
2004 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
2005 nnode->level = parent->level - 1;
2006 nnode->parent = parent;
2012 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
2013 * @c: the UBIFS file-system description object
2014 * @path: where to put the pnode
2015 * @parent: parent of the pnode
2016 * @iip: index in parent of the pnode
2018 * This function returns a pointer to the pnode on success or a negative error
2021 static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
2022 struct lpt_scan_node *path,
2023 struct ubifs_nnode *parent, int iip)
2025 struct ubifs_nbranch *branch;
2026 struct ubifs_pnode *pnode;
2027 void *buf = c->lpt_nod_buf;
2030 branch = &parent->nbranch[iip];
2031 pnode = branch->pnode;
2034 path->ptr.pnode = pnode;
2037 pnode = &path->pnode;
2039 path->ptr.pnode = pnode;
2040 memset(pnode, 0, sizeof(struct ubifs_pnode));
2041 if (branch->lnum == 0) {
2043 * This pnode was not written which just means that the LEB
2044 * properties in it describe empty LEBs. We make the pnode as
2045 * though we had read it.
2050 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
2051 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2052 struct ubifs_lprops * const lprops = &pnode->lprops[i];
2054 lprops->free = c->leb_size;
2055 lprops->flags = ubifs_categorize_lprops(c, lprops);
2058 ubifs_assert(c, branch->lnum >= c->lpt_first &&
2059 branch->lnum <= c->lpt_last);
2060 ubifs_assert(c, branch->offs >= 0 && branch->offs < c->leb_size);
2061 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
2064 return ERR_PTR(err);
2065 err = unpack_pnode(c, buf, pnode);
2067 return ERR_PTR(err);
2069 err = validate_pnode(c, pnode, parent, iip);
2071 return ERR_PTR(err);
2073 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
2074 pnode->parent = parent;
2076 set_pnode_lnum(c, pnode);
2081 * ubifs_lpt_scan_nolock - scan the LPT.
2082 * @c: the UBIFS file-system description object
2083 * @start_lnum: LEB number from which to start scanning
2084 * @end_lnum: LEB number at which to stop scanning
2085 * @scan_cb: callback function called for each lprops
2086 * @data: data to be passed to the callback function
2088 * This function returns %0 on success and a negative error code on failure.
2090 int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
2091 ubifs_lpt_scan_callback scan_cb, void *data)
2093 int err = 0, i, h, iip, shft;
2094 struct ubifs_nnode *nnode;
2095 struct ubifs_pnode *pnode;
2096 struct lpt_scan_node *path;
2098 if (start_lnum == -1) {
2099 start_lnum = end_lnum + 1;
2100 if (start_lnum >= c->leb_cnt)
2101 start_lnum = c->main_first;
2104 ubifs_assert(c, start_lnum >= c->main_first && start_lnum < c->leb_cnt);
2105 ubifs_assert(c, end_lnum >= c->main_first && end_lnum < c->leb_cnt);
2108 err = ubifs_read_nnode(c, NULL, 0);
2113 path = kmalloc_array(c->lpt_hght + 1, sizeof(struct lpt_scan_node),
2118 path[0].ptr.nnode = c->nroot;
2119 path[0].in_tree = 1;
2121 /* Descend to the pnode containing start_lnum */
2123 i = start_lnum - c->main_first;
2124 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
2125 for (h = 1; h < c->lpt_hght; h++) {
2126 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
2127 shft -= UBIFS_LPT_FANOUT_SHIFT;
2128 nnode = scan_get_nnode(c, path + h, nnode, iip);
2129 if (IS_ERR(nnode)) {
2130 err = PTR_ERR(nnode);
2134 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
2135 pnode = scan_get_pnode(c, path + h, nnode, iip);
2136 if (IS_ERR(pnode)) {
2137 err = PTR_ERR(pnode);
2140 iip = (i & (UBIFS_LPT_FANOUT - 1));
2142 /* Loop for each lprops */
2144 struct ubifs_lprops *lprops = &pnode->lprops[iip];
2145 int ret, lnum = lprops->lnum;
2147 ret = scan_cb(c, lprops, path[h].in_tree, data);
2152 if (ret & LPT_SCAN_ADD) {
2153 /* Add all the nodes in path to the tree in memory */
2154 for (h = 1; h < c->lpt_hght; h++) {
2155 const size_t sz = sizeof(struct ubifs_nnode);
2156 struct ubifs_nnode *parent;
2158 if (path[h].in_tree)
2160 nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
2165 parent = nnode->parent;
2166 parent->nbranch[nnode->iip].nnode = nnode;
2167 path[h].ptr.nnode = nnode;
2168 path[h].in_tree = 1;
2169 path[h + 1].cnode.parent = nnode;
2171 if (path[h].in_tree)
2172 ubifs_ensure_cat(c, lprops);
2174 const size_t sz = sizeof(struct ubifs_pnode);
2175 struct ubifs_nnode *parent;
2177 pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2182 parent = pnode->parent;
2183 parent->nbranch[pnode->iip].pnode = pnode;
2184 path[h].ptr.pnode = pnode;
2185 path[h].in_tree = 1;
2186 update_cats(c, pnode);
2187 c->pnodes_have += 1;
2189 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2193 err = dbg_check_cats(c);
2197 if (ret & LPT_SCAN_STOP) {
2201 /* Get the next lprops */
2202 if (lnum == end_lnum) {
2204 * We got to the end without finding what we were
2210 if (lnum + 1 >= c->leb_cnt) {
2211 /* Wrap-around to the beginning */
2212 start_lnum = c->main_first;
2215 if (iip + 1 < UBIFS_LPT_FANOUT) {
2216 /* Next lprops is in the same pnode */
2220 /* We need to get the next pnode. Go up until we can go right */
2224 ubifs_assert(c, h >= 0);
2225 nnode = path[h].ptr.nnode;
2226 if (iip + 1 < UBIFS_LPT_FANOUT)
2232 /* Descend to the pnode */
2234 for (; h < c->lpt_hght; h++) {
2235 nnode = scan_get_nnode(c, path + h, nnode, iip);
2236 if (IS_ERR(nnode)) {
2237 err = PTR_ERR(nnode);
2242 pnode = scan_get_pnode(c, path + h, nnode, iip);
2243 if (IS_ERR(pnode)) {
2244 err = PTR_ERR(pnode);
2255 * dbg_chk_pnode - check a pnode.
2256 * @c: the UBIFS file-system description object
2257 * @pnode: pnode to check
2258 * @col: pnode column
2260 * This function returns %0 on success and a negative error code on failure.
2262 static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2267 if (pnode->num != col) {
2268 ubifs_err(c, "pnode num %d expected %d parent num %d iip %d",
2269 pnode->num, col, pnode->parent->num, pnode->iip);
2272 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2273 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2274 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2276 int found, cat = lprops->flags & LPROPS_CAT_MASK;
2277 struct ubifs_lpt_heap *heap;
2278 struct list_head *list = NULL;
2280 if (lnum >= c->leb_cnt)
2282 if (lprops->lnum != lnum) {
2283 ubifs_err(c, "bad LEB number %d expected %d",
2284 lprops->lnum, lnum);
2287 if (lprops->flags & LPROPS_TAKEN) {
2288 if (cat != LPROPS_UNCAT) {
2289 ubifs_err(c, "LEB %d taken but not uncat %d",
2295 if (lprops->flags & LPROPS_INDEX) {
2298 case LPROPS_DIRTY_IDX:
2299 case LPROPS_FRDI_IDX:
2302 ubifs_err(c, "LEB %d index but cat %d",
2312 case LPROPS_FREEABLE:
2315 ubifs_err(c, "LEB %d not index but cat %d",
2322 list = &c->uncat_list;
2325 list = &c->empty_list;
2327 case LPROPS_FREEABLE:
2328 list = &c->freeable_list;
2330 case LPROPS_FRDI_IDX:
2331 list = &c->frdi_idx_list;
2337 case LPROPS_DIRTY_IDX:
2339 heap = &c->lpt_heap[cat - 1];
2340 if (lprops->hpos < heap->cnt &&
2341 heap->arr[lprops->hpos] == lprops)
2346 case LPROPS_FREEABLE:
2347 case LPROPS_FRDI_IDX:
2348 list_for_each_entry(lp, list, list)
2356 ubifs_err(c, "LEB %d cat %d not found in cat heap/list",
2362 if (lprops->free != c->leb_size) {
2363 ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2364 lprops->lnum, cat, lprops->free,
2369 case LPROPS_FREEABLE:
2370 case LPROPS_FRDI_IDX:
2371 if (lprops->free + lprops->dirty != c->leb_size) {
2372 ubifs_err(c, "LEB %d cat %d free %d dirty %d",
2373 lprops->lnum, cat, lprops->free,
2384 * dbg_check_lpt_nodes - check nnodes and pnodes.
2385 * @c: the UBIFS file-system description object
2386 * @cnode: next cnode (nnode or pnode) to check
2387 * @row: row of cnode (root is zero)
2388 * @col: column of cnode (leftmost is zero)
2390 * This function returns %0 on success and a negative error code on failure.
2392 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2395 struct ubifs_nnode *nnode, *nn;
2396 struct ubifs_cnode *cn;
2397 int num, iip = 0, err;
2399 if (!dbg_is_chk_lprops(c))
2403 ubifs_assert(c, row >= 0);
2404 nnode = cnode->parent;
2406 /* cnode is a nnode */
2407 num = calc_nnode_num(row, col);
2408 if (cnode->num != num) {
2409 ubifs_err(c, "nnode num %d expected %d parent num %d iip %d",
2411 (nnode ? nnode->num : 0), cnode->iip);
2414 nn = (struct ubifs_nnode *)cnode;
2415 while (iip < UBIFS_LPT_FANOUT) {
2416 cn = nn->nbranch[iip].cnode;
2420 col <<= UBIFS_LPT_FANOUT_SHIFT;
2429 if (iip < UBIFS_LPT_FANOUT)
2432 struct ubifs_pnode *pnode;
2434 /* cnode is a pnode */
2435 pnode = (struct ubifs_pnode *)cnode;
2436 err = dbg_chk_pnode(c, pnode, col);
2440 /* Go up and to the right */
2442 col >>= UBIFS_LPT_FANOUT_SHIFT;
2443 iip = cnode->iip + 1;
2444 cnode = (struct ubifs_cnode *)nnode;