5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Search routines for btrees
11 #include <linux/slab.h>
14 int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
20 ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
24 fd->key = ptr + tree->max_key_len + 2;
25 hfs_dbg(BNODE_REFS, "find_init: %d (%p)\n",
26 tree->cnid, __builtin_return_address(0));
27 mutex_lock(&tree->tree_lock);
31 void hfs_find_exit(struct hfs_find_data *fd)
33 hfs_bnode_put(fd->bnode);
34 kfree(fd->search_key);
35 hfs_dbg(BNODE_REFS, "find_exit: %d (%p)\n",
36 fd->tree->cnid, __builtin_return_address(0));
37 mutex_unlock(&fd->tree->tree_lock);
41 /* Find the record in bnode that best matches key (not greater than...)*/
42 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
51 e = bnode->num_recs - 1;
55 len = hfs_brec_lenoff(bnode, rec, &off);
56 keylen = hfs_brec_keylen(bnode, rec);
61 hfs_bnode_read(bnode, fd->key, off, keylen);
62 cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
73 if (rec != e && e >= 0) {
74 len = hfs_brec_lenoff(bnode, e, &off);
75 keylen = hfs_brec_keylen(bnode, e);
80 hfs_bnode_read(bnode, fd->key, off, keylen);
85 fd->keylength = keylen;
86 fd->entryoffset = off + keylen;
87 fd->entrylength = len - keylen;
92 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
93 /* Return allocated copy of node found, set recnum to best record */
94 int hfs_brec_find(struct hfs_find_data *fd)
96 struct hfs_btree *tree;
97 struct hfs_bnode *bnode;
104 hfs_bnode_put(fd->bnode);
109 height = tree->depth;
113 bnode = hfs_bnode_find(tree, nidx);
115 res = PTR_ERR(bnode);
119 if (bnode->height != height)
121 if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
123 bnode->parent = parent;
125 res = __hfs_brec_find(bnode, fd);
132 hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
133 nidx = be32_to_cpu(data);
134 hfs_bnode_put(bnode);
140 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
141 height, bnode->height, bnode->type, nidx, parent);
144 hfs_bnode_put(bnode);
148 int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
152 res = hfs_brec_find(fd);
155 if (fd->entrylength > rec_len)
157 hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
161 int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
163 struct hfs_btree *tree;
164 struct hfs_bnode *bnode;
166 u16 off, len, keylen;
173 while (cnt > fd->record) {
174 cnt -= fd->record + 1;
175 fd->record = bnode->num_recs - 1;
181 hfs_bnode_put(bnode);
182 bnode = hfs_bnode_find(tree, idx);
184 res = PTR_ERR(bnode);
191 while (cnt >= bnode->num_recs - fd->record) {
192 cnt -= bnode->num_recs - fd->record;
199 hfs_bnode_put(bnode);
200 bnode = hfs_bnode_find(tree, idx);
202 res = PTR_ERR(bnode);
210 len = hfs_brec_lenoff(bnode, fd->record, &off);
211 keylen = hfs_brec_keylen(bnode, fd->record);
217 fd->keylength = keylen;
218 fd->entryoffset = off + keylen;
219 fd->entrylength = len - keylen;
220 hfs_bnode_read(bnode, fd->key, off, keylen);