Lines Matching +full:key +full:- +full:code

1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2006-2008 Nokia Corporation.
13 * the UBIFS B-tree.
49 * insert_old_idx - record an index node obsoleted since the last commit start.
50 * @c: UBIFS file-system description object
54 * Returns %0 on success, and a negative error code on failure.
62 * That means that the garbage collection and the in-the-gaps method of
67 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
68 * That is what this function does. The RB-tree is ordered by LEB number and
78 return -ENOMEM; in insert_old_idx()
79 old_idx->lnum = lnum; in insert_old_idx()
80 old_idx->offs = offs; in insert_old_idx()
82 p = &c->old_idx.rb_node; in insert_old_idx()
86 if (lnum < o->lnum) in insert_old_idx()
87 p = &(*p)->rb_left; in insert_old_idx()
88 else if (lnum > o->lnum) in insert_old_idx()
89 p = &(*p)->rb_right; in insert_old_idx()
90 else if (offs < o->offs) in insert_old_idx()
91 p = &(*p)->rb_left; in insert_old_idx()
92 else if (offs > o->offs) in insert_old_idx()
93 p = &(*p)->rb_right; in insert_old_idx()
100 rb_link_node(&old_idx->rb, parent, p); in insert_old_idx()
101 rb_insert_color(&old_idx->rb, &c->old_idx); in insert_old_idx()
106 * insert_old_idx_znode - record a znode obsoleted since last commit start.
107 * @c: UBIFS file-system description object
110 * Returns %0 on success, and a negative error code on failure.
114 if (znode->parent) { in insert_old_idx_znode()
117 zbr = &znode->parent->zbranch[znode->iip]; in insert_old_idx_znode()
118 if (zbr->len) in insert_old_idx_znode()
119 return insert_old_idx(c, zbr->lnum, zbr->offs); in insert_old_idx_znode()
121 if (c->zroot.len) in insert_old_idx_znode()
122 return insert_old_idx(c, c->zroot.lnum, in insert_old_idx_znode()
123 c->zroot.offs); in insert_old_idx_znode()
128 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
129 * @c: UBIFS file-system description object
132 * Returns %0 on success, and a negative error code on failure.
139 if (znode->parent) { in ins_clr_old_idx_znode()
142 zbr = &znode->parent->zbranch[znode->iip]; in ins_clr_old_idx_znode()
143 if (zbr->len) { in ins_clr_old_idx_znode()
144 err = insert_old_idx(c, zbr->lnum, zbr->offs); in ins_clr_old_idx_znode()
147 zbr->lnum = 0; in ins_clr_old_idx_znode()
148 zbr->offs = 0; in ins_clr_old_idx_znode()
149 zbr->len = 0; in ins_clr_old_idx_znode()
152 if (c->zroot.len) { in ins_clr_old_idx_znode()
153 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs); in ins_clr_old_idx_znode()
156 c->zroot.lnum = 0; in ins_clr_old_idx_znode()
157 c->zroot.offs = 0; in ins_clr_old_idx_znode()
158 c->zroot.len = 0; in ins_clr_old_idx_znode()
164 * destroy_old_idx - destroy the old_idx RB-tree.
165 * @c: UBIFS file-system description object
167 * During start commit, the old_idx RB-tree is used to avoid overwriting index
170 * new index is successfully written. The old-idx RB-tree is used for the
171 * in-the-gaps method of writing index nodes and is destroyed every commit.
177 rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb) in destroy_old_idx()
180 c->old_idx = RB_ROOT; in destroy_old_idx()
184 * copy_znode - copy a dirty znode.
185 * @c: UBIFS file-system description object
195 zn = kmalloc(c->max_znode_sz, GFP_NOFS); in copy_znode()
197 return ERR_PTR(-ENOMEM); in copy_znode()
199 memcpy(zn, znode, c->max_znode_sz); in copy_znode()
200 zn->cnext = NULL; in copy_znode()
201 __set_bit(DIRTY_ZNODE, &zn->flags); in copy_znode()
202 __clear_bit(COW_ZNODE, &zn->flags); in copy_znode()
205 __set_bit(OBSOLETE_ZNODE, &znode->flags); in copy_znode()
207 if (znode->level != 0) { in copy_znode()
209 const int n = zn->child_cnt; in copy_znode()
213 struct ubifs_zbranch *zbr = &zn->zbranch[i]; in copy_znode()
215 if (zbr->znode) in copy_znode()
216 zbr->znode->parent = zn; in copy_znode()
220 atomic_long_inc(&c->dirty_zn_cnt); in copy_znode()
225 * add_idx_dirt - add dirt due to a dirty znode.
226 * @c: UBIFS file-system description object
234 c->calc_idx_sz -= ALIGN(dirt, 8); in add_idx_dirt()
239 * dirty_cow_znode - ensure a znode is not being committed.
240 * @c: UBIFS file-system description object
243 * Returns dirtied znode on success or negative error code on failure.
248 struct ubifs_znode *znode = zbr->znode; in dirty_cow_znode()
254 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) { in dirty_cow_znode()
255 atomic_long_inc(&c->dirty_zn_cnt); in dirty_cow_znode()
256 atomic_long_dec(&c->clean_zn_cnt); in dirty_cow_znode()
258 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
269 if (zbr->len) { in dirty_cow_znode()
270 err = insert_old_idx(c, zbr->lnum, zbr->offs); in dirty_cow_znode()
273 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
277 zbr->znode = zn; in dirty_cow_znode()
278 zbr->lnum = 0; in dirty_cow_znode()
279 zbr->offs = 0; in dirty_cow_znode()
280 zbr->len = 0; in dirty_cow_znode()
288 * lnc_add - add a leaf node to the leaf node cache.
289 * @c: UBIFS file-system description object
293 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
294 * purpose of the leaf node cache is to save re-reading the same leaf node over
304 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
314 ubifs_assert(!zbr->leaf); in lnc_add()
315 ubifs_assert(zbr->len != 0); in lnc_add()
316 ubifs_assert(is_hash_key(c, &zbr->key)); in lnc_add()
325 lnc_node = kmemdup(node, zbr->len, GFP_NOFS); in lnc_add()
330 zbr->leaf = lnc_node; in lnc_add()
335 * lnc_add_directly - add a leaf node to the leaf-node-cache.
336 * @c: UBIFS file-system description object
348 ubifs_assert(!zbr->leaf); in lnc_add_directly()
349 ubifs_assert(zbr->len != 0); in lnc_add_directly()
358 zbr->leaf = node; in lnc_add_directly()
363 * lnc_free - remove a leaf node from the leaf node cache.
369 if (!zbr->leaf) in lnc_free()
371 kfree(zbr->leaf); in lnc_free()
372 zbr->leaf = NULL; in lnc_free()
376 * tnc_read_node_nm - read a "hashed" leaf node.
377 * @c: UBIFS file-system description object
378 * @zbr: key and position of the node
384 * code in case of failure.
391 ubifs_assert(is_hash_key(c, &zbr->key)); in tnc_read_node_nm()
393 if (zbr->leaf) { in tnc_read_node_nm()
395 ubifs_assert(zbr->len != 0); in tnc_read_node_nm()
396 memcpy(node, zbr->leaf, zbr->len); in tnc_read_node_nm()
410 * try_read_node - read a node if it is a node.
411 * @c: UBIFS file-system description object
420 * a node is not present. A negative error code is returned for I/O errors.
423 * the return code indicates if a node was read.
425 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
427 * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
428 * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
429 * because during mounting or re-mounting from R/O mode to R/W mode we may read
449 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) in try_read_node()
452 if (ch->node_type != type) in try_read_node()
455 node_len = le32_to_cpu(ch->len); in try_read_node()
459 if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting && in try_read_node()
460 !c->remounting_rw) in try_read_node()
463 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in try_read_node()
464 node_crc = le32_to_cpu(ch->crc); in try_read_node()
472 * fallible_read_node - try to read a leaf node.
473 * @c: UBIFS file-system description object
474 * @key: key of node to read
479 * if the node is not present, and a negative error code in the case of error.
481 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key, in fallible_read_node() argument
486 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs); in fallible_read_node()
488 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum, in fallible_read_node()
489 zbr->offs); in fallible_read_node()
494 /* All nodes have key in the same place */ in fallible_read_node()
495 key_read(c, &dent->key, &node_key); in fallible_read_node()
496 if (keys_cmp(c, key, &node_key) != 0) in fallible_read_node()
499 if (ret == 0 && c->replaying) in fallible_read_node()
500 dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ", in fallible_read_node()
501 zbr->lnum, zbr->offs, zbr->len); in fallible_read_node()
506 * matches_name - determine if a direntry or xattr entry matches a given name.
507 * @c: UBIFS file-system description object
514 * of failure, a negative error code is returned.
523 if (!zbr->leaf) { in matches_name()
524 dent = kmalloc(zbr->len, GFP_NOFS); in matches_name()
526 return -ENOMEM; in matches_name()
537 dent = zbr->leaf; in matches_name()
539 nlen = le16_to_cpu(dent->nlen); in matches_name()
540 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len)); in matches_name()
542 if (nlen == nm->len) in matches_name()
544 else if (nlen < nm->len) in matches_name()
559 * get_znode - get a TNC znode that may not be loaded yet.
560 * @c: UBIFS file-system description object
564 * This function returns the znode or a negative error code.
571 zbr = &znode->zbranch[n]; in get_znode()
572 if (zbr->znode) in get_znode()
573 znode = zbr->znode; in get_znode()
580 * tnc_next - find next TNC entry.
581 * @c: UBIFS file-system description object
585 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
586 * no next entry, or a negative error code otherwise.
594 if (nn < znode->child_cnt) { in tnc_next()
601 zp = znode->parent; in tnc_next()
603 return -ENOENT; in tnc_next()
604 nn = znode->iip + 1; in tnc_next()
606 if (nn < znode->child_cnt) { in tnc_next()
610 while (znode->level != 0) { in tnc_next()
625 * tnc_prev - find previous TNC entry.
626 * @c: UBIFS file-system description object
630 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
631 * there is no next entry, or a negative error code otherwise.
639 *n = nn - 1; in tnc_prev()
645 zp = znode->parent; in tnc_prev()
647 return -ENOENT; in tnc_prev()
648 nn = znode->iip - 1; in tnc_prev()
654 while (znode->level != 0) { in tnc_prev()
655 nn = znode->child_cnt - 1; in tnc_prev()
660 nn = znode->child_cnt - 1; in tnc_prev()
670 * resolve_collision - resolve a collision.
671 * @c: UBIFS file-system description object
672 * @key: key of a directory or extended attribute entry
677 * This function is called for "hashed" keys to make sure that the found key
682 * This means that @n may be set to %-1 if the leftmost key in @zn is the
683 * previous one. A negative error code is returned on failures.
685 static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key, in resolve_collision() argument
691 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
701 if (err == -ENOENT) { in resolve_collision()
703 *n = -1; in resolve_collision()
708 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in resolve_collision()
717 * ---------------------- in resolve_collision()
719 * ----------------------- in resolve_collision()
722 * ------------ ------------ in resolve_collision()
724 * ------------ ------------ in resolve_collision()
736 * 'tnc_insert()' would correct the parent key. in resolve_collision()
738 if (*n == (*zn)->child_cnt - 1) { in resolve_collision()
743 if (err == -ENOENT) in resolve_collision()
744 err = -EINVAL; in resolve_collision()
748 *n = -1; in resolve_collision()
752 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
768 if (err == -ENOENT) in resolve_collision()
772 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision()
774 err = matches_name(c, &znode->zbranch[nn], nm); in resolve_collision()
789 * fallible_matches_name - determine if a dent matches a given name.
790 * @c: UBIFS file-system description object
801 * error code is returned in case of failure.
811 if (!zbr->leaf) { in fallible_matches_name()
812 dent = kmalloc(zbr->len, GFP_NOFS); in fallible_matches_name()
814 return -ENOMEM; in fallible_matches_name()
816 err = fallible_read_node(c, &zbr->key, zbr, dent); in fallible_matches_name()
830 dent = zbr->leaf; in fallible_matches_name()
832 nlen = le16_to_cpu(dent->nlen); in fallible_matches_name()
833 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len)); in fallible_matches_name()
835 if (nlen == nm->len) in fallible_matches_name()
837 else if (nlen < nm->len) in fallible_matches_name()
852 * fallible_resolve_collision - resolve a collision even if nodes are missing.
853 * @c: UBIFS file-system description object
854 * @key: key
858 * @adding: indicates caller is adding a key to the TNC
863 * Garbage-collected and the commit was not done. A branch that refers to a node
871 * o a negative error code is returned in case of failure.
874 const union ubifs_key *key, in fallible_resolve_collision() argument
881 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
892 * branch - to the left or to the right. Well, let's try left. in fallible_resolve_collision()
902 if (err == -ENOENT) { in fallible_resolve_collision()
904 *n = -1; in fallible_resolve_collision()
909 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in fallible_resolve_collision()
911 if (*n == (*zn)->child_cnt - 1) { in fallible_resolve_collision()
916 if (err == -ENOENT) in fallible_resolve_collision()
917 err = -EINVAL; in fallible_resolve_collision()
921 *n = -1; in fallible_resolve_collision()
925 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm); in fallible_resolve_collision()
950 if (err == -ENOENT) in fallible_resolve_collision()
954 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in fallible_resolve_collision()
956 err = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
976 dbg_mntk(key, "dangling match LEB %d:%d len %d key ", in fallible_resolve_collision()
977 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs, in fallible_resolve_collision()
978 o_znode->zbranch[o_n].len); in fallible_resolve_collision()
985 * matches_position - determine if a zbranch matches a given position.
994 if (zbr->lnum == lnum && zbr->offs == offs) in matches_position()
1001 * resolve_collision_directly - resolve a collision directly.
1002 * @c: UBIFS file-system description object
1003 * @key: key of directory entry
1015 * previous directory entry. Otherwise a negative error code is returned.
1018 const union ubifs_key *key, in resolve_collision_directly() argument
1027 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1033 if (err == -ENOENT) in resolve_collision_directly()
1037 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision_directly()
1039 if (matches_position(&znode->zbranch[nn], lnum, offs)) { in resolve_collision_directly()
1051 if (err == -ENOENT) in resolve_collision_directly()
1055 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision_directly()
1059 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1065 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1066 * @c: UBIFS file-system description object
1069 * If we do not have a unique key that resides in a znode, then we cannot
1078 int *path = c->bottom_up_buf, p = 0; in dirty_cow_bottom_up()
1080 ubifs_assert(c->zroot.znode); in dirty_cow_bottom_up()
1082 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) { in dirty_cow_bottom_up()
1083 kfree(c->bottom_up_buf); in dirty_cow_bottom_up()
1084 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int), in dirty_cow_bottom_up()
1086 if (!c->bottom_up_buf) in dirty_cow_bottom_up()
1087 return ERR_PTR(-ENOMEM); in dirty_cow_bottom_up()
1088 path = c->bottom_up_buf; in dirty_cow_bottom_up()
1090 if (c->zroot.znode->level) { in dirty_cow_bottom_up()
1095 zp = znode->parent; in dirty_cow_bottom_up()
1098 n = znode->iip; in dirty_cow_bottom_up()
1099 ubifs_assert(p < c->zroot.znode->level); in dirty_cow_bottom_up()
1101 if (!zp->cnext && ubifs_zn_dirty(znode)) in dirty_cow_bottom_up()
1111 zp = znode->parent; in dirty_cow_bottom_up()
1113 ubifs_assert(path[p - 1] >= 0); in dirty_cow_bottom_up()
1114 ubifs_assert(path[p - 1] < zp->child_cnt); in dirty_cow_bottom_up()
1115 zbr = &zp->zbranch[path[--p]]; in dirty_cow_bottom_up()
1118 ubifs_assert(znode == c->zroot.znode); in dirty_cow_bottom_up()
1119 znode = dirty_cow_znode(c, &c->zroot); in dirty_cow_bottom_up()
1123 ubifs_assert(path[p - 1] >= 0); in dirty_cow_bottom_up()
1124 ubifs_assert(path[p - 1] < znode->child_cnt); in dirty_cow_bottom_up()
1125 znode = znode->zbranch[path[p - 1]].znode; in dirty_cow_bottom_up()
1132 * ubifs_lookup_level0 - search for zero-level znode.
1133 * @c: UBIFS file-system description object
1134 * @key: key to lookup
1138 * This function looks up the TNC tree and search for zero-level znode which
1139 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1141 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1143 * o not exact match, which means that zero-level znode does not contain
1144 * @key, then %0 is returned and slot number of the closest branch is stored
1146 * o @key is so small that it is even less than the lowest key of the
1147 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1151 * case of failure, a negative error code is returned.
1153 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key, in ubifs_lookup_level0() argument
1160 dbg_tnck(key, "search key "); in ubifs_lookup_level0()
1161 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY); in ubifs_lookup_level0()
1163 znode = c->zroot.znode; in ubifs_lookup_level0()
1165 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in ubifs_lookup_level0()
1170 znode->time = time; in ubifs_lookup_level0()
1175 exact = ubifs_search_zbranch(c, znode, key, n); in ubifs_lookup_level0()
1177 if (znode->level == 0) in ubifs_lookup_level0()
1182 zbr = &znode->zbranch[*n]; in ubifs_lookup_level0()
1184 if (zbr->znode) { in ubifs_lookup_level0()
1185 znode->time = time; in ubifs_lookup_level0()
1186 znode = zbr->znode; in ubifs_lookup_level0()
1197 if (exact || !is_hash_key(c, key) || *n != -1) { in ubifs_lookup_level0()
1198 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in ubifs_lookup_level0()
1203 * Here is a tricky place. We have not found the key and this is a in ubifs_lookup_level0()
1204 * "hashed" key, which may collide. The rest of the code deals with in ubifs_lookup_level0()
1219 * In the examples, if we are looking for key "5", we may reach nodes in ubifs_lookup_level0()
1221 * left and see if there is "5" key there. If there is, we have to in ubifs_lookup_level0()
1225 * elements which are equivalent to the next key in the parent in the in ubifs_lookup_level0()
1233 * And this becomes what is at the first "picture" after key "5" marked in ubifs_lookup_level0()
1236 * removing the leftmost key, we would have to correct the key of the in ubifs_lookup_level0()
1238 * if we changed the leftmost key of the parent znode, the garbage in ubifs_lookup_level0()
1240 * indexing LEBs). Although we already have an additional RB-tree where in ubifs_lookup_level0()
1246 if (err == -ENOENT) { in ubifs_lookup_level0()
1247 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1248 *n = -1; in ubifs_lookup_level0()
1253 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in ubifs_lookup_level0()
1254 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1255 *n = -1; in ubifs_lookup_level0()
1259 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in ubifs_lookup_level0()
1265 * lookup_level0_dirty - search for zero-level znode dirtying.
1266 * @c: UBIFS file-system description object
1267 * @key: key to lookup
1271 * This function looks up the TNC tree and search for zero-level znode which
1272 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1274 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1276 * o not exact match, which means that zero-level znode does not contain @key
1279 * o @key is so small that it is even less than the lowest key of the
1280 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1282 * Additionally all znodes in the path from the root to the located zero-level
1287 * case of failure, a negative error code is returned.
1289 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key, in lookup_level0_dirty() argument
1296 dbg_tnck(key, "search and dirty key "); in lookup_level0_dirty()
1298 znode = c->zroot.znode; in lookup_level0_dirty()
1300 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_level0_dirty()
1305 znode = dirty_cow_znode(c, &c->zroot); in lookup_level0_dirty()
1309 znode->time = time; in lookup_level0_dirty()
1314 exact = ubifs_search_zbranch(c, znode, key, n); in lookup_level0_dirty()
1316 if (znode->level == 0) in lookup_level0_dirty()
1321 zbr = &znode->zbranch[*n]; in lookup_level0_dirty()
1323 if (zbr->znode) { in lookup_level0_dirty()
1324 znode->time = time; in lookup_level0_dirty()
1341 if (exact || !is_hash_key(c, key) || *n != -1) { in lookup_level0_dirty()
1342 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in lookup_level0_dirty()
1348 * code. in lookup_level0_dirty()
1351 if (err == -ENOENT) { in lookup_level0_dirty()
1352 *n = -1; in lookup_level0_dirty()
1353 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1358 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in lookup_level0_dirty()
1359 *n = -1; in lookup_level0_dirty()
1360 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1364 if (znode->cnext || !ubifs_zn_dirty(znode)) { in lookup_level0_dirty()
1370 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in lookup_level0_dirty()
1376 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1377 * @c: UBIFS file-system description object
1390 gced_lnum = c->gced_lnum; in maybe_leb_gced()
1392 gc_seq2 = c->gc_seq; in maybe_leb_gced()
1404 if (gced_lnum != c->gced_lnum) in maybe_leb_gced()
1410 /* No garbage collection in the read-only U-Boot implementation */ in maybe_leb_gced()
1416 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1417 * @c: UBIFS file-system description object
1418 * @key: node key to lookup
1423 * This function looks up and reads node with key @key. The caller has to make
1425 * of success, %-ENOENT if the node was not found, and a negative error code in
1428 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_locate() argument
1436 mutex_lock(&c->tnc_mutex); in ubifs_tnc_locate()
1437 found = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_locate()
1439 err = -ENOENT; in ubifs_tnc_locate()
1445 zt = &znode->zbranch[n]; in ubifs_tnc_locate()
1447 *lnum = zt->lnum; in ubifs_tnc_locate()
1448 *offs = zt->offs; in ubifs_tnc_locate()
1450 if (is_hash_key(c, key)) { in ubifs_tnc_locate()
1463 zbr = znode->zbranch[n]; in ubifs_tnc_locate()
1464 gc_seq1 = c->gc_seq; in ubifs_tnc_locate()
1465 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1473 err = fallible_read_node(c, key, &zbr, node); in ubifs_tnc_locate()
1485 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1490 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1491 * @c: UBIFS file-system description object
1492 * @bu: bulk-read parameters and results
1496 * and a negative error code in case of failure.
1498 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1499 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1500 * maximum possible amount of nodes for bulk-read.
1504 int n, err = 0, lnum = -1, uninitialized_var(offs); in ubifs_tnc_get_bu_keys()
1506 unsigned int block = key_block(c, &bu->key); in ubifs_tnc_get_bu_keys()
1509 bu->cnt = 0; in ubifs_tnc_get_bu_keys()
1510 bu->blk_cnt = 0; in ubifs_tnc_get_bu_keys()
1511 bu->eof = 0; in ubifs_tnc_get_bu_keys()
1513 mutex_lock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1514 /* Find first key */ in ubifs_tnc_get_bu_keys()
1515 err = ubifs_lookup_level0(c, &bu->key, &znode, &n); in ubifs_tnc_get_bu_keys()
1519 /* Key found */ in ubifs_tnc_get_bu_keys()
1520 len = znode->zbranch[n].len; in ubifs_tnc_get_bu_keys()
1522 if (len > bu->buf_len) { in ubifs_tnc_get_bu_keys()
1523 err = -EINVAL; in ubifs_tnc_get_bu_keys()
1526 /* Add this key */ in ubifs_tnc_get_bu_keys()
1527 bu->zbranch[bu->cnt++] = znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1528 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1529 lnum = znode->zbranch[n].lnum; in ubifs_tnc_get_bu_keys()
1530 offs = ALIGN(znode->zbranch[n].offs + len, 8); in ubifs_tnc_get_bu_keys()
1534 union ubifs_key *key; in ubifs_tnc_get_bu_keys() local
1537 /* Find next key */ in ubifs_tnc_get_bu_keys()
1541 zbr = &znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1542 key = &zbr->key; in ubifs_tnc_get_bu_keys()
1543 /* See if there is another data key for this file */ in ubifs_tnc_get_bu_keys()
1544 if (key_inum(c, key) != key_inum(c, &bu->key) || in ubifs_tnc_get_bu_keys()
1545 key_type(c, key) != UBIFS_DATA_KEY) { in ubifs_tnc_get_bu_keys()
1546 err = -ENOENT; in ubifs_tnc_get_bu_keys()
1550 /* First key found */ in ubifs_tnc_get_bu_keys()
1551 lnum = zbr->lnum; in ubifs_tnc_get_bu_keys()
1552 offs = ALIGN(zbr->offs + zbr->len, 8); in ubifs_tnc_get_bu_keys()
1553 len = zbr->len; in ubifs_tnc_get_bu_keys()
1554 if (len > bu->buf_len) { in ubifs_tnc_get_bu_keys()
1555 err = -EINVAL; in ubifs_tnc_get_bu_keys()
1563 if (zbr->lnum != lnum || zbr->offs != offs) in ubifs_tnc_get_bu_keys()
1565 offs += ALIGN(zbr->len, 8); in ubifs_tnc_get_bu_keys()
1566 len = ALIGN(len, 8) + zbr->len; in ubifs_tnc_get_bu_keys()
1568 if (len > bu->buf_len) in ubifs_tnc_get_bu_keys()
1572 next_block = key_block(c, key); in ubifs_tnc_get_bu_keys()
1573 bu->blk_cnt += (next_block - block - 1); in ubifs_tnc_get_bu_keys()
1574 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1577 /* Add this key */ in ubifs_tnc_get_bu_keys()
1578 bu->zbranch[bu->cnt++] = *zbr; in ubifs_tnc_get_bu_keys()
1579 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1581 if (bu->cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1583 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1587 if (err == -ENOENT) { in ubifs_tnc_get_bu_keys()
1588 bu->eof = 1; in ubifs_tnc_get_bu_keys()
1591 bu->gc_seq = c->gc_seq; in ubifs_tnc_get_bu_keys()
1592 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1596 * An enormous hole could cause bulk-read to encompass too many in ubifs_tnc_get_bu_keys()
1599 if (bu->blk_cnt > UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1600 bu->blk_cnt = UBIFS_MAX_BULK_READ; in ubifs_tnc_get_bu_keys()
1602 * Ensure that bulk-read covers a whole number of page cache in ubifs_tnc_get_bu_keys()
1606 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1))) in ubifs_tnc_get_bu_keys()
1608 if (bu->eof) { in ubifs_tnc_get_bu_keys()
1610 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1; in ubifs_tnc_get_bu_keys()
1614 block = key_block(c, &bu->key) + bu->blk_cnt; in ubifs_tnc_get_bu_keys()
1615 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1); in ubifs_tnc_get_bu_keys()
1616 while (bu->cnt) { in ubifs_tnc_get_bu_keys()
1617 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block) in ubifs_tnc_get_bu_keys()
1619 bu->cnt -= 1; in ubifs_tnc_get_bu_keys()
1625 * read_wbuf - bulk-read from a LEB with a wbuf.
1632 * This functions returns %0 on success or a negative error code on failure.
1637 const struct ubifs_info *c = wbuf->c; in read_wbuf()
1641 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in read_wbuf()
1642 ubifs_assert(!(offs & 7) && offs < c->leb_size); in read_wbuf()
1643 ubifs_assert(offs + len <= c->leb_size); in read_wbuf()
1645 spin_lock(&wbuf->lock); in read_wbuf()
1646 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in read_wbuf()
1648 /* We may safely unlock the write-buffer and read the data */ in read_wbuf()
1649 spin_unlock(&wbuf->lock); in read_wbuf()
1654 rlen = wbuf->offs - offs; in read_wbuf()
1658 /* Copy the rest from the write-buffer */ in read_wbuf()
1659 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in read_wbuf()
1660 spin_unlock(&wbuf->lock); in read_wbuf()
1663 /* Read everything that goes before write-buffer */ in read_wbuf()
1670 * validate_data_node - validate data nodes for bulk-read.
1671 * @c: UBIFS file-system description object
1675 * This functions returns %0 on success or a negative error code on failure.
1684 if (ch->node_type != UBIFS_DATA_NODE) { in validate_data_node()
1686 ch->node_type, UBIFS_DATA_NODE); in validate_data_node()
1690 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0); in validate_data_node()
1696 len = le32_to_cpu(ch->len); in validate_data_node()
1697 if (len != zbr->len) { in validate_data_node()
1698 ubifs_err(c, "bad node length %d, expected %d", len, zbr->len); in validate_data_node()
1702 /* Make sure the key of the read node is correct */ in validate_data_node()
1704 if (!keys_eq(c, &zbr->key, &key1)) { in validate_data_node()
1705 ubifs_err(c, "bad key in node at LEB %d:%d", in validate_data_node()
1706 zbr->lnum, zbr->offs); in validate_data_node()
1707 dbg_tnck(&zbr->key, "looked for key "); in validate_data_node()
1708 dbg_tnck(&key1, "found node's key "); in validate_data_node()
1715 err = -EINVAL; in validate_data_node()
1717 ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs); in validate_data_node()
1724 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1725 * @c: UBIFS file-system description object
1726 * @bu: bulk-read parameters and results
1730 * -EAGAIN to indicate a race with GC, or another negative error code on
1735 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i; in ubifs_tnc_bulk_read()
1739 len = bu->zbranch[bu->cnt - 1].offs; in ubifs_tnc_bulk_read()
1740 len += bu->zbranch[bu->cnt - 1].len - offs; in ubifs_tnc_bulk_read()
1741 if (len > bu->buf_len) { in ubifs_tnc_bulk_read()
1742 ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len); in ubifs_tnc_bulk_read()
1743 return -EINVAL; in ubifs_tnc_bulk_read()
1749 err = read_wbuf(wbuf, bu->buf, len, lnum, offs); in ubifs_tnc_bulk_read()
1751 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0); in ubifs_tnc_bulk_read()
1754 if (maybe_leb_gced(c, lnum, bu->gc_seq)) in ubifs_tnc_bulk_read()
1755 return -EAGAIN; in ubifs_tnc_bulk_read()
1757 if (err && err != -EBADMSG) { in ubifs_tnc_bulk_read()
1761 dbg_tnck(&bu->key, "key "); in ubifs_tnc_bulk_read()
1766 buf = bu->buf; in ubifs_tnc_bulk_read()
1767 for (i = 0; i < bu->cnt; i++) { in ubifs_tnc_bulk_read()
1768 err = validate_data_node(c, buf, &bu->zbranch[i]); in ubifs_tnc_bulk_read()
1771 buf = buf + ALIGN(bu->zbranch[i].len, 8); in ubifs_tnc_bulk_read()
1778 * do_lookup_nm- look up a "hashed" node.
1779 * @c: UBIFS file-system description object
1780 * @key: node key to lookup
1784 * This function look up and reads a node which contains name hash in the key.
1786 * key, so we have to sequentially look to all of them until the needed one is
1787 * found. This function returns zero in case of success, %-ENOENT if the node
1788 * was not found, and a negative error code in case of failure.
1790 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in do_lookup_nm() argument
1796 dbg_tnck(key, "name '%.*s' key ", nm->len, nm->name); in do_lookup_nm()
1797 mutex_lock(&c->tnc_mutex); in do_lookup_nm()
1798 found = ubifs_lookup_level0(c, key, &znode, &n); in do_lookup_nm()
1800 err = -ENOENT; in do_lookup_nm()
1809 err = resolve_collision(c, key, &znode, &n, nm); in do_lookup_nm()
1814 err = -ENOENT; in do_lookup_nm()
1818 err = tnc_read_node_nm(c, &znode->zbranch[n], node); in do_lookup_nm()
1821 mutex_unlock(&c->tnc_mutex); in do_lookup_nm()
1826 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1827 * @c: UBIFS file-system description object
1828 * @key: node key to lookup
1832 * This function look up and reads a node which contains name hash in the key.
1834 * key, so we have to sequentially look to all of them until the needed one is
1835 * found. This function returns zero in case of success, %-ENOENT if the node
1836 * was not found, and a negative error code in case of failure.
1838 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_lookup_nm() argument
1848 err = ubifs_tnc_lookup(c, key, node); in ubifs_tnc_lookup_nm()
1852 len = le16_to_cpu(dent->nlen); in ubifs_tnc_lookup_nm()
1853 if (nm->len == len && !memcmp(dent->name, nm->name, len)) in ubifs_tnc_lookup_nm()
1860 return do_lookup_nm(c, key, node, nm); in ubifs_tnc_lookup_nm()
1864 * correct_parent_keys - correct parent znodes' keys.
1865 * @c: UBIFS file-system description object
1868 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1875 union ubifs_key *key, *key1; in correct_parent_keys() local
1877 ubifs_assert(znode->parent); in correct_parent_keys()
1878 ubifs_assert(znode->iip == 0); in correct_parent_keys()
1880 key = &znode->zbranch[0].key; in correct_parent_keys()
1881 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
1883 while (keys_cmp(c, key, key1) < 0) { in correct_parent_keys()
1884 key_copy(c, key, key1); in correct_parent_keys()
1885 znode = znode->parent; in correct_parent_keys()
1886 znode->alt = 1; in correct_parent_keys()
1887 if (!znode->parent || znode->iip) in correct_parent_keys()
1889 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
1894 * insert_zbranch - insert a zbranch into a znode.
1901 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1911 if (znode->level) { in insert_zbranch()
1912 for (i = znode->child_cnt; i > n; i--) { in insert_zbranch()
1913 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
1914 if (znode->zbranch[i].znode) in insert_zbranch()
1915 znode->zbranch[i].znode->iip = i; in insert_zbranch()
1917 if (zbr->znode) in insert_zbranch()
1918 zbr->znode->iip = n; in insert_zbranch()
1920 for (i = znode->child_cnt; i > n; i--) in insert_zbranch()
1921 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
1923 znode->zbranch[n] = *zbr; in insert_zbranch()
1924 znode->child_cnt += 1; in insert_zbranch()
1927 * After inserting at slot zero, the lower bound of the key range of in insert_zbranch()
1929 * then the upper bound of the key range may change, and furthermore in insert_zbranch()
1932 * TNC using the key from the index node on flash. That is bad because in insert_zbranch()
1941 znode->alt = 1; in insert_zbranch()
1945 * tnc_insert - insert a node into TNC.
1946 * @c: UBIFS file-system description object
1954 * error code in case of failure.
1961 union ubifs_key *key = &zbr->key, *key1; in tnc_insert() local
1963 ubifs_assert(n >= 0 && n <= c->fanout); in tnc_insert()
1967 zp = znode->parent; in tnc_insert()
1968 if (znode->child_cnt < c->fanout) { in tnc_insert()
1969 ubifs_assert(n != c->fanout); in tnc_insert()
1970 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level); in tnc_insert()
1974 /* Ensure parent's key is correct */ in tnc_insert()
1975 if (n == 0 && zp && znode->iip == 0) in tnc_insert()
1985 dbg_tnck(key, "splitting level %d, key ", znode->level); in tnc_insert()
1987 if (znode->alt) in tnc_insert()
1989 * We can no longer be sure of finding this znode by key, so we in tnc_insert()
1994 zn = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
1996 return -ENOMEM; in tnc_insert()
1997 zn->parent = zp; in tnc_insert()
1998 zn->level = znode->level; in tnc_insert()
2001 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) { in tnc_insert()
2003 if (n == c->fanout) { in tnc_insert()
2004 key1 = &znode->zbranch[n - 1].key; in tnc_insert()
2005 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2010 } else if (appending && n != c->fanout) { in tnc_insert()
2014 if (n >= (c->fanout + 1) / 2) { in tnc_insert()
2015 key1 = &znode->zbranch[0].key; in tnc_insert()
2016 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2018 key1 = &znode->zbranch[n].key; in tnc_insert()
2019 if (key_inum(c, key1) != key_inum(c, key) || in tnc_insert()
2022 move = c->fanout - keep; in tnc_insert()
2031 keep = c->fanout; in tnc_insert()
2034 keep = (c->fanout + 1) / 2; in tnc_insert()
2035 move = c->fanout - keep; in tnc_insert()
2047 keep -= 1; in tnc_insert()
2051 n -= keep; in tnc_insert()
2052 /* Re-parent */ in tnc_insert()
2053 if (zn->level != 0) in tnc_insert()
2054 zbr->znode->parent = zn; in tnc_insert()
2059 __set_bit(DIRTY_ZNODE, &zn->flags); in tnc_insert()
2060 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2062 zn->child_cnt = move; in tnc_insert()
2063 znode->child_cnt = keep; in tnc_insert()
2069 zn->zbranch[i] = znode->zbranch[keep + i]; in tnc_insert()
2070 /* Re-parent */ in tnc_insert()
2071 if (zn->level != 0) in tnc_insert()
2072 if (zn->zbranch[i].znode) { in tnc_insert()
2073 zn->zbranch[i].znode->parent = zn; in tnc_insert()
2074 zn->zbranch[i].znode->iip = i; in tnc_insert()
2078 /* Insert new key and branch */ in tnc_insert()
2079 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level); in tnc_insert()
2085 if (n == 0 && zi == znode && znode->iip == 0) in tnc_insert()
2089 n = znode->iip + 1; in tnc_insert()
2092 zbr->key = zn->zbranch[0].key; in tnc_insert()
2093 zbr->znode = zn; in tnc_insert()
2094 zbr->lnum = 0; in tnc_insert()
2095 zbr->offs = 0; in tnc_insert()
2096 zbr->len = 0; in tnc_insert()
2103 dbg_tnc("creating new zroot at level %d", znode->level + 1); in tnc_insert()
2105 zi = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
2107 return -ENOMEM; in tnc_insert()
2109 zi->child_cnt = 2; in tnc_insert()
2110 zi->level = znode->level + 1; in tnc_insert()
2112 __set_bit(DIRTY_ZNODE, &zi->flags); in tnc_insert()
2113 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2115 zi->zbranch[0].key = znode->zbranch[0].key; in tnc_insert()
2116 zi->zbranch[0].znode = znode; in tnc_insert()
2117 zi->zbranch[0].lnum = c->zroot.lnum; in tnc_insert()
2118 zi->zbranch[0].offs = c->zroot.offs; in tnc_insert()
2119 zi->zbranch[0].len = c->zroot.len; in tnc_insert()
2120 zi->zbranch[1].key = zn->zbranch[0].key; in tnc_insert()
2121 zi->zbranch[1].znode = zn; in tnc_insert()
2123 c->zroot.lnum = 0; in tnc_insert()
2124 c->zroot.offs = 0; in tnc_insert()
2125 c->zroot.len = 0; in tnc_insert()
2126 c->zroot.znode = zi; in tnc_insert()
2128 zn->parent = zi; in tnc_insert()
2129 zn->iip = 1; in tnc_insert()
2130 znode->parent = zi; in tnc_insert()
2131 znode->iip = 0; in tnc_insert()
2137 * ubifs_tnc_add - add a node to TNC.
2138 * @c: UBIFS file-system description object
2139 * @key: key to add
2144 * This function adds a node with key @key to TNC. The node may be new or it may
2145 * obsolete some existing one. Returns %0 on success or negative error code on
2148 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum, in ubifs_tnc_add() argument
2154 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add()
2155 dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len); in ubifs_tnc_add()
2156 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add()
2164 key_copy(c, key, &zbr.key); in ubifs_tnc_add()
2167 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add()
2170 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add()
2171 zbr->lnum = lnum; in ubifs_tnc_add()
2172 zbr->offs = offs; in ubifs_tnc_add()
2173 zbr->len = len; in ubifs_tnc_add()
2178 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add()
2184 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2185 * @c: UBIFS file-system description object
2186 * @key: key to add
2193 * This function replaces a node with key @key in the TNC only if the old node
2195 * Returns %0 on success or negative error code on failure.
2197 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_replace() argument
2203 mutex_lock(&c->tnc_mutex); in ubifs_tnc_replace()
2204 dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum, in ubifs_tnc_replace()
2206 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_replace()
2213 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2216 if (zbr->lnum == old_lnum && zbr->offs == old_offs) { in ubifs_tnc_replace()
2218 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_replace()
2221 zbr->lnum = lnum; in ubifs_tnc_replace()
2222 zbr->offs = offs; in ubifs_tnc_replace()
2223 zbr->len = len; in ubifs_tnc_replace()
2225 } else if (is_hash_key(c, key)) { in ubifs_tnc_replace()
2226 found = resolve_collision_directly(c, key, &znode, &n, in ubifs_tnc_replace()
2237 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_replace()
2244 zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2246 err = ubifs_add_dirt(c, zbr->lnum, in ubifs_tnc_replace()
2247 zbr->len); in ubifs_tnc_replace()
2250 zbr->lnum = lnum; in ubifs_tnc_replace()
2251 zbr->offs = offs; in ubifs_tnc_replace()
2252 zbr->len = len; in ubifs_tnc_replace()
2264 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_replace()
2269 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2270 * @c: UBIFS file-system description object
2271 * @key: key to add
2280 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_add_nm() argument
2286 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2287 dbg_tnck(key, "LEB %d:%d, name '%.*s', key ", in ubifs_tnc_add_nm()
2288 lnum, offs, nm->len, nm->name); in ubifs_tnc_add_nm()
2289 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add_nm()
2296 if (c->replaying) in ubifs_tnc_add_nm()
2297 found = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_add_nm()
2300 found = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_add_nm()
2308 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_add_nm()
2317 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add_nm()
2320 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add_nm()
2321 zbr->lnum = lnum; in ubifs_tnc_add_nm()
2322 zbr->offs = offs; in ubifs_tnc_add_nm()
2323 zbr->len = len; in ubifs_tnc_add_nm()
2335 key_copy(c, key, &zbr.key); in ubifs_tnc_add_nm()
2339 if (c->replaying) { in ubifs_tnc_add_nm()
2343 * by passing 'ubifs_tnc_remove_nm()' the same key but in ubifs_tnc_add_nm()
2349 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2352 return ubifs_tnc_remove_nm(c, key, &noname); in ubifs_tnc_add_nm()
2359 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2364 * tnc_delete - delete a znode form TNC.
2365 * @c: UBIFS file-system description object
2369 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2370 * case of success and a negative error code in case of failure.
2379 ubifs_assert(znode->level == 0); in tnc_delete()
2380 ubifs_assert(n >= 0 && n < c->fanout); in tnc_delete()
2381 dbg_tnck(&znode->zbranch[n].key, "deleting key "); in tnc_delete()
2383 zbr = &znode->zbranch[n]; in tnc_delete()
2386 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in tnc_delete()
2393 for (i = n; i < znode->child_cnt - 1; i++) in tnc_delete()
2394 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2395 znode->child_cnt -= 1; in tnc_delete()
2397 if (znode->child_cnt > 0) in tnc_delete()
2409 zp = znode->parent; in tnc_delete()
2410 n = znode->iip; in tnc_delete()
2412 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2418 if (znode->cnext) { in tnc_delete()
2419 __set_bit(OBSOLETE_ZNODE, &znode->flags); in tnc_delete()
2420 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2425 } while (znode->child_cnt == 1); /* while removing last child */ in tnc_delete()
2427 /* Remove from znode, entry n - 1 */ in tnc_delete()
2428 znode->child_cnt -= 1; in tnc_delete()
2429 ubifs_assert(znode->level != 0); in tnc_delete()
2430 for (i = n; i < znode->child_cnt; i++) { in tnc_delete()
2431 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2432 if (znode->zbranch[i].znode) in tnc_delete()
2433 znode->zbranch[i].znode->iip = i; in tnc_delete()
2440 if (!znode->parent) { in tnc_delete()
2441 while (znode->child_cnt == 1 && znode->level != 0) { in tnc_delete()
2443 zbr = &znode->zbranch[0]; in tnc_delete()
2450 znode->parent = NULL; in tnc_delete()
2451 znode->iip = 0; in tnc_delete()
2452 if (c->zroot.len) { in tnc_delete()
2453 err = insert_old_idx(c, c->zroot.lnum, in tnc_delete()
2454 c->zroot.offs); in tnc_delete()
2458 c->zroot.lnum = zbr->lnum; in tnc_delete()
2459 c->zroot.offs = zbr->offs; in tnc_delete()
2460 c->zroot.len = zbr->len; in tnc_delete()
2461 c->zroot.znode = znode; in tnc_delete()
2464 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2466 if (zp->cnext) { in tnc_delete()
2467 __set_bit(OBSOLETE_ZNODE, &zp->flags); in tnc_delete()
2468 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2479 * ubifs_tnc_remove - remove an index entry of a node.
2480 * @c: UBIFS file-system description object
2481 * @key: key of node
2483 * Returns %0 on success or negative error code on failure.
2485 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key) in ubifs_tnc_remove() argument
2490 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove()
2491 dbg_tnck(key, "key "); in ubifs_tnc_remove()
2492 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove()
2503 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove()
2508 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2509 * @c: UBIFS file-system description object
2510 * @key: key of node
2513 * Returns %0 on success or negative error code on failure.
2515 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_remove_nm() argument
2521 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2522 dbg_tnck(key, "%.*s, key ", nm->len, nm->name); in ubifs_tnc_remove_nm()
2523 err = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove_nm()
2528 if (c->replaying) in ubifs_tnc_remove_nm()
2529 err = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_remove_nm()
2532 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_remove_nm()
2538 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_nm()
2552 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2557 * key_in_range - determine if a key falls within a range of keys.
2558 * @c: UBIFS file-system description object
2559 * @key: key to check
2560 * @from_key: lowest key in range
2561 * @to_key: highest key in range
2563 * This function returns %1 if the key is in range and %0 otherwise.
2565 static int key_in_range(struct ubifs_info *c, union ubifs_key *key, in key_in_range() argument
2568 if (keys_cmp(c, key, from_key) < 0) in key_in_range()
2570 if (keys_cmp(c, key, to_key) > 0) in key_in_range()
2576 * ubifs_tnc_remove_range - remove index entries in range.
2577 * @c: UBIFS file-system description object
2578 * @from_key: lowest key to remove
2579 * @to_key: highest key to remove
2583 * code in case of failure.
2590 union ubifs_key *key; in ubifs_tnc_remove_range() local
2592 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2600 key = from_key; in ubifs_tnc_remove_range()
2603 if (err == -ENOENT) { in ubifs_tnc_remove_range()
2609 key = &znode->zbranch[n].key; in ubifs_tnc_remove_range()
2610 if (!key_in_range(c, key, from_key, to_key)) { in ubifs_tnc_remove_range()
2617 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_range()
2626 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) { in ubifs_tnc_remove_range()
2627 key = &znode->zbranch[i].key; in ubifs_tnc_remove_range()
2628 if (!key_in_range(c, key, from_key, to_key)) in ubifs_tnc_remove_range()
2630 lnc_free(&znode->zbranch[i]); in ubifs_tnc_remove_range()
2631 err = ubifs_add_dirt(c, znode->zbranch[i].lnum, in ubifs_tnc_remove_range()
2632 znode->zbranch[i].len); in ubifs_tnc_remove_range()
2637 dbg_tnck(key, "removing key "); in ubifs_tnc_remove_range()
2640 for (i = n + 1 + k; i < znode->child_cnt; i++) in ubifs_tnc_remove_range()
2641 znode->zbranch[i - k] = znode->zbranch[i]; in ubifs_tnc_remove_range()
2642 znode->child_cnt -= k; in ubifs_tnc_remove_range()
2654 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2659 * ubifs_tnc_remove_ino - remove an inode from TNC.
2660 * @c: UBIFS file-system description object
2665 * error code in case of failure.
2687 if (err == -ENOENT) in ubifs_tnc_remove_ino()
2692 xattr_inum = le64_to_cpu(xent->inum); in ubifs_tnc_remove_ino()
2693 dbg_tnc("xent '%s', ino %lu", xent->name, in ubifs_tnc_remove_ino()
2696 nm.name = xent->name; in ubifs_tnc_remove_ino()
2697 nm.len = le16_to_cpu(xent->nlen); in ubifs_tnc_remove_ino()
2714 key_read(c, &xent->key, &key1); in ubifs_tnc_remove_ino()
2725 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2726 * @c: UBIFS file-system description object
2727 * @key: key of last entry
2731 * after the given key (@key) if there is one. @nm is used to resolve
2734 * If the name of the current entry is not known and only the key is known,
2735 * @nm->name has to be %NULL. In this case the semantics of this function is a
2736 * little bit different and it returns the entry corresponding to this key, not
2737 * the next one. If the key was not found, the closest "right" entry is
2740 * If the fist entry has to be found, @key has to contain the lowest possible
2741 * key value for this inode and @name has to be %NULL.
2744 * in case of success, %-ENOENT is returned if no entry was found, and a
2745 * negative error code is returned in case of failure.
2748 union ubifs_key *key, in ubifs_tnc_next_ent() argument
2751 int n, err, type = key_type(c, key); in ubifs_tnc_next_ent()
2757 dbg_tnck(key, "%s ", nm->name ? (char *)nm->name : "(lowest)"); in ubifs_tnc_next_ent()
2758 ubifs_assert(is_hash_key(c, key)); in ubifs_tnc_next_ent()
2760 mutex_lock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2761 err = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_next_ent()
2765 if (nm->name) { in ubifs_tnc_next_ent()
2768 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_next_ent()
2787 * However, the given key does not exist in the TNC in ubifs_tnc_next_ent()
2797 zbr = &znode->zbranch[n]; in ubifs_tnc_next_ent()
2798 dent = kmalloc(zbr->len, GFP_NOFS); in ubifs_tnc_next_ent()
2800 err = -ENOMEM; in ubifs_tnc_next_ent()
2808 dkey = &zbr->key; in ubifs_tnc_next_ent()
2809 if (key_inum(c, dkey) != key_inum(c, key) || in ubifs_tnc_next_ent()
2811 err = -ENOENT; in ubifs_tnc_next_ent()
2819 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2825 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2830 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2831 * @c: UBIFS file-system description object
2833 * Destroy left-over obsolete znodes from a failed commit.
2839 if (!c->cnext) in tnc_destroy_cnext()
2841 ubifs_assert(c->cmt_state == COMMIT_BROKEN); in tnc_destroy_cnext()
2842 cnext = c->cnext; in tnc_destroy_cnext()
2846 cnext = cnext->cnext; in tnc_destroy_cnext()
2849 } while (cnext && cnext != c->cnext); in tnc_destroy_cnext()
2853 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2854 * @c: UBIFS file-system description object
2859 if (c->zroot.znode) { in ubifs_tnc_close()
2862 n = atomic_long_read(&c->clean_zn_cnt); in ubifs_tnc_close()
2863 freed = ubifs_destroy_tnc_subtree(c->zroot.znode); in ubifs_tnc_close()
2867 kfree(c->gap_lebs); in ubifs_tnc_close()
2868 kfree(c->ilebs); in ubifs_tnc_close()
2873 * left_znode - get the znode to the left.
2874 * @c: UBIFS file-system description object
2878 * there is not one. A negative error code is returned on failure.
2883 int level = znode->level; in left_znode()
2886 int n = znode->iip - 1; in left_znode()
2889 znode = znode->parent; in left_znode()
2897 while (znode->level != level) { in left_znode()
2898 n = znode->child_cnt - 1; in left_znode()
2910 * right_znode - get the znode to the right.
2911 * @c: UBIFS file-system description object
2915 * if there is not one. A negative error code is returned on failure.
2920 int level = znode->level; in right_znode()
2923 int n = znode->iip + 1; in right_znode()
2926 znode = znode->parent; in right_znode()
2929 if (n < znode->child_cnt) { in right_znode()
2934 while (znode->level != level) { in right_znode()
2946 * lookup_znode - find a particular indexing node from TNC.
2947 * @c: UBIFS file-system description object
2948 * @key: index node key to lookup
2953 * This function searches an indexing node by its first key @key and its
2956 * were found on the media by scanning, for example when garbage-collecting or
2957 * when doing in-the-gaps commit. This means that the indexing node which is
2958 * looked for does not have to have exactly the same leftmost key @key, because
2959 * the leftmost key may have been changed, in which case TNC will contain a
2964 * not find it. For situations like this UBIFS has the old index RB-tree
2968 * found. A negative error code is returned on failure.
2971 union ubifs_key *key, int level, in lookup_znode() argument
2977 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY); in lookup_znode()
2984 return ERR_PTR(-EINVAL); in lookup_znode()
2987 znode = c->zroot.znode; in lookup_znode()
2989 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_znode()
2994 if (c->zroot.lnum == lnum && c->zroot.offs == offs) in lookup_znode()
2997 if (level >= znode->level) in lookup_znode()
3000 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3003 * We reached a znode where the leftmost key is greater in lookup_znode()
3004 * than the key we are searching for. This is the same in lookup_znode()
3015 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3018 if (znode->level == level + 1) in lookup_znode()
3025 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs) in lookup_znode()
3027 /* If the key is unique, there is nowhere else to look */ in lookup_znode()
3028 if (!is_hash_key(c, key)) in lookup_znode()
3031 * The key is not unique and so may be also in the znodes to either in lookup_znode()
3040 n -= 1; in lookup_znode()
3047 n = znode->child_cnt - 1; in lookup_znode()
3050 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3051 znode->zbranch[n].offs == offs) in lookup_znode()
3053 /* Stop if the key is less than the one we are looking for */ in lookup_znode()
3054 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0) in lookup_znode()
3063 if (++n >= znode->child_cnt) { in lookup_znode()
3072 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3073 znode->zbranch[n].offs == offs) in lookup_znode()
3075 /* Stop if the key is greater than the one we are looking for */ in lookup_znode()
3076 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0) in lookup_znode()
3083 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3084 * @c: UBIFS file-system description object
3085 * @key: key of index node
3093 * znode is clean, and a negative error code in case of failure.
3095 * Note, the @key argument has to be the key of the first child. Also note,
3097 * offset for a main-area node.
3099 int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level, in is_idx_node_in_tnc() argument
3104 znode = lookup_znode(c, key, level, lnum, offs); in is_idx_node_in_tnc()
3114 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3115 * @c: UBIFS file-system description object
3116 * @key: node key
3121 * not, and a negative error code in case of failure.
3124 * and offset for a main-area node.
3126 static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, in is_leaf_node_in_tnc() argument
3132 const int unique = !is_hash_key(c, key); in is_leaf_node_in_tnc()
3134 found = ubifs_lookup_level0(c, key, &znode, &n); in is_leaf_node_in_tnc()
3136 return found; /* Error code */ in is_leaf_node_in_tnc()
3139 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3140 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3145 * Because the key is not unique, we have to look left in is_leaf_node_in_tnc()
3153 if (err == -ENOENT) in is_leaf_node_in_tnc()
3157 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3159 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3160 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3169 if (err == -ENOENT) in is_leaf_node_in_tnc()
3173 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3175 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3176 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3183 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3184 * @c: UBIFS file-system description object
3185 * @key: node key
3189 * @is_idx: non-zero if the node is an index node
3192 * negative error code in case of failure. For index nodes, @key has to be the
3193 * key of the first child. An index node is considered to be in the TNC only if
3196 int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_tnc_has_node() argument
3201 mutex_lock(&c->tnc_mutex); in ubifs_tnc_has_node()
3203 err = is_idx_node_in_tnc(c, key, level, lnum, offs); in ubifs_tnc_has_node()
3215 err = is_leaf_node_in_tnc(c, key, lnum, offs); in ubifs_tnc_has_node()
3218 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_has_node()
3223 * ubifs_dirty_idx_node - dirty an index node.
3224 * @c: UBIFS file-system description object
3225 * @key: index node key
3231 * collected. The @key argument has to be the key of the first child. This
3233 * for a main-area node. Returns %0 on success and a negative error code on
3236 int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_dirty_idx_node() argument
3242 mutex_lock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3243 znode = lookup_znode(c, key, level, lnum, offs); in ubifs_dirty_idx_node()
3257 mutex_unlock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3262 * dbg_check_inode_size - check if inode size is correct.
3263 * @c: UBIFS file-system description object
3268 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3269 * if it has a data page beyond @size, and other negative error code in case of
3276 union ubifs_key from_key, to_key, *key; in dbg_check_inode_size() local
3280 if (!S_ISREG(inode->i_mode)) in dbg_check_inode_size()
3285 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; in dbg_check_inode_size()
3286 data_key_init(c, &from_key, inode->i_ino, block); in dbg_check_inode_size()
3287 highest_data_key(c, &to_key, inode->i_ino); in dbg_check_inode_size()
3289 mutex_lock(&c->tnc_mutex); in dbg_check_inode_size()
3295 key = &from_key; in dbg_check_inode_size()
3300 if (err == -ENOENT) { in dbg_check_inode_size()
3308 key = &znode->zbranch[n].key; in dbg_check_inode_size()
3309 if (!key_in_range(c, key, &from_key, &to_key)) in dbg_check_inode_size()
3313 block = key_block(c, key); in dbg_check_inode_size()
3315 (unsigned long)inode->i_ino, size, in dbg_check_inode_size()
3317 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()
3320 return -EINVAL; in dbg_check_inode_size()
3323 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()