tnc_misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. * read_znode - read an indexing node from flash and fill znode.
  194. * @c: UBIFS file-system description object
  195. * @lnum: LEB of the indexing node to read
  196. * @offs: node offset
  197. * @len: node length
  198. * @znode: znode to read to
  199. *
  200. * This function reads an indexing node from the flash media and fills znode
  201. * with the read data. Returns zero in case of success and a negative error
  202. * code in case of failure. The read indexing node is validated and if anything
  203. * is wrong with it, this function prints complaint messages and returns
  204. * %-EINVAL.
  205. */
  206. static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
  207. struct ubifs_znode *znode)
  208. {
  209. int i, err, type, cmp;
  210. struct ubifs_idx_node *idx;
  211. idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
  212. if (!idx)
  213. return -ENOMEM;
  214. err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
  215. if (err < 0) {
  216. kfree(idx);
  217. return err;
  218. }
  219. znode->child_cnt = le16_to_cpu(idx->child_cnt);
  220. znode->level = le16_to_cpu(idx->level);
  221. dbg_tnc("LEB %d:%d, level %d, %d branch",
  222. lnum, offs, znode->level, znode->child_cnt);
  223. if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
  224. dbg_err("current fanout %d, branch count %d",
  225. c->fanout, znode->child_cnt);
  226. dbg_err("max levels %d, znode level %d",
  227. UBIFS_MAX_LEVELS, znode->level);
  228. err = 1;
  229. goto out_dump;
  230. }
  231. for (i = 0; i < znode->child_cnt; i++) {
  232. const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
  233. struct ubifs_zbranch *zbr = &znode->zbranch[i];
  234. key_read(c, &br->key, &zbr->key);
  235. zbr->lnum = le32_to_cpu(br->lnum);
  236. zbr->offs = le32_to_cpu(br->offs);
  237. zbr->len = le32_to_cpu(br->len);
  238. zbr->znode = NULL;
  239. /* Validate branch */
  240. if (zbr->lnum < c->main_first ||
  241. zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
  242. zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
  243. dbg_err("bad branch %d", i);
  244. err = 2;
  245. goto out_dump;
  246. }
  247. switch (key_type(c, &zbr->key)) {
  248. case UBIFS_INO_KEY:
  249. case UBIFS_DATA_KEY:
  250. case UBIFS_DENT_KEY:
  251. case UBIFS_XENT_KEY:
  252. break;
  253. default:
  254. dbg_msg("bad key type at slot %d: %s", i,
  255. DBGKEY(&zbr->key));
  256. err = 3;
  257. goto out_dump;
  258. }
  259. if (znode->level)
  260. continue;
  261. type = key_type(c, &zbr->key);
  262. if (c->ranges[type].max_len == 0) {
  263. if (zbr->len != c->ranges[type].len) {
  264. dbg_err("bad target node (type %d) length (%d)",
  265. type, zbr->len);
  266. dbg_err("have to be %d", c->ranges[type].len);
  267. err = 4;
  268. goto out_dump;
  269. }
  270. } else if (zbr->len < c->ranges[type].min_len ||
  271. zbr->len > c->ranges[type].max_len) {
  272. dbg_err("bad target node (type %d) length (%d)",
  273. type, zbr->len);
  274. dbg_err("have to be in range of %d-%d",
  275. c->ranges[type].min_len,
  276. c->ranges[type].max_len);
  277. err = 5;
  278. goto out_dump;
  279. }
  280. }
  281. /*
  282. * Ensure that the next key is greater or equivalent to the
  283. * previous one.
  284. */
  285. for (i = 0; i < znode->child_cnt - 1; i++) {
  286. const union ubifs_key *key1, *key2;
  287. key1 = &znode->zbranch[i].key;
  288. key2 = &znode->zbranch[i + 1].key;
  289. cmp = keys_cmp(c, key1, key2);
  290. if (cmp > 0) {
  291. dbg_err("bad key order (keys %d and %d)", i, i + 1);
  292. err = 6;
  293. goto out_dump;
  294. } else if (cmp == 0 && !is_hash_key(c, key1)) {
  295. /* These can only be keys with colliding hash */
  296. dbg_err("keys %d and %d are not hashed but equivalent",
  297. i, i + 1);
  298. err = 7;
  299. goto out_dump;
  300. }
  301. }
  302. kfree(idx);
  303. return 0;
  304. out_dump:
  305. ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
  306. dbg_dump_node(c, idx);
  307. kfree(idx);
  308. return -EINVAL;
  309. }
  310. /**
  311. * ubifs_load_znode - load znode to TNC cache.
  312. * @c: UBIFS file-system description object
  313. * @zbr: znode branch
  314. * @parent: znode's parent
  315. * @iip: index in parent
  316. *
  317. * This function loads znode pointed to by @zbr into the TNC cache and
  318. * returns pointer to it in case of success and a negative error code in case
  319. * of failure.
  320. */
  321. struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
  322. struct ubifs_zbranch *zbr,
  323. struct ubifs_znode *parent, int iip)
  324. {
  325. int err;
  326. struct ubifs_znode *znode;
  327. ubifs_assert(!zbr->znode);
  328. /*
  329. * A slab cache is not presently used for znodes because the znode size
  330. * depends on the fanout which is stored in the superblock.
  331. */
  332. znode = kzalloc(c->max_znode_sz, GFP_NOFS);
  333. if (!znode)
  334. return ERR_PTR(-ENOMEM);
  335. err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
  336. if (err)
  337. goto out;
  338. zbr->znode = znode;
  339. znode->parent = parent;
  340. znode->time = get_seconds();
  341. znode->iip = iip;
  342. return znode;
  343. out:
  344. kfree(znode);
  345. return ERR_PTR(err);
  346. }
  347. /**
  348. * ubifs_tnc_read_node - read a leaf node from the flash media.
  349. * @c: UBIFS file-system description object
  350. * @zbr: key and position of the node
  351. * @node: node is returned here
  352. *
  353. * This function reads a node defined by @zbr from the flash media. Returns
  354. * zero in case of success or a negative negative error code in case of
  355. * failure.
  356. */
  357. int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  358. void *node)
  359. {
  360. union ubifs_key key1, *key = &zbr->key;
  361. int err, type = key_type(c, key);
  362. err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum, zbr->offs);
  363. if (err) {
  364. dbg_tnc("key %s", DBGKEY(key));
  365. return err;
  366. }
  367. /* Make sure the key of the read node is correct */
  368. key_read(c, node + UBIFS_KEY_OFFSET, &key1);
  369. if (!keys_eq(c, key, &key1)) {
  370. ubifs_err("bad key in node at LEB %d:%d",
  371. zbr->lnum, zbr->offs);
  372. dbg_tnc("looked for key %s found node's key %s",
  373. DBGKEY(key), DBGKEY1(&key1));
  374. dbg_dump_node(c, node);
  375. return -EINVAL;
  376. }
  377. return 0;
  378. }