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>
38 * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
39 * @NAME_LESS: name corresponding to the first argument is less than second
40 * @NAME_MATCHES: names match
41 * @NAME_GREATER: name corresponding to the second argument is greater than
43 * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
45 * These constants were introduce to improve readability.
55 * insert_old_idx - record an index node obsoleted since the last commit start.
56 * @c: UBIFS file-system description object
57 * @lnum: LEB number of obsoleted index node
58 * @offs: offset of obsoleted index node
60 * Returns %0 on success, and a negative error code on failure.
62 * For recovery, there must always be a complete intact version of the index on
63 * flash at all times. That is called the "old index". It is the index as at the
64 * time of the last successful commit. Many of the index nodes in the old index
65 * may be dirty, but they must not be erased until the next successful commit
66 * (at which point that index becomes the old index).
68 * That means that the garbage collection and the in-the-gaps method of
69 * committing must be able to determine if an index node is in the old index.
70 * Most of the old index nodes can be found by looking up the TNC using the
71 * 'lookup_znode()' function. However, some of the old index nodes may have
72 * been deleted from the current index or may have been changed so much that
73 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
74 * That is what this function does. The RB-tree is ordered by LEB number and
75 * offset because they uniquely identify the old index node.
77 static int insert_old_idx(struct ubifs_info
*c
, int lnum
, int offs
)
79 struct ubifs_old_idx
*old_idx
, *o
;
80 struct rb_node
**p
, *parent
= NULL
;
82 old_idx
= kmalloc(sizeof(struct ubifs_old_idx
), GFP_NOFS
);
83 if (unlikely(!old_idx
))
88 p
= &c
->old_idx
.rb_node
;
91 o
= rb_entry(parent
, struct ubifs_old_idx
, rb
);
94 else if (lnum
> o
->lnum
)
96 else if (offs
< o
->offs
)
98 else if (offs
> o
->offs
)
101 ubifs_err("old idx added twice!");
106 rb_link_node(&old_idx
->rb
, parent
, p
);
107 rb_insert_color(&old_idx
->rb
, &c
->old_idx
);
112 * insert_old_idx_znode - record a znode obsoleted since last commit start.
113 * @c: UBIFS file-system description object
114 * @znode: znode of obsoleted index node
116 * Returns %0 on success, and a negative error code on failure.
118 int insert_old_idx_znode(struct ubifs_info
*c
, struct ubifs_znode
*znode
)
121 struct ubifs_zbranch
*zbr
;
123 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
125 return insert_old_idx(c
, zbr
->lnum
, zbr
->offs
);
128 return insert_old_idx(c
, c
->zroot
.lnum
,
134 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
135 * @c: UBIFS file-system description object
136 * @znode: znode of obsoleted index node
138 * Returns %0 on success, and a negative error code on failure.
140 static int ins_clr_old_idx_znode(struct ubifs_info
*c
,
141 struct ubifs_znode
*znode
)
146 struct ubifs_zbranch
*zbr
;
148 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
150 err
= insert_old_idx(c
, zbr
->lnum
, zbr
->offs
);
159 err
= insert_old_idx(c
, c
->zroot
.lnum
, c
->zroot
.offs
);
170 * destroy_old_idx - destroy the old_idx RB-tree.
171 * @c: UBIFS file-system description object
173 * During start commit, the old_idx RB-tree is used to avoid overwriting index
174 * nodes that were in the index last commit but have since been deleted. This
175 * is necessary for recovery i.e. the old index must be kept intact until the
176 * new index is successfully written. The old-idx RB-tree is used for the
177 * in-the-gaps method of writing index nodes and is destroyed every commit.
179 void destroy_old_idx(struct ubifs_info
*c
)
181 struct rb_node
*this = c
->old_idx
.rb_node
;
182 struct ubifs_old_idx
*old_idx
;
186 this = this->rb_left
;
188 } else if (this->rb_right
) {
189 this = this->rb_right
;
192 old_idx
= rb_entry(this, struct ubifs_old_idx
, rb
);
193 this = rb_parent(this);
195 if (this->rb_left
== &old_idx
->rb
)
196 this->rb_left
= NULL
;
198 this->rb_right
= NULL
;
202 c
->old_idx
= RB_ROOT
;
206 * copy_znode - copy a dirty znode.
207 * @c: UBIFS file-system description object
208 * @znode: znode to copy
210 * A dirty znode being committed may not be changed, so it is copied.
212 static struct ubifs_znode
*copy_znode(struct ubifs_info
*c
,
213 struct ubifs_znode
*znode
)
215 struct ubifs_znode
*zn
;
217 zn
= kmalloc(c
->max_znode_sz
, GFP_NOFS
);
219 return ERR_PTR(-ENOMEM
);
221 memcpy(zn
, znode
, c
->max_znode_sz
);
223 __set_bit(DIRTY_ZNODE
, &zn
->flags
);
224 __clear_bit(COW_ZNODE
, &zn
->flags
);
226 ubifs_assert(!test_bit(OBSOLETE_ZNODE
, &znode
->flags
));
227 __set_bit(OBSOLETE_ZNODE
, &znode
->flags
);
229 if (znode
->level
!= 0) {
231 const int n
= zn
->child_cnt
;
233 /* The children now have new parent */
234 for (i
= 0; i
< n
; i
++) {
235 struct ubifs_zbranch
*zbr
= &zn
->zbranch
[i
];
238 zbr
->znode
->parent
= zn
;
242 atomic_long_inc(&c
->dirty_zn_cnt
);
247 * add_idx_dirt - add dirt due to a dirty znode.
248 * @c: UBIFS file-system description object
249 * @lnum: LEB number of index node
250 * @dirt: size of index node
252 * This function updates lprops dirty space and the new size of the index.
254 static int add_idx_dirt(struct ubifs_info
*c
, int lnum
, int dirt
)
256 c
->calc_idx_sz
-= ALIGN(dirt
, 8);
257 return ubifs_add_dirt(c
, lnum
, dirt
);
261 * dirty_cow_znode - ensure a znode is not being committed.
262 * @c: UBIFS file-system description object
263 * @zbr: branch of znode to check
265 * Returns dirtied znode on success or negative error code on failure.
267 static struct ubifs_znode
*dirty_cow_znode(struct ubifs_info
*c
,
268 struct ubifs_zbranch
*zbr
)
270 struct ubifs_znode
*znode
= zbr
->znode
;
271 struct ubifs_znode
*zn
;
274 if (!test_bit(COW_ZNODE
, &znode
->flags
)) {
275 /* znode is not being committed */
276 if (!test_and_set_bit(DIRTY_ZNODE
, &znode
->flags
)) {
277 atomic_long_inc(&c
->dirty_zn_cnt
);
278 atomic_long_dec(&c
->clean_zn_cnt
);
279 atomic_long_dec(&ubifs_clean_zn_cnt
);
280 err
= add_idx_dirt(c
, zbr
->lnum
, zbr
->len
);
287 zn
= copy_znode(c
, znode
);
292 err
= insert_old_idx(c
, zbr
->lnum
, zbr
->offs
);
295 err
= add_idx_dirt(c
, zbr
->lnum
, zbr
->len
);
310 * lnc_add - add a leaf node to the leaf node cache.
311 * @c: UBIFS file-system description object
312 * @zbr: zbranch of leaf node
315 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
316 * purpose of the leaf node cache is to save re-reading the same leaf node over
317 * and over again. Most things are cached by VFS, however the file system must
318 * cache directory entries for readdir and for resolving hash collisions. The
319 * present implementation of the leaf node cache is extremely simple, and
320 * allows for error returns that are not used but that may be needed if a more
321 * complex implementation is created.
323 * Note, this function does not add the @node object to LNC directly, but
324 * allocates a copy of the object and adds the copy to LNC. The reason for this
325 * is that @node has been allocated outside of the TNC subsystem and will be
326 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
327 * may be changed at any time, e.g. freed by the shrinker.
329 static int lnc_add(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
334 const struct ubifs_dent_node
*dent
= node
;
336 ubifs_assert(!zbr
->leaf
);
337 ubifs_assert(zbr
->len
!= 0);
338 ubifs_assert(is_hash_key(c
, &zbr
->key
));
340 err
= ubifs_validate_entry(c
, dent
);
343 dbg_dump_node(c
, dent
);
347 lnc_node
= kmalloc(zbr
->len
, GFP_NOFS
);
349 /* We don't have to have the cache, so no error */
352 memcpy(lnc_node
, node
, zbr
->len
);
353 zbr
->leaf
= lnc_node
;
358 * lnc_add_directly - add a leaf node to the leaf-node-cache.
359 * @c: UBIFS file-system description object
360 * @zbr: zbranch of leaf node
363 * This function is similar to 'lnc_add()', but it does not create a copy of
364 * @node but inserts @node to TNC directly.
366 static int lnc_add_directly(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
371 ubifs_assert(!zbr
->leaf
);
372 ubifs_assert(zbr
->len
!= 0);
374 err
= ubifs_validate_entry(c
, node
);
377 dbg_dump_node(c
, node
);
386 * lnc_free - remove a leaf node from the leaf node cache.
387 * @zbr: zbranch of leaf node
390 static void lnc_free(struct ubifs_zbranch
*zbr
)
399 * tnc_read_node_nm - read a "hashed" leaf node.
400 * @c: UBIFS file-system description object
401 * @zbr: key and position of the node
402 * @node: node is returned here
404 * This function reads a "hashed" node defined by @zbr from the leaf node cache
405 * (in it is there) or from the hash media, in which case the node is also
406 * added to LNC. Returns zero in case of success or a negative negative error
407 * code in case of failure.
409 static int tnc_read_node_nm(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
414 ubifs_assert(is_hash_key(c
, &zbr
->key
));
417 /* Read from the leaf node cache */
418 ubifs_assert(zbr
->len
!= 0);
419 memcpy(node
, zbr
->leaf
, zbr
->len
);
423 err
= ubifs_tnc_read_node(c
, zbr
, node
);
427 /* Add the node to the leaf node cache */
428 err
= lnc_add(c
, zbr
, node
);
433 * try_read_node - read a node if it is a node.
434 * @c: UBIFS file-system description object
435 * @buf: buffer to read to
437 * @len: node length (not aligned)
438 * @lnum: LEB number of node to read
439 * @offs: offset of node to read
441 * This function tries to read a node of known type and length, checks it and
442 * stores it in @buf. This function returns %1 if a node is present and %0 if
443 * a node is not present. A negative error code is returned for I/O errors.
444 * This function performs that same function as ubifs_read_node except that
445 * it does not require that there is actually a node present and instead
446 * the return code indicates if a node was read.
448 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
449 * is true (it is controlled by corresponding mount option). However, if
450 * @c->always_chk_crc is true, @c->no_chk_data_crc is ignored and CRC is always
453 static int try_read_node(const struct ubifs_info
*c
, void *buf
, int type
,
454 int len
, int lnum
, int offs
)
457 struct ubifs_ch
*ch
= buf
;
458 uint32_t crc
, node_crc
;
460 dbg_io("LEB %d:%d, %s, length %d", lnum
, offs
, dbg_ntype(type
), len
);
462 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, len
);
464 ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
465 type
, lnum
, offs
, err
);
469 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
)
472 if (ch
->node_type
!= type
)
475 node_len
= le32_to_cpu(ch
->len
);
479 if (type
== UBIFS_DATA_NODE
&& !c
->always_chk_crc
&& c
->no_chk_data_crc
)
482 crc
= crc32(UBIFS_CRC32_INIT
, buf
+ 8, node_len
- 8);
483 node_crc
= le32_to_cpu(ch
->crc
);
491 * fallible_read_node - try to read a leaf node.
492 * @c: UBIFS file-system description object
493 * @key: key of node to read
494 * @zbr: position of node
495 * @node: node returned
497 * This function tries to read a node and returns %1 if the node is read, %0
498 * if the node is not present, and a negative error code in the case of error.
500 static int fallible_read_node(struct ubifs_info
*c
, const union ubifs_key
*key
,
501 struct ubifs_zbranch
*zbr
, void *node
)
505 dbg_tnc("LEB %d:%d, key %s", zbr
->lnum
, zbr
->offs
, DBGKEY(key
));
507 ret
= try_read_node(c
, node
, key_type(c
, key
), zbr
->len
, zbr
->lnum
,
510 union ubifs_key node_key
;
511 struct ubifs_dent_node
*dent
= node
;
513 /* All nodes have key in the same place */
514 key_read(c
, &dent
->key
, &node_key
);
515 if (keys_cmp(c
, key
, &node_key
) != 0)
518 if (ret
== 0 && c
->replaying
)
519 dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
520 zbr
->lnum
, zbr
->offs
, zbr
->len
, DBGKEY(key
));
525 * matches_name - determine if a direntry or xattr entry matches a given name.
526 * @c: UBIFS file-system description object
527 * @zbr: zbranch of dent
530 * This function checks if xentry/direntry referred by zbranch @zbr matches name
531 * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
532 * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
533 * of failure, a negative error code is returned.
535 static int matches_name(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
536 const struct qstr
*nm
)
538 struct ubifs_dent_node
*dent
;
541 /* If possible, match against the dent in the leaf node cache */
543 dent
= kmalloc(zbr
->len
, GFP_NOFS
);
547 err
= ubifs_tnc_read_node(c
, zbr
, dent
);
551 /* Add the node to the leaf node cache */
552 err
= lnc_add_directly(c
, zbr
, dent
);
558 nlen
= le16_to_cpu(dent
->nlen
);
559 err
= memcmp(dent
->name
, nm
->name
, min_t(int, nlen
, nm
->len
));
563 else if (nlen
< nm
->len
)
578 * get_znode - get a TNC znode that may not be loaded yet.
579 * @c: UBIFS file-system description object
580 * @znode: parent znode
581 * @n: znode branch slot number
583 * This function returns the znode or a negative error code.
585 static struct ubifs_znode
*get_znode(struct ubifs_info
*c
,
586 struct ubifs_znode
*znode
, int n
)
588 struct ubifs_zbranch
*zbr
;
590 zbr
= &znode
->zbranch
[n
];
594 znode
= ubifs_load_znode(c
, zbr
, znode
, n
);
599 * tnc_next - find next TNC entry.
600 * @c: UBIFS file-system description object
601 * @zn: znode is passed and returned here
602 * @n: znode branch slot number is passed and returned here
604 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
605 * no next entry, or a negative error code otherwise.
607 static int tnc_next(struct ubifs_info
*c
, struct ubifs_znode
**zn
, int *n
)
609 struct ubifs_znode
*znode
= *zn
;
613 if (nn
< znode
->child_cnt
) {
618 struct ubifs_znode
*zp
;
625 if (nn
< znode
->child_cnt
) {
626 znode
= get_znode(c
, znode
, nn
);
628 return PTR_ERR(znode
);
629 while (znode
->level
!= 0) {
630 znode
= get_znode(c
, znode
, 0);
632 return PTR_ERR(znode
);
644 * tnc_prev - find previous TNC entry.
645 * @c: UBIFS file-system description object
646 * @zn: znode is returned here
647 * @n: znode branch slot number is passed and returned here
649 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
650 * there is no next entry, or a negative error code otherwise.
652 static int tnc_prev(struct ubifs_info
*c
, struct ubifs_znode
**zn
, int *n
)
654 struct ubifs_znode
*znode
= *zn
;
662 struct ubifs_znode
*zp
;
670 znode
= get_znode(c
, znode
, nn
);
672 return PTR_ERR(znode
);
673 while (znode
->level
!= 0) {
674 nn
= znode
->child_cnt
- 1;
675 znode
= get_znode(c
, znode
, nn
);
677 return PTR_ERR(znode
);
679 nn
= znode
->child_cnt
- 1;
689 * resolve_collision - resolve a collision.
690 * @c: UBIFS file-system description object
691 * @key: key of a directory or extended attribute entry
692 * @zn: znode is returned here
693 * @n: zbranch number is passed and returned here
694 * @nm: name of the entry
696 * This function is called for "hashed" keys to make sure that the found key
697 * really corresponds to the looked up node (directory or extended attribute
698 * entry). It returns %1 and sets @zn and @n if the collision is resolved.
699 * %0 is returned if @nm is not found and @zn and @n are set to the previous
700 * entry, i.e. to the entry after which @nm could follow if it were in TNC.
701 * This means that @n may be set to %-1 if the leftmost key in @zn is the
702 * previous one. A negative error code is returned on failures.
704 static int resolve_collision(struct ubifs_info
*c
, const union ubifs_key
*key
,
705 struct ubifs_znode
**zn
, int *n
,
706 const struct qstr
*nm
)
710 err
= matches_name(c
, &(*zn
)->zbranch
[*n
], nm
);
711 if (unlikely(err
< 0))
713 if (err
== NAME_MATCHES
)
716 if (err
== NAME_GREATER
) {
719 err
= tnc_prev(c
, zn
, n
);
720 if (err
== -ENOENT
) {
721 ubifs_assert(*n
== 0);
727 if (keys_cmp(c
, &(*zn
)->zbranch
[*n
].key
, key
)) {
729 * We have found the branch after which we would
730 * like to insert, but inserting in this znode
731 * may still be wrong. Consider the following 3
732 * znodes, in the case where we are resolving a
733 * collision with Key2.
736 * ----------------------
737 * level 1 | Key0 | Key1 |
738 * -----------------------
740 * znode za | | znode zb
741 * ------------ ------------
742 * level 0 | Key0 | | Key2 |
743 * ------------ ------------
745 * The lookup finds Key2 in znode zb. Lets say
746 * there is no match and the name is greater so
747 * we look left. When we find Key0, we end up
748 * here. If we return now, we will insert into
749 * znode za at slot n = 1. But that is invalid
750 * according to the parent's keys. Key2 must
751 * be inserted into znode zb.
753 * Note, this problem is not relevant for the
754 * case when we go right, because
755 * 'tnc_insert()' would correct the parent key.
757 if (*n
== (*zn
)->child_cnt
- 1) {
758 err
= tnc_next(c
, zn
, n
);
760 /* Should be impossible */
766 ubifs_assert(*n
== 0);
771 err
= matches_name(c
, &(*zn
)->zbranch
[*n
], nm
);
774 if (err
== NAME_LESS
)
776 if (err
== NAME_MATCHES
)
778 ubifs_assert(err
== NAME_GREATER
);
782 struct ubifs_znode
*znode
= *zn
;
786 err
= tnc_next(c
, &znode
, &nn
);
791 if (keys_cmp(c
, &znode
->zbranch
[nn
].key
, key
))
793 err
= matches_name(c
, &znode
->zbranch
[nn
], nm
);
796 if (err
== NAME_GREATER
)
800 if (err
== NAME_MATCHES
)
802 ubifs_assert(err
== NAME_LESS
);
808 * fallible_matches_name - determine if a dent matches a given name.
809 * @c: UBIFS file-system description object
810 * @zbr: zbranch of dent
813 * This is a "fallible" version of 'matches_name()' function which does not
814 * panic if the direntry/xentry referred by @zbr does not exist on the media.
816 * This function checks if xentry/direntry referred by zbranch @zbr matches name
817 * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
818 * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
819 * if xentry/direntry referred by @zbr does not exist on the media. A negative
820 * error code is returned in case of failure.
822 static int fallible_matches_name(struct ubifs_info
*c
,
823 struct ubifs_zbranch
*zbr
,
824 const struct qstr
*nm
)
826 struct ubifs_dent_node
*dent
;
829 /* If possible, match against the dent in the leaf node cache */
831 dent
= kmalloc(zbr
->len
, GFP_NOFS
);
835 err
= fallible_read_node(c
, &zbr
->key
, zbr
, dent
);
839 /* The node was not present */
843 ubifs_assert(err
== 1);
845 err
= lnc_add_directly(c
, zbr
, dent
);
851 nlen
= le16_to_cpu(dent
->nlen
);
852 err
= memcmp(dent
->name
, nm
->name
, min_t(int, nlen
, nm
->len
));
856 else if (nlen
< nm
->len
)
871 * fallible_resolve_collision - resolve a collision even if nodes are missing.
872 * @c: UBIFS file-system description object
874 * @zn: znode is returned here
875 * @n: branch number is passed and returned here
876 * @nm: name of directory entry
877 * @adding: indicates caller is adding a key to the TNC
879 * This is a "fallible" version of the 'resolve_collision()' function which
880 * does not panic if one of the nodes referred to by TNC does not exist on the
881 * media. This may happen when replaying the journal if a deleted node was
882 * Garbage-collected and the commit was not done. A branch that refers to a node
883 * that is not present is called a dangling branch. The following are the return
884 * codes for this function:
885 * o if @nm was found, %1 is returned and @zn and @n are set to the found
887 * o if we are @adding and @nm was not found, %0 is returned;
888 * o if we are not @adding and @nm was not found, but a dangling branch was
889 * found, then %1 is returned and @zn and @n are set to the dangling branch;
890 * o a negative error code is returned in case of failure.
892 static int fallible_resolve_collision(struct ubifs_info
*c
,
893 const union ubifs_key
*key
,
894 struct ubifs_znode
**zn
, int *n
,
895 const struct qstr
*nm
, int adding
)
897 struct ubifs_znode
*o_znode
= NULL
, *znode
= *zn
;
898 int uninitialized_var(o_n
), err
, cmp
, unsure
= 0, nn
= *n
;
900 cmp
= fallible_matches_name(c
, &znode
->zbranch
[nn
], nm
);
901 if (unlikely(cmp
< 0))
903 if (cmp
== NAME_MATCHES
)
905 if (cmp
== NOT_ON_MEDIA
) {
909 * We are unlucky and hit a dangling branch straight away.
910 * Now we do not really know where to go to find the needed
911 * branch - to the left or to the right. Well, let's try left.
915 unsure
= 1; /* Remove a dangling branch wherever it is */
917 if (cmp
== NAME_GREATER
|| unsure
) {
920 err
= tnc_prev(c
, zn
, n
);
921 if (err
== -ENOENT
) {
922 ubifs_assert(*n
== 0);
928 if (keys_cmp(c
, &(*zn
)->zbranch
[*n
].key
, key
)) {
929 /* See comments in 'resolve_collision()' */
930 if (*n
== (*zn
)->child_cnt
- 1) {
931 err
= tnc_next(c
, zn
, n
);
933 /* Should be impossible */
939 ubifs_assert(*n
== 0);
944 err
= fallible_matches_name(c
, &(*zn
)->zbranch
[*n
], nm
);
947 if (err
== NAME_MATCHES
)
949 if (err
== NOT_ON_MEDIA
) {
956 if (err
== NAME_LESS
)
963 if (cmp
== NAME_LESS
|| unsure
) {
968 err
= tnc_next(c
, &znode
, &nn
);
973 if (keys_cmp(c
, &znode
->zbranch
[nn
].key
, key
))
975 err
= fallible_matches_name(c
, &znode
->zbranch
[nn
], nm
);
978 if (err
== NAME_GREATER
)
982 if (err
== NAME_MATCHES
)
984 if (err
== NOT_ON_MEDIA
) {
991 /* Never match a dangling branch when adding */
992 if (adding
|| !o_znode
)
995 dbg_mnt("dangling match LEB %d:%d len %d %s",
996 o_znode
->zbranch
[o_n
].lnum
, o_znode
->zbranch
[o_n
].offs
,
997 o_znode
->zbranch
[o_n
].len
, DBGKEY(key
));
1004 * matches_position - determine if a zbranch matches a given position.
1005 * @zbr: zbranch of dent
1006 * @lnum: LEB number of dent to match
1007 * @offs: offset of dent to match
1009 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1011 static int matches_position(struct ubifs_zbranch
*zbr
, int lnum
, int offs
)
1013 if (zbr
->lnum
== lnum
&& zbr
->offs
== offs
)
1020 * resolve_collision_directly - resolve a collision directly.
1021 * @c: UBIFS file-system description object
1022 * @key: key of directory entry
1023 * @zn: znode is passed and returned here
1024 * @n: zbranch number is passed and returned here
1025 * @lnum: LEB number of dent node to match
1026 * @offs: offset of dent node to match
1028 * This function is used for "hashed" keys to make sure the found directory or
1029 * extended attribute entry node is what was looked for. It is used when the
1030 * flash address of the right node is known (@lnum:@offs) which makes it much
1031 * easier to resolve collisions (no need to read entries and match full
1032 * names). This function returns %1 and sets @zn and @n if the collision is
1033 * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
1034 * previous directory entry. Otherwise a negative error code is returned.
1036 static int resolve_collision_directly(struct ubifs_info
*c
,
1037 const union ubifs_key
*key
,
1038 struct ubifs_znode
**zn
, int *n
,
1041 struct ubifs_znode
*znode
;
1046 if (matches_position(&znode
->zbranch
[nn
], lnum
, offs
))
1051 err
= tnc_prev(c
, &znode
, &nn
);
1056 if (keys_cmp(c
, &znode
->zbranch
[nn
].key
, key
))
1058 if (matches_position(&znode
->zbranch
[nn
], lnum
, offs
)) {
1069 err
= tnc_next(c
, &znode
, &nn
);
1074 if (keys_cmp(c
, &znode
->zbranch
[nn
].key
, key
))
1078 if (matches_position(&znode
->zbranch
[nn
], lnum
, offs
))
1084 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1085 * @c: UBIFS file-system description object
1086 * @znode: znode to dirty
1088 * If we do not have a unique key that resides in a znode, then we cannot
1089 * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
1090 * This function records the path back to the last dirty ancestor, and then
1091 * dirties the znodes on that path.
1093 static struct ubifs_znode
*dirty_cow_bottom_up(struct ubifs_info
*c
,
1094 struct ubifs_znode
*znode
)
1096 struct ubifs_znode
*zp
;
1097 int *path
= c
->bottom_up_buf
, p
= 0;
1099 ubifs_assert(c
->zroot
.znode
);
1100 ubifs_assert(znode
);
1101 if (c
->zroot
.znode
->level
> BOTTOM_UP_HEIGHT
) {
1102 kfree(c
->bottom_up_buf
);
1103 c
->bottom_up_buf
= kmalloc(c
->zroot
.znode
->level
* sizeof(int),
1105 if (!c
->bottom_up_buf
)
1106 return ERR_PTR(-ENOMEM
);
1107 path
= c
->bottom_up_buf
;
1109 if (c
->zroot
.znode
->level
) {
1110 /* Go up until parent is dirty */
1118 ubifs_assert(p
< c
->zroot
.znode
->level
);
1120 if (!zp
->cnext
&& ubifs_zn_dirty(znode
))
1126 /* Come back down, dirtying as we go */
1128 struct ubifs_zbranch
*zbr
;
1132 ubifs_assert(path
[p
- 1] >= 0);
1133 ubifs_assert(path
[p
- 1] < zp
->child_cnt
);
1134 zbr
= &zp
->zbranch
[path
[--p
]];
1135 znode
= dirty_cow_znode(c
, zbr
);
1137 ubifs_assert(znode
== c
->zroot
.znode
);
1138 znode
= dirty_cow_znode(c
, &c
->zroot
);
1140 if (IS_ERR(znode
) || !p
)
1142 ubifs_assert(path
[p
- 1] >= 0);
1143 ubifs_assert(path
[p
- 1] < znode
->child_cnt
);
1144 znode
= znode
->zbranch
[path
[p
- 1]].znode
;
1151 * ubifs_lookup_level0 - search for zero-level znode.
1152 * @c: UBIFS file-system description object
1153 * @key: key to lookup
1154 * @zn: znode is returned here
1155 * @n: znode branch slot number is returned here
1157 * This function looks up the TNC tree and search for zero-level znode which
1158 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1160 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1161 * is returned and slot number of the matched branch is stored in @n;
1162 * o not exact match, which means that zero-level znode does not contain
1163 * @key, then %0 is returned and slot number of the closest branch is stored
1165 * o @key is so small that it is even less than the lowest key of the
1166 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1168 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1169 * function reads corresponding indexing nodes and inserts them to TNC. In
1170 * case of failure, a negative error code is returned.
1172 int ubifs_lookup_level0(struct ubifs_info
*c
, const union ubifs_key
*key
,
1173 struct ubifs_znode
**zn
, int *n
)
1176 struct ubifs_znode
*znode
;
1177 unsigned long time
= get_seconds();
1179 dbg_tnc("search key %s", DBGKEY(key
));
1180 ubifs_assert(key_type(c
, key
) < UBIFS_INVALID_KEY
);
1182 znode
= c
->zroot
.znode
;
1183 if (unlikely(!znode
)) {
1184 znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
1186 return PTR_ERR(znode
);
1192 struct ubifs_zbranch
*zbr
;
1194 exact
= ubifs_search_zbranch(c
, znode
, key
, n
);
1196 if (znode
->level
== 0)
1201 zbr
= &znode
->zbranch
[*n
];
1209 /* znode is not in TNC cache, load it from the media */
1210 znode
= ubifs_load_znode(c
, zbr
, znode
, *n
);
1212 return PTR_ERR(znode
);
1216 if (exact
|| !is_hash_key(c
, key
) || *n
!= -1) {
1217 dbg_tnc("found %d, lvl %d, n %d", exact
, znode
->level
, *n
);
1222 * Here is a tricky place. We have not found the key and this is a
1223 * "hashed" key, which may collide. The rest of the code deals with
1224 * situations like this:
1228 * | 3 | 5 | | 6 | 7 | (x)
1230 * Or more a complex example:
1234 * | 1 | 3 | | 5 | 8 |
1236 * | 5 | 5 | | 6 | 7 | (x)
1238 * In the examples, if we are looking for key "5", we may reach nodes
1239 * marked with "(x)". In this case what we have do is to look at the
1240 * left and see if there is "5" key there. If there is, we have to
1243 * Note, this whole situation is possible because we allow to have
1244 * elements which are equivalent to the next key in the parent in the
1245 * children of current znode. For example, this happens if we split a
1246 * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
1250 * | 3 | 5 | | 5 | 6 | 7 |
1252 * And this becomes what is at the first "picture" after key "5" marked
1253 * with "^" is removed. What could be done is we could prohibit
1254 * splitting in the middle of the colliding sequence. Also, when
1255 * removing the leftmost key, we would have to correct the key of the
1256 * parent node, which would introduce additional complications. Namely,
1257 * if we changed the leftmost key of the parent znode, the garbage
1258 * collector would be unable to find it (GC is doing this when GC'ing
1259 * indexing LEBs). Although we already have an additional RB-tree where
1260 * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
1261 * after the commit. But anyway, this does not look easy to implement
1262 * so we did not try this.
1264 err
= tnc_prev(c
, &znode
, n
);
1265 if (err
== -ENOENT
) {
1266 dbg_tnc("found 0, lvl %d, n -1", znode
->level
);
1270 if (unlikely(err
< 0))
1272 if (keys_cmp(c
, key
, &znode
->zbranch
[*n
].key
)) {
1273 dbg_tnc("found 0, lvl %d, n -1", znode
->level
);
1278 dbg_tnc("found 1, lvl %d, n %d", znode
->level
, *n
);
1284 * lookup_level0_dirty - search for zero-level znode dirtying.
1285 * @c: UBIFS file-system description object
1286 * @key: key to lookup
1287 * @zn: znode is returned here
1288 * @n: znode branch slot number is returned here
1290 * This function looks up the TNC tree and search for zero-level znode which
1291 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1293 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1294 * is returned and slot number of the matched branch is stored in @n;
1295 * o not exact match, which means that zero-level znode does not contain @key
1296 * then %0 is returned and slot number of the closed branch is stored in
1298 * o @key is so small that it is even less than the lowest key of the
1299 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1301 * Additionally all znodes in the path from the root to the located zero-level
1302 * znode are marked as dirty.
1304 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1305 * function reads corresponding indexing nodes and inserts them to TNC. In
1306 * case of failure, a negative error code is returned.
1308 static int lookup_level0_dirty(struct ubifs_info
*c
, const union ubifs_key
*key
,
1309 struct ubifs_znode
**zn
, int *n
)
1312 struct ubifs_znode
*znode
;
1313 unsigned long time
= get_seconds();
1315 dbg_tnc("search and dirty key %s", DBGKEY(key
));
1317 znode
= c
->zroot
.znode
;
1318 if (unlikely(!znode
)) {
1319 znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
1321 return PTR_ERR(znode
);
1324 znode
= dirty_cow_znode(c
, &c
->zroot
);
1326 return PTR_ERR(znode
);
1331 struct ubifs_zbranch
*zbr
;
1333 exact
= ubifs_search_zbranch(c
, znode
, key
, n
);
1335 if (znode
->level
== 0)
1340 zbr
= &znode
->zbranch
[*n
];
1344 znode
= dirty_cow_znode(c
, zbr
);
1346 return PTR_ERR(znode
);
1350 /* znode is not in TNC cache, load it from the media */
1351 znode
= ubifs_load_znode(c
, zbr
, znode
, *n
);
1353 return PTR_ERR(znode
);
1354 znode
= dirty_cow_znode(c
, zbr
);
1356 return PTR_ERR(znode
);
1360 if (exact
|| !is_hash_key(c
, key
) || *n
!= -1) {
1361 dbg_tnc("found %d, lvl %d, n %d", exact
, znode
->level
, *n
);
1366 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1369 err
= tnc_prev(c
, &znode
, n
);
1370 if (err
== -ENOENT
) {
1372 dbg_tnc("found 0, lvl %d, n -1", znode
->level
);
1375 if (unlikely(err
< 0))
1377 if (keys_cmp(c
, key
, &znode
->zbranch
[*n
].key
)) {
1379 dbg_tnc("found 0, lvl %d, n -1", znode
->level
);
1383 if (znode
->cnext
|| !ubifs_zn_dirty(znode
)) {
1384 znode
= dirty_cow_bottom_up(c
, znode
);
1386 return PTR_ERR(znode
);
1389 dbg_tnc("found 1, lvl %d, n %d", znode
->level
, *n
);
1395 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1396 * @c: UBIFS file-system description object
1398 * @gc_seq1: garbage collection sequence number
1400 * This function determines if @lnum may have been garbage collected since
1401 * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1404 static int maybe_leb_gced(struct ubifs_info
*c
, int lnum
, int gc_seq1
)
1406 int gc_seq2
, gced_lnum
;
1408 gced_lnum
= c
->gced_lnum
;
1410 gc_seq2
= c
->gc_seq
;
1411 /* Same seq means no GC */
1412 if (gc_seq1
== gc_seq2
)
1414 /* Different by more than 1 means we don't know */
1415 if (gc_seq1
+ 1 != gc_seq2
)
1418 * We have seen the sequence number has increased by 1. Now we need to
1419 * be sure we read the right LEB number, so read it again.
1422 if (gced_lnum
!= c
->gced_lnum
)
1424 /* Finally we can check lnum */
1425 if (gced_lnum
== lnum
)
1431 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1432 * @c: UBIFS file-system description object
1433 * @key: node key to lookup
1434 * @node: the node is returned here
1435 * @lnum: LEB number is returned here
1436 * @offs: offset is returned here
1438 * This function looks up and reads node with key @key. The caller has to make
1439 * sure the @node buffer is large enough to fit the node. Returns zero in case
1440 * of success, %-ENOENT if the node was not found, and a negative error code in
1441 * case of failure. The node location can be returned in @lnum and @offs.
1443 int ubifs_tnc_locate(struct ubifs_info
*c
, const union ubifs_key
*key
,
1444 void *node
, int *lnum
, int *offs
)
1446 int found
, n
, err
, safely
= 0, gc_seq1
;
1447 struct ubifs_znode
*znode
;
1448 struct ubifs_zbranch zbr
, *zt
;
1451 mutex_lock(&c
->tnc_mutex
);
1452 found
= ubifs_lookup_level0(c
, key
, &znode
, &n
);
1456 } else if (found
< 0) {
1460 zt
= &znode
->zbranch
[n
];
1465 if (is_hash_key(c
, key
)) {
1467 * In this case the leaf node cache gets used, so we pass the
1468 * address of the zbranch and keep the mutex locked
1470 err
= tnc_read_node_nm(c
, zt
, node
);
1474 err
= ubifs_tnc_read_node(c
, zt
, node
);
1477 /* Drop the TNC mutex prematurely and race with garbage collection */
1478 zbr
= znode
->zbranch
[n
];
1479 gc_seq1
= c
->gc_seq
;
1480 mutex_unlock(&c
->tnc_mutex
);
1482 if (ubifs_get_wbuf(c
, zbr
.lnum
)) {
1483 /* We do not GC journal heads */
1484 err
= ubifs_tnc_read_node(c
, &zbr
, node
);
1488 err
= fallible_read_node(c
, key
, &zbr
, node
);
1489 if (err
<= 0 || maybe_leb_gced(c
, zbr
.lnum
, gc_seq1
)) {
1491 * The node may have been GC'ed out from under us so try again
1492 * while keeping the TNC mutex locked.
1500 mutex_unlock(&c
->tnc_mutex
);
1505 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1506 * @c: UBIFS file-system description object
1507 * @bu: bulk-read parameters and results
1509 * Lookup consecutive data node keys for the same inode that reside
1510 * consecutively in the same LEB. This function returns zero in case of success
1511 * and a negative error code in case of failure.
1513 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1514 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1515 * maximum possible amount of nodes for bulk-read.
1517 int ubifs_tnc_get_bu_keys(struct ubifs_info
*c
, struct bu_info
*bu
)
1519 int n
, err
= 0, lnum
= -1, uninitialized_var(offs
);
1520 int uninitialized_var(len
);
1521 unsigned int block
= key_block(c
, &bu
->key
);
1522 struct ubifs_znode
*znode
;
1528 mutex_lock(&c
->tnc_mutex
);
1529 /* Find first key */
1530 err
= ubifs_lookup_level0(c
, &bu
->key
, &znode
, &n
);
1535 len
= znode
->zbranch
[n
].len
;
1536 /* The buffer must be big enough for at least 1 node */
1537 if (len
> bu
->buf_len
) {
1542 bu
->zbranch
[bu
->cnt
++] = znode
->zbranch
[n
];
1544 lnum
= znode
->zbranch
[n
].lnum
;
1545 offs
= ALIGN(znode
->zbranch
[n
].offs
+ len
, 8);
1548 struct ubifs_zbranch
*zbr
;
1549 union ubifs_key
*key
;
1550 unsigned int next_block
;
1553 err
= tnc_next(c
, &znode
, &n
);
1556 zbr
= &znode
->zbranch
[n
];
1558 /* See if there is another data key for this file */
1559 if (key_inum(c
, key
) != key_inum(c
, &bu
->key
) ||
1560 key_type(c
, key
) != UBIFS_DATA_KEY
) {
1565 /* First key found */
1567 offs
= ALIGN(zbr
->offs
+ zbr
->len
, 8);
1569 if (len
> bu
->buf_len
) {
1575 * The data nodes must be in consecutive positions in
1578 if (zbr
->lnum
!= lnum
|| zbr
->offs
!= offs
)
1580 offs
+= ALIGN(zbr
->len
, 8);
1581 len
= ALIGN(len
, 8) + zbr
->len
;
1582 /* Must not exceed buffer length */
1583 if (len
> bu
->buf_len
)
1586 /* Allow for holes */
1587 next_block
= key_block(c
, key
);
1588 bu
->blk_cnt
+= (next_block
- block
- 1);
1589 if (bu
->blk_cnt
>= UBIFS_MAX_BULK_READ
)
1593 bu
->zbranch
[bu
->cnt
++] = *zbr
;
1595 /* See if we have room for more */
1596 if (bu
->cnt
>= UBIFS_MAX_BULK_READ
)
1598 if (bu
->blk_cnt
>= UBIFS_MAX_BULK_READ
)
1602 if (err
== -ENOENT
) {
1606 bu
->gc_seq
= c
->gc_seq
;
1607 mutex_unlock(&c
->tnc_mutex
);
1611 * An enormous hole could cause bulk-read to encompass too many
1612 * page cache pages, so limit the number here.
1614 if (bu
->blk_cnt
> UBIFS_MAX_BULK_READ
)
1615 bu
->blk_cnt
= UBIFS_MAX_BULK_READ
;
1617 * Ensure that bulk-read covers a whole number of page cache
1620 if (UBIFS_BLOCKS_PER_PAGE
== 1 ||
1621 !(bu
->blk_cnt
& (UBIFS_BLOCKS_PER_PAGE
- 1)))
1624 /* At the end of file we can round up */
1625 bu
->blk_cnt
+= UBIFS_BLOCKS_PER_PAGE
- 1;
1628 /* Exclude data nodes that do not make up a whole page cache page */
1629 block
= key_block(c
, &bu
->key
) + bu
->blk_cnt
;
1630 block
&= ~(UBIFS_BLOCKS_PER_PAGE
- 1);
1632 if (key_block(c
, &bu
->zbranch
[bu
->cnt
- 1].key
) < block
)
1640 * read_wbuf - bulk-read from a LEB with a wbuf.
1641 * @wbuf: wbuf that may overlap the read
1642 * @buf: buffer into which to read
1644 * @lnum: LEB number from which to read
1645 * @offs: offset from which to read
1647 * This functions returns %0 on success or a negative error code on failure.
1649 static int read_wbuf(struct ubifs_wbuf
*wbuf
, void *buf
, int len
, int lnum
,
1652 const struct ubifs_info
*c
= wbuf
->c
;
1655 dbg_io("LEB %d:%d, length %d", lnum
, offs
, len
);
1656 ubifs_assert(wbuf
&& lnum
>= 0 && lnum
< c
->leb_cnt
&& offs
>= 0);
1657 ubifs_assert(!(offs
& 7) && offs
< c
->leb_size
);
1658 ubifs_assert(offs
+ len
<= c
->leb_size
);
1660 spin_lock(&wbuf
->lock
);
1661 overlap
= (lnum
== wbuf
->lnum
&& offs
+ len
> wbuf
->offs
);
1663 /* We may safely unlock the write-buffer and read the data */
1664 spin_unlock(&wbuf
->lock
);
1665 return ubi_read(c
->ubi
, lnum
, buf
, offs
, len
);
1668 /* Don't read under wbuf */
1669 rlen
= wbuf
->offs
- offs
;
1673 /* Copy the rest from the write-buffer */
1674 memcpy(buf
+ rlen
, wbuf
->buf
+ offs
+ rlen
- wbuf
->offs
, len
- rlen
);
1675 spin_unlock(&wbuf
->lock
);
1678 /* Read everything that goes before write-buffer */
1679 return ubi_read(c
->ubi
, lnum
, buf
, offs
, rlen
);
1685 * validate_data_node - validate data nodes for bulk-read.
1686 * @c: UBIFS file-system description object
1687 * @buf: buffer containing data node to validate
1688 * @zbr: zbranch of data node to validate
1690 * This functions returns %0 on success or a negative error code on failure.
1692 static int validate_data_node(struct ubifs_info
*c
, void *buf
,
1693 struct ubifs_zbranch
*zbr
)
1695 union ubifs_key key1
;
1696 struct ubifs_ch
*ch
= buf
;
1699 if (ch
->node_type
!= UBIFS_DATA_NODE
) {
1700 ubifs_err("bad node type (%d but expected %d)",
1701 ch
->node_type
, UBIFS_DATA_NODE
);
1705 err
= ubifs_check_node(c
, buf
, zbr
->lnum
, zbr
->offs
, 0, 0);
1707 ubifs_err("expected node type %d", UBIFS_DATA_NODE
);
1711 len
= le32_to_cpu(ch
->len
);
1712 if (len
!= zbr
->len
) {
1713 ubifs_err("bad node length %d, expected %d", len
, zbr
->len
);
1717 /* Make sure the key of the read node is correct */
1718 key_read(c
, buf
+ UBIFS_KEY_OFFSET
, &key1
);
1719 if (!keys_eq(c
, &zbr
->key
, &key1
)) {
1720 ubifs_err("bad key in node at LEB %d:%d",
1721 zbr
->lnum
, zbr
->offs
);
1722 dbg_tnc("looked for key %s found node's key %s",
1723 DBGKEY(&zbr
->key
), DBGKEY1(&key1
));
1732 ubifs_err("bad node at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
1733 dbg_dump_node(c
, buf
);
1739 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1740 * @c: UBIFS file-system description object
1741 * @bu: bulk-read parameters and results
1743 * This functions reads and validates the data nodes that were identified by the
1744 * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
1745 * -EAGAIN to indicate a race with GC, or another negative error code on
1748 int ubifs_tnc_bulk_read(struct ubifs_info
*c
, struct bu_info
*bu
)
1750 int lnum
= bu
->zbranch
[0].lnum
, offs
= bu
->zbranch
[0].offs
, len
, err
, i
;
1751 struct ubifs_wbuf
*wbuf
;
1754 len
= bu
->zbranch
[bu
->cnt
- 1].offs
;
1755 len
+= bu
->zbranch
[bu
->cnt
- 1].len
- offs
;
1756 if (len
> bu
->buf_len
) {
1757 ubifs_err("buffer too small %d vs %d", bu
->buf_len
, len
);
1762 wbuf
= ubifs_get_wbuf(c
, lnum
);
1764 err
= read_wbuf(wbuf
, bu
->buf
, len
, lnum
, offs
);
1766 err
= ubi_read(c
->ubi
, lnum
, bu
->buf
, offs
, len
);
1768 /* Check for a race with GC */
1769 if (maybe_leb_gced(c
, lnum
, bu
->gc_seq
))
1772 if (err
&& err
!= -EBADMSG
) {
1773 ubifs_err("failed to read from LEB %d:%d, error %d",
1776 dbg_tnc("key %s", DBGKEY(&bu
->key
));
1780 /* Validate the nodes read */
1782 for (i
= 0; i
< bu
->cnt
; i
++) {
1783 err
= validate_data_node(c
, buf
, &bu
->zbranch
[i
]);
1786 buf
= buf
+ ALIGN(bu
->zbranch
[i
].len
, 8);
1793 * do_lookup_nm- look up a "hashed" node.
1794 * @c: UBIFS file-system description object
1795 * @key: node key to lookup
1796 * @node: the node is returned here
1799 * This function look up and reads a node which contains name hash in the key.
1800 * Since the hash may have collisions, there may be many nodes with the same
1801 * key, so we have to sequentially look to all of them until the needed one is
1802 * found. This function returns zero in case of success, %-ENOENT if the node
1803 * was not found, and a negative error code in case of failure.
1805 static int do_lookup_nm(struct ubifs_info
*c
, const union ubifs_key
*key
,
1806 void *node
, const struct qstr
*nm
)
1809 struct ubifs_znode
*znode
;
1811 dbg_tnc("name '%.*s' key %s", nm
->len
, nm
->name
, DBGKEY(key
));
1812 mutex_lock(&c
->tnc_mutex
);
1813 found
= ubifs_lookup_level0(c
, key
, &znode
, &n
);
1817 } else if (found
< 0) {
1822 ubifs_assert(n
>= 0);
1824 err
= resolve_collision(c
, key
, &znode
, &n
, nm
);
1825 dbg_tnc("rc returned %d, znode %p, n %d", err
, znode
, n
);
1826 if (unlikely(err
< 0))
1833 err
= tnc_read_node_nm(c
, &znode
->zbranch
[n
], node
);
1836 mutex_unlock(&c
->tnc_mutex
);
1841 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1842 * @c: UBIFS file-system description object
1843 * @key: node key to lookup
1844 * @node: the node is returned here
1847 * This function look up and reads a node which contains name hash in the key.
1848 * Since the hash may have collisions, there may be many nodes with the same
1849 * key, so we have to sequentially look to all of them until the needed one is
1850 * found. This function returns zero in case of success, %-ENOENT if the node
1851 * was not found, and a negative error code in case of failure.
1853 int ubifs_tnc_lookup_nm(struct ubifs_info
*c
, const union ubifs_key
*key
,
1854 void *node
, const struct qstr
*nm
)
1857 const struct ubifs_dent_node
*dent
= node
;
1860 * We assume that in most of the cases there are no name collisions and
1861 * 'ubifs_tnc_lookup()' returns us the right direntry.
1863 err
= ubifs_tnc_lookup(c
, key
, node
);
1867 len
= le16_to_cpu(dent
->nlen
);
1868 if (nm
->len
== len
&& !memcmp(dent
->name
, nm
->name
, len
))
1872 * Unluckily, there are hash collisions and we have to iterate over
1873 * them look at each direntry with colliding name hash sequentially.
1875 return do_lookup_nm(c
, key
, node
, nm
);
1879 * correct_parent_keys - correct parent znodes' keys.
1880 * @c: UBIFS file-system description object
1881 * @znode: znode to correct parent znodes for
1883 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1884 * zbranch changes, keys of parent znodes have to be corrected. This helper
1885 * function is called in such situations and corrects the keys if needed.
1887 static void correct_parent_keys(const struct ubifs_info
*c
,
1888 struct ubifs_znode
*znode
)
1890 union ubifs_key
*key
, *key1
;
1892 ubifs_assert(znode
->parent
);
1893 ubifs_assert(znode
->iip
== 0);
1895 key
= &znode
->zbranch
[0].key
;
1896 key1
= &znode
->parent
->zbranch
[0].key
;
1898 while (keys_cmp(c
, key
, key1
) < 0) {
1899 key_copy(c
, key
, key1
);
1900 znode
= znode
->parent
;
1902 if (!znode
->parent
|| znode
->iip
)
1904 key1
= &znode
->parent
->zbranch
[0].key
;
1909 * insert_zbranch - insert a zbranch into a znode.
1910 * @znode: znode into which to insert
1911 * @zbr: zbranch to insert
1912 * @n: slot number to insert to
1914 * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
1915 * znode's array of zbranches and keeps zbranches consolidated, so when a new
1916 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1917 * slot, zbranches starting from @n have to be moved right.
1919 static void insert_zbranch(struct ubifs_znode
*znode
,
1920 const struct ubifs_zbranch
*zbr
, int n
)
1924 ubifs_assert(ubifs_zn_dirty(znode
));
1927 for (i
= znode
->child_cnt
; i
> n
; i
--) {
1928 znode
->zbranch
[i
] = znode
->zbranch
[i
- 1];
1929 if (znode
->zbranch
[i
].znode
)
1930 znode
->zbranch
[i
].znode
->iip
= i
;
1933 zbr
->znode
->iip
= n
;
1935 for (i
= znode
->child_cnt
; i
> n
; i
--)
1936 znode
->zbranch
[i
] = znode
->zbranch
[i
- 1];
1938 znode
->zbranch
[n
] = *zbr
;
1939 znode
->child_cnt
+= 1;
1942 * After inserting at slot zero, the lower bound of the key range of
1943 * this znode may have changed. If this znode is subsequently split
1944 * then the upper bound of the key range may change, and furthermore
1945 * it could change to be lower than the original lower bound. If that
1946 * happens, then it will no longer be possible to find this znode in the
1947 * TNC using the key from the index node on flash. That is bad because
1948 * if it is not found, we will assume it is obsolete and may overwrite
1949 * it. Then if there is an unclean unmount, we will start using the
1950 * old index which will be broken.
1952 * So we first mark znodes that have insertions at slot zero, and then
1953 * if they are split we add their lnum/offs to the old_idx tree.
1960 * tnc_insert - insert a node into TNC.
1961 * @c: UBIFS file-system description object
1962 * @znode: znode to insert into
1963 * @zbr: branch to insert
1964 * @n: slot number to insert new zbranch to
1966 * This function inserts a new node described by @zbr into znode @znode. If
1967 * znode does not have a free slot for new zbranch, it is split. Parent znodes
1968 * are splat as well if needed. Returns zero in case of success or a negative
1969 * error code in case of failure.
1971 static int tnc_insert(struct ubifs_info
*c
, struct ubifs_znode
*znode
,
1972 struct ubifs_zbranch
*zbr
, int n
)
1974 struct ubifs_znode
*zn
, *zi
, *zp
;
1975 int i
, keep
, move
, appending
= 0;
1976 union ubifs_key
*key
= &zbr
->key
, *key1
;
1978 ubifs_assert(n
>= 0 && n
<= c
->fanout
);
1980 /* Implement naive insert for now */
1983 if (znode
->child_cnt
< c
->fanout
) {
1984 ubifs_assert(n
!= c
->fanout
);
1985 dbg_tnc("inserted at %d level %d, key %s", n
, znode
->level
,
1988 insert_zbranch(znode
, zbr
, n
);
1990 /* Ensure parent's key is correct */
1991 if (n
== 0 && zp
&& znode
->iip
== 0)
1992 correct_parent_keys(c
, znode
);
1998 * Unfortunately, @znode does not have more empty slots and we have to
2001 dbg_tnc("splitting level %d, key %s", znode
->level
, DBGKEY(key
));
2005 * We can no longer be sure of finding this znode by key, so we
2006 * record it in the old_idx tree.
2008 ins_clr_old_idx_znode(c
, znode
);
2010 zn
= kzalloc(c
->max_znode_sz
, GFP_NOFS
);
2014 zn
->level
= znode
->level
;
2016 /* Decide where to split */
2017 if (znode
->level
== 0 && key_type(c
, key
) == UBIFS_DATA_KEY
) {
2018 /* Try not to split consecutive data keys */
2019 if (n
== c
->fanout
) {
2020 key1
= &znode
->zbranch
[n
- 1].key
;
2021 if (key_inum(c
, key1
) == key_inum(c
, key
) &&
2022 key_type(c
, key1
) == UBIFS_DATA_KEY
)
2026 } else if (appending
&& n
!= c
->fanout
) {
2027 /* Try not to split consecutive data keys */
2030 if (n
>= (c
->fanout
+ 1) / 2) {
2031 key1
= &znode
->zbranch
[0].key
;
2032 if (key_inum(c
, key1
) == key_inum(c
, key
) &&
2033 key_type(c
, key1
) == UBIFS_DATA_KEY
) {
2034 key1
= &znode
->zbranch
[n
].key
;
2035 if (key_inum(c
, key1
) != key_inum(c
, key
) ||
2036 key_type(c
, key1
) != UBIFS_DATA_KEY
) {
2038 move
= c
->fanout
- keep
;
2050 keep
= (c
->fanout
+ 1) / 2;
2051 move
= c
->fanout
- keep
;
2055 * Although we don't at present, we could look at the neighbors and see
2056 * if we can move some zbranches there.
2060 /* Insert into existing znode */
2065 /* Insert into new znode */
2070 zbr
->znode
->parent
= zn
;
2075 __set_bit(DIRTY_ZNODE
, &zn
->flags
);
2076 atomic_long_inc(&c
->dirty_zn_cnt
);
2078 zn
->child_cnt
= move
;
2079 znode
->child_cnt
= keep
;
2081 dbg_tnc("moving %d, keeping %d", move
, keep
);
2084 for (i
= 0; i
< move
; i
++) {
2085 zn
->zbranch
[i
] = znode
->zbranch
[keep
+ i
];
2088 if (zn
->zbranch
[i
].znode
) {
2089 zn
->zbranch
[i
].znode
->parent
= zn
;
2090 zn
->zbranch
[i
].znode
->iip
= i
;
2094 /* Insert new key and branch */
2095 dbg_tnc("inserting at %d level %d, key %s", n
, zn
->level
, DBGKEY(key
));
2097 insert_zbranch(zi
, zbr
, n
);
2099 /* Insert new znode (produced by spitting) into the parent */
2101 if (n
== 0 && zi
== znode
&& znode
->iip
== 0)
2102 correct_parent_keys(c
, znode
);
2104 /* Locate insertion point */
2107 /* Tail recursion */
2108 zbr
->key
= zn
->zbranch
[0].key
;
2118 /* We have to split root znode */
2119 dbg_tnc("creating new zroot at level %d", znode
->level
+ 1);
2121 zi
= kzalloc(c
->max_znode_sz
, GFP_NOFS
);
2126 zi
->level
= znode
->level
+ 1;
2128 __set_bit(DIRTY_ZNODE
, &zi
->flags
);
2129 atomic_long_inc(&c
->dirty_zn_cnt
);
2131 zi
->zbranch
[0].key
= znode
->zbranch
[0].key
;
2132 zi
->zbranch
[0].znode
= znode
;
2133 zi
->zbranch
[0].lnum
= c
->zroot
.lnum
;
2134 zi
->zbranch
[0].offs
= c
->zroot
.offs
;
2135 zi
->zbranch
[0].len
= c
->zroot
.len
;
2136 zi
->zbranch
[1].key
= zn
->zbranch
[0].key
;
2137 zi
->zbranch
[1].znode
= zn
;
2142 c
->zroot
.znode
= zi
;
2153 * ubifs_tnc_add - add a node to TNC.
2154 * @c: UBIFS file-system description object
2156 * @lnum: LEB number of node
2157 * @offs: node offset
2160 * This function adds a node with key @key to TNC. The node may be new or it may
2161 * obsolete some existing one. Returns %0 on success or negative error code on
2164 int ubifs_tnc_add(struct ubifs_info
*c
, const union ubifs_key
*key
, int lnum
,
2167 int found
, n
, err
= 0;
2168 struct ubifs_znode
*znode
;
2170 mutex_lock(&c
->tnc_mutex
);
2171 dbg_tnc("%d:%d, len %d, key %s", lnum
, offs
, len
, DBGKEY(key
));
2172 found
= lookup_level0_dirty(c
, key
, &znode
, &n
);
2174 struct ubifs_zbranch zbr
;
2180 key_copy(c
, key
, &zbr
.key
);
2181 err
= tnc_insert(c
, znode
, &zbr
, n
+ 1);
2182 } else if (found
== 1) {
2183 struct ubifs_zbranch
*zbr
= &znode
->zbranch
[n
];
2186 err
= ubifs_add_dirt(c
, zbr
->lnum
, zbr
->len
);
2193 err
= dbg_check_tnc(c
, 0);
2194 mutex_unlock(&c
->tnc_mutex
);
2200 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2201 * @c: UBIFS file-system description object
2203 * @old_lnum: LEB number of old node
2204 * @old_offs: old node offset
2205 * @lnum: LEB number of node
2206 * @offs: node offset
2209 * This function replaces a node with key @key in the TNC only if the old node
2210 * is found. This function is called by garbage collection when node are moved.
2211 * Returns %0 on success or negative error code on failure.
2213 int ubifs_tnc_replace(struct ubifs_info
*c
, const union ubifs_key
*key
,
2214 int old_lnum
, int old_offs
, int lnum
, int offs
, int len
)
2216 int found
, n
, err
= 0;
2217 struct ubifs_znode
*znode
;
2219 mutex_lock(&c
->tnc_mutex
);
2220 dbg_tnc("old LEB %d:%d, new LEB %d:%d, len %d, key %s", old_lnum
,
2221 old_offs
, lnum
, offs
, len
, DBGKEY(key
));
2222 found
= lookup_level0_dirty(c
, key
, &znode
, &n
);
2229 struct ubifs_zbranch
*zbr
= &znode
->zbranch
[n
];
2232 if (zbr
->lnum
== old_lnum
&& zbr
->offs
== old_offs
) {
2234 err
= ubifs_add_dirt(c
, zbr
->lnum
, zbr
->len
);
2241 } else if (is_hash_key(c
, key
)) {
2242 found
= resolve_collision_directly(c
, key
, &znode
, &n
,
2243 old_lnum
, old_offs
);
2244 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2245 found
, znode
, n
, old_lnum
, old_offs
);
2252 /* Ensure the znode is dirtied */
2253 if (znode
->cnext
|| !ubifs_zn_dirty(znode
)) {
2254 znode
= dirty_cow_bottom_up(c
, znode
);
2255 if (IS_ERR(znode
)) {
2256 err
= PTR_ERR(znode
);
2260 zbr
= &znode
->zbranch
[n
];
2262 err
= ubifs_add_dirt(c
, zbr
->lnum
,
2274 err
= ubifs_add_dirt(c
, lnum
, len
);
2277 err
= dbg_check_tnc(c
, 0);
2280 mutex_unlock(&c
->tnc_mutex
);
2285 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2286 * @c: UBIFS file-system description object
2288 * @lnum: LEB number of node
2289 * @offs: node offset
2293 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
2294 * may have collisions, like directory entry keys.
2296 int ubifs_tnc_add_nm(struct ubifs_info
*c
, const union ubifs_key
*key
,
2297 int lnum
, int offs
, int len
, const struct qstr
*nm
)
2299 int found
, n
, err
= 0;
2300 struct ubifs_znode
*znode
;
2302 mutex_lock(&c
->tnc_mutex
);
2303 dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum
, offs
, nm
->len
, nm
->name
,
2305 found
= lookup_level0_dirty(c
, key
, &znode
, &n
);
2313 found
= fallible_resolve_collision(c
, key
, &znode
, &n
,
2316 found
= resolve_collision(c
, key
, &znode
, &n
, nm
);
2317 dbg_tnc("rc returned %d, znode %p, n %d", found
, znode
, n
);
2323 /* Ensure the znode is dirtied */
2324 if (znode
->cnext
|| !ubifs_zn_dirty(znode
)) {
2325 znode
= dirty_cow_bottom_up(c
, znode
);
2326 if (IS_ERR(znode
)) {
2327 err
= PTR_ERR(znode
);
2333 struct ubifs_zbranch
*zbr
= &znode
->zbranch
[n
];
2336 err
= ubifs_add_dirt(c
, zbr
->lnum
, zbr
->len
);
2345 struct ubifs_zbranch zbr
;
2351 key_copy(c
, key
, &zbr
.key
);
2352 err
= tnc_insert(c
, znode
, &zbr
, n
+ 1);
2357 * We did not find it in the index so there may be a
2358 * dangling branch still in the index. So we remove it
2359 * by passing 'ubifs_tnc_remove_nm()' the same key but
2360 * an unmatchable name.
2362 struct qstr noname
= { .len
= 0, .name
= "" };
2364 err
= dbg_check_tnc(c
, 0);
2365 mutex_unlock(&c
->tnc_mutex
);
2368 return ubifs_tnc_remove_nm(c
, key
, &noname
);
2374 err
= dbg_check_tnc(c
, 0);
2375 mutex_unlock(&c
->tnc_mutex
);
2380 * tnc_delete - delete a znode form TNC.
2381 * @c: UBIFS file-system description object
2382 * @znode: znode to delete from
2383 * @n: zbranch slot number to delete
2385 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2386 * case of success and a negative error code in case of failure.
2388 static int tnc_delete(struct ubifs_info
*c
, struct ubifs_znode
*znode
, int n
)
2390 struct ubifs_zbranch
*zbr
;
2391 struct ubifs_znode
*zp
;
2394 /* Delete without merge for now */
2395 ubifs_assert(znode
->level
== 0);
2396 ubifs_assert(n
>= 0 && n
< c
->fanout
);
2397 dbg_tnc("deleting %s", DBGKEY(&znode
->zbranch
[n
].key
));
2399 zbr
= &znode
->zbranch
[n
];
2402 err
= ubifs_add_dirt(c
, zbr
->lnum
, zbr
->len
);
2404 dbg_dump_znode(c
, znode
);
2408 /* We do not "gap" zbranch slots */
2409 for (i
= n
; i
< znode
->child_cnt
- 1; i
++)
2410 znode
->zbranch
[i
] = znode
->zbranch
[i
+ 1];
2411 znode
->child_cnt
-= 1;
2413 if (znode
->child_cnt
> 0)
2417 * This was the last zbranch, we have to delete this znode from the
2422 ubifs_assert(!test_bit(OBSOLETE_ZNODE
, &znode
->flags
));
2423 ubifs_assert(ubifs_zn_dirty(znode
));
2428 atomic_long_dec(&c
->dirty_zn_cnt
);
2430 err
= insert_old_idx_znode(c
, znode
);
2435 __set_bit(OBSOLETE_ZNODE
, &znode
->flags
);
2436 atomic_long_inc(&c
->clean_zn_cnt
);
2437 atomic_long_inc(&ubifs_clean_zn_cnt
);
2441 } while (znode
->child_cnt
== 1); /* while removing last child */
2443 /* Remove from znode, entry n - 1 */
2444 znode
->child_cnt
-= 1;
2445 ubifs_assert(znode
->level
!= 0);
2446 for (i
= n
; i
< znode
->child_cnt
; i
++) {
2447 znode
->zbranch
[i
] = znode
->zbranch
[i
+ 1];
2448 if (znode
->zbranch
[i
].znode
)
2449 znode
->zbranch
[i
].znode
->iip
= i
;
2453 * If this is the root and it has only 1 child then
2454 * collapse the tree.
2456 if (!znode
->parent
) {
2457 while (znode
->child_cnt
== 1 && znode
->level
!= 0) {
2459 zbr
= &znode
->zbranch
[0];
2460 znode
= get_znode(c
, znode
, 0);
2462 return PTR_ERR(znode
);
2463 znode
= dirty_cow_znode(c
, zbr
);
2465 return PTR_ERR(znode
);
2466 znode
->parent
= NULL
;
2469 err
= insert_old_idx(c
, c
->zroot
.lnum
,
2474 c
->zroot
.lnum
= zbr
->lnum
;
2475 c
->zroot
.offs
= zbr
->offs
;
2476 c
->zroot
.len
= zbr
->len
;
2477 c
->zroot
.znode
= znode
;
2478 ubifs_assert(!test_bit(OBSOLETE_ZNODE
,
2480 ubifs_assert(test_bit(DIRTY_ZNODE
, &zp
->flags
));
2481 atomic_long_dec(&c
->dirty_zn_cnt
);
2484 __set_bit(OBSOLETE_ZNODE
, &zp
->flags
);
2485 atomic_long_inc(&c
->clean_zn_cnt
);
2486 atomic_long_inc(&ubifs_clean_zn_cnt
);
2496 * ubifs_tnc_remove - remove an index entry of a node.
2497 * @c: UBIFS file-system description object
2500 * Returns %0 on success or negative error code on failure.
2502 int ubifs_tnc_remove(struct ubifs_info
*c
, const union ubifs_key
*key
)
2504 int found
, n
, err
= 0;
2505 struct ubifs_znode
*znode
;
2507 mutex_lock(&c
->tnc_mutex
);
2508 dbg_tnc("key %s", DBGKEY(key
));
2509 found
= lookup_level0_dirty(c
, key
, &znode
, &n
);
2515 err
= tnc_delete(c
, znode
, n
);
2517 err
= dbg_check_tnc(c
, 0);
2520 mutex_unlock(&c
->tnc_mutex
);
2525 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2526 * @c: UBIFS file-system description object
2528 * @nm: directory entry name
2530 * Returns %0 on success or negative error code on failure.
2532 int ubifs_tnc_remove_nm(struct ubifs_info
*c
, const union ubifs_key
*key
,
2533 const struct qstr
*nm
)
2536 struct ubifs_znode
*znode
;
2538 mutex_lock(&c
->tnc_mutex
);
2539 dbg_tnc("%.*s, key %s", nm
->len
, nm
->name
, DBGKEY(key
));
2540 err
= lookup_level0_dirty(c
, key
, &znode
, &n
);
2546 err
= fallible_resolve_collision(c
, key
, &znode
, &n
,
2549 err
= resolve_collision(c
, key
, &znode
, &n
, nm
);
2550 dbg_tnc("rc returned %d, znode %p, n %d", err
, znode
, n
);
2554 /* Ensure the znode is dirtied */
2555 if (znode
->cnext
|| !ubifs_zn_dirty(znode
)) {
2556 znode
= dirty_cow_bottom_up(c
, znode
);
2557 if (IS_ERR(znode
)) {
2558 err
= PTR_ERR(znode
);
2562 err
= tnc_delete(c
, znode
, n
);
2568 err
= dbg_check_tnc(c
, 0);
2569 mutex_unlock(&c
->tnc_mutex
);
2574 * key_in_range - determine if a key falls within a range of keys.
2575 * @c: UBIFS file-system description object
2576 * @key: key to check
2577 * @from_key: lowest key in range
2578 * @to_key: highest key in range
2580 * This function returns %1 if the key is in range and %0 otherwise.
2582 static int key_in_range(struct ubifs_info
*c
, union ubifs_key
*key
,
2583 union ubifs_key
*from_key
, union ubifs_key
*to_key
)
2585 if (keys_cmp(c
, key
, from_key
) < 0)
2587 if (keys_cmp(c
, key
, to_key
) > 0)
2593 * ubifs_tnc_remove_range - remove index entries in range.
2594 * @c: UBIFS file-system description object
2595 * @from_key: lowest key to remove
2596 * @to_key: highest key to remove
2598 * This function removes index entries starting at @from_key and ending at
2599 * @to_key. This function returns zero in case of success and a negative error
2600 * code in case of failure.
2602 int ubifs_tnc_remove_range(struct ubifs_info
*c
, union ubifs_key
*from_key
,
2603 union ubifs_key
*to_key
)
2605 int i
, n
, k
, err
= 0;
2606 struct ubifs_znode
*znode
;
2607 union ubifs_key
*key
;
2609 mutex_lock(&c
->tnc_mutex
);
2611 /* Find first level 0 znode that contains keys to remove */
2612 err
= ubifs_lookup_level0(c
, from_key
, &znode
, &n
);
2619 err
= tnc_next(c
, &znode
, &n
);
2620 if (err
== -ENOENT
) {
2626 key
= &znode
->zbranch
[n
].key
;
2627 if (!key_in_range(c
, key
, from_key
, to_key
)) {
2633 /* Ensure the znode is dirtied */
2634 if (znode
->cnext
|| !ubifs_zn_dirty(znode
)) {
2635 znode
= dirty_cow_bottom_up(c
, znode
);
2636 if (IS_ERR(znode
)) {
2637 err
= PTR_ERR(znode
);
2642 /* Remove all keys in range except the first */
2643 for (i
= n
+ 1, k
= 0; i
< znode
->child_cnt
; i
++, k
++) {
2644 key
= &znode
->zbranch
[i
].key
;
2645 if (!key_in_range(c
, key
, from_key
, to_key
))
2647 lnc_free(&znode
->zbranch
[i
]);
2648 err
= ubifs_add_dirt(c
, znode
->zbranch
[i
].lnum
,
2649 znode
->zbranch
[i
].len
);
2651 dbg_dump_znode(c
, znode
);
2654 dbg_tnc("removing %s", DBGKEY(key
));
2657 for (i
= n
+ 1 + k
; i
< znode
->child_cnt
; i
++)
2658 znode
->zbranch
[i
- k
] = znode
->zbranch
[i
];
2659 znode
->child_cnt
-= k
;
2662 /* Now delete the first */
2663 err
= tnc_delete(c
, znode
, n
);
2670 err
= dbg_check_tnc(c
, 0);
2671 mutex_unlock(&c
->tnc_mutex
);
2676 * ubifs_tnc_remove_ino - remove an inode from TNC.
2677 * @c: UBIFS file-system description object
2678 * @inum: inode number to remove
2680 * This function remove inode @inum and all the extended attributes associated
2681 * with the anode from TNC and returns zero in case of success or a negative
2682 * error code in case of failure.
2684 int ubifs_tnc_remove_ino(struct ubifs_info
*c
, ino_t inum
)
2686 union ubifs_key key1
, key2
;
2687 struct ubifs_dent_node
*xent
, *pxent
= NULL
;
2688 struct qstr nm
= { .name
= NULL
};
2690 dbg_tnc("ino %lu", (unsigned long)inum
);
2693 * Walk all extended attribute entries and remove them together with
2694 * corresponding extended attribute inodes.
2696 lowest_xent_key(c
, &key1
, inum
);
2701 xent
= ubifs_tnc_next_ent(c
, &key1
, &nm
);
2703 err
= PTR_ERR(xent
);
2709 xattr_inum
= le64_to_cpu(xent
->inum
);
2710 dbg_tnc("xent '%s', ino %lu", xent
->name
,
2711 (unsigned long)xattr_inum
);
2713 nm
.name
= xent
->name
;
2714 nm
.len
= le16_to_cpu(xent
->nlen
);
2715 err
= ubifs_tnc_remove_nm(c
, &key1
, &nm
);
2721 lowest_ino_key(c
, &key1
, xattr_inum
);
2722 highest_ino_key(c
, &key2
, xattr_inum
);
2723 err
= ubifs_tnc_remove_range(c
, &key1
, &key2
);
2731 key_read(c
, &xent
->key
, &key1
);
2735 lowest_ino_key(c
, &key1
, inum
);
2736 highest_ino_key(c
, &key2
, inum
);
2738 return ubifs_tnc_remove_range(c
, &key1
, &key2
);
2742 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2743 * @c: UBIFS file-system description object
2744 * @key: key of last entry
2745 * @nm: name of last entry found or %NULL
2747 * This function finds and reads the next directory or extended attribute entry
2748 * after the given key (@key) if there is one. @nm is used to resolve
2751 * If the name of the current entry is not known and only the key is known,
2752 * @nm->name has to be %NULL. In this case the semantics of this function is a
2753 * little bit different and it returns the entry corresponding to this key, not
2754 * the next one. If the key was not found, the closest "right" entry is
2757 * If the fist entry has to be found, @key has to contain the lowest possible
2758 * key value for this inode and @name has to be %NULL.
2760 * This function returns the found directory or extended attribute entry node
2761 * in case of success, %-ENOENT is returned if no entry was found, and a
2762 * negative error code is returned in case of failure.
2764 struct ubifs_dent_node
*ubifs_tnc_next_ent(struct ubifs_info
*c
,
2765 union ubifs_key
*key
,
2766 const struct qstr
*nm
)
2768 int n
, err
, type
= key_type(c
, key
);
2769 struct ubifs_znode
*znode
;
2770 struct ubifs_dent_node
*dent
;
2771 struct ubifs_zbranch
*zbr
;
2772 union ubifs_key
*dkey
;
2774 dbg_tnc("%s %s", nm
->name
? (char *)nm
->name
: "(lowest)", DBGKEY(key
));
2775 ubifs_assert(is_hash_key(c
, key
));
2777 mutex_lock(&c
->tnc_mutex
);
2778 err
= ubifs_lookup_level0(c
, key
, &znode
, &n
);
2779 if (unlikely(err
< 0))
2784 /* Handle collisions */
2785 err
= resolve_collision(c
, key
, &znode
, &n
, nm
);
2786 dbg_tnc("rc returned %d, znode %p, n %d",
2788 if (unlikely(err
< 0))
2792 /* Now find next entry */
2793 err
= tnc_next(c
, &znode
, &n
);
2798 * The full name of the entry was not given, in which case the
2799 * behavior of this function is a little different and it
2800 * returns current entry, not the next one.
2804 * However, the given key does not exist in the TNC
2805 * tree and @znode/@n variables contain the closest
2806 * "preceding" element. Switch to the next one.
2808 err
= tnc_next(c
, &znode
, &n
);
2814 zbr
= &znode
->zbranch
[n
];
2815 dent
= kmalloc(zbr
->len
, GFP_NOFS
);
2816 if (unlikely(!dent
)) {
2822 * The above 'tnc_next()' call could lead us to the next inode, check
2826 if (key_inum(c
, dkey
) != key_inum(c
, key
) ||
2827 key_type(c
, dkey
) != type
) {
2832 err
= tnc_read_node_nm(c
, zbr
, dent
);
2836 mutex_unlock(&c
->tnc_mutex
);
2842 mutex_unlock(&c
->tnc_mutex
);
2843 return ERR_PTR(err
);
2847 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2848 * @c: UBIFS file-system description object
2850 * Destroy left-over obsolete znodes from a failed commit.
2852 static void tnc_destroy_cnext(struct ubifs_info
*c
)
2854 struct ubifs_znode
*cnext
;
2858 ubifs_assert(c
->cmt_state
== COMMIT_BROKEN
);
2861 struct ubifs_znode
*znode
= cnext
;
2863 cnext
= cnext
->cnext
;
2864 if (test_bit(OBSOLETE_ZNODE
, &znode
->flags
))
2866 } while (cnext
&& cnext
!= c
->cnext
);
2870 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2871 * @c: UBIFS file-system description object
2873 void ubifs_tnc_close(struct ubifs_info
*c
)
2877 tnc_destroy_cnext(c
);
2878 if (c
->zroot
.znode
) {
2879 clean_freed
= ubifs_destroy_tnc_subtree(c
->zroot
.znode
);
2880 atomic_long_sub(clean_freed
, &ubifs_clean_zn_cnt
);
2888 * left_znode - get the znode to the left.
2889 * @c: UBIFS file-system description object
2892 * This function returns a pointer to the znode to the left of @znode or NULL if
2893 * there is not one. A negative error code is returned on failure.
2895 static struct ubifs_znode
*left_znode(struct ubifs_info
*c
,
2896 struct ubifs_znode
*znode
)
2898 int level
= znode
->level
;
2901 int n
= znode
->iip
- 1;
2903 /* Go up until we can go left */
2904 znode
= znode
->parent
;
2908 /* Now go down the rightmost branch to 'level' */
2909 znode
= get_znode(c
, znode
, n
);
2912 while (znode
->level
!= level
) {
2913 n
= znode
->child_cnt
- 1;
2914 znode
= get_znode(c
, znode
, n
);
2925 * right_znode - get the znode to the right.
2926 * @c: UBIFS file-system description object
2929 * This function returns a pointer to the znode to the right of @znode or NULL
2930 * if there is not one. A negative error code is returned on failure.
2932 static struct ubifs_znode
*right_znode(struct ubifs_info
*c
,
2933 struct ubifs_znode
*znode
)
2935 int level
= znode
->level
;
2938 int n
= znode
->iip
+ 1;
2940 /* Go up until we can go right */
2941 znode
= znode
->parent
;
2944 if (n
< znode
->child_cnt
) {
2945 /* Now go down the leftmost branch to 'level' */
2946 znode
= get_znode(c
, znode
, n
);
2949 while (znode
->level
!= level
) {
2950 znode
= get_znode(c
, znode
, 0);
2961 * lookup_znode - find a particular indexing node from TNC.
2962 * @c: UBIFS file-system description object
2963 * @key: index node key to lookup
2964 * @level: index node level
2965 * @lnum: index node LEB number
2966 * @offs: index node offset
2968 * This function searches an indexing node by its first key @key and its
2969 * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
2970 * nodes it traverses to TNC. This function is called for indexing nodes which
2971 * were found on the media by scanning, for example when garbage-collecting or
2972 * when doing in-the-gaps commit. This means that the indexing node which is
2973 * looked for does not have to have exactly the same leftmost key @key, because
2974 * the leftmost key may have been changed, in which case TNC will contain a
2975 * dirty znode which still refers the same @lnum:@offs. This function is clever
2976 * enough to recognize such indexing nodes.
2978 * Note, if a znode was deleted or changed too much, then this function will
2979 * not find it. For situations like this UBIFS has the old index RB-tree
2980 * (indexed by @lnum:@offs).
2982 * This function returns a pointer to the znode found or %NULL if it is not
2983 * found. A negative error code is returned on failure.
2985 static struct ubifs_znode
*lookup_znode(struct ubifs_info
*c
,
2986 union ubifs_key
*key
, int level
,
2989 struct ubifs_znode
*znode
, *zn
;
2992 ubifs_assert(key_type(c
, key
) < UBIFS_INVALID_KEY
);
2995 * The arguments have probably been read off flash, so don't assume
2999 return ERR_PTR(-EINVAL
);
3001 /* Get the root znode */
3002 znode
= c
->zroot
.znode
;
3004 znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
3008 /* Check if it is the one we are looking for */
3009 if (c
->zroot
.lnum
== lnum
&& c
->zroot
.offs
== offs
)
3011 /* Descend to the parent level i.e. (level + 1) */
3012 if (level
>= znode
->level
)
3015 ubifs_search_zbranch(c
, znode
, key
, &n
);
3018 * We reached a znode where the leftmost key is greater
3019 * than the key we are searching for. This is the same
3020 * situation as the one described in a huge comment at
3021 * the end of the 'ubifs_lookup_level0()' function. And
3022 * for exactly the same reasons we have to try to look
3023 * left before giving up.
3025 znode
= left_znode(c
, znode
);
3030 ubifs_search_zbranch(c
, znode
, key
, &n
);
3031 ubifs_assert(n
>= 0);
3033 if (znode
->level
== level
+ 1)
3035 znode
= get_znode(c
, znode
, n
);
3039 /* Check if the child is the one we are looking for */
3040 if (znode
->zbranch
[n
].lnum
== lnum
&& znode
->zbranch
[n
].offs
== offs
)
3041 return get_znode(c
, znode
, n
);
3042 /* If the key is unique, there is nowhere else to look */
3043 if (!is_hash_key(c
, key
))
3046 * The key is not unique and so may be also in the znodes to either
3053 /* Move one branch to the left */
3057 znode
= left_znode(c
, znode
);
3062 n
= znode
->child_cnt
- 1;
3065 if (znode
->zbranch
[n
].lnum
== lnum
&&
3066 znode
->zbranch
[n
].offs
== offs
)
3067 return get_znode(c
, znode
, n
);
3068 /* Stop if the key is less than the one we are looking for */
3069 if (keys_cmp(c
, &znode
->zbranch
[n
].key
, key
) < 0)
3072 /* Back to the middle */
3077 /* Move one branch to the right */
3078 if (++n
>= znode
->child_cnt
) {
3079 znode
= right_znode(c
, znode
);
3087 if (znode
->zbranch
[n
].lnum
== lnum
&&
3088 znode
->zbranch
[n
].offs
== offs
)
3089 return get_znode(c
, znode
, n
);
3090 /* Stop if the key is greater than the one we are looking for */
3091 if (keys_cmp(c
, &znode
->zbranch
[n
].key
, key
) > 0)
3098 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3099 * @c: UBIFS file-system description object
3100 * @key: key of index node
3101 * @level: index node level
3102 * @lnum: LEB number of index node
3103 * @offs: offset of index node
3105 * This function returns %0 if the index node is not referred to in the TNC, %1
3106 * if the index node is referred to in the TNC and the corresponding znode is
3107 * dirty, %2 if an index node is referred to in the TNC and the corresponding
3108 * znode is clean, and a negative error code in case of failure.
3110 * Note, the @key argument has to be the key of the first child. Also note,
3111 * this function relies on the fact that 0:0 is never a valid LEB number and
3112 * offset for a main-area node.
3114 int is_idx_node_in_tnc(struct ubifs_info
*c
, union ubifs_key
*key
, int level
,
3117 struct ubifs_znode
*znode
;
3119 znode
= lookup_znode(c
, key
, level
, lnum
, offs
);
3123 return PTR_ERR(znode
);
3125 return ubifs_zn_dirty(znode
) ? 1 : 2;
3129 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3130 * @c: UBIFS file-system description object
3132 * @lnum: node LEB number
3133 * @offs: node offset
3135 * This function returns %1 if the node is referred to in the TNC, %0 if it is
3136 * not, and a negative error code in case of failure.
3138 * Note, this function relies on the fact that 0:0 is never a valid LEB number
3139 * and offset for a main-area node.
3141 static int is_leaf_node_in_tnc(struct ubifs_info
*c
, union ubifs_key
*key
,
3144 struct ubifs_zbranch
*zbr
;
3145 struct ubifs_znode
*znode
, *zn
;
3146 int n
, found
, err
, nn
;
3147 const int unique
= !is_hash_key(c
, key
);
3149 found
= ubifs_lookup_level0(c
, key
, &znode
, &n
);
3151 return found
; /* Error code */
3154 zbr
= &znode
->zbranch
[n
];
3155 if (lnum
== zbr
->lnum
&& offs
== zbr
->offs
)
3156 return 1; /* Found it */
3160 * Because the key is not unique, we have to look left
3167 err
= tnc_prev(c
, &znode
, &n
);
3172 if (keys_cmp(c
, key
, &znode
->zbranch
[n
].key
))
3174 zbr
= &znode
->zbranch
[n
];
3175 if (lnum
== zbr
->lnum
&& offs
== zbr
->offs
)
3176 return 1; /* Found it */
3182 err
= tnc_next(c
, &znode
, &n
);
3188 if (keys_cmp(c
, key
, &znode
->zbranch
[n
].key
))
3190 zbr
= &znode
->zbranch
[n
];
3191 if (lnum
== zbr
->lnum
&& offs
== zbr
->offs
)
3192 return 1; /* Found it */
3198 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3199 * @c: UBIFS file-system description object
3201 * @level: index node level (if it is an index node)
3202 * @lnum: node LEB number
3203 * @offs: node offset
3204 * @is_idx: non-zero if the node is an index node
3206 * This function returns %1 if the node is in the TNC, %0 if it is not, and a
3207 * negative error code in case of failure. For index nodes, @key has to be the
3208 * key of the first child. An index node is considered to be in the TNC only if
3209 * the corresponding znode is clean or has not been loaded.
3211 int ubifs_tnc_has_node(struct ubifs_info
*c
, union ubifs_key
*key
, int level
,
3212 int lnum
, int offs
, int is_idx
)
3216 mutex_lock(&c
->tnc_mutex
);
3218 err
= is_idx_node_in_tnc(c
, key
, level
, lnum
, offs
);
3222 /* The index node was found but it was dirty */
3225 /* The index node was found and it was clean */
3230 err
= is_leaf_node_in_tnc(c
, key
, lnum
, offs
);
3233 mutex_unlock(&c
->tnc_mutex
);
3238 * ubifs_dirty_idx_node - dirty an index node.
3239 * @c: UBIFS file-system description object
3240 * @key: index node key
3241 * @level: index node level
3242 * @lnum: index node LEB number
3243 * @offs: index node offset
3245 * This function loads and dirties an index node so that it can be garbage
3246 * collected. The @key argument has to be the key of the first child. This
3247 * function relies on the fact that 0:0 is never a valid LEB number and offset
3248 * for a main-area node. Returns %0 on success and a negative error code on
3251 int ubifs_dirty_idx_node(struct ubifs_info
*c
, union ubifs_key
*key
, int level
,
3254 struct ubifs_znode
*znode
;
3257 mutex_lock(&c
->tnc_mutex
);
3258 znode
= lookup_znode(c
, key
, level
, lnum
, offs
);
3261 if (IS_ERR(znode
)) {
3262 err
= PTR_ERR(znode
);
3265 znode
= dirty_cow_bottom_up(c
, znode
);
3266 if (IS_ERR(znode
)) {
3267 err
= PTR_ERR(znode
);
3272 mutex_unlock(&c
->tnc_mutex
);
3276 #ifdef CONFIG_UBIFS_FS_DEBUG
3279 * dbg_check_inode_size - check if inode size is correct.
3280 * @c: UBIFS file-system description object
3281 * @inum: inode number
3284 * This function makes sure that the inode size (@size) is correct and it does
3285 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3286 * if it has a data page beyond @size, and other negative error code in case of
3289 int dbg_check_inode_size(struct ubifs_info
*c
, const struct inode
*inode
,
3293 union ubifs_key from_key
, to_key
, *key
;
3294 struct ubifs_znode
*znode
;
3297 if (!S_ISREG(inode
->i_mode
))
3299 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
3302 block
= (size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
3303 data_key_init(c
, &from_key
, inode
->i_ino
, block
);
3304 highest_data_key(c
, &to_key
, inode
->i_ino
);
3306 mutex_lock(&c
->tnc_mutex
);
3307 err
= ubifs_lookup_level0(c
, &from_key
, &znode
, &n
);
3317 err
= tnc_next(c
, &znode
, &n
);
3318 if (err
== -ENOENT
) {
3325 ubifs_assert(err
== 0);
3326 key
= &znode
->zbranch
[n
].key
;
3327 if (!key_in_range(c
, key
, &from_key
, &to_key
))
3331 block
= key_block(c
, key
);
3332 ubifs_err("inode %lu has size %lld, but there are data at offset %lld "
3333 "(data key %s)", (unsigned long)inode
->i_ino
, size
,
3334 ((loff_t
)block
) << UBIFS_BLOCK_SHIFT
, DBGKEY(key
));
3335 dbg_dump_inode(c
, inode
);
3340 mutex_unlock(&c
->tnc_mutex
);
3344 #endif /* CONFIG_UBIFS_FS_DEBUG */
This page took 0.097378 seconds and 5 git commands to generate.