49 #include <linux/slab.h>
60 int i,
n,
bits, per_leb_wastage, max_pnode_cnt;
61 long long sz, tot_wastage;
68 while (n < max_pnode_cnt) {
120 sz += per_leb_wastage;
121 tot_wastage = per_leb_wastage;
123 sz += per_leb_wastage;
125 tot_wastage += per_leb_wastage;
174 static int calc_dflt_lpt_geom(
struct ubifs_info *c,
int *main_lebs,
203 for (i = 0; i < 64 ; i++) {
247 *++p = (
uint8_t)(val >>= (8 - b));
285 const int k = 32 - nrbits;
289 const int bytes = (nrbits + b + 7) >> 3;
304 val = p[1] | ((
uint32_t)p[2] << 8) |
308 val = p[1] | ((
uint32_t)p[2] << 8) |
324 val = p[0] | ((
uint32_t)p[1] << 8) |
328 val = p[0] | ((
uint32_t)p[1] << 8) |
361 pack_bits(&addr, &pos, pnode->
lprops[i].free >> 3,
363 pack_bits(&addr, &pos, pnode->
lprops[i].dirty >> 3,
366 pack_bits(&addr, &pos, 1, 1);
368 pack_bits(&addr, &pos, 0, 1);
370 crc =
crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
399 pack_bits(&addr, &pos, nnode->
nbranch[i].offs,
402 crc =
crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
427 crc =
crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
428 c->
ltab_sz - UBIFS_LPT_CRC_BYTES);
448 pack_bits(&addr, &pos, lsave[i], c->
lnum_bits);
449 crc =
crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
466 dbg_lp(
"LEB %d add %d to %d",
481 dbg_lp(
"LEB %d free %d dirty %d to %d %d",
532 static int calc_nnode_num(
int row,
int col)
538 bits = (col & (UBIFS_LPT_FANOUT - 1));
558 static int calc_nnode_num_from_parent(
const struct ubifs_info *c,
566 num = parent->
num ^ (1 << shft);
567 num |= (UBIFS_LPT_FANOUT +
iip) << shft;
583 static int calc_pnode_num_from_parent(
const struct ubifs_info *c,
586 int i, n = c->
lpt_hght - 1, pnum = parent->
num, num = 0;
588 for (i = 0; i <
n; i++) {
590 num |= pnum & (UBIFS_LPT_FANOUT - 1);
609 int *lpt_lebs,
int *big_lpt)
612 int blnum, boffs, bsz, bcnt;
619 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
636 if (!pnode || !nnode || !buf || !ltab || !lsave) {
683 pnode->
lprops[0].dirty = 0;
684 pnode->
lprops[0].flags = 0;
687 pnode->
lprops[1].dirty = 0;
699 for (i = 1; i <
cnt; i++) {
702 set_ltab(c, lnum, c->
leb_size - alen, alen - len);
703 memset(p, 0xff, alen - len);
728 for (i = 0; i <
cnt; i++) {
731 set_ltab(c, lnum, c->
leb_size - alen,
733 memset(p, 0xff, alen - len);
761 nnode->
num = calc_nnode_num(row, i);
779 set_ltab(c, lnum, c->
leb_size - alen, alen - len);
780 memset(p, 0xff, alen - len);
791 for (i = 0; i < c->
lsave_cnt && i < *main_lebs; i++)
804 set_ltab(c, lnum, c->
leb_size - alen, alen - len);
805 memset(p, 0xff, alen - len);
819 set_ltab(c, lnum, c->
leb_size - alen, alen - len);
825 memset(p, 0xff, alen - len);
875 int lnum = pnode->
lprops[
i].lnum;
899 if (!new_pnode->
lprops[i].lnum)
914 static int check_lpt_crc(
void *
buf,
int len)
923 if (crc != calc_crc) {
924 ubifs_err(
"invalid crc in LPT node: crc %hx calc %hx", crc,
941 static int check_lpt_type(
uint8_t **addr,
int *pos,
int type)
946 if (node_type != type) {
947 ubifs_err(
"invalid type (%d) in LPT node type %d", node_type,
963 static int unpack_pnode(
const struct ubifs_info *c,
void *buf,
1004 int i, pos = 0,
err;
1033 static int unpack_ltab(
const struct ubifs_info *c,
void *buf)
1036 int i, pos = 0,
err;
1041 for (i = 0; i < c->
lpt_lebs; i++) {
1045 if (free < 0 || free > c->
leb_size || dirty < 0 ||
1065 static int unpack_lsave(
const struct ubifs_info *c,
void *buf)
1068 int i, pos = 0,
err;
1076 if (lnum < c->main_first || lnum >= c->
leb_cnt)
1096 int i, lvl, max_offs;
1099 int num = calc_nnode_num_from_parent(c, parent, iip);
1101 if (nnode->
num != num)
1120 if (lnum < c->lpt_first || lnum > c->
lpt_last)
1122 if (offs < 0 || offs > max_offs)
1143 int num = calc_pnode_num_from_parent(c, parent, iip);
1145 if (pnode->
num != num)
1155 if (dirty < 0 || dirty > c->
leb_size || (dirty & 7))
1171 static void set_pnode_lnum(
const struct ubifs_info *c,
1180 pnode->
lprops[
i].lnum = lnum++;
1201 lnum = branch->
lnum;
1202 offs = branch->
offs;
1220 nnode->
num = calc_nnode_num_from_parent(c, parent, iip);
1229 err = validate_nnode(c, nnode, parent, iip);
1233 nnode->
num = calc_nnode_num_from_parent(c, parent, iip);
1235 branch->
nnode = nnode;
1246 ubifs_err(
"error %d reading nnode at %d:%d", err, lnum, offs);
1268 lnum = branch->
lnum;
1269 offs = branch->
offs;
1283 pnode->
num = calc_pnode_num_from_parent(c, parent, iip);
1294 err = unpack_pnode(c, buf, pnode);
1298 err = validate_pnode(c, pnode, parent, iip);
1302 pnode->
num = calc_pnode_num_from_parent(c, parent, iip);
1303 branch->
pnode = pnode;
1306 set_pnode_lnum(c, pnode);
1311 ubifs_err(
"error %d reading pnode at %d:%d", err, lnum, offs);
1314 ubifs_err(
"calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1336 err = unpack_ltab(c, buf);
1360 err = unpack_lsave(c, buf);
1374 if (IS_ERR(lprops)) {
1375 err = PTR_ERR(lprops);
1401 nnode = branch->
nnode;
1406 return ERR_PTR(err);
1407 return branch->
nnode;
1427 pnode = branch->
pnode;
1430 err = read_pnode(c, parent, iip);
1432 return ERR_PTR(err);
1433 update_cats(c, branch->
pnode);
1434 return branch->
pnode;
1447 int err,
i,
h, iip, shft;
1454 return ERR_PTR(err);
1459 for (h = 1; h < c->
lpt_hght; h++) {
1460 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1464 return ERR_CAST(nnode);
1466 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1470 return ERR_CAST(pnode);
1471 iip = (i & (UBIFS_LPT_FANOUT - 1));
1472 dbg_lp(
"LEB %d, free %d, dirty %d, flags %d", lnum,
1474 pnode->
lprops[iip].flags);
1515 branch->
cnode->parent =
n;
1546 add_pnode_dirt(c, pnode);
1560 replace_cats(c, pnode, p);
1566 add_pnode_dirt(c, pnode);
1581 int err,
i,
h, iip, shft;
1588 return ERR_PTR(err);
1591 nnode = dirty_cow_nnode(c, nnode);
1593 return ERR_CAST(nnode);
1596 for (h = 1; h < c->
lpt_hght; h++) {
1597 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1601 return ERR_CAST(nnode);
1602 nnode = dirty_cow_nnode(c, nnode);
1604 return ERR_CAST(nnode);
1606 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1610 return ERR_CAST(pnode);
1611 pnode = dirty_cow_pnode(c, pnode);
1613 return ERR_CAST(pnode);
1614 iip = (i & (UBIFS_LPT_FANOUT - 1));
1615 dbg_lp(
"LEB %d, free %d, dirty %d, flags %d", lnum,
1617 pnode->
lprops[iip].flags);
1706 err = read_lsave(c);
1738 err = lpt_init_rd(c);
1744 err = lpt_init_wr(c);
1804 nnode = branch->
nnode;
1810 nnode = &path->
nnode;
1814 if (branch->
lnum == 0) {
1822 nnode->
num = calc_nnode_num_from_parent(c, parent, iip);
1827 return ERR_PTR(err);
1830 return ERR_PTR(err);
1832 err = validate_nnode(c, nnode, parent, iip);
1834 return ERR_PTR(err);
1836 nnode->
num = calc_nnode_num_from_parent(c, parent, iip);
1863 pnode = branch->
pnode;
1869 pnode = &path->
pnode;
1873 if (branch->
lnum == 0) {
1882 pnode->
num = calc_pnode_num_from_parent(c, parent, iip);
1896 return ERR_PTR(err);
1897 err = unpack_pnode(c, buf, pnode);
1899 return ERR_PTR(err);
1901 err = validate_pnode(c, pnode, parent, iip);
1903 return ERR_PTR(err);
1905 pnode->
num = calc_pnode_num_from_parent(c, parent, iip);
1908 set_pnode_lnum(c, pnode);
1925 int err = 0,
i,
h, iip, shft;
1930 if (start_lnum == -1) {
1931 start_lnum = end_lnum + 1;
1957 for (h = 1; h < c->
lpt_hght; h++) {
1958 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1960 nnode = scan_get_nnode(c, path + h, nnode, iip);
1961 if (IS_ERR(nnode)) {
1962 err = PTR_ERR(nnode);
1966 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1968 pnode = scan_get_pnode(c, path + h, nnode, iip);
1969 if (IS_ERR(pnode)) {
1970 err = PTR_ERR(pnode);
1973 iip = (i & (UBIFS_LPT_FANOUT - 1));
1980 ret = scan_cb(c, lprops, path[h].in_tree, data);
1987 for (h = 1; h < c->
lpt_hght; h++) {
1991 if (path[h].in_tree)
1999 parent->
nbranch[nnode->iip].nnode = nnode;
2002 path[h + 1].
cnode.parent = nnode;
2004 if (path[h].in_tree)
2016 parent->
nbranch[pnode->iip].pnode = pnode;
2019 update_cats(c, pnode);
2035 if (lnum == end_lnum) {
2048 if (iip + 1 < UBIFS_LPT_FANOUT) {
2059 if (iip + 1 < UBIFS_LPT_FANOUT)
2068 nnode = scan_get_nnode(c, path + h, nnode, iip);
2069 if (IS_ERR(nnode)) {
2070 err = PTR_ERR(nnode);
2075 pnode = scan_get_pnode(c, path + h, nnode, iip);
2076 if (IS_ERR(pnode)) {
2077 err = PTR_ERR(pnode);
2100 if (pnode->
num != col) {
2101 ubifs_err(
"pnode num %d expected %d parent num %d iip %d",
2115 if (lprops->
lnum != lnum) {
2116 ubifs_err(
"bad LEB number %d expected %d",
2117 lprops->
lnum, lnum);
2122 ubifs_err(
"LEB %d taken but not uncat %d",
2148 ubifs_err(
"LEB %d not index but cat %d",
2173 if (lprops->
hpos < heap->cnt &&
2174 heap->arr[lprops->
hpos] == lprops)
2189 ubifs_err(
"LEB %d cat %d not found in cat heap/list",
2196 ubifs_err(
"LEB %d cat %d free %d dirty %d",
2204 ubifs_err(
"LEB %d cat %d free %d dirty %d",
2230 if (!dbg_is_chk_lprops(c))
2238 num = calc_nnode_num(row, col);
2239 if (cnode->
num != num) {
2240 ubifs_err(
"nnode num %d expected %d parent num %d iip %d",
2242 (nnode ? nnode->
num : 0), cnode->
iip);
2246 while (iip < UBIFS_LPT_FANOUT) {
2260 if (iip < UBIFS_LPT_FANOUT)
2267 err = dbg_chk_pnode(c, pnode, col);
2274 iip = cnode->
iip + 1;