tnc_misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file contains miscelanious TNC-related functions shared betweend
  12. * different files. This file does not form any logically separate TNC
  13. * sub-system. The file was created because there is a lot of TNC code and
  14. * putting it all in one file would make that file too big and unreadable.
  15. */
  16. #ifdef __UBOOT__
  17. #include <linux/err.h>
  18. #endif
  19. #include "ubifs.h"
  20. /**
  21. * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
  22. * @zr: root of the subtree to traverse
  23. * @znode: previous znode
  24. *
  25. * This function implements levelorder TNC traversal. The LNC is ignored.
  26. * Returns the next element or %NULL if @znode is already the last one.
  27. */
  28. struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
  29. struct ubifs_znode *znode)
  30. {
  31. int level, iip, level_search = 0;
  32. struct ubifs_znode *zn;
  33. ubifs_assert(zr);
  34. if (unlikely(!znode))
  35. return zr;
  36. if (unlikely(znode == zr)) {
  37. if (znode->level == 0)
  38. return NULL;
  39. return ubifs_tnc_find_child(zr, 0);
  40. }
  41. level = znode->level;
  42. iip = znode->iip;
  43. while (1) {
  44. ubifs_assert(znode->level <= zr->level);
  45. /*
  46. * First walk up until there is a znode with next branch to
  47. * look at.
  48. */
  49. while (znode->parent != zr && iip >= znode->parent->child_cnt) {
  50. znode = znode->parent;
  51. iip = znode->iip;
  52. }
  53. if (unlikely(znode->parent == zr &&
  54. iip >= znode->parent->child_cnt)) {
  55. /* This level is done, switch to the lower one */
  56. level -= 1;
  57. if (level_search || level < 0)
  58. /*
  59. * We were already looking for znode at lower
  60. * level ('level_search'). As we are here
  61. * again, it just does not exist. Or all levels
  62. * were finished ('level < 0').
  63. */
  64. return NULL;
  65. level_search = 1;
  66. iip = -1;
  67. znode = ubifs_tnc_find_child(zr, 0);
  68. ubifs_assert(znode);
  69. }
  70. /* Switch to the next index */
  71. zn = ubifs_tnc_find_child(znode->parent, iip + 1);
  72. if (!zn) {
  73. /* No more children to look at, we have walk up */
  74. iip = znode->parent->child_cnt;
  75. continue;
  76. }
  77. /* Walk back down to the level we came from ('level') */
  78. while (zn->level != level) {
  79. znode = zn;
  80. zn = ubifs_tnc_find_child(zn, 0);
  81. if (!zn) {
  82. /*
  83. * This path is not too deep so it does not
  84. * reach 'level'. Try next path.
  85. */
  86. iip = znode->iip;
  87. break;
  88. }
  89. }
  90. if (zn) {
  91. ubifs_assert(zn->level >= 0);
  92. return zn;
  93. }
  94. }
  95. }
  96. /**
  97. * ubifs_search_zbranch - search znode branch.
  98. * @c: UBIFS file-system description object
  99. * @znode: znode to search in
  100. * @key: key to search for
  101. * @n: znode branch slot number is returned here
  102. *
  103. * This is a helper function which search branch with key @key in @znode using
  104. * binary search. The result of the search may be:
  105. * o exact match, then %1 is returned, and the slot number of the branch is
  106. * stored in @n;
  107. * o no exact match, then %0 is returned and the slot number of the left
  108. * closest branch is returned in @n; the slot if all keys in this znode are
  109. * greater than @key, then %-1 is returned in @n.
  110. */
  111. int ubifs_search_zbranch(const struct ubifs_info *c,
  112. const struct ubifs_znode *znode,
  113. const union ubifs_key *key, int *n)
  114. {
  115. int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
  116. int uninitialized_var(cmp);
  117. const struct ubifs_zbranch *zbr = &znode->zbranch[0];
  118. ubifs_assert(end > beg);
  119. while (end > beg) {
  120. mid = (beg + end) >> 1;
  121. cmp = keys_cmp(c, key, &zbr[mid].key);
  122. if (cmp > 0)
  123. beg = mid + 1;
  124. else if (cmp < 0)
  125. end = mid;
  126. else {
  127. *n = mid;
  128. return 1;
  129. }
  130. }
  131. *n = end - 1;
  132. /* The insert point is after *n */
  133. ubifs_assert(*n >= -1 && *n < znode->child_cnt);
  134. if (*n == -1)
  135. ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
  136. else
  137. ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
  138. if (*n + 1 < znode->child_cnt)
  139. ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
  140. return 0;
  141. }
  142. /**
  143. * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
  144. * @znode: znode to start at (root of the sub-tree to traverse)
  145. *
  146. * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
  147. * ignored.
  148. */
  149. struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
  150. {
  151. if (unlikely(!znode))
  152. return NULL;
  153. while (znode->level > 0) {
  154. struct ubifs_znode *child;
  155. child = ubifs_tnc_find_child(znode, 0);
  156. if (!child)
  157. return znode;
  158. znode = child;
  159. }
  160. return znode;
  161. }
  162. /**
  163. * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
  164. * @znode: previous znode
  165. *
  166. * This function implements postorder TNC traversal. The LNC is ignored.
  167. * Returns the next element or %NULL if @znode is already the last one.
  168. */
  169. struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
  170. {
  171. struct ubifs_znode *zn;
  172. ubifs_assert(znode);
  173. if (unlikely(!znode->parent))
  174. return NULL;
  175. /* Switch to the next index in the parent */
  176. zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
  177. if (!zn)
  178. /* This is in fact the last child, return parent */
  179. return znode->parent;
  180. /* Go to the first znode in this new subtree */
  181. return ubifs_tnc_postorder_first(zn);
  182. }
  183. /**
  184. * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
  185. * @znode: znode defining subtree to destroy
  186. *
  187. * This function destroys subtree of the TNC tree. Returns number of clean
  188. * znodes in the subtree.
  189. */
  190. long ubifs_destroy_tnc_subtree(struct ubifs_znode *znode)
  191. {
  192. struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
  193. long clean_freed = 0;
  194. int n;
  195. ubifs_assert(zn);
  196. while (1) {
  197. for (n = 0; n < zn->child_cnt; n++) {
  198. if (!zn->zbranch[n].znode)
  199. continue;
  200. if (zn->level > 0 &&
  201. !ubifs_zn_dirty(zn->zbranch[n].znode))
  202. clean_freed += 1;
  203. cond_resched();
  204. kfree(zn->zbranch[n].znode);
  205. }
  206. if (zn == znode) {
  207. if (!ubifs_zn_dirty(zn))
  208. clean_freed += 1;
  209. kfree(zn);
  210. return clean_freed;
  211. }
  212. zn = ubifs_tnc_postorder_next(zn);
  213. }
  214. }
  215. /**
  216. * read_znode - read an indexing node from flash and fill znode.
  217. * @c: UBIFS file-system description object
  218. * @lnum: LEB of the indexing node to read
  219. * @offs: node offset
  220. * @len: node length
  221. * @znode: znode to read to
  222. *
  223. * This function reads an indexing node from the flash media and fills znode
  224. * with the read data. Returns zero in case of success and a negative error
  225. * code in case of failure. The read indexing node is validated and if anything
  226. * is wrong with it, this function prints complaint messages and returns
  227. * %-EINVAL.
  228. */
  229. static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
  230. struct ubifs_znode *znode)
  231. {
  232. int i, err, type, cmp;
  233. struct ubifs_idx_node *idx;
  234. idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
  235. if (!idx)
  236. return -ENOMEM;
  237. err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
  238. if (err < 0) {
  239. kfree(idx);
  240. return err;
  241. }
  242. znode->child_cnt = le16_to_cpu(idx->child_cnt);
  243. znode->level = le16_to_cpu(idx->level);
  244. dbg_tnc("LEB %d:%d, level %d, %d branch",
  245. lnum, offs, znode->level, znode->child_cnt);
  246. if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
  247. ubifs_err(c, "current fanout %d, branch count %d",
  248. c->fanout, znode->child_cnt);
  249. ubifs_err(c, "max levels %d, znode level %d",
  250. UBIFS_MAX_LEVELS, znode->level);
  251. err = 1;
  252. goto out_dump;
  253. }
  254. for (i = 0; i < znode->child_cnt; i++) {
  255. const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  256. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  257. key_read(c, &br->key, &zbr->key);
  258. zbr->lnum = le32_to_cpu(br->lnum);
  259. zbr->offs = le32_to_cpu(br->offs);
  260. zbr->len = le32_to_cpu(br->len);
  261. zbr->znode = NULL;
  262. /* Validate branch */
  263. if (zbr->lnum < c->main_first ||
  264. zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
  265. zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
  266. ubifs_err(c, "bad branch %d", i);
  267. err = 2;
  268. goto out_dump;
  269. }
  270. switch (key_type(c, &zbr->key)) {
  271. case UBIFS_INO_KEY:
  272. case UBIFS_DATA_KEY:
  273. case UBIFS_DENT_KEY:
  274. case UBIFS_XENT_KEY:
  275. break;
  276. default:
  277. ubifs_err(c, "bad key type at slot %d: %d",
  278. i, key_type(c, &zbr->key));
  279. err = 3;
  280. goto out_dump;
  281. }
  282. if (znode->level)
  283. continue;
  284. type = key_type(c, &zbr->key);
  285. if (c->ranges[type].max_len == 0) {
  286. if (zbr->len != c->ranges[type].len) {
  287. ubifs_err(c, "bad target node (type %d) length (%d)",
  288. type, zbr->len);
  289. ubifs_err(c, "have to be %d", c->ranges[type].len);
  290. err = 4;
  291. goto out_dump;
  292. }
  293. } else if (zbr->len < c->ranges[type].min_len ||
  294. zbr->len > c->ranges[type].max_len) {
  295. ubifs_err(c, "bad target node (type %d) length (%d)",
  296. type, zbr->len);
  297. ubifs_err(c, "have to be in range of %d-%d",
  298. c->ranges[type].min_len,
  299. c->ranges[type].max_len);
  300. err = 5;
  301. goto out_dump;
  302. }
  303. }
  304. /*
  305. * Ensure that the next key is greater or equivalent to the
  306. * previous one.
  307. */
  308. for (i = 0; i < znode->child_cnt - 1; i++) {
  309. const union ubifs_key *key1, *key2;
  310. key1 = &znode->zbranch[i].key;
  311. key2 = &znode->zbranch[i + 1].key;
  312. cmp = keys_cmp(c, key1, key2);
  313. if (cmp > 0) {
  314. ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
  315. err = 6;
  316. goto out_dump;
  317. } else if (cmp == 0 && !is_hash_key(c, key1)) {
  318. /* These can only be keys with colliding hash */
  319. ubifs_err(c, "keys %d and %d are not hashed but equivalent",
  320. i, i + 1);
  321. err = 7;
  322. goto out_dump;
  323. }
  324. }
  325. kfree(idx);
  326. return 0;
  327. out_dump:
  328. ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
  329. ubifs_dump_node(c, idx);
  330. kfree(idx);
  331. return -EINVAL;
  332. }
  333. /**
  334. * ubifs_load_znode - load znode to TNC cache.
  335. * @c: UBIFS file-system description object
  336. * @zbr: znode branch
  337. * @parent: znode's parent
  338. * @iip: index in parent
  339. *
  340. * This function loads znode pointed to by @zbr into the TNC cache and
  341. * returns pointer to it in case of success and a negative error code in case
  342. * of failure.
  343. */
  344. struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
  345. struct ubifs_zbranch *zbr,
  346. struct ubifs_znode *parent, int iip)
  347. {
  348. int err;
  349. struct ubifs_znode *znode;
  350. ubifs_assert(!zbr->znode);
  351. /*
  352. * A slab cache is not presently used for znodes because the znode size
  353. * depends on the fanout which is stored in the superblock.
  354. */
  355. znode = kzalloc(c->max_znode_sz, GFP_NOFS);
  356. if (!znode)
  357. return ERR_PTR(-ENOMEM);
  358. err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
  359. if (err)
  360. goto out;
  361. atomic_long_inc(&c->clean_zn_cnt);
  362. /*
  363. * Increment the global clean znode counter as well. It is OK that
  364. * global and per-FS clean znode counters may be inconsistent for some
  365. * short time (because we might be preempted at this point), the global
  366. * one is only used in shrinker.
  367. */
  368. atomic_long_inc(&ubifs_clean_zn_cnt);
  369. zbr->znode = znode;
  370. znode->parent = parent;
  371. znode->time = get_seconds();
  372. znode->iip = iip;
  373. return znode;
  374. out:
  375. kfree(znode);
  376. return ERR_PTR(err);
  377. }
  378. /**
  379. * ubifs_tnc_read_node - read a leaf node from the flash media.
  380. * @c: UBIFS file-system description object
  381. * @zbr: key and position of the node
  382. * @node: node is returned here
  383. *
  384. * This function reads a node defined by @zbr from the flash media. Returns
  385. * zero in case of success or a negative negative error code in case of
  386. * failure.
  387. */
  388. int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  389. void *node)
  390. {
  391. union ubifs_key key1, *key = &zbr->key;
  392. int err, type = key_type(c, key);
  393. struct ubifs_wbuf *wbuf;
  394. /*
  395. * 'zbr' has to point to on-flash node. The node may sit in a bud and
  396. * may even be in a write buffer, so we have to take care about this.
  397. */
  398. wbuf = ubifs_get_wbuf(c, zbr->lnum);
  399. if (wbuf)
  400. err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
  401. zbr->lnum, zbr->offs);
  402. else
  403. err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
  404. zbr->offs);
  405. if (err) {
  406. dbg_tnck(key, "key ");
  407. return err;
  408. }
  409. /* Make sure the key of the read node is correct */
  410. key_read(c, node + UBIFS_KEY_OFFSET, &key1);
  411. if (!keys_eq(c, key, &key1)) {
  412. ubifs_err(c, "bad key in node at LEB %d:%d",
  413. zbr->lnum, zbr->offs);
  414. dbg_tnck(key, "looked for key ");
  415. dbg_tnck(&key1, "but found node's key ");
  416. ubifs_dump_node(c, node);
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }