29 #include <linux/slab.h>
30 #include <linux/random.h>
33 static int dbg_populate_lsave(
struct ubifs_info *
c);
54 if (cnode->
level == 0)
81 for (i = cnode->
iip + 1; i < UBIFS_LPT_FANOUT; i++) {
84 if (cnode->
level == 0)
87 return first_dirty_cnode((
struct ubifs_nnode *)cnode);
99 static int get_cnodes_to_commit(
struct ubifs_info *
c)
118 cnext = next_dirty_cnode(cnode);
127 dbg_cmt(
"committing %d cnodes", cnt);
128 dbg_lp(
"committing %d cnodes", cnt);
142 dbg_lp(
"LEB %d free %d dirty %d to %d +%d",
160 static int alloc_lpt_leb(
struct ubifs_info *c,
int *lnum)
166 if (c->
ltab[i].tgc || c->
ltab[i].cmt)
175 for (i = 0; i <
n; i++) {
176 if (c->
ltab[i].tgc || c->
ltab[i].cmt)
193 static int layout_cnodes(
struct ubifs_info *c)
195 int lnum,
offs,
len, alen, done_lsave, done_ltab,
err;
235 upd_ltab(c, lnum, c->
leb_size - alen, alen - offs);
237 err = alloc_lpt_leb(c, &lnum);
242 lnum <= c->lpt_last);
263 cnode->
parent->nbranch[cnode->
iip].lnum = lnum;
271 cnode = cnode->
cnext;
272 }
while (cnode && cnode != c->
lpt_cnext);
278 upd_ltab(c, lnum, c->
leb_size - alen, alen - offs);
280 err = alloc_lpt_leb(c, &lnum);
285 lnum <= c->lpt_last);
298 upd_ltab(c, lnum, c->
leb_size - alen, alen - offs);
300 err = alloc_lpt_leb(c, &lnum);
305 lnum <= c->lpt_last);
315 upd_ltab(c, lnum, c->
leb_size - alen, alen - offs);
323 ubifs_err(
"LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
324 lnum, offs, len, done_ltab, done_lsave);
345 static int realloc_lpt_leb(
struct ubifs_info *c,
int *lnum)
351 if (c->
ltab[i].cmt) {
357 for (i = 0; i <
n; i++)
358 if (c->
ltab[i].cmt) {
374 int lnum,
offs, len,
from,
err, wlen, alen, done_ltab, done_lsave;
417 memset(buf + offs, 0xff, alen - wlen);
424 err = realloc_lpt_leb(c, &lnum);
429 lnum <= c->lpt_last);
468 cnode = cnode->
cnext;
469 }
while (cnode && cnode != c->
lpt_cnext);
476 memset(buf + offs, 0xff, alen - wlen);
481 err = realloc_lpt_leb(c, &lnum);
486 lnum <= c->lpt_last);
502 memset(buf + offs, 0xff, alen - wlen);
507 err = realloc_lpt_leb(c, &lnum);
512 lnum <= c->lpt_last);
526 memset(buf + offs, 0xff, alen - wlen);
548 ubifs_err(
"LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
549 lnum, offs, len, done_ltab, done_lsave);
573 for (iip = pnode->
iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
580 iip = nnode->
iip + 1;
588 }
while (iip >= UBIFS_LPT_FANOUT);
593 return (
void *)nnode;
596 while (nnode->
level > 1) {
601 if (iip >= UBIFS_LPT_FANOUT) {
610 return (
void *)nnode;
616 if (iip >= UBIFS_LPT_FANOUT)
644 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
648 return ERR_CAST(nnode);
650 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
677 add_pnode_dirt(c, pnode);
702 static int make_tree_dirty(
struct ubifs_info *c)
706 pnode = pnode_lookup(c, 0);
708 return PTR_ERR(pnode);
711 do_make_pnode_dirty(c, pnode);
712 pnode = next_pnode_to_dirty(c, pnode);
714 return PTR_ERR(pnode);
726 static int need_write_all(
struct ubifs_info *c)
740 if (free <= c->lpt_sz * 2)
753 static void lpt_tgc_start(
struct ubifs_info *c)
760 if (c->
ltab[i].dirty > 0 &&
764 c->
ltab[
i].dirty = 0;
784 if (c->
ltab[i].tgc) {
806 static void populate_lsave(
struct ubifs_info *c)
818 if (dbg_populate_lsave(c))
837 for (i = 0; i < heap->
cnt; i++) {
843 for (i = 0; i < heap->
cnt; i++) {
849 for (i = 0; i < heap->
cnt; i++) {
855 while (cnt < c->lsave_cnt)
879 iip = i & (UBIFS_LPT_FANOUT - 1);
905 static int make_nnode_dirty(
struct ubifs_info *c,
int node_num,
int lnum,
910 nnode = nnode_lookup(c, node_num);
912 return PTR_ERR(nnode);
916 branch = &nnode->
parent->nbranch[nnode->
iip];
917 if (branch->
lnum != lnum || branch->
offs != offs)
954 static int make_pnode_dirty(
struct ubifs_info *c,
int node_num,
int lnum,
960 pnode = pnode_lookup(c, node_num);
962 return PTR_ERR(pnode);
963 branch = &pnode->
parent->nbranch[pnode->
iip];
964 if (branch->
lnum != lnum || branch->
offs != offs)
966 do_make_pnode_dirty(c, pnode);
984 static int make_ltab_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1009 static int make_lsave_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1039 switch (node_type) {
1041 return make_nnode_dirty(c, node_num, lnum, offs);
1043 return make_pnode_dirty(c, node_num, lnum, offs);
1045 return make_ltab_dirty(c, lnum, offs);
1047 return make_lsave_dirty(c, lnum, offs);
1057 static int get_lpt_node_len(
const struct ubifs_info *c,
int node_type)
1059 switch (node_type) {
1125 node_len = get_lpt_node_len(c, node_type);
1126 if (!node_len || node_len > len)
1131 calc_crc =
crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1132 node_len - UBIFS_LPT_CRC_BYTES);
1133 if (crc != calc_crc)
1150 static int lpt_gc_lnum(
struct ubifs_info *c,
int lnum)
1162 if (!is_a_node(c, buf, len)) {
1165 pad_len = get_pad_len(c, buf, len);
1173 node_type = get_lpt_node_type(c, buf, &node_num);
1174 node_len = get_lpt_node_len(c, node_type);
1178 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1197 int i, lnum = -1,
dirty = 0;
1200 for (i = 0; i < c->
lpt_lebs; i++) {
1213 return lpt_gc_lnum(c, lnum);
1248 while (need_write_all(c)) {
1260 dbg_cmt(
"no cnodes to commit");
1265 if (!c->
big_lpt && need_write_all(c)) {
1267 err = make_tree_dirty(c);
1276 cnt = get_cnodes_to_commit(c);
1279 err = layout_cnodes(c);
1297 static void free_obsolete_cnodes(
struct ubifs_info *c)
1306 cnext = cnode->
cnext;
1333 err = write_cnodes(c);
1338 free_obsolete_cnodes(c);
1356 err = lpt_tgc_end(c);
1360 while (need_write_all(c)) {
1389 for (h = 1; h < c->
lpt_hght; h++) {
1392 if (nnode->
nbranch[i].nnode) {
1418 int iip,
h,
i, found;
1423 if (nnode->
iip == UBIFS_LPT_FANOUT - 1) {
1427 for (iip = nnode->
iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1436 for (h = *hght + 1; h < c->
lpt_hght; h++) {
1439 if (nnode->
nbranch[i].nnode) {
1464 free_obsolete_cnodes(c);
1478 nnode = first_nnode(c, &hght);
1482 nnode = next_nnode(c, nnode, &hght);
1501 static int dbg_is_all_ff(
uint8_t *buf,
int len)
1505 for (i = 0; i < len; i++)
1517 static int dbg_is_nnode_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1523 nnode = first_nnode(c, &hght);
1524 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1529 branch = &nnode->
parent->nbranch[nnode->
iip];
1530 if (branch->
lnum != lnum || branch->
offs != offs)
1552 static int dbg_is_pnode_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1557 for (i = 0; i <
cnt; i++) {
1562 pnode = pnode_lookup(c, i);
1564 return PTR_ERR(pnode);
1565 branch = &pnode->
parent->nbranch[pnode->
iip];
1566 if (branch->
lnum != lnum || branch->
offs != offs)
1581 static int dbg_is_ltab_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1594 static int dbg_is_lsave_dirty(
struct ubifs_info *c,
int lnum,
int offs)
1608 static int dbg_is_node_dirty(
struct ubifs_info *c,
int node_type,
int lnum,
1611 switch (node_type) {
1613 return dbg_is_nnode_dirty(c, lnum, offs);
1615 return dbg_is_pnode_dirty(c, lnum, offs);
1617 return dbg_is_ltab_dirty(c, lnum, offs);
1619 return dbg_is_lsave_dirty(c, lnum, offs);
1632 static int dbg_check_ltab_lnum(
struct ubifs_info *c,
int lnum)
1638 if (!dbg_is_chk_lprops(c))
1643 ubifs_err(
"cannot allocate memory for ltab checking");
1654 if (!is_a_node(c, p, len)) {
1657 pad_len = get_pad_len(c, p, len);
1664 if (!dbg_is_all_ff(p, len)) {
1665 ubifs_err(
"invalid empty space in LEB %d at %d",
1670 if (len != c->
ltab[i].free) {
1671 ubifs_err(
"invalid free space in LEB %d (free %d, expected %d)",
1672 lnum, len, c->
ltab[i].free);
1676 ubifs_err(
"invalid dirty space in LEB %d (dirty %d, expected %d)",
1682 node_type = get_lpt_node_type(c, p, &node_num);
1683 node_len = get_lpt_node_len(c, node_type);
1684 ret = dbg_is_node_dirty(c, node_type, lnum, c->
leb_size - len);
1707 if (!dbg_is_chk_lprops(c))
1712 for (i = 0; i <
cnt; i++) {
1715 pnode = pnode_lookup(c, i);
1717 return PTR_ERR(pnode);
1727 for (lnum = c->
lpt_first; lnum <= c->lpt_last; lnum++) {
1728 err = dbg_check_ltab_lnum(c, lnum);
1750 if (!dbg_is_chk_lprops(c))
1753 for (i = 0; i < c->
lpt_lebs; i++) {
1754 if (c->
ltab[i].tgc || c->
ltab[i].cmt)
1761 if (free < c->lpt_sz) {
1762 ubifs_err(
"LPT space error: free %lld lpt_sz %lld",
1792 if (!dbg_is_chk_lprops(c))
1802 ubifs_err(
"dirty pnodes %d exceed max %d",
1807 ubifs_err(
"dirty nnodes %d exceed max %d",
1825 ubifs_err(
"LPT wrote %lld but space used was %lld",
1830 ubifs_err(
"LPT wrote %lld but lpt_sz is %lld",
1835 ubifs_err(
"LPT layout size %lld but wrote %lld",
1840 ubifs_err(
"LPT new nhead offs: expected %d was %d",
1850 ubifs_err(
"LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1884 static void dump_lpt_leb(
const struct ubifs_info *c,
int lnum)
1889 pr_err(
"(pid %d) start dumping LEB %d\n",
current->pid, lnum);
1892 ubifs_err(
"cannot allocate memory to dump LPT");
1902 if (!is_a_node(c, p, len)) {
1905 pad_len = get_pad_len(c, p, len);
1907 pr_err(
"LEB %d:%d, pad %d bytes\n",
1908 lnum, offs, pad_len);
1914 pr_err(
"LEB %d:%d, free %d bytes\n",
1919 node_type = get_lpt_node_type(c, p, &node_num);
1920 switch (node_type) {
1925 pr_err(
"LEB %d:%d, pnode num %d\n",
1926 lnum, offs, node_num);
1928 pr_err(
"LEB %d:%d, pnode\n", lnum, offs);
1938 pr_err(
"LEB %d:%d, nnode num %d, ",
1939 lnum, offs, node_num);
1941 pr_err(
"LEB %d:%d, nnode, ",
1947 if (i != UBIFS_LPT_FANOUT - 1)
1955 pr_err(
"LEB %d:%d, ltab\n", lnum, offs);
1959 pr_err(
"LEB %d:%d, lsave len\n", lnum, offs);
1962 ubifs_err(
"LPT node type %d not recognized", node_type);
1970 pr_err(
"(pid %d) finish dumping LEB %d\n",
current->pid, lnum);
1987 pr_err(
"(pid %d) start dumping all LPT LEBs\n",
current->pid);
1990 pr_err(
"(pid %d) finish dumping all LPT LEBs\n",
current->pid);
2002 static int dbg_populate_lsave(
struct ubifs_info *c)
2008 if (!dbg_is_chk_gen(c))
2019 c->lsave[
random32() % c->lsave_cnt] = lprops->lnum;
2021 c->lsave[
random32() % c->lsave_cnt] = lprops->lnum;
2024 for (i = 0; i < heap->cnt; i++)
2025 c->lsave[
random32() % c->lsave_cnt] = heap->
arr[i]->lnum;
2027 for (i = 0; i < heap->cnt; i++)
2028 c->lsave[
random32() % c->lsave_cnt] = heap->
arr[i]->lnum;
2030 for (i = 0; i < heap->cnt; i++)
2031 c->lsave[
random32() % c->lsave_cnt] = heap->
arr[i]->lnum;