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));
29 mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX);
32 mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX);
35 mutex_lock_nested(&tree->tree_lock, ATTR_BTREE_MUTEX);
43 void hfs_find_exit(struct hfs_find_data *fd)
45 hfs_bnode_put(fd->bnode);
46 kfree(fd->search_key);
47 hfs_dbg(BNODE_REFS, "find_exit: %d (%p)\n",
48 fd->tree->cnid, __builtin_return_address(0));
49 mutex_unlock(&fd->tree->tree_lock);
53 /* Find the record in bnode that best matches key (not greater than...)*/
54 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
63 e = bnode->num_recs - 1;
67 len = hfs_brec_lenoff(bnode, rec, &off);
68 keylen = hfs_brec_keylen(bnode, rec);
73 hfs_bnode_read(bnode, fd->key, off, keylen);
74 cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
85 if (rec != e && e >= 0) {
86 len = hfs_brec_lenoff(bnode, e, &off);
87 keylen = hfs_brec_keylen(bnode, e);
92 hfs_bnode_read(bnode, fd->key, off, keylen);
97 fd->keylength = keylen;
98 fd->entryoffset = off + keylen;
99 fd->entrylength = len - keylen;
104 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
105 /* Return allocated copy of node found, set recnum to best record */
106 int hfs_brec_find(struct hfs_find_data *fd)
108 struct hfs_btree *tree;
109 struct hfs_bnode *bnode;
116 hfs_bnode_put(fd->bnode);
121 height = tree->depth;
125 bnode = hfs_bnode_find(tree, nidx);
127 res = PTR_ERR(bnode);
131 if (bnode->height != height)
133 if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
135 bnode->parent = parent;
137 res = __hfs_brec_find(bnode, fd);
144 hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
145 nidx = be32_to_cpu(data);
146 hfs_bnode_put(bnode);
152 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
153 height, bnode->height, bnode->type, nidx, parent);
156 hfs_bnode_put(bnode);
160 int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
164 res = hfs_brec_find(fd);
167 if (fd->entrylength > rec_len)
169 hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
173 int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
175 struct hfs_btree *tree;
176 struct hfs_bnode *bnode;
178 u16 off, len, keylen;
185 while (cnt > fd->record) {
186 cnt -= fd->record + 1;
187 fd->record = bnode->num_recs - 1;
193 hfs_bnode_put(bnode);
194 bnode = hfs_bnode_find(tree, idx);
196 res = PTR_ERR(bnode);
203 while (cnt >= bnode->num_recs - fd->record) {
204 cnt -= bnode->num_recs - fd->record;
211 hfs_bnode_put(bnode);
212 bnode = hfs_bnode_find(tree, idx);
214 res = PTR_ERR(bnode);
222 len = hfs_brec_lenoff(bnode, fd->record, &off);
223 keylen = hfs_brec_keylen(bnode, fd->record);
229 fd->keylength = keylen;
230 fd->entryoffset = off + keylen;
231 fd->entrylength = len - keylen;
232 hfs_bnode_read(bnode, fd->key, off, keylen);