2 * linux/fs/hfsplus/bfind.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Search routines for btrees
11 #include <linux/slab.h>
12 #include "hfsplus_fs.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 dprint(DBG_BNODE_REFS, "find_init: %d (%p)\n", tree->cnid, __builtin_return_address(0));
26 down(&tree->tree_lock);
30 void hfs_find_exit(struct hfs_find_data *fd)
32 hfs_bnode_put(fd->bnode);
33 kfree(fd->search_key);
34 dprint(DBG_BNODE_REFS, "find_exit: %d (%p)\n", fd->tree->cnid, __builtin_return_address(0));
35 up(&fd->tree->tree_lock);
39 /* Find the record in bnode that best matches key (not greater than...)*/
40 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
49 e = bnode->num_recs - 1;
53 len = hfs_brec_lenoff(bnode, rec, &off);
54 keylen = hfs_brec_keylen(bnode, rec);
55 hfs_bnode_read(bnode, fd->key, off, keylen);
56 cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
67 //printk("%d: %d,%d,%d\n", bnode->this, b, e, rec);
68 if (rec != e && e >= 0) {
69 len = hfs_brec_lenoff(bnode, e, &off);
70 keylen = hfs_brec_keylen(bnode, e);
71 hfs_bnode_read(bnode, fd->key, off, keylen);
76 fd->keylength = keylen;
77 fd->entryoffset = off + keylen;
78 fd->entrylength = len - keylen;
82 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
83 /* Return allocated copy of node found, set recnum to best record */
84 int hfs_brec_find(struct hfs_find_data *fd)
86 struct hfs_btree *tree;
87 struct hfs_bnode *bnode;
94 hfs_bnode_put(fd->bnode);
103 bnode = hfs_bnode_find(tree, nidx);
105 res = PTR_ERR(bnode);
109 if (bnode->height != height)
111 if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
113 bnode->parent = parent;
115 res = __hfs_brec_find(bnode, fd);
122 hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
123 nidx = be32_to_cpu(data);
124 hfs_bnode_put(bnode);
130 printk("HFS+-fs: inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
131 height, bnode->height, bnode->type, nidx, parent);
134 hfs_bnode_put(bnode);
138 int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
142 res = hfs_brec_find(fd);
145 if (fd->entrylength > rec_len)
147 hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
151 int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
153 struct hfs_btree *tree;
154 struct hfs_bnode *bnode;
156 u16 off, len, keylen;
163 while (cnt > fd->record) {
164 cnt -= fd->record + 1;
165 fd->record = bnode->num_recs - 1;
171 hfs_bnode_put(bnode);
172 bnode = hfs_bnode_find(tree, idx);
174 res = PTR_ERR(bnode);
181 while (cnt >= bnode->num_recs - fd->record) {
182 cnt -= bnode->num_recs - fd->record;
189 hfs_bnode_put(bnode);
190 bnode = hfs_bnode_find(tree, idx);
192 res = PTR_ERR(bnode);
200 len = hfs_brec_lenoff(bnode, fd->record, &off);
201 keylen = hfs_brec_keylen(bnode, fd->record);
203 fd->keylength = keylen;
204 fd->entryoffset = off + keylen;
205 fd->entrylength = len - keylen;
206 hfs_bnode_read(bnode, fd->key, off, keylen);