tnc_misc.c 13 KB

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