34 #include <linux/slab.h>
94 else if (lnum > o->
lnum)
96 else if (offs < o->offs)
98 else if (offs > o->
offs)
106 rb_link_node(&old_idx->
rb, parent, p);
123 zbr = &znode->
parent->zbranch[znode->
iip];
125 return insert_old_idx(c, zbr->
lnum, zbr->
offs);
128 return insert_old_idx(c, c->
zroot.lnum,
140 static int ins_clr_old_idx_znode(
struct ubifs_info *
c,
148 zbr = &znode->
parent->zbranch[znode->
iip];
150 err = insert_old_idx(c, zbr->
lnum, zbr->
offs);
159 err = insert_old_idx(c, c->
zroot.lnum, c->
zroot.offs);
229 if (znode->
level != 0) {
234 for (i = 0; i <
n; i++) {
238 zbr->
znode->parent = zn;
257 return ubifs_add_dirt(c, lnum, dirt);
274 if (!ubifs_zn_cow(znode)) {
280 err = add_idx_dirt(c, zbr->
lnum, zbr->
len);
287 zn = copy_znode(c, znode);
292 err = insert_old_idx(c, zbr->
lnum, zbr->
offs);
295 err = add_idx_dirt(c, zbr->
lnum, zbr->
len);
352 zbr->
leaf = lnc_node;
427 err = lnc_add(c, zbr, node);
456 int len,
int lnum,
int offs)
466 ubifs_err(
"cannot read node type %d from LEB %d:%d, error %d",
467 type, lnum, offs, err);
510 ret = try_read_node(c, node, key_type(c, key), zbr->
len, zbr->
lnum,
517 key_read(c, &dent->
key, &node_key);
518 if (keys_cmp(c, key, &node_key) != 0)
522 dbg_mntk(key,
"dangling branch LEB %d:%d len %d, key ",
539 const struct qstr *nm)
555 err = lnc_add_directly(c, zbr, dent);
566 else if (nlen < nm->len)
628 if (nn < znode->child_cnt) {
629 znode = get_znode(c, znode, nn);
631 return PTR_ERR(znode);
632 while (znode->
level != 0) {
633 znode = get_znode(c, znode, 0);
635 return PTR_ERR(znode);
673 znode = get_znode(c, znode, nn);
675 return PTR_ERR(znode);
676 while (znode->
level != 0) {
678 znode = get_znode(c, znode, nn);
680 return PTR_ERR(znode);
709 const struct qstr *nm)
713 err = matches_name(c, &(*zn)->zbranch[*n], nm);
722 err = tnc_prev(c, zn, n);
730 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
760 if (*n == (*zn)->child_cnt - 1) {
761 err = tnc_next(c, zn, n);
774 err = matches_name(c, &(*zn)->zbranch[*n], nm);
789 err = tnc_next(c, &znode, &nn);
794 if (keys_cmp(c, &znode->
zbranch[nn].key, key))
796 err = matches_name(c, &znode->
zbranch[nn], nm);
825 static int fallible_matches_name(
struct ubifs_info *c,
827 const struct qstr *nm)
838 err = fallible_read_node(c, &zbr->
key, zbr, dent);
848 err = lnc_add_directly(c, zbr, dent);
859 else if (nlen < nm->len)
895 static int fallible_resolve_collision(
struct ubifs_info *c,
898 const struct qstr *nm,
int adding)
903 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
923 err = tnc_prev(c, zn, n);
931 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
933 if (*n == (*zn)->child_cnt - 1) {
934 err = tnc_next(c, zn, n);
947 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
971 err = tnc_next(c, &znode, &nn);
976 if (keys_cmp(c, &znode->zbranch[nn].key, key))
978 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
995 if (adding || !o_znode)
998 dbg_mntk(key,
"dangling match LEB %d:%d len %d key ",
1014 static int matches_position(
struct ubifs_zbranch *zbr,
int lnum,
int offs)
1016 if (zbr->
lnum == lnum && zbr->
offs == offs)
1039 static int resolve_collision_directly(
struct ubifs_info *c,
1049 if (matches_position(&znode->
zbranch[nn], lnum, offs))
1054 err = tnc_prev(c, &znode, &nn);
1059 if (keys_cmp(c, &znode->
zbranch[nn].key, key))
1061 if (matches_position(&znode->
zbranch[nn], lnum, offs)) {
1072 err = tnc_next(c, &znode, &nn);
1077 if (keys_cmp(c, &znode->
zbranch[nn].key, key))
1081 if (matches_position(&znode->
zbranch[nn], lnum, offs))
1112 if (c->
zroot.znode->level) {
1123 if (!zp->
cnext && ubifs_zn_dirty(znode))
1138 znode = dirty_cow_znode(c, zbr);
1141 znode = dirty_cow_znode(c, &c->
zroot);
1143 if (IS_ERR(znode) || !
p)
1147 znode = znode->
zbranch[path[
p - 1]].znode;
1185 znode = c->
zroot.znode;
1189 return PTR_ERR(znode);
1199 if (znode->
level == 0)
1215 return PTR_ERR(znode);
1219 if (exact || !is_hash_key(c, key) || *n != -1) {
1220 dbg_tnc(
"found %d, lvl %d, n %d", exact, znode->
level, *n);
1267 err = tnc_prev(c, &znode, n);
1275 if (keys_cmp(c, key, &znode->
zbranch[*n].key)) {
1318 dbg_tnck(key,
"search and dirty key ");
1320 znode = c->
zroot.znode;
1324 return PTR_ERR(znode);
1327 znode = dirty_cow_znode(c, &c->
zroot);
1329 return PTR_ERR(znode);
1338 if (znode->
level == 0)
1347 znode = dirty_cow_znode(c, zbr);
1349 return PTR_ERR(znode);
1356 return PTR_ERR(znode);
1357 znode = dirty_cow_znode(c, zbr);
1359 return PTR_ERR(znode);
1363 if (exact || !is_hash_key(c, key) || *n != -1) {
1364 dbg_tnc(
"found %d, lvl %d, n %d", exact, znode->
level, *n);
1372 err = tnc_prev(c, &znode, n);
1380 if (keys_cmp(c, key, &znode->
zbranch[*n].key)) {
1386 if (znode->
cnext || !ubifs_zn_dirty(znode)) {
1387 znode = dirty_cow_bottom_up(c, znode);
1389 return PTR_ERR(znode);
1407 static int maybe_leb_gced(
struct ubifs_info *c,
int lnum,
int gc_seq1)
1409 int gc_seq2, gced_lnum;
1415 if (gc_seq1 == gc_seq2)
1418 if (gc_seq1 + 1 != gc_seq2)
1428 if (gced_lnum == lnum)
1447 void *node,
int *lnum,
int *offs)
1449 int found,
n,
err, safely = 0, gc_seq1;
1459 }
else if (found < 0) {
1468 if (is_hash_key(c, key)) {
1473 err = tnc_read_node_nm(c, zt, node);
1491 err = fallible_read_node(c, key, &zbr, node);
1492 if (err <= 0 || maybe_leb_gced(c, zbr.
lnum, gc_seq1)) {
1524 unsigned int block = key_block(c, &bu->
key);
1553 unsigned int next_block;
1556 err = tnc_next(c, &znode, &n);
1562 if (key_inum(c, key) != key_inum(c, &bu->
key) ||
1581 if (zbr->
lnum != lnum || zbr->
offs != offs)
1590 next_block = key_block(c, key);
1591 bu->
blk_cnt += (next_block - block - 1);
1632 block = key_block(c, &bu->
key) + bu->
blk_cnt;
1652 static int read_wbuf(
struct ubifs_wbuf *wbuf,
void *buf,
int len,
int lnum,
1658 dbg_io(
"LEB %d:%d, length %d", lnum, offs, len);
1663 spin_lock(&wbuf->
lock);
1664 overlap = (lnum == wbuf->
lnum && offs + len > wbuf->
offs);
1667 spin_unlock(&wbuf->
lock);
1677 memcpy(buf + rlen, wbuf->
buf + offs + rlen - wbuf->
offs, len - rlen);
1678 spin_unlock(&wbuf->
lock);
1695 static int validate_data_node(
struct ubifs_info *c,
void *buf,
1703 ubifs_err(
"bad node type (%d but expected %d)",
1715 if (len != zbr->
len) {
1716 ubifs_err(
"bad node length %d, expected %d", len, zbr->
len);
1722 if (!keys_eq(c, &zbr->
key, &key1)) {
1723 ubifs_err(
"bad key in node at LEB %d:%d",
1726 dbg_tnck(&key1,
"found node's key ");
1767 err = read_wbuf(wbuf, bu->
buf, len, lnum, offs);
1772 if (maybe_leb_gced(c, lnum, bu->
gc_seq))
1776 ubifs_err(
"failed to read from LEB %d:%d, error %d",
1785 for (i = 0; i < bu->
cnt; i++) {
1786 err = validate_data_node(c, buf, &bu->
zbranch[i]);
1809 void *node,
const struct qstr *nm)
1820 }
else if (found < 0) {
1827 err = resolve_collision(c, key, &znode, &n, nm);
1828 dbg_tnc(
"rc returned %d, znode %p, n %d", err, znode, n);
1836 err = tnc_read_node_nm(c, &znode->
zbranch[n], node);
1857 void *node,
const struct qstr *nm)
1866 err = ubifs_tnc_lookup(c, key, node);
1878 return do_lookup_nm(c, key, node, nm);
1890 static void correct_parent_keys(
const struct ubifs_info *c,
1899 key1 = &znode->
parent->zbranch[0].key;
1901 while (keys_cmp(c, key, key1) < 0) {
1902 key_copy(c, key, key1);
1907 key1 = &znode->
parent->zbranch[0].key;
1922 static void insert_zbranch(
struct ubifs_znode *znode,
1930 for (i = znode->
child_cnt; i > n; i--) {
1978 int i, keep, move, appending = 0;
1988 dbg_tnck(key,
"inserted at %d level %d, key ", n, znode->
level);
1990 insert_zbranch(znode, zbr, n);
1993 if (n == 0 && zp && znode->
iip == 0)
1994 correct_parent_keys(c, znode);
2010 ins_clr_old_idx_znode(c, znode);
2022 key1 = &znode->
zbranch[n - 1].key;
2023 if (key_inum(c, key1) == key_inum(c, key) &&
2028 }
else if (appending && n != c->
fanout) {
2032 if (n >= (c->
fanout + 1) / 2) {
2033 key1 = &znode->
zbranch[0].key;
2034 if (key_inum(c, key1) == key_inum(c, key) &&
2037 if (key_inum(c, key1) != key_inum(c, key) ||
2052 keep = (c->
fanout + 1) / 2;
2072 zbr->
znode->parent = zn;
2083 dbg_tnc(
"moving %d, keeping %d", move, keep);
2086 for (i = 0; i < move; i++) {
2097 dbg_tnck(key,
"inserting at %d level %d, key ", n, zn->
level);
2099 insert_zbranch(zi, zbr, n);
2103 if (n == 0 && zi == znode && znode->
iip == 0)
2104 correct_parent_keys(c, znode);
2121 dbg_tnc(
"creating new zroot at level %d", znode->
level + 1);
2144 c->
zroot.znode = zi;
2169 int found,
n, err = 0;
2173 dbg_tnck(key,
"%d:%d, len %d, key ", lnum, offs, len);
2174 found = lookup_level0_dirty(c, key, &znode, &n);
2182 key_copy(c, key, &zbr.
key);
2183 err = tnc_insert(c, znode, &zbr, n + 1);
2184 }
else if (found == 1) {
2188 err = ubifs_add_dirt(c, zbr->
lnum, zbr->
len);
2216 int old_lnum,
int old_offs,
int lnum,
int offs,
int len)
2218 int found,
n, err = 0;
2222 dbg_tnck(key,
"old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum,
2223 old_offs, lnum, offs, len);
2224 found = lookup_level0_dirty(c, key, &znode, &n);
2234 if (zbr->
lnum == old_lnum && zbr->
offs == old_offs) {
2236 err = ubifs_add_dirt(c, zbr->
lnum, zbr->
len);
2243 }
else if (is_hash_key(c, key)) {
2244 found = resolve_collision_directly(c, key, &znode, &n,
2245 old_lnum, old_offs);
2246 dbg_tnc(
"rc returned %d, znode %p, n %d, LEB %d:%d",
2247 found, znode, n, old_lnum, old_offs);
2255 if (znode->
cnext || !ubifs_zn_dirty(znode)) {
2256 znode = dirty_cow_bottom_up(c, znode);
2257 if (IS_ERR(znode)) {
2258 err = PTR_ERR(znode);
2264 err = ubifs_add_dirt(c, zbr->
lnum,
2276 err = ubifs_add_dirt(c, lnum, len);
2299 int lnum,
int offs,
int len,
const struct qstr *nm)
2301 int found,
n, err = 0;
2305 dbg_tnck(key,
"LEB %d:%d, name '%.*s', key ",
2306 lnum, offs, nm->len, nm->
name);
2307 found = lookup_level0_dirty(c, key, &znode, &n);
2315 found = fallible_resolve_collision(c, key, &znode, &n,
2318 found = resolve_collision(c, key, &znode, &n, nm);
2319 dbg_tnc(
"rc returned %d, znode %p, n %d", found, znode, n);
2326 if (znode->
cnext || !ubifs_zn_dirty(znode)) {
2327 znode = dirty_cow_bottom_up(c, znode);
2328 if (IS_ERR(znode)) {
2329 err = PTR_ERR(znode);
2338 err = ubifs_add_dirt(c, zbr->
lnum, zbr->
len);
2353 key_copy(c, key, &zbr.
key);
2354 err = tnc_insert(c, znode, &zbr, n + 1);
2364 struct qstr noname = { .
name =
"" };
2404 err = ubifs_add_dirt(c, zbr->
lnum, zbr->
len);
2411 for (i = n; i < znode->
child_cnt - 1; i++)
2448 for (i = n; i < znode->
child_cnt; i++) {
2462 znode = get_znode(c, znode, 0);
2464 return PTR_ERR(znode);
2465 znode = dirty_cow_znode(c, zbr);
2467 return PTR_ERR(znode);
2471 err = insert_old_idx(c, c->
zroot.lnum,
2479 c->
zroot.znode = znode;
2505 int found,
n, err = 0;
2510 found = lookup_level0_dirty(c, key, &znode, &n);
2516 err = tnc_delete(c, znode, n);
2534 const struct qstr *nm)
2541 err = lookup_level0_dirty(c, key, &znode, &n);
2547 err = fallible_resolve_collision(c, key, &znode, &n,
2550 err = resolve_collision(c, key, &znode, &n, nm);
2551 dbg_tnc(
"rc returned %d, znode %p, n %d", err, znode, n);
2556 if (znode->
cnext || !ubifs_zn_dirty(znode)) {
2557 znode = dirty_cow_bottom_up(c, znode);
2558 if (IS_ERR(znode)) {
2559 err = PTR_ERR(znode);
2563 err = tnc_delete(c, znode, n);
2586 if (keys_cmp(c, key, from_key) < 0)
2588 if (keys_cmp(c, key, to_key) > 0)
2606 int i,
n,
k, err = 0;
2620 err = tnc_next(c, &znode, &n);
2628 if (!key_in_range(c, key, from_key, to_key)) {
2635 if (znode->
cnext || !ubifs_zn_dirty(znode)) {
2636 znode = dirty_cow_bottom_up(c, znode);
2637 if (IS_ERR(znode)) {
2638 err = PTR_ERR(znode);
2644 for (i = n + 1, k = 0; i < znode->
child_cnt; i++, k++) {
2646 if (!key_in_range(c, key, from_key, to_key))
2649 err = ubifs_add_dirt(c, znode->
zbranch[i].lnum,
2658 for (i = n + 1 + k; i < znode->
child_cnt; i++)
2664 err = tnc_delete(c, znode, n);
2691 dbg_tnc(
"ino %lu", (
unsigned long)inum);
2697 lowest_xent_key(c, &key1, inum);
2704 err = PTR_ERR(xent);
2712 (
unsigned long)xattr_inum);
2722 lowest_ino_key(c, &key1, xattr_inum);
2723 highest_ino_key(c, &key2, xattr_inum);
2732 key_read(c, &xent->
key, &key1);
2736 lowest_ino_key(c, &key1, inum);
2737 highest_ino_key(c, &key2, inum);
2767 const struct qstr *nm)
2769 int n,
err, type = key_type(c, key);
2786 err = resolve_collision(c, key, &znode, &n, nm);
2787 dbg_tnc(
"rc returned %d, znode %p, n %d",
2794 err = tnc_next(c, &znode, &n);
2809 err = tnc_next(c, &znode, &n);
2827 if (key_inum(c, dkey) != key_inum(c, key) ||
2828 key_type(c, dkey) != type) {
2833 err = tnc_read_node_nm(c, zbr, dent);
2844 return ERR_PTR(err);
2853 static void tnc_destroy_cnext(
struct ubifs_info *c)
2864 cnext = cnext->
cnext;
2865 if (ubifs_zn_obsolete(znode))
2867 }
while (cnext && cnext != c->
cnext);
2876 tnc_destroy_cnext(c);
2877 if (c->
zroot.znode) {
2903 int n = znode->
iip - 1;
2911 znode = get_znode(c, znode, n);
2914 while (znode->
level != level) {
2916 znode = get_znode(c, znode, n);
2937 int level = znode->
level;
2940 int n = znode->
iip + 1;
2946 if (n < znode->child_cnt) {
2948 znode = get_znode(c, znode, n);
2951 while (znode->
level != level) {
2952 znode = get_znode(c, znode, 0);
3004 znode = c->
zroot.znode;
3011 if (c->
zroot.lnum == lnum && c->
zroot.offs == offs)
3014 if (level >= znode->
level)
3027 znode = left_znode(c, znode);
3035 if (znode->
level == level + 1)
3037 znode = get_znode(c, znode, n);
3042 if (znode->
zbranch[n].lnum == lnum && znode->
zbranch[n].offs == offs)
3043 return get_znode(c, znode, n);
3045 if (!is_hash_key(c, key))
3059 znode = left_znode(c, znode);
3067 if (znode->
zbranch[n].lnum == lnum &&
3068 znode->
zbranch[n].offs == offs)
3069 return get_znode(c, znode, n);
3071 if (keys_cmp(c, &znode->
zbranch[n].key, key) < 0)
3081 znode = right_znode(c, znode);
3089 if (znode->
zbranch[n].lnum == lnum &&
3090 znode->
zbranch[n].offs == offs)
3091 return get_znode(c, znode, n);
3093 if (keys_cmp(c, &znode->
zbranch[n].key, key) > 0)
3121 znode = lookup_znode(c, key, level, lnum, offs);
3125 return PTR_ERR(znode);
3127 return ubifs_zn_dirty(znode) ? 1 : 2;
3149 const int unique = !is_hash_key(c, key);
3157 if (lnum == zbr->
lnum && offs == zbr->
offs)
3169 err = tnc_prev(c, &znode, &n);
3174 if (keys_cmp(c, key, &znode->
zbranch[n].key))
3177 if (lnum == zbr->
lnum && offs == zbr->
offs)
3184 err = tnc_next(c, &znode, &n);
3190 if (keys_cmp(c, key, &znode->
zbranch[n].key))
3193 if (lnum == zbr->
lnum && offs == zbr->
offs)
3214 int lnum,
int offs,
int is_idx)
3232 err = is_leaf_node_in_tnc(c, key, lnum, offs);
3260 znode = lookup_znode(c, key, level, lnum, offs);
3263 if (IS_ERR(znode)) {
3264 err = PTR_ERR(znode);
3267 znode = dirty_cow_bottom_up(c, znode);
3268 if (IS_ERR(znode)) {
3269 err = PTR_ERR(znode);
3299 if (!dbg_is_chk_gen(c))
3303 data_key_init(c, &from_key, inode->
i_ino, block);
3304 highest_data_key(c, &to_key, inode->
i_ino);
3317 err = tnc_next(c, &znode, &n);
3327 if (!key_in_range(c, key, &from_key, &to_key))
3331 block = key_block(c, key);
3332 ubifs_err(
"inode %lu has size %lld, but there are data at offset %lld",
3333 (
unsigned long)inode->
i_ino, size,