1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
12 * This file contains miscelanious TNC-related functions shared betweend
13 * different files. This file does not form any logically separate TNC
14 * sub-system. The file was created because there is a lot of TNC code and
15 * putting it all in one file would make that file too big and unreadable.
19 #include <linux/err.h>
24 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
25 * @zr: root of the subtree to traverse
26 * @znode: previous znode
28 * This function implements levelorder TNC traversal. The LNC is ignored.
29 * Returns the next element or %NULL if @znode is already the last one.
31 struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
32 struct ubifs_znode *znode)
34 int level, iip, level_search = 0;
35 struct ubifs_znode *zn;
42 if (unlikely(znode == zr)) {
43 if (znode->level == 0)
45 return ubifs_tnc_find_child(zr, 0);
52 ubifs_assert(znode->level <= zr->level);
55 * First walk up until there is a znode with next branch to
58 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
59 znode = znode->parent;
63 if (unlikely(znode->parent == zr &&
64 iip >= znode->parent->child_cnt)) {
65 /* This level is done, switch to the lower one */
67 if (level_search || level < 0)
69 * We were already looking for znode at lower
70 * level ('level_search'). As we are here
71 * again, it just does not exist. Or all levels
72 * were finished ('level < 0').
78 znode = ubifs_tnc_find_child(zr, 0);
82 /* Switch to the next index */
83 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
85 /* No more children to look at, we have walk up */
86 iip = znode->parent->child_cnt;
90 /* Walk back down to the level we came from ('level') */
91 while (zn->level != level) {
93 zn = ubifs_tnc_find_child(zn, 0);
96 * This path is not too deep so it does not
97 * reach 'level'. Try next path.
105 ubifs_assert(zn->level >= 0);
112 * ubifs_search_zbranch - search znode branch.
113 * @c: UBIFS file-system description object
114 * @znode: znode to search in
115 * @key: key to search for
116 * @n: znode branch slot number is returned here
118 * This is a helper function which search branch with key @key in @znode using
119 * binary search. The result of the search may be:
120 * o exact match, then %1 is returned, and the slot number of the branch is
122 * o no exact match, then %0 is returned and the slot number of the left
123 * closest branch is returned in @n; the slot if all keys in this znode are
124 * greater than @key, then %-1 is returned in @n.
126 int ubifs_search_zbranch(const struct ubifs_info *c,
127 const struct ubifs_znode *znode,
128 const union ubifs_key *key, int *n)
130 int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
131 int uninitialized_var(cmp);
132 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
134 ubifs_assert(end > beg);
137 mid = (beg + end) >> 1;
138 cmp = keys_cmp(c, key, &zbr[mid].key);
151 /* The insert point is after *n */
152 ubifs_assert(*n >= -1 && *n < znode->child_cnt);
154 ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
156 ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
157 if (*n + 1 < znode->child_cnt)
158 ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
164 * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
165 * @znode: znode to start at (root of the sub-tree to traverse)
167 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
170 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
172 if (unlikely(!znode))
175 while (znode->level > 0) {
176 struct ubifs_znode *child;
178 child = ubifs_tnc_find_child(znode, 0);
188 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
189 * @znode: previous znode
191 * This function implements postorder TNC traversal. The LNC is ignored.
192 * Returns the next element or %NULL if @znode is already the last one.
194 struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
196 struct ubifs_znode *zn;
199 if (unlikely(!znode->parent))
202 /* Switch to the next index in the parent */
203 zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
205 /* This is in fact the last child, return parent */
206 return znode->parent;
208 /* Go to the first znode in this new subtree */
209 return ubifs_tnc_postorder_first(zn);
213 * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
214 * @znode: znode defining subtree to destroy
216 * This function destroys subtree of the TNC tree. Returns number of clean
217 * znodes in the subtree.
219 long ubifs_destroy_tnc_subtree(struct ubifs_znode *znode)
221 struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
222 long clean_freed = 0;
227 for (n = 0; n < zn->child_cnt; n++) {
228 if (!zn->zbranch[n].znode)
232 !ubifs_zn_dirty(zn->zbranch[n].znode))
236 kfree(zn->zbranch[n].znode);
240 if (!ubifs_zn_dirty(zn))
246 zn = ubifs_tnc_postorder_next(zn);
251 * read_znode - read an indexing node from flash and fill znode.
252 * @c: UBIFS file-system description object
253 * @lnum: LEB of the indexing node to read
256 * @znode: znode to read to
258 * This function reads an indexing node from the flash media and fills znode
259 * with the read data. Returns zero in case of success and a negative error
260 * code in case of failure. The read indexing node is validated and if anything
261 * is wrong with it, this function prints complaint messages and returns
264 static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
265 struct ubifs_znode *znode)
267 int i, err, type, cmp;
268 struct ubifs_idx_node *idx;
270 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
274 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
280 znode->child_cnt = le16_to_cpu(idx->child_cnt);
281 znode->level = le16_to_cpu(idx->level);
283 dbg_tnc("LEB %d:%d, level %d, %d branch",
284 lnum, offs, znode->level, znode->child_cnt);
286 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
287 ubifs_err(c, "current fanout %d, branch count %d",
288 c->fanout, znode->child_cnt);
289 ubifs_err(c, "max levels %d, znode level %d",
290 UBIFS_MAX_LEVELS, znode->level);
295 for (i = 0; i < znode->child_cnt; i++) {
296 const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
297 struct ubifs_zbranch *zbr = &znode->zbranch[i];
299 key_read(c, &br->key, &zbr->key);
300 zbr->lnum = le32_to_cpu(br->lnum);
301 zbr->offs = le32_to_cpu(br->offs);
302 zbr->len = le32_to_cpu(br->len);
305 /* Validate branch */
307 if (zbr->lnum < c->main_first ||
308 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
309 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
310 ubifs_err(c, "bad branch %d", i);
315 switch (key_type(c, &zbr->key)) {
322 ubifs_err(c, "bad key type at slot %d: %d",
323 i, key_type(c, &zbr->key));
331 type = key_type(c, &zbr->key);
332 if (c->ranges[type].max_len == 0) {
333 if (zbr->len != c->ranges[type].len) {
334 ubifs_err(c, "bad target node (type %d) length (%d)",
336 ubifs_err(c, "have to be %d", c->ranges[type].len);
340 } else if (zbr->len < c->ranges[type].min_len ||
341 zbr->len > c->ranges[type].max_len) {
342 ubifs_err(c, "bad target node (type %d) length (%d)",
344 ubifs_err(c, "have to be in range of %d-%d",
345 c->ranges[type].min_len,
346 c->ranges[type].max_len);
353 * Ensure that the next key is greater or equivalent to the
356 for (i = 0; i < znode->child_cnt - 1; i++) {
357 const union ubifs_key *key1, *key2;
359 key1 = &znode->zbranch[i].key;
360 key2 = &znode->zbranch[i + 1].key;
362 cmp = keys_cmp(c, key1, key2);
364 ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
367 } else if (cmp == 0 && !is_hash_key(c, key1)) {
368 /* These can only be keys with colliding hash */
369 ubifs_err(c, "keys %d and %d are not hashed but equivalent",
380 ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
381 ubifs_dump_node(c, idx);
387 * ubifs_load_znode - load znode to TNC cache.
388 * @c: UBIFS file-system description object
390 * @parent: znode's parent
391 * @iip: index in parent
393 * This function loads znode pointed to by @zbr into the TNC cache and
394 * returns pointer to it in case of success and a negative error code in case
397 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
398 struct ubifs_zbranch *zbr,
399 struct ubifs_znode *parent, int iip)
402 struct ubifs_znode *znode;
404 ubifs_assert(!zbr->znode);
406 * A slab cache is not presently used for znodes because the znode size
407 * depends on the fanout which is stored in the superblock.
409 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
411 return ERR_PTR(-ENOMEM);
413 err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
417 atomic_long_inc(&c->clean_zn_cnt);
420 * Increment the global clean znode counter as well. It is OK that
421 * global and per-FS clean znode counters may be inconsistent for some
422 * short time (because we might be preempted at this point), the global
423 * one is only used in shrinker.
425 atomic_long_inc(&ubifs_clean_zn_cnt);
428 znode->parent = parent;
429 znode->time = get_seconds();
440 * ubifs_tnc_read_node - read a leaf node from the flash media.
441 * @c: UBIFS file-system description object
442 * @zbr: key and position of the node
443 * @node: node is returned here
445 * This function reads a node defined by @zbr from the flash media. Returns
446 * zero in case of success or a negative negative error code in case of
449 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
452 union ubifs_key key1, *key = &zbr->key;
453 int err, type = key_type(c, key);
454 struct ubifs_wbuf *wbuf;
457 * 'zbr' has to point to on-flash node. The node may sit in a bud and
458 * may even be in a write buffer, so we have to take care about this.
460 wbuf = ubifs_get_wbuf(c, zbr->lnum);
462 err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
463 zbr->lnum, zbr->offs);
465 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
469 dbg_tnck(key, "key ");
473 /* Make sure the key of the read node is correct */
474 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
475 if (!keys_eq(c, key, &key1)) {
476 ubifs_err(c, "bad key in node at LEB %d:%d",
477 zbr->lnum, zbr->offs);
478 dbg_tnck(key, "looked for key ");
479 dbg_tnck(&key1, "but found node's key ");
480 ubifs_dump_node(c, node);