1 // SPDX-License-Identifier: GPL-2.0-only
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.
21 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
22 * @c: UBIFS file-system description object
23 * @zr: root of the subtree to traverse
24 * @znode: previous znode
26 * This function implements levelorder TNC traversal. The LNC is ignored.
27 * Returns the next element or %NULL if @znode is already the last one.
29 struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
30 struct ubifs_znode *zr,
31 struct ubifs_znode *znode)
33 int level, iip, level_search = 0;
34 struct ubifs_znode *zn;
41 if (unlikely(znode == zr)) {
42 if (znode->level == 0)
44 return ubifs_tnc_find_child(zr, 0);
51 ubifs_assert(c, znode->level <= zr->level);
54 * First walk up until there is a znode with next branch to
57 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
58 znode = znode->parent;
62 if (unlikely(znode->parent == zr &&
63 iip >= znode->parent->child_cnt)) {
64 /* This level is done, switch to the lower one */
66 if (level_search || level < 0)
68 * We were already looking for znode at lower
69 * level ('level_search'). As we are here
70 * again, it just does not exist. Or all levels
71 * were finished ('level < 0').
77 znode = ubifs_tnc_find_child(zr, 0);
78 ubifs_assert(c, znode);
81 /* Switch to the next index */
82 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
84 /* No more children to look at, we have walk up */
85 iip = znode->parent->child_cnt;
89 /* Walk back down to the level we came from ('level') */
90 while (zn->level != level) {
92 zn = ubifs_tnc_find_child(zn, 0);
95 * This path is not too deep so it does not
96 * reach 'level'. Try next path.
104 ubifs_assert(c, zn->level >= 0);
111 * ubifs_search_zbranch - search znode branch.
112 * @c: UBIFS file-system description object
113 * @znode: znode to search in
114 * @key: key to search for
115 * @n: znode branch slot number is returned here
117 * This is a helper function which search branch with key @key in @znode using
118 * binary search. The result of the search may be:
119 * o exact match, then %1 is returned, and the slot number of the branch is
121 * o no exact match, then %0 is returned and the slot number of the left
122 * closest branch is returned in @n; the slot if all keys in this znode are
123 * greater than @key, then %-1 is returned in @n.
125 int ubifs_search_zbranch(const struct ubifs_info *c,
126 const struct ubifs_znode *znode,
127 const union ubifs_key *key, int *n)
129 int beg = 0, end = znode->child_cnt, mid;
131 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
133 ubifs_assert(c, end > beg);
136 mid = (beg + end) >> 1;
137 cmp = keys_cmp(c, key, &zbr[mid].key);
150 /* The insert point is after *n */
151 ubifs_assert(c, *n >= -1 && *n < znode->child_cnt);
153 ubifs_assert(c, keys_cmp(c, key, &zbr[0].key) < 0);
155 ubifs_assert(c, keys_cmp(c, key, &zbr[*n].key) > 0);
156 if (*n + 1 < znode->child_cnt)
157 ubifs_assert(c, keys_cmp(c, key, &zbr[*n + 1].key) < 0);
163 * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
164 * @znode: znode to start at (root of the sub-tree to traverse)
166 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
169 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
171 if (unlikely(!znode))
174 while (znode->level > 0) {
175 struct ubifs_znode *child;
177 child = ubifs_tnc_find_child(znode, 0);
187 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
188 * @c: UBIFS file-system description object
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(const struct ubifs_info *c,
195 struct ubifs_znode *znode)
197 struct ubifs_znode *zn;
199 ubifs_assert(c, znode);
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 * @c: UBIFS file-system description object
216 * @znode: znode defining subtree to destroy
218 * This function destroys subtree of the TNC tree. Returns number of clean
219 * znodes in the subtree.
221 long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
222 struct ubifs_znode *znode)
224 struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
225 long clean_freed = 0;
230 for (n = 0; n < zn->child_cnt; n++) {
231 if (!zn->zbranch[n].znode)
235 !ubifs_zn_dirty(zn->zbranch[n].znode))
239 kfree(zn->zbranch[n].znode);
243 if (!ubifs_zn_dirty(zn))
249 zn = ubifs_tnc_postorder_next(c, zn);
254 * ubifs_destroy_tnc_tree - destroy all znodes connected to the TNC tree.
255 * @c: UBIFS file-system description object
257 * This function destroys the whole TNC tree and updates clean global znode
260 void ubifs_destroy_tnc_tree(struct ubifs_info *c)
267 n = atomic_long_read(&c->clean_zn_cnt);
268 freed = ubifs_destroy_tnc_subtree(c, c->zroot.znode);
269 ubifs_assert(c, freed == n);
270 atomic_long_sub(n, &ubifs_clean_zn_cnt);
272 c->zroot.znode = NULL;
276 * read_znode - read an indexing node from flash and fill znode.
277 * @c: UBIFS file-system description object
278 * @zzbr: the zbranch describing the node to read
279 * @znode: znode to read to
281 * This function reads an indexing node from the flash media and fills znode
282 * with the read data. Returns zero in case of success and a negative error
283 * code in case of failure. The read indexing node is validated and if anything
284 * is wrong with it, this function prints complaint messages and returns
287 static int read_znode(struct ubifs_info *c, struct ubifs_zbranch *zzbr,
288 struct ubifs_znode *znode)
290 int lnum = zzbr->lnum;
291 int offs = zzbr->offs;
293 int i, err, type, cmp;
294 struct ubifs_idx_node *idx;
296 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
300 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
306 err = ubifs_node_check_hash(c, idx, zzbr->hash);
308 ubifs_bad_hash(c, idx, zzbr->hash, lnum, offs);
313 znode->child_cnt = le16_to_cpu(idx->child_cnt);
314 znode->level = le16_to_cpu(idx->level);
316 dbg_tnc("LEB %d:%d, level %d, %d branch",
317 lnum, offs, znode->level, znode->child_cnt);
319 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
320 ubifs_err(c, "current fanout %d, branch count %d",
321 c->fanout, znode->child_cnt);
322 ubifs_err(c, "max levels %d, znode level %d",
323 UBIFS_MAX_LEVELS, znode->level);
328 for (i = 0; i < znode->child_cnt; i++) {
329 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
330 struct ubifs_zbranch *zbr = &znode->zbranch[i];
332 key_read(c, &br->key, &zbr->key);
333 zbr->lnum = le32_to_cpu(br->lnum);
334 zbr->offs = le32_to_cpu(br->offs);
335 zbr->len = le32_to_cpu(br->len);
336 ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash);
339 /* Validate branch */
341 if (zbr->lnum < c->main_first ||
342 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
343 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
344 ubifs_err(c, "bad branch %d", i);
349 switch (key_type(c, &zbr->key)) {
356 ubifs_err(c, "bad key type at slot %d: %d",
357 i, key_type(c, &zbr->key));
365 type = key_type(c, &zbr->key);
366 if (c->ranges[type].max_len == 0) {
367 if (zbr->len != c->ranges[type].len) {
368 ubifs_err(c, "bad target node (type %d) length (%d)",
370 ubifs_err(c, "have to be %d", c->ranges[type].len);
374 } else if (zbr->len < c->ranges[type].min_len ||
375 zbr->len > c->ranges[type].max_len) {
376 ubifs_err(c, "bad target node (type %d) length (%d)",
378 ubifs_err(c, "have to be in range of %d-%d",
379 c->ranges[type].min_len,
380 c->ranges[type].max_len);
387 * Ensure that the next key is greater or equivalent to the
390 for (i = 0; i < znode->child_cnt - 1; i++) {
391 const union ubifs_key *key1, *key2;
393 key1 = &znode->zbranch[i].key;
394 key2 = &znode->zbranch[i + 1].key;
396 cmp = keys_cmp(c, key1, key2);
398 ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
401 } else if (cmp == 0 && !is_hash_key(c, key1)) {
402 /* These can only be keys with colliding hash */
403 ubifs_err(c, "keys %d and %d are not hashed but equivalent",
414 ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
415 ubifs_dump_node(c, idx, c->max_idx_node_sz);
421 * ubifs_load_znode - load znode to TNC cache.
422 * @c: UBIFS file-system description object
424 * @parent: znode's parent
425 * @iip: index in parent
427 * This function loads znode pointed to by @zbr into the TNC cache and
428 * returns pointer to it in case of success and a negative error code in case
431 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
432 struct ubifs_zbranch *zbr,
433 struct ubifs_znode *parent, int iip)
436 struct ubifs_znode *znode;
438 ubifs_assert(c, !zbr->znode);
440 * A slab cache is not presently used for znodes because the znode size
441 * depends on the fanout which is stored in the superblock.
443 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
445 return ERR_PTR(-ENOMEM);
447 err = read_znode(c, zbr, znode);
451 atomic_long_inc(&c->clean_zn_cnt);
454 * Increment the global clean znode counter as well. It is OK that
455 * global and per-FS clean znode counters may be inconsistent for some
456 * short time (because we might be preempted at this point), the global
457 * one is only used in shrinker.
459 atomic_long_inc(&ubifs_clean_zn_cnt);
462 znode->parent = parent;
463 znode->time = ktime_get_seconds();
474 * ubifs_tnc_read_node - read a leaf node from the flash media.
475 * @c: UBIFS file-system description object
476 * @zbr: key and position of the node
477 * @node: node is returned here
479 * This function reads a node defined by @zbr from the flash media. Returns
480 * zero in case of success or a negative error code in case of failure.
482 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
485 union ubifs_key key1, *key = &zbr->key;
486 int err, type = key_type(c, key);
487 struct ubifs_wbuf *wbuf;
490 * 'zbr' has to point to on-flash node. The node may sit in a bud and
491 * may even be in a write buffer, so we have to take care about this.
493 wbuf = ubifs_get_wbuf(c, zbr->lnum);
495 err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
496 zbr->lnum, zbr->offs);
498 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
502 dbg_tnck(key, "key ");
506 /* Make sure the key of the read node is correct */
507 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
508 if (!keys_eq(c, key, &key1)) {
509 ubifs_err(c, "bad key in node at LEB %d:%d",
510 zbr->lnum, zbr->offs);
511 dbg_tnck(key, "looked for key ");
512 dbg_tnck(&key1, "but found node's key ");
513 ubifs_dump_node(c, node, zbr->len);
517 err = ubifs_node_check_hash(c, node, zbr->hash);
519 ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs);