blob: 6f293f662d983afed23d90649134c6b915c4d69a [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Artem Bityutskiy1e517642008-07-14 19:08:37 +03007 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
9 */
10
11/*
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.
16 */
17
18#include "ubifs.h"
19
20/**
21 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
Richard Weinberger6eb61d52018-07-12 13:01:57 +020022 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +030023 * @zr: root of the subtree to traverse
24 * @znode: previous znode
25 *
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.
28 */
Richard Weinberger6eb61d52018-07-12 13:01:57 +020029struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
30 struct ubifs_znode *zr,
Artem Bityutskiy1e517642008-07-14 19:08:37 +030031 struct ubifs_znode *znode)
32{
33 int level, iip, level_search = 0;
34 struct ubifs_znode *zn;
35
Richard Weinberger6eb61d52018-07-12 13:01:57 +020036 ubifs_assert(c, zr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030037
38 if (unlikely(!znode))
39 return zr;
40
41 if (unlikely(znode == zr)) {
42 if (znode->level == 0)
43 return NULL;
44 return ubifs_tnc_find_child(zr, 0);
45 }
46
47 level = znode->level;
48
49 iip = znode->iip;
50 while (1) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +020051 ubifs_assert(c, znode->level <= zr->level);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030052
53 /*
54 * First walk up until there is a znode with next branch to
55 * look at.
56 */
57 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
58 znode = znode->parent;
59 iip = znode->iip;
60 }
61
62 if (unlikely(znode->parent == zr &&
63 iip >= znode->parent->child_cnt)) {
64 /* This level is done, switch to the lower one */
65 level -= 1;
66 if (level_search || level < 0)
67 /*
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').
72 */
73 return NULL;
74
75 level_search = 1;
76 iip = -1;
77 znode = ubifs_tnc_find_child(zr, 0);
Richard Weinberger6eb61d52018-07-12 13:01:57 +020078 ubifs_assert(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030079 }
80
81 /* Switch to the next index */
82 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
83 if (!zn) {
84 /* No more children to look at, we have walk up */
85 iip = znode->parent->child_cnt;
86 continue;
87 }
88
89 /* Walk back down to the level we came from ('level') */
90 while (zn->level != level) {
91 znode = zn;
92 zn = ubifs_tnc_find_child(zn, 0);
93 if (!zn) {
94 /*
95 * This path is not too deep so it does not
96 * reach 'level'. Try next path.
97 */
98 iip = znode->iip;
99 break;
100 }
101 }
102
103 if (zn) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200104 ubifs_assert(c, zn->level >= 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300105 return zn;
106 }
107 }
108}
109
110/**
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
116 *
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
120 * stored in @n;
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.
124 */
125int ubifs_search_zbranch(const struct ubifs_info *c,
126 const struct ubifs_znode *znode,
127 const union ubifs_key *key, int *n)
128{
129 int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
130 int uninitialized_var(cmp);
131 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
132
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200133 ubifs_assert(c, end > beg);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300134
135 while (end > beg) {
136 mid = (beg + end) >> 1;
137 cmp = keys_cmp(c, key, &zbr[mid].key);
138 if (cmp > 0)
139 beg = mid + 1;
140 else if (cmp < 0)
141 end = mid;
142 else {
143 *n = mid;
144 return 1;
145 }
146 }
147
148 *n = end - 1;
149
150 /* The insert point is after *n */
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200151 ubifs_assert(c, *n >= -1 && *n < znode->child_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300152 if (*n == -1)
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200153 ubifs_assert(c, keys_cmp(c, key, &zbr[0].key) < 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300154 else
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200155 ubifs_assert(c, keys_cmp(c, key, &zbr[*n].key) > 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300156 if (*n + 1 < znode->child_cnt)
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200157 ubifs_assert(c, keys_cmp(c, key, &zbr[*n + 1].key) < 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300158
159 return 0;
160}
161
162/**
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)
165 *
166 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
167 * ignored.
168 */
169struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
170{
171 if (unlikely(!znode))
172 return NULL;
173
174 while (znode->level > 0) {
175 struct ubifs_znode *child;
176
177 child = ubifs_tnc_find_child(znode, 0);
178 if (!child)
179 return znode;
180 znode = child;
181 }
182
183 return znode;
184}
185
186/**
187 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200188 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300189 * @znode: previous znode
190 *
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.
193 */
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200194struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
195 struct ubifs_znode *znode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300196{
197 struct ubifs_znode *zn;
198
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200199 ubifs_assert(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300200 if (unlikely(!znode->parent))
201 return NULL;
202
203 /* Switch to the next index in the parent */
204 zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
205 if (!zn)
206 /* This is in fact the last child, return parent */
207 return znode->parent;
208
209 /* Go to the first znode in this new subtree */
210 return ubifs_tnc_postorder_first(zn);
211}
212
213/**
214 * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200215 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300216 * @znode: znode defining subtree to destroy
217 *
218 * This function destroys subtree of the TNC tree. Returns number of clean
219 * znodes in the subtree.
220 */
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200221long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
222 struct ubifs_znode *znode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300223{
224 struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
225 long clean_freed = 0;
226 int n;
227
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200228 ubifs_assert(c, zn);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300229 while (1) {
230 for (n = 0; n < zn->child_cnt; n++) {
231 if (!zn->zbranch[n].znode)
232 continue;
233
234 if (zn->level > 0 &&
235 !ubifs_zn_dirty(zn->zbranch[n].znode))
236 clean_freed += 1;
237
238 cond_resched();
239 kfree(zn->zbranch[n].znode);
240 }
241
242 if (zn == znode) {
243 if (!ubifs_zn_dirty(zn))
244 clean_freed += 1;
245 kfree(zn);
246 return clean_freed;
247 }
248
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200249 zn = ubifs_tnc_postorder_next(c, zn);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300250 }
251}
252
253/**
254 * read_znode - read an indexing node from flash and fill znode.
255 * @c: UBIFS file-system description object
Sascha Hauer22ceaa82018-09-07 14:36:25 +0200256 * @zzbr: the zbranch describing the node to read
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300257 * @znode: znode to read to
258 *
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
263 * %-EINVAL.
264 */
Sascha Hauer22ceaa82018-09-07 14:36:25 +0200265static int read_znode(struct ubifs_info *c, struct ubifs_zbranch *zzbr,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300266 struct ubifs_znode *znode)
267{
Sascha Hauer22ceaa82018-09-07 14:36:25 +0200268 int lnum = zzbr->lnum;
269 int offs = zzbr->offs;
270 int len = zzbr->len;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300271 int i, err, type, cmp;
272 struct ubifs_idx_node *idx;
273
274 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
275 if (!idx)
276 return -ENOMEM;
277
278 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
279 if (err < 0) {
280 kfree(idx);
281 return err;
282 }
283
Sascha Hauer16a26b22018-09-07 14:36:35 +0200284 err = ubifs_node_check_hash(c, idx, zzbr->hash);
285 if (err) {
286 ubifs_bad_hash(c, idx, zzbr->hash, lnum, offs);
287 return err;
288 }
289
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300290 znode->child_cnt = le16_to_cpu(idx->child_cnt);
291 znode->level = le16_to_cpu(idx->level);
292
293 dbg_tnc("LEB %d:%d, level %d, %d branch",
294 lnum, offs, znode->level, znode->child_cnt);
295
296 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
Sheng Yong235c3622015-03-20 10:39:42 +0000297 ubifs_err(c, "current fanout %d, branch count %d",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300298 c->fanout, znode->child_cnt);
Sheng Yong235c3622015-03-20 10:39:42 +0000299 ubifs_err(c, "max levels %d, znode level %d",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300300 UBIFS_MAX_LEVELS, znode->level);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300301 err = 1;
302 goto out_dump;
303 }
304
305 for (i = 0; i < znode->child_cnt; i++) {
Sascha Hauer16a26b22018-09-07 14:36:35 +0200306 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307 struct ubifs_zbranch *zbr = &znode->zbranch[i];
308
309 key_read(c, &br->key, &zbr->key);
310 zbr->lnum = le32_to_cpu(br->lnum);
311 zbr->offs = le32_to_cpu(br->offs);
312 zbr->len = le32_to_cpu(br->len);
Sascha Hauer16a26b22018-09-07 14:36:35 +0200313 ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300314 zbr->znode = NULL;
315
316 /* Validate branch */
317
318 if (zbr->lnum < c->main_first ||
319 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
320 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
Sheng Yong235c3622015-03-20 10:39:42 +0000321 ubifs_err(c, "bad branch %d", i);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300322 err = 2;
323 goto out_dump;
324 }
325
326 switch (key_type(c, &zbr->key)) {
327 case UBIFS_INO_KEY:
328 case UBIFS_DATA_KEY:
329 case UBIFS_DENT_KEY:
330 case UBIFS_XENT_KEY:
331 break;
332 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000333 ubifs_err(c, "bad key type at slot %d: %d",
Artem Bityutskiy3668b702012-08-27 16:56:58 +0300334 i, key_type(c, &zbr->key));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300335 err = 3;
336 goto out_dump;
337 }
338
339 if (znode->level)
340 continue;
341
342 type = key_type(c, &zbr->key);
343 if (c->ranges[type].max_len == 0) {
344 if (zbr->len != c->ranges[type].len) {
Sheng Yong235c3622015-03-20 10:39:42 +0000345 ubifs_err(c, "bad target node (type %d) length (%d)",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300346 type, zbr->len);
Sheng Yong235c3622015-03-20 10:39:42 +0000347 ubifs_err(c, "have to be %d", c->ranges[type].len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300348 err = 4;
349 goto out_dump;
350 }
351 } else if (zbr->len < c->ranges[type].min_len ||
352 zbr->len > c->ranges[type].max_len) {
Sheng Yong235c3622015-03-20 10:39:42 +0000353 ubifs_err(c, "bad target node (type %d) length (%d)",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300354 type, zbr->len);
Sheng Yong235c3622015-03-20 10:39:42 +0000355 ubifs_err(c, "have to be in range of %d-%d",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300356 c->ranges[type].min_len,
357 c->ranges[type].max_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300358 err = 5;
359 goto out_dump;
360 }
361 }
362
363 /*
364 * Ensure that the next key is greater or equivalent to the
365 * previous one.
366 */
367 for (i = 0; i < znode->child_cnt - 1; i++) {
368 const union ubifs_key *key1, *key2;
369
370 key1 = &znode->zbranch[i].key;
371 key2 = &znode->zbranch[i + 1].key;
372
373 cmp = keys_cmp(c, key1, key2);
374 if (cmp > 0) {
Sheng Yong235c3622015-03-20 10:39:42 +0000375 ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300376 err = 6;
377 goto out_dump;
378 } else if (cmp == 0 && !is_hash_key(c, key1)) {
379 /* These can only be keys with colliding hash */
Sheng Yong235c3622015-03-20 10:39:42 +0000380 ubifs_err(c, "keys %d and %d are not hashed but equivalent",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300381 i, i + 1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382 err = 7;
383 goto out_dump;
384 }
385 }
386
387 kfree(idx);
388 return 0;
389
390out_dump:
Sheng Yong235c3622015-03-20 10:39:42 +0000391 ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300392 ubifs_dump_node(c, idx);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300393 kfree(idx);
394 return -EINVAL;
395}
396
397/**
398 * ubifs_load_znode - load znode to TNC cache.
399 * @c: UBIFS file-system description object
400 * @zbr: znode branch
401 * @parent: znode's parent
402 * @iip: index in parent
403 *
404 * This function loads znode pointed to by @zbr into the TNC cache and
405 * returns pointer to it in case of success and a negative error code in case
406 * of failure.
407 */
408struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
409 struct ubifs_zbranch *zbr,
410 struct ubifs_znode *parent, int iip)
411{
412 int err;
413 struct ubifs_znode *znode;
414
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200415 ubifs_assert(c, !zbr->znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300416 /*
417 * A slab cache is not presently used for znodes because the znode size
418 * depends on the fanout which is stored in the superblock.
419 */
420 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
421 if (!znode)
422 return ERR_PTR(-ENOMEM);
423
Sascha Hauer22ceaa82018-09-07 14:36:25 +0200424 err = read_znode(c, zbr, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300425 if (err)
426 goto out;
427
428 atomic_long_inc(&c->clean_zn_cnt);
429
430 /*
431 * Increment the global clean znode counter as well. It is OK that
432 * global and per-FS clean znode counters may be inconsistent for some
433 * short time (because we might be preempted at this point), the global
434 * one is only used in shrinker.
435 */
436 atomic_long_inc(&ubifs_clean_zn_cnt);
437
438 zbr->znode = znode;
439 znode->parent = parent;
Arnd Bergmann6cff5732018-07-13 16:31:56 +0200440 znode->time = ktime_get_seconds();
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300441 znode->iip = iip;
442
443 return znode;
444
445out:
446 kfree(znode);
447 return ERR_PTR(err);
448}
449
450/**
451 * ubifs_tnc_read_node - read a leaf node from the flash media.
452 * @c: UBIFS file-system description object
453 * @zbr: key and position of the node
454 * @node: node is returned here
455 *
456 * This function reads a node defined by @zbr from the flash media. Returns
457 * zero in case of success or a negative negative error code in case of
458 * failure.
459 */
460int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
461 void *node)
462{
463 union ubifs_key key1, *key = &zbr->key;
464 int err, type = key_type(c, key);
465 struct ubifs_wbuf *wbuf;
466
467 /*
468 * 'zbr' has to point to on-flash node. The node may sit in a bud and
469 * may even be in a write buffer, so we have to take care about this.
470 */
471 wbuf = ubifs_get_wbuf(c, zbr->lnum);
472 if (wbuf)
473 err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
474 zbr->lnum, zbr->offs);
475 else
476 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
477 zbr->offs);
478
479 if (err) {
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200480 dbg_tnck(key, "key ");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300481 return err;
482 }
483
484 /* Make sure the key of the read node is correct */
Adrian Hunter2094c332008-09-05 15:20:04 +0300485 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
486 if (!keys_eq(c, key, &key1)) {
Sheng Yong235c3622015-03-20 10:39:42 +0000487 ubifs_err(c, "bad key in node at LEB %d:%d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300488 zbr->lnum, zbr->offs);
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200489 dbg_tnck(key, "looked for key ");
490 dbg_tnck(&key1, "but found node's key ");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300491 ubifs_dump_node(c, node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300492 return -EINVAL;
493 }
494
Sascha Hauer16a26b22018-09-07 14:36:35 +0200495 err = ubifs_node_check_hash(c, node, zbr->hash);
496 if (err) {
497 ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs);
498 return err;
499 }
500
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300501 return 0;
502}