bfind.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * linux/fs/hfs/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 "btree.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. hfs_dbg(BNODE_REFS, "find_init: %d (%p)\n",
  23. tree->cnid, __builtin_return_address(0));
  24. mutex_lock(&tree->tree_lock);
  25. return 0;
  26. }
  27. void hfs_find_exit(struct hfs_find_data *fd)
  28. {
  29. hfs_bnode_put(fd->bnode);
  30. kfree(fd->search_key);
  31. hfs_dbg(BNODE_REFS, "find_exit: %d (%p)\n",
  32. fd->tree->cnid, __builtin_return_address(0));
  33. mutex_unlock(&fd->tree->tree_lock);
  34. fd->tree = NULL;
  35. }
  36. /* Find the record in bnode that best matches key (not greater than...)*/
  37. int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
  38. {
  39. int cmpval;
  40. u16 off, len, keylen;
  41. int rec;
  42. int b, e;
  43. int res;
  44. b = 0;
  45. e = bnode->num_recs - 1;
  46. res = -ENOENT;
  47. do {
  48. rec = (e + b) / 2;
  49. len = hfs_brec_lenoff(bnode, rec, &off);
  50. keylen = hfs_brec_keylen(bnode, rec);
  51. if (keylen == 0) {
  52. res = -EINVAL;
  53. goto fail;
  54. }
  55. hfs_bnode_read(bnode, fd->key, off, keylen);
  56. cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
  57. if (!cmpval) {
  58. e = rec;
  59. res = 0;
  60. goto done;
  61. }
  62. if (cmpval < 0)
  63. b = rec + 1;
  64. else
  65. e = rec - 1;
  66. } while (b <= e);
  67. if (rec != e && e >= 0) {
  68. len = hfs_brec_lenoff(bnode, e, &off);
  69. keylen = hfs_brec_keylen(bnode, e);
  70. if (keylen == 0) {
  71. res = -EINVAL;
  72. goto fail;
  73. }
  74. hfs_bnode_read(bnode, fd->key, off, keylen);
  75. }
  76. done:
  77. fd->record = e;
  78. fd->keyoffset = off;
  79. fd->keylength = keylen;
  80. fd->entryoffset = off + keylen;
  81. fd->entrylength = len - keylen;
  82. fail:
  83. return res;
  84. }
  85. /* Traverse a B*Tree from the root to a leaf finding best fit to key */
  86. /* Return allocated copy of node found, set recnum to best record */
  87. int hfs_brec_find(struct hfs_find_data *fd)
  88. {
  89. struct hfs_btree *tree;
  90. struct hfs_bnode *bnode;
  91. u32 nidx, parent;
  92. __be32 data;
  93. int height, res;
  94. tree = fd->tree;
  95. if (fd->bnode)
  96. hfs_bnode_put(fd->bnode);
  97. fd->bnode = NULL;
  98. nidx = tree->root;
  99. if (!nidx)
  100. return -ENOENT;
  101. height = tree->depth;
  102. res = 0;
  103. parent = 0;
  104. for (;;) {
  105. bnode = hfs_bnode_find(tree, nidx);
  106. if (IS_ERR(bnode)) {
  107. res = PTR_ERR(bnode);
  108. bnode = NULL;
  109. break;
  110. }
  111. if (bnode->height != height)
  112. goto invalid;
  113. if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
  114. goto invalid;
  115. bnode->parent = parent;
  116. res = __hfs_brec_find(bnode, fd);
  117. if (!height)
  118. break;
  119. if (fd->record < 0)
  120. goto release;
  121. parent = nidx;
  122. hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
  123. nidx = be32_to_cpu(data);
  124. hfs_bnode_put(bnode);
  125. }
  126. fd->bnode = bnode;
  127. return res;
  128. invalid:
  129. pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
  130. height, bnode->height, bnode->type, nidx, parent);
  131. res = -EIO;
  132. release:
  133. hfs_bnode_put(bnode);
  134. return res;
  135. }
  136. int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
  137. {
  138. int res;
  139. res = hfs_brec_find(fd);
  140. if (res)
  141. return res;
  142. if (fd->entrylength > rec_len)
  143. return -EINVAL;
  144. hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
  145. return 0;
  146. }
  147. int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
  148. {
  149. struct hfs_btree *tree;
  150. struct hfs_bnode *bnode;
  151. int idx, res = 0;
  152. u16 off, len, keylen;
  153. bnode = fd->bnode;
  154. tree = bnode->tree;
  155. if (cnt < 0) {
  156. cnt = -cnt;
  157. while (cnt > fd->record) {
  158. cnt -= fd->record + 1;
  159. fd->record = bnode->num_recs - 1;
  160. idx = bnode->prev;
  161. if (!idx) {
  162. res = -ENOENT;
  163. goto out;
  164. }
  165. hfs_bnode_put(bnode);
  166. bnode = hfs_bnode_find(tree, idx);
  167. if (IS_ERR(bnode)) {
  168. res = PTR_ERR(bnode);
  169. bnode = NULL;
  170. goto out;
  171. }
  172. }
  173. fd->record -= cnt;
  174. } else {
  175. while (cnt >= bnode->num_recs - fd->record) {
  176. cnt -= bnode->num_recs - fd->record;
  177. fd->record = 0;
  178. idx = bnode->next;
  179. if (!idx) {
  180. res = -ENOENT;
  181. goto out;
  182. }
  183. hfs_bnode_put(bnode);
  184. bnode = hfs_bnode_find(tree, idx);
  185. if (IS_ERR(bnode)) {
  186. res = PTR_ERR(bnode);
  187. bnode = NULL;
  188. goto out;
  189. }
  190. }
  191. fd->record += cnt;
  192. }
  193. len = hfs_brec_lenoff(bnode, fd->record, &off);
  194. keylen = hfs_brec_keylen(bnode, fd->record);
  195. if (keylen == 0) {
  196. res = -EINVAL;
  197. goto out;
  198. }
  199. fd->keyoffset = off;
  200. fd->keylength = keylen;
  201. fd->entryoffset = off + keylen;
  202. fd->entrylength = len - keylen;
  203. hfs_bnode_read(bnode, fd->key, off, keylen);
  204. out:
  205. fd->bnode = bnode;
  206. return res;
  207. }