2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements TNC (Tree Node Cache) which caches indexing nodes of
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
33 #include <linux/crc32.h>
34 #include <linux/slab.h>
37 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
38 int len, int lnum, int offs);
39 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
40 struct ubifs_zbranch *zbr, void *node);
43 * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
44 * @NAME_LESS: name corresponding to the first argument is less than second
45 * @NAME_MATCHES: names match
46 * @NAME_GREATER: name corresponding to the second argument is greater than
48 * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
50 * These constants were introduce to improve readability.
60 * insert_old_idx - record an index node obsoleted since the last commit start.
61 * @c: UBIFS file-system description object
62 * @lnum: LEB number of obsoleted index node
63 * @offs: offset of obsoleted index node
65 * Returns %0 on success, and a negative error code on failure.
67 * For recovery, there must always be a complete intact version of the index on
68 * flash at all times. That is called the "old index". It is the index as at the
69 * time of the last successful commit. Many of the index nodes in the old index
70 * may be dirty, but they must not be erased until the next successful commit
71 * (at which point that index becomes the old index).
73 * That means that the garbage collection and the in-the-gaps method of
74 * committing must be able to determine if an index node is in the old index.
75 * Most of the old index nodes can be found by looking up the TNC using the
76 * 'lookup_znode()' function. However, some of the old index nodes may have
77 * been deleted from the current index or may have been changed so much that
78 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
79 * That is what this function does. The RB-tree is ordered by LEB number and
80 * offset because they uniquely identify the old index node.
82 static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
84 struct ubifs_old_idx *old_idx, *o;
85 struct rb_node **p, *parent = NULL;
87 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
88 if (unlikely(!old_idx))
93 p = &c->old_idx.rb_node;
96 o = rb_entry(parent, struct ubifs_old_idx, rb);
99 else if (lnum > o->lnum)
101 else if (offs < o->offs)
103 else if (offs > o->offs)
106 ubifs_err("old idx added twice!");
111 rb_link_node(&old_idx->rb, parent, p);
112 rb_insert_color(&old_idx->rb, &c->old_idx);
117 * insert_old_idx_znode - record a znode obsoleted since last commit start.
118 * @c: UBIFS file-system description object
119 * @znode: znode of obsoleted index node
121 * Returns %0 on success, and a negative error code on failure.
123 int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
126 struct ubifs_zbranch *zbr;
128 zbr = &znode->parent->zbranch[znode->iip];
130 return insert_old_idx(c, zbr->lnum, zbr->offs);
133 return insert_old_idx(c, c->zroot.lnum,
139 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
140 * @c: UBIFS file-system description object
141 * @znode: znode of obsoleted index node
143 * Returns %0 on success, and a negative error code on failure.
145 static int ins_clr_old_idx_znode(struct ubifs_info *c,
146 struct ubifs_znode *znode)
151 struct ubifs_zbranch *zbr;
153 zbr = &znode->parent->zbranch[znode->iip];
155 err = insert_old_idx(c, zbr->lnum, zbr->offs);
164 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
175 * destroy_old_idx - destroy the old_idx RB-tree.
176 * @c: UBIFS file-system description object
178 * During start commit, the old_idx RB-tree is used to avoid overwriting index
179 * nodes that were in the index last commit but have since been deleted. This
180 * is necessary for recovery i.e. the old index must be kept intact until the
181 * new index is successfully written. The old-idx RB-tree is used for the
182 * in-the-gaps method of writing index nodes and is destroyed every commit.
184 void destroy_old_idx(struct ubifs_info *c)
186 struct rb_node *this = c->old_idx.rb_node;
187 struct ubifs_old_idx *old_idx;
191 this = this->rb_left;
193 } else if (this->rb_right) {
194 this = this->rb_right;
197 old_idx = rb_entry(this, struct ubifs_old_idx, rb);
198 this = rb_parent(this);
200 if (this->rb_left == &old_idx->rb)
201 this->rb_left = NULL;
203 this->rb_right = NULL;
207 c->old_idx = RB_ROOT;
211 * copy_znode - copy a dirty znode.
212 * @c: UBIFS file-system description object
213 * @znode: znode to copy
215 * A dirty znode being committed may not be changed, so it is copied.
217 static struct ubifs_znode *copy_znode(struct ubifs_info *c,
218 struct ubifs_znode *znode)
220 struct ubifs_znode *zn;
222 zn = kmalloc(c->max_znode_sz, GFP_NOFS);
224 return ERR_PTR(-ENOMEM);
226 memcpy(zn, znode, c->max_znode_sz);
228 __set_bit(DIRTY_ZNODE, &zn->flags);
229 __clear_bit(COW_ZNODE, &zn->flags);
231 ubifs_assert(!ubifs_zn_obsolete(znode));
232 __set_bit(OBSOLETE_ZNODE, &znode->flags);
234 if (znode->level != 0) {
236 const int n = zn->child_cnt;
238 /* The children now have new parent */
239 for (i = 0; i < n; i++) {
240 struct ubifs_zbranch *zbr = &zn->zbranch[i];
243 zbr->znode->parent = zn;
247 atomic_long_inc(&c->dirty_zn_cnt);
252 * add_idx_dirt - add dirt due to a dirty znode.
253 * @c: UBIFS file-system description object
254 * @lnum: LEB number of index node
255 * @dirt: size of index node
257 * This function updates lprops dirty space and the new size of the index.
259 static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
261 c->calc_idx_sz -= ALIGN(dirt, 8);
262 return ubifs_add_dirt(c, lnum, dirt);
266 * dirty_cow_znode - ensure a znode is not being committed.
267 * @c: UBIFS file-system description object
268 * @zbr: branch of znode to check
270 * Returns dirtied znode on success or negative error code on failure.
272 static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
273 struct ubifs_zbranch *zbr)
275 struct ubifs_znode *znode = zbr->znode;
276 struct ubifs_znode *zn;
279 if (!ubifs_zn_cow(znode)) {
280 /* znode is not being committed */
281 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
282 atomic_long_inc(&c->dirty_zn_cnt);
283 atomic_long_dec(&c->clean_zn_cnt);
284 atomic_long_dec(&ubifs_clean_zn_cnt);
285 err = add_idx_dirt(c, zbr->lnum, zbr->len);
292 zn = copy_znode(c, znode);
297 err = insert_old_idx(c, zbr->lnum, zbr->offs);
300 err = add_idx_dirt(c, zbr->lnum, zbr->len);
315 * lnc_add - add a leaf node to the leaf node cache.
316 * @c: UBIFS file-system description object
317 * @zbr: zbranch of leaf node
320 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
321 * purpose of the leaf node cache is to save re-reading the same leaf node over
322 * and over again. Most things are cached by VFS, however the file system must
323 * cache directory entries for readdir and for resolving hash collisions. The
324 * present implementation of the leaf node cache is extremely simple, and
325 * allows for error returns that are not used but that may be needed if a more
326 * complex implementation is created.
328 * Note, this function does not add the @node object to LNC directly, but
329 * allocates a copy of the object and adds the copy to LNC. The reason for this
330 * is that @node has been allocated outside of the TNC subsystem and will be
331 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
332 * may be changed at any time, e.g. freed by the shrinker.
334 static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
339 const struct ubifs_dent_node *dent = node;
341 ubifs_assert(!zbr->leaf);
342 ubifs_assert(zbr->len != 0);
343 ubifs_assert(is_hash_key(c, &zbr->key));
345 err = ubifs_validate_entry(c, dent);
348 ubifs_dump_node(c, dent);
352 lnc_node = kmemdup(node, zbr->len, GFP_NOFS);
354 /* We don't have to have the cache, so no error */
357 zbr->leaf = lnc_node;
362 * lnc_add_directly - add a leaf node to the leaf-node-cache.
363 * @c: UBIFS file-system description object
364 * @zbr: zbranch of leaf node
367 * This function is similar to 'lnc_add()', but it does not create a copy of
368 * @node but inserts @node to TNC directly.
370 static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
375 ubifs_assert(!zbr->leaf);
376 ubifs_assert(zbr->len != 0);
378 err = ubifs_validate_entry(c, node);
381 ubifs_dump_node(c, node);
390 * lnc_free - remove a leaf node from the leaf node cache.
391 * @zbr: zbranch of leaf node
394 static void lnc_free(struct ubifs_zbranch *zbr)
403 * tnc_read_node_nm - read a "hashed" leaf node.
404 * @c: UBIFS file-system description object
405 * @zbr: key and position of the node
406 * @node: node is returned here
408 * This function reads a "hashed" node defined by @zbr from the leaf node cache
409 * (in it is there) or from the hash media, in which case the node is also
410 * added to LNC. Returns zero in case of success or a negative negative error
411 * code in case of failure.
413 static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
418 ubifs_assert(is_hash_key(c, &zbr->key));
421 /* Read from the leaf node cache */
422 ubifs_assert(zbr->len != 0);
423 memcpy(node, zbr->leaf, zbr->len);
428 err = fallible_read_node(c, &zbr->key, zbr, node);
430 * When the node was not found, return -ENOENT, 0 otherwise.
431 * Negative return codes stay as-is.
438 err = ubifs_tnc_read_node(c, zbr, node);
443 /* Add the node to the leaf node cache */
444 err = lnc_add(c, zbr, node);
449 * try_read_node - read a node if it is a node.
450 * @c: UBIFS file-system description object
451 * @buf: buffer to read to
453 * @len: node length (not aligned)
454 * @lnum: LEB number of node to read
455 * @offs: offset of node to read
457 * This function tries to read a node of known type and length, checks it and
458 * stores it in @buf. This function returns %1 if a node is present and %0 if
459 * a node is not present. A negative error code is returned for I/O errors.
460 * This function performs that same function as ubifs_read_node except that
461 * it does not require that there is actually a node present and instead
462 * the return code indicates if a node was read.
464 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
465 * is true (it is controlled by corresponding mount option). However, if
466 * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
467 * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
468 * because during mounting or re-mounting from R/O mode to R/W mode we may read
469 * journal nodes (when replying the journal or doing the recovery) and the
470 * journal nodes may potentially be corrupted, so checking is required.
472 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
473 int len, int lnum, int offs)
476 struct ubifs_ch *ch = buf;
477 uint32_t crc, node_crc;
479 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
481 err = ubifs_leb_read(c, lnum, buf, offs, len, 1);
483 ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
484 type, lnum, offs, err);
488 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
491 if (ch->node_type != type)
494 node_len = le32_to_cpu(ch->len);
498 if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
502 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
503 node_crc = le32_to_cpu(ch->crc);
511 * fallible_read_node - try to read a leaf node.
512 * @c: UBIFS file-system description object
513 * @key: key of node to read
514 * @zbr: position of node
515 * @node: node returned
517 * This function tries to read a node and returns %1 if the node is read, %0
518 * if the node is not present, and a negative error code in the case of error.
520 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
521 struct ubifs_zbranch *zbr, void *node)
525 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
527 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
530 union ubifs_key node_key;
531 struct ubifs_dent_node *dent = node;
533 /* All nodes have key in the same place */
534 key_read(c, &dent->key, &node_key);
535 if (keys_cmp(c, key, &node_key) != 0)
538 if (ret == 0 && c->replaying)
539 dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ",
540 zbr->lnum, zbr->offs, zbr->len);
545 * matches_name - determine if a direntry or xattr entry matches a given name.
546 * @c: UBIFS file-system description object
547 * @zbr: zbranch of dent
550 * This function checks if xentry/direntry referred by zbranch @zbr matches name
551 * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
552 * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
553 * of failure, a negative error code is returned.
555 static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
556 const struct qstr *nm)
558 struct ubifs_dent_node *dent;
561 /* If possible, match against the dent in the leaf node cache */
563 dent = kmalloc(zbr->len, GFP_NOFS);
567 err = ubifs_tnc_read_node(c, zbr, dent);
571 /* Add the node to the leaf node cache */
572 err = lnc_add_directly(c, zbr, dent);
578 nlen = le16_to_cpu(dent->nlen);
579 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
583 else if (nlen < nm->len)
598 * get_znode - get a TNC znode that may not be loaded yet.
599 * @c: UBIFS file-system description object
600 * @znode: parent znode
601 * @n: znode branch slot number
603 * This function returns the znode or a negative error code.
605 static struct ubifs_znode *get_znode(struct ubifs_info *c,
606 struct ubifs_znode *znode, int n)
608 struct ubifs_zbranch *zbr;
610 zbr = &znode->zbranch[n];
614 znode = ubifs_load_znode(c, zbr, znode, n);
619 * tnc_next - find next TNC entry.
620 * @c: UBIFS file-system description object
621 * @zn: znode is passed and returned here
622 * @n: znode branch slot number is passed and returned here
624 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
625 * no next entry, or a negative error code otherwise.
627 static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
629 struct ubifs_znode *znode = *zn;
633 if (nn < znode->child_cnt) {
638 struct ubifs_znode *zp;
645 if (nn < znode->child_cnt) {
646 znode = get_znode(c, znode, nn);
648 return PTR_ERR(znode);
649 while (znode->level != 0) {
650 znode = get_znode(c, znode, 0);
652 return PTR_ERR(znode);
664 * tnc_prev - find previous TNC entry.
665 * @c: UBIFS file-system description object
666 * @zn: znode is returned here
667 * @n: znode branch slot number is passed and returned here
669 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
670 * there is no next entry, or a negative error code otherwise.
672 static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
674 struct ubifs_znode *znode = *zn;
682 struct ubifs_znode *zp;
690 znode = get_znode(c, znode, nn);
692 return PTR_ERR(znode);
693 while (znode->level != 0) {
694 nn = znode->child_cnt - 1;
695 znode = get_znode(c, znode, nn);
697 return PTR_ERR(znode);
699 nn = znode->child_cnt - 1;
709 * resolve_collision - resolve a collision.
710 * @c: UBIFS file-system description object
711 * @key: key of a directory or extended attribute entry
712 * @zn: znode is returned here
713 * @n: zbranch number is passed and returned here
714 * @nm: name of the entry
716 * This function is called for "hashed" keys to make sure that the found key
717 * really corresponds to the looked up node (directory or extended attribute
718 * entry). It returns %1 and sets @zn and @n if the collision is resolved.
719 * %0 is returned if @nm is not found and @zn and @n are set to the previous
720 * entry, i.e. to the entry after which @nm could follow if it were in TNC.
721 * This means that @n may be set to %-1 if the leftmost key in @zn is the
722 * previous one. A negative error code is returned on failures.
724 static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
725 struct ubifs_znode **zn, int *n,
726 const struct qstr *nm)
730 err = matches_name(c, &(*zn)->zbranch[*n], nm);
731 if (unlikely(err < 0))
733 if (err == NAME_MATCHES)
736 if (err == NAME_GREATER) {
739 err = tnc_prev(c, zn, n);
740 if (err == -ENOENT) {
741 ubifs_assert(*n == 0);
747 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
749 * We have found the branch after which we would
750 * like to insert, but inserting in this znode
751 * may still be wrong. Consider the following 3
752 * znodes, in the case where we are resolving a
753 * collision with Key2.
756 * ----------------------
757 * level 1 | Key0 | Key1 |
758 * -----------------------
760 * znode za | | znode zb
761 * ------------ ------------
762 * level 0 | Key0 | | Key2 |
763 * ------------ ------------
765 * The lookup finds Key2 in znode zb. Lets say
766 * there is no match and the name is greater so
767 * we look left. When we find Key0, we end up
768 * here. If we return now, we will insert into
769 * znode za at slot n = 1. But that is invalid
770 * according to the parent's keys. Key2 must
771 * be inserted into znode zb.
773 * Note, this problem is not relevant for the
774 * case when we go right, because
775 * 'tnc_insert()' would correct the parent key.
777 if (*n == (*zn)->child_cnt - 1) {
778 err = tnc_next(c, zn, n);
780 /* Should be impossible */
786 ubifs_assert(*n == 0);
791 err = matches_name(c, &(*zn)->zbranch[*n], nm);
794 if (err == NAME_LESS)
796 if (err == NAME_MATCHES)
798 ubifs_assert(err == NAME_GREATER);
802 struct ubifs_znode *znode = *zn;
806 err = tnc_next(c, &znode, &nn);
811 if (keys_cmp(c, &znode->zbranch[nn].key, key))
813 err = matches_name(c, &znode->zbranch[nn], nm);
816 if (err == NAME_GREATER)
820 if (err == NAME_MATCHES)
822 ubifs_assert(err == NAME_LESS);
828 * fallible_matches_name - determine if a dent matches a given name.
829 * @c: UBIFS file-system description object
830 * @zbr: zbranch of dent
833 * This is a "fallible" version of 'matches_name()' function which does not
834 * panic if the direntry/xentry referred by @zbr does not exist on the media.
836 * This function checks if xentry/direntry referred by zbranch @zbr matches name
837 * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
838 * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
839 * if xentry/direntry referred by @zbr does not exist on the media. A negative
840 * error code is returned in case of failure.
842 static int fallible_matches_name(struct ubifs_info *c,
843 struct ubifs_zbranch *zbr,
844 const struct qstr *nm)
846 struct ubifs_dent_node *dent;
849 /* If possible, match against the dent in the leaf node cache */
851 dent = kmalloc(zbr->len, GFP_NOFS);
855 err = fallible_read_node(c, &zbr->key, zbr, dent);
859 /* The node was not present */
863 ubifs_assert(err == 1);
865 err = lnc_add_directly(c, zbr, dent);
871 nlen = le16_to_cpu(dent->nlen);
872 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
876 else if (nlen < nm->len)
891 * fallible_resolve_collision - resolve a collision even if nodes are missing.
892 * @c: UBIFS file-system description object
894 * @zn: znode is returned here
895 * @n: branch number is passed and returned here
896 * @nm: name of directory entry
897 * @adding: indicates caller is adding a key to the TNC
899 * This is a "fallible" version of the 'resolve_collision()' function which
900 * does not panic if one of the nodes referred to by TNC does not exist on the
901 * media. This may happen when replaying the journal if a deleted node was
902 * Garbage-collected and the commit was not done. A branch that refers to a node
903 * that is not present is called a dangling branch. The following are the return
904 * codes for this function:
905 * o if @nm was found, %1 is returned and @zn and @n are set to the found
907 * o if we are @adding and @nm was not found, %0 is returned;
908 * o if we are not @adding and @nm was not found, but a dangling branch was
909 * found, then %1 is returned and @zn and @n are set to the dangling branch;
910 * o a negative error code is returned in case of failure.
912 static int fallible_resolve_collision(struct ubifs_info *c,
913 const union ubifs_key *key,
914 struct ubifs_znode **zn, int *n,
915 const struct qstr *nm, int adding)
917 struct ubifs_znode *o_znode = NULL, *znode = *zn;
918 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
920 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
921 if (unlikely(cmp < 0))
923 if (cmp == NAME_MATCHES)
925 if (cmp == NOT_ON_MEDIA) {
929 * We are unlucky and hit a dangling branch straight away.
930 * Now we do not really know where to go to find the needed
931 * branch - to the left or to the right. Well, let's try left.
935 unsure = 1; /* Remove a dangling branch wherever it is */
937 if (cmp == NAME_GREATER || unsure) {
940 err = tnc_prev(c, zn, n);
941 if (err == -ENOENT) {
942 ubifs_assert(*n == 0);
948 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
949 /* See comments in 'resolve_collision()' */
950 if (*n == (*zn)->child_cnt - 1) {
951 err = tnc_next(c, zn, n);
953 /* Should be impossible */
959 ubifs_assert(*n == 0);
964 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
967 if (err == NAME_MATCHES)
969 if (err == NOT_ON_MEDIA) {
976 if (err == NAME_LESS)
983 if (cmp == NAME_LESS || unsure) {
988 err = tnc_next(c, &znode, &nn);
993 if (keys_cmp(c, &znode->zbranch[nn].key, key))
995 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
998 if (err == NAME_GREATER)
1002 if (err == NAME_MATCHES)
1004 if (err == NOT_ON_MEDIA) {
1011 /* Never match a dangling branch when adding */
1012 if (adding || !o_znode)
1015 dbg_mntk(key, "dangling match LEB %d:%d len %d key ",
1016 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
1017 o_znode->zbranch[o_n].len);
1024 * matches_position - determine if a zbranch matches a given position.
1025 * @zbr: zbranch of dent
1026 * @lnum: LEB number of dent to match
1027 * @offs: offset of dent to match
1029 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1031 static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1033 if (zbr->lnum == lnum && zbr->offs == offs)
1040 * resolve_collision_directly - resolve a collision directly.
1041 * @c: UBIFS file-system description object
1042 * @key: key of directory entry
1043 * @zn: znode is passed and returned here
1044 * @n: zbranch number is passed and returned here
1045 * @lnum: LEB number of dent node to match
1046 * @offs: offset of dent node to match
1048 * This function is used for "hashed" keys to make sure the found directory or
1049 * extended attribute entry node is what was looked for. It is used when the
1050 * flash address of the right node is known (@lnum:@offs) which makes it much
1051 * easier to resolve collisions (no need to read entries and match full
1052 * names). This function returns %1 and sets @zn and @n if the collision is
1053 * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
1054 * previous directory entry. Otherwise a negative error code is returned.
1056 static int resolve_collision_directly(struct ubifs_info *c,
1057 const union ubifs_key *key,
1058 struct ubifs_znode **zn, int *n,
1061 struct ubifs_znode *znode;
1066 if (matches_position(&znode->zbranch[nn], lnum, offs))
1071 err = tnc_prev(c, &znode, &nn);
1076 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1078 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1089 err = tnc_next(c, &znode, &nn);
1094 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1098 if (matches_position(&znode->zbranch[nn], lnum, offs))
1104 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1105 * @c: UBIFS file-system description object
1106 * @znode: znode to dirty
1108 * If we do not have a unique key that resides in a znode, then we cannot
1109 * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
1110 * This function records the path back to the last dirty ancestor, and then
1111 * dirties the znodes on that path.
1113 static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1114 struct ubifs_znode *znode)
1116 struct ubifs_znode *zp;
1117 int *path = c->bottom_up_buf, p = 0;
1119 ubifs_assert(c->zroot.znode);
1120 ubifs_assert(znode);
1121 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1122 kfree(c->bottom_up_buf);
1123 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1125 if (!c->bottom_up_buf)
1126 return ERR_PTR(-ENOMEM);
1127 path = c->bottom_up_buf;
1129 if (c->zroot.znode->level) {
1130 /* Go up until parent is dirty */
1138 ubifs_assert(p < c->zroot.znode->level);
1140 if (!zp->cnext && ubifs_zn_dirty(znode))
1146 /* Come back down, dirtying as we go */
1148 struct ubifs_zbranch *zbr;
1152 ubifs_assert(path[p - 1] >= 0);
1153 ubifs_assert(path[p - 1] < zp->child_cnt);
1154 zbr = &zp->zbranch[path[--p]];
1155 znode = dirty_cow_znode(c, zbr);
1157 ubifs_assert(znode == c->zroot.znode);
1158 znode = dirty_cow_znode(c, &c->zroot);
1160 if (IS_ERR(znode) || !p)
1162 ubifs_assert(path[p - 1] >= 0);
1163 ubifs_assert(path[p - 1] < znode->child_cnt);
1164 znode = znode->zbranch[path[p - 1]].znode;
1171 * ubifs_lookup_level0 - search for zero-level znode.
1172 * @c: UBIFS file-system description object
1173 * @key: key to lookup
1174 * @zn: znode is returned here
1175 * @n: znode branch slot number is returned here
1177 * This function looks up the TNC tree and search for zero-level znode which
1178 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1180 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1181 * is returned and slot number of the matched branch is stored in @n;
1182 * o not exact match, which means that zero-level znode does not contain
1183 * @key, then %0 is returned and slot number of the closest branch is stored
1185 * o @key is so small that it is even less than the lowest key of the
1186 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1188 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1189 * function reads corresponding indexing nodes and inserts them to TNC. In
1190 * case of failure, a negative error code is returned.
1192 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1193 struct ubifs_znode **zn, int *n)
1196 struct ubifs_znode *znode;
1197 unsigned long time = get_seconds();
1199 dbg_tnck(key, "search key ");
1200 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
1202 znode = c->zroot.znode;
1203 if (unlikely(!znode)) {
1204 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1206 return PTR_ERR(znode);
1212 struct ubifs_zbranch *zbr;
1214 exact = ubifs_search_zbranch(c, znode, key, n);
1216 if (znode->level == 0)
1221 zbr = &znode->zbranch[*n];
1229 /* znode is not in TNC cache, load it from the media */
1230 znode = ubifs_load_znode(c, zbr, znode, *n);
1232 return PTR_ERR(znode);
1236 if (exact || !is_hash_key(c, key) || *n != -1) {
1237 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1242 * Here is a tricky place. We have not found the key and this is a
1243 * "hashed" key, which may collide. The rest of the code deals with
1244 * situations like this:
1248 * | 3 | 5 | | 6 | 7 | (x)
1250 * Or more a complex example:
1254 * | 1 | 3 | | 5 | 8 |
1256 * | 5 | 5 | | 6 | 7 | (x)
1258 * In the examples, if we are looking for key "5", we may reach nodes
1259 * marked with "(x)". In this case what we have do is to look at the
1260 * left and see if there is "5" key there. If there is, we have to
1263 * Note, this whole situation is possible because we allow to have
1264 * elements which are equivalent to the next key in the parent in the
1265 * children of current znode. For example, this happens if we split a
1266 * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
1270 * | 3 | 5 | | 5 | 6 | 7 |
1272 * And this becomes what is at the first "picture" after key "5" marked
1273 * with "^" is removed. What could be done is we could prohibit
1274 * splitting in the middle of the colliding sequence. Also, when
1275 * removing the leftmost key, we would have to correct the key of the
1276 * parent node, which would introduce additional complications. Namely,
1277 * if we changed the leftmost key of the parent znode, the garbage
1278 * collector would be unable to find it (GC is doing this when GC'ing
1279 * indexing LEBs). Although we already have an additional RB-tree where
1280 * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
1281 * after the commit. But anyway, this does not look easy to implement
1282 * so we did not try this.
1284 err = tnc_prev(c, &znode, n);
1285 if (err == -ENOENT) {
1286 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1290 if (unlikely(err < 0))
1292 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1293 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1298 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1304 * lookup_level0_dirty - search for zero-level znode dirtying.
1305 * @c: UBIFS file-system description object
1306 * @key: key to lookup
1307 * @zn: znode is returned here
1308 * @n: znode branch slot number is returned here
1310 * This function looks up the TNC tree and search for zero-level znode which
1311 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1313 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1314 * is returned and slot number of the matched branch is stored in @n;
1315 * o not exact match, which means that zero-level znode does not contain @key
1316 * then %0 is returned and slot number of the closed branch is stored in
1318 * o @key is so small that it is even less than the lowest key of the
1319 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1321 * Additionally all znodes in the path from the root to the located zero-level
1322 * znode are marked as dirty.
1324 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1325 * function reads corresponding indexing nodes and inserts them to TNC. In
1326 * case of failure, a negative error code is returned.
1328 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1329 struct ubifs_znode **zn, int *n)
1332 struct ubifs_znode *znode;
1333 unsigned long time = get_seconds();
1335 dbg_tnck(key, "search and dirty key ");
1337 znode = c->zroot.znode;
1338 if (unlikely(!znode)) {
1339 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1341 return PTR_ERR(znode);
1344 znode = dirty_cow_znode(c, &c->zroot);
1346 return PTR_ERR(znode);
1351 struct ubifs_zbranch *zbr;
1353 exact = ubifs_search_zbranch(c, znode, key, n);
1355 if (znode->level == 0)
1360 zbr = &znode->zbranch[*n];
1364 znode = dirty_cow_znode(c, zbr);
1366 return PTR_ERR(znode);
1370 /* znode is not in TNC cache, load it from the media */
1371 znode = ubifs_load_znode(c, zbr, znode, *n);
1373 return PTR_ERR(znode);
1374 znode = dirty_cow_znode(c, zbr);
1376 return PTR_ERR(znode);
1380 if (exact || !is_hash_key(c, key) || *n != -1) {
1381 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1386 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1389 err = tnc_prev(c, &znode, n);
1390 if (err == -ENOENT) {
1392 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1395 if (unlikely(err < 0))
1397 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1399 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1403 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1404 znode = dirty_cow_bottom_up(c, znode);
1406 return PTR_ERR(znode);
1409 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1415 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1416 * @c: UBIFS file-system description object
1418 * @gc_seq1: garbage collection sequence number
1420 * This function determines if @lnum may have been garbage collected since
1421 * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1424 static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1426 int gc_seq2, gced_lnum;
1428 gced_lnum = c->gced_lnum;
1430 gc_seq2 = c->gc_seq;
1431 /* Same seq means no GC */
1432 if (gc_seq1 == gc_seq2)
1434 /* Different by more than 1 means we don't know */
1435 if (gc_seq1 + 1 != gc_seq2)
1438 * We have seen the sequence number has increased by 1. Now we need to
1439 * be sure we read the right LEB number, so read it again.
1442 if (gced_lnum != c->gced_lnum)
1444 /* Finally we can check lnum */
1445 if (gced_lnum == lnum)
1451 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1452 * @c: UBIFS file-system description object
1453 * @key: node key to lookup
1454 * @node: the node is returned here
1455 * @lnum: LEB number is returned here
1456 * @offs: offset is returned here
1458 * This function looks up and reads node with key @key. The caller has to make
1459 * sure the @node buffer is large enough to fit the node. Returns zero in case
1460 * of success, %-ENOENT if the node was not found, and a negative error code in
1461 * case of failure. The node location can be returned in @lnum and @offs.
1463 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1464 void *node, int *lnum, int *offs)
1466 int found, n, err, safely = 0, gc_seq1;
1467 struct ubifs_znode *znode;
1468 struct ubifs_zbranch zbr, *zt;
1471 mutex_lock(&c->tnc_mutex);
1472 found = ubifs_lookup_level0(c, key, &znode, &n);
1476 } else if (found < 0) {
1480 zt = &znode->zbranch[n];
1485 if (is_hash_key(c, key)) {
1487 * In this case the leaf node cache gets used, so we pass the
1488 * address of the zbranch and keep the mutex locked
1490 err = tnc_read_node_nm(c, zt, node);
1494 err = ubifs_tnc_read_node(c, zt, node);
1497 /* Drop the TNC mutex prematurely and race with garbage collection */
1498 zbr = znode->zbranch[n];
1499 gc_seq1 = c->gc_seq;
1500 mutex_unlock(&c->tnc_mutex);
1502 if (ubifs_get_wbuf(c, zbr.lnum)) {
1503 /* We do not GC journal heads */
1504 err = ubifs_tnc_read_node(c, &zbr, node);
1508 err = fallible_read_node(c, key, &zbr, node);
1509 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1511 * The node may have been GC'ed out from under us so try again
1512 * while keeping the TNC mutex locked.
1520 mutex_unlock(&c->tnc_mutex);
1525 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1526 * @c: UBIFS file-system description object
1527 * @bu: bulk-read parameters and results
1529 * Lookup consecutive data node keys for the same inode that reside
1530 * consecutively in the same LEB. This function returns zero in case of success
1531 * and a negative error code in case of failure.
1533 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1534 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1535 * maximum possible amount of nodes for bulk-read.
1537 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1539 int n, err = 0, lnum = -1, uninitialized_var(offs);
1540 int uninitialized_var(len);
1541 unsigned int block = key_block(c, &bu->key);
1542 struct ubifs_znode *znode;
1548 mutex_lock(&c->tnc_mutex);
1549 /* Find first key */
1550 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1555 len = znode->zbranch[n].len;
1556 /* The buffer must be big enough for at least 1 node */
1557 if (len > bu->buf_len) {
1562 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1564 lnum = znode->zbranch[n].lnum;
1565 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1568 struct ubifs_zbranch *zbr;
1569 union ubifs_key *key;
1570 unsigned int next_block;
1573 err = tnc_next(c, &znode, &n);
1576 zbr = &znode->zbranch[n];
1578 /* See if there is another data key for this file */
1579 if (key_inum(c, key) != key_inum(c, &bu->key) ||
1580 key_type(c, key) != UBIFS_DATA_KEY) {
1585 /* First key found */
1587 offs = ALIGN(zbr->offs + zbr->len, 8);
1589 if (len > bu->buf_len) {
1595 * The data nodes must be in consecutive positions in
1598 if (zbr->lnum != lnum || zbr->offs != offs)
1600 offs += ALIGN(zbr->len, 8);
1601 len = ALIGN(len, 8) + zbr->len;
1602 /* Must not exceed buffer length */
1603 if (len > bu->buf_len)
1606 /* Allow for holes */
1607 next_block = key_block(c, key);
1608 bu->blk_cnt += (next_block - block - 1);
1609 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1613 bu->zbranch[bu->cnt++] = *zbr;
1615 /* See if we have room for more */
1616 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1618 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1622 if (err == -ENOENT) {
1626 bu->gc_seq = c->gc_seq;
1627 mutex_unlock(&c->tnc_mutex);
1631 * An enormous hole could cause bulk-read to encompass too many
1632 * page cache pages, so limit the number here.
1634 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1635 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1637 * Ensure that bulk-read covers a whole number of page cache
1640 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1641 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1644 /* At the end of file we can round up */
1645 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1648 /* Exclude data nodes that do not make up a whole page cache page */
1649 block = key_block(c, &bu->key) + bu->blk_cnt;
1650 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1652 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1660 * read_wbuf - bulk-read from a LEB with a wbuf.
1661 * @wbuf: wbuf that may overlap the read
1662 * @buf: buffer into which to read
1664 * @lnum: LEB number from which to read
1665 * @offs: offset from which to read
1667 * This functions returns %0 on success or a negative error code on failure.
1669 static int read_wbuf(struct ubifs_wbuf *wbuf, void *buf, int len, int lnum,
1672 const struct ubifs_info *c = wbuf->c;
1675 dbg_io("LEB %d:%d, length %d", lnum, offs, len);
1676 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1677 ubifs_assert(!(offs & 7) && offs < c->leb_size);
1678 ubifs_assert(offs + len <= c->leb_size);
1680 spin_lock(&wbuf->lock);
1681 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
1683 /* We may safely unlock the write-buffer and read the data */
1684 spin_unlock(&wbuf->lock);
1685 return ubifs_leb_read(c, lnum, buf, offs, len, 0);
1688 /* Don't read under wbuf */
1689 rlen = wbuf->offs - offs;
1693 /* Copy the rest from the write-buffer */
1694 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
1695 spin_unlock(&wbuf->lock);
1698 /* Read everything that goes before write-buffer */
1699 return ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
1705 * validate_data_node - validate data nodes for bulk-read.
1706 * @c: UBIFS file-system description object
1707 * @buf: buffer containing data node to validate
1708 * @zbr: zbranch of data node to validate
1710 * This functions returns %0 on success or a negative error code on failure.
1712 static int validate_data_node(struct ubifs_info *c, void *buf,
1713 struct ubifs_zbranch *zbr)
1715 union ubifs_key key1;
1716 struct ubifs_ch *ch = buf;
1719 if (ch->node_type != UBIFS_DATA_NODE) {
1720 ubifs_err("bad node type (%d but expected %d)",
1721 ch->node_type, UBIFS_DATA_NODE);
1725 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1727 ubifs_err("expected node type %d", UBIFS_DATA_NODE);
1731 len = le32_to_cpu(ch->len);
1732 if (len != zbr->len) {
1733 ubifs_err("bad node length %d, expected %d", len, zbr->len);
1737 /* Make sure the key of the read node is correct */
1738 key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1739 if (!keys_eq(c, &zbr->key, &key1)) {
1740 ubifs_err("bad key in node at LEB %d:%d",
1741 zbr->lnum, zbr->offs);
1742 dbg_tnck(&zbr->key, "looked for key ");
1743 dbg_tnck(&key1, "found node's key ");
1752 ubifs_err("bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1753 ubifs_dump_node(c, buf);
1759 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1760 * @c: UBIFS file-system description object
1761 * @bu: bulk-read parameters and results
1763 * This functions reads and validates the data nodes that were identified by the
1764 * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
1765 * -EAGAIN to indicate a race with GC, or another negative error code on
1768 int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1770 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
1771 struct ubifs_wbuf *wbuf;
1774 len = bu->zbranch[bu->cnt - 1].offs;
1775 len += bu->zbranch[bu->cnt - 1].len - offs;
1776 if (len > bu->buf_len) {
1777 ubifs_err("buffer too small %d vs %d", bu->buf_len, len);
1782 wbuf = ubifs_get_wbuf(c, lnum);
1784 err = read_wbuf(wbuf, bu->buf, len, lnum, offs);
1786 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0);
1788 /* Check for a race with GC */
1789 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1792 if (err && err != -EBADMSG) {
1793 ubifs_err("failed to read from LEB %d:%d, error %d",
1796 dbg_tnck(&bu->key, "key ");
1800 /* Validate the nodes read */
1802 for (i = 0; i < bu->cnt; i++) {
1803 err = validate_data_node(c, buf, &bu->zbranch[i]);
1806 buf = buf + ALIGN(bu->zbranch[i].len, 8);
1813 * do_lookup_nm- look up a "hashed" node.
1814 * @c: UBIFS file-system description object
1815 * @key: node key to lookup
1816 * @node: the node is returned here
1819 * This function look up and reads a node which contains name hash in the key.
1820 * Since the hash may have collisions, there may be many nodes with the same
1821 * key, so we have to sequentially look to all of them until the needed one is
1822 * found. This function returns zero in case of success, %-ENOENT if the node
1823 * was not found, and a negative error code in case of failure.
1825 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1826 void *node, const struct qstr *nm)
1829 struct ubifs_znode *znode;
1831 dbg_tnck(key, "name '%.*s' key ", nm->len, nm->name);
1832 mutex_lock(&c->tnc_mutex);
1833 found = ubifs_lookup_level0(c, key, &znode, &n);
1837 } else if (found < 0) {
1842 ubifs_assert(n >= 0);
1844 err = resolve_collision(c, key, &znode, &n, nm);
1845 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1846 if (unlikely(err < 0))
1853 err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1856 mutex_unlock(&c->tnc_mutex);
1861 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1862 * @c: UBIFS file-system description object
1863 * @key: node key to lookup
1864 * @node: the node is returned here
1867 * This function look up and reads a node which contains name hash in the key.
1868 * Since the hash may have collisions, there may be many nodes with the same
1869 * key, so we have to sequentially look to all of them until the needed one is
1870 * found. This function returns zero in case of success, %-ENOENT if the node
1871 * was not found, and a negative error code in case of failure.
1873 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1874 void *node, const struct qstr *nm)
1877 const struct ubifs_dent_node *dent = node;
1880 * We assume that in most of the cases there are no name collisions and
1881 * 'ubifs_tnc_lookup()' returns us the right direntry.
1883 err = ubifs_tnc_lookup(c, key, node);
1887 len = le16_to_cpu(dent->nlen);
1888 if (nm->len == len && !memcmp(dent->name, nm->name, len))
1892 * Unluckily, there are hash collisions and we have to iterate over
1893 * them look at each direntry with colliding name hash sequentially.
1895 return do_lookup_nm(c, key, node, nm);
1899 * correct_parent_keys - correct parent znodes' keys.
1900 * @c: UBIFS file-system description object
1901 * @znode: znode to correct parent znodes for
1903 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1904 * zbranch changes, keys of parent znodes have to be corrected. This helper
1905 * function is called in such situations and corrects the keys if needed.
1907 static void correct_parent_keys(const struct ubifs_info *c,
1908 struct ubifs_znode *znode)
1910 union ubifs_key *key, *key1;
1912 ubifs_assert(znode->parent);
1913 ubifs_assert(znode->iip == 0);
1915 key = &znode->zbranch[0].key;
1916 key1 = &znode->parent->zbranch[0].key;
1918 while (keys_cmp(c, key, key1) < 0) {
1919 key_copy(c, key, key1);
1920 znode = znode->parent;
1922 if (!znode->parent || znode->iip)
1924 key1 = &znode->parent->zbranch[0].key;
1929 * insert_zbranch - insert a zbranch into a znode.
1930 * @znode: znode into which to insert
1931 * @zbr: zbranch to insert
1932 * @n: slot number to insert to
1934 * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
1935 * znode's array of zbranches and keeps zbranches consolidated, so when a new
1936 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1937 * slot, zbranches starting from @n have to be moved right.
1939 static void insert_zbranch(struct ubifs_znode *znode,
1940 const struct ubifs_zbranch *zbr, int n)
1944 ubifs_assert(ubifs_zn_dirty(znode));
1947 for (i = znode->child_cnt; i > n; i--) {
1948 znode->zbranch[i] = znode->zbranch[i - 1];
1949 if (znode->zbranch[i].znode)
1950 znode->zbranch[i].znode->iip = i;
1953 zbr->znode->iip = n;
1955 for (i = znode->child_cnt; i > n; i--)
1956 znode->zbranch[i] = znode->zbranch[i - 1];
1958 znode->zbranch[n] = *zbr;
1959 znode->child_cnt += 1;
1962 * After inserting at slot zero, the lower bound of the key range of
1963 * this znode may have changed. If this znode is subsequently split
1964 * then the upper bound of the key range may change, and furthermore
1965 * it could change to be lower than the original lower bound. If that
1966 * happens, then it will no longer be possible to find this znode in the
1967 * TNC using the key from the index node on flash. That is bad because
1968 * if it is not found, we will assume it is obsolete and may overwrite
1969 * it. Then if there is an unclean unmount, we will start using the
1970 * old index which will be broken.
1972 * So we first mark znodes that have insertions at slot zero, and then
1973 * if they are split we add their lnum/offs to the old_idx tree.
1980 * tnc_insert - insert a node into TNC.
1981 * @c: UBIFS file-system description object
1982 * @znode: znode to insert into
1983 * @zbr: branch to insert
1984 * @n: slot number to insert new zbranch to
1986 * This function inserts a new node described by @zbr into znode @znode. If
1987 * znode does not have a free slot for new zbranch, it is split. Parent znodes
1988 * are splat as well if needed. Returns zero in case of success or a negative
1989 * error code in case of failure.
1991 static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1992 struct ubifs_zbranch *zbr, int n)
1994 struct ubifs_znode *zn, *zi, *zp;
1995 int i, keep, move, appending = 0;
1996 union ubifs_key *key = &zbr->key, *key1;
1998 ubifs_assert(n >= 0 && n <= c->fanout);
2000 /* Implement naive insert for now */
2003 if (znode->child_cnt < c->fanout) {
2004 ubifs_assert(n != c->fanout);
2005 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level);
2007 insert_zbranch(znode, zbr, n);
2009 /* Ensure parent's key is correct */
2010 if (n == 0 && zp && znode->iip == 0)
2011 correct_parent_keys(c, znode);
2017 * Unfortunately, @znode does not have more empty slots and we have to
2020 dbg_tnck(key, "splitting level %d, key ", znode->level);
2024 * We can no longer be sure of finding this znode by key, so we
2025 * record it in the old_idx tree.
2027 ins_clr_old_idx_znode(c, znode);
2029 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
2033 zn->level = znode->level;
2035 /* Decide where to split */
2036 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
2037 /* Try not to split consecutive data keys */
2038 if (n == c->fanout) {
2039 key1 = &znode->zbranch[n - 1].key;
2040 if (key_inum(c, key1) == key_inum(c, key) &&
2041 key_type(c, key1) == UBIFS_DATA_KEY)
2045 } else if (appending && n != c->fanout) {
2046 /* Try not to split consecutive data keys */
2049 if (n >= (c->fanout + 1) / 2) {
2050 key1 = &znode->zbranch[0].key;
2051 if (key_inum(c, key1) == key_inum(c, key) &&
2052 key_type(c, key1) == UBIFS_DATA_KEY) {
2053 key1 = &znode->zbranch[n].key;
2054 if (key_inum(c, key1) != key_inum(c, key) ||
2055 key_type(c, key1) != UBIFS_DATA_KEY) {
2057 move = c->fanout - keep;
2069 keep = (c->fanout + 1) / 2;
2070 move = c->fanout - keep;
2074 * Although we don't at present, we could look at the neighbors and see
2075 * if we can move some zbranches there.
2079 /* Insert into existing znode */
2084 /* Insert into new znode */
2089 zbr->znode->parent = zn;
2094 __set_bit(DIRTY_ZNODE, &zn->flags);
2095 atomic_long_inc(&c->dirty_zn_cnt);
2097 zn->child_cnt = move;
2098 znode->child_cnt = keep;
2100 dbg_tnc("moving %d, keeping %d", move, keep);
2103 for (i = 0; i < move; i++) {
2104 zn->zbranch[i] = znode->zbranch[keep + i];
2107 if (zn->zbranch[i].znode) {
2108 zn->zbranch[i].znode->parent = zn;
2109 zn->zbranch[i].znode->iip = i;
2113 /* Insert new key and branch */
2114 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level);
2116 insert_zbranch(zi, zbr, n);
2118 /* Insert new znode (produced by spitting) into the parent */
2120 if (n == 0 && zi == znode && znode->iip == 0)
2121 correct_parent_keys(c, znode);
2123 /* Locate insertion point */
2126 /* Tail recursion */
2127 zbr->key = zn->zbranch[0].key;
2137 /* We have to split root znode */
2138 dbg_tnc("creating new zroot at level %d", znode->level + 1);
2140 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2145 zi->level = znode->level + 1;
2147 __set_bit(DIRTY_ZNODE, &zi->flags);
2148 atomic_long_inc(&c->dirty_zn_cnt);
2150 zi->zbranch[0].key = znode->zbranch[0].key;
2151 zi->zbranch[0].znode = znode;
2152 zi->zbranch[0].lnum = c->zroot.lnum;
2153 zi->zbranch[0].offs = c->zroot.offs;
2154 zi->zbranch[0].len = c->zroot.len;
2155 zi->zbranch[1].key = zn->zbranch[0].key;
2156 zi->zbranch[1].znode = zn;
2161 c->zroot.znode = zi;
2172 * ubifs_tnc_add - add a node to TNC.
2173 * @c: UBIFS file-system description object
2175 * @lnum: LEB number of node
2176 * @offs: node offset
2179 * This function adds a node with key @key to TNC. The node may be new or it may
2180 * obsolete some existing one. Returns %0 on success or negative error code on
2183 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2186 int found, n, err = 0;
2187 struct ubifs_znode *znode;
2189 mutex_lock(&c->tnc_mutex);
2190 dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len);
2191 found = lookup_level0_dirty(c, key, &znode, &n);
2193 struct ubifs_zbranch zbr;
2199 key_copy(c, key, &zbr.key);
2200 err = tnc_insert(c, znode, &zbr, n + 1);
2201 } else if (found == 1) {
2202 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2205 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2212 err = dbg_check_tnc(c, 0);
2213 mutex_unlock(&c->tnc_mutex);
2219 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2220 * @c: UBIFS file-system description object
2222 * @old_lnum: LEB number of old node
2223 * @old_offs: old node offset
2224 * @lnum: LEB number of node
2225 * @offs: node offset
2228 * This function replaces a node with key @key in the TNC only if the old node
2229 * is found. This function is called by garbage collection when node are moved.
2230 * Returns %0 on success or negative error code on failure.
2232 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2233 int old_lnum, int old_offs, int lnum, int offs, int len)
2235 int found, n, err = 0;
2236 struct ubifs_znode *znode;
2238 mutex_lock(&c->tnc_mutex);
2239 dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum,
2240 old_offs, lnum, offs, len);
2241 found = lookup_level0_dirty(c, key, &znode, &n);
2248 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2251 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2253 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2260 } else if (is_hash_key(c, key)) {
2261 found = resolve_collision_directly(c, key, &znode, &n,
2262 old_lnum, old_offs);
2263 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2264 found, znode, n, old_lnum, old_offs);
2271 /* Ensure the znode is dirtied */
2272 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2273 znode = dirty_cow_bottom_up(c, znode);
2274 if (IS_ERR(znode)) {
2275 err = PTR_ERR(znode);
2279 zbr = &znode->zbranch[n];
2281 err = ubifs_add_dirt(c, zbr->lnum,
2293 err = ubifs_add_dirt(c, lnum, len);
2296 err = dbg_check_tnc(c, 0);
2299 mutex_unlock(&c->tnc_mutex);
2304 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2305 * @c: UBIFS file-system description object
2307 * @lnum: LEB number of node
2308 * @offs: node offset
2312 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
2313 * may have collisions, like directory entry keys.
2315 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2316 int lnum, int offs, int len, const struct qstr *nm)
2318 int found, n, err = 0;
2319 struct ubifs_znode *znode;
2321 mutex_lock(&c->tnc_mutex);
2322 dbg_tnck(key, "LEB %d:%d, name '%.*s', key ",
2323 lnum, offs, nm->len, nm->name);
2324 found = lookup_level0_dirty(c, key, &znode, &n);
2332 found = fallible_resolve_collision(c, key, &znode, &n,
2335 found = resolve_collision(c, key, &znode, &n, nm);
2336 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2342 /* Ensure the znode is dirtied */
2343 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2344 znode = dirty_cow_bottom_up(c, znode);
2345 if (IS_ERR(znode)) {
2346 err = PTR_ERR(znode);
2352 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2355 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2364 struct ubifs_zbranch zbr;
2370 key_copy(c, key, &zbr.key);
2371 err = tnc_insert(c, znode, &zbr, n + 1);
2376 * We did not find it in the index so there may be a
2377 * dangling branch still in the index. So we remove it
2378 * by passing 'ubifs_tnc_remove_nm()' the same key but
2379 * an unmatchable name.
2381 struct qstr noname = { .len = 0, .name = "" };
2383 err = dbg_check_tnc(c, 0);
2384 mutex_unlock(&c->tnc_mutex);
2387 return ubifs_tnc_remove_nm(c, key, &noname);
2393 err = dbg_check_tnc(c, 0);
2394 mutex_unlock(&c->tnc_mutex);
2399 * tnc_delete - delete a znode form TNC.
2400 * @c: UBIFS file-system description object
2401 * @znode: znode to delete from
2402 * @n: zbranch slot number to delete
2404 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2405 * case of success and a negative error code in case of failure.
2407 static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2409 struct ubifs_zbranch *zbr;
2410 struct ubifs_znode *zp;
2413 /* Delete without merge for now */
2414 ubifs_assert(znode->level == 0);
2415 ubifs_assert(n >= 0 && n < c->fanout);
2416 dbg_tnck(&znode->zbranch[n].key, "deleting key ");
2418 zbr = &znode->zbranch[n];
2421 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2423 ubifs_dump_znode(c, znode);
2427 /* We do not "gap" zbranch slots */
2428 for (i = n; i < znode->child_cnt - 1; i++)
2429 znode->zbranch[i] = znode->zbranch[i + 1];
2430 znode->child_cnt -= 1;
2432 if (znode->child_cnt > 0)
2436 * This was the last zbranch, we have to delete this znode from the
2441 ubifs_assert(!ubifs_zn_obsolete(znode));
2442 ubifs_assert(ubifs_zn_dirty(znode));
2447 atomic_long_dec(&c->dirty_zn_cnt);
2449 err = insert_old_idx_znode(c, znode);
2454 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2455 atomic_long_inc(&c->clean_zn_cnt);
2456 atomic_long_inc(&ubifs_clean_zn_cnt);
2460 } while (znode->child_cnt == 1); /* while removing last child */
2462 /* Remove from znode, entry n - 1 */
2463 znode->child_cnt -= 1;
2464 ubifs_assert(znode->level != 0);
2465 for (i = n; i < znode->child_cnt; i++) {
2466 znode->zbranch[i] = znode->zbranch[i + 1];
2467 if (znode->zbranch[i].znode)
2468 znode->zbranch[i].znode->iip = i;
2472 * If this is the root and it has only 1 child then
2473 * collapse the tree.
2475 if (!znode->parent) {
2476 while (znode->child_cnt == 1 && znode->level != 0) {
2478 zbr = &znode->zbranch[0];
2479 znode = get_znode(c, znode, 0);
2481 return PTR_ERR(znode);
2482 znode = dirty_cow_znode(c, zbr);
2484 return PTR_ERR(znode);
2485 znode->parent = NULL;
2488 err = insert_old_idx(c, c->zroot.lnum,
2493 c->zroot.lnum = zbr->lnum;
2494 c->zroot.offs = zbr->offs;
2495 c->zroot.len = zbr->len;
2496 c->zroot.znode = znode;
2497 ubifs_assert(!ubifs_zn_obsolete(zp));
2498 ubifs_assert(ubifs_zn_dirty(zp));
2499 atomic_long_dec(&c->dirty_zn_cnt);
2502 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2503 atomic_long_inc(&c->clean_zn_cnt);
2504 atomic_long_inc(&ubifs_clean_zn_cnt);
2514 * ubifs_tnc_remove - remove an index entry of a node.
2515 * @c: UBIFS file-system description object
2518 * Returns %0 on success or negative error code on failure.
2520 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2522 int found, n, err = 0;
2523 struct ubifs_znode *znode;
2525 mutex_lock(&c->tnc_mutex);
2526 dbg_tnck(key, "key ");
2527 found = lookup_level0_dirty(c, key, &znode, &n);
2533 err = tnc_delete(c, znode, n);
2535 err = dbg_check_tnc(c, 0);
2538 mutex_unlock(&c->tnc_mutex);
2543 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2544 * @c: UBIFS file-system description object
2546 * @nm: directory entry name
2548 * Returns %0 on success or negative error code on failure.
2550 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2551 const struct qstr *nm)
2554 struct ubifs_znode *znode;
2556 mutex_lock(&c->tnc_mutex);
2557 dbg_tnck(key, "%.*s, key ", nm->len, nm->name);
2558 err = lookup_level0_dirty(c, key, &znode, &n);
2564 err = fallible_resolve_collision(c, key, &znode, &n,
2567 err = resolve_collision(c, key, &znode, &n, nm);
2568 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2572 /* Ensure the znode is dirtied */
2573 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2574 znode = dirty_cow_bottom_up(c, znode);
2575 if (IS_ERR(znode)) {
2576 err = PTR_ERR(znode);
2580 err = tnc_delete(c, znode, n);
2586 err = dbg_check_tnc(c, 0);
2587 mutex_unlock(&c->tnc_mutex);
2592 * key_in_range - determine if a key falls within a range of keys.
2593 * @c: UBIFS file-system description object
2594 * @key: key to check
2595 * @from_key: lowest key in range
2596 * @to_key: highest key in range
2598 * This function returns %1 if the key is in range and %0 otherwise.
2600 static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2601 union ubifs_key *from_key, union ubifs_key *to_key)
2603 if (keys_cmp(c, key, from_key) < 0)
2605 if (keys_cmp(c, key, to_key) > 0)
2611 * ubifs_tnc_remove_range - remove index entries in range.
2612 * @c: UBIFS file-system description object
2613 * @from_key: lowest key to remove
2614 * @to_key: highest key to remove
2616 * This function removes index entries starting at @from_key and ending at
2617 * @to_key. This function returns zero in case of success and a negative error
2618 * code in case of failure.
2620 int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2621 union ubifs_key *to_key)
2623 int i, n, k, err = 0;
2624 struct ubifs_znode *znode;
2625 union ubifs_key *key;
2627 mutex_lock(&c->tnc_mutex);
2629 /* Find first level 0 znode that contains keys to remove */
2630 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2637 err = tnc_next(c, &znode, &n);
2638 if (err == -ENOENT) {
2644 key = &znode->zbranch[n].key;
2645 if (!key_in_range(c, key, from_key, to_key)) {
2651 /* Ensure the znode is dirtied */
2652 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2653 znode = dirty_cow_bottom_up(c, znode);
2654 if (IS_ERR(znode)) {
2655 err = PTR_ERR(znode);
2660 /* Remove all keys in range except the first */
2661 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2662 key = &znode->zbranch[i].key;
2663 if (!key_in_range(c, key, from_key, to_key))
2665 lnc_free(&znode->zbranch[i]);
2666 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2667 znode->zbranch[i].len);
2669 ubifs_dump_znode(c, znode);
2672 dbg_tnck(key, "removing key ");
2675 for (i = n + 1 + k; i < znode->child_cnt; i++)
2676 znode->zbranch[i - k] = znode->zbranch[i];
2677 znode->child_cnt -= k;
2680 /* Now delete the first */
2681 err = tnc_delete(c, znode, n);
2688 err = dbg_check_tnc(c, 0);
2689 mutex_unlock(&c->tnc_mutex);
2694 * ubifs_tnc_remove_ino - remove an inode from TNC.
2695 * @c: UBIFS file-system description object
2696 * @inum: inode number to remove
2698 * This function remove inode @inum and all the extended attributes associated
2699 * with the anode from TNC and returns zero in case of success or a negative
2700 * error code in case of failure.
2702 int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2704 union ubifs_key key1, key2;
2705 struct ubifs_dent_node *xent, *pxent = NULL;
2706 struct qstr nm = { .name = NULL };
2708 dbg_tnc("ino %lu", (unsigned long)inum);
2711 * Walk all extended attribute entries and remove them together with
2712 * corresponding extended attribute inodes.
2714 lowest_xent_key(c, &key1, inum);
2719 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2721 err = PTR_ERR(xent);
2727 xattr_inum = le64_to_cpu(xent->inum);
2728 dbg_tnc("xent '%s', ino %lu", xent->name,
2729 (unsigned long)xattr_inum);
2731 #ifdef CONFIG_UBIFS_FS_XATTR
2732 ubifs_evict_xattr_inode(c, xattr_inum);
2735 nm.name = xent->name;
2736 nm.len = le16_to_cpu(xent->nlen);
2737 err = ubifs_tnc_remove_nm(c, &key1, &nm);
2743 lowest_ino_key(c, &key1, xattr_inum);
2744 highest_ino_key(c, &key2, xattr_inum);
2745 err = ubifs_tnc_remove_range(c, &key1, &key2);
2753 key_read(c, &xent->key, &key1);
2757 lowest_ino_key(c, &key1, inum);
2758 highest_ino_key(c, &key2, inum);
2760 return ubifs_tnc_remove_range(c, &key1, &key2);
2764 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2765 * @c: UBIFS file-system description object
2766 * @key: key of last entry
2767 * @nm: name of last entry found or %NULL
2769 * This function finds and reads the next directory or extended attribute entry
2770 * after the given key (@key) if there is one. @nm is used to resolve
2773 * If the name of the current entry is not known and only the key is known,
2774 * @nm->name has to be %NULL. In this case the semantics of this function is a
2775 * little bit different and it returns the entry corresponding to this key, not
2776 * the next one. If the key was not found, the closest "right" entry is
2779 * If the fist entry has to be found, @key has to contain the lowest possible
2780 * key value for this inode and @name has to be %NULL.
2782 * This function returns the found directory or extended attribute entry node
2783 * in case of success, %-ENOENT is returned if no entry was found, and a
2784 * negative error code is returned in case of failure.
2786 struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2787 union ubifs_key *key,
2788 const struct qstr *nm)
2790 int n, err, type = key_type(c, key);
2791 struct ubifs_znode *znode;
2792 struct ubifs_dent_node *dent;
2793 struct ubifs_zbranch *zbr;
2794 union ubifs_key *dkey;
2796 dbg_tnck(key, "%s ", nm->name ? (char *)nm->name : "(lowest)");
2797 ubifs_assert(is_hash_key(c, key));
2799 mutex_lock(&c->tnc_mutex);
2800 err = ubifs_lookup_level0(c, key, &znode, &n);
2801 if (unlikely(err < 0))
2806 /* Handle collisions */
2808 err = fallible_resolve_collision(c, key, &znode, &n,
2811 err = resolve_collision(c, key, &znode, &n, nm);
2812 dbg_tnc("rc returned %d, znode %p, n %d",
2814 if (unlikely(err < 0))
2818 /* Now find next entry */
2819 err = tnc_next(c, &znode, &n);
2824 * The full name of the entry was not given, in which case the
2825 * behavior of this function is a little different and it
2826 * returns current entry, not the next one.
2830 * However, the given key does not exist in the TNC
2831 * tree and @znode/@n variables contain the closest
2832 * "preceding" element. Switch to the next one.
2834 err = tnc_next(c, &znode, &n);
2840 zbr = &znode->zbranch[n];
2841 dent = kmalloc(zbr->len, GFP_NOFS);
2842 if (unlikely(!dent)) {
2848 * The above 'tnc_next()' call could lead us to the next inode, check
2852 if (key_inum(c, dkey) != key_inum(c, key) ||
2853 key_type(c, dkey) != type) {
2858 err = tnc_read_node_nm(c, zbr, dent);
2862 mutex_unlock(&c->tnc_mutex);
2868 mutex_unlock(&c->tnc_mutex);
2869 return ERR_PTR(err);
2873 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2874 * @c: UBIFS file-system description object
2876 * Destroy left-over obsolete znodes from a failed commit.
2878 static void tnc_destroy_cnext(struct ubifs_info *c)
2880 struct ubifs_znode *cnext;
2884 ubifs_assert(c->cmt_state == COMMIT_BROKEN);
2887 struct ubifs_znode *znode = cnext;
2889 cnext = cnext->cnext;
2890 if (ubifs_zn_obsolete(znode))
2892 } while (cnext && cnext != c->cnext);
2896 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2897 * @c: UBIFS file-system description object
2899 void ubifs_tnc_close(struct ubifs_info *c)
2901 tnc_destroy_cnext(c);
2902 if (c->zroot.znode) {
2905 ubifs_destroy_tnc_subtree(c->zroot.znode);
2906 n = atomic_long_read(&c->clean_zn_cnt);
2907 atomic_long_sub(n, &ubifs_clean_zn_cnt);
2915 * left_znode - get the znode to the left.
2916 * @c: UBIFS file-system description object
2919 * This function returns a pointer to the znode to the left of @znode or NULL if
2920 * there is not one. A negative error code is returned on failure.
2922 static struct ubifs_znode *left_znode(struct ubifs_info *c,
2923 struct ubifs_znode *znode)
2925 int level = znode->level;
2928 int n = znode->iip - 1;
2930 /* Go up until we can go left */
2931 znode = znode->parent;
2935 /* Now go down the rightmost branch to 'level' */
2936 znode = get_znode(c, znode, n);
2939 while (znode->level != level) {
2940 n = znode->child_cnt - 1;
2941 znode = get_znode(c, znode, n);
2952 * right_znode - get the znode to the right.
2953 * @c: UBIFS file-system description object
2956 * This function returns a pointer to the znode to the right of @znode or NULL
2957 * if there is not one. A negative error code is returned on failure.
2959 static struct ubifs_znode *right_znode(struct ubifs_info *c,
2960 struct ubifs_znode *znode)
2962 int level = znode->level;
2965 int n = znode->iip + 1;
2967 /* Go up until we can go right */
2968 znode = znode->parent;
2971 if (n < znode->child_cnt) {
2972 /* Now go down the leftmost branch to 'level' */
2973 znode = get_znode(c, znode, n);
2976 while (znode->level != level) {
2977 znode = get_znode(c, znode, 0);
2988 * lookup_znode - find a particular indexing node from TNC.
2989 * @c: UBIFS file-system description object
2990 * @key: index node key to lookup
2991 * @level: index node level
2992 * @lnum: index node LEB number
2993 * @offs: index node offset
2995 * This function searches an indexing node by its first key @key and its
2996 * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
2997 * nodes it traverses to TNC. This function is called for indexing nodes which
2998 * were found on the media by scanning, for example when garbage-collecting or
2999 * when doing in-the-gaps commit. This means that the indexing node which is
3000 * looked for does not have to have exactly the same leftmost key @key, because
3001 * the leftmost key may have been changed, in which case TNC will contain a
3002 * dirty znode which still refers the same @lnum:@offs. This function is clever
3003 * enough to recognize such indexing nodes.
3005 * Note, if a znode was deleted or changed too much, then this function will
3006 * not find it. For situations like this UBIFS has the old index RB-tree
3007 * (indexed by @lnum:@offs).
3009 * This function returns a pointer to the znode found or %NULL if it is not
3010 * found. A negative error code is returned on failure.
3012 static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
3013 union ubifs_key *key, int level,
3016 struct ubifs_znode *znode, *zn;
3019 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
3022 * The arguments have probably been read off flash, so don't assume
3026 return ERR_PTR(-EINVAL);
3028 /* Get the root znode */
3029 znode = c->zroot.znode;
3031 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
3035 /* Check if it is the one we are looking for */
3036 if (c->zroot.lnum == lnum && c->zroot.offs == offs)
3038 /* Descend to the parent level i.e. (level + 1) */
3039 if (level >= znode->level)
3042 ubifs_search_zbranch(c, znode, key, &n);
3045 * We reached a znode where the leftmost key is greater
3046 * than the key we are searching for. This is the same
3047 * situation as the one described in a huge comment at
3048 * the end of the 'ubifs_lookup_level0()' function. And
3049 * for exactly the same reasons we have to try to look
3050 * left before giving up.
3052 znode = left_znode(c, znode);
3057 ubifs_search_zbranch(c, znode, key, &n);
3058 ubifs_assert(n >= 0);
3060 if (znode->level == level + 1)
3062 znode = get_znode(c, znode, n);
3066 /* Check if the child is the one we are looking for */
3067 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
3068 return get_znode(c, znode, n);
3069 /* If the key is unique, there is nowhere else to look */
3070 if (!is_hash_key(c, key))
3073 * The key is not unique and so may be also in the znodes to either
3080 /* Move one branch to the left */
3084 znode = left_znode(c, znode);
3089 n = znode->child_cnt - 1;
3092 if (znode->zbranch[n].lnum == lnum &&
3093 znode->zbranch[n].offs == offs)
3094 return get_znode(c, znode, n);
3095 /* Stop if the key is less than the one we are looking for */
3096 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
3099 /* Back to the middle */
3104 /* Move one branch to the right */
3105 if (++n >= znode->child_cnt) {
3106 znode = right_znode(c, znode);
3114 if (znode->zbranch[n].lnum == lnum &&
3115 znode->zbranch[n].offs == offs)
3116 return get_znode(c, znode, n);
3117 /* Stop if the key is greater than the one we are looking for */
3118 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
3125 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3126 * @c: UBIFS file-system description object
3127 * @key: key of index node
3128 * @level: index node level
3129 * @lnum: LEB number of index node
3130 * @offs: offset of index node
3132 * This function returns %0 if the index node is not referred to in the TNC, %1
3133 * if the index node is referred to in the TNC and the corresponding znode is
3134 * dirty, %2 if an index node is referred to in the TNC and the corresponding
3135 * znode is clean, and a negative error code in case of failure.
3137 * Note, the @key argument has to be the key of the first child. Also note,
3138 * this function relies on the fact that 0:0 is never a valid LEB number and
3139 * offset for a main-area node.
3141 int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
3144 struct ubifs_znode *znode;
3146 znode = lookup_znode(c, key, level, lnum, offs);
3150 return PTR_ERR(znode);
3152 return ubifs_zn_dirty(znode) ? 1 : 2;
3156 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3157 * @c: UBIFS file-system description object
3159 * @lnum: node LEB number
3160 * @offs: node offset
3162 * This function returns %1 if the node is referred to in the TNC, %0 if it is
3163 * not, and a negative error code in case of failure.
3165 * Note, this function relies on the fact that 0:0 is never a valid LEB number
3166 * and offset for a main-area node.
3168 static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
3171 struct ubifs_zbranch *zbr;
3172 struct ubifs_znode *znode, *zn;
3173 int n, found, err, nn;
3174 const int unique = !is_hash_key(c, key);
3176 found = ubifs_lookup_level0(c, key, &znode, &n);
3178 return found; /* Error code */
3181 zbr = &znode->zbranch[n];
3182 if (lnum == zbr->lnum && offs == zbr->offs)
3183 return 1; /* Found it */
3187 * Because the key is not unique, we have to look left
3194 err = tnc_prev(c, &znode, &n);
3199 if (keys_cmp(c, key, &znode->zbranch[n].key))
3201 zbr = &znode->zbranch[n];
3202 if (lnum == zbr->lnum && offs == zbr->offs)
3203 return 1; /* Found it */
3209 err = tnc_next(c, &znode, &n);
3215 if (keys_cmp(c, key, &znode->zbranch[n].key))
3217 zbr = &znode->zbranch[n];
3218 if (lnum == zbr->lnum && offs == zbr->offs)
3219 return 1; /* Found it */
3225 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3226 * @c: UBIFS file-system description object
3228 * @level: index node level (if it is an index node)
3229 * @lnum: node LEB number
3230 * @offs: node offset
3231 * @is_idx: non-zero if the node is an index node
3233 * This function returns %1 if the node is in the TNC, %0 if it is not, and a
3234 * negative error code in case of failure. For index nodes, @key has to be the
3235 * key of the first child. An index node is considered to be in the TNC only if
3236 * the corresponding znode is clean or has not been loaded.
3238 int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
3239 int lnum, int offs, int is_idx)
3243 mutex_lock(&c->tnc_mutex);
3245 err = is_idx_node_in_tnc(c, key, level, lnum, offs);
3249 /* The index node was found but it was dirty */
3252 /* The index node was found and it was clean */
3257 err = is_leaf_node_in_tnc(c, key, lnum, offs);
3260 mutex_unlock(&c->tnc_mutex);
3265 * ubifs_dirty_idx_node - dirty an index node.
3266 * @c: UBIFS file-system description object
3267 * @key: index node key
3268 * @level: index node level
3269 * @lnum: index node LEB number
3270 * @offs: index node offset
3272 * This function loads and dirties an index node so that it can be garbage
3273 * collected. The @key argument has to be the key of the first child. This
3274 * function relies on the fact that 0:0 is never a valid LEB number and offset
3275 * for a main-area node. Returns %0 on success and a negative error code on
3278 int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
3281 struct ubifs_znode *znode;
3284 mutex_lock(&c->tnc_mutex);
3285 znode = lookup_znode(c, key, level, lnum, offs);
3288 if (IS_ERR(znode)) {
3289 err = PTR_ERR(znode);
3292 znode = dirty_cow_bottom_up(c, znode);
3293 if (IS_ERR(znode)) {
3294 err = PTR_ERR(znode);
3299 mutex_unlock(&c->tnc_mutex);
3304 * dbg_check_inode_size - check if inode size is correct.
3305 * @c: UBIFS file-system description object
3306 * @inum: inode number
3309 * This function makes sure that the inode size (@size) is correct and it does
3310 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3311 * if it has a data page beyond @size, and other negative error code in case of
3314 int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
3318 union ubifs_key from_key, to_key, *key;
3319 struct ubifs_znode *znode;
3322 if (!S_ISREG(inode->i_mode))
3324 if (!dbg_is_chk_gen(c))
3327 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
3328 data_key_init(c, &from_key, inode->i_ino, block);
3329 highest_data_key(c, &to_key, inode->i_ino);
3331 mutex_lock(&c->tnc_mutex);
3332 err = ubifs_lookup_level0(c, &from_key, &znode, &n);
3342 err = tnc_next(c, &znode, &n);
3343 if (err == -ENOENT) {
3350 ubifs_assert(err == 0);
3351 key = &znode->zbranch[n].key;
3352 if (!key_in_range(c, key, &from_key, &to_key))
3356 block = key_block(c, key);
3357 ubifs_err("inode %lu has size %lld, but there are data at offset %lld",
3358 (unsigned long)inode->i_ino, size,
3359 ((loff_t)block) << UBIFS_BLOCK_SHIFT);
3360 mutex_unlock(&c->tnc_mutex);
3361 ubifs_dump_inode(c, inode);
3366 mutex_unlock(&c->tnc_mutex);