tnc_misc.c 13 KB

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