Lines Matching +full:- +full:c
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
14 * the UBIFS B-tree.
49 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
51 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
55 * insert_old_idx - record an index node obsoleted since the last commit start.
56 * @c: UBIFS file-system description object
68 * That means that the garbage collection and the in-the-gaps method of
73 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
74 * That is what this function does. The RB-tree is ordered by LEB number and
77 static int insert_old_idx(struct ubifs_info *c, int lnum, int offs) in insert_old_idx() argument
84 return -ENOMEM; in insert_old_idx()
85 old_idx->lnum = lnum; in insert_old_idx()
86 old_idx->offs = offs; in insert_old_idx()
88 p = &c->old_idx.rb_node; in insert_old_idx()
92 if (lnum < o->lnum) in insert_old_idx()
93 p = &(*p)->rb_left; in insert_old_idx()
94 else if (lnum > o->lnum) in insert_old_idx()
95 p = &(*p)->rb_right; in insert_old_idx()
96 else if (offs < o->offs) in insert_old_idx()
97 p = &(*p)->rb_left; in insert_old_idx()
98 else if (offs > o->offs) in insert_old_idx()
99 p = &(*p)->rb_right; in insert_old_idx()
101 ubifs_err(c, "old idx added twice!"); in insert_old_idx()
106 rb_link_node(&old_idx->rb, parent, p); in insert_old_idx()
107 rb_insert_color(&old_idx->rb, &c->old_idx); in insert_old_idx()
112 * insert_old_idx_znode - record a znode obsoleted since last commit start.
113 * @c: UBIFS file-system description object
118 int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode) in insert_old_idx_znode() argument
120 if (znode->parent) { in insert_old_idx_znode()
123 zbr = &znode->parent->zbranch[znode->iip]; in insert_old_idx_znode()
124 if (zbr->len) in insert_old_idx_znode()
125 return insert_old_idx(c, zbr->lnum, zbr->offs); in insert_old_idx_znode()
127 if (c->zroot.len) in insert_old_idx_znode()
128 return insert_old_idx(c, c->zroot.lnum, in insert_old_idx_znode()
129 c->zroot.offs); in insert_old_idx_znode()
134 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
135 * @c: UBIFS file-system description object
140 static int ins_clr_old_idx_znode(struct ubifs_info *c, in ins_clr_old_idx_znode() argument
145 if (znode->parent) { in ins_clr_old_idx_znode()
148 zbr = &znode->parent->zbranch[znode->iip]; in ins_clr_old_idx_znode()
149 if (zbr->len) { in ins_clr_old_idx_znode()
150 err = insert_old_idx(c, zbr->lnum, zbr->offs); in ins_clr_old_idx_znode()
153 zbr->lnum = 0; in ins_clr_old_idx_znode()
154 zbr->offs = 0; in ins_clr_old_idx_znode()
155 zbr->len = 0; in ins_clr_old_idx_znode()
158 if (c->zroot.len) { in ins_clr_old_idx_znode()
159 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs); in ins_clr_old_idx_znode()
162 c->zroot.lnum = 0; in ins_clr_old_idx_znode()
163 c->zroot.offs = 0; in ins_clr_old_idx_znode()
164 c->zroot.len = 0; in ins_clr_old_idx_znode()
170 * destroy_old_idx - destroy the old_idx RB-tree.
171 * @c: UBIFS file-system description object
173 * During start commit, the old_idx RB-tree is used to avoid overwriting index
176 * new index is successfully written. The old-idx RB-tree is used for the
177 * in-the-gaps method of writing index nodes and is destroyed every commit.
179 void destroy_old_idx(struct ubifs_info *c) in destroy_old_idx() argument
183 rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb) in destroy_old_idx()
186 c->old_idx = RB_ROOT; in destroy_old_idx()
190 * copy_znode - copy a dirty znode.
191 * @c: UBIFS file-system description object
196 static struct ubifs_znode *copy_znode(struct ubifs_info *c, in copy_znode() argument
201 zn = kmalloc(c->max_znode_sz, GFP_NOFS); in copy_znode()
203 return ERR_PTR(-ENOMEM); in copy_znode()
205 memcpy(zn, znode, c->max_znode_sz); 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()
211 __set_bit(OBSOLETE_ZNODE, &znode->flags); in copy_znode()
213 if (znode->level != 0) { in copy_znode()
215 const int n = zn->child_cnt; in copy_znode()
219 struct ubifs_zbranch *zbr = &zn->zbranch[i]; in copy_znode()
221 if (zbr->znode) in copy_znode()
222 zbr->znode->parent = zn; in copy_znode()
226 atomic_long_inc(&c->dirty_zn_cnt); in copy_znode()
231 * add_idx_dirt - add dirt due to a dirty znode.
232 * @c: UBIFS file-system description object
238 static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt) in add_idx_dirt() argument
240 c->calc_idx_sz -= ALIGN(dirt, 8); in add_idx_dirt()
241 return ubifs_add_dirt(c, lnum, dirt); in add_idx_dirt()
245 * dirty_cow_znode - ensure a znode is not being committed.
246 * @c: UBIFS file-system description object
251 static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c, in dirty_cow_znode() argument
254 struct ubifs_znode *znode = zbr->znode; in dirty_cow_znode()
260 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) { in dirty_cow_znode()
261 atomic_long_inc(&c->dirty_zn_cnt); in dirty_cow_znode()
262 atomic_long_dec(&c->clean_zn_cnt); in dirty_cow_znode()
264 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
271 zn = copy_znode(c, znode); in dirty_cow_znode()
275 if (zbr->len) { in dirty_cow_znode()
276 err = insert_old_idx(c, zbr->lnum, zbr->offs); in dirty_cow_znode()
279 err = add_idx_dirt(c, zbr->lnum, zbr->len); in dirty_cow_znode()
283 zbr->znode = zn; in dirty_cow_znode()
284 zbr->lnum = 0; in dirty_cow_znode()
285 zbr->offs = 0; in dirty_cow_znode()
286 zbr->len = 0; in dirty_cow_znode()
294 * lnc_add - add a leaf node to the leaf node cache.
295 * @c: UBIFS file-system description object
299 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
300 * purpose of the leaf node cache is to save re-reading the same leaf node over
310 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
313 static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr, in lnc_add() argument
320 ubifs_assert(!zbr->leaf); in lnc_add()
321 ubifs_assert(zbr->len != 0); in lnc_add()
322 ubifs_assert(is_hash_key(c, &zbr->key)); in lnc_add()
324 err = ubifs_validate_entry(c, dent); in lnc_add()
327 ubifs_dump_node(c, dent); in lnc_add()
331 lnc_node = kmemdup(node, zbr->len, GFP_NOFS); in lnc_add()
336 zbr->leaf = lnc_node; in lnc_add()
341 * lnc_add_directly - add a leaf node to the leaf-node-cache.
342 * @c: UBIFS file-system description object
349 static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr, in lnc_add_directly() argument
354 ubifs_assert(!zbr->leaf); in lnc_add_directly()
355 ubifs_assert(zbr->len != 0); in lnc_add_directly()
357 err = ubifs_validate_entry(c, node); in lnc_add_directly()
360 ubifs_dump_node(c, node); in lnc_add_directly()
364 zbr->leaf = node; in lnc_add_directly()
369 * lnc_free - remove a leaf node from the leaf node cache.
375 if (!zbr->leaf) in lnc_free()
377 kfree(zbr->leaf); in lnc_free()
378 zbr->leaf = NULL; in lnc_free()
382 * tnc_read_node_nm - read a "hashed" leaf node.
383 * @c: UBIFS file-system description object
392 static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr, in tnc_read_node_nm() argument
397 ubifs_assert(is_hash_key(c, &zbr->key)); in tnc_read_node_nm()
399 if (zbr->leaf) { in tnc_read_node_nm()
401 ubifs_assert(zbr->len != 0); in tnc_read_node_nm()
402 memcpy(node, zbr->leaf, zbr->len); in tnc_read_node_nm()
406 if (c->replaying) { in tnc_read_node_nm()
407 err = fallible_read_node(c, &zbr->key, zbr, node); in tnc_read_node_nm()
409 * When the node was not found, return -ENOENT, 0 otherwise. in tnc_read_node_nm()
410 * Negative return codes stay as-is. in tnc_read_node_nm()
413 err = -ENOENT; in tnc_read_node_nm()
417 err = ubifs_tnc_read_node(c, zbr, node); in tnc_read_node_nm()
423 err = lnc_add(c, zbr, node); in tnc_read_node_nm()
428 * try_read_node - read a node if it is a node.
429 * @c: UBIFS file-system description object
443 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
445 * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
446 * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
447 * because during mounting or re-mounting from R/O mode to R/W mode we may read
451 static int try_read_node(const struct ubifs_info *c, void *buf, int type, in try_read_node() argument
460 err = ubifs_leb_read(c, lnum, buf, offs, len, 1); in try_read_node()
462 ubifs_err(c, "cannot read node type %d from LEB %d:%d, error %d", in try_read_node()
467 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) in try_read_node()
470 if (ch->node_type != type) in try_read_node()
473 node_len = le32_to_cpu(ch->len); in try_read_node()
477 if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting && in try_read_node()
478 !c->remounting_rw) in try_read_node()
481 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in try_read_node()
482 node_crc = le32_to_cpu(ch->crc); in try_read_node()
490 * fallible_read_node - try to read a leaf node.
491 * @c: UBIFS file-system description object
499 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key, in fallible_read_node() argument
504 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs); in fallible_read_node()
506 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum, in fallible_read_node()
507 zbr->offs); in fallible_read_node()
513 key_read(c, &dent->key, &node_key); in fallible_read_node()
514 if (keys_cmp(c, key, &node_key) != 0) in fallible_read_node()
517 if (ret == 0 && c->replaying) in fallible_read_node()
519 zbr->lnum, zbr->offs, zbr->len); in fallible_read_node()
524 * matches_name - determine if a direntry or xattr entry matches a given name.
525 * @c: UBIFS file-system description object
534 static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr, in matches_name() argument
541 if (!zbr->leaf) { in matches_name()
542 dent = kmalloc(zbr->len, GFP_NOFS); in matches_name()
544 return -ENOMEM; in matches_name()
546 err = ubifs_tnc_read_node(c, zbr, dent); in matches_name()
551 err = lnc_add_directly(c, zbr, dent); in matches_name()
555 dent = zbr->leaf; in matches_name()
557 nlen = le16_to_cpu(dent->nlen); in matches_name()
558 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len)); in matches_name()
560 if (nlen == nm->len) in matches_name()
562 else if (nlen < nm->len) in matches_name()
577 * get_znode - get a TNC znode that may not be loaded yet.
578 * @c: UBIFS file-system description object
584 static struct ubifs_znode *get_znode(struct ubifs_info *c, in get_znode() argument
589 zbr = &znode->zbranch[n]; in get_znode()
590 if (zbr->znode) in get_znode()
591 znode = zbr->znode; in get_znode()
593 znode = ubifs_load_znode(c, zbr, znode, n); in get_znode()
598 * tnc_next - find next TNC entry.
599 * @c: UBIFS file-system description object
603 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
606 static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n) in tnc_next() argument
612 if (nn < znode->child_cnt) { in tnc_next()
619 zp = znode->parent; in tnc_next()
621 return -ENOENT; in tnc_next()
622 nn = znode->iip + 1; in tnc_next()
624 if (nn < znode->child_cnt) { in tnc_next()
625 znode = get_znode(c, znode, nn); in tnc_next()
628 while (znode->level != 0) { in tnc_next()
629 znode = get_znode(c, znode, 0); in tnc_next()
643 * tnc_prev - find previous TNC entry.
644 * @c: UBIFS file-system description object
648 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
651 static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n) in tnc_prev() argument
657 *n = nn - 1; in tnc_prev()
663 zp = znode->parent; in tnc_prev()
665 return -ENOENT; in tnc_prev()
666 nn = znode->iip - 1; in tnc_prev()
669 znode = get_znode(c, znode, nn); in tnc_prev()
672 while (znode->level != 0) { in tnc_prev()
673 nn = znode->child_cnt - 1; in tnc_prev()
674 znode = get_znode(c, znode, nn); in tnc_prev()
678 nn = znode->child_cnt - 1; in tnc_prev()
688 * resolve_collision - resolve a collision.
689 * @c: UBIFS file-system description object
700 * This means that @n may be set to %-1 if the leftmost key in @zn is the
703 static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key, in resolve_collision() argument
709 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
718 err = tnc_prev(c, zn, n); in resolve_collision()
719 if (err == -ENOENT) { in resolve_collision()
721 *n = -1; in resolve_collision()
726 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in resolve_collision()
735 * ---------------------- in resolve_collision()
737 * ----------------------- in resolve_collision()
740 * ------------ ------------ in resolve_collision()
742 * ------------ ------------ in resolve_collision()
756 if (*n == (*zn)->child_cnt - 1) { in resolve_collision()
757 err = tnc_next(c, zn, n); in resolve_collision()
761 if (err == -ENOENT) in resolve_collision()
762 err = -EINVAL; in resolve_collision()
766 *n = -1; in resolve_collision()
770 err = matches_name(c, &(*zn)->zbranch[*n], nm); in resolve_collision()
785 err = tnc_next(c, &znode, &nn); in resolve_collision()
786 if (err == -ENOENT) in resolve_collision()
790 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision()
792 err = matches_name(c, &znode->zbranch[nn], nm); in resolve_collision()
807 * fallible_matches_name - determine if a dent matches a given name.
808 * @c: UBIFS file-system description object
821 static int fallible_matches_name(struct ubifs_info *c, in fallible_matches_name() argument
829 if (!zbr->leaf) { in fallible_matches_name()
830 dent = kmalloc(zbr->len, GFP_NOFS); in fallible_matches_name()
832 return -ENOMEM; in fallible_matches_name()
834 err = fallible_read_node(c, &zbr->key, zbr, dent); in fallible_matches_name()
844 err = lnc_add_directly(c, zbr, dent); in fallible_matches_name()
848 dent = zbr->leaf; in fallible_matches_name()
850 nlen = le16_to_cpu(dent->nlen); in fallible_matches_name()
851 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len)); in fallible_matches_name()
853 if (nlen == nm->len) in fallible_matches_name()
855 else if (nlen < nm->len) in fallible_matches_name()
870 * fallible_resolve_collision - resolve a collision even if nodes are missing.
871 * @c: UBIFS file-system description object
881 * Garbage-collected and the commit was not done. A branch that refers to a node
891 static int fallible_resolve_collision(struct ubifs_info *c, in fallible_resolve_collision() argument
899 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
910 * branch - to the left or to the right. Well, let's try left. in fallible_resolve_collision()
919 err = tnc_prev(c, zn, n); in fallible_resolve_collision()
920 if (err == -ENOENT) { in fallible_resolve_collision()
922 *n = -1; in fallible_resolve_collision()
927 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) { in fallible_resolve_collision()
929 if (*n == (*zn)->child_cnt - 1) { in fallible_resolve_collision()
930 err = tnc_next(c, zn, n); in fallible_resolve_collision()
934 if (err == -ENOENT) in fallible_resolve_collision()
935 err = -EINVAL; in fallible_resolve_collision()
939 *n = -1; in fallible_resolve_collision()
943 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm); in fallible_resolve_collision()
967 err = tnc_next(c, &znode, &nn); in fallible_resolve_collision()
968 if (err == -ENOENT) in fallible_resolve_collision()
972 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in fallible_resolve_collision()
974 err = fallible_matches_name(c, &znode->zbranch[nn], nm); in fallible_resolve_collision()
995 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs, in fallible_resolve_collision()
996 o_znode->zbranch[o_n].len); in fallible_resolve_collision()
1003 * matches_position - determine if a zbranch matches a given position.
1012 if (zbr->lnum == lnum && zbr->offs == offs) in matches_position()
1019 * resolve_collision_directly - resolve a collision directly.
1020 * @c: UBIFS file-system description object
1035 static int resolve_collision_directly(struct ubifs_info *c, in resolve_collision_directly() argument
1045 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1050 err = tnc_prev(c, &znode, &nn); 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()
1057 if (matches_position(&znode->zbranch[nn], lnum, offs)) { in resolve_collision_directly()
1068 err = tnc_next(c, &znode, &nn); in resolve_collision_directly()
1069 if (err == -ENOENT) in resolve_collision_directly()
1073 if (keys_cmp(c, &znode->zbranch[nn].key, key)) in resolve_collision_directly()
1077 if (matches_position(&znode->zbranch[nn], lnum, offs)) in resolve_collision_directly()
1083 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1084 * @c: UBIFS file-system description object
1092 static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c, in dirty_cow_bottom_up() argument
1096 int *path = c->bottom_up_buf, p = 0; in dirty_cow_bottom_up()
1098 ubifs_assert(c->zroot.znode); in dirty_cow_bottom_up()
1100 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) { in dirty_cow_bottom_up()
1101 kfree(c->bottom_up_buf); in dirty_cow_bottom_up()
1102 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int), in dirty_cow_bottom_up()
1104 if (!c->bottom_up_buf) in dirty_cow_bottom_up()
1105 return ERR_PTR(-ENOMEM); in dirty_cow_bottom_up()
1106 path = c->bottom_up_buf; in dirty_cow_bottom_up()
1108 if (c->zroot.znode->level) { in dirty_cow_bottom_up()
1113 zp = znode->parent; in dirty_cow_bottom_up()
1116 n = znode->iip; in dirty_cow_bottom_up()
1117 ubifs_assert(p < c->zroot.znode->level); in dirty_cow_bottom_up()
1119 if (!zp->cnext && ubifs_zn_dirty(znode)) in dirty_cow_bottom_up()
1129 zp = znode->parent; in dirty_cow_bottom_up()
1131 ubifs_assert(path[p - 1] >= 0); in dirty_cow_bottom_up()
1132 ubifs_assert(path[p - 1] < zp->child_cnt); in dirty_cow_bottom_up()
1133 zbr = &zp->zbranch[path[--p]]; in dirty_cow_bottom_up()
1134 znode = dirty_cow_znode(c, zbr); in dirty_cow_bottom_up()
1136 ubifs_assert(znode == c->zroot.znode); in dirty_cow_bottom_up()
1137 znode = dirty_cow_znode(c, &c->zroot); in dirty_cow_bottom_up()
1141 ubifs_assert(path[p - 1] >= 0); in dirty_cow_bottom_up()
1142 ubifs_assert(path[p - 1] < znode->child_cnt); in dirty_cow_bottom_up()
1143 znode = znode->zbranch[path[p - 1]].znode; in dirty_cow_bottom_up()
1150 * ubifs_lookup_level0 - search for zero-level znode.
1151 * @c: UBIFS file-system description object
1156 * This function looks up the TNC tree and search for zero-level znode which
1157 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1159 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1161 * o not exact match, which means that zero-level znode does not contain
1165 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1171 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key, in ubifs_lookup_level0() argument
1179 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY); in ubifs_lookup_level0()
1181 znode = c->zroot.znode; in ubifs_lookup_level0()
1183 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in ubifs_lookup_level0()
1188 znode->time = time; in ubifs_lookup_level0()
1193 exact = ubifs_search_zbranch(c, znode, key, n); in ubifs_lookup_level0()
1195 if (znode->level == 0) in ubifs_lookup_level0()
1200 zbr = &znode->zbranch[*n]; in ubifs_lookup_level0()
1202 if (zbr->znode) { in ubifs_lookup_level0()
1203 znode->time = time; in ubifs_lookup_level0()
1204 znode = zbr->znode; in ubifs_lookup_level0()
1209 znode = ubifs_load_znode(c, zbr, znode, *n); in ubifs_lookup_level0()
1215 if (exact || !is_hash_key(c, key) || *n != -1) { in ubifs_lookup_level0()
1216 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in ubifs_lookup_level0()
1258 * indexing LEBs). Although we already have an additional RB-tree where in ubifs_lookup_level0()
1263 err = tnc_prev(c, &znode, n); in ubifs_lookup_level0()
1264 if (err == -ENOENT) { in ubifs_lookup_level0()
1265 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1266 *n = -1; in ubifs_lookup_level0()
1271 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in ubifs_lookup_level0()
1272 dbg_tnc("found 0, lvl %d, n -1", znode->level); in ubifs_lookup_level0()
1273 *n = -1; in ubifs_lookup_level0()
1277 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in ubifs_lookup_level0()
1283 * lookup_level0_dirty - search for zero-level znode dirtying.
1284 * @c: UBIFS file-system description object
1289 * This function looks up the TNC tree and search for zero-level znode which
1290 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1292 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1294 * o not exact match, which means that zero-level znode does not contain @key
1298 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1300 * Additionally all znodes in the path from the root to the located zero-level
1307 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key, in lookup_level0_dirty() argument
1316 znode = c->zroot.znode; in lookup_level0_dirty()
1318 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_level0_dirty()
1323 znode = dirty_cow_znode(c, &c->zroot); in lookup_level0_dirty()
1327 znode->time = time; in lookup_level0_dirty()
1332 exact = ubifs_search_zbranch(c, znode, key, n); in lookup_level0_dirty()
1334 if (znode->level == 0) in lookup_level0_dirty()
1339 zbr = &znode->zbranch[*n]; in lookup_level0_dirty()
1341 if (zbr->znode) { in lookup_level0_dirty()
1342 znode->time = time; in lookup_level0_dirty()
1343 znode = dirty_cow_znode(c, zbr); in lookup_level0_dirty()
1350 znode = ubifs_load_znode(c, zbr, znode, *n); in lookup_level0_dirty()
1353 znode = dirty_cow_znode(c, zbr); in lookup_level0_dirty()
1359 if (exact || !is_hash_key(c, key) || *n != -1) { in lookup_level0_dirty()
1360 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n); in lookup_level0_dirty()
1368 err = tnc_prev(c, &znode, n); in lookup_level0_dirty()
1369 if (err == -ENOENT) { in lookup_level0_dirty()
1370 *n = -1; in lookup_level0_dirty()
1371 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1376 if (keys_cmp(c, key, &znode->zbranch[*n].key)) { in lookup_level0_dirty()
1377 *n = -1; in lookup_level0_dirty()
1378 dbg_tnc("found 0, lvl %d, n -1", znode->level); in lookup_level0_dirty()
1382 if (znode->cnext || !ubifs_zn_dirty(znode)) { in lookup_level0_dirty()
1383 znode = dirty_cow_bottom_up(c, znode); in lookup_level0_dirty()
1388 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n); in lookup_level0_dirty()
1394 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1395 * @c: UBIFS file-system description object
1403 static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1) in maybe_leb_gced() argument
1408 gced_lnum = c->gced_lnum; in maybe_leb_gced()
1410 gc_seq2 = c->gc_seq; in maybe_leb_gced()
1422 if (gced_lnum != c->gced_lnum) in maybe_leb_gced()
1428 /* No garbage collection in the read-only U-Boot implementation */ in maybe_leb_gced()
1434 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1435 * @c: UBIFS file-system description object
1443 * of success, %-ENOENT if the node was not found, and a negative error code in
1446 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_locate() argument
1454 mutex_lock(&c->tnc_mutex); in ubifs_tnc_locate()
1455 found = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_locate()
1457 err = -ENOENT; in ubifs_tnc_locate()
1463 zt = &znode->zbranch[n]; in ubifs_tnc_locate()
1465 *lnum = zt->lnum; in ubifs_tnc_locate()
1466 *offs = zt->offs; in ubifs_tnc_locate()
1468 if (is_hash_key(c, key)) { in ubifs_tnc_locate()
1473 err = tnc_read_node_nm(c, zt, node); in ubifs_tnc_locate()
1477 err = ubifs_tnc_read_node(c, zt, node); in ubifs_tnc_locate()
1481 zbr = znode->zbranch[n]; in ubifs_tnc_locate()
1482 gc_seq1 = c->gc_seq; in ubifs_tnc_locate()
1483 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1485 if (ubifs_get_wbuf(c, zbr.lnum)) { in ubifs_tnc_locate()
1487 err = ubifs_tnc_read_node(c, &zbr, node); in ubifs_tnc_locate()
1491 err = fallible_read_node(c, key, &zbr, node); in ubifs_tnc_locate()
1492 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) { in ubifs_tnc_locate()
1503 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_locate()
1508 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1509 * @c: UBIFS file-system description object
1510 * @bu: bulk-read parameters and results
1516 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1517 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1518 * maximum possible amount of nodes for bulk-read.
1520 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu) in ubifs_tnc_get_bu_keys() argument
1522 int n, err = 0, lnum = -1, uninitialized_var(offs); in ubifs_tnc_get_bu_keys()
1524 unsigned int block = key_block(c, &bu->key); in ubifs_tnc_get_bu_keys()
1527 bu->cnt = 0; in ubifs_tnc_get_bu_keys()
1528 bu->blk_cnt = 0; in ubifs_tnc_get_bu_keys()
1529 bu->eof = 0; in ubifs_tnc_get_bu_keys()
1531 mutex_lock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1533 err = ubifs_lookup_level0(c, &bu->key, &znode, &n); in ubifs_tnc_get_bu_keys()
1538 len = znode->zbranch[n].len; in ubifs_tnc_get_bu_keys()
1540 if (len > bu->buf_len) { in ubifs_tnc_get_bu_keys()
1541 err = -EINVAL; in ubifs_tnc_get_bu_keys()
1545 bu->zbranch[bu->cnt++] = znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1546 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1547 lnum = znode->zbranch[n].lnum; in ubifs_tnc_get_bu_keys()
1548 offs = ALIGN(znode->zbranch[n].offs + len, 8); in ubifs_tnc_get_bu_keys()
1556 err = tnc_next(c, &znode, &n); in ubifs_tnc_get_bu_keys()
1559 zbr = &znode->zbranch[n]; in ubifs_tnc_get_bu_keys()
1560 key = &zbr->key; in ubifs_tnc_get_bu_keys()
1562 if (key_inum(c, key) != key_inum(c, &bu->key) || in ubifs_tnc_get_bu_keys()
1563 key_type(c, key) != UBIFS_DATA_KEY) { in ubifs_tnc_get_bu_keys()
1564 err = -ENOENT; in ubifs_tnc_get_bu_keys()
1569 lnum = zbr->lnum; in ubifs_tnc_get_bu_keys()
1570 offs = ALIGN(zbr->offs + zbr->len, 8); in ubifs_tnc_get_bu_keys()
1571 len = zbr->len; 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()
1581 if (zbr->lnum != lnum || zbr->offs != offs) in ubifs_tnc_get_bu_keys()
1583 offs += ALIGN(zbr->len, 8); in ubifs_tnc_get_bu_keys()
1584 len = ALIGN(len, 8) + zbr->len; in ubifs_tnc_get_bu_keys()
1586 if (len > bu->buf_len) in ubifs_tnc_get_bu_keys()
1590 next_block = key_block(c, key); in ubifs_tnc_get_bu_keys()
1591 bu->blk_cnt += (next_block - block - 1); in ubifs_tnc_get_bu_keys()
1592 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1596 bu->zbranch[bu->cnt++] = *zbr; in ubifs_tnc_get_bu_keys()
1597 bu->blk_cnt += 1; in ubifs_tnc_get_bu_keys()
1599 if (bu->cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1601 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1605 if (err == -ENOENT) { in ubifs_tnc_get_bu_keys()
1606 bu->eof = 1; in ubifs_tnc_get_bu_keys()
1609 bu->gc_seq = c->gc_seq; in ubifs_tnc_get_bu_keys()
1610 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_get_bu_keys()
1614 * An enormous hole could cause bulk-read to encompass too many in ubifs_tnc_get_bu_keys()
1617 if (bu->blk_cnt > UBIFS_MAX_BULK_READ) in ubifs_tnc_get_bu_keys()
1618 bu->blk_cnt = UBIFS_MAX_BULK_READ; in ubifs_tnc_get_bu_keys()
1620 * Ensure that bulk-read covers a whole number of page cache in ubifs_tnc_get_bu_keys()
1624 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1))) in ubifs_tnc_get_bu_keys()
1626 if (bu->eof) { in ubifs_tnc_get_bu_keys()
1628 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1; in ubifs_tnc_get_bu_keys()
1632 block = key_block(c, &bu->key) + bu->blk_cnt; in ubifs_tnc_get_bu_keys()
1633 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1); in ubifs_tnc_get_bu_keys()
1634 while (bu->cnt) { in ubifs_tnc_get_bu_keys()
1635 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block) in ubifs_tnc_get_bu_keys()
1637 bu->cnt -= 1; in ubifs_tnc_get_bu_keys()
1643 * read_wbuf - bulk-read from a LEB with a wbuf.
1655 const struct ubifs_info *c = wbuf->c; in read_wbuf() local
1659 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in read_wbuf()
1660 ubifs_assert(!(offs & 7) && offs < c->leb_size); in read_wbuf()
1661 ubifs_assert(offs + len <= c->leb_size); in read_wbuf()
1663 spin_lock(&wbuf->lock); in read_wbuf()
1664 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in read_wbuf()
1666 /* We may safely unlock the write-buffer and read the data */ in read_wbuf()
1667 spin_unlock(&wbuf->lock); in read_wbuf()
1668 return ubifs_leb_read(c, lnum, buf, offs, len, 0); in read_wbuf()
1672 rlen = wbuf->offs - offs; in read_wbuf()
1676 /* Copy the rest from the write-buffer */ in read_wbuf()
1677 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in read_wbuf()
1678 spin_unlock(&wbuf->lock); in read_wbuf()
1681 /* Read everything that goes before write-buffer */ in read_wbuf()
1682 return ubifs_leb_read(c, lnum, buf, offs, rlen, 0); in read_wbuf()
1688 * validate_data_node - validate data nodes for bulk-read.
1689 * @c: UBIFS file-system description object
1695 static int validate_data_node(struct ubifs_info *c, void *buf, in validate_data_node() argument
1702 if (ch->node_type != UBIFS_DATA_NODE) { in validate_data_node()
1703 ubifs_err(c, "bad node type (%d but expected %d)", in validate_data_node()
1704 ch->node_type, UBIFS_DATA_NODE); in validate_data_node()
1708 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0); in validate_data_node()
1710 ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE); in validate_data_node()
1714 len = le32_to_cpu(ch->len); in validate_data_node()
1715 if (len != zbr->len) { in validate_data_node()
1716 ubifs_err(c, "bad node length %d, expected %d", len, zbr->len); in validate_data_node()
1721 key_read(c, buf + UBIFS_KEY_OFFSET, &key1); in validate_data_node()
1722 if (!keys_eq(c, &zbr->key, &key1)) { in validate_data_node()
1723 ubifs_err(c, "bad key in node at LEB %d:%d", in validate_data_node()
1724 zbr->lnum, zbr->offs); in validate_data_node()
1725 dbg_tnck(&zbr->key, "looked for key "); in validate_data_node()
1733 err = -EINVAL; in validate_data_node()
1735 ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs); in validate_data_node()
1736 ubifs_dump_node(c, buf); in validate_data_node()
1742 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1743 * @c: UBIFS file-system description object
1744 * @bu: bulk-read parameters and results
1748 * -EAGAIN to indicate a race with GC, or another negative error code on
1751 int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu) in ubifs_tnc_bulk_read() argument
1753 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i; in ubifs_tnc_bulk_read()
1757 len = bu->zbranch[bu->cnt - 1].offs; in ubifs_tnc_bulk_read()
1758 len += bu->zbranch[bu->cnt - 1].len - offs; in ubifs_tnc_bulk_read()
1759 if (len > bu->buf_len) { in ubifs_tnc_bulk_read()
1760 ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len); in ubifs_tnc_bulk_read()
1761 return -EINVAL; in ubifs_tnc_bulk_read()
1765 wbuf = ubifs_get_wbuf(c, lnum); in ubifs_tnc_bulk_read()
1767 err = read_wbuf(wbuf, bu->buf, len, lnum, offs); in ubifs_tnc_bulk_read()
1769 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0); in ubifs_tnc_bulk_read()
1772 if (maybe_leb_gced(c, lnum, bu->gc_seq)) in ubifs_tnc_bulk_read()
1773 return -EAGAIN; in ubifs_tnc_bulk_read()
1775 if (err && err != -EBADMSG) { in ubifs_tnc_bulk_read()
1776 ubifs_err(c, "failed to read from LEB %d:%d, error %d", in ubifs_tnc_bulk_read()
1779 dbg_tnck(&bu->key, "key "); in ubifs_tnc_bulk_read()
1784 buf = bu->buf; in ubifs_tnc_bulk_read()
1785 for (i = 0; i < bu->cnt; i++) { in ubifs_tnc_bulk_read()
1786 err = validate_data_node(c, buf, &bu->zbranch[i]); in ubifs_tnc_bulk_read()
1789 buf = buf + ALIGN(bu->zbranch[i].len, 8); in ubifs_tnc_bulk_read()
1796 * do_lookup_nm- look up a "hashed" node.
1797 * @c: UBIFS file-system description object
1805 * found. This function returns zero in case of success, %-ENOENT if the node
1808 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in do_lookup_nm() argument
1814 dbg_tnck(key, "name '%.*s' key ", nm->len, nm->name); in do_lookup_nm()
1815 mutex_lock(&c->tnc_mutex); in do_lookup_nm()
1816 found = ubifs_lookup_level0(c, key, &znode, &n); in do_lookup_nm()
1818 err = -ENOENT; in do_lookup_nm()
1827 err = resolve_collision(c, key, &znode, &n, nm); in do_lookup_nm()
1832 err = -ENOENT; in do_lookup_nm()
1836 err = tnc_read_node_nm(c, &znode->zbranch[n], node); in do_lookup_nm()
1839 mutex_unlock(&c->tnc_mutex); in do_lookup_nm()
1844 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1845 * @c: UBIFS file-system description object
1853 * found. This function returns zero in case of success, %-ENOENT if the node
1856 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_lookup_nm() argument
1866 err = ubifs_tnc_lookup(c, key, node); in ubifs_tnc_lookup_nm()
1870 len = le16_to_cpu(dent->nlen); in ubifs_tnc_lookup_nm()
1871 if (nm->len == len && !memcmp(dent->name, nm->name, len)) in ubifs_tnc_lookup_nm()
1878 return do_lookup_nm(c, key, node, nm); in ubifs_tnc_lookup_nm()
1882 * correct_parent_keys - correct parent znodes' keys.
1883 * @c: UBIFS file-system description object
1890 static void correct_parent_keys(const struct ubifs_info *c, in correct_parent_keys() argument
1895 ubifs_assert(znode->parent); in correct_parent_keys()
1896 ubifs_assert(znode->iip == 0); in correct_parent_keys()
1898 key = &znode->zbranch[0].key; in correct_parent_keys()
1899 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
1901 while (keys_cmp(c, key, key1) < 0) { in correct_parent_keys()
1902 key_copy(c, key, key1); in correct_parent_keys()
1903 znode = znode->parent; in correct_parent_keys()
1904 znode->alt = 1; in correct_parent_keys()
1905 if (!znode->parent || znode->iip) in correct_parent_keys()
1907 key1 = &znode->parent->zbranch[0].key; in correct_parent_keys()
1912 * insert_zbranch - insert a zbranch into a znode.
1919 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1929 if (znode->level) { in insert_zbranch()
1930 for (i = znode->child_cnt; i > n; i--) { in insert_zbranch()
1931 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
1932 if (znode->zbranch[i].znode) in insert_zbranch()
1933 znode->zbranch[i].znode->iip = i; in insert_zbranch()
1935 if (zbr->znode) in insert_zbranch()
1936 zbr->znode->iip = n; in insert_zbranch()
1938 for (i = znode->child_cnt; i > n; i--) in insert_zbranch()
1939 znode->zbranch[i] = znode->zbranch[i - 1]; in insert_zbranch()
1941 znode->zbranch[n] = *zbr; in insert_zbranch()
1942 znode->child_cnt += 1; in insert_zbranch()
1959 znode->alt = 1; in insert_zbranch()
1963 * tnc_insert - insert a node into TNC.
1964 * @c: UBIFS file-system description object
1974 static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode, in tnc_insert() argument
1979 union ubifs_key *key = &zbr->key, *key1; in tnc_insert()
1981 ubifs_assert(n >= 0 && n <= c->fanout); in tnc_insert()
1985 zp = znode->parent; in tnc_insert()
1986 if (znode->child_cnt < c->fanout) { in tnc_insert()
1987 ubifs_assert(n != c->fanout); in tnc_insert()
1988 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level); in tnc_insert()
1993 if (n == 0 && zp && znode->iip == 0) in tnc_insert()
1994 correct_parent_keys(c, znode); in tnc_insert()
2003 dbg_tnck(key, "splitting level %d, key ", znode->level); in tnc_insert()
2005 if (znode->alt) in tnc_insert()
2010 ins_clr_old_idx_znode(c, znode); in tnc_insert()
2012 zn = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
2014 return -ENOMEM; in tnc_insert()
2015 zn->parent = zp; in tnc_insert()
2016 zn->level = znode->level; in tnc_insert()
2019 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) { in tnc_insert()
2021 if (n == c->fanout) { in tnc_insert()
2022 key1 = &znode->zbranch[n - 1].key; in tnc_insert()
2023 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2024 key_type(c, key1) == UBIFS_DATA_KEY) in tnc_insert()
2028 } else if (appending && n != c->fanout) { in tnc_insert()
2032 if (n >= (c->fanout + 1) / 2) { in tnc_insert()
2033 key1 = &znode->zbranch[0].key; in tnc_insert()
2034 if (key_inum(c, key1) == key_inum(c, key) && in tnc_insert()
2035 key_type(c, key1) == UBIFS_DATA_KEY) { in tnc_insert()
2036 key1 = &znode->zbranch[n].key; in tnc_insert()
2037 if (key_inum(c, key1) != key_inum(c, key) || in tnc_insert()
2038 key_type(c, key1) != UBIFS_DATA_KEY) { in tnc_insert()
2040 move = c->fanout - keep; in tnc_insert()
2049 keep = c->fanout; in tnc_insert()
2052 keep = (c->fanout + 1) / 2; in tnc_insert()
2053 move = c->fanout - keep; in tnc_insert()
2065 keep -= 1; in tnc_insert()
2069 n -= keep; in tnc_insert()
2070 /* Re-parent */ in tnc_insert()
2071 if (zn->level != 0) in tnc_insert()
2072 zbr->znode->parent = zn; in tnc_insert()
2077 __set_bit(DIRTY_ZNODE, &zn->flags); in tnc_insert()
2078 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2080 zn->child_cnt = move; in tnc_insert()
2081 znode->child_cnt = keep; in tnc_insert()
2087 zn->zbranch[i] = znode->zbranch[keep + i]; in tnc_insert()
2088 /* Re-parent */ in tnc_insert()
2089 if (zn->level != 0) in tnc_insert()
2090 if (zn->zbranch[i].znode) { in tnc_insert()
2091 zn->zbranch[i].znode->parent = zn; in tnc_insert()
2092 zn->zbranch[i].znode->iip = i; in tnc_insert()
2097 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level); in tnc_insert()
2103 if (n == 0 && zi == znode && znode->iip == 0) in tnc_insert()
2104 correct_parent_keys(c, znode); in tnc_insert()
2107 n = znode->iip + 1; in tnc_insert()
2110 zbr->key = zn->zbranch[0].key; in tnc_insert()
2111 zbr->znode = zn; in tnc_insert()
2112 zbr->lnum = 0; in tnc_insert()
2113 zbr->offs = 0; in tnc_insert()
2114 zbr->len = 0; in tnc_insert()
2121 dbg_tnc("creating new zroot at level %d", znode->level + 1); in tnc_insert()
2123 zi = kzalloc(c->max_znode_sz, GFP_NOFS); in tnc_insert()
2125 return -ENOMEM; in tnc_insert()
2127 zi->child_cnt = 2; in tnc_insert()
2128 zi->level = znode->level + 1; in tnc_insert()
2130 __set_bit(DIRTY_ZNODE, &zi->flags); in tnc_insert()
2131 atomic_long_inc(&c->dirty_zn_cnt); in tnc_insert()
2133 zi->zbranch[0].key = znode->zbranch[0].key; in tnc_insert()
2134 zi->zbranch[0].znode = znode; in tnc_insert()
2135 zi->zbranch[0].lnum = c->zroot.lnum; in tnc_insert()
2136 zi->zbranch[0].offs = c->zroot.offs; in tnc_insert()
2137 zi->zbranch[0].len = c->zroot.len; in tnc_insert()
2138 zi->zbranch[1].key = zn->zbranch[0].key; in tnc_insert()
2139 zi->zbranch[1].znode = zn; in tnc_insert()
2141 c->zroot.lnum = 0; in tnc_insert()
2142 c->zroot.offs = 0; in tnc_insert()
2143 c->zroot.len = 0; in tnc_insert()
2144 c->zroot.znode = zi; in tnc_insert()
2146 zn->parent = zi; in tnc_insert()
2147 zn->iip = 1; in tnc_insert()
2148 znode->parent = zi; in tnc_insert()
2149 znode->iip = 0; in tnc_insert()
2155 * ubifs_tnc_add - add a node to TNC.
2156 * @c: UBIFS file-system description object
2166 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum, in ubifs_tnc_add() argument
2172 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add()
2174 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add()
2182 key_copy(c, key, &zbr.key); in ubifs_tnc_add()
2183 err = tnc_insert(c, znode, &zbr, n + 1); in ubifs_tnc_add()
2185 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add()
2188 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add()
2189 zbr->lnum = lnum; in ubifs_tnc_add()
2190 zbr->offs = offs; in ubifs_tnc_add()
2191 zbr->len = len; in ubifs_tnc_add()
2195 err = dbg_check_tnc(c, 0); in ubifs_tnc_add()
2196 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add()
2202 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2203 * @c: UBIFS file-system description object
2215 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_replace() argument
2221 mutex_lock(&c->tnc_mutex); in ubifs_tnc_replace()
2224 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_replace()
2231 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2234 if (zbr->lnum == old_lnum && zbr->offs == old_offs) { in ubifs_tnc_replace()
2236 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_replace()
2239 zbr->lnum = lnum; in ubifs_tnc_replace()
2240 zbr->offs = offs; in ubifs_tnc_replace()
2241 zbr->len = len; in ubifs_tnc_replace()
2243 } else if (is_hash_key(c, key)) { in ubifs_tnc_replace()
2244 found = resolve_collision_directly(c, key, &znode, &n, in ubifs_tnc_replace()
2255 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_replace()
2256 znode = dirty_cow_bottom_up(c, znode); in ubifs_tnc_replace()
2262 zbr = &znode->zbranch[n]; in ubifs_tnc_replace()
2264 err = ubifs_add_dirt(c, zbr->lnum, in ubifs_tnc_replace()
2265 zbr->len); in ubifs_tnc_replace()
2268 zbr->lnum = lnum; in ubifs_tnc_replace()
2269 zbr->offs = offs; in ubifs_tnc_replace()
2270 zbr->len = len; in ubifs_tnc_replace()
2276 err = ubifs_add_dirt(c, lnum, len); in ubifs_tnc_replace()
2279 err = dbg_check_tnc(c, 0); in ubifs_tnc_replace()
2282 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_replace()
2287 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2288 * @c: UBIFS file-system description object
2298 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_add_nm() argument
2304 mutex_lock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2306 lnum, offs, nm->len, nm->name); in ubifs_tnc_add_nm()
2307 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_add_nm()
2314 if (c->replaying) in ubifs_tnc_add_nm()
2315 found = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_add_nm()
2318 found = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_add_nm()
2326 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_add_nm()
2327 znode = dirty_cow_bottom_up(c, znode); in ubifs_tnc_add_nm()
2335 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in ubifs_tnc_add_nm()
2338 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in ubifs_tnc_add_nm()
2339 zbr->lnum = lnum; in ubifs_tnc_add_nm()
2340 zbr->offs = offs; in ubifs_tnc_add_nm()
2341 zbr->len = len; in ubifs_tnc_add_nm()
2353 key_copy(c, key, &zbr.key); in ubifs_tnc_add_nm()
2354 err = tnc_insert(c, znode, &zbr, n + 1); in ubifs_tnc_add_nm()
2357 if (c->replaying) { in ubifs_tnc_add_nm()
2366 err = dbg_check_tnc(c, 0); in ubifs_tnc_add_nm()
2367 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2370 return ubifs_tnc_remove_nm(c, key, &noname); in ubifs_tnc_add_nm()
2376 err = dbg_check_tnc(c, 0); in ubifs_tnc_add_nm()
2377 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_add_nm()
2382 * tnc_delete - delete a znode form TNC.
2383 * @c: UBIFS file-system description object
2387 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2390 static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n) in tnc_delete() argument
2397 ubifs_assert(znode->level == 0); in tnc_delete()
2398 ubifs_assert(n >= 0 && n < c->fanout); in tnc_delete()
2399 dbg_tnck(&znode->zbranch[n].key, "deleting key "); in tnc_delete()
2401 zbr = &znode->zbranch[n]; in tnc_delete()
2404 err = ubifs_add_dirt(c, zbr->lnum, zbr->len); in tnc_delete()
2406 ubifs_dump_znode(c, znode); in tnc_delete()
2411 for (i = n; i < znode->child_cnt - 1; i++) in tnc_delete()
2412 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2413 znode->child_cnt -= 1; in tnc_delete()
2415 if (znode->child_cnt > 0) in tnc_delete()
2427 zp = znode->parent; in tnc_delete()
2428 n = znode->iip; in tnc_delete()
2430 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2432 err = insert_old_idx_znode(c, znode); in tnc_delete()
2436 if (znode->cnext) { in tnc_delete()
2437 __set_bit(OBSOLETE_ZNODE, &znode->flags); in tnc_delete()
2438 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2443 } while (znode->child_cnt == 1); /* while removing last child */ in tnc_delete()
2445 /* Remove from znode, entry n - 1 */ in tnc_delete()
2446 znode->child_cnt -= 1; in tnc_delete()
2447 ubifs_assert(znode->level != 0); in tnc_delete()
2448 for (i = n; i < znode->child_cnt; i++) { in tnc_delete()
2449 znode->zbranch[i] = znode->zbranch[i + 1]; in tnc_delete()
2450 if (znode->zbranch[i].znode) in tnc_delete()
2451 znode->zbranch[i].znode->iip = i; in tnc_delete()
2458 if (!znode->parent) { in tnc_delete()
2459 while (znode->child_cnt == 1 && znode->level != 0) { in tnc_delete()
2461 zbr = &znode->zbranch[0]; in tnc_delete()
2462 znode = get_znode(c, znode, 0); in tnc_delete()
2465 znode = dirty_cow_znode(c, zbr); in tnc_delete()
2468 znode->parent = NULL; in tnc_delete()
2469 znode->iip = 0; in tnc_delete()
2470 if (c->zroot.len) { in tnc_delete()
2471 err = insert_old_idx(c, c->zroot.lnum, in tnc_delete()
2472 c->zroot.offs); in tnc_delete()
2476 c->zroot.lnum = zbr->lnum; in tnc_delete()
2477 c->zroot.offs = zbr->offs; in tnc_delete()
2478 c->zroot.len = zbr->len; in tnc_delete()
2479 c->zroot.znode = znode; in tnc_delete()
2482 atomic_long_dec(&c->dirty_zn_cnt); in tnc_delete()
2484 if (zp->cnext) { in tnc_delete()
2485 __set_bit(OBSOLETE_ZNODE, &zp->flags); in tnc_delete()
2486 atomic_long_inc(&c->clean_zn_cnt); in tnc_delete()
2497 * ubifs_tnc_remove - remove an index entry of a node.
2498 * @c: UBIFS file-system description object
2503 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key) in ubifs_tnc_remove() argument
2508 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove()
2510 found = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove()
2516 err = tnc_delete(c, znode, n); in ubifs_tnc_remove()
2518 err = dbg_check_tnc(c, 0); in ubifs_tnc_remove()
2521 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove()
2526 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2527 * @c: UBIFS file-system description object
2533 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key, in ubifs_tnc_remove_nm() argument
2539 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2540 dbg_tnck(key, "%.*s, key ", nm->len, nm->name); in ubifs_tnc_remove_nm()
2541 err = lookup_level0_dirty(c, key, &znode, &n); in ubifs_tnc_remove_nm()
2546 if (c->replaying) in ubifs_tnc_remove_nm()
2547 err = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_remove_nm()
2550 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_remove_nm()
2556 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_nm()
2557 znode = dirty_cow_bottom_up(c, znode); in ubifs_tnc_remove_nm()
2563 err = tnc_delete(c, znode, n); in ubifs_tnc_remove_nm()
2569 err = dbg_check_tnc(c, 0); in ubifs_tnc_remove_nm()
2570 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_nm()
2575 * key_in_range - determine if a key falls within a range of keys.
2576 * @c: UBIFS file-system description object
2583 static int key_in_range(struct ubifs_info *c, union ubifs_key *key, in key_in_range() argument
2586 if (keys_cmp(c, key, from_key) < 0) in key_in_range()
2588 if (keys_cmp(c, key, to_key) > 0) in key_in_range()
2594 * ubifs_tnc_remove_range - remove index entries in range.
2595 * @c: UBIFS file-system description object
2603 int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key, in ubifs_tnc_remove_range() argument
2610 mutex_lock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2613 err = ubifs_lookup_level0(c, from_key, &znode, &n); in ubifs_tnc_remove_range()
2620 err = tnc_next(c, &znode, &n); in ubifs_tnc_remove_range()
2621 if (err == -ENOENT) { in ubifs_tnc_remove_range()
2627 key = &znode->zbranch[n].key; in ubifs_tnc_remove_range()
2628 if (!key_in_range(c, key, from_key, to_key)) { in ubifs_tnc_remove_range()
2635 if (znode->cnext || !ubifs_zn_dirty(znode)) { in ubifs_tnc_remove_range()
2636 znode = dirty_cow_bottom_up(c, znode); in ubifs_tnc_remove_range()
2644 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) { in ubifs_tnc_remove_range()
2645 key = &znode->zbranch[i].key; in ubifs_tnc_remove_range()
2646 if (!key_in_range(c, key, from_key, to_key)) in ubifs_tnc_remove_range()
2648 lnc_free(&znode->zbranch[i]); in ubifs_tnc_remove_range()
2649 err = ubifs_add_dirt(c, znode->zbranch[i].lnum, in ubifs_tnc_remove_range()
2650 znode->zbranch[i].len); in ubifs_tnc_remove_range()
2652 ubifs_dump_znode(c, znode); in ubifs_tnc_remove_range()
2658 for (i = n + 1 + k; i < znode->child_cnt; i++) in ubifs_tnc_remove_range()
2659 znode->zbranch[i - k] = znode->zbranch[i]; in ubifs_tnc_remove_range()
2660 znode->child_cnt -= k; in ubifs_tnc_remove_range()
2664 err = tnc_delete(c, znode, n); in ubifs_tnc_remove_range()
2671 err = dbg_check_tnc(c, 0); in ubifs_tnc_remove_range()
2672 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_remove_range()
2677 * ubifs_tnc_remove_ino - remove an inode from TNC.
2678 * @c: UBIFS file-system description object
2685 int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum) in ubifs_tnc_remove_ino() argument
2697 lowest_xent_key(c, &key1, inum); in ubifs_tnc_remove_ino()
2702 xent = ubifs_tnc_next_ent(c, &key1, &nm); in ubifs_tnc_remove_ino()
2705 if (err == -ENOENT) in ubifs_tnc_remove_ino()
2710 xattr_inum = le64_to_cpu(xent->inum); in ubifs_tnc_remove_ino()
2711 dbg_tnc("xent '%s', ino %lu", xent->name, in ubifs_tnc_remove_ino()
2714 nm.name = xent->name; in ubifs_tnc_remove_ino()
2715 nm.len = le16_to_cpu(xent->nlen); in ubifs_tnc_remove_ino()
2716 err = ubifs_tnc_remove_nm(c, &key1, &nm); in ubifs_tnc_remove_ino()
2722 lowest_ino_key(c, &key1, xattr_inum); in ubifs_tnc_remove_ino()
2723 highest_ino_key(c, &key2, xattr_inum); in ubifs_tnc_remove_ino()
2724 err = ubifs_tnc_remove_range(c, &key1, &key2); in ubifs_tnc_remove_ino()
2732 key_read(c, &xent->key, &key1); in ubifs_tnc_remove_ino()
2736 lowest_ino_key(c, &key1, inum); in ubifs_tnc_remove_ino()
2737 highest_ino_key(c, &key2, inum); in ubifs_tnc_remove_ino()
2739 return ubifs_tnc_remove_range(c, &key1, &key2); in ubifs_tnc_remove_ino()
2743 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2744 * @c: UBIFS file-system description object
2753 * @nm->name has to be %NULL. In this case the semantics of this function is a
2762 * in case of success, %-ENOENT is returned if no entry was found, and a
2765 struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c, in ubifs_tnc_next_ent() argument
2769 int n, err, type = key_type(c, key); in ubifs_tnc_next_ent()
2775 dbg_tnck(key, "%s ", nm->name ? (char *)nm->name : "(lowest)"); in ubifs_tnc_next_ent()
2776 ubifs_assert(is_hash_key(c, key)); in ubifs_tnc_next_ent()
2778 mutex_lock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2779 err = ubifs_lookup_level0(c, key, &znode, &n); in ubifs_tnc_next_ent()
2783 if (nm->name) { in ubifs_tnc_next_ent()
2786 if (c->replaying) in ubifs_tnc_next_ent()
2787 err = fallible_resolve_collision(c, key, &znode, &n, in ubifs_tnc_next_ent()
2790 err = resolve_collision(c, key, &znode, &n, nm); in ubifs_tnc_next_ent()
2798 err = tnc_next(c, &znode, &n); in ubifs_tnc_next_ent()
2813 err = tnc_next(c, &znode, &n); in ubifs_tnc_next_ent()
2819 zbr = &znode->zbranch[n]; in ubifs_tnc_next_ent()
2820 dent = kmalloc(zbr->len, GFP_NOFS); in ubifs_tnc_next_ent()
2822 err = -ENOMEM; in ubifs_tnc_next_ent()
2830 dkey = &zbr->key; in ubifs_tnc_next_ent()
2831 if (key_inum(c, dkey) != key_inum(c, key) || in ubifs_tnc_next_ent()
2832 key_type(c, dkey) != type) { in ubifs_tnc_next_ent()
2833 err = -ENOENT; in ubifs_tnc_next_ent()
2837 err = tnc_read_node_nm(c, zbr, dent); in ubifs_tnc_next_ent()
2841 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2847 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_next_ent()
2852 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2853 * @c: UBIFS file-system description object
2855 * Destroy left-over obsolete znodes from a failed commit.
2857 static void tnc_destroy_cnext(struct ubifs_info *c) in tnc_destroy_cnext() argument
2861 if (!c->cnext) in tnc_destroy_cnext()
2863 ubifs_assert(c->cmt_state == COMMIT_BROKEN); in tnc_destroy_cnext()
2864 cnext = c->cnext; in tnc_destroy_cnext()
2868 cnext = cnext->cnext; in tnc_destroy_cnext()
2871 } while (cnext && cnext != c->cnext); in tnc_destroy_cnext()
2875 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2876 * @c: UBIFS file-system description object
2878 void ubifs_tnc_close(struct ubifs_info *c) in ubifs_tnc_close() argument
2880 tnc_destroy_cnext(c); in ubifs_tnc_close()
2881 if (c->zroot.znode) { in ubifs_tnc_close()
2884 n = atomic_long_read(&c->clean_zn_cnt); in ubifs_tnc_close()
2885 freed = ubifs_destroy_tnc_subtree(c->zroot.znode); in ubifs_tnc_close()
2889 kfree(c->gap_lebs); in ubifs_tnc_close()
2890 kfree(c->ilebs); in ubifs_tnc_close()
2891 destroy_old_idx(c); in ubifs_tnc_close()
2895 * left_znode - get the znode to the left.
2896 * @c: UBIFS file-system description object
2902 static struct ubifs_znode *left_znode(struct ubifs_info *c, in left_znode() argument
2905 int level = znode->level; in left_znode()
2908 int n = znode->iip - 1; in left_znode()
2911 znode = znode->parent; in left_znode()
2916 znode = get_znode(c, znode, n); in left_znode()
2919 while (znode->level != level) { in left_znode()
2920 n = znode->child_cnt - 1; in left_znode()
2921 znode = get_znode(c, znode, n); in left_znode()
2932 * right_znode - get the znode to the right.
2933 * @c: UBIFS file-system description object
2939 static struct ubifs_znode *right_znode(struct ubifs_info *c, in right_znode() argument
2942 int level = znode->level; in right_znode()
2945 int n = znode->iip + 1; in right_znode()
2948 znode = znode->parent; in right_znode()
2951 if (n < znode->child_cnt) { in right_znode()
2953 znode = get_znode(c, znode, n); in right_znode()
2956 while (znode->level != level) { in right_znode()
2957 znode = get_znode(c, znode, 0); in right_znode()
2968 * lookup_znode - find a particular indexing node from TNC.
2969 * @c: UBIFS file-system description object
2978 * were found on the media by scanning, for example when garbage-collecting or
2979 * when doing in-the-gaps commit. This means that the indexing node which is
2986 * not find it. For situations like this UBIFS has the old index RB-tree
2992 static struct ubifs_znode *lookup_znode(struct ubifs_info *c, in lookup_znode() argument
2999 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY); in lookup_znode()
3006 return ERR_PTR(-EINVAL); in lookup_znode()
3009 znode = c->zroot.znode; in lookup_znode()
3011 znode = ubifs_load_znode(c, &c->zroot, NULL, 0); in lookup_znode()
3016 if (c->zroot.lnum == lnum && c->zroot.offs == offs) in lookup_znode()
3019 if (level >= znode->level) in lookup_znode()
3022 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3032 znode = left_znode(c, znode); in lookup_znode()
3037 ubifs_search_zbranch(c, znode, key, &n); in lookup_znode()
3040 if (znode->level == level + 1) in lookup_znode()
3042 znode = get_znode(c, znode, n); in lookup_znode()
3047 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs) in lookup_znode()
3048 return get_znode(c, znode, n); in lookup_znode()
3050 if (!is_hash_key(c, key)) in lookup_znode()
3062 n -= 1; in lookup_znode()
3064 znode = left_znode(c, znode); in lookup_znode()
3069 n = znode->child_cnt - 1; in lookup_znode()
3072 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3073 znode->zbranch[n].offs == offs) in lookup_znode()
3074 return get_znode(c, znode, n); in lookup_znode()
3076 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0) in lookup_znode()
3085 if (++n >= znode->child_cnt) { in lookup_znode()
3086 znode = right_znode(c, znode); in lookup_znode()
3094 if (znode->zbranch[n].lnum == lnum && in lookup_znode()
3095 znode->zbranch[n].offs == offs) in lookup_znode()
3096 return get_znode(c, znode, n); in lookup_znode()
3098 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0) in lookup_znode()
3105 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3106 * @c: UBIFS file-system description object
3119 * offset for a main-area node.
3121 int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level, in is_idx_node_in_tnc() argument
3126 znode = lookup_znode(c, key, level, lnum, offs); in is_idx_node_in_tnc()
3136 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3137 * @c: UBIFS file-system description object
3146 * and offset for a main-area node.
3148 static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, in is_leaf_node_in_tnc() argument
3154 const int unique = !is_hash_key(c, key); in is_leaf_node_in_tnc()
3156 found = ubifs_lookup_level0(c, key, &znode, &n); in is_leaf_node_in_tnc()
3161 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3162 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3174 err = tnc_prev(c, &znode, &n); in is_leaf_node_in_tnc()
3175 if (err == -ENOENT) in is_leaf_node_in_tnc()
3179 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3181 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3182 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3189 err = tnc_next(c, &znode, &n); in is_leaf_node_in_tnc()
3191 if (err == -ENOENT) in is_leaf_node_in_tnc()
3195 if (keys_cmp(c, key, &znode->zbranch[n].key)) in is_leaf_node_in_tnc()
3197 zbr = &znode->zbranch[n]; in is_leaf_node_in_tnc()
3198 if (lnum == zbr->lnum && offs == zbr->offs) in is_leaf_node_in_tnc()
3205 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3206 * @c: UBIFS file-system description object
3211 * @is_idx: non-zero if the node is an index node
3218 int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_tnc_has_node() argument
3223 mutex_lock(&c->tnc_mutex); in ubifs_tnc_has_node()
3225 err = is_idx_node_in_tnc(c, key, level, lnum, offs); in ubifs_tnc_has_node()
3237 err = is_leaf_node_in_tnc(c, key, lnum, offs); in ubifs_tnc_has_node()
3240 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_has_node()
3245 * ubifs_dirty_idx_node - dirty an index node.
3246 * @c: UBIFS file-system description object
3255 * for a main-area node. Returns %0 on success and a negative error code on
3258 int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level, in ubifs_dirty_idx_node() argument
3264 mutex_lock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3265 znode = lookup_znode(c, key, level, lnum, offs); in ubifs_dirty_idx_node()
3272 znode = dirty_cow_bottom_up(c, znode); in ubifs_dirty_idx_node()
3279 mutex_unlock(&c->tnc_mutex); in ubifs_dirty_idx_node()
3284 * dbg_check_inode_size - check if inode size is correct.
3285 * @c: UBIFS file-system description object
3290 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3294 int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode, in dbg_check_inode_size() argument
3302 if (!S_ISREG(inode->i_mode)) in dbg_check_inode_size()
3304 if (!dbg_is_chk_gen(c)) in dbg_check_inode_size()
3307 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; in dbg_check_inode_size()
3308 data_key_init(c, &from_key, inode->i_ino, block); in dbg_check_inode_size()
3309 highest_data_key(c, &to_key, inode->i_ino); in dbg_check_inode_size()
3311 mutex_lock(&c->tnc_mutex); in dbg_check_inode_size()
3312 err = ubifs_lookup_level0(c, &from_key, &znode, &n); in dbg_check_inode_size()
3321 err = tnc_next(c, &znode, &n); in dbg_check_inode_size()
3322 if (err == -ENOENT) { in dbg_check_inode_size()
3330 key = &znode->zbranch[n].key; in dbg_check_inode_size()
3331 if (!key_in_range(c, key, &from_key, &to_key)) in dbg_check_inode_size()
3335 block = key_block(c, key); in dbg_check_inode_size()
3336 ubifs_err(c, "inode %lu has size %lld, but there are data at offset %lld", in dbg_check_inode_size()
3337 (unsigned long)inode->i_ino, size, in dbg_check_inode_size()
3339 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()
3340 ubifs_dump_inode(c, inode); in dbg_check_inode_size()
3342 return -EINVAL; in dbg_check_inode_size()
3345 mutex_unlock(&c->tnc_mutex); in dbg_check_inode_size()