2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
13 * This file implements the LEB properties tree (LPT) area. The LPT area
14 * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
15 * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
16 * between the log and the orphan area.
18 * The LPT area is like a miniature self-contained file system. It is required
19 * that it never runs out of space, is fast to access and update, and scales
20 * logarithmically. The LEB properties tree is implemented as a wandering tree
21 * much like the TNC, and the LPT area has its own garbage collection.
23 * The LPT has two slightly different forms called the "small model" and the
24 * "big model". The small model is used when the entire LEB properties table
25 * can be written into a single eraseblock. In that case, garbage collection
26 * consists of just writing the whole table, which therefore makes all other
27 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
28 * selected for garbage collection, which consists of marking the clean nodes in
29 * that LEB as dirty, and then only the dirty nodes are written out. Also, in
30 * the case of the big model, a table of LEB numbers is saved so that the entire
31 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
38 #include <linux/crc16.h>
39 #include <linux/math64.h>
40 #include <linux/slab.h>
42 #include <linux/compat.h>
43 #include <linux/err.h>
44 #include <ubi_uboot.h>
49 * do_calc_lpt_geom - calculate sizes for the LPT area.
50 * @c: the UBIFS file-system description object
52 * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
53 * properties of the flash and whether LPT is "big" (c->big_lpt).
55 static void do_calc_lpt_geom(struct ubifs_info *c)
57 int i, n, bits, per_leb_wastage, max_pnode_cnt;
58 long long sz, tot_wastage;
60 n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
61 max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
65 while (n < max_pnode_cnt) {
67 n <<= UBIFS_LPT_FANOUT_SHIFT;
70 c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
72 n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
74 for (i = 1; i < c->lpt_hght; i++) {
75 n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
79 c->space_bits = fls(c->leb_size) - 3;
80 c->lpt_lnum_bits = fls(c->lpt_lebs);
81 c->lpt_offs_bits = fls(c->leb_size - 1);
82 c->lpt_spc_bits = fls(c->leb_size);
84 n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
85 c->pcnt_bits = fls(n - 1);
87 c->lnum_bits = fls(c->max_leb_cnt - 1);
89 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
90 (c->big_lpt ? c->pcnt_bits : 0) +
91 (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
92 c->pnode_sz = (bits + 7) / 8;
94 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
95 (c->big_lpt ? c->pcnt_bits : 0) +
96 (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
97 c->nnode_sz = (bits + 7) / 8;
99 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
100 c->lpt_lebs * c->lpt_spc_bits * 2;
101 c->ltab_sz = (bits + 7) / 8;
103 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
104 c->lnum_bits * c->lsave_cnt;
105 c->lsave_sz = (bits + 7) / 8;
107 /* Calculate the minimum LPT size */
108 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
109 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
110 c->lpt_sz += c->ltab_sz;
112 c->lpt_sz += c->lsave_sz;
116 per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
117 sz += per_leb_wastage;
118 tot_wastage = per_leb_wastage;
119 while (sz > c->leb_size) {
120 sz += per_leb_wastage;
122 tot_wastage += per_leb_wastage;
124 tot_wastage += ALIGN(sz, c->min_io_size) - sz;
125 c->lpt_sz += tot_wastage;
129 * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
130 * @c: the UBIFS file-system description object
132 * This function returns %0 on success and a negative error code on failure.
134 int ubifs_calc_lpt_geom(struct ubifs_info *c)
141 /* Verify that lpt_lebs is big enough */
142 sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
143 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
144 if (lebs_needed > c->lpt_lebs) {
145 ubifs_err("too few LPT LEBs");
149 /* Verify that ltab fits in a single LEB (since ltab is a single node */
150 if (c->ltab_sz > c->leb_size) {
151 ubifs_err("LPT ltab too big");
155 c->check_lpt_free = c->big_lpt;
160 * calc_dflt_lpt_geom - calculate default LPT geometry.
161 * @c: the UBIFS file-system description object
162 * @main_lebs: number of main area LEBs is passed and returned here
163 * @big_lpt: whether the LPT area is "big" is returned here
165 * The size of the LPT area depends on parameters that themselves are dependent
166 * on the size of the LPT area. This function, successively recalculates the LPT
167 * area geometry until the parameters and resultant geometry are consistent.
169 * This function returns %0 on success and a negative error code on failure.
171 static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
177 /* Start by assuming the minimum number of LPT LEBs */
178 c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
179 c->main_lebs = *main_lebs - c->lpt_lebs;
180 if (c->main_lebs <= 0)
183 /* And assume we will use the small LPT model */
187 * Calculate the geometry based on assumptions above and then see if it
192 /* Small LPT model must have lpt_sz < leb_size */
193 if (c->lpt_sz > c->leb_size) {
194 /* Nope, so try again using big LPT model */
199 /* Now check there are enough LPT LEBs */
200 for (i = 0; i < 64 ; i++) {
201 sz = c->lpt_sz * 4; /* Allow 4 times the size */
202 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
203 if (lebs_needed > c->lpt_lebs) {
204 /* Not enough LPT LEBs so try again with more */
205 c->lpt_lebs = lebs_needed;
206 c->main_lebs = *main_lebs - c->lpt_lebs;
207 if (c->main_lebs <= 0)
212 if (c->ltab_sz > c->leb_size) {
213 ubifs_err("LPT ltab too big");
216 *main_lebs = c->main_lebs;
217 *big_lpt = c->big_lpt;
224 * pack_bits - pack bit fields end-to-end.
225 * @addr: address at which to pack (passed and next address returned)
226 * @pos: bit position at which to pack (passed and next position returned)
227 * @val: value to pack
228 * @nrbits: number of bits of value to pack (1-32)
230 static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits)
235 ubifs_assert(nrbits > 0);
236 ubifs_assert(nrbits <= 32);
237 ubifs_assert(*pos >= 0);
238 ubifs_assert(*pos < 8);
239 ubifs_assert((val >> nrbits) == 0 || nrbits == 32);
241 *p |= ((uint8_t)val) << b;
244 *++p = (uint8_t)(val >>= (8 - b));
246 *++p = (uint8_t)(val >>= 8);
248 *++p = (uint8_t)(val >>= 8);
250 *++p = (uint8_t)(val >>= 8);
257 *++p = (uint8_t)(val >>= 8);
259 *++p = (uint8_t)(val >>= 8);
261 *++p = (uint8_t)(val >>= 8);
273 * ubifs_unpack_bits - unpack bit fields.
274 * @addr: address at which to unpack (passed and next address returned)
275 * @pos: bit position at which to unpack (passed and next position returned)
276 * @nrbits: number of bits of value to unpack (1-32)
278 * This functions returns the value unpacked.
280 uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
282 const int k = 32 - nrbits;
285 uint32_t uninitialized_var(val);
286 const int bytes = (nrbits + b + 7) >> 3;
288 ubifs_assert(nrbits > 0);
289 ubifs_assert(nrbits <= 32);
290 ubifs_assert(*pos >= 0);
291 ubifs_assert(*pos < 8);
298 val = p[1] | ((uint32_t)p[2] << 8);
301 val = p[1] | ((uint32_t)p[2] << 8) |
302 ((uint32_t)p[3] << 16);
305 val = p[1] | ((uint32_t)p[2] << 8) |
306 ((uint32_t)p[3] << 16) |
307 ((uint32_t)p[4] << 24);
318 val = p[0] | ((uint32_t)p[1] << 8);
321 val = p[0] | ((uint32_t)p[1] << 8) |
322 ((uint32_t)p[2] << 16);
325 val = p[0] | ((uint32_t)p[1] << 8) |
326 ((uint32_t)p[2] << 16) |
327 ((uint32_t)p[3] << 24);
337 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
342 * ubifs_pack_pnode - pack all the bit fields of a pnode.
343 * @c: UBIFS file-system description object
344 * @buf: buffer into which to pack
345 * @pnode: pnode to pack
347 void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
348 struct ubifs_pnode *pnode)
350 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
354 pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS);
356 pack_bits(&addr, &pos, pnode->num, c->pcnt_bits);
357 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
358 pack_bits(&addr, &pos, pnode->lprops[i].free >> 3,
360 pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3,
362 if (pnode->lprops[i].flags & LPROPS_INDEX)
363 pack_bits(&addr, &pos, 1, 1);
365 pack_bits(&addr, &pos, 0, 1);
367 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
368 c->pnode_sz - UBIFS_LPT_CRC_BYTES);
371 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
375 * ubifs_pack_nnode - pack all the bit fields of a nnode.
376 * @c: UBIFS file-system description object
377 * @buf: buffer into which to pack
378 * @nnode: nnode to pack
380 void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
381 struct ubifs_nnode *nnode)
383 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
387 pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS);
389 pack_bits(&addr, &pos, nnode->num, c->pcnt_bits);
390 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
391 int lnum = nnode->nbranch[i].lnum;
394 lnum = c->lpt_last + 1;
395 pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits);
396 pack_bits(&addr, &pos, nnode->nbranch[i].offs,
399 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
400 c->nnode_sz - UBIFS_LPT_CRC_BYTES);
403 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
407 * ubifs_pack_ltab - pack the LPT's own lprops table.
408 * @c: UBIFS file-system description object
409 * @buf: buffer into which to pack
410 * @ltab: LPT's own lprops table to pack
412 void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
413 struct ubifs_lpt_lprops *ltab)
415 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
419 pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS);
420 for (i = 0; i < c->lpt_lebs; i++) {
421 pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits);
422 pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits);
424 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
425 c->ltab_sz - UBIFS_LPT_CRC_BYTES);
428 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
432 * ubifs_pack_lsave - pack the LPT's save table.
433 * @c: UBIFS file-system description object
434 * @buf: buffer into which to pack
435 * @lsave: LPT's save table to pack
437 void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave)
439 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
443 pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS);
444 for (i = 0; i < c->lsave_cnt; i++)
445 pack_bits(&addr, &pos, lsave[i], c->lnum_bits);
446 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
447 c->lsave_sz - UBIFS_LPT_CRC_BYTES);
450 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
454 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
455 * @c: UBIFS file-system description object
456 * @lnum: LEB number to which to add dirty space
457 * @dirty: amount of dirty space to add
459 void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
463 dbg_lp("LEB %d add %d to %d",
464 lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
465 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
466 c->ltab[lnum - c->lpt_first].dirty += dirty;
470 * set_ltab - set LPT LEB properties.
471 * @c: UBIFS file-system description object
473 * @free: amount of free space
474 * @dirty: amount of dirty space
476 static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
478 dbg_lp("LEB %d free %d dirty %d to %d %d",
479 lnum, c->ltab[lnum - c->lpt_first].free,
480 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
481 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
482 c->ltab[lnum - c->lpt_first].free = free;
483 c->ltab[lnum - c->lpt_first].dirty = dirty;
487 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
488 * @c: UBIFS file-system description object
489 * @nnode: nnode for which to add dirt
491 void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
493 struct ubifs_nnode *np = nnode->parent;
496 ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
499 ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
500 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
501 c->lpt_drty_flgs |= LTAB_DIRTY;
502 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
508 * add_pnode_dirt - add dirty space to LPT LEB properties.
509 * @c: UBIFS file-system description object
510 * @pnode: pnode for which to add dirt
512 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
514 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
519 * calc_nnode_num - calculate nnode number.
520 * @row: the row in the tree (root is zero)
521 * @col: the column in the row (leftmost is zero)
523 * The nnode number is a number that uniquely identifies a nnode and can be used
524 * easily to traverse the tree from the root to that nnode.
526 * This function calculates and returns the nnode number for the nnode at @row
529 static int calc_nnode_num(int row, int col)
535 bits = (col & (UBIFS_LPT_FANOUT - 1));
536 col >>= UBIFS_LPT_FANOUT_SHIFT;
537 num <<= UBIFS_LPT_FANOUT_SHIFT;
544 * calc_nnode_num_from_parent - calculate nnode number.
545 * @c: UBIFS file-system description object
546 * @parent: parent nnode
547 * @iip: index in parent
549 * The nnode number is a number that uniquely identifies a nnode and can be used
550 * easily to traverse the tree from the root to that nnode.
552 * This function calculates and returns the nnode number based on the parent's
553 * nnode number and the index in parent.
555 static int calc_nnode_num_from_parent(const struct ubifs_info *c,
556 struct ubifs_nnode *parent, int iip)
562 shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
563 num = parent->num ^ (1 << shft);
564 num |= (UBIFS_LPT_FANOUT + iip) << shft;
569 * calc_pnode_num_from_parent - calculate pnode number.
570 * @c: UBIFS file-system description object
571 * @parent: parent nnode
572 * @iip: index in parent
574 * The pnode number is a number that uniquely identifies a pnode and can be used
575 * easily to traverse the tree from the root to that pnode.
577 * This function calculates and returns the pnode number based on the parent's
578 * nnode number and the index in parent.
580 static int calc_pnode_num_from_parent(const struct ubifs_info *c,
581 struct ubifs_nnode *parent, int iip)
583 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
585 for (i = 0; i < n; i++) {
586 num <<= UBIFS_LPT_FANOUT_SHIFT;
587 num |= pnum & (UBIFS_LPT_FANOUT - 1);
588 pnum >>= UBIFS_LPT_FANOUT_SHIFT;
590 num <<= UBIFS_LPT_FANOUT_SHIFT;
596 * ubifs_create_dflt_lpt - create default LPT.
597 * @c: UBIFS file-system description object
598 * @main_lebs: number of main area LEBs is passed and returned here
599 * @lpt_first: LEB number of first LPT LEB
600 * @lpt_lebs: number of LEBs for LPT is passed and returned here
601 * @big_lpt: use big LPT model is passed and returned here
603 * This function returns %0 on success and a negative error code on failure.
605 int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
606 int *lpt_lebs, int *big_lpt)
608 int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
609 int blnum, boffs, bsz, bcnt;
610 struct ubifs_pnode *pnode = NULL;
611 struct ubifs_nnode *nnode = NULL;
612 void *buf = NULL, *p;
613 struct ubifs_lpt_lprops *ltab = NULL;
616 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
619 *lpt_lebs = c->lpt_lebs;
621 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
622 c->lpt_first = lpt_first;
623 /* Needed by 'set_ltab()' */
624 c->lpt_last = lpt_first + c->lpt_lebs - 1;
625 /* Needed by 'ubifs_pack_lsave()' */
626 c->main_first = c->leb_cnt - *main_lebs;
628 lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL);
629 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
630 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
631 buf = vmalloc(c->leb_size);
632 ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
633 if (!pnode || !nnode || !buf || !ltab || !lsave) {
638 ubifs_assert(!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);
678 /* Reset pnode values for remaining pnodes */
679 pnode->lprops[0].free = c->leb_size;
680 pnode->lprops[0].dirty = 0;
681 pnode->lprops[0].flags = 0;
683 pnode->lprops[1].free = c->leb_size;
684 pnode->lprops[1].dirty = 0;
687 * To calculate the internal node branches, we keep information about
690 blnum = lnum; /* LEB number of level below */
691 boffs = 0; /* Offset of level below */
692 bcnt = cnt; /* Number of nodes in level below */
693 bsz = c->pnode_sz; /* Size of nodes in level below */
695 /* Add all remaining pnodes */
696 for (i = 1; i < cnt; i++) {
697 if (len + c->pnode_sz > c->leb_size) {
698 alen = ALIGN(len, c->min_io_size);
699 set_ltab(c, lnum, c->leb_size - alen, alen - len);
700 memset(p, 0xff, alen - len);
701 err = ubifs_leb_change(c, lnum++, buf, alen);
707 ubifs_pack_pnode(c, p, pnode);
711 * pnodes are simply numbered left to right starting at zero,
712 * which means the pnode number can be used easily to traverse
713 * down the tree to the corresponding pnode.
719 for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
721 /* Add all nnodes, one level at a time */
723 /* Number of internal nodes (nnodes) at next level */
724 cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
725 for (i = 0; i < cnt; i++) {
726 if (len + c->nnode_sz > c->leb_size) {
727 alen = ALIGN(len, c->min_io_size);
728 set_ltab(c, lnum, c->leb_size - alen,
730 memset(p, 0xff, alen - len);
731 err = ubifs_leb_change(c, lnum++, buf, alen);
737 /* Only 1 nnode at this level, so it is the root */
742 /* Set branches to the level below */
743 for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
745 if (boffs + bsz > c->leb_size) {
749 nnode->nbranch[j].lnum = blnum;
750 nnode->nbranch[j].offs = boffs;
754 nnode->nbranch[j].lnum = 0;
755 nnode->nbranch[j].offs = 0;
758 nnode->num = calc_nnode_num(row, i);
759 ubifs_pack_nnode(c, p, nnode);
763 /* Only 1 nnode at this level, so it is the root */
766 /* Update the information about the level below */
773 /* Need to add LPT's save table */
774 if (len + c->lsave_sz > c->leb_size) {
775 alen = ALIGN(len, c->min_io_size);
776 set_ltab(c, lnum, c->leb_size - alen, alen - len);
777 memset(p, 0xff, alen - len);
778 err = ubifs_leb_change(c, lnum++, buf, alen);
785 c->lsave_lnum = lnum;
788 for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
789 lsave[i] = c->main_first + i;
790 for (; i < c->lsave_cnt; i++)
791 lsave[i] = c->main_first;
793 ubifs_pack_lsave(c, p, lsave);
798 /* Need to add LPT's own LEB properties table */
799 if (len + c->ltab_sz > c->leb_size) {
800 alen = ALIGN(len, c->min_io_size);
801 set_ltab(c, lnum, c->leb_size - alen, alen - len);
802 memset(p, 0xff, alen - len);
803 err = ubifs_leb_change(c, lnum++, buf, alen);
813 /* Update ltab before packing it */
815 alen = ALIGN(len, c->min_io_size);
816 set_ltab(c, lnum, c->leb_size - alen, alen - len);
818 ubifs_pack_ltab(c, p, ltab);
821 /* Write remaining buffer */
822 memset(p, 0xff, alen - len);
823 err = ubifs_leb_change(c, lnum, buf, alen);
827 c->nhead_lnum = lnum;
828 c->nhead_offs = ALIGN(len, c->min_io_size);
830 dbg_lp("space_bits %d", c->space_bits);
831 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
832 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
833 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
834 dbg_lp("pcnt_bits %d", c->pcnt_bits);
835 dbg_lp("lnum_bits %d", c->lnum_bits);
836 dbg_lp("pnode_sz %d", c->pnode_sz);
837 dbg_lp("nnode_sz %d", c->nnode_sz);
838 dbg_lp("ltab_sz %d", c->ltab_sz);
839 dbg_lp("lsave_sz %d", c->lsave_sz);
840 dbg_lp("lsave_cnt %d", c->lsave_cnt);
841 dbg_lp("lpt_hght %d", c->lpt_hght);
842 dbg_lp("big_lpt %d", c->big_lpt);
843 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
844 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
845 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
847 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
859 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
860 * @c: UBIFS file-system description object
863 * When a pnode is loaded into memory, the LEB properties it contains are added,
864 * by this function, to the LEB category lists and heaps.
866 static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
870 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
871 int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
872 int lnum = pnode->lprops[i].lnum;
876 ubifs_add_to_cat(c, &pnode->lprops[i], cat);
881 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
882 * @c: UBIFS file-system description object
883 * @old_pnode: pnode copied
884 * @new_pnode: pnode copy
886 * During commit it is sometimes necessary to copy a pnode
887 * (see dirty_cow_pnode). When that happens, references in
888 * category lists and heaps must be replaced. This function does that.
890 static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
891 struct ubifs_pnode *new_pnode)
895 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
896 if (!new_pnode->lprops[i].lnum)
898 ubifs_replace_cat(c, &old_pnode->lprops[i],
899 &new_pnode->lprops[i]);
904 * check_lpt_crc - check LPT node crc is correct.
905 * @c: UBIFS file-system description object
906 * @buf: buffer containing node
907 * @len: length of node
909 * This function returns %0 on success and a negative error code on failure.
911 static int check_lpt_crc(void *buf, int len)
915 uint16_t crc, calc_crc;
917 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
918 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
919 len - UBIFS_LPT_CRC_BYTES);
920 if (crc != calc_crc) {
921 ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
930 * check_lpt_type - check LPT node type is correct.
931 * @c: UBIFS file-system description object
932 * @addr: address of type bit field is passed and returned updated here
933 * @pos: position of type bit field is passed and returned updated here
934 * @type: expected type
936 * This function returns %0 on success and a negative error code on failure.
938 static int check_lpt_type(uint8_t **addr, int *pos, int type)
942 node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
943 if (node_type != type) {
944 ubifs_err("invalid type (%d) in LPT node type %d", node_type,
953 * unpack_pnode - unpack a pnode.
954 * @c: UBIFS file-system description object
955 * @buf: buffer containing packed pnode to unpack
956 * @pnode: pnode structure to fill
958 * This function returns %0 on success and a negative error code on failure.
960 static int unpack_pnode(const struct ubifs_info *c, void *buf,
961 struct ubifs_pnode *pnode)
963 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
966 err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
970 pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
971 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
972 struct ubifs_lprops * const lprops = &pnode->lprops[i];
974 lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
976 lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
979 if (ubifs_unpack_bits(&addr, &pos, 1))
980 lprops->flags = LPROPS_INDEX;
983 lprops->flags |= ubifs_categorize_lprops(c, lprops);
985 err = check_lpt_crc(buf, c->pnode_sz);
990 * ubifs_unpack_nnode - unpack a nnode.
991 * @c: UBIFS file-system description object
992 * @buf: buffer containing packed nnode to unpack
993 * @nnode: nnode structure to fill
995 * This function returns %0 on success and a negative error code on failure.
997 int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
998 struct ubifs_nnode *nnode)
1000 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1001 int i, pos = 0, err;
1003 err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
1007 nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1008 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1011 lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
1013 if (lnum == c->lpt_last + 1)
1015 nnode->nbranch[i].lnum = lnum;
1016 nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
1019 err = check_lpt_crc(buf, c->nnode_sz);
1024 * unpack_ltab - unpack the LPT's own lprops table.
1025 * @c: UBIFS file-system description object
1026 * @buf: buffer from which to unpack
1028 * This function returns %0 on success and a negative error code on failure.
1030 static int unpack_ltab(const struct ubifs_info *c, void *buf)
1032 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1033 int i, pos = 0, err;
1035 err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
1038 for (i = 0; i < c->lpt_lebs; i++) {
1039 int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1040 int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1042 if (free < 0 || free > c->leb_size || dirty < 0 ||
1043 dirty > c->leb_size || free + dirty > c->leb_size)
1046 c->ltab[i].free = free;
1047 c->ltab[i].dirty = dirty;
1051 err = check_lpt_crc(buf, c->ltab_sz);
1057 * unpack_lsave - unpack the LPT's save table.
1058 * @c: UBIFS file-system description object
1059 * @buf: buffer from which to unpack
1061 * This function returns %0 on success and a negative error code on failure.
1063 static int unpack_lsave(const struct ubifs_info *c, void *buf)
1065 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1066 int i, pos = 0, err;
1068 err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
1071 for (i = 0; i < c->lsave_cnt; i++) {
1072 int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1074 if (lnum < c->main_first || lnum >= c->leb_cnt)
1078 err = check_lpt_crc(buf, c->lsave_sz);
1084 * validate_nnode - validate a nnode.
1085 * @c: UBIFS file-system description object
1086 * @nnode: nnode to validate
1087 * @parent: parent nnode (or NULL for the root nnode)
1088 * @iip: index in parent
1090 * This function returns %0 on success and a negative error code on failure.
1092 static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode,
1093 struct ubifs_nnode *parent, int iip)
1095 int i, lvl, max_offs;
1098 int num = calc_nnode_num_from_parent(c, parent, iip);
1100 if (nnode->num != num)
1103 lvl = parent ? parent->level - 1 : c->lpt_hght;
1107 max_offs = c->leb_size - c->pnode_sz;
1109 max_offs = c->leb_size - c->nnode_sz;
1110 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1111 int lnum = nnode->nbranch[i].lnum;
1112 int offs = nnode->nbranch[i].offs;
1119 if (lnum < c->lpt_first || lnum > c->lpt_last)
1121 if (offs < 0 || offs > max_offs)
1128 * validate_pnode - validate a pnode.
1129 * @c: UBIFS file-system description object
1130 * @pnode: pnode to validate
1131 * @parent: parent nnode
1132 * @iip: index in parent
1134 * This function returns %0 on success and a negative error code on failure.
1136 static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode,
1137 struct ubifs_nnode *parent, int iip)
1142 int num = calc_pnode_num_from_parent(c, parent, iip);
1144 if (pnode->num != num)
1147 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1148 int free = pnode->lprops[i].free;
1149 int dirty = pnode->lprops[i].dirty;
1151 if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1154 if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1156 if (dirty + free > c->leb_size)
1163 * set_pnode_lnum - set LEB numbers on a pnode.
1164 * @c: UBIFS file-system description object
1165 * @pnode: pnode to update
1167 * This function calculates the LEB numbers for the LEB properties it contains
1168 * based on the pnode number.
1170 static void set_pnode_lnum(const struct ubifs_info *c,
1171 struct ubifs_pnode *pnode)
1175 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1176 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1177 if (lnum >= c->leb_cnt)
1179 pnode->lprops[i].lnum = lnum++;
1184 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1185 * @c: UBIFS file-system description object
1186 * @parent: parent nnode (or NULL for the root)
1187 * @iip: index in parent
1189 * This function returns %0 on success and a negative error code on failure.
1191 int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1193 struct ubifs_nbranch *branch = NULL;
1194 struct ubifs_nnode *nnode = NULL;
1195 void *buf = c->lpt_nod_buf;
1196 int err, lnum, offs;
1199 branch = &parent->nbranch[iip];
1200 lnum = branch->lnum;
1201 offs = branch->offs;
1206 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1213 * This nnode was not written which just means that the LEB
1214 * properties in the subtree below it describe empty LEBs. We
1215 * make the nnode as though we had read it, which in fact means
1216 * doing almost nothing.
1219 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1221 err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1);
1224 err = ubifs_unpack_nnode(c, buf, nnode);
1228 err = validate_nnode(c, nnode, parent, iip);
1232 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1234 branch->nnode = nnode;
1235 nnode->level = parent->level - 1;
1238 nnode->level = c->lpt_hght;
1240 nnode->parent = parent;
1245 ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
1252 * read_pnode - read a pnode from flash and link it to the tree in memory.
1253 * @c: UBIFS file-system description object
1254 * @parent: parent nnode
1255 * @iip: index in parent
1257 * This function returns %0 on success and a negative error code on failure.
1259 static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1261 struct ubifs_nbranch *branch;
1262 struct ubifs_pnode *pnode = NULL;
1263 void *buf = c->lpt_nod_buf;
1264 int err, lnum, offs;
1266 branch = &parent->nbranch[iip];
1267 lnum = branch->lnum;
1268 offs = branch->offs;
1269 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1275 * This pnode was not written which just means that the LEB
1276 * properties in it describe empty LEBs. We make the pnode as
1277 * though we had read it.
1282 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1283 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1284 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1286 lprops->free = c->leb_size;
1287 lprops->flags = ubifs_categorize_lprops(c, lprops);
1290 err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1);
1293 err = unpack_pnode(c, buf, pnode);
1297 err = validate_pnode(c, pnode, parent, iip);
1301 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1302 branch->pnode = pnode;
1303 pnode->parent = parent;
1305 set_pnode_lnum(c, pnode);
1306 c->pnodes_have += 1;
1310 ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
1311 ubifs_dump_pnode(c, pnode, parent, iip);
1313 ubifs_err("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1319 * read_ltab - read LPT's own lprops table.
1320 * @c: UBIFS file-system description object
1322 * This function returns %0 on success and a negative error code on failure.
1324 static int read_ltab(struct ubifs_info *c)
1329 buf = vmalloc(c->ltab_sz);
1332 err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1);
1335 err = unpack_ltab(c, buf);
1343 * read_lsave - read LPT's save 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_lsave(struct ubifs_info *c)
1353 buf = vmalloc(c->lsave_sz);
1356 err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs,
1360 err = unpack_lsave(c, buf);
1363 for (i = 0; i < c->lsave_cnt; i++) {
1364 int lnum = c->lsave[i];
1365 struct ubifs_lprops *lprops;
1368 * Due to automatic resizing, the values in the lsave table
1369 * could be beyond the volume size - just ignore them.
1371 if (lnum >= c->leb_cnt)
1373 lprops = ubifs_lpt_lookup(c, lnum);
1374 if (IS_ERR(lprops)) {
1375 err = PTR_ERR(lprops);
1386 * ubifs_get_nnode - get a nnode.
1387 * @c: UBIFS file-system description object
1388 * @parent: parent nnode (or NULL for the root)
1389 * @iip: index in parent
1391 * This function returns a pointer to the nnode on success or a negative error
1394 struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1395 struct ubifs_nnode *parent, int iip)
1397 struct ubifs_nbranch *branch;
1398 struct ubifs_nnode *nnode;
1401 branch = &parent->nbranch[iip];
1402 nnode = branch->nnode;
1405 err = ubifs_read_nnode(c, parent, iip);
1407 return ERR_PTR(err);
1408 return branch->nnode;
1412 * ubifs_get_pnode - get a pnode.
1413 * @c: UBIFS file-system description object
1414 * @parent: parent nnode
1415 * @iip: index in parent
1417 * This function returns a pointer to the pnode on success or a negative error
1420 struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1421 struct ubifs_nnode *parent, int iip)
1423 struct ubifs_nbranch *branch;
1424 struct ubifs_pnode *pnode;
1427 branch = &parent->nbranch[iip];
1428 pnode = branch->pnode;
1431 err = read_pnode(c, parent, iip);
1433 return ERR_PTR(err);
1434 update_cats(c, branch->pnode);
1435 return branch->pnode;
1439 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1440 * @c: UBIFS file-system description object
1441 * @lnum: LEB number to lookup
1443 * This function returns a pointer to the LEB properties on success or a
1444 * negative error code on failure.
1446 struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1448 int err, i, h, iip, shft;
1449 struct ubifs_nnode *nnode;
1450 struct ubifs_pnode *pnode;
1453 err = ubifs_read_nnode(c, NULL, 0);
1455 return ERR_PTR(err);
1458 i = lnum - c->main_first;
1459 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1460 for (h = 1; h < c->lpt_hght; h++) {
1461 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1462 shft -= UBIFS_LPT_FANOUT_SHIFT;
1463 nnode = ubifs_get_nnode(c, nnode, iip);
1465 return ERR_CAST(nnode);
1467 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1468 shft -= UBIFS_LPT_FANOUT_SHIFT;
1469 pnode = ubifs_get_pnode(c, nnode, iip);
1471 return ERR_CAST(pnode);
1472 iip = (i & (UBIFS_LPT_FANOUT - 1));
1473 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1474 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1475 pnode->lprops[iip].flags);
1476 return &pnode->lprops[iip];
1480 * dirty_cow_nnode - ensure a nnode is not being committed.
1481 * @c: UBIFS file-system description object
1482 * @nnode: nnode to check
1484 * Returns dirtied nnode on success or negative error code on failure.
1486 static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1487 struct ubifs_nnode *nnode)
1489 struct ubifs_nnode *n;
1492 if (!test_bit(COW_CNODE, &nnode->flags)) {
1493 /* nnode is not being committed */
1494 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1495 c->dirty_nn_cnt += 1;
1496 ubifs_add_nnode_dirt(c, nnode);
1501 /* nnode is being committed, so copy it */
1502 n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1504 return ERR_PTR(-ENOMEM);
1506 memcpy(n, nnode, sizeof(struct ubifs_nnode));
1508 __set_bit(DIRTY_CNODE, &n->flags);
1509 __clear_bit(COW_CNODE, &n->flags);
1511 /* The children now have new parent */
1512 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1513 struct ubifs_nbranch *branch = &n->nbranch[i];
1516 branch->cnode->parent = n;
1519 ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1520 __set_bit(OBSOLETE_CNODE, &nnode->flags);
1522 c->dirty_nn_cnt += 1;
1523 ubifs_add_nnode_dirt(c, nnode);
1525 nnode->parent->nbranch[n->iip].nnode = n;
1532 * dirty_cow_pnode - ensure a pnode is not being committed.
1533 * @c: UBIFS file-system description object
1534 * @pnode: pnode to check
1536 * Returns dirtied pnode on success or negative error code on failure.
1538 static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1539 struct ubifs_pnode *pnode)
1541 struct ubifs_pnode *p;
1543 if (!test_bit(COW_CNODE, &pnode->flags)) {
1544 /* pnode is not being committed */
1545 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1546 c->dirty_pn_cnt += 1;
1547 add_pnode_dirt(c, pnode);
1552 /* pnode is being committed, so copy it */
1553 p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1555 return ERR_PTR(-ENOMEM);
1557 memcpy(p, pnode, sizeof(struct ubifs_pnode));
1559 __set_bit(DIRTY_CNODE, &p->flags);
1560 __clear_bit(COW_CNODE, &p->flags);
1561 replace_cats(c, pnode, p);
1563 ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1564 __set_bit(OBSOLETE_CNODE, &pnode->flags);
1566 c->dirty_pn_cnt += 1;
1567 add_pnode_dirt(c, pnode);
1568 pnode->parent->nbranch[p->iip].pnode = p;
1573 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1574 * @c: UBIFS file-system description object
1575 * @lnum: LEB number to lookup
1577 * This function returns a pointer to the LEB properties on success or a
1578 * negative error code on failure.
1580 struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1582 int err, i, h, iip, shft;
1583 struct ubifs_nnode *nnode;
1584 struct ubifs_pnode *pnode;
1587 err = ubifs_read_nnode(c, NULL, 0);
1589 return ERR_PTR(err);
1592 nnode = dirty_cow_nnode(c, nnode);
1594 return ERR_CAST(nnode);
1595 i = lnum - c->main_first;
1596 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1597 for (h = 1; h < c->lpt_hght; h++) {
1598 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1599 shft -= UBIFS_LPT_FANOUT_SHIFT;
1600 nnode = ubifs_get_nnode(c, nnode, iip);
1602 return ERR_CAST(nnode);
1603 nnode = dirty_cow_nnode(c, nnode);
1605 return ERR_CAST(nnode);
1607 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1608 shft -= UBIFS_LPT_FANOUT_SHIFT;
1609 pnode = ubifs_get_pnode(c, nnode, iip);
1611 return ERR_CAST(pnode);
1612 pnode = dirty_cow_pnode(c, pnode);
1614 return ERR_CAST(pnode);
1615 iip = (i & (UBIFS_LPT_FANOUT - 1));
1616 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1617 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1618 pnode->lprops[iip].flags);
1619 ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1620 return &pnode->lprops[iip];
1624 * lpt_init_rd - initialize the LPT for reading.
1625 * @c: UBIFS file-system description object
1627 * This function returns %0 on success and a negative error code on failure.
1629 static int lpt_init_rd(struct ubifs_info *c)
1633 c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1637 i = max_t(int, c->nnode_sz, c->pnode_sz);
1638 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1639 if (!c->lpt_nod_buf)
1642 for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1643 c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1645 if (!c->lpt_heap[i].arr)
1647 c->lpt_heap[i].cnt = 0;
1648 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1651 c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1652 if (!c->dirty_idx.arr)
1654 c->dirty_idx.cnt = 0;
1655 c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1661 dbg_lp("space_bits %d", c->space_bits);
1662 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1663 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1664 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1665 dbg_lp("pcnt_bits %d", c->pcnt_bits);
1666 dbg_lp("lnum_bits %d", c->lnum_bits);
1667 dbg_lp("pnode_sz %d", c->pnode_sz);
1668 dbg_lp("nnode_sz %d", c->nnode_sz);
1669 dbg_lp("ltab_sz %d", c->ltab_sz);
1670 dbg_lp("lsave_sz %d", c->lsave_sz);
1671 dbg_lp("lsave_cnt %d", c->lsave_cnt);
1672 dbg_lp("lpt_hght %d", c->lpt_hght);
1673 dbg_lp("big_lpt %d", c->big_lpt);
1674 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1675 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1676 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1678 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1685 * lpt_init_wr - initialize the LPT for writing.
1686 * @c: UBIFS file-system description object
1688 * 'lpt_init_rd()' must have been called already.
1690 * This function returns %0 on success and a negative error code on failure.
1692 static int lpt_init_wr(struct ubifs_info *c)
1696 c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1700 c->lpt_buf = vmalloc(c->leb_size);
1705 c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1708 err = read_lsave(c);
1713 for (i = 0; i < c->lpt_lebs; i++)
1714 if (c->ltab[i].free == c->leb_size) {
1715 err = ubifs_leb_unmap(c, i + c->lpt_first);
1725 * ubifs_lpt_init - initialize the LPT.
1726 * @c: UBIFS file-system description object
1727 * @rd: whether to initialize lpt for reading
1728 * @wr: whether to initialize lpt for writing
1730 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1731 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1734 * This function returns %0 on success and a negative error code on failure.
1736 int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1741 err = lpt_init_rd(c);
1748 err = lpt_init_wr(c);
1759 ubifs_lpt_free(c, 1);
1762 ubifs_lpt_free(c, 0);
1767 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1768 * @nnode: where to keep a nnode
1769 * @pnode: where to keep a pnode
1770 * @cnode: where to keep a cnode
1771 * @in_tree: is the node in the tree in memory
1772 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1774 * @ptr.pnode: ditto for pnode
1775 * @ptr.cnode: ditto for cnode
1777 struct lpt_scan_node {
1779 struct ubifs_nnode nnode;
1780 struct ubifs_pnode pnode;
1781 struct ubifs_cnode cnode;
1785 struct ubifs_nnode *nnode;
1786 struct ubifs_pnode *pnode;
1787 struct ubifs_cnode *cnode;
1792 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1793 * @c: the UBIFS file-system description object
1794 * @path: where to put the nnode
1795 * @parent: parent of the nnode
1796 * @iip: index in parent of the nnode
1798 * This function returns a pointer to the nnode on success or a negative error
1801 static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1802 struct lpt_scan_node *path,
1803 struct ubifs_nnode *parent, int iip)
1805 struct ubifs_nbranch *branch;
1806 struct ubifs_nnode *nnode;
1807 void *buf = c->lpt_nod_buf;
1810 branch = &parent->nbranch[iip];
1811 nnode = branch->nnode;
1814 path->ptr.nnode = nnode;
1817 nnode = &path->nnode;
1819 path->ptr.nnode = nnode;
1820 memset(nnode, 0, sizeof(struct ubifs_nnode));
1821 if (branch->lnum == 0) {
1823 * This nnode was not written which just means that the LEB
1824 * properties in the subtree below it describe empty LEBs. We
1825 * make the nnode as though we had read it, which in fact means
1826 * doing almost nothing.
1829 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1831 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1834 return ERR_PTR(err);
1835 err = ubifs_unpack_nnode(c, buf, nnode);
1837 return ERR_PTR(err);
1839 err = validate_nnode(c, nnode, parent, iip);
1841 return ERR_PTR(err);
1843 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1844 nnode->level = parent->level - 1;
1845 nnode->parent = parent;
1851 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1852 * @c: the UBIFS file-system description object
1853 * @path: where to put the pnode
1854 * @parent: parent of the pnode
1855 * @iip: index in parent of the pnode
1857 * This function returns a pointer to the pnode on success or a negative error
1860 static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1861 struct lpt_scan_node *path,
1862 struct ubifs_nnode *parent, int iip)
1864 struct ubifs_nbranch *branch;
1865 struct ubifs_pnode *pnode;
1866 void *buf = c->lpt_nod_buf;
1869 branch = &parent->nbranch[iip];
1870 pnode = branch->pnode;
1873 path->ptr.pnode = pnode;
1876 pnode = &path->pnode;
1878 path->ptr.pnode = pnode;
1879 memset(pnode, 0, sizeof(struct ubifs_pnode));
1880 if (branch->lnum == 0) {
1882 * This pnode was not written which just means that the LEB
1883 * properties in it describe empty LEBs. We make the pnode as
1884 * though we had read it.
1889 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1890 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1891 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1893 lprops->free = c->leb_size;
1894 lprops->flags = ubifs_categorize_lprops(c, lprops);
1897 ubifs_assert(branch->lnum >= c->lpt_first &&
1898 branch->lnum <= c->lpt_last);
1899 ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1900 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs,
1903 return ERR_PTR(err);
1904 err = unpack_pnode(c, buf, pnode);
1906 return ERR_PTR(err);
1908 err = validate_pnode(c, pnode, parent, iip);
1910 return ERR_PTR(err);
1912 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1913 pnode->parent = parent;
1915 set_pnode_lnum(c, pnode);
1920 * ubifs_lpt_scan_nolock - scan the LPT.
1921 * @c: the UBIFS file-system description object
1922 * @start_lnum: LEB number from which to start scanning
1923 * @end_lnum: LEB number at which to stop scanning
1924 * @scan_cb: callback function called for each lprops
1925 * @data: data to be passed to the callback function
1927 * This function returns %0 on success and a negative error code on failure.
1929 int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1930 ubifs_lpt_scan_callback scan_cb, void *data)
1932 int err = 0, i, h, iip, shft;
1933 struct ubifs_nnode *nnode;
1934 struct ubifs_pnode *pnode;
1935 struct lpt_scan_node *path;
1937 if (start_lnum == -1) {
1938 start_lnum = end_lnum + 1;
1939 if (start_lnum >= c->leb_cnt)
1940 start_lnum = c->main_first;
1943 ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1944 ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1947 err = ubifs_read_nnode(c, NULL, 0);
1952 path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1957 path[0].ptr.nnode = c->nroot;
1958 path[0].in_tree = 1;
1960 /* Descend to the pnode containing start_lnum */
1962 i = start_lnum - c->main_first;
1963 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1964 for (h = 1; h < c->lpt_hght; h++) {
1965 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1966 shft -= UBIFS_LPT_FANOUT_SHIFT;
1967 nnode = scan_get_nnode(c, path + h, nnode, iip);
1968 if (IS_ERR(nnode)) {
1969 err = PTR_ERR(nnode);
1973 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1974 shft -= UBIFS_LPT_FANOUT_SHIFT;
1975 pnode = scan_get_pnode(c, path + h, nnode, iip);
1976 if (IS_ERR(pnode)) {
1977 err = PTR_ERR(pnode);
1980 iip = (i & (UBIFS_LPT_FANOUT - 1));
1982 /* Loop for each lprops */
1984 struct ubifs_lprops *lprops = &pnode->lprops[iip];
1985 int ret, lnum = lprops->lnum;
1987 ret = scan_cb(c, lprops, path[h].in_tree, data);
1992 if (ret & LPT_SCAN_ADD) {
1993 /* Add all the nodes in path to the tree in memory */
1994 for (h = 1; h < c->lpt_hght; h++) {
1995 const size_t sz = sizeof(struct ubifs_nnode);
1996 struct ubifs_nnode *parent;
1998 if (path[h].in_tree)
2000 nnode = kmemdup(&path[h].nnode, sz, GFP_NOFS);
2005 parent = nnode->parent;
2006 parent->nbranch[nnode->iip].nnode = nnode;
2007 path[h].ptr.nnode = nnode;
2008 path[h].in_tree = 1;
2009 path[h + 1].cnode.parent = nnode;
2011 if (path[h].in_tree)
2012 ubifs_ensure_cat(c, lprops);
2014 const size_t sz = sizeof(struct ubifs_pnode);
2015 struct ubifs_nnode *parent;
2017 pnode = kmemdup(&path[h].pnode, sz, GFP_NOFS);
2022 parent = pnode->parent;
2023 parent->nbranch[pnode->iip].pnode = pnode;
2024 path[h].ptr.pnode = pnode;
2025 path[h].in_tree = 1;
2026 update_cats(c, pnode);
2027 c->pnodes_have += 1;
2029 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
2033 err = dbg_check_cats(c);
2037 if (ret & LPT_SCAN_STOP) {
2041 /* Get the next lprops */
2042 if (lnum == end_lnum) {
2044 * We got to the end without finding what we were
2050 if (lnum + 1 >= c->leb_cnt) {
2051 /* Wrap-around to the beginning */
2052 start_lnum = c->main_first;
2055 if (iip + 1 < UBIFS_LPT_FANOUT) {
2056 /* Next lprops is in the same pnode */
2060 /* We need to get the next pnode. Go up until we can go right */
2064 ubifs_assert(h >= 0);
2065 nnode = path[h].ptr.nnode;
2066 if (iip + 1 < UBIFS_LPT_FANOUT)
2072 /* Descend to the pnode */
2074 for (; h < c->lpt_hght; h++) {
2075 nnode = scan_get_nnode(c, path + h, nnode, iip);
2076 if (IS_ERR(nnode)) {
2077 err = PTR_ERR(nnode);
2082 pnode = scan_get_pnode(c, path + h, nnode, iip);
2083 if (IS_ERR(pnode)) {
2084 err = PTR_ERR(pnode);
2095 * dbg_chk_pnode - check a pnode.
2096 * @c: the UBIFS file-system description object
2097 * @pnode: pnode to check
2098 * @col: pnode column
2100 * This function returns %0 on success and a negative error code on failure.
2102 static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2107 if (pnode->num != col) {
2108 ubifs_err("pnode num %d expected %d parent num %d iip %d",
2109 pnode->num, col, pnode->parent->num, pnode->iip);
2112 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2113 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2114 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2116 int found, cat = lprops->flags & LPROPS_CAT_MASK;
2117 struct ubifs_lpt_heap *heap;
2118 struct list_head *list = NULL;
2120 if (lnum >= c->leb_cnt)
2122 if (lprops->lnum != lnum) {
2123 ubifs_err("bad LEB number %d expected %d",
2124 lprops->lnum, lnum);
2127 if (lprops->flags & LPROPS_TAKEN) {
2128 if (cat != LPROPS_UNCAT) {
2129 ubifs_err("LEB %d taken but not uncat %d",
2135 if (lprops->flags & LPROPS_INDEX) {
2138 case LPROPS_DIRTY_IDX:
2139 case LPROPS_FRDI_IDX:
2142 ubifs_err("LEB %d index but cat %d",
2152 case LPROPS_FREEABLE:
2155 ubifs_err("LEB %d not index but cat %d",
2162 list = &c->uncat_list;
2165 list = &c->empty_list;
2167 case LPROPS_FREEABLE:
2168 list = &c->freeable_list;
2170 case LPROPS_FRDI_IDX:
2171 list = &c->frdi_idx_list;
2177 case LPROPS_DIRTY_IDX:
2179 heap = &c->lpt_heap[cat - 1];
2180 if (lprops->hpos < heap->cnt &&
2181 heap->arr[lprops->hpos] == lprops)
2186 case LPROPS_FREEABLE:
2187 case LPROPS_FRDI_IDX:
2188 list_for_each_entry(lp, list, list)
2196 ubifs_err("LEB %d cat %d not found in cat heap/list",
2202 if (lprops->free != c->leb_size) {
2203 ubifs_err("LEB %d cat %d free %d dirty %d",
2204 lprops->lnum, cat, lprops->free,
2208 case LPROPS_FREEABLE:
2209 case LPROPS_FRDI_IDX:
2210 if (lprops->free + lprops->dirty != c->leb_size) {
2211 ubifs_err("LEB %d cat %d free %d dirty %d",
2212 lprops->lnum, cat, lprops->free,
2222 * dbg_check_lpt_nodes - check nnodes and pnodes.
2223 * @c: the UBIFS file-system description object
2224 * @cnode: next cnode (nnode or pnode) to check
2225 * @row: row of cnode (root is zero)
2226 * @col: column of cnode (leftmost is zero)
2228 * This function returns %0 on success and a negative error code on failure.
2230 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2233 struct ubifs_nnode *nnode, *nn;
2234 struct ubifs_cnode *cn;
2235 int num, iip = 0, err;
2237 if (!dbg_is_chk_lprops(c))
2241 ubifs_assert(row >= 0);
2242 nnode = cnode->parent;
2244 /* cnode is a nnode */
2245 num = calc_nnode_num(row, col);
2246 if (cnode->num != num) {
2247 ubifs_err("nnode num %d expected %d parent num %d iip %d",
2249 (nnode ? nnode->num : 0), cnode->iip);
2252 nn = (struct ubifs_nnode *)cnode;
2253 while (iip < UBIFS_LPT_FANOUT) {
2254 cn = nn->nbranch[iip].cnode;
2258 col <<= UBIFS_LPT_FANOUT_SHIFT;
2267 if (iip < UBIFS_LPT_FANOUT)
2270 struct ubifs_pnode *pnode;
2272 /* cnode is a pnode */
2273 pnode = (struct ubifs_pnode *)cnode;
2274 err = dbg_chk_pnode(c, pnode, col);
2278 /* Go up and to the right */
2280 col >>= UBIFS_LPT_FANOUT_SHIFT;
2281 iip = cnode->iip + 1;
2282 cnode = (struct ubifs_cnode *)nnode;