tnc_misc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. err = 1;
  280. goto out_dump;
  281. }
  282. for (i = 0; i < znode->child_cnt; i++) {
  283. struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  284. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  285. key_read(c, &br->key, &zbr->key);
  286. zbr->lnum = le32_to_cpu(br->lnum);
  287. zbr->offs = le32_to_cpu(br->offs);
  288. zbr->len = le32_to_cpu(br->len);
  289. ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash);
  290. zbr->znode = NULL;
  291. /* Validate branch */
  292. if (zbr->lnum < c->main_first ||
  293. zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
  294. zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
  295. ubifs_err(c, "bad branch %d", i);
  296. err = 2;
  297. goto out_dump;
  298. }
  299. switch (key_type(c, &zbr->key)) {
  300. case UBIFS_INO_KEY:
  301. case UBIFS_DATA_KEY:
  302. case UBIFS_DENT_KEY:
  303. case UBIFS_XENT_KEY:
  304. break;
  305. default:
  306. ubifs_err(c, "bad key type at slot %d: %d",
  307. i, key_type(c, &zbr->key));
  308. err = 3;
  309. goto out_dump;
  310. }
  311. if (znode->level)
  312. continue;
  313. type = key_type(c, &zbr->key);
  314. if (c->ranges[type].max_len == 0) {
  315. if (zbr->len != c->ranges[type].len) {
  316. ubifs_err(c, "bad target node (type %d) length (%d)",
  317. type, zbr->len);
  318. ubifs_err(c, "have to be %d", c->ranges[type].len);
  319. err = 4;
  320. goto out_dump;
  321. }
  322. } else if (zbr->len < c->ranges[type].min_len ||
  323. zbr->len > c->ranges[type].max_len) {
  324. ubifs_err(c, "bad target node (type %d) length (%d)",
  325. type, zbr->len);
  326. ubifs_err(c, "have to be in range of %d-%d",
  327. c->ranges[type].min_len,
  328. c->ranges[type].max_len);
  329. err = 5;
  330. goto out_dump;
  331. }
  332. }
  333. /*
  334. * Ensure that the next key is greater or equivalent to the
  335. * previous one.
  336. */
  337. for (i = 0; i < znode->child_cnt - 1; i++) {
  338. const union ubifs_key *key1, *key2;
  339. key1 = &znode->zbranch[i].key;
  340. key2 = &znode->zbranch[i + 1].key;
  341. cmp = keys_cmp(c, key1, key2);
  342. if (cmp > 0) {
  343. ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
  344. err = 6;
  345. goto out_dump;
  346. } else if (cmp == 0 && !is_hash_key(c, key1)) {
  347. /* These can only be keys with colliding hash */
  348. ubifs_err(c, "keys %d and %d are not hashed but equivalent",
  349. i, i + 1);
  350. err = 7;
  351. goto out_dump;
  352. }
  353. }
  354. kfree(idx);
  355. return 0;
  356. out_dump:
  357. ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
  358. ubifs_dump_node(c, idx, c->max_idx_node_sz);
  359. kfree(idx);
  360. return -EINVAL;
  361. }
  362. /**
  363. * ubifs_load_znode - load znode to TNC cache.
  364. * @c: UBIFS file-system description object
  365. * @zbr: znode branch
  366. * @parent: znode's parent
  367. * @iip: index in parent
  368. *
  369. * This function loads znode pointed to by @zbr into the TNC cache and
  370. * returns pointer to it in case of success and a negative error code in case
  371. * of failure.
  372. */
  373. struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
  374. struct ubifs_zbranch *zbr,
  375. struct ubifs_znode *parent, int iip)
  376. {
  377. int err;
  378. struct ubifs_znode *znode;
  379. ubifs_assert(c, !zbr->znode);
  380. /*
  381. * A slab cache is not presently used for znodes because the znode size
  382. * depends on the fanout which is stored in the superblock.
  383. */
  384. znode = kzalloc(c->max_znode_sz, GFP_NOFS);
  385. if (!znode)
  386. return ERR_PTR(-ENOMEM);
  387. err = read_znode(c, zbr, znode);
  388. if (err)
  389. goto out;
  390. atomic_long_inc(&c->clean_zn_cnt);
  391. /*
  392. * Increment the global clean znode counter as well. It is OK that
  393. * global and per-FS clean znode counters may be inconsistent for some
  394. * short time (because we might be preempted at this point), the global
  395. * one is only used in shrinker.
  396. */
  397. atomic_long_inc(&ubifs_clean_zn_cnt);
  398. zbr->znode = znode;
  399. znode->parent = parent;
  400. znode->time = ktime_get_seconds();
  401. znode->iip = iip;
  402. return znode;
  403. out:
  404. kfree(znode);
  405. return ERR_PTR(err);
  406. }
  407. /**
  408. * ubifs_tnc_read_node - read a leaf node from the flash media.
  409. * @c: UBIFS file-system description object
  410. * @zbr: key and position of the node
  411. * @node: node is returned here
  412. *
  413. * This function reads a node defined by @zbr from the flash media. Returns
  414. * zero in case of success or a negative error code in case of failure.
  415. */
  416. int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  417. void *node)
  418. {
  419. union ubifs_key key1, *key = &zbr->key;
  420. int err, type = key_type(c, key);
  421. struct ubifs_wbuf *wbuf;
  422. /*
  423. * 'zbr' has to point to on-flash node. The node may sit in a bud and
  424. * may even be in a write buffer, so we have to take care about this.
  425. */
  426. wbuf = ubifs_get_wbuf(c, zbr->lnum);
  427. if (wbuf)
  428. err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
  429. zbr->lnum, zbr->offs);
  430. else
  431. err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
  432. zbr->offs);
  433. if (err) {
  434. dbg_tnck(key, "key ");
  435. return err;
  436. }
  437. /* Make sure the key of the read node is correct */
  438. key_read(c, node + UBIFS_KEY_OFFSET, &key1);
  439. if (!keys_eq(c, key, &key1)) {
  440. ubifs_err(c, "bad key in node at LEB %d:%d",
  441. zbr->lnum, zbr->offs);
  442. dbg_tnck(key, "looked for key ");
  443. dbg_tnck(&key1, "but found node's key ");
  444. ubifs_dump_node(c, node, zbr->len);
  445. return -EINVAL;
  446. }
  447. err = ubifs_node_check_hash(c, node, zbr->hash);
  448. if (err) {
  449. ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs);
  450. return err;
  451. }
  452. return 0;
  453. }