Lines Matching +full:key +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006-2008 Nokia Corporation.
13 * the UBIFS B-tree.
27 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
42 NAME_MATCHES = 1,
53 p = &c->old_idx.rb_node; in do_insert_old_idx()
57 if (old_idx->lnum < o->lnum) in do_insert_old_idx()
58 p = &(*p)->rb_left; in do_insert_old_idx()
59 else if (old_idx->lnum > o->lnum) in do_insert_old_idx()
60 p = &(*p)->rb_right; in do_insert_old_idx()
61 else if (old_idx->offs < o->offs) in do_insert_old_idx()
62 p = &(*p)->rb_left; in do_insert_old_idx()
63 else if (old_idx->offs > o->offs) in do_insert_old_idx()
64 p = &(*p)->rb_right; in do_insert_old_idx()
71 rb_link_node(&old_idx->rb, parent, p); in do_insert_old_idx()
72 rb_insert_color(&old_idx->rb, &c->old_idx); in do_insert_old_idx()
76 * insert_old_idx - record an index node obsoleted since the last commit start.
77 * @c: UBIFS file-system description object
89 * That means that the garbage collection and the in-the-gaps method of
94 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
95 * That is what this function does. The RB-tree is ordered by LEB number and
104 return -ENOMEM; in insert_old_idx()
105 old_idx->lnum = lnum; in insert_old_idx()
106 old_idx->offs = offs; in insert_old_idx()
113 * insert_old_idx_znode - record a znode obsoleted since last commit start.
114 * @c: UBIFS file-system description object
121 if (znode->parent) { in insert_old_idx_znode()
124 zbr = &znode->parent->zbranch[znode->iip]; in insert_old_idx_znode()
125 if (zbr->len) in insert_old_idx_znode()
126 return insert_old_idx(c, zbr->lnum, zbr->offs); in insert_old_idx_znode()
128 if (c->zroot.len) in insert_old_idx_znode()
129 return insert_old_idx(c, c->zroot.lnum, in insert_old_idx_znode()
130 c->zroot.offs); in insert_old_idx_znode()
135 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
136 * @c: UBIFS file-system description object
146 if (znode->parent) { in ins_clr_old_idx_znode()
149 zbr = &znode->parent->zbranch[znode->iip]; in ins_clr_old_idx_znode()
150 if (zbr->len) { in ins_clr_old_idx_znode()
151 err = insert_old_idx(c, zbr->lnum, zbr->offs); in ins_clr_old_idx_znode()
154 zbr->lnum = 0; in ins_clr_old_idx_znode()
155 zbr->offs = 0; in ins_clr_old_idx_znode()
156 zbr->len = 0; in ins_clr_old_idx_znode()
159 if (c->zroot.len) { in ins_clr_old_idx_znode()
160 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs); in ins_clr_old_idx_znode()
163 c->zroot.lnum = 0; in ins_clr_old_idx_znode()
164 c->zroot.offs = 0; in ins_clr_old_idx_znode()
165 c->zroot.len = 0; in ins_clr_old_idx_znode()
171 * destroy_old_idx - destroy the old_idx RB-tree.
172 * @c: UBIFS file-system description object
174 * During start commit, the old_idx RB-tree is used to avoid overwriting index
177 * new index is successfully written. The old-idx RB-tree is used for the
178 * in-the-gaps method of writing index nodes and is destroyed every commit.
184 rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb) in destroy_old_idx()
187 c->old_idx = RB_ROOT; in destroy_old_idx()
191 * copy_znode - copy a dirty znode.
192 * @c: UBIFS file-system description object
202 zn = kmemdup(znode, c->max_znode_sz, GFP_NOFS); in copy_znode()
204 return ERR_PTR(-ENOMEM); in copy_znode()
206 zn->cnext = NULL; in copy_znode()
207 __set_bit(DIRTY_ZNODE, &zn->flags); in copy_znode()
208 __clear_bit(COW_ZNODE, &zn->flags); in copy_znode()
214 * add_idx_dirt - add dirt due to a dirty znode.
215 * @c: UBIFS file-system description object
223 c->calc_idx_sz -= ALIGN(dirt, 8); in add_idx_dirt()
228 * replace_znode - replace old znode with new znode.
229 * @c: UBIFS file-system description object
240 __set_bit(OBSOLETE_ZNODE, &old_zn->flags); in replace_znode()
242 if (old_zn->level != 0) { in replace_znode()
244 const int n = new_zn->child_cnt; in replace_znode()
248 struct ubifs_zbranch *child = &new_zn->zbranch[i]; in replace_znode()
250 if (child->znode) in replace_znode()
251 child->znode->parent = new_zn; in replace_znode()
255 zbr->znode = new_zn; in replace_znode()
256 zbr->lnum = 0; in replace_znode()
257 zbr->offs = 0; in replace_znode()
258 zbr->len = 0; in replace_znode()
260 atomic_long_inc(&c->dirty_zn_cnt); in replace_znode()
264 * dirty_cow_znode - ensure a znode is not being committed.
265 * @c: UBIFS file-system description object
273 struct ubifs_znode *znode = zbr->znode; in dirty_cow_znode()
279 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) { in dirty_cow_znode()
280 atomic_long_inc(&c->dirty_zn_cnt); in dirty_cow_znode()
281 atomic_long_dec(&c->clean_zn_cnt); in dirty_cow_znode()
283 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
294 if (zbr->len) { in dirty_cow_znode()
299 err = -ENOMEM; in dirty_cow_znode()
302 old_idx->lnum = zbr->lnum; in dirty_cow_znode()
303 old_idx->offs = zbr->offs; in dirty_cow_znode()
305 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
324 * lnc_add - add a leaf node to the leaf node cache.
325 * @c: UBIFS file-system description object
329 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
330 * purpose of the leaf node cache is to save re-reading the same leaf node over
340 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
350 ubifs_assert(c, !zbr->leaf); in lnc_add()
351 ubifs_assert(c, zbr->len != 0); in lnc_add()
352 ubifs_assert(c, is_hash_key(c, &zbr->key)); in lnc_add()
357 ubifs_dump_node(c, dent, zbr->len); in lnc_add()
361 lnc_node = kmemdup(node, zbr->len, GFP_NOFS); in lnc_add()
366 zbr->leaf = lnc_node; in lnc_add()
371 * lnc_add_directly - add a leaf node to the leaf-node-cache.
372 * @c: UBIFS file-system description object
384 ubifs_assert(c, !zbr->leaf); in lnc_add_directly()
385 ubifs_assert(c, zbr->len != 0); in lnc_add_directly()
390 ubifs_dump_node(c, node, zbr->len); in lnc_add_directly()
394 zbr->leaf = node; in lnc_add_directly()
399 * lnc_free - remove a leaf node from the leaf node cache.
404 if (!zbr->leaf) in lnc_free()
406 kfree(zbr->leaf); in lnc_free()
407 zbr->leaf = NULL; in lnc_free()
411 * tnc_read_hashed_node - read a "hashed" leaf node.
412 * @c: UBIFS file-system description object
413 * @zbr: key and position of the node
426 ubifs_assert(c, is_hash_key(c, &zbr->key)); in tnc_read_hashed_node()
428 if (zbr->leaf) { in tnc_read_hashed_node()
430 ubifs_assert(c, zbr->len != 0); in tnc_read_hashed_node()
431 memcpy(node, zbr->leaf, zbr->len); in tnc_read_hashed_node()
435 if (c->replaying) { in tnc_read_hashed_node()
436 err = fallible_read_node(c, &zbr->key, zbr, node); in tnc_read_hashed_node()
438 * When the node was not found, return -ENOENT, 0 otherwise. in tnc_read_hashed_node()
439 * Negative return codes stay as-is. in tnc_read_hashed_node()
442 err = -ENOENT; in tnc_read_hashed_node()
443 else if (err == 1) in tnc_read_hashed_node()
457 * try_read_node - read a node if it is a node.
458 * @c: UBIFS file-system description object
464 * stores it in @buf. This function returns %1 if a node is present and %0 if
470 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
472 * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
473 * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
474 * because during mounting or re-mounting from R/O mode to R/W mode we may read
481 int len = zbr->len; in try_read_node()
482 int lnum = zbr->lnum; in try_read_node()
483 int offs = zbr->offs; in try_read_node()
490 err = ubifs_leb_read(c, lnum, buf, offs, len, 1); in try_read_node()
497 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) in try_read_node()
500 if (ch->node_type != type) in try_read_node()
503 node_len = le32_to_cpu(ch->len); in try_read_node()
507 if (type != UBIFS_DATA_NODE || !c->no_chk_data_crc || c->mounting || in try_read_node()
508 c->remounting_rw) { in try_read_node()
509 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in try_read_node()
510 node_crc = le32_to_cpu(ch->crc); in try_read_node()
515 err = ubifs_node_check_hash(c, buf, zbr->hash); in try_read_node()
517 ubifs_bad_hash(c, buf, zbr->hash, lnum, offs); in try_read_node()
521 return 1; in try_read_node()
525 * fallible_read_node - try to read a leaf node.
526 * @c: UBIFS file-system description object
527 * @key: key of node to read
531 * This function tries to read a node and returns %1 if the node is read, %0
534 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key, in fallible_read_node() argument
539 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs); in fallible_read_node()
541 ret = try_read_node(c, node, key_type(c, key), zbr); in fallible_read_node()
542 if (ret == 1) { in fallible_read_node()
546 /* All nodes have key in the same place */ in fallible_read_node()
547 key_read(c, &dent->key, &node_key); in fallible_read_node()
548 if (keys_cmp(c, key, &node_key) != 0) in fallible_read_node()
551 if (ret == 0 && c->replaying) in fallible_read_node()
552 dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ", in fallible_read_node()
553 zbr->lnum, zbr->offs, zbr->len); in fallible_read_node()
558 * matches_name - determine if a direntry or xattr entry matches a given name.
559 * @c: UBIFS file-system description object
575 if (!zbr->leaf) { in matches_name()
576 dent = kmalloc(zbr->len, GFP_NOFS); in matches_name()
578 return -ENOMEM; in matches_name()
589 dent = zbr->leaf; in matches_name()
591 nlen = le16_to_cpu(dent->nlen); in matches_name()
592 err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm))); in matches_name()
611 * get_znode - get a TNC znode that may not be loaded yet.
612 * @c: UBIFS file-system description object
623 zbr = &znode->zbranch[n]; in get_znode()
624 if (zbr->znode) in get_znode()
625 znode = zbr->znode; in get_znode()
632 * tnc_next - find next TNC entry.
633 * @c: UBIFS file-system description object
637 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
645 nn += 1; in tnc_next()
646 if (nn < znode->child_cnt) { in tnc_next()
650 while (1) { in tnc_next()
653 zp = znode->parent; in tnc_next()
655 return -ENOENT; in tnc_next()
656 nn = znode->iip + 1; in tnc_next()
658 if (nn < znode->child_cnt) { in tnc_next()
662 while (znode->level != 0) { in tnc_next()
677 * tnc_prev - find previous TNC entry.
678 * @c: UBIFS file-system description object
682 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
691 *n = nn - 1; in tnc_prev()
694 while (1) { in tnc_prev()
697 zp = znode->parent; in tnc_prev()
699 return -ENOENT; in tnc_prev()
700 nn = znode->iip - 1; in tnc_prev()
706 while (znode->level != 0) { in tnc_prev()
707 nn = znode->child_cnt - 1; in tnc_prev()
712 nn = znode->child_cnt - 1; in tnc_prev()
722 * resolve_collision - resolve a collision.
723 * @c: UBIFS file-system description object
724 * @key: key of a directory or extended attribute entry
729 * This function is called for "hashed" keys to make sure that the found key
731 * entry). It returns %1 and sets @zn and @n if the collision is resolved.
734 * This means that @n may be set to %-1 if the leftmost key in @zn is the
737 static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key, in resolve_collision() argument
743 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
747 return 1; in resolve_collision()
751 while (1) { in resolve_collision()
753 if (err == -ENOENT) { in resolve_collision()
755 *n = -1; in resolve_collision()
760 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in resolve_collision()
769 * ---------------------- in resolve_collision()
770 * level 1 | Key0 | Key1 | in resolve_collision()
771 * ----------------------- in resolve_collision()
774 * ------------ ------------ in resolve_collision()
776 * ------------ ------------ in resolve_collision()
782 * znode za at slot n = 1. But that is invalid in resolve_collision()
788 * 'tnc_insert()' would correct the parent key. in resolve_collision()
790 if (*n == (*zn)->child_cnt - 1) { in resolve_collision()
795 if (err == -ENOENT) in resolve_collision()
796 err = -EINVAL; in resolve_collision()
800 *n = -1; in resolve_collision()
804 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
810 return 1; in resolve_collision()
818 while (1) { in resolve_collision()
820 if (err == -ENOENT) in resolve_collision()
824 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision()
826 err = matches_name(c, &znode->zbranch[nn], nm); in resolve_collision()
834 return 1; in resolve_collision()
841 * fallible_matches_name - determine if a dent matches a given name.
842 * @c: UBIFS file-system description object
863 if (!zbr->leaf) { in fallible_matches_name()
864 dent = kmalloc(zbr->len, GFP_NOFS); in fallible_matches_name()
866 return -ENOMEM; in fallible_matches_name()
868 err = fallible_read_node(c, &zbr->key, zbr, dent); in fallible_matches_name()
876 ubifs_assert(c, err == 1); in fallible_matches_name()
882 dent = zbr->leaf; in fallible_matches_name()
884 nlen = le16_to_cpu(dent->nlen); in fallible_matches_name()
885 err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm))); in fallible_matches_name()
904 * fallible_resolve_collision - resolve a collision even if nodes are missing.
905 * @c: UBIFS file-system description object
906 * @key: key
910 * @adding: indicates caller is adding a key to the TNC
915 * Garbage-collected and the commit was not done. A branch that refers to a node
918 * o if @nm was found, %1 is returned and @zn and @n are set to the found
922 * found, then %1 is returned and @zn and @n are set to the dangling branch;
926 const union ubifs_key *key, in fallible_resolve_collision() argument
934 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
938 return 1; in fallible_resolve_collision()
945 * branch - to the left or to the right. Well, let's try left. in fallible_resolve_collision()
947 unsure = 1; in fallible_resolve_collision()
949 unsure = 1; /* Remove a dangling branch wherever it is */ in fallible_resolve_collision()
953 while (1) { in fallible_resolve_collision()
955 if (err == -ENOENT) { in fallible_resolve_collision()
957 *n = -1; in fallible_resolve_collision()
962 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in fallible_resolve_collision()
964 if (*n == (*zn)->child_cnt - 1) { in fallible_resolve_collision()
969 if (err == -ENOENT) in fallible_resolve_collision()
970 err = -EINVAL; in fallible_resolve_collision()
974 *n = -1; in fallible_resolve_collision()
978 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm); in fallible_resolve_collision()
982 return 1; in fallible_resolve_collision()
1001 while (1) { in fallible_resolve_collision()
1003 if (err == -ENOENT) in fallible_resolve_collision()
1007 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in fallible_resolve_collision()
1009 err = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
1017 return 1; in fallible_resolve_collision()
1029 dbg_mntk(key, "dangling match LEB %d:%d len %d key ", in fallible_resolve_collision()
1030 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs, in fallible_resolve_collision()
1031 o_znode->zbranch[o_n].len); in fallible_resolve_collision()
1034 return 1; in fallible_resolve_collision()
1038 * matches_position - determine if a zbranch matches a given position.
1043 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1047 if (zbr->lnum == lnum && zbr->offs == offs) in matches_position()
1048 return 1; in matches_position()
1054 * resolve_collision_directly - resolve a collision directly.
1055 * @c: UBIFS file-system description object
1056 * @key: key of directory entry
1066 * names). This function returns %1 and sets @zn and @n if the collision is
1071 const union ubifs_key *key, in resolve_collision_directly() argument
1080 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1081 return 1; in resolve_collision_directly()
1084 while (1) { in resolve_collision_directly()
1086 if (err == -ENOENT) in resolve_collision_directly()
1090 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision_directly()
1092 if (matches_position(&znode->zbranch[nn], lnum, offs)) { in resolve_collision_directly()
1095 return 1; in resolve_collision_directly()
1102 while (1) { in resolve_collision_directly()
1104 if (err == -ENOENT) in resolve_collision_directly()
1108 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision_directly()
1112 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1113 return 1; in resolve_collision_directly()
1118 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1119 * @c: UBIFS file-system description object
1122 * If we do not have a unique key that resides in a znode, then we cannot
1131 int *path = c->bottom_up_buf, p = 0; in dirty_cow_bottom_up()
1133 ubifs_assert(c, c->zroot.znode); in dirty_cow_bottom_up()
1135 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) { in dirty_cow_bottom_up()
1136 kfree(c->bottom_up_buf); in dirty_cow_bottom_up()
1137 c->bottom_up_buf = kmalloc_array(c->zroot.znode->level, in dirty_cow_bottom_up()
1140 if (!c->bottom_up_buf) in dirty_cow_bottom_up()
1141 return ERR_PTR(-ENOMEM); in dirty_cow_bottom_up()
1142 path = c->bottom_up_buf; in dirty_cow_bottom_up()
1144 if (c->zroot.znode->level) { in dirty_cow_bottom_up()
1146 while (1) { in dirty_cow_bottom_up()
1149 zp = znode->parent; in dirty_cow_bottom_up()
1152 n = znode->iip; in dirty_cow_bottom_up()
1153 ubifs_assert(c, p < c->zroot.znode->level); in dirty_cow_bottom_up()
1155 if (!zp->cnext && ubifs_zn_dirty(znode)) in dirty_cow_bottom_up()
1162 while (1) { in dirty_cow_bottom_up()
1165 zp = znode->parent; in dirty_cow_bottom_up()
1167 ubifs_assert(c, path[p - 1] >= 0); in dirty_cow_bottom_up()
1168 ubifs_assert(c, path[p - 1] < zp->child_cnt); in dirty_cow_bottom_up()
1169 zbr = &zp->zbranch[path[--p]]; in dirty_cow_bottom_up()
1172 ubifs_assert(c, znode == c->zroot.znode); in dirty_cow_bottom_up()
1173 znode = dirty_cow_znode(c, &c->zroot); in dirty_cow_bottom_up()
1177 ubifs_assert(c, path[p - 1] >= 0); in dirty_cow_bottom_up()
1178 ubifs_assert(c, path[p - 1] < znode->child_cnt); in dirty_cow_bottom_up()
1179 znode = znode->zbranch[path[p - 1]].znode; in dirty_cow_bottom_up()
1186 * ubifs_lookup_level0 - search for zero-level znode.
1187 * @c: UBIFS file-system description object
1188 * @key: key to lookup
1192 * This function looks up the TNC tree and search for zero-level znode which
1193 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1195 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1197 * o not exact match, which means that zero-level znode does not contain
1198 * @key, then %0 is returned and slot number of the closest branch or %-1
1200 * o @key is so small that it is even less than the lowest key of the
1201 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1207 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key, in ubifs_lookup_level0() argument
1214 dbg_tnck(key, "search key "); in ubifs_lookup_level0()
1215 ubifs_assert(c, key_type(c, key) < UBIFS_INVALID_KEY); in ubifs_lookup_level0()
1217 znode = c->zroot.znode; in ubifs_lookup_level0()
1219 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in ubifs_lookup_level0()
1224 znode->time = time; in ubifs_lookup_level0()
1226 while (1) { in ubifs_lookup_level0()
1229 exact = ubifs_search_zbranch(c, znode, key, n); in ubifs_lookup_level0()
1231 if (znode->level == 0) in ubifs_lookup_level0()
1236 zbr = &znode->zbranch[*n]; in ubifs_lookup_level0()
1238 if (zbr->znode) { in ubifs_lookup_level0()
1239 znode->time = time; in ubifs_lookup_level0()
1240 znode = zbr->znode; in ubifs_lookup_level0()
1251 if (exact || !is_hash_key(c, key) || *n != -1) { in ubifs_lookup_level0()
1252 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in ubifs_lookup_level0()
1257 * Here is a tricky place. We have not found the key and this is a in ubifs_lookup_level0()
1258 * "hashed" key, which may collide. The rest of the code deals with in ubifs_lookup_level0()
1267 * | 1 | 5 | in ubifs_lookup_level0()
1269 * | 1 | 3 | | 5 | 8 | in ubifs_lookup_level0()
1273 * In the examples, if we are looking for key "5", we may reach nodes in ubifs_lookup_level0()
1275 * left and see if there is "5" key there. If there is, we have to in ubifs_lookup_level0()
1279 * elements which are equivalent to the next key in the parent in the in ubifs_lookup_level0()
1287 * And this becomes what is at the first "picture" after key "5" marked in ubifs_lookup_level0()
1290 * removing the leftmost key, we would have to correct the key of the in ubifs_lookup_level0()
1292 * if we changed the leftmost key of the parent znode, the garbage in ubifs_lookup_level0()
1294 * indexing LEBs). Although we already have an additional RB-tree where in ubifs_lookup_level0()
1300 if (err == -ENOENT) { in ubifs_lookup_level0()
1301 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1302 *n = -1; in ubifs_lookup_level0()
1307 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in ubifs_lookup_level0()
1308 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1309 *n = -1; in ubifs_lookup_level0()
1313 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in ubifs_lookup_level0()
1315 return 1; in ubifs_lookup_level0()
1319 * lookup_level0_dirty - search for zero-level znode dirtying.
1320 * @c: UBIFS file-system description object
1321 * @key: key to lookup
1325 * This function looks up the TNC tree and search for zero-level znode which
1326 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1328 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1330 * o not exact match, which means that zero-level znode does not contain @key
1333 * o @key is so small that it is even less than the lowest key of the
1334 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1336 * Additionally all znodes in the path from the root to the located zero-level
1343 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key, in lookup_level0_dirty() argument
1350 dbg_tnck(key, "search and dirty key "); in lookup_level0_dirty()
1352 znode = c->zroot.znode; in lookup_level0_dirty()
1354 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_level0_dirty()
1359 znode = dirty_cow_znode(c, &c->zroot); in lookup_level0_dirty()
1363 znode->time = time; in lookup_level0_dirty()
1365 while (1) { in lookup_level0_dirty()
1368 exact = ubifs_search_zbranch(c, znode, key, n); in lookup_level0_dirty()
1370 if (znode->level == 0) in lookup_level0_dirty()
1375 zbr = &znode->zbranch[*n]; in lookup_level0_dirty()
1377 if (zbr->znode) { in lookup_level0_dirty()
1378 znode->time = time; in lookup_level0_dirty()
1395 if (exact || !is_hash_key(c, key) || *n != -1) { in lookup_level0_dirty()
1396 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in lookup_level0_dirty()
1405 if (err == -ENOENT) { in lookup_level0_dirty()
1406 *n = -1; in lookup_level0_dirty()
1407 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1412 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in lookup_level0_dirty()
1413 *n = -1; in lookup_level0_dirty()
1414 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1418 if (znode->cnext || !ubifs_zn_dirty(znode)) { in lookup_level0_dirty()
1424 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in lookup_level0_dirty()
1426 return 1; in lookup_level0_dirty()
1430 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1431 * @c: UBIFS file-system description object
1436 * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1443 gced_lnum = c->gced_lnum; in maybe_leb_gced()
1445 gc_seq2 = c->gc_seq; in maybe_leb_gced()
1449 /* Different by more than 1 means we don't know */ in maybe_leb_gced()
1450 if (gc_seq1 + 1 != gc_seq2) in maybe_leb_gced()
1451 return 1; in maybe_leb_gced()
1453 * We have seen the sequence number has increased by 1. Now we need to in maybe_leb_gced()
1457 if (gced_lnum != c->gced_lnum) in maybe_leb_gced()
1458 return 1; in maybe_leb_gced()
1461 return 1; in maybe_leb_gced()
1466 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1467 * @c: UBIFS file-system description object
1468 * @key: node key to lookup
1473 * This function looks up and reads node with key @key. The caller has to make
1475 * of success, %-ENOENT if the node was not found, and a negative error code in
1478 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_locate() argument
1486 mutex_lock(&c->tnc_mutex); in ubifs_tnc_locate()
1487 found = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_locate()
1489 err = -ENOENT; in ubifs_tnc_locate()
1495 zt = &znode->zbranch[n]; in ubifs_tnc_locate()
1497 *lnum = zt->lnum; in ubifs_tnc_locate()
1498 *offs = zt->offs; in ubifs_tnc_locate()
1500 if (is_hash_key(c, key)) { in ubifs_tnc_locate()
1513 zbr = znode->zbranch[n]; in ubifs_tnc_locate()
1514 gc_seq1 = c->gc_seq; in ubifs_tnc_locate()
1515 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1523 err = fallible_read_node(c, key, &zbr, node); in ubifs_tnc_locate()
1529 safely = 1; in ubifs_tnc_locate()
1535 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1540 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1541 * @c: UBIFS file-system description object
1542 * @bu: bulk-read parameters and results
1548 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1549 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1550 * maximum possible amount of nodes for bulk-read.
1554 int n, err = 0, lnum = -1, offs; in ubifs_tnc_get_bu_keys()
1556 unsigned int block = key_block(c, &bu->key); in ubifs_tnc_get_bu_keys()
1559 bu->cnt = 0; in ubifs_tnc_get_bu_keys()
1560 bu->blk_cnt = 0; in ubifs_tnc_get_bu_keys()
1561 bu->eof = 0; in ubifs_tnc_get_bu_keys()
1563 mutex_lock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1564 /* Find first key */ in ubifs_tnc_get_bu_keys()
1565 err = ubifs_lookup_level0(c, &bu->key, &znode, &n); in ubifs_tnc_get_bu_keys()
1569 /* Key found */ in ubifs_tnc_get_bu_keys()
1570 len = znode->zbranch[n].len; in ubifs_tnc_get_bu_keys()
1571 /* The buffer must be big enough for at least 1 node */ in ubifs_tnc_get_bu_keys()
1572 if (len > bu->buf_len) { in ubifs_tnc_get_bu_keys()
1573 err = -EINVAL; in ubifs_tnc_get_bu_keys()
1576 /* Add this key */ in ubifs_tnc_get_bu_keys()
1577 bu->zbranch[bu->cnt++] = znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1578 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1579 lnum = znode->zbranch[n].lnum; in ubifs_tnc_get_bu_keys()
1580 offs = ALIGN(znode->zbranch[n].offs + len, 8); in ubifs_tnc_get_bu_keys()
1582 while (1) { in ubifs_tnc_get_bu_keys()
1584 union ubifs_key *key; in ubifs_tnc_get_bu_keys() local
1587 /* Find next key */ in ubifs_tnc_get_bu_keys()
1591 zbr = &znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1592 key = &zbr->key; in ubifs_tnc_get_bu_keys()
1593 /* See if there is another data key for this file */ in ubifs_tnc_get_bu_keys()
1594 if (key_inum(c, key) != key_inum(c, &bu->key) || in ubifs_tnc_get_bu_keys()
1595 key_type(c, key) != UBIFS_DATA_KEY) { in ubifs_tnc_get_bu_keys()
1596 err = -ENOENT; in ubifs_tnc_get_bu_keys()
1600 /* First key found */ in ubifs_tnc_get_bu_keys()
1601 lnum = zbr->lnum; in ubifs_tnc_get_bu_keys()
1602 offs = ALIGN(zbr->offs + zbr->len, 8); in ubifs_tnc_get_bu_keys()
1603 len = zbr->len; in ubifs_tnc_get_bu_keys()
1604 if (len > bu->buf_len) { in ubifs_tnc_get_bu_keys()
1605 err = -EINVAL; in ubifs_tnc_get_bu_keys()
1613 if (zbr->lnum != lnum || zbr->offs != offs) in ubifs_tnc_get_bu_keys()
1615 offs += ALIGN(zbr->len, 8); in ubifs_tnc_get_bu_keys()
1616 len = ALIGN(len, 8) + zbr->len; in ubifs_tnc_get_bu_keys()
1618 if (len > bu->buf_len) in ubifs_tnc_get_bu_keys()
1622 next_block = key_block(c, key); in ubifs_tnc_get_bu_keys()
1623 bu->blk_cnt += (next_block - block - 1); in ubifs_tnc_get_bu_keys()
1624 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1627 /* Add this key */ in ubifs_tnc_get_bu_keys()
1628 bu->zbranch[bu->cnt++] = *zbr; in ubifs_tnc_get_bu_keys()
1629 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1631 if (bu->cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1633 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1637 if (err == -ENOENT) { in ubifs_tnc_get_bu_keys()
1638 bu->eof = 1; in ubifs_tnc_get_bu_keys()
1641 bu->gc_seq = c->gc_seq; in ubifs_tnc_get_bu_keys()
1642 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1646 * An enormous hole could cause bulk-read to encompass too many in ubifs_tnc_get_bu_keys()
1649 if (bu->blk_cnt > UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1650 bu->blk_cnt = UBIFS_MAX_BULK_READ; in ubifs_tnc_get_bu_keys()
1652 * Ensure that bulk-read covers a whole number of page cache in ubifs_tnc_get_bu_keys()
1655 if (UBIFS_BLOCKS_PER_PAGE == 1 || in ubifs_tnc_get_bu_keys()
1656 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1))) in ubifs_tnc_get_bu_keys()
1658 if (bu->eof) { in ubifs_tnc_get_bu_keys()
1660 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1; in ubifs_tnc_get_bu_keys()
1664 block = key_block(c, &bu->key) + bu->blk_cnt; in ubifs_tnc_get_bu_keys()
1665 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1); in ubifs_tnc_get_bu_keys()
1666 while (bu->cnt) { in ubifs_tnc_get_bu_keys()
1667 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block) in ubifs_tnc_get_bu_keys()
1669 bu->cnt -= 1; in ubifs_tnc_get_bu_keys()
1675 * read_wbuf - bulk-read from a LEB with a wbuf.
1687 const struct ubifs_info *c = wbuf->c; in read_wbuf()
1691 ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in read_wbuf()
1692 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in read_wbuf()
1693 ubifs_assert(c, offs + len <= c->leb_size); in read_wbuf()
1695 spin_lock(&wbuf->lock); in read_wbuf()
1696 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in read_wbuf()
1698 /* We may safely unlock the write-buffer and read the data */ in read_wbuf()
1699 spin_unlock(&wbuf->lock); in read_wbuf()
1704 rlen = wbuf->offs - offs; in read_wbuf()
1708 /* Copy the rest from the write-buffer */ in read_wbuf()
1709 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in read_wbuf()
1710 spin_unlock(&wbuf->lock); in read_wbuf()
1713 /* Read everything that goes before write-buffer */ in read_wbuf()
1720 * validate_data_node - validate data nodes for bulk-read.
1721 * @c: UBIFS file-system description object
1734 if (ch->node_type != UBIFS_DATA_NODE) { in validate_data_node()
1736 ch->node_type, UBIFS_DATA_NODE); in validate_data_node()
1740 err = ubifs_check_node(c, buf, zbr->len, zbr->lnum, zbr->offs, 0, 0); in validate_data_node()
1746 err = ubifs_node_check_hash(c, buf, zbr->hash); in validate_data_node()
1748 ubifs_bad_hash(c, buf, zbr->hash, zbr->lnum, zbr->offs); in validate_data_node()
1752 len = le32_to_cpu(ch->len); in validate_data_node()
1753 if (len != zbr->len) { in validate_data_node()
1754 ubifs_err(c, "bad node length %d, expected %d", len, zbr->len); in validate_data_node()
1758 /* Make sure the key of the read node is correct */ in validate_data_node()
1760 if (!keys_eq(c, &zbr->key, &key1)) { in validate_data_node()
1761 ubifs_err(c, "bad key in node at LEB %d:%d", in validate_data_node()
1762 zbr->lnum, zbr->offs); in validate_data_node()
1763 dbg_tnck(&zbr->key, "looked for key "); in validate_data_node()
1764 dbg_tnck(&key1, "found node's key "); in validate_data_node()
1771 err = -EINVAL; in validate_data_node()
1773 ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs); in validate_data_node()
1774 ubifs_dump_node(c, buf, zbr->len); in validate_data_node()
1780 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1781 * @c: UBIFS file-system description object
1782 * @bu: bulk-read parameters and results
1786 * -EAGAIN to indicate a race with GC, or another negative error code on
1791 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i; in ubifs_tnc_bulk_read()
1795 len = bu->zbranch[bu->cnt - 1].offs; in ubifs_tnc_bulk_read()
1796 len += bu->zbranch[bu->cnt - 1].len - offs; in ubifs_tnc_bulk_read()
1797 if (len > bu->buf_len) { in ubifs_tnc_bulk_read()
1798 ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len); in ubifs_tnc_bulk_read()
1799 return -EINVAL; in ubifs_tnc_bulk_read()
1805 err = read_wbuf(wbuf, bu->buf, len, lnum, offs); in ubifs_tnc_bulk_read()
1807 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0); in ubifs_tnc_bulk_read()
1810 if (maybe_leb_gced(c, lnum, bu->gc_seq)) in ubifs_tnc_bulk_read()
1811 return -EAGAIN; in ubifs_tnc_bulk_read()
1813 if (err && err != -EBADMSG) { in ubifs_tnc_bulk_read()
1817 dbg_tnck(&bu->key, "key "); in ubifs_tnc_bulk_read()
1822 buf = bu->buf; in ubifs_tnc_bulk_read()
1823 for (i = 0; i < bu->cnt; i++) { in ubifs_tnc_bulk_read()
1824 err = validate_data_node(c, buf, &bu->zbranch[i]); in ubifs_tnc_bulk_read()
1827 buf = buf + ALIGN(bu->zbranch[i].len, 8); in ubifs_tnc_bulk_read()
1834 * do_lookup_nm- look up a "hashed" node.
1835 * @c: UBIFS file-system description object
1836 * @key: node key to lookup
1840 * This function looks up and reads a node which contains name hash in the key.
1842 * key, so we have to sequentially look to all of them until the needed one is
1843 * found. This function returns zero in case of success, %-ENOENT if the node
1846 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in do_lookup_nm() argument
1852 dbg_tnck(key, "key "); in do_lookup_nm()
1853 mutex_lock(&c->tnc_mutex); in do_lookup_nm()
1854 found = ubifs_lookup_level0(c, key, &znode, &n); in do_lookup_nm()
1856 err = -ENOENT; in do_lookup_nm()
1865 err = resolve_collision(c, key, &znode, &n, nm); in do_lookup_nm()
1870 err = -ENOENT; in do_lookup_nm()
1874 err = tnc_read_hashed_node(c, &znode->zbranch[n], node); in do_lookup_nm()
1877 mutex_unlock(&c->tnc_mutex); in do_lookup_nm()
1882 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1883 * @c: UBIFS file-system description object
1884 * @key: node key to lookup
1888 * This function looks up and reads a node which contains name hash in the key.
1890 * key, so we have to sequentially look to all of them until the needed one is
1891 * found. This function returns zero in case of success, %-ENOENT if the node
1894 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_lookup_nm() argument
1904 err = ubifs_tnc_lookup(c, key, node); in ubifs_tnc_lookup_nm()
1908 len = le16_to_cpu(dent->nlen); in ubifs_tnc_lookup_nm()
1909 if (fname_len(nm) == len && !memcmp(dent->name, fname_name(nm), len)) in ubifs_tnc_lookup_nm()
1917 return do_lookup_nm(c, key, node, nm); in ubifs_tnc_lookup_nm()
1920 static int search_dh_cookie(struct ubifs_info *c, const union ubifs_key *key, in search_dh_cookie() argument
1936 zbr = &znode->zbranch[*n]; in search_dh_cookie()
1937 dkey = &zbr->key; in search_dh_cookie()
1939 if (key_inum(c, dkey) != key_inum(c, key) || in search_dh_cookie()
1940 key_type(c, dkey) != key_type(c, key)) { in search_dh_cookie()
1941 return -ENOENT; in search_dh_cookie()
1948 if (key_hash(c, key) == key_hash(c, dkey) && in search_dh_cookie()
1949 le32_to_cpu(dent->cookie) == cookie) { in search_dh_cookie()
1960 static int do_lookup_dh(struct ubifs_info *c, const union ubifs_key *key, in do_lookup_dh() argument
1967 ubifs_assert(c, is_hash_key(c, key)); in do_lookup_dh()
1969 lowest_dent_key(c, &start_key, key_inum(c, key)); in do_lookup_dh()
1971 mutex_lock(&c->tnc_mutex); in do_lookup_dh()
1976 err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err); in do_lookup_dh()
1979 mutex_unlock(&c->tnc_mutex); in do_lookup_dh()
1984 * ubifs_tnc_lookup_dh - look up a "double hashed" node.
1985 * @c: UBIFS file-system description object
1986 * @key: node key to lookup
1990 * This function looks up and reads a node which contains name hash in the key.
1992 * key, so we have to sequentially look to all of them until the needed one
1994 * This function returns zero in case of success, %-ENOENT if the node
1997 int ubifs_tnc_lookup_dh(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_lookup_dh() argument
2003 if (!c->double_hash) in ubifs_tnc_lookup_dh()
2004 return -EOPNOTSUPP; in ubifs_tnc_lookup_dh()
2010 err = ubifs_tnc_lookup(c, key, node); in ubifs_tnc_lookup_dh()
2014 if (le32_to_cpu(dent->cookie) == cookie) in ubifs_tnc_lookup_dh()
2021 return do_lookup_dh(c, key, node, cookie); in ubifs_tnc_lookup_dh()
2025 * correct_parent_keys - correct parent znodes' keys.
2026 * @c: UBIFS file-system description object
2029 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
2036 union ubifs_key *key, *key1; in correct_parent_keys() local
2038 ubifs_assert(c, znode->parent); in correct_parent_keys()
2039 ubifs_assert(c, znode->iip == 0); in correct_parent_keys()
2041 key = &znode->zbranch[0].key; in correct_parent_keys()
2042 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
2044 while (keys_cmp(c, key, key1) < 0) { in correct_parent_keys()
2045 key_copy(c, key, key1); in correct_parent_keys()
2046 znode = znode->parent; in correct_parent_keys()
2047 znode->alt = 1; in correct_parent_keys()
2048 if (!znode->parent || znode->iip) in correct_parent_keys()
2050 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
2055 * insert_zbranch - insert a zbranch into a znode.
2056 * @c: UBIFS file-system description object
2063 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
2073 if (znode->level) { in insert_zbranch()
2074 for (i = znode->child_cnt; i > n; i--) { in insert_zbranch()
2075 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
2076 if (znode->zbranch[i].znode) in insert_zbranch()
2077 znode->zbranch[i].znode->iip = i; in insert_zbranch()
2079 if (zbr->znode) in insert_zbranch()
2080 zbr->znode->iip = n; in insert_zbranch()
2082 for (i = znode->child_cnt; i > n; i--) in insert_zbranch()
2083 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
2085 znode->zbranch[n] = *zbr; in insert_zbranch()
2086 znode->child_cnt += 1; in insert_zbranch()
2089 * After inserting at slot zero, the lower bound of the key range of in insert_zbranch()
2091 * then the upper bound of the key range may change, and furthermore in insert_zbranch()
2094 * TNC using the key from the index node on flash. That is bad because in insert_zbranch()
2103 znode->alt = 1; in insert_zbranch()
2107 * tnc_insert - insert a node into TNC.
2108 * @c: UBIFS file-system description object
2123 union ubifs_key *key = &zbr->key, *key1; in tnc_insert() local
2125 ubifs_assert(c, n >= 0 && n <= c->fanout); in tnc_insert()
2129 zp = znode->parent; in tnc_insert()
2130 if (znode->child_cnt < c->fanout) { in tnc_insert()
2131 ubifs_assert(c, n != c->fanout); in tnc_insert()
2132 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level); in tnc_insert()
2136 /* Ensure parent's key is correct */ in tnc_insert()
2137 if (n == 0 && zp && znode->iip == 0) in tnc_insert()
2147 dbg_tnck(key, "splitting level %d, key ", znode->level); in tnc_insert()
2149 if (znode->alt) in tnc_insert()
2151 * We can no longer be sure of finding this znode by key, so we in tnc_insert()
2156 zn = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
2158 return -ENOMEM; in tnc_insert()
2159 zn->parent = zp; in tnc_insert()
2160 zn->level = znode->level; in tnc_insert()
2163 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) { in tnc_insert()
2165 if (n == c->fanout) { in tnc_insert()
2166 key1 = &znode->zbranch[n - 1].key; in tnc_insert()
2167 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2169 appending = 1; in tnc_insert()
2172 } else if (appending && n != c->fanout) { in tnc_insert()
2176 if (n >= (c->fanout + 1) / 2) { in tnc_insert()
2177 key1 = &znode->zbranch[0].key; in tnc_insert()
2178 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2180 key1 = &znode->zbranch[n].key; in tnc_insert()
2181 if (key_inum(c, key1) != key_inum(c, key) || in tnc_insert()
2184 move = c->fanout - keep; in tnc_insert()
2193 keep = c->fanout; in tnc_insert()
2196 keep = (c->fanout + 1) / 2; in tnc_insert()
2197 move = c->fanout - keep; in tnc_insert()
2208 move += 1; in tnc_insert()
2209 keep -= 1; in tnc_insert()
2213 n -= keep; in tnc_insert()
2214 /* Re-parent */ in tnc_insert()
2215 if (zn->level != 0) in tnc_insert()
2216 zbr->znode->parent = zn; in tnc_insert()
2221 __set_bit(DIRTY_ZNODE, &zn->flags); in tnc_insert()
2222 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2224 zn->child_cnt = move; in tnc_insert()
2225 znode->child_cnt = keep; in tnc_insert()
2231 zn->zbranch[i] = znode->zbranch[keep + i]; in tnc_insert()
2232 /* Re-parent */ in tnc_insert()
2233 if (zn->level != 0) in tnc_insert()
2234 if (zn->zbranch[i].znode) { in tnc_insert()
2235 zn->zbranch[i].znode->parent = zn; in tnc_insert()
2236 zn->zbranch[i].znode->iip = i; in tnc_insert()
2240 /* Insert new key and branch */ in tnc_insert()
2241 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level); in tnc_insert()
2247 if (n == 0 && zi == znode && znode->iip == 0) in tnc_insert()
2251 n = znode->iip + 1; in tnc_insert()
2254 zbr->key = zn->zbranch[0].key; in tnc_insert()
2255 zbr->znode = zn; in tnc_insert()
2256 zbr->lnum = 0; in tnc_insert()
2257 zbr->offs = 0; in tnc_insert()
2258 zbr->len = 0; in tnc_insert()
2265 dbg_tnc("creating new zroot at level %d", znode->level + 1); in tnc_insert()
2267 zi = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
2269 return -ENOMEM; in tnc_insert()
2271 zi->child_cnt = 2; in tnc_insert()
2272 zi->level = znode->level + 1; in tnc_insert()
2274 __set_bit(DIRTY_ZNODE, &zi->flags); in tnc_insert()
2275 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2277 zi->zbranch[0].key = znode->zbranch[0].key; in tnc_insert()
2278 zi->zbranch[0].znode = znode; in tnc_insert()
2279 zi->zbranch[0].lnum = c->zroot.lnum; in tnc_insert()
2280 zi->zbranch[0].offs = c->zroot.offs; in tnc_insert()
2281 zi->zbranch[0].len = c->zroot.len; in tnc_insert()
2282 zi->zbranch[1].key = zn->zbranch[0].key; in tnc_insert()
2283 zi->zbranch[1].znode = zn; in tnc_insert()
2285 c->zroot.lnum = 0; in tnc_insert()
2286 c->zroot.offs = 0; in tnc_insert()
2287 c->zroot.len = 0; in tnc_insert()
2288 c->zroot.znode = zi; in tnc_insert()
2290 zn->parent = zi; in tnc_insert()
2291 zn->iip = 1; in tnc_insert()
2292 znode->parent = zi; in tnc_insert()
2293 znode->iip = 0; in tnc_insert()
2299 * ubifs_tnc_add - add a node to TNC.
2300 * @c: UBIFS file-system description object
2301 * @key: key to add
2307 * This function adds a node with key @key to TNC. The node may be new or it may
2311 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum, in ubifs_tnc_add() argument
2317 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add()
2318 dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len); in ubifs_tnc_add()
2319 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add()
2328 key_copy(c, key, &zbr.key); in ubifs_tnc_add()
2329 err = tnc_insert(c, znode, &zbr, n + 1); in ubifs_tnc_add()
2330 } else if (found == 1) { in ubifs_tnc_add()
2331 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add()
2334 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add()
2335 zbr->lnum = lnum; in ubifs_tnc_add()
2336 zbr->offs = offs; in ubifs_tnc_add()
2337 zbr->len = len; in ubifs_tnc_add()
2338 ubifs_copy_hash(c, hash, zbr->hash); in ubifs_tnc_add()
2343 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add()
2349 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2350 * @c: UBIFS file-system description object
2351 * @key: key to add
2358 * This function replaces a node with key @key in the TNC only if the old node
2362 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_replace() argument
2368 mutex_lock(&c->tnc_mutex); in ubifs_tnc_replace()
2369 dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum, in ubifs_tnc_replace()
2371 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_replace()
2377 if (found == 1) { in ubifs_tnc_replace()
2378 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2381 if (zbr->lnum == old_lnum && zbr->offs == old_offs) { in ubifs_tnc_replace()
2383 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_replace()
2386 zbr->lnum = lnum; in ubifs_tnc_replace()
2387 zbr->offs = offs; in ubifs_tnc_replace()
2388 zbr->len = len; in ubifs_tnc_replace()
2389 found = 1; in ubifs_tnc_replace()
2390 } else if (is_hash_key(c, key)) { in ubifs_tnc_replace()
2391 found = resolve_collision_directly(c, key, &znode, &n, in ubifs_tnc_replace()
2402 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_replace()
2409 zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2411 err = ubifs_add_dirt(c, zbr->lnum, in ubifs_tnc_replace()
2412 zbr->len); in ubifs_tnc_replace()
2415 zbr->lnum = lnum; in ubifs_tnc_replace()
2416 zbr->offs = offs; in ubifs_tnc_replace()
2417 zbr->len = len; in ubifs_tnc_replace()
2429 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_replace()
2434 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2435 * @c: UBIFS file-system description object
2436 * @key: key to add
2446 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_add_nm() argument
2453 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2454 dbg_tnck(key, "LEB %d:%d, key ", lnum, offs); in ubifs_tnc_add_nm()
2455 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add_nm()
2461 if (found == 1) { in ubifs_tnc_add_nm()
2462 if (c->replaying) in ubifs_tnc_add_nm()
2463 found = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_add_nm()
2464 nm, 1); in ubifs_tnc_add_nm()
2466 found = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_add_nm()
2474 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_add_nm()
2482 if (found == 1) { in ubifs_tnc_add_nm()
2483 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add_nm()
2486 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add_nm()
2487 zbr->lnum = lnum; in ubifs_tnc_add_nm()
2488 zbr->offs = offs; in ubifs_tnc_add_nm()
2489 zbr->len = len; in ubifs_tnc_add_nm()
2490 ubifs_copy_hash(c, hash, zbr->hash); in ubifs_tnc_add_nm()
2503 key_copy(c, key, &zbr.key); in ubifs_tnc_add_nm()
2504 err = tnc_insert(c, znode, &zbr, n + 1); in ubifs_tnc_add_nm()
2507 if (c->replaying) { in ubifs_tnc_add_nm()
2511 * by passing 'ubifs_tnc_remove_nm()' the same key but in ubifs_tnc_add_nm()
2514 struct fscrypt_name noname = { .disk_name = { .name = "", .len = 1 } }; in ubifs_tnc_add_nm()
2517 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2520 return ubifs_tnc_remove_nm(c, key, &noname); in ubifs_tnc_add_nm()
2527 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2532 * tnc_delete - delete a znode form TNC.
2533 * @c: UBIFS file-system description object
2537 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2547 ubifs_assert(c, znode->level == 0); in tnc_delete()
2548 ubifs_assert(c, n >= 0 && n < c->fanout); in tnc_delete()
2549 dbg_tnck(&znode->zbranch[n].key, "deleting key "); in tnc_delete()
2551 zbr = &znode->zbranch[n]; in tnc_delete()
2554 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in tnc_delete()
2561 for (i = n; i < znode->child_cnt - 1; i++) in tnc_delete()
2562 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2563 znode->child_cnt -= 1; in tnc_delete()
2565 if (znode->child_cnt > 0) in tnc_delete()
2577 zp = znode->parent; in tnc_delete()
2578 n = znode->iip; in tnc_delete()
2580 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2586 if (znode->cnext) { in tnc_delete()
2587 __set_bit(OBSOLETE_ZNODE, &znode->flags); in tnc_delete()
2588 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2593 } while (znode->child_cnt == 1); /* while removing last child */ in tnc_delete()
2595 /* Remove from znode, entry n - 1 */ in tnc_delete()
2596 znode->child_cnt -= 1; in tnc_delete()
2597 ubifs_assert(c, znode->level != 0); in tnc_delete()
2598 for (i = n; i < znode->child_cnt; i++) { in tnc_delete()
2599 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2600 if (znode->zbranch[i].znode) in tnc_delete()
2601 znode->zbranch[i].znode->iip = i; in tnc_delete()
2605 * If this is the root and it has only 1 child then in tnc_delete()
2608 if (!znode->parent) { in tnc_delete()
2609 while (znode->child_cnt == 1 && znode->level != 0) { in tnc_delete()
2611 zbr = &znode->zbranch[0]; in tnc_delete()
2618 znode->parent = NULL; in tnc_delete()
2619 znode->iip = 0; in tnc_delete()
2620 if (c->zroot.len) { in tnc_delete()
2621 err = insert_old_idx(c, c->zroot.lnum, in tnc_delete()
2622 c->zroot.offs); in tnc_delete()
2626 c->zroot.lnum = zbr->lnum; in tnc_delete()
2627 c->zroot.offs = zbr->offs; in tnc_delete()
2628 c->zroot.len = zbr->len; in tnc_delete()
2629 c->zroot.znode = znode; in tnc_delete()
2632 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2634 if (zp->cnext) { in tnc_delete()
2635 __set_bit(OBSOLETE_ZNODE, &zp->flags); in tnc_delete()
2636 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2647 * ubifs_tnc_remove - remove an index entry of a node.
2648 * @c: UBIFS file-system description object
2649 * @key: key of node
2653 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key) in ubifs_tnc_remove() argument
2658 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove()
2659 dbg_tnck(key, "key "); in ubifs_tnc_remove()
2660 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove()
2665 if (found == 1) in ubifs_tnc_remove()
2671 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove()
2676 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2677 * @c: UBIFS file-system description object
2678 * @key: key of node
2683 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_remove_nm() argument
2689 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2690 dbg_tnck(key, "key "); in ubifs_tnc_remove_nm()
2691 err = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove_nm()
2696 if (c->replaying) in ubifs_tnc_remove_nm()
2697 err = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_remove_nm()
2700 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_remove_nm()
2706 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_nm()
2720 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2725 * ubifs_tnc_remove_dh - remove an index entry for a "double hashed" node.
2726 * @c: UBIFS file-system description object
2727 * @key: key of node
2732 int ubifs_tnc_remove_dh(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_remove_dh() argument
2740 if (!c->double_hash) in ubifs_tnc_remove_dh()
2741 return -EOPNOTSUPP; in ubifs_tnc_remove_dh()
2743 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_dh()
2744 err = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove_dh()
2748 zbr = &znode->zbranch[n]; in ubifs_tnc_remove_dh()
2751 err = -ENOMEM; in ubifs_tnc_remove_dh()
2760 if (le32_to_cpu(dent->cookie) != cookie) { in ubifs_tnc_remove_dh()
2763 lowest_dent_key(c, &start_key, key_inum(c, key)); in ubifs_tnc_remove_dh()
2769 err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err); in ubifs_tnc_remove_dh()
2774 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_dh()
2788 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_dh()
2793 * key_in_range - determine if a key falls within a range of keys.
2794 * @c: UBIFS file-system description object
2795 * @key: key to check
2796 * @from_key: lowest key in range
2797 * @to_key: highest key in range
2799 * This function returns %1 if the key is in range and %0 otherwise.
2801 static int key_in_range(struct ubifs_info *c, union ubifs_key *key, in key_in_range() argument
2804 if (keys_cmp(c, key, from_key) < 0) in key_in_range()
2806 if (keys_cmp(c, key, to_key) > 0) in key_in_range()
2808 return 1; in key_in_range()
2812 * ubifs_tnc_remove_range - remove index entries in range.
2813 * @c: UBIFS file-system description object
2814 * @from_key: lowest key to remove
2815 * @to_key: highest key to remove
2826 union ubifs_key *key; in ubifs_tnc_remove_range() local
2828 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2829 while (1) { in ubifs_tnc_remove_range()
2836 key = from_key; in ubifs_tnc_remove_range()
2839 if (err == -ENOENT) { in ubifs_tnc_remove_range()
2845 key = &znode->zbranch[n].key; in ubifs_tnc_remove_range()
2846 if (!key_in_range(c, key, from_key, to_key)) { in ubifs_tnc_remove_range()
2853 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_range()
2862 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) { in ubifs_tnc_remove_range()
2863 key = &znode->zbranch[i].key; in ubifs_tnc_remove_range()
2864 if (!key_in_range(c, key, from_key, to_key)) in ubifs_tnc_remove_range()
2866 lnc_free(&znode->zbranch[i]); in ubifs_tnc_remove_range()
2867 err = ubifs_add_dirt(c, znode->zbranch[i].lnum, in ubifs_tnc_remove_range()
2868 znode->zbranch[i].len); in ubifs_tnc_remove_range()
2873 dbg_tnck(key, "removing key "); in ubifs_tnc_remove_range()
2876 for (i = n + 1 + k; i < znode->child_cnt; i++) in ubifs_tnc_remove_range()
2877 znode->zbranch[i - k] = znode->zbranch[i]; in ubifs_tnc_remove_range()
2878 znode->child_cnt -= k; in ubifs_tnc_remove_range()
2890 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2895 * ubifs_tnc_remove_ino - remove an inode from TNC.
2896 * @c: UBIFS file-system description object
2916 while (1) { in ubifs_tnc_remove_ino()
2923 if (err == -ENOENT) in ubifs_tnc_remove_ino()
2929 xattr_inum = le64_to_cpu(xent->inum); in ubifs_tnc_remove_ino()
2930 dbg_tnc("xent '%s', ino %lu", xent->name, in ubifs_tnc_remove_ino()
2935 fname_name(&nm) = xent->name; in ubifs_tnc_remove_ino()
2936 fname_len(&nm) = le16_to_cpu(xent->nlen); in ubifs_tnc_remove_ino()
2955 key_read(c, &xent->key, &key1); in ubifs_tnc_remove_ino()
2966 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2967 * @c: UBIFS file-system description object
2968 * @key: key of last entry
2972 * after the given key (@key) if there is one. @nm is used to resolve
2975 * If the name of the current entry is not known and only the key is known,
2976 * @nm->name has to be %NULL. In this case the semantics of this function is a
2977 * little bit different and it returns the entry corresponding to this key, not
2978 * the next one. If the key was not found, the closest "right" entry is
2981 * If the fist entry has to be found, @key has to contain the lowest possible
2982 * key value for this inode and @name has to be %NULL.
2985 * in case of success, %-ENOENT is returned if no entry was found, and a
2989 union ubifs_key *key, in ubifs_tnc_next_ent() argument
2992 int n, err, type = key_type(c, key); in ubifs_tnc_next_ent()
2998 dbg_tnck(key, "key "); in ubifs_tnc_next_ent()
2999 ubifs_assert(c, is_hash_key(c, key)); in ubifs_tnc_next_ent()
3001 mutex_lock(&c->tnc_mutex); in ubifs_tnc_next_ent()
3002 err = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_next_ent()
3009 if (c->replaying) in ubifs_tnc_next_ent()
3010 err = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_next_ent()
3013 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_next_ent()
3032 * However, the given key does not exist in the TNC in ubifs_tnc_next_ent()
3042 zbr = &znode->zbranch[n]; in ubifs_tnc_next_ent()
3043 dent = kmalloc(zbr->len, GFP_NOFS); in ubifs_tnc_next_ent()
3045 err = -ENOMEM; in ubifs_tnc_next_ent()
3053 dkey = &zbr->key; in ubifs_tnc_next_ent()
3054 if (key_inum(c, dkey) != key_inum(c, key) || in ubifs_tnc_next_ent()
3056 err = -ENOENT; in ubifs_tnc_next_ent()
3064 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
3070 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
3075 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
3076 * @c: UBIFS file-system description object
3078 * Destroy left-over obsolete znodes from a failed commit.
3084 if (!c->cnext) in tnc_destroy_cnext()
3086 ubifs_assert(c, c->cmt_state == COMMIT_BROKEN); in tnc_destroy_cnext()
3087 cnext = c->cnext; in tnc_destroy_cnext()
3091 cnext = cnext->cnext; in tnc_destroy_cnext()
3098 * count while closing tnc. Non-obsolete znode could in tnc_destroy_cnext()
3099 * be re-dirtied during committing process, so dirty in tnc_destroy_cnext()
3106 atomic_long_inc(&c->clean_zn_cnt); in tnc_destroy_cnext()
3109 } while (cnext && cnext != c->cnext); in tnc_destroy_cnext()
3113 * ubifs_tnc_close - close TNC subsystem and free all related resources.
3114 * @c: UBIFS file-system description object
3120 kfree(c->gap_lebs); in ubifs_tnc_close()
3121 kfree(c->ilebs); in ubifs_tnc_close()
3126 * left_znode - get the znode to the left.
3127 * @c: UBIFS file-system description object
3136 int level = znode->level; in left_znode()
3138 while (1) { in left_znode()
3139 int n = znode->iip - 1; in left_znode()
3142 znode = znode->parent; in left_znode()
3150 while (znode->level != level) { in left_znode()
3151 n = znode->child_cnt - 1; in left_znode()
3163 * right_znode - get the znode to the right.
3164 * @c: UBIFS file-system description object
3173 int level = znode->level; in right_znode()
3175 while (1) { in right_znode()
3176 int n = znode->iip + 1; in right_znode()
3179 znode = znode->parent; in right_znode()
3182 if (n < znode->child_cnt) { in right_znode()
3187 while (znode->level != level) { in right_znode()
3199 * lookup_znode - find a particular indexing node from TNC.
3200 * @c: UBIFS file-system description object
3201 * @key: index node key to lookup
3206 * This function searches an indexing node by its first key @key and its
3209 * were found on the media by scanning, for example when garbage-collecting or
3210 * when doing in-the-gaps commit. This means that the indexing node which is
3211 * looked for does not have to have exactly the same leftmost key @key, because
3212 * the leftmost key may have been changed, in which case TNC will contain a
3217 * not find it. For situations like this UBIFS has the old index RB-tree
3224 union ubifs_key *key, int level, in lookup_znode() argument
3230 ubifs_assert(c, key_type(c, key) < UBIFS_INVALID_KEY); in lookup_znode()
3237 return ERR_PTR(-EINVAL); in lookup_znode()
3240 znode = c->zroot.znode; in lookup_znode()
3242 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_znode()
3247 if (c->zroot.lnum == lnum && c->zroot.offs == offs) in lookup_znode()
3249 /* Descend to the parent level i.e. (level + 1) */ in lookup_znode()
3250 if (level >= znode->level) in lookup_znode()
3252 while (1) { in lookup_znode()
3253 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3256 * We reached a znode where the leftmost key is greater in lookup_znode()
3257 * than the key we are searching for. This is the same in lookup_znode()
3268 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3271 if (znode->level == level + 1) in lookup_znode()
3278 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs) in lookup_znode()
3280 /* If the key is unique, there is nowhere else to look */ in lookup_znode()
3281 if (!is_hash_key(c, key)) in lookup_znode()
3284 * The key is not unique and so may be also in the znodes to either in lookup_znode()
3290 while (1) { in lookup_znode()
3293 n -= 1; in lookup_znode()
3300 n = znode->child_cnt - 1; in lookup_znode()
3303 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3304 znode->zbranch[n].offs == offs) in lookup_znode()
3306 /* Stop if the key is less than the one we are looking for */ in lookup_znode()
3307 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0) in lookup_znode()
3314 while (1) { in lookup_znode()
3316 if (++n >= znode->child_cnt) { in lookup_znode()
3325 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3326 znode->zbranch[n].offs == offs) in lookup_znode()
3328 /* Stop if the key is greater than the one we are looking for */ in lookup_znode()
3329 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0) in lookup_znode()
3336 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3337 * @c: UBIFS file-system description object
3338 * @key: key of index node
3343 * This function returns %0 if the index node is not referred to in the TNC, %1
3348 * Note, the @key argument has to be the key of the first child. Also note,
3350 * offset for a main-area node.
3352 int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level, in is_idx_node_in_tnc() argument
3357 znode = lookup_znode(c, key, level, lnum, offs); in is_idx_node_in_tnc()
3363 return ubifs_zn_dirty(znode) ? 1 : 2; in is_idx_node_in_tnc()
3367 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3368 * @c: UBIFS file-system description object
3369 * @key: node key
3373 * This function returns %1 if the node is referred to in the TNC, %0 if it is
3377 * and offset for a main-area node.
3379 static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, in is_leaf_node_in_tnc() argument
3385 const int unique = !is_hash_key(c, key); in is_leaf_node_in_tnc()
3387 found = ubifs_lookup_level0(c, key, &znode, &n); in is_leaf_node_in_tnc()
3392 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3393 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3394 return 1; /* Found it */ in is_leaf_node_in_tnc()
3398 * Because the key is not unique, we have to look left in is_leaf_node_in_tnc()
3404 while (1) { in is_leaf_node_in_tnc()
3406 if (err == -ENOENT) in is_leaf_node_in_tnc()
3410 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3412 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3413 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3414 return 1; /* Found it */ in is_leaf_node_in_tnc()
3419 while (1) { in is_leaf_node_in_tnc()
3422 if (err == -ENOENT) in is_leaf_node_in_tnc()
3426 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3428 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3429 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3430 return 1; /* Found it */ in is_leaf_node_in_tnc()
3436 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3437 * @c: UBIFS file-system description object
3438 * @key: node key
3442 * @is_idx: non-zero if the node is an index node
3444 * This function returns %1 if the node is in the TNC, %0 if it is not, and a
3445 * negative error code in case of failure. For index nodes, @key has to be the
3446 * key of the first child. An index node is considered to be in the TNC only if
3449 int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_tnc_has_node() argument
3454 mutex_lock(&c->tnc_mutex); in ubifs_tnc_has_node()
3456 err = is_idx_node_in_tnc(c, key, level, lnum, offs); in ubifs_tnc_has_node()
3459 if (err == 1) in ubifs_tnc_has_node()
3464 err = 1; in ubifs_tnc_has_node()
3468 err = is_leaf_node_in_tnc(c, key, lnum, offs); in ubifs_tnc_has_node()
3471 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_has_node()
3476 * ubifs_dirty_idx_node - dirty an index node.
3477 * @c: UBIFS file-system description object
3478 * @key: index node key
3484 * collected. The @key argument has to be the key of the first child. This
3486 * for a main-area node. Returns %0 on success and a negative error code on
3489 int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_dirty_idx_node() argument
3495 mutex_lock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3496 znode = lookup_znode(c, key, level, lnum, offs); in ubifs_dirty_idx_node()
3510 mutex_unlock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3515 * dbg_check_inode_size - check if inode size is correct.
3516 * @c: UBIFS file-system description object
3521 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3529 union ubifs_key from_key, to_key, *key; in dbg_check_inode_size() local
3533 if (!S_ISREG(inode->i_mode)) in dbg_check_inode_size()
3538 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; in dbg_check_inode_size()
3539 data_key_init(c, &from_key, inode->i_ino, block); in dbg_check_inode_size()
3540 highest_data_key(c, &to_key, inode->i_ino); in dbg_check_inode_size()
3542 mutex_lock(&c->tnc_mutex); in dbg_check_inode_size()
3548 key = &from_key; in dbg_check_inode_size()
3553 if (err == -ENOENT) { in dbg_check_inode_size()
3561 key = &znode->zbranch[n].key; in dbg_check_inode_size()
3562 if (!key_in_range(c, key, &from_key, &to_key)) in dbg_check_inode_size()
3566 block = key_block(c, key); in dbg_check_inode_size()
3568 (unsigned long)inode->i_ino, size, in dbg_check_inode_size()
3570 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()
3573 return -EINVAL; in dbg_check_inode_size()
3576 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()