tnc_misc.c 13 KB

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