bfind.c 4.6 KB

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