]>
git.ipfire.org Git - people/ms/u-boot.git/blob - fs/ubifs/tnc_misc.c
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
13 * This file contains miscelanious TNC-related functions shared betweend
14 * different files. This file does not form any logically separate TNC
15 * sub-system. The file was created because there is a lot of TNC code and
16 * putting it all in one file would make that file too big and unreadable.
20 #include <linux/err.h>
25 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
26 * @zr: root of the subtree to traverse
27 * @znode: previous znode
29 * This function implements levelorder TNC traversal. The LNC is ignored.
30 * Returns the next element or %NULL if @znode is already the last one.
32 struct ubifs_znode
*ubifs_tnc_levelorder_next(struct ubifs_znode
*zr
,
33 struct ubifs_znode
*znode
)
35 int level
, iip
, level_search
= 0;
36 struct ubifs_znode
*zn
;
43 if (unlikely(znode
== zr
)) {
44 if (znode
->level
== 0)
46 return ubifs_tnc_find_child(zr
, 0);
53 ubifs_assert(znode
->level
<= zr
->level
);
56 * First walk up until there is a znode with next branch to
59 while (znode
->parent
!= zr
&& iip
>= znode
->parent
->child_cnt
) {
60 znode
= znode
->parent
;
64 if (unlikely(znode
->parent
== zr
&&
65 iip
>= znode
->parent
->child_cnt
)) {
66 /* This level is done, switch to the lower one */
68 if (level_search
|| level
< 0)
70 * We were already looking for znode at lower
71 * level ('level_search'). As we are here
72 * again, it just does not exist. Or all levels
73 * were finished ('level < 0').
79 znode
= ubifs_tnc_find_child(zr
, 0);
83 /* Switch to the next index */
84 zn
= ubifs_tnc_find_child(znode
->parent
, iip
+ 1);
86 /* No more children to look at, we have walk up */
87 iip
= znode
->parent
->child_cnt
;
91 /* Walk back down to the level we came from ('level') */
92 while (zn
->level
!= level
) {
94 zn
= ubifs_tnc_find_child(zn
, 0);
97 * This path is not too deep so it does not
98 * reach 'level'. Try next path.
106 ubifs_assert(zn
->level
>= 0);
113 * ubifs_search_zbranch - search znode branch.
114 * @c: UBIFS file-system description object
115 * @znode: znode to search in
116 * @key: key to search for
117 * @n: znode branch slot number is returned here
119 * This is a helper function which search branch with key @key in @znode using
120 * binary search. The result of the search may be:
121 * o exact match, then %1 is returned, and the slot number of the branch is
123 * o no exact match, then %0 is returned and the slot number of the left
124 * closest branch is returned in @n; the slot if all keys in this znode are
125 * greater than @key, then %-1 is returned in @n.
127 int ubifs_search_zbranch(const struct ubifs_info
*c
,
128 const struct ubifs_znode
*znode
,
129 const union ubifs_key
*key
, int *n
)
131 int beg
= 0, end
= znode
->child_cnt
, uninitialized_var(mid
);
132 int uninitialized_var(cmp
);
133 const struct ubifs_zbranch
*zbr
= &znode
->zbranch
[0];
135 ubifs_assert(end
> beg
);
138 mid
= (beg
+ end
) >> 1;
139 cmp
= keys_cmp(c
, key
, &zbr
[mid
].key
);
152 /* The insert point is after *n */
153 ubifs_assert(*n
>= -1 && *n
< znode
->child_cnt
);
155 ubifs_assert(keys_cmp(c
, key
, &zbr
[0].key
) < 0);
157 ubifs_assert(keys_cmp(c
, key
, &zbr
[*n
].key
) > 0);
158 if (*n
+ 1 < znode
->child_cnt
)
159 ubifs_assert(keys_cmp(c
, key
, &zbr
[*n
+ 1].key
) < 0);
165 * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
166 * @znode: znode to start at (root of the sub-tree to traverse)
168 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
171 struct ubifs_znode
*ubifs_tnc_postorder_first(struct ubifs_znode
*znode
)
173 if (unlikely(!znode
))
176 while (znode
->level
> 0) {
177 struct ubifs_znode
*child
;
179 child
= ubifs_tnc_find_child(znode
, 0);
189 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
190 * @znode: previous znode
192 * This function implements postorder TNC traversal. The LNC is ignored.
193 * Returns the next element or %NULL if @znode is already the last one.
195 struct ubifs_znode
*ubifs_tnc_postorder_next(struct ubifs_znode
*znode
)
197 struct ubifs_znode
*zn
;
200 if (unlikely(!znode
->parent
))
203 /* Switch to the next index in the parent */
204 zn
= ubifs_tnc_find_child(znode
->parent
, znode
->iip
+ 1);
206 /* This is in fact the last child, return parent */
207 return znode
->parent
;
209 /* Go to the first znode in this new subtree */
210 return ubifs_tnc_postorder_first(zn
);
214 * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
215 * @znode: znode defining subtree to destroy
217 * This function destroys subtree of the TNC tree. Returns number of clean
218 * znodes in the subtree.
220 long ubifs_destroy_tnc_subtree(struct ubifs_znode
*znode
)
222 struct ubifs_znode
*zn
= ubifs_tnc_postorder_first(znode
);
223 long clean_freed
= 0;
228 for (n
= 0; n
< zn
->child_cnt
; n
++) {
229 if (!zn
->zbranch
[n
].znode
)
233 !ubifs_zn_dirty(zn
->zbranch
[n
].znode
))
237 kfree(zn
->zbranch
[n
].znode
);
241 if (!ubifs_zn_dirty(zn
))
247 zn
= ubifs_tnc_postorder_next(zn
);
252 * read_znode - read an indexing node from flash and fill znode.
253 * @c: UBIFS file-system description object
254 * @lnum: LEB of the indexing node to read
257 * @znode: znode to read to
259 * This function reads an indexing node from the flash media and fills znode
260 * with the read data. Returns zero in case of success and a negative error
261 * code in case of failure. The read indexing node is validated and if anything
262 * is wrong with it, this function prints complaint messages and returns
265 static int read_znode(struct ubifs_info
*c
, int lnum
, int offs
, int len
,
266 struct ubifs_znode
*znode
)
268 int i
, err
, type
, cmp
;
269 struct ubifs_idx_node
*idx
;
271 idx
= kmalloc(c
->max_idx_node_sz
, GFP_NOFS
);
275 err
= ubifs_read_node(c
, idx
, UBIFS_IDX_NODE
, len
, lnum
, offs
);
281 znode
->child_cnt
= le16_to_cpu(idx
->child_cnt
);
282 znode
->level
= le16_to_cpu(idx
->level
);
284 dbg_tnc("LEB %d:%d, level %d, %d branch",
285 lnum
, offs
, znode
->level
, znode
->child_cnt
);
287 if (znode
->child_cnt
> c
->fanout
|| znode
->level
> UBIFS_MAX_LEVELS
) {
288 ubifs_err("current fanout %d, branch count %d",
289 c
->fanout
, znode
->child_cnt
);
290 ubifs_err("max levels %d, znode level %d",
291 UBIFS_MAX_LEVELS
, znode
->level
);
296 for (i
= 0; i
< znode
->child_cnt
; i
++) {
297 const struct ubifs_branch
*br
= ubifs_idx_branch(c
, idx
, i
);
298 struct ubifs_zbranch
*zbr
= &znode
->zbranch
[i
];
300 key_read(c
, &br
->key
, &zbr
->key
);
301 zbr
->lnum
= le32_to_cpu(br
->lnum
);
302 zbr
->offs
= le32_to_cpu(br
->offs
);
303 zbr
->len
= le32_to_cpu(br
->len
);
306 /* Validate branch */
308 if (zbr
->lnum
< c
->main_first
||
309 zbr
->lnum
>= c
->leb_cnt
|| zbr
->offs
< 0 ||
310 zbr
->offs
+ zbr
->len
> c
->leb_size
|| zbr
->offs
& 7) {
311 ubifs_err("bad branch %d", i
);
316 switch (key_type(c
, &zbr
->key
)) {
323 ubifs_err("bad key type at slot %d: %d",
324 i
, key_type(c
, &zbr
->key
));
332 type
= key_type(c
, &zbr
->key
);
333 if (c
->ranges
[type
].max_len
== 0) {
334 if (zbr
->len
!= c
->ranges
[type
].len
) {
335 ubifs_err("bad target node (type %d) length (%d)",
337 ubifs_err("have to be %d", c
->ranges
[type
].len
);
341 } else if (zbr
->len
< c
->ranges
[type
].min_len
||
342 zbr
->len
> c
->ranges
[type
].max_len
) {
343 ubifs_err("bad target node (type %d) length (%d)",
345 ubifs_err("have to be in range of %d-%d",
346 c
->ranges
[type
].min_len
,
347 c
->ranges
[type
].max_len
);
354 * Ensure that the next key is greater or equivalent to the
357 for (i
= 0; i
< znode
->child_cnt
- 1; i
++) {
358 const union ubifs_key
*key1
, *key2
;
360 key1
= &znode
->zbranch
[i
].key
;
361 key2
= &znode
->zbranch
[i
+ 1].key
;
363 cmp
= keys_cmp(c
, key1
, key2
);
365 ubifs_err("bad key order (keys %d and %d)", i
, i
+ 1);
368 } else if (cmp
== 0 && !is_hash_key(c
, key1
)) {
369 /* These can only be keys with colliding hash */
370 ubifs_err("keys %d and %d are not hashed but equivalent",
381 ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum
, offs
, err
);
382 ubifs_dump_node(c
, idx
);
388 * ubifs_load_znode - load znode to TNC cache.
389 * @c: UBIFS file-system description object
391 * @parent: znode's parent
392 * @iip: index in parent
394 * This function loads znode pointed to by @zbr into the TNC cache and
395 * returns pointer to it in case of success and a negative error code in case
398 struct ubifs_znode
*ubifs_load_znode(struct ubifs_info
*c
,
399 struct ubifs_zbranch
*zbr
,
400 struct ubifs_znode
*parent
, int iip
)
403 struct ubifs_znode
*znode
;
405 ubifs_assert(!zbr
->znode
);
407 * A slab cache is not presently used for znodes because the znode size
408 * depends on the fanout which is stored in the superblock.
410 znode
= kzalloc(c
->max_znode_sz
, GFP_NOFS
);
412 return ERR_PTR(-ENOMEM
);
414 err
= read_znode(c
, zbr
->lnum
, zbr
->offs
, zbr
->len
, znode
);
418 atomic_long_inc(&c
->clean_zn_cnt
);
421 * Increment the global clean znode counter as well. It is OK that
422 * global and per-FS clean znode counters may be inconsistent for some
423 * short time (because we might be preempted at this point), the global
424 * one is only used in shrinker.
426 atomic_long_inc(&ubifs_clean_zn_cnt
);
429 znode
->parent
= parent
;
430 znode
->time
= get_seconds();
441 * ubifs_tnc_read_node - read a leaf node from the flash media.
442 * @c: UBIFS file-system description object
443 * @zbr: key and position of the node
444 * @node: node is returned here
446 * This function reads a node defined by @zbr from the flash media. Returns
447 * zero in case of success or a negative negative error code in case of
450 int ubifs_tnc_read_node(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
453 union ubifs_key key1
, *key
= &zbr
->key
;
454 int err
, type
= key_type(c
, key
);
455 struct ubifs_wbuf
*wbuf
;
458 * 'zbr' has to point to on-flash node. The node may sit in a bud and
459 * may even be in a write buffer, so we have to take care about this.
461 wbuf
= ubifs_get_wbuf(c
, zbr
->lnum
);
463 err
= ubifs_read_node_wbuf(wbuf
, node
, type
, zbr
->len
,
464 zbr
->lnum
, zbr
->offs
);
466 err
= ubifs_read_node(c
, node
, type
, zbr
->len
, zbr
->lnum
,
470 dbg_tnck(key
, "key ");
474 /* Make sure the key of the read node is correct */
475 key_read(c
, node
+ UBIFS_KEY_OFFSET
, &key1
);
476 if (!keys_eq(c
, key
, &key1
)) {
477 ubifs_err("bad key in node at LEB %d:%d",
478 zbr
->lnum
, zbr
->offs
);
479 dbg_tnck(key
, "looked for key ");
480 dbg_tnck(&key1
, "but found node's key ");
481 ubifs_dump_node(c
, node
);