bfind.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * linux/fs/hfsplus/bfind.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Search routines for btrees
  9. */
  10. #include <linux/slab.h>
  11. #include "hfsplus_fs.h"
  12. int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
  13. {
  14. void *ptr;
  15. fd->tree = tree;
  16. fd->bnode = NULL;
  17. ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
  18. if (!ptr)
  19. return -ENOMEM;
  20. fd->search_key = ptr;
  21. fd->key = ptr + tree->max_key_len + 2;
  22. dprint(DBG_BNODE_REFS, "find_init: %d (%p)\n",
  23. tree->cnid, __builtin_return_address(0));
  24. switch (tree->cnid) {
  25. case HFSPLUS_CAT_CNID:
  26. mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX);
  27. break;
  28. case HFSPLUS_EXT_CNID:
  29. mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX);
  30. break;
  31. case HFSPLUS_ATTR_CNID:
  32. mutex_lock_nested(&tree->tree_lock, ATTR_BTREE_MUTEX);
  33. break;
  34. default:
  35. BUG();
  36. }
  37. return 0;
  38. }
  39. void hfs_find_exit(struct hfs_find_data *fd)
  40. {
  41. hfs_bnode_put(fd->bnode);
  42. kfree(fd->search_key);
  43. dprint(DBG_BNODE_REFS, "find_exit: %d (%p)\n",
  44. fd->tree->cnid, __builtin_return_address(0));
  45. mutex_unlock(&fd->tree->tree_lock);
  46. fd->tree = NULL;
  47. }
  48. int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode,
  49. struct hfs_find_data *fd,
  50. int *begin,
  51. int *end,
  52. int *cur_rec)
  53. {
  54. __be32 cur_cnid, search_cnid;
  55. if (bnode->tree->cnid == HFSPLUS_EXT_CNID) {
  56. cur_cnid = fd->key->ext.cnid;
  57. search_cnid = fd->search_key->ext.cnid;
  58. } else if (bnode->tree->cnid == HFSPLUS_CAT_CNID) {
  59. cur_cnid = fd->key->cat.parent;
  60. search_cnid = fd->search_key->cat.parent;
  61. } else if (bnode->tree->cnid == HFSPLUS_ATTR_CNID) {
  62. cur_cnid = fd->key->attr.cnid;
  63. search_cnid = fd->search_key->attr.cnid;
  64. } else
  65. BUG();
  66. if (cur_cnid == search_cnid) {
  67. (*end) = (*cur_rec);
  68. if ((*begin) == (*end))
  69. return 1;
  70. } else {
  71. if (be32_to_cpu(cur_cnid) < be32_to_cpu(search_cnid))
  72. (*begin) = (*cur_rec) + 1;
  73. else
  74. (*end) = (*cur_rec) - 1;
  75. }
  76. return 0;
  77. }
  78. int hfs_find_rec_by_key(struct hfs_bnode *bnode,
  79. struct hfs_find_data *fd,
  80. int *begin,
  81. int *end,
  82. int *cur_rec)
  83. {
  84. int cmpval;
  85. cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
  86. if (!cmpval) {
  87. (*end) = (*cur_rec);
  88. return 1;
  89. }
  90. if (cmpval < 0)
  91. (*begin) = (*cur_rec) + 1;
  92. else
  93. *(end) = (*cur_rec) - 1;
  94. return 0;
  95. }
  96. /* Find the record in bnode that best matches key (not greater than...)*/
  97. int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd,
  98. search_strategy_t rec_found)
  99. {
  100. u16 off, len, keylen;
  101. int rec;
  102. int b, e;
  103. int res;
  104. if (!rec_found)
  105. BUG();
  106. b = 0;
  107. e = bnode->num_recs - 1;
  108. res = -ENOENT;
  109. do {
  110. rec = (e + b) / 2;
  111. len = hfs_brec_lenoff(bnode, rec, &off);
  112. keylen = hfs_brec_keylen(bnode, rec);
  113. if (keylen == 0) {
  114. res = -EINVAL;
  115. goto fail;
  116. }
  117. hfs_bnode_read(bnode, fd->key, off, keylen);
  118. if (rec_found(bnode, fd, &b, &e, &rec)) {
  119. res = 0;
  120. goto done;
  121. }
  122. } while (b <= e);
  123. if (rec != e && e >= 0) {
  124. len = hfs_brec_lenoff(bnode, e, &off);
  125. keylen = hfs_brec_keylen(bnode, e);
  126. if (keylen == 0) {
  127. res = -EINVAL;
  128. goto fail;
  129. }
  130. hfs_bnode_read(bnode, fd->key, off, keylen);
  131. }
  132. done:
  133. fd->record = e;
  134. fd->keyoffset = off;
  135. fd->keylength = keylen;
  136. fd->entryoffset = off + keylen;
  137. fd->entrylength = len - keylen;
  138. fail:
  139. return res;
  140. }
  141. /* Traverse a B*Tree from the root to a leaf finding best fit to key */
  142. /* Return allocated copy of node found, set recnum to best record */
  143. int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare)
  144. {
  145. struct hfs_btree *tree;
  146. struct hfs_bnode *bnode;
  147. u32 nidx, parent;
  148. __be32 data;
  149. int height, res;
  150. tree = fd->tree;
  151. if (fd->bnode)
  152. hfs_bnode_put(fd->bnode);
  153. fd->bnode = NULL;
  154. nidx = tree->root;
  155. if (!nidx)
  156. return -ENOENT;
  157. height = tree->depth;
  158. res = 0;
  159. parent = 0;
  160. for (;;) {
  161. bnode = hfs_bnode_find(tree, nidx);
  162. if (IS_ERR(bnode)) {
  163. res = PTR_ERR(bnode);
  164. bnode = NULL;
  165. break;
  166. }
  167. if (bnode->height != height)
  168. goto invalid;
  169. if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
  170. goto invalid;
  171. bnode->parent = parent;
  172. res = __hfs_brec_find(bnode, fd, do_key_compare);
  173. if (!height)
  174. break;
  175. if (fd->record < 0)
  176. goto release;
  177. parent = nidx;
  178. hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
  179. nidx = be32_to_cpu(data);
  180. hfs_bnode_put(bnode);
  181. }
  182. fd->bnode = bnode;
  183. return res;
  184. invalid:
  185. printk(KERN_ERR "hfs: inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
  186. height, bnode->height, bnode->type, nidx, parent);
  187. res = -EIO;
  188. release:
  189. hfs_bnode_put(bnode);
  190. return res;
  191. }
  192. int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
  193. {
  194. int res;
  195. res = hfs_brec_find(fd, hfs_find_rec_by_key);
  196. if (res)
  197. return res;
  198. if (fd->entrylength > rec_len)
  199. return -EINVAL;
  200. hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
  201. return 0;
  202. }
  203. int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
  204. {
  205. struct hfs_btree *tree;
  206. struct hfs_bnode *bnode;
  207. int idx, res = 0;
  208. u16 off, len, keylen;
  209. bnode = fd->bnode;
  210. tree = bnode->tree;
  211. if (cnt < 0) {
  212. cnt = -cnt;
  213. while (cnt > fd->record) {
  214. cnt -= fd->record + 1;
  215. fd->record = bnode->num_recs - 1;
  216. idx = bnode->prev;
  217. if (!idx) {
  218. res = -ENOENT;
  219. goto out;
  220. }
  221. hfs_bnode_put(bnode);
  222. bnode = hfs_bnode_find(tree, idx);
  223. if (IS_ERR(bnode)) {
  224. res = PTR_ERR(bnode);
  225. bnode = NULL;
  226. goto out;
  227. }
  228. }
  229. fd->record -= cnt;
  230. } else {
  231. while (cnt >= bnode->num_recs - fd->record) {
  232. cnt -= bnode->num_recs - fd->record;
  233. fd->record = 0;
  234. idx = bnode->next;
  235. if (!idx) {
  236. res = -ENOENT;
  237. goto out;
  238. }
  239. hfs_bnode_put(bnode);
  240. bnode = hfs_bnode_find(tree, idx);
  241. if (IS_ERR(bnode)) {
  242. res = PTR_ERR(bnode);
  243. bnode = NULL;
  244. goto out;
  245. }
  246. }
  247. fd->record += cnt;
  248. }
  249. len = hfs_brec_lenoff(bnode, fd->record, &off);
  250. keylen = hfs_brec_keylen(bnode, fd->record);
  251. if (keylen == 0) {
  252. res = -EINVAL;
  253. goto out;
  254. }
  255. fd->keyoffset = off;
  256. fd->keylength = keylen;
  257. fd->entryoffset = off + keylen;
  258. fd->entrylength = len - keylen;
  259. hfs_bnode_read(bnode, fd->key, off, keylen);
  260. out:
  261. fd->bnode = bnode;
  262. return res;
  263. }