Lines Matching +full:- +full:c
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
18 * The LPT area is like a miniature self-contained file system. It is required
48 * do_calc_lpt_geom - calculate sizes for the LPT area.
49 * @c: the UBIFS file-system description object
52 * properties of the flash and whether LPT is "big" (c->big_lpt).
54 static void do_calc_lpt_geom(struct ubifs_info *c) in do_calc_lpt_geom() argument
59 n = c->main_lebs + c->max_leb_cnt - c->leb_cnt; in do_calc_lpt_geom()
62 c->lpt_hght = 1; in do_calc_lpt_geom()
65 c->lpt_hght += 1; in do_calc_lpt_geom()
69 c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in do_calc_lpt_geom()
71 n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT); in do_calc_lpt_geom()
72 c->nnode_cnt = n; in do_calc_lpt_geom()
73 for (i = 1; i < c->lpt_hght; i++) { in do_calc_lpt_geom()
75 c->nnode_cnt += n; in do_calc_lpt_geom()
78 c->space_bits = fls(c->leb_size) - 3; in do_calc_lpt_geom()
79 c->lpt_lnum_bits = fls(c->lpt_lebs); in do_calc_lpt_geom()
80 c->lpt_offs_bits = fls(c->leb_size - 1); in do_calc_lpt_geom()
81 c->lpt_spc_bits = fls(c->leb_size); in do_calc_lpt_geom()
83 n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT); in do_calc_lpt_geom()
84 c->pcnt_bits = fls(n - 1); in do_calc_lpt_geom()
86 c->lnum_bits = fls(c->max_leb_cnt - 1); in do_calc_lpt_geom()
89 (c->big_lpt ? c->pcnt_bits : 0) + in do_calc_lpt_geom()
90 (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT; in do_calc_lpt_geom()
91 c->pnode_sz = (bits + 7) / 8; in do_calc_lpt_geom()
94 (c->big_lpt ? c->pcnt_bits : 0) + in do_calc_lpt_geom()
95 (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT; in do_calc_lpt_geom()
96 c->nnode_sz = (bits + 7) / 8; in do_calc_lpt_geom()
99 c->lpt_lebs * c->lpt_spc_bits * 2; in do_calc_lpt_geom()
100 c->ltab_sz = (bits + 7) / 8; in do_calc_lpt_geom()
103 c->lnum_bits * c->lsave_cnt; in do_calc_lpt_geom()
104 c->lsave_sz = (bits + 7) / 8; in do_calc_lpt_geom()
107 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; in do_calc_lpt_geom()
108 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; in do_calc_lpt_geom()
109 c->lpt_sz += c->ltab_sz; in do_calc_lpt_geom()
110 if (c->big_lpt) in do_calc_lpt_geom()
111 c->lpt_sz += c->lsave_sz; in do_calc_lpt_geom()
114 sz = c->lpt_sz; in do_calc_lpt_geom()
115 per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz); in do_calc_lpt_geom()
118 while (sz > c->leb_size) { in do_calc_lpt_geom()
120 sz -= c->leb_size; in do_calc_lpt_geom()
123 tot_wastage += ALIGN(sz, c->min_io_size) - sz; in do_calc_lpt_geom()
124 c->lpt_sz += tot_wastage; in do_calc_lpt_geom()
128 * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
129 * @c: the UBIFS file-system description object
133 int ubifs_calc_lpt_geom(struct ubifs_info *c) in ubifs_calc_lpt_geom() argument
138 do_calc_lpt_geom(c); in ubifs_calc_lpt_geom()
141 sz = c->lpt_sz * 2; /* Must have at least 2 times the size */ in ubifs_calc_lpt_geom()
142 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size); in ubifs_calc_lpt_geom()
143 if (lebs_needed > c->lpt_lebs) { in ubifs_calc_lpt_geom()
144 ubifs_err(c, "too few LPT LEBs"); in ubifs_calc_lpt_geom()
145 return -EINVAL; in ubifs_calc_lpt_geom()
149 if (c->ltab_sz > c->leb_size) { in ubifs_calc_lpt_geom()
150 ubifs_err(c, "LPT ltab too big"); in ubifs_calc_lpt_geom()
151 return -EINVAL; in ubifs_calc_lpt_geom()
154 c->check_lpt_free = c->big_lpt; in ubifs_calc_lpt_geom()
159 * calc_dflt_lpt_geom - calculate default LPT geometry.
160 * @c: the UBIFS file-system description object
170 static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, in calc_dflt_lpt_geom() argument
177 c->lpt_lebs = UBIFS_MIN_LPT_LEBS; in calc_dflt_lpt_geom()
178 c->main_lebs = *main_lebs - c->lpt_lebs; in calc_dflt_lpt_geom()
179 if (c->main_lebs <= 0) in calc_dflt_lpt_geom()
180 return -EINVAL; in calc_dflt_lpt_geom()
183 c->big_lpt = 0; in calc_dflt_lpt_geom()
189 do_calc_lpt_geom(c); in calc_dflt_lpt_geom()
192 if (c->lpt_sz > c->leb_size) { in calc_dflt_lpt_geom()
194 c->big_lpt = 1; in calc_dflt_lpt_geom()
195 do_calc_lpt_geom(c); in calc_dflt_lpt_geom()
200 sz = c->lpt_sz * 4; /* Allow 4 times the size */ in calc_dflt_lpt_geom()
201 lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size); in calc_dflt_lpt_geom()
202 if (lebs_needed > c->lpt_lebs) { in calc_dflt_lpt_geom()
204 c->lpt_lebs = lebs_needed; in calc_dflt_lpt_geom()
205 c->main_lebs = *main_lebs - c->lpt_lebs; in calc_dflt_lpt_geom()
206 if (c->main_lebs <= 0) in calc_dflt_lpt_geom()
207 return -EINVAL; in calc_dflt_lpt_geom()
208 do_calc_lpt_geom(c); in calc_dflt_lpt_geom()
211 if (c->ltab_sz > c->leb_size) { in calc_dflt_lpt_geom()
212 ubifs_err(c, "LPT ltab too big"); in calc_dflt_lpt_geom()
213 return -EINVAL; in calc_dflt_lpt_geom()
215 *main_lebs = c->main_lebs; in calc_dflt_lpt_geom()
216 *big_lpt = c->big_lpt; in calc_dflt_lpt_geom()
219 return -EINVAL; in calc_dflt_lpt_geom()
223 * pack_bits - pack bit fields end-to-end.
227 * @nrbits: number of bits of value to pack (1-32)
243 *++p = (uint8_t)(val >>= (8 - b)); in pack_bits()
272 * ubifs_unpack_bits - unpack bit fields.
275 * @nrbits: number of bits of value to unpack (1-32)
281 const int k = 32 - nrbits; in ubifs_unpack_bits()
308 val <<= (8 - b); in ubifs_unpack_bits()
336 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32); in ubifs_unpack_bits()
341 * ubifs_pack_pnode - pack all the bit fields of a pnode.
342 * @c: UBIFS file-system description object
346 void ubifs_pack_pnode(struct ubifs_info *c, void *buf, in ubifs_pack_pnode() argument
354 if (c->big_lpt) in ubifs_pack_pnode()
355 pack_bits(&addr, &pos, pnode->num, c->pcnt_bits); in ubifs_pack_pnode()
357 pack_bits(&addr, &pos, pnode->lprops[i].free >> 3, in ubifs_pack_pnode()
358 c->space_bits); in ubifs_pack_pnode()
359 pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3, in ubifs_pack_pnode()
360 c->space_bits); in ubifs_pack_pnode()
361 if (pnode->lprops[i].flags & LPROPS_INDEX) in ubifs_pack_pnode()
366 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in ubifs_pack_pnode()
367 c->pnode_sz - UBIFS_LPT_CRC_BYTES); in ubifs_pack_pnode()
374 * ubifs_pack_nnode - pack all the bit fields of a nnode.
375 * @c: UBIFS file-system description object
379 void ubifs_pack_nnode(struct ubifs_info *c, void *buf, in ubifs_pack_nnode() argument
387 if (c->big_lpt) in ubifs_pack_nnode()
388 pack_bits(&addr, &pos, nnode->num, c->pcnt_bits); in ubifs_pack_nnode()
390 int lnum = nnode->nbranch[i].lnum; in ubifs_pack_nnode()
393 lnum = c->lpt_last + 1; in ubifs_pack_nnode()
394 pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits); in ubifs_pack_nnode()
395 pack_bits(&addr, &pos, nnode->nbranch[i].offs, in ubifs_pack_nnode()
396 c->lpt_offs_bits); in ubifs_pack_nnode()
398 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in ubifs_pack_nnode()
399 c->nnode_sz - UBIFS_LPT_CRC_BYTES); in ubifs_pack_nnode()
406 * ubifs_pack_ltab - pack the LPT's own lprops table.
407 * @c: UBIFS file-system description object
411 void ubifs_pack_ltab(struct ubifs_info *c, void *buf, in ubifs_pack_ltab() argument
419 for (i = 0; i < c->lpt_lebs; i++) { in ubifs_pack_ltab()
420 pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits); in ubifs_pack_ltab()
421 pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits); in ubifs_pack_ltab()
423 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in ubifs_pack_ltab()
424 c->ltab_sz - UBIFS_LPT_CRC_BYTES); in ubifs_pack_ltab()
431 * ubifs_pack_lsave - pack the LPT's save table.
432 * @c: UBIFS file-system description object
436 void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave) in ubifs_pack_lsave() argument
443 for (i = 0; i < c->lsave_cnt; i++) in ubifs_pack_lsave()
444 pack_bits(&addr, &pos, lsave[i], c->lnum_bits); in ubifs_pack_lsave()
445 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in ubifs_pack_lsave()
446 c->lsave_sz - UBIFS_LPT_CRC_BYTES); in ubifs_pack_lsave()
453 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
454 * @c: UBIFS file-system description object
458 void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty) in ubifs_add_lpt_dirt() argument
463 lnum, dirty, c->ltab[lnum - c->lpt_first].dirty); in ubifs_add_lpt_dirt()
464 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); in ubifs_add_lpt_dirt()
465 c->ltab[lnum - c->lpt_first].dirty += dirty; in ubifs_add_lpt_dirt()
469 * set_ltab - set LPT LEB properties.
470 * @c: UBIFS file-system description object
475 static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty) in set_ltab() argument
478 lnum, c->ltab[lnum - c->lpt_first].free, in set_ltab()
479 c->ltab[lnum - c->lpt_first].dirty, free, dirty); in set_ltab()
480 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); in set_ltab()
481 c->ltab[lnum - c->lpt_first].free = free; in set_ltab()
482 c->ltab[lnum - c->lpt_first].dirty = dirty; in set_ltab()
486 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
487 * @c: UBIFS file-system description object
490 void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode) in ubifs_add_nnode_dirt() argument
492 struct ubifs_nnode *np = nnode->parent; in ubifs_add_nnode_dirt()
495 ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum, in ubifs_add_nnode_dirt()
496 c->nnode_sz); in ubifs_add_nnode_dirt()
498 ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz); in ubifs_add_nnode_dirt()
499 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { in ubifs_add_nnode_dirt()
500 c->lpt_drty_flgs |= LTAB_DIRTY; in ubifs_add_nnode_dirt()
501 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); in ubifs_add_nnode_dirt()
507 * add_pnode_dirt - add dirty space to LPT LEB properties.
508 * @c: UBIFS file-system description object
511 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) in add_pnode_dirt() argument
513 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, in add_pnode_dirt()
514 c->pnode_sz); in add_pnode_dirt()
518 * calc_nnode_num - calculate nnode number.
533 while (row--) { in calc_nnode_num()
534 bits = (col & (UBIFS_LPT_FANOUT - 1)); in calc_nnode_num()
543 * calc_nnode_num_from_parent - calculate nnode number.
544 * @c: UBIFS file-system description object
554 static int calc_nnode_num_from_parent(const struct ubifs_info *c, in calc_nnode_num_from_parent() argument
561 shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT; in calc_nnode_num_from_parent()
562 num = parent->num ^ (1 << shft); in calc_nnode_num_from_parent()
568 * calc_pnode_num_from_parent - calculate pnode number.
569 * @c: UBIFS file-system description object
579 static int calc_pnode_num_from_parent(const struct ubifs_info *c, in calc_pnode_num_from_parent() argument
582 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0; in calc_pnode_num_from_parent()
586 num |= pnum & (UBIFS_LPT_FANOUT - 1); in calc_pnode_num_from_parent()
595 * ubifs_create_dflt_lpt - create default LPT.
596 * @c: UBIFS file-system description object
604 int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first, in ubifs_create_dflt_lpt() argument
615 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt); in ubifs_create_dflt_lpt()
618 *lpt_lebs = c->lpt_lebs; in ubifs_create_dflt_lpt()
621 c->lpt_first = lpt_first; in ubifs_create_dflt_lpt()
623 c->lpt_last = lpt_first + c->lpt_lebs - 1; in ubifs_create_dflt_lpt()
625 c->main_first = c->leb_cnt - *main_lebs; in ubifs_create_dflt_lpt()
627 lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL); in ubifs_create_dflt_lpt()
630 buf = vmalloc(c->leb_size); in ubifs_create_dflt_lpt()
631 ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in ubifs_create_dflt_lpt()
633 err = -ENOMEM; in ubifs_create_dflt_lpt()
637 ubifs_assert(!c->ltab); in ubifs_create_dflt_lpt()
638 c->ltab = ltab; /* Needed by set_ltab */ in ubifs_create_dflt_lpt()
641 for (i = 0; i < c->lpt_lebs; i++) { in ubifs_create_dflt_lpt()
642 ltab[i].free = c->leb_size; in ubifs_create_dflt_lpt()
651 cnt = c->pnode_cnt; in ubifs_create_dflt_lpt()
657 node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8); in ubifs_create_dflt_lpt()
658 iopos = ALIGN(node_sz, c->min_io_size); in ubifs_create_dflt_lpt()
659 pnode->lprops[0].free = c->leb_size - iopos; in ubifs_create_dflt_lpt()
660 pnode->lprops[0].dirty = iopos - node_sz; in ubifs_create_dflt_lpt()
661 pnode->lprops[0].flags = LPROPS_INDEX; in ubifs_create_dflt_lpt()
664 iopos = ALIGN(node_sz, c->min_io_size); in ubifs_create_dflt_lpt()
665 pnode->lprops[1].free = c->leb_size - iopos; in ubifs_create_dflt_lpt()
666 pnode->lprops[1].dirty = iopos - node_sz; in ubifs_create_dflt_lpt()
669 pnode->lprops[i].free = c->leb_size; in ubifs_create_dflt_lpt()
672 ubifs_pack_pnode(c, p, pnode); in ubifs_create_dflt_lpt()
673 p += c->pnode_sz; in ubifs_create_dflt_lpt()
674 len = c->pnode_sz; in ubifs_create_dflt_lpt()
675 pnode->num += 1; in ubifs_create_dflt_lpt()
678 pnode->lprops[0].free = c->leb_size; in ubifs_create_dflt_lpt()
679 pnode->lprops[0].dirty = 0; in ubifs_create_dflt_lpt()
680 pnode->lprops[0].flags = 0; in ubifs_create_dflt_lpt()
682 pnode->lprops[1].free = c->leb_size; in ubifs_create_dflt_lpt()
683 pnode->lprops[1].dirty = 0; in ubifs_create_dflt_lpt()
692 bsz = c->pnode_sz; /* Size of nodes in level below */ in ubifs_create_dflt_lpt()
696 if (len + c->pnode_sz > c->leb_size) { in ubifs_create_dflt_lpt()
697 alen = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
698 set_ltab(c, lnum, c->leb_size - alen, alen - len); in ubifs_create_dflt_lpt()
699 memset(p, 0xff, alen - len); in ubifs_create_dflt_lpt()
700 err = ubifs_leb_change(c, lnum++, buf, alen); in ubifs_create_dflt_lpt()
706 ubifs_pack_pnode(c, p, pnode); in ubifs_create_dflt_lpt()
707 p += c->pnode_sz; in ubifs_create_dflt_lpt()
708 len += c->pnode_sz; in ubifs_create_dflt_lpt()
714 pnode->num += 1; in ubifs_create_dflt_lpt()
725 if (len + c->nnode_sz > c->leb_size) { in ubifs_create_dflt_lpt()
726 alen = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
727 set_ltab(c, lnum, c->leb_size - alen, in ubifs_create_dflt_lpt()
728 alen - len); in ubifs_create_dflt_lpt()
729 memset(p, 0xff, alen - len); in ubifs_create_dflt_lpt()
730 err = ubifs_leb_change(c, lnum++, buf, alen); in ubifs_create_dflt_lpt()
738 c->lpt_lnum = lnum; in ubifs_create_dflt_lpt()
739 c->lpt_offs = len; in ubifs_create_dflt_lpt()
744 if (boffs + bsz > c->leb_size) { in ubifs_create_dflt_lpt()
748 nnode->nbranch[j].lnum = blnum; in ubifs_create_dflt_lpt()
749 nnode->nbranch[j].offs = boffs; in ubifs_create_dflt_lpt()
751 bcnt--; in ubifs_create_dflt_lpt()
753 nnode->nbranch[j].lnum = 0; in ubifs_create_dflt_lpt()
754 nnode->nbranch[j].offs = 0; in ubifs_create_dflt_lpt()
757 nnode->num = calc_nnode_num(row, i); in ubifs_create_dflt_lpt()
758 ubifs_pack_nnode(c, p, nnode); in ubifs_create_dflt_lpt()
759 p += c->nnode_sz; in ubifs_create_dflt_lpt()
760 len += c->nnode_sz; in ubifs_create_dflt_lpt()
767 bsz = c->nnode_sz; in ubifs_create_dflt_lpt()
768 row -= 1; in ubifs_create_dflt_lpt()
773 if (len + c->lsave_sz > c->leb_size) { in ubifs_create_dflt_lpt()
774 alen = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
775 set_ltab(c, lnum, c->leb_size - alen, alen - len); in ubifs_create_dflt_lpt()
776 memset(p, 0xff, alen - len); in ubifs_create_dflt_lpt()
777 err = ubifs_leb_change(c, lnum++, buf, alen); in ubifs_create_dflt_lpt()
784 c->lsave_lnum = lnum; in ubifs_create_dflt_lpt()
785 c->lsave_offs = len; in ubifs_create_dflt_lpt()
787 for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++) in ubifs_create_dflt_lpt()
788 lsave[i] = c->main_first + i; in ubifs_create_dflt_lpt()
789 for (; i < c->lsave_cnt; i++) in ubifs_create_dflt_lpt()
790 lsave[i] = c->main_first; in ubifs_create_dflt_lpt()
792 ubifs_pack_lsave(c, p, lsave); in ubifs_create_dflt_lpt()
793 p += c->lsave_sz; in ubifs_create_dflt_lpt()
794 len += c->lsave_sz; in ubifs_create_dflt_lpt()
798 if (len + c->ltab_sz > c->leb_size) { in ubifs_create_dflt_lpt()
799 alen = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
800 set_ltab(c, lnum, c->leb_size - alen, alen - len); in ubifs_create_dflt_lpt()
801 memset(p, 0xff, alen - len); in ubifs_create_dflt_lpt()
802 err = ubifs_leb_change(c, lnum++, buf, alen); in ubifs_create_dflt_lpt()
809 c->ltab_lnum = lnum; in ubifs_create_dflt_lpt()
810 c->ltab_offs = len; in ubifs_create_dflt_lpt()
813 len += c->ltab_sz; in ubifs_create_dflt_lpt()
814 alen = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
815 set_ltab(c, lnum, c->leb_size - alen, alen - len); in ubifs_create_dflt_lpt()
817 ubifs_pack_ltab(c, p, ltab); in ubifs_create_dflt_lpt()
818 p += c->ltab_sz; in ubifs_create_dflt_lpt()
821 memset(p, 0xff, alen - len); in ubifs_create_dflt_lpt()
822 err = ubifs_leb_change(c, lnum, buf, alen); in ubifs_create_dflt_lpt()
826 c->nhead_lnum = lnum; in ubifs_create_dflt_lpt()
827 c->nhead_offs = ALIGN(len, c->min_io_size); in ubifs_create_dflt_lpt()
829 dbg_lp("space_bits %d", c->space_bits); in ubifs_create_dflt_lpt()
830 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits); in ubifs_create_dflt_lpt()
831 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits); in ubifs_create_dflt_lpt()
832 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits); in ubifs_create_dflt_lpt()
833 dbg_lp("pcnt_bits %d", c->pcnt_bits); in ubifs_create_dflt_lpt()
834 dbg_lp("lnum_bits %d", c->lnum_bits); in ubifs_create_dflt_lpt()
835 dbg_lp("pnode_sz %d", c->pnode_sz); in ubifs_create_dflt_lpt()
836 dbg_lp("nnode_sz %d", c->nnode_sz); in ubifs_create_dflt_lpt()
837 dbg_lp("ltab_sz %d", c->ltab_sz); in ubifs_create_dflt_lpt()
838 dbg_lp("lsave_sz %d", c->lsave_sz); in ubifs_create_dflt_lpt()
839 dbg_lp("lsave_cnt %d", c->lsave_cnt); in ubifs_create_dflt_lpt()
840 dbg_lp("lpt_hght %d", c->lpt_hght); in ubifs_create_dflt_lpt()
841 dbg_lp("big_lpt %d", c->big_lpt); in ubifs_create_dflt_lpt()
842 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in ubifs_create_dflt_lpt()
843 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in ubifs_create_dflt_lpt()
844 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in ubifs_create_dflt_lpt()
845 if (c->big_lpt) in ubifs_create_dflt_lpt()
846 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in ubifs_create_dflt_lpt()
848 c->ltab = NULL; in ubifs_create_dflt_lpt()
858 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
859 * @c: UBIFS file-system description object
865 static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode) in update_cats() argument
870 int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK; in update_cats()
871 int lnum = pnode->lprops[i].lnum; in update_cats()
875 ubifs_add_to_cat(c, &pnode->lprops[i], cat); in update_cats()
880 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
881 * @c: UBIFS file-system description object
889 static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode, in replace_cats() argument
895 if (!new_pnode->lprops[i].lnum) in replace_cats()
897 ubifs_replace_cat(c, &old_pnode->lprops[i], in replace_cats()
898 &new_pnode->lprops[i]); in replace_cats()
903 * check_lpt_crc - check LPT node crc is correct.
904 * @c: UBIFS file-system description object
910 static int check_lpt_crc(const struct ubifs_info *c, void *buf, int len) in check_lpt_crc() argument
917 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in check_lpt_crc()
918 len - UBIFS_LPT_CRC_BYTES); in check_lpt_crc()
920 ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx", in check_lpt_crc()
923 return -EINVAL; in check_lpt_crc()
929 * check_lpt_type - check LPT node type is correct.
930 * @c: UBIFS file-system description object
937 static int check_lpt_type(const struct ubifs_info *c, uint8_t **addr, in check_lpt_type() argument
944 ubifs_err(c, "invalid type (%d) in LPT node type %d", in check_lpt_type()
947 return -EINVAL; in check_lpt_type()
953 * unpack_pnode - unpack a pnode.
954 * @c: UBIFS file-system description object
960 static int unpack_pnode(const struct ubifs_info *c, void *buf, in unpack_pnode() argument
966 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE); in unpack_pnode()
969 if (c->big_lpt) in unpack_pnode()
970 pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); in unpack_pnode()
972 struct ubifs_lprops * const lprops = &pnode->lprops[i]; in unpack_pnode()
974 lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits); in unpack_pnode()
975 lprops->free <<= 3; in unpack_pnode()
976 lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits); in unpack_pnode()
977 lprops->dirty <<= 3; in unpack_pnode()
980 lprops->flags = LPROPS_INDEX; in unpack_pnode()
982 lprops->flags = 0; in unpack_pnode()
983 lprops->flags |= ubifs_categorize_lprops(c, lprops); in unpack_pnode()
985 err = check_lpt_crc(c, buf, c->pnode_sz); in unpack_pnode()
990 * ubifs_unpack_nnode - unpack a nnode.
991 * @c: UBIFS file-system description object
997 int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf, in ubifs_unpack_nnode() argument
1003 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE); in ubifs_unpack_nnode()
1006 if (c->big_lpt) in ubifs_unpack_nnode()
1007 nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); in ubifs_unpack_nnode()
1011 lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) + in ubifs_unpack_nnode()
1012 c->lpt_first; in ubifs_unpack_nnode()
1013 if (lnum == c->lpt_last + 1) in ubifs_unpack_nnode()
1015 nnode->nbranch[i].lnum = lnum; in ubifs_unpack_nnode()
1016 nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos, in ubifs_unpack_nnode()
1017 c->lpt_offs_bits); in ubifs_unpack_nnode()
1019 err = check_lpt_crc(c, buf, c->nnode_sz); in ubifs_unpack_nnode()
1024 * unpack_ltab - unpack the LPT's own lprops table.
1025 * @c: UBIFS file-system description object
1030 static int unpack_ltab(const struct ubifs_info *c, void *buf) in unpack_ltab() argument
1035 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB); in unpack_ltab()
1038 for (i = 0; i < c->lpt_lebs; i++) { in unpack_ltab()
1039 int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits); in unpack_ltab()
1040 int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits); in unpack_ltab()
1042 if (free < 0 || free > c->leb_size || dirty < 0 || in unpack_ltab()
1043 dirty > c->leb_size || free + dirty > c->leb_size) in unpack_ltab()
1044 return -EINVAL; in unpack_ltab()
1046 c->ltab[i].free = free; in unpack_ltab()
1047 c->ltab[i].dirty = dirty; in unpack_ltab()
1048 c->ltab[i].tgc = 0; in unpack_ltab()
1049 c->ltab[i].cmt = 0; in unpack_ltab()
1051 err = check_lpt_crc(c, buf, c->ltab_sz); in unpack_ltab()
1057 * unpack_lsave - unpack the LPT's save table.
1058 * @c: UBIFS file-system description object
1063 static int unpack_lsave(const struct ubifs_info *c, void *buf) in unpack_lsave() argument
1068 err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LSAVE); in unpack_lsave()
1071 for (i = 0; i < c->lsave_cnt; i++) { in unpack_lsave()
1072 int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits); in unpack_lsave()
1074 if (lnum < c->main_first || lnum >= c->leb_cnt) in unpack_lsave()
1075 return -EINVAL; in unpack_lsave()
1076 c->lsave[i] = lnum; in unpack_lsave()
1078 err = check_lpt_crc(c, buf, c->lsave_sz); in unpack_lsave()
1084 * validate_nnode - validate a nnode.
1085 * @c: UBIFS file-system description object
1092 static int validate_nnode(const struct ubifs_info *c, struct ubifs_nnode *nnode, in validate_nnode() argument
1097 if (c->big_lpt) { in validate_nnode()
1098 int num = calc_nnode_num_from_parent(c, parent, iip); in validate_nnode()
1100 if (nnode->num != num) in validate_nnode()
1101 return -EINVAL; in validate_nnode()
1103 lvl = parent ? parent->level - 1 : c->lpt_hght; in validate_nnode()
1105 return -EINVAL; in validate_nnode()
1107 max_offs = c->leb_size - c->pnode_sz; in validate_nnode()
1109 max_offs = c->leb_size - c->nnode_sz; in validate_nnode()
1111 int lnum = nnode->nbranch[i].lnum; in validate_nnode()
1112 int offs = nnode->nbranch[i].offs; in validate_nnode()
1116 return -EINVAL; in validate_nnode()
1119 if (lnum < c->lpt_first || lnum > c->lpt_last) in validate_nnode()
1120 return -EINVAL; in validate_nnode()
1122 return -EINVAL; in validate_nnode()
1128 * validate_pnode - validate a pnode.
1129 * @c: UBIFS file-system description object
1136 static int validate_pnode(const struct ubifs_info *c, struct ubifs_pnode *pnode, in validate_pnode() argument
1141 if (c->big_lpt) { in validate_pnode()
1142 int num = calc_pnode_num_from_parent(c, parent, iip); in validate_pnode()
1144 if (pnode->num != num) in validate_pnode()
1145 return -EINVAL; in validate_pnode()
1148 int free = pnode->lprops[i].free; in validate_pnode()
1149 int dirty = pnode->lprops[i].dirty; in validate_pnode()
1151 if (free < 0 || free > c->leb_size || free % c->min_io_size || in validate_pnode()
1153 return -EINVAL; in validate_pnode()
1154 if (dirty < 0 || dirty > c->leb_size || (dirty & 7)) in validate_pnode()
1155 return -EINVAL; in validate_pnode()
1156 if (dirty + free > c->leb_size) in validate_pnode()
1157 return -EINVAL; in validate_pnode()
1163 * set_pnode_lnum - set LEB numbers on a pnode.
1164 * @c: UBIFS file-system description object
1170 static void set_pnode_lnum(const struct ubifs_info *c, in set_pnode_lnum() argument
1175 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first; in set_pnode_lnum()
1177 if (lnum >= c->leb_cnt) in set_pnode_lnum()
1179 pnode->lprops[i].lnum = lnum++; in set_pnode_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
1191 int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip) in ubifs_read_nnode() argument
1195 void *buf = c->lpt_nod_buf; in ubifs_read_nnode()
1199 branch = &parent->nbranch[iip]; in ubifs_read_nnode()
1200 lnum = branch->lnum; in ubifs_read_nnode()
1201 offs = branch->offs; in ubifs_read_nnode()
1203 lnum = c->lpt_lnum; in ubifs_read_nnode()
1204 offs = c->lpt_offs; in ubifs_read_nnode()
1208 err = -ENOMEM; in ubifs_read_nnode()
1218 if (c->big_lpt) in ubifs_read_nnode()
1219 nnode->num = calc_nnode_num_from_parent(c, parent, iip); in ubifs_read_nnode()
1221 err = ubifs_leb_read(c, lnum, buf, offs, c->nnode_sz, 1); in ubifs_read_nnode()
1224 err = ubifs_unpack_nnode(c, buf, nnode); in ubifs_read_nnode()
1228 err = validate_nnode(c, nnode, parent, iip); in ubifs_read_nnode()
1231 if (!c->big_lpt) in ubifs_read_nnode()
1232 nnode->num = calc_nnode_num_from_parent(c, parent, iip); in ubifs_read_nnode()
1234 branch->nnode = nnode; in ubifs_read_nnode()
1235 nnode->level = parent->level - 1; in ubifs_read_nnode()
1237 c->nroot = nnode; in ubifs_read_nnode()
1238 nnode->level = c->lpt_hght; in ubifs_read_nnode()
1240 nnode->parent = parent; in ubifs_read_nnode()
1241 nnode->iip = iip; in ubifs_read_nnode()
1245 ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs); in ubifs_read_nnode()
1252 * read_pnode - read a pnode from flash and link it to the tree in memory.
1253 * @c: UBIFS file-system description object
1259 static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip) in read_pnode() argument
1263 void *buf = c->lpt_nod_buf; in read_pnode()
1266 branch = &parent->nbranch[iip]; in read_pnode()
1267 lnum = branch->lnum; in read_pnode()
1268 offs = branch->offs; in read_pnode()
1271 return -ENOMEM; in read_pnode()
1281 if (c->big_lpt) in read_pnode()
1282 pnode->num = calc_pnode_num_from_parent(c, parent, iip); in read_pnode()
1284 struct ubifs_lprops * const lprops = &pnode->lprops[i]; in read_pnode()
1286 lprops->free = c->leb_size; in read_pnode()
1287 lprops->flags = ubifs_categorize_lprops(c, lprops); in read_pnode()
1290 err = ubifs_leb_read(c, lnum, buf, offs, c->pnode_sz, 1); in read_pnode()
1293 err = unpack_pnode(c, buf, pnode); in read_pnode()
1297 err = validate_pnode(c, pnode, parent, iip); in read_pnode()
1300 if (!c->big_lpt) in read_pnode()
1301 pnode->num = calc_pnode_num_from_parent(c, parent, iip); in read_pnode()
1302 branch->pnode = pnode; in read_pnode()
1303 pnode->parent = parent; in read_pnode()
1304 pnode->iip = iip; in read_pnode()
1305 set_pnode_lnum(c, pnode); in read_pnode()
1306 c->pnodes_have += 1; in read_pnode()
1310 ubifs_err(c, "error %d reading pnode at %d:%d", err, lnum, offs); in read_pnode()
1311 ubifs_dump_pnode(c, pnode, parent, iip); in read_pnode()
1313 ubifs_err(c, "calc num: %d", calc_pnode_num_from_parent(c, parent, iip)); in read_pnode()
1319 * read_ltab - read LPT's own lprops table.
1320 * @c: UBIFS file-system description object
1324 static int read_ltab(struct ubifs_info *c) in read_ltab() argument
1329 buf = vmalloc(c->ltab_sz); in read_ltab()
1331 return -ENOMEM; in read_ltab()
1332 err = ubifs_leb_read(c, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz, 1); in read_ltab()
1335 err = unpack_ltab(c, buf); in read_ltab()
1343 * read_lsave - read LPT's save table.
1344 * @c: UBIFS file-system description object
1348 static int read_lsave(struct ubifs_info *c) in read_lsave() argument
1353 buf = vmalloc(c->lsave_sz); in read_lsave()
1355 return -ENOMEM; in read_lsave()
1356 err = ubifs_leb_read(c, c->lsave_lnum, buf, c->lsave_offs, in read_lsave()
1357 c->lsave_sz, 1); in read_lsave()
1360 err = unpack_lsave(c, buf); in read_lsave()
1363 for (i = 0; i < c->lsave_cnt; i++) { in read_lsave()
1364 int lnum = c->lsave[i]; in read_lsave()
1369 * could be beyond the volume size - just ignore them. in read_lsave()
1371 if (lnum >= c->leb_cnt) in read_lsave()
1373 lprops = ubifs_lpt_lookup(c, lnum); in read_lsave()
1386 * ubifs_get_nnode - get a nnode.
1387 * @c: UBIFS file-system description object
1394 struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c, in ubifs_get_nnode() argument
1401 branch = &parent->nbranch[iip]; in ubifs_get_nnode()
1402 nnode = branch->nnode; in ubifs_get_nnode()
1405 err = ubifs_read_nnode(c, parent, iip); in ubifs_get_nnode()
1408 return branch->nnode; in ubifs_get_nnode()
1412 * ubifs_get_pnode - get a pnode.
1413 * @c: UBIFS file-system description object
1420 struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c, in ubifs_get_pnode() argument
1427 branch = &parent->nbranch[iip]; in ubifs_get_pnode()
1428 pnode = branch->pnode; in ubifs_get_pnode()
1431 err = read_pnode(c, parent, iip); in ubifs_get_pnode()
1434 update_cats(c, branch->pnode); in ubifs_get_pnode()
1435 return branch->pnode; in ubifs_get_pnode()
1439 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1440 * @c: UBIFS file-system description object
1446 struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum) in ubifs_lpt_lookup() argument
1452 if (!c->nroot) { in ubifs_lpt_lookup()
1453 err = ubifs_read_nnode(c, NULL, 0); in ubifs_lpt_lookup()
1457 nnode = c->nroot; in ubifs_lpt_lookup()
1458 i = lnum - c->main_first; in ubifs_lpt_lookup()
1459 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_lookup()
1460 for (h = 1; h < c->lpt_hght; h++) { in ubifs_lpt_lookup()
1461 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup()
1462 shft -= UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_lookup()
1463 nnode = ubifs_get_nnode(c, nnode, iip); in ubifs_lpt_lookup()
1467 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup()
1468 pnode = ubifs_get_pnode(c, nnode, iip); in ubifs_lpt_lookup()
1471 iip = (i & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup()
1473 pnode->lprops[iip].free, pnode->lprops[iip].dirty, in ubifs_lpt_lookup()
1474 pnode->lprops[iip].flags); in ubifs_lpt_lookup()
1475 return &pnode->lprops[iip]; in ubifs_lpt_lookup()
1479 * dirty_cow_nnode - ensure a nnode is not being committed.
1480 * @c: UBIFS file-system description object
1485 static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c, in dirty_cow_nnode() argument
1491 if (!test_bit(COW_CNODE, &nnode->flags)) { in dirty_cow_nnode()
1493 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in dirty_cow_nnode()
1494 c->dirty_nn_cnt += 1; in dirty_cow_nnode()
1495 ubifs_add_nnode_dirt(c, nnode); in dirty_cow_nnode()
1503 return ERR_PTR(-ENOMEM); in dirty_cow_nnode()
1506 n->cnext = NULL; in dirty_cow_nnode()
1507 __set_bit(DIRTY_CNODE, &n->flags); in dirty_cow_nnode()
1508 __clear_bit(COW_CNODE, &n->flags); in dirty_cow_nnode()
1512 struct ubifs_nbranch *branch = &n->nbranch[i]; in dirty_cow_nnode()
1514 if (branch->cnode) in dirty_cow_nnode()
1515 branch->cnode->parent = n; in dirty_cow_nnode()
1518 ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags)); in dirty_cow_nnode()
1519 __set_bit(OBSOLETE_CNODE, &nnode->flags); in dirty_cow_nnode()
1521 c->dirty_nn_cnt += 1; in dirty_cow_nnode()
1522 ubifs_add_nnode_dirt(c, nnode); in dirty_cow_nnode()
1523 if (nnode->parent) in dirty_cow_nnode()
1524 nnode->parent->nbranch[n->iip].nnode = n; in dirty_cow_nnode()
1526 c->nroot = n; in dirty_cow_nnode()
1531 * dirty_cow_pnode - ensure a pnode is not being committed.
1532 * @c: UBIFS file-system description object
1537 static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c, in dirty_cow_pnode() argument
1542 if (!test_bit(COW_CNODE, &pnode->flags)) { in dirty_cow_pnode()
1544 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { in dirty_cow_pnode()
1545 c->dirty_pn_cnt += 1; in dirty_cow_pnode()
1546 add_pnode_dirt(c, pnode); in dirty_cow_pnode()
1554 return ERR_PTR(-ENOMEM); in dirty_cow_pnode()
1557 p->cnext = NULL; in dirty_cow_pnode()
1558 __set_bit(DIRTY_CNODE, &p->flags); in dirty_cow_pnode()
1559 __clear_bit(COW_CNODE, &p->flags); in dirty_cow_pnode()
1560 replace_cats(c, pnode, p); in dirty_cow_pnode()
1562 ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags)); in dirty_cow_pnode()
1563 __set_bit(OBSOLETE_CNODE, &pnode->flags); in dirty_cow_pnode()
1565 c->dirty_pn_cnt += 1; in dirty_cow_pnode()
1566 add_pnode_dirt(c, pnode); in dirty_cow_pnode()
1567 pnode->parent->nbranch[p->iip].pnode = p; in dirty_cow_pnode()
1572 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1573 * @c: UBIFS file-system description object
1579 struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum) in ubifs_lpt_lookup_dirty() argument
1585 if (!c->nroot) { in ubifs_lpt_lookup_dirty()
1586 err = ubifs_read_nnode(c, NULL, 0); in ubifs_lpt_lookup_dirty()
1590 nnode = c->nroot; in ubifs_lpt_lookup_dirty()
1591 nnode = dirty_cow_nnode(c, nnode); in ubifs_lpt_lookup_dirty()
1594 i = lnum - c->main_first; in ubifs_lpt_lookup_dirty()
1595 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_lookup_dirty()
1596 for (h = 1; h < c->lpt_hght; h++) { in ubifs_lpt_lookup_dirty()
1597 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup_dirty()
1598 shft -= UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_lookup_dirty()
1599 nnode = ubifs_get_nnode(c, nnode, iip); in ubifs_lpt_lookup_dirty()
1602 nnode = dirty_cow_nnode(c, nnode); in ubifs_lpt_lookup_dirty()
1606 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup_dirty()
1607 pnode = ubifs_get_pnode(c, nnode, iip); in ubifs_lpt_lookup_dirty()
1610 pnode = dirty_cow_pnode(c, pnode); in ubifs_lpt_lookup_dirty()
1613 iip = (i & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_lookup_dirty()
1615 pnode->lprops[iip].free, pnode->lprops[iip].dirty, in ubifs_lpt_lookup_dirty()
1616 pnode->lprops[iip].flags); in ubifs_lpt_lookup_dirty()
1617 ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags)); in ubifs_lpt_lookup_dirty()
1618 return &pnode->lprops[iip]; in ubifs_lpt_lookup_dirty()
1622 * lpt_init_rd - initialize the LPT for reading.
1623 * @c: UBIFS file-system description object
1627 static int lpt_init_rd(struct ubifs_info *c) in lpt_init_rd() argument
1631 c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in lpt_init_rd()
1632 if (!c->ltab) in lpt_init_rd()
1633 return -ENOMEM; in lpt_init_rd()
1635 i = max_t(int, c->nnode_sz, c->pnode_sz); in lpt_init_rd()
1636 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL); in lpt_init_rd()
1637 if (!c->lpt_nod_buf) in lpt_init_rd()
1638 return -ENOMEM; in lpt_init_rd()
1641 c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, in lpt_init_rd()
1643 if (!c->lpt_heap[i].arr) in lpt_init_rd()
1644 return -ENOMEM; in lpt_init_rd()
1645 c->lpt_heap[i].cnt = 0; in lpt_init_rd()
1646 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ; in lpt_init_rd()
1649 c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL); in lpt_init_rd()
1650 if (!c->dirty_idx.arr) in lpt_init_rd()
1651 return -ENOMEM; in lpt_init_rd()
1652 c->dirty_idx.cnt = 0; in lpt_init_rd()
1653 c->dirty_idx.max_cnt = LPT_HEAP_SZ; in lpt_init_rd()
1655 err = read_ltab(c); in lpt_init_rd()
1659 dbg_lp("space_bits %d", c->space_bits); in lpt_init_rd()
1660 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits); in lpt_init_rd()
1661 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits); in lpt_init_rd()
1662 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits); in lpt_init_rd()
1663 dbg_lp("pcnt_bits %d", c->pcnt_bits); in lpt_init_rd()
1664 dbg_lp("lnum_bits %d", c->lnum_bits); in lpt_init_rd()
1665 dbg_lp("pnode_sz %d", c->pnode_sz); in lpt_init_rd()
1666 dbg_lp("nnode_sz %d", c->nnode_sz); in lpt_init_rd()
1667 dbg_lp("ltab_sz %d", c->ltab_sz); in lpt_init_rd()
1668 dbg_lp("lsave_sz %d", c->lsave_sz); in lpt_init_rd()
1669 dbg_lp("lsave_cnt %d", c->lsave_cnt); in lpt_init_rd()
1670 dbg_lp("lpt_hght %d", c->lpt_hght); in lpt_init_rd()
1671 dbg_lp("big_lpt %d", c->big_lpt); in lpt_init_rd()
1672 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in lpt_init_rd()
1673 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in lpt_init_rd()
1674 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in lpt_init_rd()
1675 if (c->big_lpt) in lpt_init_rd()
1676 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in lpt_init_rd()
1683 * lpt_init_wr - initialize the LPT for writing.
1684 * @c: UBIFS file-system description object
1690 static int lpt_init_wr(struct ubifs_info *c) in lpt_init_wr() argument
1694 c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in lpt_init_wr()
1695 if (!c->ltab_cmt) in lpt_init_wr()
1696 return -ENOMEM; in lpt_init_wr()
1698 c->lpt_buf = vmalloc(c->leb_size); in lpt_init_wr()
1699 if (!c->lpt_buf) in lpt_init_wr()
1700 return -ENOMEM; in lpt_init_wr()
1702 if (c->big_lpt) { in lpt_init_wr()
1703 c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS); in lpt_init_wr()
1704 if (!c->lsave) in lpt_init_wr()
1705 return -ENOMEM; in lpt_init_wr()
1706 err = read_lsave(c); in lpt_init_wr()
1711 for (i = 0; i < c->lpt_lebs; i++) in lpt_init_wr()
1712 if (c->ltab[i].free == c->leb_size) { in lpt_init_wr()
1713 err = ubifs_leb_unmap(c, i + c->lpt_first); in lpt_init_wr()
1723 * ubifs_lpt_init - initialize the LPT.
1724 * @c: UBIFS file-system description object
1734 int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr) in ubifs_lpt_init() argument
1739 err = lpt_init_rd(c); in ubifs_lpt_init()
1746 err = lpt_init_wr(c); in ubifs_lpt_init()
1757 ubifs_lpt_free(c, 1); in ubifs_lpt_init()
1760 ubifs_lpt_free(c, 0); in ubifs_lpt_init()
1765 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1790 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1791 * @c: the UBIFS file-system description object
1799 static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c, in scan_get_nnode() argument
1805 void *buf = c->lpt_nod_buf; in scan_get_nnode()
1808 branch = &parent->nbranch[iip]; in scan_get_nnode()
1809 nnode = branch->nnode; in scan_get_nnode()
1811 path->in_tree = 1; in scan_get_nnode()
1812 path->ptr.nnode = nnode; in scan_get_nnode()
1815 nnode = &path->nnode; in scan_get_nnode()
1816 path->in_tree = 0; in scan_get_nnode()
1817 path->ptr.nnode = nnode; in scan_get_nnode()
1819 if (branch->lnum == 0) { in scan_get_nnode()
1826 if (c->big_lpt) in scan_get_nnode()
1827 nnode->num = calc_nnode_num_from_parent(c, parent, iip); in scan_get_nnode()
1829 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs, in scan_get_nnode()
1830 c->nnode_sz, 1); in scan_get_nnode()
1833 err = ubifs_unpack_nnode(c, buf, nnode); in scan_get_nnode()
1837 err = validate_nnode(c, nnode, parent, iip); in scan_get_nnode()
1840 if (!c->big_lpt) in scan_get_nnode()
1841 nnode->num = calc_nnode_num_from_parent(c, parent, iip); in scan_get_nnode()
1842 nnode->level = parent->level - 1; in scan_get_nnode()
1843 nnode->parent = parent; in scan_get_nnode()
1844 nnode->iip = iip; in scan_get_nnode()
1849 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1850 * @c: the UBIFS file-system description object
1858 static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c, in scan_get_pnode() argument
1864 void *buf = c->lpt_nod_buf; in scan_get_pnode()
1867 branch = &parent->nbranch[iip]; in scan_get_pnode()
1868 pnode = branch->pnode; in scan_get_pnode()
1870 path->in_tree = 1; in scan_get_pnode()
1871 path->ptr.pnode = pnode; in scan_get_pnode()
1874 pnode = &path->pnode; in scan_get_pnode()
1875 path->in_tree = 0; in scan_get_pnode()
1876 path->ptr.pnode = pnode; in scan_get_pnode()
1878 if (branch->lnum == 0) { in scan_get_pnode()
1886 if (c->big_lpt) in scan_get_pnode()
1887 pnode->num = calc_pnode_num_from_parent(c, parent, iip); in scan_get_pnode()
1889 struct ubifs_lprops * const lprops = &pnode->lprops[i]; in scan_get_pnode()
1891 lprops->free = c->leb_size; in scan_get_pnode()
1892 lprops->flags = ubifs_categorize_lprops(c, lprops); in scan_get_pnode()
1895 ubifs_assert(branch->lnum >= c->lpt_first && in scan_get_pnode()
1896 branch->lnum <= c->lpt_last); in scan_get_pnode()
1897 ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size); in scan_get_pnode()
1898 err = ubifs_leb_read(c, branch->lnum, buf, branch->offs, in scan_get_pnode()
1899 c->pnode_sz, 1); in scan_get_pnode()
1902 err = unpack_pnode(c, buf, pnode); in scan_get_pnode()
1906 err = validate_pnode(c, pnode, parent, iip); in scan_get_pnode()
1909 if (!c->big_lpt) in scan_get_pnode()
1910 pnode->num = calc_pnode_num_from_parent(c, parent, iip); in scan_get_pnode()
1911 pnode->parent = parent; in scan_get_pnode()
1912 pnode->iip = iip; in scan_get_pnode()
1913 set_pnode_lnum(c, pnode); in scan_get_pnode()
1918 * ubifs_lpt_scan_nolock - scan the LPT.
1919 * @c: the UBIFS file-system description object
1927 int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum, in ubifs_lpt_scan_nolock() argument
1935 if (start_lnum == -1) { in ubifs_lpt_scan_nolock()
1937 if (start_lnum >= c->leb_cnt) in ubifs_lpt_scan_nolock()
1938 start_lnum = c->main_first; in ubifs_lpt_scan_nolock()
1941 ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt); in ubifs_lpt_scan_nolock()
1942 ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt); in ubifs_lpt_scan_nolock()
1944 if (!c->nroot) { in ubifs_lpt_scan_nolock()
1945 err = ubifs_read_nnode(c, NULL, 0); in ubifs_lpt_scan_nolock()
1950 path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1), in ubifs_lpt_scan_nolock()
1953 return -ENOMEM; in ubifs_lpt_scan_nolock()
1955 path[0].ptr.nnode = c->nroot; in ubifs_lpt_scan_nolock()
1959 nnode = c->nroot; in ubifs_lpt_scan_nolock()
1960 i = start_lnum - c->main_first; in ubifs_lpt_scan_nolock()
1961 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_scan_nolock()
1962 for (h = 1; h < c->lpt_hght; h++) { in ubifs_lpt_scan_nolock()
1963 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_scan_nolock()
1964 shft -= UBIFS_LPT_FANOUT_SHIFT; in ubifs_lpt_scan_nolock()
1965 nnode = scan_get_nnode(c, path + h, nnode, iip); in ubifs_lpt_scan_nolock()
1971 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_scan_nolock()
1972 pnode = scan_get_pnode(c, path + h, nnode, iip); in ubifs_lpt_scan_nolock()
1977 iip = (i & (UBIFS_LPT_FANOUT - 1)); in ubifs_lpt_scan_nolock()
1981 struct ubifs_lprops *lprops = &pnode->lprops[iip]; in ubifs_lpt_scan_nolock()
1982 int ret, lnum = lprops->lnum; in ubifs_lpt_scan_nolock()
1984 ret = scan_cb(c, lprops, path[h].in_tree, data); in ubifs_lpt_scan_nolock()
1991 for (h = 1; h < c->lpt_hght; h++) { in ubifs_lpt_scan_nolock()
1999 err = -ENOMEM; in ubifs_lpt_scan_nolock()
2002 parent = nnode->parent; in ubifs_lpt_scan_nolock()
2003 parent->nbranch[nnode->iip].nnode = nnode; in ubifs_lpt_scan_nolock()
2009 ubifs_ensure_cat(c, lprops); in ubifs_lpt_scan_nolock()
2016 err = -ENOMEM; in ubifs_lpt_scan_nolock()
2019 parent = pnode->parent; in ubifs_lpt_scan_nolock()
2020 parent->nbranch[pnode->iip].pnode = pnode; in ubifs_lpt_scan_nolock()
2023 update_cats(c, pnode); in ubifs_lpt_scan_nolock()
2024 c->pnodes_have += 1; in ubifs_lpt_scan_nolock()
2026 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *) in ubifs_lpt_scan_nolock()
2027 c->nroot, 0, 0); in ubifs_lpt_scan_nolock()
2030 err = dbg_check_cats(c); in ubifs_lpt_scan_nolock()
2044 err = -ENOSPC; in ubifs_lpt_scan_nolock()
2047 if (lnum + 1 >= c->leb_cnt) { in ubifs_lpt_scan_nolock()
2048 /* Wrap-around to the beginning */ in ubifs_lpt_scan_nolock()
2049 start_lnum = c->main_first; in ubifs_lpt_scan_nolock()
2058 iip = pnode->iip; in ubifs_lpt_scan_nolock()
2060 h -= 1; in ubifs_lpt_scan_nolock()
2065 iip = nnode->iip; in ubifs_lpt_scan_nolock()
2071 for (; h < c->lpt_hght; h++) { in ubifs_lpt_scan_nolock()
2072 nnode = scan_get_nnode(c, path + h, nnode, iip); in ubifs_lpt_scan_nolock()
2079 pnode = scan_get_pnode(c, path + h, nnode, iip); in ubifs_lpt_scan_nolock()
2092 * dbg_chk_pnode - check a pnode.
2093 * @c: the UBIFS file-system description object
2099 static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, in dbg_chk_pnode() argument
2104 if (pnode->num != col) { in dbg_chk_pnode()
2105 ubifs_err(c, "pnode num %d expected %d parent num %d iip %d", in dbg_chk_pnode()
2106 pnode->num, col, pnode->parent->num, pnode->iip); in dbg_chk_pnode()
2107 return -EINVAL; in dbg_chk_pnode()
2110 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i]; in dbg_chk_pnode()
2111 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i + in dbg_chk_pnode()
2112 c->main_first; in dbg_chk_pnode()
2113 int found, cat = lprops->flags & LPROPS_CAT_MASK; in dbg_chk_pnode()
2117 if (lnum >= c->leb_cnt) in dbg_chk_pnode()
2119 if (lprops->lnum != lnum) { in dbg_chk_pnode()
2120 ubifs_err(c, "bad LEB number %d expected %d", in dbg_chk_pnode()
2121 lprops->lnum, lnum); in dbg_chk_pnode()
2122 return -EINVAL; in dbg_chk_pnode()
2124 if (lprops->flags & LPROPS_TAKEN) { in dbg_chk_pnode()
2126 ubifs_err(c, "LEB %d taken but not uncat %d", in dbg_chk_pnode()
2127 lprops->lnum, cat); in dbg_chk_pnode()
2128 return -EINVAL; in dbg_chk_pnode()
2132 if (lprops->flags & LPROPS_INDEX) { in dbg_chk_pnode()
2139 ubifs_err(c, "LEB %d index but cat %d", in dbg_chk_pnode()
2140 lprops->lnum, cat); in dbg_chk_pnode()
2141 return -EINVAL; in dbg_chk_pnode()
2152 ubifs_err(c, "LEB %d not index but cat %d", in dbg_chk_pnode()
2153 lprops->lnum, cat); in dbg_chk_pnode()
2154 return -EINVAL; in dbg_chk_pnode()
2159 list = &c->uncat_list; in dbg_chk_pnode()
2162 list = &c->empty_list; in dbg_chk_pnode()
2165 list = &c->freeable_list; in dbg_chk_pnode()
2168 list = &c->frdi_idx_list; in dbg_chk_pnode()
2176 heap = &c->lpt_heap[cat - 1]; in dbg_chk_pnode()
2177 if (lprops->hpos < heap->cnt && in dbg_chk_pnode()
2178 heap->arr[lprops->hpos] == lprops) in dbg_chk_pnode()
2193 ubifs_err(c, "LEB %d cat %d not found in cat heap/list", in dbg_chk_pnode()
2194 lprops->lnum, cat); in dbg_chk_pnode()
2195 return -EINVAL; in dbg_chk_pnode()
2199 if (lprops->free != c->leb_size) { in dbg_chk_pnode()
2200 ubifs_err(c, "LEB %d cat %d free %d dirty %d", in dbg_chk_pnode()
2201 lprops->lnum, cat, lprops->free, in dbg_chk_pnode()
2202 lprops->dirty); in dbg_chk_pnode()
2203 return -EINVAL; in dbg_chk_pnode()
2208 if (lprops->free + lprops->dirty != c->leb_size) { in dbg_chk_pnode()
2209 ubifs_err(c, "LEB %d cat %d free %d dirty %d", in dbg_chk_pnode()
2210 lprops->lnum, cat, lprops->free, in dbg_chk_pnode()
2211 lprops->dirty); in dbg_chk_pnode()
2212 return -EINVAL; in dbg_chk_pnode()
2221 * dbg_check_lpt_nodes - check nnodes and pnodes.
2222 * @c: the UBIFS file-system description object
2229 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode, in dbg_check_lpt_nodes() argument
2236 if (!dbg_is_chk_lprops(c)) in dbg_check_lpt_nodes()
2241 nnode = cnode->parent; in dbg_check_lpt_nodes()
2242 if (cnode->level) { in dbg_check_lpt_nodes()
2245 if (cnode->num != num) { in dbg_check_lpt_nodes()
2246 ubifs_err(c, "nnode num %d expected %d parent num %d iip %d", in dbg_check_lpt_nodes()
2247 cnode->num, num, in dbg_check_lpt_nodes()
2248 (nnode ? nnode->num : 0), cnode->iip); in dbg_check_lpt_nodes()
2249 return -EINVAL; in dbg_check_lpt_nodes()
2253 cn = nn->nbranch[iip].cnode; in dbg_check_lpt_nodes()
2273 err = dbg_chk_pnode(c, pnode, col); in dbg_check_lpt_nodes()
2278 row -= 1; in dbg_check_lpt_nodes()
2280 iip = cnode->iip + 1; in dbg_check_lpt_nodes()