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
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
41 * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
43 * These constants were introduce to improve readability.
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
58 * Returns %0 on success, and a negative error code on failure.
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).
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.
75 static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
77 struct ubifs_old_idx *old_idx, *o;
78 struct rb_node **p, *parent = NULL;
80 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
81 if (unlikely(!old_idx))
86 p = &c->old_idx.rb_node;
89 o = rb_entry(parent, struct ubifs_old_idx, rb);
92 else if (lnum > o->lnum)
94 else if (offs < o->offs)
96 else if (offs > o->offs)
99 ubifs_err("old idx added twice!");
104 rb_link_node(&old_idx->rb, parent, p);
105 rb_insert_color(&old_idx->rb, &c->old_idx);
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
114 * Returns %0 on success, and a negative error code on failure.
116 int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
119 struct ubifs_zbranch *zbr;
121 zbr = &znode->parent->zbranch[znode->iip];
123 return insert_old_idx(c, zbr->lnum, zbr->offs);
126 return insert_old_idx(c, c->zroot.lnum,
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
136 * Returns %0 on success, and a negative error code on failure.
138 static int ins_clr_old_idx_znode(struct ubifs_info *c,
139 struct ubifs_znode *znode)
144 struct ubifs_zbranch *zbr;
146 zbr = &znode->parent->zbranch[znode->iip];
148 err = insert_old_idx(c, zbr->lnum, zbr->offs);
157 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
168 * destroy_old_idx - destroy the old_idx RB-tree.
169 * @c: UBIFS file-system description object
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.
177 void destroy_old_idx(struct ubifs_info *c)
179 struct rb_node *this = c->old_idx.rb_node;
180 struct ubifs_old_idx *old_idx;
184 this = this->rb_left;
186 } else if (this->rb_right) {
187 this = this->rb_right;
190 old_idx = rb_entry(this, struct ubifs_old_idx, rb);
191 this = rb_parent(this);
193 if (this->rb_left == &old_idx->rb)
194 this->rb_left = NULL;
196 this->rb_right = NULL;
200 c->old_idx = RB_ROOT;
204 * copy_znode - copy a dirty znode.
205 * @c: UBIFS file-system description object
206 * @znode: znode to copy
208 * A dirty znode being committed may not be changed, so it is copied.
210 static struct ubifs_znode *copy_znode(struct ubifs_info *c,
211 struct ubifs_znode *znode)
213 struct ubifs_znode *zn;
215 zn = kmalloc(c->max_znode_sz, GFP_NOFS);
217 return ERR_PTR(-ENOMEM);
219 memcpy(zn, znode, c->max_znode_sz);
221 __set_bit(DIRTY_ZNODE, &zn->flags);
222 __clear_bit(COW_ZNODE, &zn->flags);
224 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
225 __set_bit(OBSOLETE_ZNODE, &znode->flags);
227 if (znode->level != 0) {
229 const int n = zn->child_cnt;
231 /* The children now have new parent */
232 for (i = 0; i < n; i++) {
233 struct ubifs_zbranch *zbr = &zn->zbranch[i];
236 zbr->znode->parent = zn;
240 atomic_long_inc(&c->dirty_zn_cnt);
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
250 * This function updates lprops dirty space and the new size of the index.
252 static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
254 c->calc_idx_sz -= ALIGN(dirt, 8);
255 return ubifs_add_dirt(c, lnum, dirt);
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
263 * Returns dirtied znode on success or negative error code on failure.
265 static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
266 struct ubifs_zbranch *zbr)
268 struct ubifs_znode *znode = zbr->znode;
269 struct ubifs_znode *zn;
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);
285 zn = copy_znode(c, znode);
290 err = insert_old_idx(c, zbr->lnum, zbr->offs);
293 err = add_idx_dirt(c, zbr->lnum, zbr->len);
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
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.
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.
327 static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
332 const struct ubifs_dent_node *dent = node;
334 ubifs_assert(!zbr->leaf);
335 ubifs_assert(zbr->len != 0);
336 ubifs_assert(is_hash_key(c, &zbr->key));
338 err = ubifs_validate_entry(c, dent);
341 dbg_dump_node(c, dent);
345 lnc_node = kmalloc(zbr->len, GFP_NOFS);
347 /* We don't have to have the cache, so no error */
350 memcpy(lnc_node, node, zbr->len);
351 zbr->leaf = lnc_node;
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
361 * This function is similar to 'lnc_add()', but it does not create a copy of
362 * @node but inserts @node to TNC directly.
364 static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
369 ubifs_assert(!zbr->leaf);
370 ubifs_assert(zbr->len != 0);
372 err = ubifs_validate_entry(c, node);
375 dbg_dump_node(c, node);
384 * lnc_free - remove a leaf node from the leaf node cache.
385 * @zbr: zbranch of leaf node
388 static void lnc_free(struct ubifs_zbranch *zbr)
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
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.
407 static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
412 ubifs_assert(is_hash_key(c, &zbr->key));
415 /* Read from the leaf node cache */
416 ubifs_assert(zbr->len != 0);
417 memcpy(node, zbr->leaf, zbr->len);
421 err = ubifs_tnc_read_node(c, zbr, node);
425 /* Add the node to the leaf node cache */
426 err = lnc_add(c, zbr, node);
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
435 * @len: node length (not aligned)
436 * @lnum: LEB number of node to read
437 * @offs: offset of node to read
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.
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
451 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
452 int len, int lnum, int offs)
455 struct ubifs_ch *ch = buf;
456 uint32_t crc, node_crc;
458 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
460 err = ubi_read(c->ubi, lnum, buf, offs, len);
462 ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
463 type, lnum, offs, err);
467 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
470 if (ch->node_type != type)
473 node_len = le32_to_cpu(ch->len);
477 if (type == UBIFS_DATA_NODE && !c->always_chk_crc && c->no_chk_data_crc)
480 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
481 node_crc = le32_to_cpu(ch->crc);
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
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.
498 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
499 struct ubifs_zbranch *zbr, void *node)
503 dbg_tnc("LEB %d:%d, key %s", zbr->lnum, zbr->offs, DBGKEY(key));
505 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
508 union ubifs_key node_key;
509 struct ubifs_dent_node *dent = node;
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)
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));
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
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.
533 static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
534 const struct qstr *nm)
536 struct ubifs_dent_node *dent;
539 /* If possible, match against the dent in the leaf node cache */
541 dent = kmalloc(zbr->len, GFP_NOFS);
545 err = ubifs_tnc_read_node(c, zbr, dent);
549 /* Add the node to the leaf node cache */
550 err = lnc_add_directly(c, zbr, dent);
556 nlen = le16_to_cpu(dent->nlen);
557 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
561 else if (nlen < nm->len)
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
581 * This function returns the znode or a negative error code.
583 static struct ubifs_znode *get_znode(struct ubifs_info *c,
584 struct ubifs_znode *znode, int n)
586 struct ubifs_zbranch *zbr;
588 zbr = &znode->zbranch[n];
592 znode = ubifs_load_znode(c, zbr, znode, n);
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
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.
605 static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
607 struct ubifs_znode *znode = *zn;
611 if (nn < znode->child_cnt) {
616 struct ubifs_znode *zp;
623 if (nn < znode->child_cnt) {
624 znode = get_znode(c, znode, nn);
626 return PTR_ERR(znode);
627 while (znode->level != 0) {
628 znode = get_znode(c, znode, 0);
630 return PTR_ERR(znode);
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
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.
650 static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
652 struct ubifs_znode *znode = *zn;
660 struct ubifs_znode *zp;
668 znode = get_znode(c, znode, nn);
670 return PTR_ERR(znode);
671 while (znode->level != 0) {
672 nn = znode->child_cnt - 1;
673 znode = get_znode(c, znode, nn);
675 return PTR_ERR(znode);
677 nn = znode->child_cnt - 1;
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
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.
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)
708 err = matches_name(c, &(*zn)->zbranch[*n], nm);
709 if (unlikely(err < 0))
711 if (err == NAME_MATCHES)
714 if (err == NAME_GREATER) {
717 err = tnc_prev(c, zn, n);
718 if (err == -ENOENT) {
719 ubifs_assert(*n == 0);
725 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
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.
734 * ----------------------
735 * level 1 | Key0 | Key1 |
736 * -----------------------
738 * znode za | | znode zb
739 * ------------ ------------
740 * level 0 | Key0 | | Key2 |
741 * ------------ ------------
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.
751 * Note, this problem is not relevant for the
752 * case when we go right, because
753 * 'tnc_insert()' would correct the parent key.
755 if (*n == (*zn)->child_cnt - 1) {
756 err = tnc_next(c, zn, n);
758 /* Should be impossible */
764 ubifs_assert(*n == 0);
769 err = matches_name(c, &(*zn)->zbranch[*n], nm);
772 if (err == NAME_LESS)
774 if (err == NAME_MATCHES)
776 ubifs_assert(err == NAME_GREATER);
780 struct ubifs_znode *znode = *zn;
784 err = tnc_next(c, &znode, &nn);
789 if (keys_cmp(c, &znode->zbranch[nn].key, key))
791 err = matches_name(c, &znode->zbranch[nn], nm);
794 if (err == NAME_GREATER)
798 if (err == NAME_MATCHES)
800 ubifs_assert(err == NAME_LESS);
806 * fallible_matches_name - determine if a dent matches a given name.
807 * @c: UBIFS file-system description object
808 * @zbr: zbranch of dent
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.
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.
820 static int fallible_matches_name(struct ubifs_info *c,
821 struct ubifs_zbranch *zbr,
822 const struct qstr *nm)
824 struct ubifs_dent_node *dent;
827 /* If possible, match against the dent in the leaf node cache */
829 dent = kmalloc(zbr->len, GFP_NOFS);
833 err = fallible_read_node(c, &zbr->key, zbr, dent);
837 /* The node was not present */
841 ubifs_assert(err == 1);
843 err = lnc_add_directly(c, zbr, dent);
849 nlen = le16_to_cpu(dent->nlen);
850 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
854 else if (nlen < nm->len)
869 * fallible_resolve_collision - resolve a collision even if nodes are missing.
870 * @c: UBIFS file-system description object
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
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
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.
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)
895 struct ubifs_znode *o_znode = NULL, *znode = *zn;
896 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
898 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
899 if (unlikely(cmp < 0))
901 if (cmp == NAME_MATCHES)
903 if (cmp == NOT_ON_MEDIA) {
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.
913 unsure = 1; /* Remove a dangling branch wherever it is */
915 if (cmp == NAME_GREATER || unsure) {
918 err = tnc_prev(c, zn, n);
919 if (err == -ENOENT) {
920 ubifs_assert(*n == 0);
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);
931 /* Should be impossible */
937 ubifs_assert(*n == 0);
942 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
945 if (err == NAME_MATCHES)
947 if (err == NOT_ON_MEDIA) {
954 if (err == NAME_LESS)
961 if (cmp == NAME_LESS || unsure) {
966 err = tnc_next(c, &znode, &nn);
971 if (keys_cmp(c, &znode->zbranch[nn].key, key))
973 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
976 if (err == NAME_GREATER)
980 if (err == NAME_MATCHES)
982 if (err == NOT_ON_MEDIA) {
989 /* Never match a dangling branch when adding */
990 if (adding || !o_znode)
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));
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
1007 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1009 static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1011 if (zbr->lnum == lnum && zbr->offs == offs)
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
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.
1034 static int resolve_collision_directly(struct ubifs_info *c,
1035 const union ubifs_key *key,
1036 struct ubifs_znode **zn, int *n,
1039 struct ubifs_znode *znode;
1044 if (matches_position(&znode->zbranch[nn], lnum, offs))
1049 err = tnc_prev(c, &znode, &nn);
1054 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1056 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1067 err = tnc_next(c, &znode, &nn);
1072 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1076 if (matches_position(&znode->zbranch[nn], lnum, offs))
1082 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1083 * @c: UBIFS file-system description object
1084 * @znode: znode to dirty
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.
1091 static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1092 struct ubifs_znode *znode)
1094 struct ubifs_znode *zp;
1095 int *path = c->bottom_up_buf, p = 0;
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),
1103 if (!c->bottom_up_buf)
1104 return ERR_PTR(-ENOMEM);
1105 path = c->bottom_up_buf;
1107 if (c->zroot.znode->level) {
1108 /* Go up until parent is dirty */
1116 ubifs_assert(p < c->zroot.znode->level);
1118 if (!zp->cnext && ubifs_zn_dirty(znode))
1124 /* Come back down, dirtying as we go */
1126 struct ubifs_zbranch *zbr;
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);
1135 ubifs_assert(znode == c->zroot.znode);
1136 znode = dirty_cow_znode(c, &c->zroot);
1138 if (IS_ERR(znode) || !p)
1140 ubifs_assert(path[p - 1] >= 0);
1141 ubifs_assert(path[p - 1] < znode->child_cnt);
1142 znode = znode->zbranch[path[p - 1]].znode;
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
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
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
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.
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.
1170 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1171 struct ubifs_znode **zn, int *n)
1174 struct ubifs_znode *znode;
1175 unsigned long time = get_seconds();
1177 dbg_tnc("search key %s", DBGKEY(key));
1179 znode = c->zroot.znode;
1180 if (unlikely(!znode)) {
1181 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1183 return PTR_ERR(znode);
1189 struct ubifs_zbranch *zbr;
1191 exact = ubifs_search_zbranch(c, znode, key, n);
1193 if (znode->level == 0)
1198 zbr = &znode->zbranch[*n];
1206 /* znode is not in TNC cache, load it from the media */
1207 znode = ubifs_load_znode(c, zbr, znode, *n);
1209 return PTR_ERR(znode);
1213 if (exact || !is_hash_key(c, key) || *n != -1) {
1214 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
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:
1225 * | 3 | 5 | | 6 | 7 | (x)
1227 * Or more a complex example:
1231 * | 1 | 3 | | 5 | 8 |
1233 * | 5 | 5 | | 6 | 7 | (x)
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
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
1247 * | 3 | 5 | | 5 | 6 | 7 |
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.
1261 err = tnc_prev(c, &znode, n);
1262 if (err == -ENOENT) {
1263 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1267 if (unlikely(err < 0))
1269 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1270 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1275 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
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
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
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
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.
1298 * Additionally all znodes in the path from the root to the located zero-level
1299 * znode are marked as dirty.
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.
1305 static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1306 struct ubifs_znode **zn, int *n)
1309 struct ubifs_znode *znode;
1310 unsigned long time = get_seconds();
1312 dbg_tnc("search and dirty key %s", DBGKEY(key));
1314 znode = c->zroot.znode;
1315 if (unlikely(!znode)) {
1316 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1318 return PTR_ERR(znode);
1321 znode = dirty_cow_znode(c, &c->zroot);
1323 return PTR_ERR(znode);
1328 struct ubifs_zbranch *zbr;
1330 exact = ubifs_search_zbranch(c, znode, key, n);
1332 if (znode->level == 0)
1337 zbr = &znode->zbranch[*n];
1341 znode = dirty_cow_znode(c, zbr);
1343 return PTR_ERR(znode);
1347 /* znode is not in TNC cache, load it from the media */
1348 znode = ubifs_load_znode(c, zbr, znode, *n);
1350 return PTR_ERR(znode);
1351 znode = dirty_cow_znode(c, zbr);
1353 return PTR_ERR(znode);
1357 if (exact || !is_hash_key(c, key) || *n != -1) {
1358 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1363 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1366 err = tnc_prev(c, &znode, n);
1367 if (err == -ENOENT) {
1369 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1372 if (unlikely(err < 0))
1374 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1376 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1380 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1381 znode = dirty_cow_bottom_up(c, znode);
1383 return PTR_ERR(znode);
1386 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1392 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1393 * @c: UBIFS file-system description object
1395 * @gc_seq1: garbage collection sequence number
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
1401 static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1404 * No garbage collection in the read-only U-Boot implementation
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
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.
1422 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1423 void *node, int *lnum, int *offs)
1425 int found, n, err, safely = 0, gc_seq1;
1426 struct ubifs_znode *znode;
1427 struct ubifs_zbranch zbr, *zt;
1430 mutex_lock(&c->tnc_mutex);
1431 found = ubifs_lookup_level0(c, key, &znode, &n);
1435 } else if (found < 0) {
1439 zt = &znode->zbranch[n];
1444 if (is_hash_key(c, key)) {
1446 * In this case the leaf node cache gets used, so we pass the
1447 * address of the zbranch and keep the mutex locked
1449 err = tnc_read_node_nm(c, zt, node);
1453 err = ubifs_tnc_read_node(c, zt, node);
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);
1461 err = fallible_read_node(c, key, &zbr, node);
1462 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1464 * The node may have been GC'ed out from under us so try again
1465 * while keeping the TNC mutex locked.
1473 mutex_unlock(&c->tnc_mutex);
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
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.
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.
1490 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
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;
1501 mutex_lock(&c->tnc_mutex);
1502 /* Find first key */
1503 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1508 len = znode->zbranch[n].len;
1509 /* The buffer must be big enough for at least 1 node */
1510 if (len > bu->buf_len) {
1515 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1517 lnum = znode->zbranch[n].lnum;
1518 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1521 struct ubifs_zbranch *zbr;
1522 union ubifs_key *key;
1523 unsigned int next_block;
1526 err = tnc_next(c, &znode, &n);
1529 zbr = &znode->zbranch[n];
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) {
1538 /* First key found */
1540 offs = ALIGN(zbr->offs + zbr->len, 8);
1542 if (len > bu->buf_len) {
1548 * The data nodes must be in consecutive positions in
1551 if (zbr->lnum != lnum || zbr->offs != offs)
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)
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)
1566 bu->zbranch[bu->cnt++] = *zbr;
1568 /* See if we have room for more */
1569 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1571 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1575 if (err == -ENOENT) {
1579 bu->gc_seq = c->gc_seq;
1580 mutex_unlock(&c->tnc_mutex);
1584 * An enormous hole could cause bulk-read to encompass too many
1585 * page cache pages, so limit the number here.
1587 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1588 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1590 * Ensure that bulk-read covers a whole number of page cache
1593 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1594 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1597 /* At the end of file we can round up */
1598 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
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);
1605 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
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
1618 * This functions returns %0 on success or a negative error code on failure.
1620 static int validate_data_node(struct ubifs_info *c, void *buf,
1621 struct ubifs_zbranch *zbr)
1623 union ubifs_key key1;
1624 struct ubifs_ch *ch = buf;
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);
1633 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1635 ubifs_err("expected node type %d", UBIFS_DATA_NODE);
1639 len = le32_to_cpu(ch->len);
1640 if (len != zbr->len) {
1641 ubifs_err("bad node length %d, expected %d", len, zbr->len);
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));
1660 ubifs_err("bad node at LEB %d:%d", zbr->lnum, zbr->offs);
1661 dbg_dump_node(c, buf);
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
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
1676 int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1678 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
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);
1689 err = ubi_read(c->ubi, lnum, bu->buf, offs, len);
1691 /* Check for a race with GC */
1692 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1695 if (err && err != -EBADMSG) {
1696 ubifs_err("failed to read from LEB %d:%d, error %d",
1699 dbg_tnc("key %s", DBGKEY(&bu->key));
1703 /* Validate the nodes read */
1705 for (i = 0; i < bu->cnt; i++) {
1706 err = validate_data_node(c, buf, &bu->zbranch[i]);
1709 buf = buf + ALIGN(bu->zbranch[i].len, 8);
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
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.
1728 static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1729 void *node, const struct qstr *nm)
1732 struct ubifs_znode *znode;
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);
1740 } else if (found < 0) {
1745 ubifs_assert(n >= 0);
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))
1756 err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1759 mutex_unlock(&c->tnc_mutex);
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
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.
1776 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1777 void *node, const struct qstr *nm)
1780 const struct ubifs_dent_node *dent = node;
1783 * We assume that in most of the cases there are no name collisions and
1784 * 'ubifs_tnc_lookup()' returns us the right direntry.
1786 err = ubifs_tnc_lookup(c, key, node);
1790 len = le16_to_cpu(dent->nlen);
1791 if (nm->len == len && !memcmp(dent->name, nm->name, len))
1795 * Unluckily, there are hash collisions and we have to iterate over
1796 * them look at each direntry with colliding name hash sequentially.
1798 return do_lookup_nm(c, key, node, nm);
1802 * correct_parent_keys - correct parent znodes' keys.
1803 * @c: UBIFS file-system description object
1804 * @znode: znode to correct parent znodes for
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.
1810 static void correct_parent_keys(const struct ubifs_info *c,
1811 struct ubifs_znode *znode)
1813 union ubifs_key *key, *key1;
1815 ubifs_assert(znode->parent);
1816 ubifs_assert(znode->iip == 0);
1818 key = &znode->zbranch[0].key;
1819 key1 = &znode->parent->zbranch[0].key;
1821 while (keys_cmp(c, key, key1) < 0) {
1822 key_copy(c, key, key1);
1823 znode = znode->parent;
1825 if (!znode->parent || znode->iip)
1827 key1 = &znode->parent->zbranch[0].key;
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
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.
1842 static void insert_zbranch(struct ubifs_znode *znode,
1843 const struct ubifs_zbranch *zbr, int n)
1847 ubifs_assert(ubifs_zn_dirty(znode));
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;
1856 zbr->znode->iip = n;
1858 for (i = znode->child_cnt; i > n; i--)
1859 znode->zbranch[i] = znode->zbranch[i - 1];
1861 znode->zbranch[n] = *zbr;
1862 znode->child_cnt += 1;
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.
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.
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
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.
1894 static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1895 struct ubifs_zbranch *zbr, int n)
1897 struct ubifs_znode *zn, *zi, *zp;
1898 int i, keep, move, appending = 0;
1899 union ubifs_key *key = &zbr->key, *key1;
1901 ubifs_assert(n >= 0 && n <= c->fanout);
1903 /* Implement naive insert for now */
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,
1911 insert_zbranch(znode, zbr, n);
1913 /* Ensure parent's key is correct */
1914 if (n == 0 && zp && znode->iip == 0)
1915 correct_parent_keys(c, znode);
1921 * Unfortunately, @znode does not have more empty slots and we have to
1924 dbg_tnc("splitting level %d, key %s", znode->level, DBGKEY(key));
1928 * We can no longer be sure of finding this znode by key, so we
1929 * record it in the old_idx tree.
1931 ins_clr_old_idx_znode(c, znode);
1933 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
1937 zn->level = znode->level;
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)
1949 } else if (appending && n != c->fanout) {
1950 /* Try not to split consecutive data keys */
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) {
1961 move = c->fanout - keep;
1973 keep = (c->fanout + 1) / 2;
1974 move = c->fanout - keep;
1978 * Although we don't at present, we could look at the neighbors and see
1979 * if we can move some zbranches there.
1983 /* Insert into existing znode */
1988 /* Insert into new znode */
1993 zbr->znode->parent = zn;
1998 __set_bit(DIRTY_ZNODE, &zn->flags);
1999 atomic_long_inc(&c->dirty_zn_cnt);
2001 zn->child_cnt = move;
2002 znode->child_cnt = keep;
2004 dbg_tnc("moving %d, keeping %d", move, keep);
2007 for (i = 0; i < move; i++) {
2008 zn->zbranch[i] = znode->zbranch[keep + i];
2011 if (zn->zbranch[i].znode) {
2012 zn->zbranch[i].znode->parent = zn;
2013 zn->zbranch[i].znode->iip = i;
2017 /* Insert new key and branch */
2018 dbg_tnc("inserting at %d level %d, key %s", n, zn->level, DBGKEY(key));
2020 insert_zbranch(zi, zbr, n);
2022 /* Insert new znode (produced by spitting) into the parent */
2024 if (n == 0 && zi == znode && znode->iip == 0)
2025 correct_parent_keys(c, znode);
2027 /* Locate insertion point */
2030 /* Tail recursion */
2031 zbr->key = zn->zbranch[0].key;
2041 /* We have to split root znode */
2042 dbg_tnc("creating new zroot at level %d", znode->level + 1);
2044 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2049 zi->level = znode->level + 1;
2051 __set_bit(DIRTY_ZNODE, &zi->flags);
2052 atomic_long_inc(&c->dirty_zn_cnt);
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;
2065 c->zroot.znode = zi;
2076 * ubifs_tnc_add - add a node to TNC.
2077 * @c: UBIFS file-system description object
2079 * @lnum: LEB number of node
2080 * @offs: node offset
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
2087 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2090 int found, n, err = 0;
2091 struct ubifs_znode *znode;
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);
2097 struct ubifs_zbranch zbr;
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];
2109 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2116 err = dbg_check_tnc(c, 0);
2117 mutex_unlock(&c->tnc_mutex);
2123 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2124 * @c: UBIFS file-system description object
2126 * @old_lnum: LEB number of old node
2127 * @old_offs: old node offset
2128 * @lnum: LEB number of node
2129 * @offs: node offset
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.
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)
2139 int found, n, err = 0;
2140 struct ubifs_znode *znode;
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);
2152 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2155 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2157 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
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);
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);
2183 zbr = &znode->zbranch[n];
2185 err = ubifs_add_dirt(c, zbr->lnum,
2197 err = ubifs_add_dirt(c, lnum, len);
2200 err = dbg_check_tnc(c, 0);
2203 mutex_unlock(&c->tnc_mutex);
2208 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2209 * @c: UBIFS file-system description object
2211 * @lnum: LEB number of node
2212 * @offs: node offset
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.
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)
2222 int found, n, err = 0;
2223 struct ubifs_znode *znode;
2225 mutex_lock(&c->tnc_mutex);
2226 dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum, offs, nm->len, nm->name,
2228 found = lookup_level0_dirty(c, key, &znode, &n);
2236 found = fallible_resolve_collision(c, key, &znode, &n,
2239 found = resolve_collision(c, key, &znode, &n, nm);
2240 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
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);
2256 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2259 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2268 struct ubifs_zbranch zbr;
2274 key_copy(c, key, &zbr.key);
2275 err = tnc_insert(c, znode, &zbr, n + 1);
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.
2285 struct qstr noname = { .len = 0, .name = "" };
2287 err = dbg_check_tnc(c, 0);
2288 mutex_unlock(&c->tnc_mutex);
2291 return ubifs_tnc_remove_nm(c, key, &noname);
2297 err = dbg_check_tnc(c, 0);
2298 mutex_unlock(&c->tnc_mutex);
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
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.
2311 static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2313 struct ubifs_zbranch *zbr;
2314 struct ubifs_znode *zp;
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));
2322 zbr = &znode->zbranch[n];
2325 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2327 dbg_dump_znode(c, znode);
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;
2336 if (znode->child_cnt > 0)
2340 * This was the last zbranch, we have to delete this znode from the
2345 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
2346 ubifs_assert(ubifs_zn_dirty(znode));
2351 atomic_long_dec(&c->dirty_zn_cnt);
2353 err = insert_old_idx_znode(c, znode);
2358 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2359 atomic_long_inc(&c->clean_zn_cnt);
2360 atomic_long_inc(&ubifs_clean_zn_cnt);
2364 } while (znode->child_cnt == 1); /* while removing last child */
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;
2376 * If this is the root and it has only 1 child then
2377 * collapse the tree.
2379 if (!znode->parent) {
2380 while (znode->child_cnt == 1 && znode->level != 0) {
2382 zbr = &znode->zbranch[0];
2383 znode = get_znode(c, znode, 0);
2385 return PTR_ERR(znode);
2386 znode = dirty_cow_znode(c, zbr);
2388 return PTR_ERR(znode);
2389 znode->parent = NULL;
2392 err = insert_old_idx(c, c->zroot.lnum,
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,
2403 ubifs_assert(test_bit(DIRTY_ZNODE, &zp->flags));
2404 atomic_long_dec(&c->dirty_zn_cnt);
2407 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2408 atomic_long_inc(&c->clean_zn_cnt);
2409 atomic_long_inc(&ubifs_clean_zn_cnt);
2419 * ubifs_tnc_remove - remove an index entry of a node.
2420 * @c: UBIFS file-system description object
2423 * Returns %0 on success or negative error code on failure.
2425 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2427 int found, n, err = 0;
2428 struct ubifs_znode *znode;
2430 mutex_lock(&c->tnc_mutex);
2431 dbg_tnc("key %s", DBGKEY(key));
2432 found = lookup_level0_dirty(c, key, &znode, &n);
2438 err = tnc_delete(c, znode, n);
2440 err = dbg_check_tnc(c, 0);
2443 mutex_unlock(&c->tnc_mutex);
2448 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2449 * @c: UBIFS file-system description object
2451 * @nm: directory entry name
2453 * Returns %0 on success or negative error code on failure.
2455 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2456 const struct qstr *nm)
2459 struct ubifs_znode *znode;
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);
2469 err = fallible_resolve_collision(c, key, &znode, &n,
2472 err = resolve_collision(c, key, &znode, &n, nm);
2473 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
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);
2485 err = tnc_delete(c, znode, n);
2491 err = dbg_check_tnc(c, 0);
2492 mutex_unlock(&c->tnc_mutex);
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
2503 * This function returns %1 if the key is in range and %0 otherwise.
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)
2508 if (keys_cmp(c, key, from_key) < 0)
2510 if (keys_cmp(c, key, to_key) > 0)
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
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.
2525 int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2526 union ubifs_key *to_key)
2528 int i, n, k, err = 0;
2529 struct ubifs_znode *znode;
2530 union ubifs_key *key;
2532 mutex_lock(&c->tnc_mutex);
2534 /* Find first level 0 znode that contains keys to remove */
2535 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2542 err = tnc_next(c, &znode, &n);
2543 if (err == -ENOENT) {
2549 key = &znode->zbranch[n].key;
2550 if (!key_in_range(c, key, from_key, to_key)) {
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);
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))
2570 lnc_free(&znode->zbranch[i]);
2571 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2572 znode->zbranch[i].len);
2574 dbg_dump_znode(c, znode);
2577 dbg_tnc("removing %s", DBGKEY(key));
2580 for (i = n + 1 + k; i < znode->child_cnt; i++)
2581 znode->zbranch[i - k] = znode->zbranch[i];
2582 znode->child_cnt -= k;
2585 /* Now delete the first */
2586 err = tnc_delete(c, znode, n);
2593 err = dbg_check_tnc(c, 0);
2594 mutex_unlock(&c->tnc_mutex);
2599 * ubifs_tnc_remove_ino - remove an inode from TNC.
2600 * @c: UBIFS file-system description object
2601 * @inum: inode number to remove
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.
2607 int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2609 union ubifs_key key1, key2;
2610 struct ubifs_dent_node *xent, *pxent = NULL;
2611 struct qstr nm = { .name = NULL };
2613 dbg_tnc("ino %lu", (unsigned long)inum);
2616 * Walk all extended attribute entries and remove them together with
2617 * corresponding extended attribute inodes.
2619 lowest_xent_key(c, &key1, inum);
2624 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2626 err = PTR_ERR(xent);
2632 xattr_inum = le64_to_cpu(xent->inum);
2633 dbg_tnc("xent '%s', ino %lu", xent->name,
2634 (unsigned long)xattr_inum);
2636 nm.name = (char *)xent->name;
2637 nm.len = le16_to_cpu(xent->nlen);
2638 err = ubifs_tnc_remove_nm(c, &key1, &nm);
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);
2654 key_read(c, &xent->key, &key1);
2658 lowest_ino_key(c, &key1, inum);
2659 highest_ino_key(c, &key2, inum);
2661 return ubifs_tnc_remove_range(c, &key1, &key2);
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
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
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
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.
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.
2687 struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2688 union ubifs_key *key,
2689 const struct qstr *nm)
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;
2697 dbg_tnc("%s %s", nm->name ? (char *)nm->name : "(lowest)", DBGKEY(key));
2698 ubifs_assert(is_hash_key(c, key));
2700 mutex_lock(&c->tnc_mutex);
2701 err = ubifs_lookup_level0(c, key, &znode, &n);
2702 if (unlikely(err < 0))
2707 /* Handle collisions */
2708 err = resolve_collision(c, key, &znode, &n, nm);
2709 dbg_tnc("rc returned %d, znode %p, n %d",
2711 if (unlikely(err < 0))
2715 /* Now find next entry */
2716 err = tnc_next(c, &znode, &n);
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.
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.
2731 err = tnc_next(c, &znode, &n);
2737 zbr = &znode->zbranch[n];
2738 dent = kmalloc(zbr->len, GFP_NOFS);
2739 if (unlikely(!dent)) {
2745 * The above 'tnc_next()' call could lead us to the next inode, check
2749 if (key_inum(c, dkey) != key_inum(c, key) ||
2750 key_type(c, dkey) != type) {
2755 err = tnc_read_node_nm(c, zbr, dent);
2759 mutex_unlock(&c->tnc_mutex);
2765 mutex_unlock(&c->tnc_mutex);
2766 return ERR_PTR(err);