xref: /openbmc/u-boot/fs/ubifs/tnc.c (revision 1e52fea3)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Adrian Hunter
20  *          Artem Bityutskiy (Битюцкий Артём)
21  */
22 
23 /*
24  * This file implements TNC (Tree Node Cache) which caches indexing nodes of
25  * the UBIFS B-tree.
26  *
27  * At the moment the locking rules of the TNC tree are quite simple and
28  * straightforward. We just have a mutex and lock it when we traverse the
29  * tree. If a znode is not in memory, we read it from flash while still having
30  * the mutex locked.
31  */
32 
33 #include "ubifs.h"
34 
35 /*
36  * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
37  * @NAME_LESS: name corresponding to the first argument is less than second
38  * @NAME_MATCHES: names match
39  * @NAME_GREATER: name corresponding to the second argument is greater than
40  *                first
41  * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
42  *
43  * These constants were introduce to improve readability.
44  */
45 enum {
46 	NAME_LESS    = 0,
47 	NAME_MATCHES = 1,
48 	NAME_GREATER = 2,
49 	NOT_ON_MEDIA = 3,
50 };
51 
52 /**
53  * insert_old_idx - record an index node obsoleted since the last commit start.
54  * @c: UBIFS file-system description object
55  * @lnum: LEB number of obsoleted index node
56  * @offs: offset of obsoleted index node
57  *
58  * Returns %0 on success, and a negative error code on failure.
59  *
60  * For recovery, there must always be a complete intact version of the index on
61  * flash at all times. That is called the "old index". It is the index as at the
62  * time of the last successful commit. Many of the index nodes in the old index
63  * may be dirty, but they must not be erased until the next successful commit
64  * (at which point that index becomes the old index).
65  *
66  * That means that the garbage collection and the in-the-gaps method of
67  * committing must be able to determine if an index node is in the old index.
68  * Most of the old index nodes can be found by looking up the TNC using the
69  * 'lookup_znode()' function. However, some of the old index nodes may have
70  * been deleted from the current index or may have been changed so much that
71  * they cannot be easily found. In those cases, an entry is added to an RB-tree.
72  * That is what this function does. The RB-tree is ordered by LEB number and
73  * offset because they uniquely identify the old index node.
74  */
75 static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
76 {
77 	struct ubifs_old_idx *old_idx, *o;
78 	struct rb_node **p, *parent = NULL;
79 
80 	old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
81 	if (unlikely(!old_idx))
82 		return -ENOMEM;
83 	old_idx->lnum = lnum;
84 	old_idx->offs = offs;
85 
86 	p = &c->old_idx.rb_node;
87 	while (*p) {
88 		parent = *p;
89 		o = rb_entry(parent, struct ubifs_old_idx, rb);
90 		if (lnum < o->lnum)
91 			p = &(*p)->rb_left;
92 		else if (lnum > o->lnum)
93 			p = &(*p)->rb_right;
94 		else if (offs < o->offs)
95 			p = &(*p)->rb_left;
96 		else if (offs > o->offs)
97 			p = &(*p)->rb_right;
98 		else {
99 			ubifs_err("old idx added twice!");
100 			kfree(old_idx);
101 			return 0;
102 		}
103 	}
104 	rb_link_node(&old_idx->rb, parent, p);
105 	rb_insert_color(&old_idx->rb, &c->old_idx);
106 	return 0;
107 }
108 
109 /**
110  * insert_old_idx_znode - record a znode obsoleted since last commit start.
111  * @c: UBIFS file-system description object
112  * @znode: znode of obsoleted index node
113  *
114  * Returns %0 on success, and a negative error code on failure.
115  */
116 int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
117 {
118 	if (znode->parent) {
119 		struct ubifs_zbranch *zbr;
120 
121 		zbr = &znode->parent->zbranch[znode->iip];
122 		if (zbr->len)
123 			return insert_old_idx(c, zbr->lnum, zbr->offs);
124 	} else
125 		if (c->zroot.len)
126 			return insert_old_idx(c, c->zroot.lnum,
127 					      c->zroot.offs);
128 	return 0;
129 }
130 
131 /**
132  * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
133  * @c: UBIFS file-system description object
134  * @znode: znode of obsoleted index node
135  *
136  * Returns %0 on success, and a negative error code on failure.
137  */
138 static int ins_clr_old_idx_znode(struct ubifs_info *c,
139 				 struct ubifs_znode *znode)
140 {
141 	int err;
142 
143 	if (znode->parent) {
144 		struct ubifs_zbranch *zbr;
145 
146 		zbr = &znode->parent->zbranch[znode->iip];
147 		if (zbr->len) {
148 			err = insert_old_idx(c, zbr->lnum, zbr->offs);
149 			if (err)
150 				return err;
151 			zbr->lnum = 0;
152 			zbr->offs = 0;
153 			zbr->len = 0;
154 		}
155 	} else
156 		if (c->zroot.len) {
157 			err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
158 			if (err)
159 				return err;
160 			c->zroot.lnum = 0;
161 			c->zroot.offs = 0;
162 			c->zroot.len = 0;
163 		}
164 	return 0;
165 }
166 
167 /**
168  * destroy_old_idx - destroy the old_idx RB-tree.
169  * @c: UBIFS file-system description object
170  *
171  * During start commit, the old_idx RB-tree is used to avoid overwriting index
172  * nodes that were in the index last commit but have since been deleted.  This
173  * is necessary for recovery i.e. the old index must be kept intact until the
174  * new index is successfully written.  The old-idx RB-tree is used for the
175  * in-the-gaps method of writing index nodes and is destroyed every commit.
176  */
177 void destroy_old_idx(struct ubifs_info *c)
178 {
179 	struct rb_node *this = c->old_idx.rb_node;
180 	struct ubifs_old_idx *old_idx;
181 
182 	while (this) {
183 		if (this->rb_left) {
184 			this = this->rb_left;
185 			continue;
186 		} else if (this->rb_right) {
187 			this = this->rb_right;
188 			continue;
189 		}
190 		old_idx = rb_entry(this, struct ubifs_old_idx, rb);
191 		this = rb_parent(this);
192 		if (this) {
193 			if (this->rb_left == &old_idx->rb)
194 				this->rb_left = NULL;
195 			else
196 				this->rb_right = NULL;
197 		}
198 		kfree(old_idx);
199 	}
200 	c->old_idx = RB_ROOT;
201 }
202 
203 /**
204  * copy_znode - copy a dirty znode.
205  * @c: UBIFS file-system description object
206  * @znode: znode to copy
207  *
208  * A dirty znode being committed may not be changed, so it is copied.
209  */
210 static struct ubifs_znode *copy_znode(struct ubifs_info *c,
211 				      struct ubifs_znode *znode)
212 {
213 	struct ubifs_znode *zn;
214 
215 	zn = kmalloc(c->max_znode_sz, GFP_NOFS);
216 	if (unlikely(!zn))
217 		return ERR_PTR(-ENOMEM);
218 
219 	memcpy(zn, znode, c->max_znode_sz);
220 	zn->cnext = NULL;
221 	__set_bit(DIRTY_ZNODE, &zn->flags);
222 	__clear_bit(COW_ZNODE, &zn->flags);
223 
224 	ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
225 	__set_bit(OBSOLETE_ZNODE, &znode->flags);
226 
227 	if (znode->level != 0) {
228 		int i;
229 		const int n = zn->child_cnt;
230 
231 		/* The children now have new parent */
232 		for (i = 0; i < n; i++) {
233 			struct ubifs_zbranch *zbr = &zn->zbranch[i];
234 
235 			if (zbr->znode)
236 				zbr->znode->parent = zn;
237 		}
238 	}
239 
240 	atomic_long_inc(&c->dirty_zn_cnt);
241 	return zn;
242 }
243 
244 /**
245  * add_idx_dirt - add dirt due to a dirty znode.
246  * @c: UBIFS file-system description object
247  * @lnum: LEB number of index node
248  * @dirt: size of index node
249  *
250  * This function updates lprops dirty space and the new size of the index.
251  */
252 static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
253 {
254 	c->calc_idx_sz -= ALIGN(dirt, 8);
255 	return ubifs_add_dirt(c, lnum, dirt);
256 }
257 
258 /**
259  * dirty_cow_znode - ensure a znode is not being committed.
260  * @c: UBIFS file-system description object
261  * @zbr: branch of znode to check
262  *
263  * Returns dirtied znode on success or negative error code on failure.
264  */
265 static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
266 					   struct ubifs_zbranch *zbr)
267 {
268 	struct ubifs_znode *znode = zbr->znode;
269 	struct ubifs_znode *zn;
270 	int err;
271 
272 	if (!test_bit(COW_ZNODE, &znode->flags)) {
273 		/* znode is not being committed */
274 		if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
275 			atomic_long_inc(&c->dirty_zn_cnt);
276 			atomic_long_dec(&c->clean_zn_cnt);
277 			atomic_long_dec(&ubifs_clean_zn_cnt);
278 			err = add_idx_dirt(c, zbr->lnum, zbr->len);
279 			if (unlikely(err))
280 				return ERR_PTR(err);
281 		}
282 		return znode;
283 	}
284 
285 	zn = copy_znode(c, znode);
286 	if (IS_ERR(zn))
287 		return zn;
288 
289 	if (zbr->len) {
290 		err = insert_old_idx(c, zbr->lnum, zbr->offs);
291 		if (unlikely(err))
292 			return ERR_PTR(err);
293 		err = add_idx_dirt(c, zbr->lnum, zbr->len);
294 	} else
295 		err = 0;
296 
297 	zbr->znode = zn;
298 	zbr->lnum = 0;
299 	zbr->offs = 0;
300 	zbr->len = 0;
301 
302 	if (unlikely(err))
303 		return ERR_PTR(err);
304 	return zn;
305 }
306 
307 /**
308  * lnc_add - add a leaf node to the leaf node cache.
309  * @c: UBIFS file-system description object
310  * @zbr: zbranch of leaf node
311  * @node: leaf node
312  *
313  * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
314  * purpose of the leaf node cache is to save re-reading the same leaf node over
315  * and over again. Most things are cached by VFS, however the file system must
316  * cache directory entries for readdir and for resolving hash collisions. The
317  * present implementation of the leaf node cache is extremely simple, and
318  * allows for error returns that are not used but that may be needed if a more
319  * complex implementation is created.
320  *
321  * Note, this function does not add the @node object to LNC directly, but
322  * allocates a copy of the object and adds the copy to LNC. The reason for this
323  * is that @node has been allocated outside of the TNC subsystem and will be
324  * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
325  * may be changed at any time, e.g. freed by the shrinker.
326  */
327 static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
328 		   const void *node)
329 {
330 	int err;
331 	void *lnc_node;
332 	const struct ubifs_dent_node *dent = node;
333 
334 	ubifs_assert(!zbr->leaf);
335 	ubifs_assert(zbr->len != 0);
336 	ubifs_assert(is_hash_key(c, &zbr->key));
337 
338 	err = ubifs_validate_entry(c, dent);
339 	if (err) {
340 		dbg_dump_stack();
341 		dbg_dump_node(c, dent);
342 		return err;
343 	}
344 
345 	lnc_node = kmalloc(zbr->len, GFP_NOFS);
346 	if (!lnc_node)
347 		/* We don't have to have the cache, so no error */
348 		return 0;
349 
350 	memcpy(lnc_node, node, zbr->len);
351 	zbr->leaf = lnc_node;
352 	return 0;
353 }
354 
355  /**
356  * lnc_add_directly - add a leaf node to the leaf-node-cache.
357  * @c: UBIFS file-system description object
358  * @zbr: zbranch of leaf node
359  * @node: leaf node
360  *
361  * This function is similar to 'lnc_add()', but it does not create a copy of
362  * @node but inserts @node to TNC directly.
363  */
364 static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
365 			    void *node)
366 {
367 	int err;
368 
369 	ubifs_assert(!zbr->leaf);
370 	ubifs_assert(zbr->len != 0);
371 
372 	err = ubifs_validate_entry(c, node);
373 	if (err) {
374 		dbg_dump_stack();
375 		dbg_dump_node(c, node);
376 		return err;
377 	}
378 
379 	zbr->leaf = node;
380 	return 0;
381 }
382 
383 /**
384  * lnc_free - remove a leaf node from the leaf node cache.
385  * @zbr: zbranch of leaf node
386  * @node: leaf node
387  */
388 static void lnc_free(struct ubifs_zbranch *zbr)
389 {
390 	if (!zbr->leaf)
391 		return;
392 	kfree(zbr->leaf);
393 	zbr->leaf = NULL;
394 }
395 
396 /**
397  * tnc_read_node_nm - read a "hashed" leaf node.
398  * @c: UBIFS file-system description object
399  * @zbr: key and position of the node
400  * @node: node is returned here
401  *
402  * This function reads a "hashed" node defined by @zbr from the leaf node cache
403  * (in it is there) or from the hash media, in which case the node is also
404  * added to LNC. Returns zero in case of success or a negative negative error
405  * code in case of failure.
406  */
407 static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
408 			    void *node)
409 {
410 	int err;
411 
412 	ubifs_assert(is_hash_key(c, &zbr->key));
413 
414 	if (zbr->leaf) {
415 		/* Read from the leaf node cache */
416 		ubifs_assert(zbr->len != 0);
417 		memcpy(node, zbr->leaf, zbr->len);
418 		return 0;
419 	}
420 
421 	err = ubifs_tnc_read_node(c, zbr, node);
422 	if (err)
423 		return err;
424 
425 	/* Add the node to the leaf node cache */
426 	err = lnc_add(c, zbr, node);
427 	return err;
428 }
429 
430 /**
431  * try_read_node - read a node if it is a node.
432  * @c: UBIFS file-system description object
433  * @buf: buffer to read to
434  * @type: node type
435  * @len: node length (not aligned)
436  * @lnum: LEB number of node to read
437  * @offs: offset of node to read
438  *
439  * This function tries to read a node of known type and length, checks it and
440  * stores it in @buf. This function returns %1 if a node is present and %0 if
441  * a node is not present. A negative error code is returned for I/O errors.
442  * This function performs that same function as ubifs_read_node except that
443  * it does not require that there is actually a node present and instead
444  * the return code indicates if a node was read.
445  *
446  * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
447  * is true (it is controlled by corresponding mount option). However, if
448  * @c->always_chk_crc is true, @c->no_chk_data_crc is ignored and CRC is always
449  * checked.
450  */
451 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
452 			 int len, int lnum, int offs)
453 {
454 	int err, node_len;
455 	struct ubifs_ch *ch = buf;
456 	uint32_t crc, node_crc;
457 
458 	dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
459 
460 	err = ubi_read(c->ubi, lnum, buf, offs, len);
461 	if (err) {
462 		ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
463 			  type, lnum, offs, err);
464 		return err;
465 	}
466 
467 	if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
468 		return 0;
469 
470 	if (ch->node_type != type)
471 		return 0;
472 
473 	node_len = le32_to_cpu(ch->len);
474 	if (node_len != len)
475 		return 0;
476 
477 	if (type == UBIFS_DATA_NODE && !c->always_chk_crc && c->no_chk_data_crc)
478 		return 1;
479 
480 	crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
481 	node_crc = le32_to_cpu(ch->crc);
482 	if (crc != node_crc)
483 		return 0;
484 
485 	return 1;
486 }
487 
488 /**
489  * fallible_read_node - try to read a leaf node.
490  * @c: UBIFS file-system description object
491  * @key:  key of node to read
492  * @zbr:  position of node
493  * @node: node returned
494  *
495  * This function tries to read a node and returns %1 if the node is read, %0
496  * if the node is not present, and a negative error code in the case of error.
497  */
498 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
499 			      struct ubifs_zbranch *zbr, void *node)
500 {
501 	int ret;
502 
503 	dbg_tnc("LEB %d:%d, key %s", zbr->lnum, zbr->offs, DBGKEY(key));
504 
505 	ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
506 			    zbr->offs);
507 	if (ret == 1) {
508 		union ubifs_key node_key;
509 		struct ubifs_dent_node *dent = node;
510 
511 		/* All nodes have key in the same place */
512 		key_read(c, &dent->key, &node_key);
513 		if (keys_cmp(c, key, &node_key) != 0)
514 			ret = 0;
515 	}
516 	if (ret == 0 && c->replaying)
517 		dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
518 			zbr->lnum, zbr->offs, zbr->len, DBGKEY(key));
519 	return ret;
520 }
521 
522 /**
523  * matches_name - determine if a direntry or xattr entry matches a given name.
524  * @c: UBIFS file-system description object
525  * @zbr: zbranch of dent
526  * @nm: name to match
527  *
528  * This function checks if xentry/direntry referred by zbranch @zbr matches name
529  * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
530  * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
531  * of failure, a negative error code is returned.
532  */
533 static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
534 			const struct qstr *nm)
535 {
536 	struct ubifs_dent_node *dent;
537 	int nlen, err;
538 
539 	/* If possible, match against the dent in the leaf node cache */
540 	if (!zbr->leaf) {
541 		dent = kmalloc(zbr->len, GFP_NOFS);
542 		if (!dent)
543 			return -ENOMEM;
544 
545 		err = ubifs_tnc_read_node(c, zbr, dent);
546 		if (err)
547 			goto out_free;
548 
549 		/* Add the node to the leaf node cache */
550 		err = lnc_add_directly(c, zbr, dent);
551 		if (err)
552 			goto out_free;
553 	} else
554 		dent = zbr->leaf;
555 
556 	nlen = le16_to_cpu(dent->nlen);
557 	err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
558 	if (err == 0) {
559 		if (nlen == nm->len)
560 			return NAME_MATCHES;
561 		else if (nlen < nm->len)
562 			return NAME_LESS;
563 		else
564 			return NAME_GREATER;
565 	} else if (err < 0)
566 		return NAME_LESS;
567 	else
568 		return NAME_GREATER;
569 
570 out_free:
571 	kfree(dent);
572 	return err;
573 }
574 
575 /**
576  * get_znode - get a TNC znode that may not be loaded yet.
577  * @c: UBIFS file-system description object
578  * @znode: parent znode
579  * @n: znode branch slot number
580  *
581  * This function returns the znode or a negative error code.
582  */
583 static struct ubifs_znode *get_znode(struct ubifs_info *c,
584 				     struct ubifs_znode *znode, int n)
585 {
586 	struct ubifs_zbranch *zbr;
587 
588 	zbr = &znode->zbranch[n];
589 	if (zbr->znode)
590 		znode = zbr->znode;
591 	else
592 		znode = ubifs_load_znode(c, zbr, znode, n);
593 	return znode;
594 }
595 
596 /**
597  * tnc_next - find next TNC entry.
598  * @c: UBIFS file-system description object
599  * @zn: znode is passed and returned here
600  * @n: znode branch slot number is passed and returned here
601  *
602  * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
603  * no next entry, or a negative error code otherwise.
604  */
605 static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
606 {
607 	struct ubifs_znode *znode = *zn;
608 	int nn = *n;
609 
610 	nn += 1;
611 	if (nn < znode->child_cnt) {
612 		*n = nn;
613 		return 0;
614 	}
615 	while (1) {
616 		struct ubifs_znode *zp;
617 
618 		zp = znode->parent;
619 		if (!zp)
620 			return -ENOENT;
621 		nn = znode->iip + 1;
622 		znode = zp;
623 		if (nn < znode->child_cnt) {
624 			znode = get_znode(c, znode, nn);
625 			if (IS_ERR(znode))
626 				return PTR_ERR(znode);
627 			while (znode->level != 0) {
628 				znode = get_znode(c, znode, 0);
629 				if (IS_ERR(znode))
630 					return PTR_ERR(znode);
631 			}
632 			nn = 0;
633 			break;
634 		}
635 	}
636 	*zn = znode;
637 	*n = nn;
638 	return 0;
639 }
640 
641 /**
642  * tnc_prev - find previous TNC entry.
643  * @c: UBIFS file-system description object
644  * @zn: znode is returned here
645  * @n: znode branch slot number is passed and returned here
646  *
647  * This function returns %0 if the previous TNC entry is found, %-ENOENT if
648  * there is no next entry, or a negative error code otherwise.
649  */
650 static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
651 {
652 	struct ubifs_znode *znode = *zn;
653 	int nn = *n;
654 
655 	if (nn > 0) {
656 		*n = nn - 1;
657 		return 0;
658 	}
659 	while (1) {
660 		struct ubifs_znode *zp;
661 
662 		zp = znode->parent;
663 		if (!zp)
664 			return -ENOENT;
665 		nn = znode->iip - 1;
666 		znode = zp;
667 		if (nn >= 0) {
668 			znode = get_znode(c, znode, nn);
669 			if (IS_ERR(znode))
670 				return PTR_ERR(znode);
671 			while (znode->level != 0) {
672 				nn = znode->child_cnt - 1;
673 				znode = get_znode(c, znode, nn);
674 				if (IS_ERR(znode))
675 					return PTR_ERR(znode);
676 			}
677 			nn = znode->child_cnt - 1;
678 			break;
679 		}
680 	}
681 	*zn = znode;
682 	*n = nn;
683 	return 0;
684 }
685 
686 /**
687  * resolve_collision - resolve a collision.
688  * @c: UBIFS file-system description object
689  * @key: key of a directory or extended attribute entry
690  * @zn: znode is returned here
691  * @n: zbranch number is passed and returned here
692  * @nm: name of the entry
693  *
694  * This function is called for "hashed" keys to make sure that the found key
695  * really corresponds to the looked up node (directory or extended attribute
696  * entry). It returns %1 and sets @zn and @n if the collision is resolved.
697  * %0 is returned if @nm is not found and @zn and @n are set to the previous
698  * entry, i.e. to the entry after which @nm could follow if it were in TNC.
699  * This means that @n may be set to %-1 if the leftmost key in @zn is the
700  * previous one. A negative error code is returned on failures.
701  */
702 static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
703 			     struct ubifs_znode **zn, int *n,
704 			     const struct qstr *nm)
705 {
706 	int err;
707 
708 	err = matches_name(c, &(*zn)->zbranch[*n], nm);
709 	if (unlikely(err < 0))
710 		return err;
711 	if (err == NAME_MATCHES)
712 		return 1;
713 
714 	if (err == NAME_GREATER) {
715 		/* Look left */
716 		while (1) {
717 			err = tnc_prev(c, zn, n);
718 			if (err == -ENOENT) {
719 				ubifs_assert(*n == 0);
720 				*n = -1;
721 				return 0;
722 			}
723 			if (err < 0)
724 				return err;
725 			if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
726 				/*
727 				 * We have found the branch after which we would
728 				 * like to insert, but inserting in this znode
729 				 * may still be wrong. Consider the following 3
730 				 * znodes, in the case where we are resolving a
731 				 * collision with Key2.
732 				 *
733 				 *                  znode zp
734 				 *            ----------------------
735 				 * level 1     |  Key0  |  Key1  |
736 				 *            -----------------------
737 				 *                 |            |
738 				 *       znode za  |            |  znode zb
739 				 *          ------------      ------------
740 				 * level 0  |  Key0  |        |  Key2  |
741 				 *          ------------      ------------
742 				 *
743 				 * The lookup finds Key2 in znode zb. Lets say
744 				 * there is no match and the name is greater so
745 				 * we look left. When we find Key0, we end up
746 				 * here. If we return now, we will insert into
747 				 * znode za at slot n = 1.  But that is invalid
748 				 * according to the parent's keys.  Key2 must
749 				 * be inserted into znode zb.
750 				 *
751 				 * Note, this problem is not relevant for the
752 				 * case when we go right, because
753 				 * 'tnc_insert()' would correct the parent key.
754 				 */
755 				if (*n == (*zn)->child_cnt - 1) {
756 					err = tnc_next(c, zn, n);
757 					if (err) {
758 						/* Should be impossible */
759 						ubifs_assert(0);
760 						if (err == -ENOENT)
761 							err = -EINVAL;
762 						return err;
763 					}
764 					ubifs_assert(*n == 0);
765 					*n = -1;
766 				}
767 				return 0;
768 			}
769 			err = matches_name(c, &(*zn)->zbranch[*n], nm);
770 			if (err < 0)
771 				return err;
772 			if (err == NAME_LESS)
773 				return 0;
774 			if (err == NAME_MATCHES)
775 				return 1;
776 			ubifs_assert(err == NAME_GREATER);
777 		}
778 	} else {
779 		int nn = *n;
780 		struct ubifs_znode *znode = *zn;
781 
782 		/* Look right */
783 		while (1) {
784 			err = tnc_next(c, &znode, &nn);
785 			if (err == -ENOENT)
786 				return 0;
787 			if (err < 0)
788 				return err;
789 			if (keys_cmp(c, &znode->zbranch[nn].key, key))
790 				return 0;
791 			err = matches_name(c, &znode->zbranch[nn], nm);
792 			if (err < 0)
793 				return err;
794 			if (err == NAME_GREATER)
795 				return 0;
796 			*zn = znode;
797 			*n = nn;
798 			if (err == NAME_MATCHES)
799 				return 1;
800 			ubifs_assert(err == NAME_LESS);
801 		}
802 	}
803 }
804 
805 /**
806  * fallible_matches_name - determine if a dent matches a given name.
807  * @c: UBIFS file-system description object
808  * @zbr: zbranch of dent
809  * @nm: name to match
810  *
811  * This is a "fallible" version of 'matches_name()' function which does not
812  * panic if the direntry/xentry referred by @zbr does not exist on the media.
813  *
814  * This function checks if xentry/direntry referred by zbranch @zbr matches name
815  * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
816  * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
817  * if xentry/direntry referred by @zbr does not exist on the media. A negative
818  * error code is returned in case of failure.
819  */
820 static int fallible_matches_name(struct ubifs_info *c,
821 				 struct ubifs_zbranch *zbr,
822 				 const struct qstr *nm)
823 {
824 	struct ubifs_dent_node *dent;
825 	int nlen, err;
826 
827 	/* If possible, match against the dent in the leaf node cache */
828 	if (!zbr->leaf) {
829 		dent = kmalloc(zbr->len, GFP_NOFS);
830 		if (!dent)
831 			return -ENOMEM;
832 
833 		err = fallible_read_node(c, &zbr->key, zbr, dent);
834 		if (err < 0)
835 			goto out_free;
836 		if (err == 0) {
837 			/* The node was not present */
838 			err = NOT_ON_MEDIA;
839 			goto out_free;
840 		}
841 		ubifs_assert(err == 1);
842 
843 		err = lnc_add_directly(c, zbr, dent);
844 		if (err)
845 			goto out_free;
846 	} else
847 		dent = zbr->leaf;
848 
849 	nlen = le16_to_cpu(dent->nlen);
850 	err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
851 	if (err == 0) {
852 		if (nlen == nm->len)
853 			return NAME_MATCHES;
854 		else if (nlen < nm->len)
855 			return NAME_LESS;
856 		else
857 			return NAME_GREATER;
858 	} else if (err < 0)
859 		return NAME_LESS;
860 	else
861 		return NAME_GREATER;
862 
863 out_free:
864 	kfree(dent);
865 	return err;
866 }
867 
868 /**
869  * fallible_resolve_collision - resolve a collision even if nodes are missing.
870  * @c: UBIFS file-system description object
871  * @key: key
872  * @zn: znode is returned here
873  * @n: branch number is passed and returned here
874  * @nm: name of directory entry
875  * @adding: indicates caller is adding a key to the TNC
876  *
877  * This is a "fallible" version of the 'resolve_collision()' function which
878  * does not panic if one of the nodes referred to by TNC does not exist on the
879  * media. This may happen when replaying the journal if a deleted node was
880  * Garbage-collected and the commit was not done. A branch that refers to a node
881  * that is not present is called a dangling branch. The following are the return
882  * codes for this function:
883  *  o if @nm was found, %1 is returned and @zn and @n are set to the found
884  *    branch;
885  *  o if we are @adding and @nm was not found, %0 is returned;
886  *  o if we are not @adding and @nm was not found, but a dangling branch was
887  *    found, then %1 is returned and @zn and @n are set to the dangling branch;
888  *  o a negative error code is returned in case of failure.
889  */
890 static int fallible_resolve_collision(struct ubifs_info *c,
891 				      const union ubifs_key *key,
892 				      struct ubifs_znode **zn, int *n,
893 				      const struct qstr *nm, int adding)
894 {
895 	struct ubifs_znode *o_znode = NULL, *znode = *zn;
896 	int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
897 
898 	cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
899 	if (unlikely(cmp < 0))
900 		return cmp;
901 	if (cmp == NAME_MATCHES)
902 		return 1;
903 	if (cmp == NOT_ON_MEDIA) {
904 		o_znode = znode;
905 		o_n = nn;
906 		/*
907 		 * We are unlucky and hit a dangling branch straight away.
908 		 * Now we do not really know where to go to find the needed
909 		 * branch - to the left or to the right. Well, let's try left.
910 		 */
911 		unsure = 1;
912 	} else if (!adding)
913 		unsure = 1; /* Remove a dangling branch wherever it is */
914 
915 	if (cmp == NAME_GREATER || unsure) {
916 		/* Look left */
917 		while (1) {
918 			err = tnc_prev(c, zn, n);
919 			if (err == -ENOENT) {
920 				ubifs_assert(*n == 0);
921 				*n = -1;
922 				break;
923 			}
924 			if (err < 0)
925 				return err;
926 			if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
927 				/* See comments in 'resolve_collision()' */
928 				if (*n == (*zn)->child_cnt - 1) {
929 					err = tnc_next(c, zn, n);
930 					if (err) {
931 						/* Should be impossible */
932 						ubifs_assert(0);
933 						if (err == -ENOENT)
934 							err = -EINVAL;
935 						return err;
936 					}
937 					ubifs_assert(*n == 0);
938 					*n = -1;
939 				}
940 				break;
941 			}
942 			err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
943 			if (err < 0)
944 				return err;
945 			if (err == NAME_MATCHES)
946 				return 1;
947 			if (err == NOT_ON_MEDIA) {
948 				o_znode = *zn;
949 				o_n = *n;
950 				continue;
951 			}
952 			if (!adding)
953 				continue;
954 			if (err == NAME_LESS)
955 				break;
956 			else
957 				unsure = 0;
958 		}
959 	}
960 
961 	if (cmp == NAME_LESS || unsure) {
962 		/* Look right */
963 		*zn = znode;
964 		*n = nn;
965 		while (1) {
966 			err = tnc_next(c, &znode, &nn);
967 			if (err == -ENOENT)
968 				break;
969 			if (err < 0)
970 				return err;
971 			if (keys_cmp(c, &znode->zbranch[nn].key, key))
972 				break;
973 			err = fallible_matches_name(c, &znode->zbranch[nn], nm);
974 			if (err < 0)
975 				return err;
976 			if (err == NAME_GREATER)
977 				break;
978 			*zn = znode;
979 			*n = nn;
980 			if (err == NAME_MATCHES)
981 				return 1;
982 			if (err == NOT_ON_MEDIA) {
983 				o_znode = znode;
984 				o_n = nn;
985 			}
986 		}
987 	}
988 
989 	/* Never match a dangling branch when adding */
990 	if (adding || !o_znode)
991 		return 0;
992 
993 	dbg_mnt("dangling match LEB %d:%d len %d %s",
994 		o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
995 		o_znode->zbranch[o_n].len, DBGKEY(key));
996 	*zn = o_znode;
997 	*n = o_n;
998 	return 1;
999 }
1000 
1001 /**
1002  * matches_position - determine if a zbranch matches a given position.
1003  * @zbr: zbranch of dent
1004  * @lnum: LEB number of dent to match
1005  * @offs: offset of dent to match
1006  *
1007  * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1008  */
1009 static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1010 {
1011 	if (zbr->lnum == lnum && zbr->offs == offs)
1012 		return 1;
1013 	else
1014 		return 0;
1015 }
1016 
1017 /**
1018  * resolve_collision_directly - resolve a collision directly.
1019  * @c: UBIFS file-system description object
1020  * @key: key of directory entry
1021  * @zn: znode is passed and returned here
1022  * @n: zbranch number is passed and returned here
1023  * @lnum: LEB number of dent node to match
1024  * @offs: offset of dent node to match
1025  *
1026  * This function is used for "hashed" keys to make sure the found directory or
1027  * extended attribute entry node is what was looked for. It is used when the
1028  * flash address of the right node is known (@lnum:@offs) which makes it much
1029  * easier to resolve collisions (no need to read entries and match full
1030  * names). This function returns %1 and sets @zn and @n if the collision is
1031  * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
1032  * previous directory entry. Otherwise a negative error code is returned.
1033  */
1034 static int resolve_collision_directly(struct ubifs_info *c,
1035 				      const union ubifs_key *key,
1036 				      struct ubifs_znode **zn, int *n,
1037 				      int lnum, int offs)
1038 {
1039 	struct ubifs_znode *znode;
1040 	int nn, err;
1041 
1042 	znode = *zn;
1043 	nn = *n;
1044 	if (matches_position(&znode->zbranch[nn], lnum, offs))
1045 		return 1;
1046 
1047 	/* Look left */
1048 	while (1) {
1049 		err = tnc_prev(c, &znode, &nn);
1050 		if (err == -ENOENT)
1051 			break;
1052 		if (err < 0)
1053 			return err;
1054 		if (keys_cmp(c, &znode->zbranch[nn].key, key))
1055 			break;
1056 		if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1057 			*zn = znode;
1058 			*n = nn;
1059 			return 1;
1060 		}
1061 	}
1062 
1063 	/* Look right */
1064 	znode = *zn;
1065 	nn = *n;
1066 	while (1) {
1067 		err = tnc_next(c, &znode, &nn);
1068 		if (err == -ENOENT)
1069 			return 0;
1070 		if (err < 0)
1071 			return err;
1072 		if (keys_cmp(c, &znode->zbranch[nn].key, key))
1073 			return 0;
1074 		*zn = znode;
1075 		*n = nn;
1076 		if (matches_position(&znode->zbranch[nn], lnum, offs))
1077 			return 1;
1078 	}
1079 }
1080 
1081 /**
1082  * dirty_cow_bottom_up - dirty a znode and its ancestors.
1083  * @c: UBIFS file-system description object
1084  * @znode: znode to dirty
1085  *
1086  * If we do not have a unique key that resides in a znode, then we cannot
1087  * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
1088  * This function records the path back to the last dirty ancestor, and then
1089  * dirties the znodes on that path.
1090  */
1091 static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1092 					       struct ubifs_znode *znode)
1093 {
1094 	struct ubifs_znode *zp;
1095 	int *path = c->bottom_up_buf, p = 0;
1096 
1097 	ubifs_assert(c->zroot.znode);
1098 	ubifs_assert(znode);
1099 	if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1100 		kfree(c->bottom_up_buf);
1101 		c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1102 					   GFP_NOFS);
1103 		if (!c->bottom_up_buf)
1104 			return ERR_PTR(-ENOMEM);
1105 		path = c->bottom_up_buf;
1106 	}
1107 	if (c->zroot.znode->level) {
1108 		/* Go up until parent is dirty */
1109 		while (1) {
1110 			int n;
1111 
1112 			zp = znode->parent;
1113 			if (!zp)
1114 				break;
1115 			n = znode->iip;
1116 			ubifs_assert(p < c->zroot.znode->level);
1117 			path[p++] = n;
1118 			if (!zp->cnext && ubifs_zn_dirty(znode))
1119 				break;
1120 			znode = zp;
1121 		}
1122 	}
1123 
1124 	/* Come back down, dirtying as we go */
1125 	while (1) {
1126 		struct ubifs_zbranch *zbr;
1127 
1128 		zp = znode->parent;
1129 		if (zp) {
1130 			ubifs_assert(path[p - 1] >= 0);
1131 			ubifs_assert(path[p - 1] < zp->child_cnt);
1132 			zbr = &zp->zbranch[path[--p]];
1133 			znode = dirty_cow_znode(c, zbr);
1134 		} else {
1135 			ubifs_assert(znode == c->zroot.znode);
1136 			znode = dirty_cow_znode(c, &c->zroot);
1137 		}
1138 		if (IS_ERR(znode) || !p)
1139 			break;
1140 		ubifs_assert(path[p - 1] >= 0);
1141 		ubifs_assert(path[p - 1] < znode->child_cnt);
1142 		znode = znode->zbranch[path[p - 1]].znode;
1143 	}
1144 
1145 	return znode;
1146 }
1147 
1148 /**
1149  * ubifs_lookup_level0 - search for zero-level znode.
1150  * @c: UBIFS file-system description object
1151  * @key:  key to lookup
1152  * @zn: znode is returned here
1153  * @n: znode branch slot number is returned here
1154  *
1155  * This function looks up the TNC tree and search for zero-level znode which
1156  * refers key @key. The found zero-level znode is returned in @zn. There are 3
1157  * cases:
1158  *   o exact match, i.e. the found zero-level znode contains key @key, then %1
1159  *     is returned and slot number of the matched branch is stored in @n;
1160  *   o not exact match, which means that zero-level znode does not contain
1161  *     @key, then %0 is returned and slot number of the closed branch is stored
1162  *     in  @n;
1163  *   o @key is so small that it is even less than the lowest key of the
1164  *     leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1165  *
1166  * Note, when the TNC tree is traversed, some znodes may be absent, then this
1167  * function reads corresponding indexing nodes and inserts them to TNC. In
1168  * case of failure, a negative error code is returned.
1169  */
1170 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1171 			struct ubifs_znode **zn, int *n)
1172 {
1173 	int err, exact;
1174 	struct ubifs_znode *znode;
1175 	unsigned long time = get_seconds();
1176 
1177 	dbg_tnc("search key %s", DBGKEY(key));
1178 
1179 	znode = c->zroot.znode;
1180 	if (unlikely(!znode)) {
1181 		znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1182 		if (IS_ERR(znode))
1183 			return PTR_ERR(znode);
1184 	}
1185 
1186 	znode->time = time;
1187 
1188 	while (1) {
1189 		struct ubifs_zbranch *zbr;
1190 
1191 		exact = ubifs_search_zbranch(c, znode, key, n);
1192 
1193 		if (znode->level == 0)
1194 			break;
1195 
1196 		if (*n < 0)
1197 			*n = 0;
1198 		zbr = &znode->zbranch[*n];
1199 
1200 		if (zbr->znode) {
1201 			znode->time = time;
1202 			znode = zbr->znode;
1203 			continue;
1204 		}
1205 
1206 		/* znode is not in TNC cache, load it from the media */
1207 		znode = ubifs_load_znode(c, zbr, znode, *n);
1208 		if (IS_ERR(znode))
1209 			return PTR_ERR(znode);
1210 	}
1211 
1212 	*zn = znode;
1213 	if (exact || !is_hash_key(c, key) || *n != -1) {
1214 		dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1215 		return exact;
1216 	}
1217 
1218 	/*
1219 	 * Here is a tricky place. We have not found the key and this is a
1220 	 * "hashed" key, which may collide. The rest of the code deals with
1221 	 * situations like this:
1222 	 *
1223 	 *                  | 3 | 5 |
1224 	 *                  /       \
1225 	 *          | 3 | 5 |      | 6 | 7 | (x)
1226 	 *
1227 	 * Or more a complex example:
1228 	 *
1229 	 *                | 1 | 5 |
1230 	 *                /       \
1231 	 *       | 1 | 3 |         | 5 | 8 |
1232 	 *              \           /
1233 	 *          | 5 | 5 |   | 6 | 7 | (x)
1234 	 *
1235 	 * In the examples, if we are looking for key "5", we may reach nodes
1236 	 * marked with "(x)". In this case what we have do is to look at the
1237 	 * left and see if there is "5" key there. If there is, we have to
1238 	 * return it.
1239 	 *
1240 	 * Note, this whole situation is possible because we allow to have
1241 	 * elements which are equivalent to the next key in the parent in the
1242 	 * children of current znode. For example, this happens if we split a
1243 	 * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
1244 	 * like this:
1245 	 *                      | 3 | 5 |
1246 	 *                       /     \
1247 	 *                | 3 | 5 |   | 5 | 6 | 7 |
1248 	 *                              ^
1249 	 * And this becomes what is at the first "picture" after key "5" marked
1250 	 * with "^" is removed. What could be done is we could prohibit
1251 	 * splitting in the middle of the colliding sequence. Also, when
1252 	 * removing the leftmost key, we would have to correct the key of the
1253 	 * parent node, which would introduce additional complications. Namely,
1254 	 * if we changed the the leftmost key of the parent znode, the garbage
1255 	 * collector would be unable to find it (GC is doing this when GC'ing
1256 	 * indexing LEBs). Although we already have an additional RB-tree where
1257 	 * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
1258 	 * after the commit. But anyway, this does not look easy to implement
1259 	 * so we did not try this.
1260 	 */
1261 	err = tnc_prev(c, &znode, n);
1262 	if (err == -ENOENT) {
1263 		dbg_tnc("found 0, lvl %d, n -1", znode->level);
1264 		*n = -1;
1265 		return 0;
1266 	}
1267 	if (unlikely(err < 0))
1268 		return err;
1269 	if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1270 		dbg_tnc("found 0, lvl %d, n -1", znode->level);
1271 		*n = -1;
1272 		return 0;
1273 	}
1274 
1275 	dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1276 	*zn = znode;
1277 	return 1;
1278 }
1279 
1280 /**
1281  * lookup_level0_dirty - search for zero-level znode dirtying.
1282  * @c: UBIFS file-system description object
1283  * @key:  key to lookup
1284  * @zn: znode is returned here
1285  * @n: znode branch slot number is returned here
1286  *
1287  * This function looks up the TNC tree and search for zero-level znode which
1288  * refers key @key. The found zero-level znode is returned in @zn. There are 3
1289  * cases:
1290  *   o exact match, i.e. the found zero-level znode contains key @key, then %1
1291  *     is returned and slot number of the matched branch is stored in @n;
1292  *   o not exact match, which means that zero-level znode does not contain @key
1293  *     then %0 is returned and slot number of the closed branch is stored in
1294  *     @n;
1295  *   o @key is so small that it is even less than the lowest key of the
1296  *     leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1297  *
1298  * Additionally all znodes in the path from the root to the located zero-level
1299  * znode are marked as dirty.
1300  *
1301  * Note, when the TNC tree is traversed, some znodes may be absent, then this
1302  * function reads corresponding indexing nodes and inserts them to TNC. In
1303  * case of failure, a negative error code is returned.
1304  */
1305 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1306 			       struct ubifs_znode **zn, int *n)
1307 {
1308 	int err, exact;
1309 	struct ubifs_znode *znode;
1310 	unsigned long time = get_seconds();
1311 
1312 	dbg_tnc("search and dirty key %s", DBGKEY(key));
1313 
1314 	znode = c->zroot.znode;
1315 	if (unlikely(!znode)) {
1316 		znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1317 		if (IS_ERR(znode))
1318 			return PTR_ERR(znode);
1319 	}
1320 
1321 	znode = dirty_cow_znode(c, &c->zroot);
1322 	if (IS_ERR(znode))
1323 		return PTR_ERR(znode);
1324 
1325 	znode->time = time;
1326 
1327 	while (1) {
1328 		struct ubifs_zbranch *zbr;
1329 
1330 		exact = ubifs_search_zbranch(c, znode, key, n);
1331 
1332 		if (znode->level == 0)
1333 			break;
1334 
1335 		if (*n < 0)
1336 			*n = 0;
1337 		zbr = &znode->zbranch[*n];
1338 
1339 		if (zbr->znode) {
1340 			znode->time = time;
1341 			znode = dirty_cow_znode(c, zbr);
1342 			if (IS_ERR(znode))
1343 				return PTR_ERR(znode);
1344 			continue;
1345 		}
1346 
1347 		/* znode is not in TNC cache, load it from the media */
1348 		znode = ubifs_load_znode(c, zbr, znode, *n);
1349 		if (IS_ERR(znode))
1350 			return PTR_ERR(znode);
1351 		znode = dirty_cow_znode(c, zbr);
1352 		if (IS_ERR(znode))
1353 			return PTR_ERR(znode);
1354 	}
1355 
1356 	*zn = znode;
1357 	if (exact || !is_hash_key(c, key) || *n != -1) {
1358 		dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1359 		return exact;
1360 	}
1361 
1362 	/*
1363 	 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1364 	 * code.
1365 	 */
1366 	err = tnc_prev(c, &znode, n);
1367 	if (err == -ENOENT) {
1368 		*n = -1;
1369 		dbg_tnc("found 0, lvl %d, n -1", znode->level);
1370 		return 0;
1371 	}
1372 	if (unlikely(err < 0))
1373 		return err;
1374 	if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1375 		*n = -1;
1376 		dbg_tnc("found 0, lvl %d, n -1", znode->level);
1377 		return 0;
1378 	}
1379 
1380 	if (znode->cnext || !ubifs_zn_dirty(znode)) {
1381 		znode = dirty_cow_bottom_up(c, znode);
1382 		if (IS_ERR(znode))
1383 			return PTR_ERR(znode);
1384 	}
1385 
1386 	dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1387 	*zn = znode;
1388 	return 1;
1389 }
1390 
1391 /**
1392  * maybe_leb_gced - determine if a LEB may have been garbage collected.
1393  * @c: UBIFS file-system description object
1394  * @lnum: LEB number
1395  * @gc_seq1: garbage collection sequence number
1396  *
1397  * This function determines if @lnum may have been garbage collected since
1398  * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1399  * %0 is returned.
1400  */
1401 static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1402 {
1403 	/*
1404 	 * No garbage collection in the read-only U-Boot implementation
1405 	 */
1406 	return 0;
1407 }
1408 
1409 /**
1410  * ubifs_tnc_locate - look up a file-system node and return it and its location.
1411  * @c: UBIFS file-system description object
1412  * @key: node key to lookup
1413  * @node: the node is returned here
1414  * @lnum: LEB number is returned here
1415  * @offs: offset is returned here
1416  *
1417  * This function look up and reads node with key @key. The caller has to make
1418  * sure the @node buffer is large enough to fit the node. Returns zero in case
1419  * of success, %-ENOENT if the node was not found, and a negative error code in
1420  * case of failure. The node location can be returned in @lnum and @offs.
1421  */
1422 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1423 		     void *node, int *lnum, int *offs)
1424 {
1425 	int found, n, err, safely = 0, gc_seq1;
1426 	struct ubifs_znode *znode;
1427 	struct ubifs_zbranch zbr, *zt;
1428 
1429 again:
1430 	mutex_lock(&c->tnc_mutex);
1431 	found = ubifs_lookup_level0(c, key, &znode, &n);
1432 	if (!found) {
1433 		err = -ENOENT;
1434 		goto out;
1435 	} else if (found < 0) {
1436 		err = found;
1437 		goto out;
1438 	}
1439 	zt = &znode->zbranch[n];
1440 	if (lnum) {
1441 		*lnum = zt->lnum;
1442 		*offs = zt->offs;
1443 	}
1444 	if (is_hash_key(c, key)) {
1445 		/*
1446 		 * In this case the leaf node cache gets used, so we pass the
1447 		 * address of the zbranch and keep the mutex locked
1448 		 */
1449 		err = tnc_read_node_nm(c, zt, node);
1450 		goto out;
1451 	}
1452 	if (safely) {
1453 		err = ubifs_tnc_read_node(c, zt, node);
1454 		goto out;
1455 	}
1456 	/* Drop the TNC mutex prematurely and race with garbage collection */
1457 	zbr = znode->zbranch[n];
1458 	gc_seq1 = c->gc_seq;
1459 	mutex_unlock(&c->tnc_mutex);
1460 
1461 	err = fallible_read_node(c, key, &zbr, node);
1462 	if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1463 		/*
1464 		 * The node may have been GC'ed out from under us so try again
1465 		 * while keeping the TNC mutex locked.
1466 		 */
1467 		safely = 1;
1468 		goto again;
1469 	}
1470 	return 0;
1471 
1472 out:
1473 	mutex_unlock(&c->tnc_mutex);
1474 	return err;
1475 }
1476 
1477 /**
1478  * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1479  * @c: UBIFS file-system description object
1480  * @bu: bulk-read parameters and results
1481  *
1482  * Lookup consecutive data node keys for the same inode that reside
1483  * consecutively in the same LEB. This function returns zero in case of success
1484  * and a negative error code in case of failure.
1485  *
1486  * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1487  * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1488  * maximum possible amount of nodes for bulk-read.
1489  */
1490 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1491 {
1492 	int n, err = 0, lnum = -1, uninitialized_var(offs);
1493 	int uninitialized_var(len);
1494 	unsigned int block = key_block(c, &bu->key);
1495 	struct ubifs_znode *znode;
1496 
1497 	bu->cnt = 0;
1498 	bu->blk_cnt = 0;
1499 	bu->eof = 0;
1500 
1501 	mutex_lock(&c->tnc_mutex);
1502 	/* Find first key */
1503 	err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1504 	if (err < 0)
1505 		goto out;
1506 	if (err) {
1507 		/* Key found */
1508 		len = znode->zbranch[n].len;
1509 		/* The buffer must be big enough for at least 1 node */
1510 		if (len > bu->buf_len) {
1511 			err = -EINVAL;
1512 			goto out;
1513 		}
1514 		/* Add this key */
1515 		bu->zbranch[bu->cnt++] = znode->zbranch[n];
1516 		bu->blk_cnt += 1;
1517 		lnum = znode->zbranch[n].lnum;
1518 		offs = ALIGN(znode->zbranch[n].offs + len, 8);
1519 	}
1520 	while (1) {
1521 		struct ubifs_zbranch *zbr;
1522 		union ubifs_key *key;
1523 		unsigned int next_block;
1524 
1525 		/* Find next key */
1526 		err = tnc_next(c, &znode, &n);
1527 		if (err)
1528 			goto out;
1529 		zbr = &znode->zbranch[n];
1530 		key = &zbr->key;
1531 		/* See if there is another data key for this file */
1532 		if (key_inum(c, key) != key_inum(c, &bu->key) ||
1533 		    key_type(c, key) != UBIFS_DATA_KEY) {
1534 			err = -ENOENT;
1535 			goto out;
1536 		}
1537 		if (lnum < 0) {
1538 			/* First key found */
1539 			lnum = zbr->lnum;
1540 			offs = ALIGN(zbr->offs + zbr->len, 8);
1541 			len = zbr->len;
1542 			if (len > bu->buf_len) {
1543 				err = -EINVAL;
1544 				goto out;
1545 			}
1546 		} else {
1547 			/*
1548 			 * The data nodes must be in consecutive positions in
1549 			 * the same LEB.
1550 			 */
1551 			if (zbr->lnum != lnum || zbr->offs != offs)
1552 				goto out;
1553 			offs += ALIGN(zbr->len, 8);
1554 			len = ALIGN(len, 8) + zbr->len;
1555 			/* Must not exceed buffer length */
1556 			if (len > bu->buf_len)
1557 				goto out;
1558 		}
1559 		/* Allow for holes */
1560 		next_block = key_block(c, key);
1561 		bu->blk_cnt += (next_block - block - 1);
1562 		if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1563 			goto out;
1564 		block = next_block;
1565 		/* Add this key */
1566 		bu->zbranch[bu->cnt++] = *zbr;
1567 		bu->blk_cnt += 1;
1568 		/* See if we have room for more */
1569 		if (bu->cnt >= UBIFS_MAX_BULK_READ)
1570 			goto out;
1571 		if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1572 			goto out;
1573 	}
1574 out:
1575 	if (err == -ENOENT) {
1576 		bu->eof = 1;
1577 		err = 0;
1578 	}
1579 	bu->gc_seq = c->gc_seq;
1580 	mutex_unlock(&c->tnc_mutex);
1581 	if (err)
1582 		return err;
1583 	/*
1584 	 * An enormous hole could cause bulk-read to encompass too many
1585 	 * page cache pages, so limit the number here.
1586 	 */
1587 	if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1588 		bu->blk_cnt = UBIFS_MAX_BULK_READ;
1589 	/*
1590 	 * Ensure that bulk-read covers a whole number of page cache
1591 	 * pages.
1592 	 */
1593 	if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1594 	    !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1595 		return 0;
1596 	if (bu->eof) {
1597 		/* At the end of file we can round up */
1598 		bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1599 		return 0;
1600 	}
1601 	/* Exclude data nodes that do not make up a whole page cache page */
1602 	block = key_block(c, &bu->key) + bu->blk_cnt;
1603 	block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1604 	while (bu->cnt) {
1605 		if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1606 			break;
1607 		bu->cnt -= 1;
1608 	}
1609 	return 0;
1610 }
1611 
1612 /**
1613  * validate_data_node - validate data nodes for bulk-read.
1614  * @c: UBIFS file-system description object
1615  * @buf: buffer containing data node to validate
1616  * @zbr: zbranch of data node to validate
1617  *
1618  * This functions returns %0 on success or a negative error code on failure.
1619  */
1620 static int validate_data_node(struct ubifs_info *c, void *buf,
1621 			      struct ubifs_zbranch *zbr)
1622 {
1623 	union ubifs_key key1;
1624 	struct ubifs_ch *ch = buf;
1625 	int err, len;
1626 
1627 	if (ch->node_type != UBIFS_DATA_NODE) {
1628 		ubifs_err("bad node type (%d but expected %d)",
1629 			  ch->node_type, UBIFS_DATA_NODE);
1630 		goto out_err;
1631 	}
1632 
1633 	err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1634 	if (err) {
1635 		ubifs_err("expected node type %d", UBIFS_DATA_NODE);
1636 		goto out;
1637 	}
1638 
1639 	len = le32_to_cpu(ch->len);
1640 	if (len != zbr->len) {
1641 		ubifs_err("bad node length %d, expected %d", len, zbr->len);
1642 		goto out_err;
1643 	}
1644 
1645 	/* Make sure the key of the read node is correct */
1646 	key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1647 	if (!keys_eq(c, &zbr->key, &key1)) {
1648 		ubifs_err("bad key in node at LEB %d:%d",
1649 			  zbr->lnum, zbr->offs);
1650 		dbg_tnc("looked for key %s found node's key %s",
1651 			DBGKEY(&zbr->key), DBGKEY1(&key1));
1652 		goto out_err;
1653 	}
1654 
1655 	return 0;
1656 
1657 out_err:
1658 	err = -EINVAL;
1659 out:
1660 	ubifs_err("bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1661 	dbg_dump_node(c, buf);
1662 	dbg_dump_stack();
1663 	return err;
1664 }
1665 
1666 /**
1667  * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1668  * @c: UBIFS file-system description object
1669  * @bu: bulk-read parameters and results
1670  *
1671  * This functions reads and validates the data nodes that were identified by the
1672  * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
1673  * -EAGAIN to indicate a race with GC, or another negative error code on
1674  * failure.
1675  */
1676 int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1677 {
1678 	int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
1679 	void *buf;
1680 
1681 	len = bu->zbranch[bu->cnt - 1].offs;
1682 	len += bu->zbranch[bu->cnt - 1].len - offs;
1683 	if (len > bu->buf_len) {
1684 		ubifs_err("buffer too small %d vs %d", bu->buf_len, len);
1685 		return -EINVAL;
1686 	}
1687 
1688 	/* Do the read */
1689 	err = ubi_read(c->ubi, lnum, bu->buf, offs, len);
1690 
1691 	/* Check for a race with GC */
1692 	if (maybe_leb_gced(c, lnum, bu->gc_seq))
1693 		return -EAGAIN;
1694 
1695 	if (err && err != -EBADMSG) {
1696 		ubifs_err("failed to read from LEB %d:%d, error %d",
1697 			  lnum, offs, err);
1698 		dbg_dump_stack();
1699 		dbg_tnc("key %s", DBGKEY(&bu->key));
1700 		return err;
1701 	}
1702 
1703 	/* Validate the nodes read */
1704 	buf = bu->buf;
1705 	for (i = 0; i < bu->cnt; i++) {
1706 		err = validate_data_node(c, buf, &bu->zbranch[i]);
1707 		if (err)
1708 			return err;
1709 		buf = buf + ALIGN(bu->zbranch[i].len, 8);
1710 	}
1711 
1712 	return 0;
1713 }
1714 
1715 /**
1716  * do_lookup_nm- look up a "hashed" node.
1717  * @c: UBIFS file-system description object
1718  * @key: node key to lookup
1719  * @node: the node is returned here
1720  * @nm: node name
1721  *
1722  * This function look up and reads a node which contains name hash in the key.
1723  * Since the hash may have collisions, there may be many nodes with the same
1724  * key, so we have to sequentially look to all of them until the needed one is
1725  * found. This function returns zero in case of success, %-ENOENT if the node
1726  * was not found, and a negative error code in case of failure.
1727  */
1728 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1729 			void *node, const struct qstr *nm)
1730 {
1731 	int found, n, err;
1732 	struct ubifs_znode *znode;
1733 
1734 	dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key));
1735 	mutex_lock(&c->tnc_mutex);
1736 	found = ubifs_lookup_level0(c, key, &znode, &n);
1737 	if (!found) {
1738 		err = -ENOENT;
1739 		goto out_unlock;
1740 	} else if (found < 0) {
1741 		err = found;
1742 		goto out_unlock;
1743 	}
1744 
1745 	ubifs_assert(n >= 0);
1746 
1747 	err = resolve_collision(c, key, &znode, &n, nm);
1748 	dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1749 	if (unlikely(err < 0))
1750 		goto out_unlock;
1751 	if (err == 0) {
1752 		err = -ENOENT;
1753 		goto out_unlock;
1754 	}
1755 
1756 	err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1757 
1758 out_unlock:
1759 	mutex_unlock(&c->tnc_mutex);
1760 	return err;
1761 }
1762 
1763 /**
1764  * ubifs_tnc_lookup_nm - look up a "hashed" node.
1765  * @c: UBIFS file-system description object
1766  * @key: node key to lookup
1767  * @node: the node is returned here
1768  * @nm: node name
1769  *
1770  * This function look up and reads a node which contains name hash in the key.
1771  * Since the hash may have collisions, there may be many nodes with the same
1772  * key, so we have to sequentially look to all of them until the needed one is
1773  * found. This function returns zero in case of success, %-ENOENT if the node
1774  * was not found, and a negative error code in case of failure.
1775  */
1776 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1777 			void *node, const struct qstr *nm)
1778 {
1779 	int err, len;
1780 	const struct ubifs_dent_node *dent = node;
1781 
1782 	/*
1783 	 * We assume that in most of the cases there are no name collisions and
1784 	 * 'ubifs_tnc_lookup()' returns us the right direntry.
1785 	 */
1786 	err = ubifs_tnc_lookup(c, key, node);
1787 	if (err)
1788 		return err;
1789 
1790 	len = le16_to_cpu(dent->nlen);
1791 	if (nm->len == len && !memcmp(dent->name, nm->name, len))
1792 		return 0;
1793 
1794 	/*
1795 	 * Unluckily, there are hash collisions and we have to iterate over
1796 	 * them look at each direntry with colliding name hash sequentially.
1797 	 */
1798 	return do_lookup_nm(c, key, node, nm);
1799 }
1800 
1801 /**
1802  * correct_parent_keys - correct parent znodes' keys.
1803  * @c: UBIFS file-system description object
1804  * @znode: znode to correct parent znodes for
1805  *
1806  * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1807  * zbranch changes, keys of parent znodes have to be corrected. This helper
1808  * function is called in such situations and corrects the keys if needed.
1809  */
1810 static void correct_parent_keys(const struct ubifs_info *c,
1811 				struct ubifs_znode *znode)
1812 {
1813 	union ubifs_key *key, *key1;
1814 
1815 	ubifs_assert(znode->parent);
1816 	ubifs_assert(znode->iip == 0);
1817 
1818 	key = &znode->zbranch[0].key;
1819 	key1 = &znode->parent->zbranch[0].key;
1820 
1821 	while (keys_cmp(c, key, key1) < 0) {
1822 		key_copy(c, key, key1);
1823 		znode = znode->parent;
1824 		znode->alt = 1;
1825 		if (!znode->parent || znode->iip)
1826 			break;
1827 		key1 = &znode->parent->zbranch[0].key;
1828 	}
1829 }
1830 
1831 /**
1832  * insert_zbranch - insert a zbranch into a znode.
1833  * @znode: znode into which to insert
1834  * @zbr: zbranch to insert
1835  * @n: slot number to insert to
1836  *
1837  * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
1838  * znode's array of zbranches and keeps zbranches consolidated, so when a new
1839  * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1840  * slot, zbranches starting from @n have to be moved right.
1841  */
1842 static void insert_zbranch(struct ubifs_znode *znode,
1843 			   const struct ubifs_zbranch *zbr, int n)
1844 {
1845 	int i;
1846 
1847 	ubifs_assert(ubifs_zn_dirty(znode));
1848 
1849 	if (znode->level) {
1850 		for (i = znode->child_cnt; i > n; i--) {
1851 			znode->zbranch[i] = znode->zbranch[i - 1];
1852 			if (znode->zbranch[i].znode)
1853 				znode->zbranch[i].znode->iip = i;
1854 		}
1855 		if (zbr->znode)
1856 			zbr->znode->iip = n;
1857 	} else
1858 		for (i = znode->child_cnt; i > n; i--)
1859 			znode->zbranch[i] = znode->zbranch[i - 1];
1860 
1861 	znode->zbranch[n] = *zbr;
1862 	znode->child_cnt += 1;
1863 
1864 	/*
1865 	 * After inserting at slot zero, the lower bound of the key range of
1866 	 * this znode may have changed. If this znode is subsequently split
1867 	 * then the upper bound of the key range may change, and furthermore
1868 	 * it could change to be lower than the original lower bound. If that
1869 	 * happens, then it will no longer be possible to find this znode in the
1870 	 * TNC using the key from the index node on flash. That is bad because
1871 	 * if it is not found, we will assume it is obsolete and may overwrite
1872 	 * it. Then if there is an unclean unmount, we will start using the
1873 	 * old index which will be broken.
1874 	 *
1875 	 * So we first mark znodes that have insertions at slot zero, and then
1876 	 * if they are split we add their lnum/offs to the old_idx tree.
1877 	 */
1878 	if (n == 0)
1879 		znode->alt = 1;
1880 }
1881 
1882 /**
1883  * tnc_insert - insert a node into TNC.
1884  * @c: UBIFS file-system description object
1885  * @znode: znode to insert into
1886  * @zbr: branch to insert
1887  * @n: slot number to insert new zbranch to
1888  *
1889  * This function inserts a new node described by @zbr into znode @znode. If
1890  * znode does not have a free slot for new zbranch, it is split. Parent znodes
1891  * are splat as well if needed. Returns zero in case of success or a negative
1892  * error code in case of failure.
1893  */
1894 static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1895 		      struct ubifs_zbranch *zbr, int n)
1896 {
1897 	struct ubifs_znode *zn, *zi, *zp;
1898 	int i, keep, move, appending = 0;
1899 	union ubifs_key *key = &zbr->key, *key1;
1900 
1901 	ubifs_assert(n >= 0 && n <= c->fanout);
1902 
1903 	/* Implement naive insert for now */
1904 again:
1905 	zp = znode->parent;
1906 	if (znode->child_cnt < c->fanout) {
1907 		ubifs_assert(n != c->fanout);
1908 		dbg_tnc("inserted at %d level %d, key %s", n, znode->level,
1909 			DBGKEY(key));
1910 
1911 		insert_zbranch(znode, zbr, n);
1912 
1913 		/* Ensure parent's key is correct */
1914 		if (n == 0 && zp && znode->iip == 0)
1915 			correct_parent_keys(c, znode);
1916 
1917 		return 0;
1918 	}
1919 
1920 	/*
1921 	 * Unfortunately, @znode does not have more empty slots and we have to
1922 	 * split it.
1923 	 */
1924 	dbg_tnc("splitting level %d, key %s", znode->level, DBGKEY(key));
1925 
1926 	if (znode->alt)
1927 		/*
1928 		 * We can no longer be sure of finding this znode by key, so we
1929 		 * record it in the old_idx tree.
1930 		 */
1931 		ins_clr_old_idx_znode(c, znode);
1932 
1933 	zn = kzalloc(c->max_znode_sz, GFP_NOFS);
1934 	if (!zn)
1935 		return -ENOMEM;
1936 	zn->parent = zp;
1937 	zn->level = znode->level;
1938 
1939 	/* Decide where to split */
1940 	if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
1941 		/* Try not to split consecutive data keys */
1942 		if (n == c->fanout) {
1943 			key1 = &znode->zbranch[n - 1].key;
1944 			if (key_inum(c, key1) == key_inum(c, key) &&
1945 			    key_type(c, key1) == UBIFS_DATA_KEY)
1946 				appending = 1;
1947 		} else
1948 			goto check_split;
1949 	} else if (appending && n != c->fanout) {
1950 		/* Try not to split consecutive data keys */
1951 		appending = 0;
1952 check_split:
1953 		if (n >= (c->fanout + 1) / 2) {
1954 			key1 = &znode->zbranch[0].key;
1955 			if (key_inum(c, key1) == key_inum(c, key) &&
1956 			    key_type(c, key1) == UBIFS_DATA_KEY) {
1957 				key1 = &znode->zbranch[n].key;
1958 				if (key_inum(c, key1) != key_inum(c, key) ||
1959 				    key_type(c, key1) != UBIFS_DATA_KEY) {
1960 					keep = n;
1961 					move = c->fanout - keep;
1962 					zi = znode;
1963 					goto do_split;
1964 				}
1965 			}
1966 		}
1967 	}
1968 
1969 	if (appending) {
1970 		keep = c->fanout;
1971 		move = 0;
1972 	} else {
1973 		keep = (c->fanout + 1) / 2;
1974 		move = c->fanout - keep;
1975 	}
1976 
1977 	/*
1978 	 * Although we don't at present, we could look at the neighbors and see
1979 	 * if we can move some zbranches there.
1980 	 */
1981 
1982 	if (n < keep) {
1983 		/* Insert into existing znode */
1984 		zi = znode;
1985 		move += 1;
1986 		keep -= 1;
1987 	} else {
1988 		/* Insert into new znode */
1989 		zi = zn;
1990 		n -= keep;
1991 		/* Re-parent */
1992 		if (zn->level != 0)
1993 			zbr->znode->parent = zn;
1994 	}
1995 
1996 do_split:
1997 
1998 	__set_bit(DIRTY_ZNODE, &zn->flags);
1999 	atomic_long_inc(&c->dirty_zn_cnt);
2000 
2001 	zn->child_cnt = move;
2002 	znode->child_cnt = keep;
2003 
2004 	dbg_tnc("moving %d, keeping %d", move, keep);
2005 
2006 	/* Move zbranch */
2007 	for (i = 0; i < move; i++) {
2008 		zn->zbranch[i] = znode->zbranch[keep + i];
2009 		/* Re-parent */
2010 		if (zn->level != 0)
2011 			if (zn->zbranch[i].znode) {
2012 				zn->zbranch[i].znode->parent = zn;
2013 				zn->zbranch[i].znode->iip = i;
2014 			}
2015 	}
2016 
2017 	/* Insert new key and branch */
2018 	dbg_tnc("inserting at %d level %d, key %s", n, zn->level, DBGKEY(key));
2019 
2020 	insert_zbranch(zi, zbr, n);
2021 
2022 	/* Insert new znode (produced by spitting) into the parent */
2023 	if (zp) {
2024 		if (n == 0 && zi == znode && znode->iip == 0)
2025 			correct_parent_keys(c, znode);
2026 
2027 		/* Locate insertion point */
2028 		n = znode->iip + 1;
2029 
2030 		/* Tail recursion */
2031 		zbr->key = zn->zbranch[0].key;
2032 		zbr->znode = zn;
2033 		zbr->lnum = 0;
2034 		zbr->offs = 0;
2035 		zbr->len = 0;
2036 		znode = zp;
2037 
2038 		goto again;
2039 	}
2040 
2041 	/* We have to split root znode */
2042 	dbg_tnc("creating new zroot at level %d", znode->level + 1);
2043 
2044 	zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2045 	if (!zi)
2046 		return -ENOMEM;
2047 
2048 	zi->child_cnt = 2;
2049 	zi->level = znode->level + 1;
2050 
2051 	__set_bit(DIRTY_ZNODE, &zi->flags);
2052 	atomic_long_inc(&c->dirty_zn_cnt);
2053 
2054 	zi->zbranch[0].key = znode->zbranch[0].key;
2055 	zi->zbranch[0].znode = znode;
2056 	zi->zbranch[0].lnum = c->zroot.lnum;
2057 	zi->zbranch[0].offs = c->zroot.offs;
2058 	zi->zbranch[0].len = c->zroot.len;
2059 	zi->zbranch[1].key = zn->zbranch[0].key;
2060 	zi->zbranch[1].znode = zn;
2061 
2062 	c->zroot.lnum = 0;
2063 	c->zroot.offs = 0;
2064 	c->zroot.len = 0;
2065 	c->zroot.znode = zi;
2066 
2067 	zn->parent = zi;
2068 	zn->iip = 1;
2069 	znode->parent = zi;
2070 	znode->iip = 0;
2071 
2072 	return 0;
2073 }
2074 
2075 /**
2076  * ubifs_tnc_add - add a node to TNC.
2077  * @c: UBIFS file-system description object
2078  * @key: key to add
2079  * @lnum: LEB number of node
2080  * @offs: node offset
2081  * @len: node length
2082  *
2083  * This function adds a node with key @key to TNC. The node may be new or it may
2084  * obsolete some existing one. Returns %0 on success or negative error code on
2085  * failure.
2086  */
2087 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2088 		  int offs, int len)
2089 {
2090 	int found, n, err = 0;
2091 	struct ubifs_znode *znode;
2092 
2093 	mutex_lock(&c->tnc_mutex);
2094 	dbg_tnc("%d:%d, len %d, key %s", lnum, offs, len, DBGKEY(key));
2095 	found = lookup_level0_dirty(c, key, &znode, &n);
2096 	if (!found) {
2097 		struct ubifs_zbranch zbr;
2098 
2099 		zbr.znode = NULL;
2100 		zbr.lnum = lnum;
2101 		zbr.offs = offs;
2102 		zbr.len = len;
2103 		key_copy(c, key, &zbr.key);
2104 		err = tnc_insert(c, znode, &zbr, n + 1);
2105 	} else if (found == 1) {
2106 		struct ubifs_zbranch *zbr = &znode->zbranch[n];
2107 
2108 		lnc_free(zbr);
2109 		err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2110 		zbr->lnum = lnum;
2111 		zbr->offs = offs;
2112 		zbr->len = len;
2113 	} else
2114 		err = found;
2115 	if (!err)
2116 		err = dbg_check_tnc(c, 0);
2117 	mutex_unlock(&c->tnc_mutex);
2118 
2119 	return err;
2120 }
2121 
2122 /**
2123  * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2124  * @c: UBIFS file-system description object
2125  * @key: key to add
2126  * @old_lnum: LEB number of old node
2127  * @old_offs: old node offset
2128  * @lnum: LEB number of node
2129  * @offs: node offset
2130  * @len: node length
2131  *
2132  * This function replaces a node with key @key in the TNC only if the old node
2133  * is found.  This function is called by garbage collection when node are moved.
2134  * Returns %0 on success or negative error code on failure.
2135  */
2136 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2137 		      int old_lnum, int old_offs, int lnum, int offs, int len)
2138 {
2139 	int found, n, err = 0;
2140 	struct ubifs_znode *znode;
2141 
2142 	mutex_lock(&c->tnc_mutex);
2143 	dbg_tnc("old LEB %d:%d, new LEB %d:%d, len %d, key %s", old_lnum,
2144 		old_offs, lnum, offs, len, DBGKEY(key));
2145 	found = lookup_level0_dirty(c, key, &znode, &n);
2146 	if (found < 0) {
2147 		err = found;
2148 		goto out_unlock;
2149 	}
2150 
2151 	if (found == 1) {
2152 		struct ubifs_zbranch *zbr = &znode->zbranch[n];
2153 
2154 		found = 0;
2155 		if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2156 			lnc_free(zbr);
2157 			err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2158 			if (err)
2159 				goto out_unlock;
2160 			zbr->lnum = lnum;
2161 			zbr->offs = offs;
2162 			zbr->len = len;
2163 			found = 1;
2164 		} else if (is_hash_key(c, key)) {
2165 			found = resolve_collision_directly(c, key, &znode, &n,
2166 							   old_lnum, old_offs);
2167 			dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2168 				found, znode, n, old_lnum, old_offs);
2169 			if (found < 0) {
2170 				err = found;
2171 				goto out_unlock;
2172 			}
2173 
2174 			if (found) {
2175 				/* Ensure the znode is dirtied */
2176 				if (znode->cnext || !ubifs_zn_dirty(znode)) {
2177 					znode = dirty_cow_bottom_up(c, znode);
2178 					if (IS_ERR(znode)) {
2179 						err = PTR_ERR(znode);
2180 						goto out_unlock;
2181 					}
2182 				}
2183 				zbr = &znode->zbranch[n];
2184 				lnc_free(zbr);
2185 				err = ubifs_add_dirt(c, zbr->lnum,
2186 						     zbr->len);
2187 				if (err)
2188 					goto out_unlock;
2189 				zbr->lnum = lnum;
2190 				zbr->offs = offs;
2191 				zbr->len = len;
2192 			}
2193 		}
2194 	}
2195 
2196 	if (!found)
2197 		err = ubifs_add_dirt(c, lnum, len);
2198 
2199 	if (!err)
2200 		err = dbg_check_tnc(c, 0);
2201 
2202 out_unlock:
2203 	mutex_unlock(&c->tnc_mutex);
2204 	return err;
2205 }
2206 
2207 /**
2208  * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2209  * @c: UBIFS file-system description object
2210  * @key: key to add
2211  * @lnum: LEB number of node
2212  * @offs: node offset
2213  * @len: node length
2214  * @nm: node name
2215  *
2216  * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
2217  * may have collisions, like directory entry keys.
2218  */
2219 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2220 		     int lnum, int offs, int len, const struct qstr *nm)
2221 {
2222 	int found, n, err = 0;
2223 	struct ubifs_znode *znode;
2224 
2225 	mutex_lock(&c->tnc_mutex);
2226 	dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum, offs, nm->len, nm->name,
2227 		DBGKEY(key));
2228 	found = lookup_level0_dirty(c, key, &znode, &n);
2229 	if (found < 0) {
2230 		err = found;
2231 		goto out_unlock;
2232 	}
2233 
2234 	if (found == 1) {
2235 		if (c->replaying)
2236 			found = fallible_resolve_collision(c, key, &znode, &n,
2237 							   nm, 1);
2238 		else
2239 			found = resolve_collision(c, key, &znode, &n, nm);
2240 		dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2241 		if (found < 0) {
2242 			err = found;
2243 			goto out_unlock;
2244 		}
2245 
2246 		/* Ensure the znode is dirtied */
2247 		if (znode->cnext || !ubifs_zn_dirty(znode)) {
2248 			znode = dirty_cow_bottom_up(c, znode);
2249 			if (IS_ERR(znode)) {
2250 				err = PTR_ERR(znode);
2251 				goto out_unlock;
2252 			}
2253 		}
2254 
2255 		if (found == 1) {
2256 			struct ubifs_zbranch *zbr = &znode->zbranch[n];
2257 
2258 			lnc_free(zbr);
2259 			err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2260 			zbr->lnum = lnum;
2261 			zbr->offs = offs;
2262 			zbr->len = len;
2263 			goto out_unlock;
2264 		}
2265 	}
2266 
2267 	if (!found) {
2268 		struct ubifs_zbranch zbr;
2269 
2270 		zbr.znode = NULL;
2271 		zbr.lnum = lnum;
2272 		zbr.offs = offs;
2273 		zbr.len = len;
2274 		key_copy(c, key, &zbr.key);
2275 		err = tnc_insert(c, znode, &zbr, n + 1);
2276 		if (err)
2277 			goto out_unlock;
2278 		if (c->replaying) {
2279 			/*
2280 			 * We did not find it in the index so there may be a
2281 			 * dangling branch still in the index. So we remove it
2282 			 * by passing 'ubifs_tnc_remove_nm()' the same key but
2283 			 * an unmatchable name.
2284 			 */
2285 			struct qstr noname = { .len = 0, .name = "" };
2286 
2287 			err = dbg_check_tnc(c, 0);
2288 			mutex_unlock(&c->tnc_mutex);
2289 			if (err)
2290 				return err;
2291 			return ubifs_tnc_remove_nm(c, key, &noname);
2292 		}
2293 	}
2294 
2295 out_unlock:
2296 	if (!err)
2297 		err = dbg_check_tnc(c, 0);
2298 	mutex_unlock(&c->tnc_mutex);
2299 	return err;
2300 }
2301 
2302 /**
2303  * tnc_delete - delete a znode form TNC.
2304  * @c: UBIFS file-system description object
2305  * @znode: znode to delete from
2306  * @n: zbranch slot number to delete
2307  *
2308  * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2309  * case of success and a negative error code in case of failure.
2310  */
2311 static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2312 {
2313 	struct ubifs_zbranch *zbr;
2314 	struct ubifs_znode *zp;
2315 	int i, err;
2316 
2317 	/* Delete without merge for now */
2318 	ubifs_assert(znode->level == 0);
2319 	ubifs_assert(n >= 0 && n < c->fanout);
2320 	dbg_tnc("deleting %s", DBGKEY(&znode->zbranch[n].key));
2321 
2322 	zbr = &znode->zbranch[n];
2323 	lnc_free(zbr);
2324 
2325 	err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2326 	if (err) {
2327 		dbg_dump_znode(c, znode);
2328 		return err;
2329 	}
2330 
2331 	/* We do not "gap" zbranch slots */
2332 	for (i = n; i < znode->child_cnt - 1; i++)
2333 		znode->zbranch[i] = znode->zbranch[i + 1];
2334 	znode->child_cnt -= 1;
2335 
2336 	if (znode->child_cnt > 0)
2337 		return 0;
2338 
2339 	/*
2340 	 * This was the last zbranch, we have to delete this znode from the
2341 	 * parent.
2342 	 */
2343 
2344 	do {
2345 		ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
2346 		ubifs_assert(ubifs_zn_dirty(znode));
2347 
2348 		zp = znode->parent;
2349 		n = znode->iip;
2350 
2351 		atomic_long_dec(&c->dirty_zn_cnt);
2352 
2353 		err = insert_old_idx_znode(c, znode);
2354 		if (err)
2355 			return err;
2356 
2357 		if (znode->cnext) {
2358 			__set_bit(OBSOLETE_ZNODE, &znode->flags);
2359 			atomic_long_inc(&c->clean_zn_cnt);
2360 			atomic_long_inc(&ubifs_clean_zn_cnt);
2361 		} else
2362 			kfree(znode);
2363 		znode = zp;
2364 	} while (znode->child_cnt == 1); /* while removing last child */
2365 
2366 	/* Remove from znode, entry n - 1 */
2367 	znode->child_cnt -= 1;
2368 	ubifs_assert(znode->level != 0);
2369 	for (i = n; i < znode->child_cnt; i++) {
2370 		znode->zbranch[i] = znode->zbranch[i + 1];
2371 		if (znode->zbranch[i].znode)
2372 			znode->zbranch[i].znode->iip = i;
2373 	}
2374 
2375 	/*
2376 	 * If this is the root and it has only 1 child then
2377 	 * collapse the tree.
2378 	 */
2379 	if (!znode->parent) {
2380 		while (znode->child_cnt == 1 && znode->level != 0) {
2381 			zp = znode;
2382 			zbr = &znode->zbranch[0];
2383 			znode = get_znode(c, znode, 0);
2384 			if (IS_ERR(znode))
2385 				return PTR_ERR(znode);
2386 			znode = dirty_cow_znode(c, zbr);
2387 			if (IS_ERR(znode))
2388 				return PTR_ERR(znode);
2389 			znode->parent = NULL;
2390 			znode->iip = 0;
2391 			if (c->zroot.len) {
2392 				err = insert_old_idx(c, c->zroot.lnum,
2393 						     c->zroot.offs);
2394 				if (err)
2395 					return err;
2396 			}
2397 			c->zroot.lnum = zbr->lnum;
2398 			c->zroot.offs = zbr->offs;
2399 			c->zroot.len = zbr->len;
2400 			c->zroot.znode = znode;
2401 			ubifs_assert(!test_bit(OBSOLETE_ZNODE,
2402 				     &zp->flags));
2403 			ubifs_assert(test_bit(DIRTY_ZNODE, &zp->flags));
2404 			atomic_long_dec(&c->dirty_zn_cnt);
2405 
2406 			if (zp->cnext) {
2407 				__set_bit(OBSOLETE_ZNODE, &zp->flags);
2408 				atomic_long_inc(&c->clean_zn_cnt);
2409 				atomic_long_inc(&ubifs_clean_zn_cnt);
2410 			} else
2411 				kfree(zp);
2412 		}
2413 	}
2414 
2415 	return 0;
2416 }
2417 
2418 /**
2419  * ubifs_tnc_remove - remove an index entry of a node.
2420  * @c: UBIFS file-system description object
2421  * @key: key of node
2422  *
2423  * Returns %0 on success or negative error code on failure.
2424  */
2425 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2426 {
2427 	int found, n, err = 0;
2428 	struct ubifs_znode *znode;
2429 
2430 	mutex_lock(&c->tnc_mutex);
2431 	dbg_tnc("key %s", DBGKEY(key));
2432 	found = lookup_level0_dirty(c, key, &znode, &n);
2433 	if (found < 0) {
2434 		err = found;
2435 		goto out_unlock;
2436 	}
2437 	if (found == 1)
2438 		err = tnc_delete(c, znode, n);
2439 	if (!err)
2440 		err = dbg_check_tnc(c, 0);
2441 
2442 out_unlock:
2443 	mutex_unlock(&c->tnc_mutex);
2444 	return err;
2445 }
2446 
2447 /**
2448  * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2449  * @c: UBIFS file-system description object
2450  * @key: key of node
2451  * @nm: directory entry name
2452  *
2453  * Returns %0 on success or negative error code on failure.
2454  */
2455 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2456 			const struct qstr *nm)
2457 {
2458 	int n, err;
2459 	struct ubifs_znode *znode;
2460 
2461 	mutex_lock(&c->tnc_mutex);
2462 	dbg_tnc("%.*s, key %s", nm->len, nm->name, DBGKEY(key));
2463 	err = lookup_level0_dirty(c, key, &znode, &n);
2464 	if (err < 0)
2465 		goto out_unlock;
2466 
2467 	if (err) {
2468 		if (c->replaying)
2469 			err = fallible_resolve_collision(c, key, &znode, &n,
2470 							 nm, 0);
2471 		else
2472 			err = resolve_collision(c, key, &znode, &n, nm);
2473 		dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2474 		if (err < 0)
2475 			goto out_unlock;
2476 		if (err) {
2477 			/* Ensure the znode is dirtied */
2478 			if (znode->cnext || !ubifs_zn_dirty(znode)) {
2479 				    znode = dirty_cow_bottom_up(c, znode);
2480 				    if (IS_ERR(znode)) {
2481 					    err = PTR_ERR(znode);
2482 					    goto out_unlock;
2483 				    }
2484 			}
2485 			err = tnc_delete(c, znode, n);
2486 		}
2487 	}
2488 
2489 out_unlock:
2490 	if (!err)
2491 		err = dbg_check_tnc(c, 0);
2492 	mutex_unlock(&c->tnc_mutex);
2493 	return err;
2494 }
2495 
2496 /**
2497  * key_in_range - determine if a key falls within a range of keys.
2498  * @c: UBIFS file-system description object
2499  * @key: key to check
2500  * @from_key: lowest key in range
2501  * @to_key: highest key in range
2502  *
2503  * This function returns %1 if the key is in range and %0 otherwise.
2504  */
2505 static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2506 			union ubifs_key *from_key, union ubifs_key *to_key)
2507 {
2508 	if (keys_cmp(c, key, from_key) < 0)
2509 		return 0;
2510 	if (keys_cmp(c, key, to_key) > 0)
2511 		return 0;
2512 	return 1;
2513 }
2514 
2515 /**
2516  * ubifs_tnc_remove_range - remove index entries in range.
2517  * @c: UBIFS file-system description object
2518  * @from_key: lowest key to remove
2519  * @to_key: highest key to remove
2520  *
2521  * This function removes index entries starting at @from_key and ending at
2522  * @to_key.  This function returns zero in case of success and a negative error
2523  * code in case of failure.
2524  */
2525 int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2526 			   union ubifs_key *to_key)
2527 {
2528 	int i, n, k, err = 0;
2529 	struct ubifs_znode *znode;
2530 	union ubifs_key *key;
2531 
2532 	mutex_lock(&c->tnc_mutex);
2533 	while (1) {
2534 		/* Find first level 0 znode that contains keys to remove */
2535 		err = ubifs_lookup_level0(c, from_key, &znode, &n);
2536 		if (err < 0)
2537 			goto out_unlock;
2538 
2539 		if (err)
2540 			key = from_key;
2541 		else {
2542 			err = tnc_next(c, &znode, &n);
2543 			if (err == -ENOENT) {
2544 				err = 0;
2545 				goto out_unlock;
2546 			}
2547 			if (err < 0)
2548 				goto out_unlock;
2549 			key = &znode->zbranch[n].key;
2550 			if (!key_in_range(c, key, from_key, to_key)) {
2551 				err = 0;
2552 				goto out_unlock;
2553 			}
2554 		}
2555 
2556 		/* Ensure the znode is dirtied */
2557 		if (znode->cnext || !ubifs_zn_dirty(znode)) {
2558 			znode = dirty_cow_bottom_up(c, znode);
2559 			if (IS_ERR(znode)) {
2560 				err = PTR_ERR(znode);
2561 				goto out_unlock;
2562 			}
2563 		}
2564 
2565 		/* Remove all keys in range except the first */
2566 		for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2567 			key = &znode->zbranch[i].key;
2568 			if (!key_in_range(c, key, from_key, to_key))
2569 				break;
2570 			lnc_free(&znode->zbranch[i]);
2571 			err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2572 					     znode->zbranch[i].len);
2573 			if (err) {
2574 				dbg_dump_znode(c, znode);
2575 				goto out_unlock;
2576 			}
2577 			dbg_tnc("removing %s", DBGKEY(key));
2578 		}
2579 		if (k) {
2580 			for (i = n + 1 + k; i < znode->child_cnt; i++)
2581 				znode->zbranch[i - k] = znode->zbranch[i];
2582 			znode->child_cnt -= k;
2583 		}
2584 
2585 		/* Now delete the first */
2586 		err = tnc_delete(c, znode, n);
2587 		if (err)
2588 			goto out_unlock;
2589 	}
2590 
2591 out_unlock:
2592 	if (!err)
2593 		err = dbg_check_tnc(c, 0);
2594 	mutex_unlock(&c->tnc_mutex);
2595 	return err;
2596 }
2597 
2598 /**
2599  * ubifs_tnc_remove_ino - remove an inode from TNC.
2600  * @c: UBIFS file-system description object
2601  * @inum: inode number to remove
2602  *
2603  * This function remove inode @inum and all the extended attributes associated
2604  * with the anode from TNC and returns zero in case of success or a negative
2605  * error code in case of failure.
2606  */
2607 int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2608 {
2609 	union ubifs_key key1, key2;
2610 	struct ubifs_dent_node *xent, *pxent = NULL;
2611 	struct qstr nm = { .name = NULL };
2612 
2613 	dbg_tnc("ino %lu", (unsigned long)inum);
2614 
2615 	/*
2616 	 * Walk all extended attribute entries and remove them together with
2617 	 * corresponding extended attribute inodes.
2618 	 */
2619 	lowest_xent_key(c, &key1, inum);
2620 	while (1) {
2621 		ino_t xattr_inum;
2622 		int err;
2623 
2624 		xent = ubifs_tnc_next_ent(c, &key1, &nm);
2625 		if (IS_ERR(xent)) {
2626 			err = PTR_ERR(xent);
2627 			if (err == -ENOENT)
2628 				break;
2629 			return err;
2630 		}
2631 
2632 		xattr_inum = le64_to_cpu(xent->inum);
2633 		dbg_tnc("xent '%s', ino %lu", xent->name,
2634 			(unsigned long)xattr_inum);
2635 
2636 		nm.name = (char *)xent->name;
2637 		nm.len = le16_to_cpu(xent->nlen);
2638 		err = ubifs_tnc_remove_nm(c, &key1, &nm);
2639 		if (err) {
2640 			kfree(xent);
2641 			return err;
2642 		}
2643 
2644 		lowest_ino_key(c, &key1, xattr_inum);
2645 		highest_ino_key(c, &key2, xattr_inum);
2646 		err = ubifs_tnc_remove_range(c, &key1, &key2);
2647 		if (err) {
2648 			kfree(xent);
2649 			return err;
2650 		}
2651 
2652 		kfree(pxent);
2653 		pxent = xent;
2654 		key_read(c, &xent->key, &key1);
2655 	}
2656 
2657 	kfree(pxent);
2658 	lowest_ino_key(c, &key1, inum);
2659 	highest_ino_key(c, &key2, inum);
2660 
2661 	return ubifs_tnc_remove_range(c, &key1, &key2);
2662 }
2663 
2664 /**
2665  * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2666  * @c: UBIFS file-system description object
2667  * @key: key of last entry
2668  * @nm: name of last entry found or %NULL
2669  *
2670  * This function finds and reads the next directory or extended attribute entry
2671  * after the given key (@key) if there is one. @nm is used to resolve
2672  * collisions.
2673  *
2674  * If the name of the current entry is not known and only the key is known,
2675  * @nm->name has to be %NULL. In this case the semantics of this function is a
2676  * little bit different and it returns the entry corresponding to this key, not
2677  * the next one. If the key was not found, the closest "right" entry is
2678  * returned.
2679  *
2680  * If the fist entry has to be found, @key has to contain the lowest possible
2681  * key value for this inode and @name has to be %NULL.
2682  *
2683  * This function returns the found directory or extended attribute entry node
2684  * in case of success, %-ENOENT is returned if no entry was found, and a
2685  * negative error code is returned in case of failure.
2686  */
2687 struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2688 					   union ubifs_key *key,
2689 					   const struct qstr *nm)
2690 {
2691 	int n, err, type = key_type(c, key);
2692 	struct ubifs_znode *znode;
2693 	struct ubifs_dent_node *dent;
2694 	struct ubifs_zbranch *zbr;
2695 	union ubifs_key *dkey;
2696 
2697 	dbg_tnc("%s %s", nm->name ? (char *)nm->name : "(lowest)", DBGKEY(key));
2698 	ubifs_assert(is_hash_key(c, key));
2699 
2700 	mutex_lock(&c->tnc_mutex);
2701 	err = ubifs_lookup_level0(c, key, &znode, &n);
2702 	if (unlikely(err < 0))
2703 		goto out_unlock;
2704 
2705 	if (nm->name) {
2706 		if (err) {
2707 			/* Handle collisions */
2708 			err = resolve_collision(c, key, &znode, &n, nm);
2709 			dbg_tnc("rc returned %d, znode %p, n %d",
2710 				err, znode, n);
2711 			if (unlikely(err < 0))
2712 				goto out_unlock;
2713 		}
2714 
2715 		/* Now find next entry */
2716 		err = tnc_next(c, &znode, &n);
2717 		if (unlikely(err))
2718 			goto out_unlock;
2719 	} else {
2720 		/*
2721 		 * The full name of the entry was not given, in which case the
2722 		 * behavior of this function is a little different and it
2723 		 * returns current entry, not the next one.
2724 		 */
2725 		if (!err) {
2726 			/*
2727 			 * However, the given key does not exist in the TNC
2728 			 * tree and @znode/@n variables contain the closest
2729 			 * "preceding" element. Switch to the next one.
2730 			 */
2731 			err = tnc_next(c, &znode, &n);
2732 			if (err)
2733 				goto out_unlock;
2734 		}
2735 	}
2736 
2737 	zbr = &znode->zbranch[n];
2738 	dent = kmalloc(zbr->len, GFP_NOFS);
2739 	if (unlikely(!dent)) {
2740 		err = -ENOMEM;
2741 		goto out_unlock;
2742 	}
2743 
2744 	/*
2745 	 * The above 'tnc_next()' call could lead us to the next inode, check
2746 	 * this.
2747 	 */
2748 	dkey = &zbr->key;
2749 	if (key_inum(c, dkey) != key_inum(c, key) ||
2750 	    key_type(c, dkey) != type) {
2751 		err = -ENOENT;
2752 		goto out_free;
2753 	}
2754 
2755 	err = tnc_read_node_nm(c, zbr, dent);
2756 	if (unlikely(err))
2757 		goto out_free;
2758 
2759 	mutex_unlock(&c->tnc_mutex);
2760 	return dent;
2761 
2762 out_free:
2763 	kfree(dent);
2764 out_unlock:
2765 	mutex_unlock(&c->tnc_mutex);
2766 	return ERR_PTR(err);
2767 }
2768