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 hfs_dbg(BNODE_REFS, "find_init: %d (%p)\n",
26 tree->cnid, __builtin_return_address(0));
28 case HFSPLUS_CAT_CNID:
29 mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX);
31 case HFSPLUS_EXT_CNID:
32 mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX);
34 case HFSPLUS_ATTR_CNID:
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 int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode,
54 struct hfs_find_data *fd,
62 if (bnode->tree->cnid == HFSPLUS_EXT_CNID) {
63 cur_cnid = fd->key->ext.cnid;
64 search_cnid = fd->search_key->ext.cnid;
65 } else if (bnode->tree->cnid == HFSPLUS_CAT_CNID) {
66 cur_cnid = fd->key->cat.parent;
67 search_cnid = fd->search_key->cat.parent;
68 } else if (bnode->tree->cnid == HFSPLUS_ATTR_CNID) {
69 cur_cnid = fd->key->attr.cnid;
70 search_cnid = fd->search_key->attr.cnid;
72 cur_cnid = 0; /* used-uninitialized warning */
77 if (cur_cnid == search_cnid) {
79 if ((*begin) == (*end))
82 if (be32_to_cpu(cur_cnid) < be32_to_cpu(search_cnid))
83 (*begin) = (*cur_rec) + 1;
85 (*end) = (*cur_rec) - 1;
91 int hfs_find_rec_by_key(struct hfs_bnode *bnode,
92 struct hfs_find_data *fd,
99 cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
105 (*begin) = (*cur_rec) + 1;
107 *(end) = (*cur_rec) - 1;
112 /* Find the record in bnode that best matches key (not greater than...)*/
113 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd,
114 search_strategy_t rec_found)
116 u16 off, len, keylen;
123 e = bnode->num_recs - 1;
127 len = hfs_brec_lenoff(bnode, rec, &off);
128 keylen = hfs_brec_keylen(bnode, rec);
133 hfs_bnode_read(bnode, fd->key, off, keylen);
134 if (rec_found(bnode, fd, &b, &e, &rec)) {
140 if (rec != e && e >= 0) {
141 len = hfs_brec_lenoff(bnode, e, &off);
142 keylen = hfs_brec_keylen(bnode, e);
147 hfs_bnode_read(bnode, fd->key, off, keylen);
153 fd->keylength = keylen;
154 fd->entryoffset = off + keylen;
155 fd->entrylength = len - keylen;
161 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
162 /* Return allocated copy of node found, set recnum to best record */
163 int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare)
165 struct hfs_btree *tree;
166 struct hfs_bnode *bnode;
173 hfs_bnode_put(fd->bnode);
178 height = tree->depth;
182 bnode = hfs_bnode_find(tree, nidx);
184 res = PTR_ERR(bnode);
188 if (bnode->height != height)
190 if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
192 bnode->parent = parent;
194 res = __hfs_brec_find(bnode, fd, do_key_compare);
201 hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
202 nidx = be32_to_cpu(data);
203 hfs_bnode_put(bnode);
209 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
210 height, bnode->height, bnode->type, nidx, parent);
213 hfs_bnode_put(bnode);
217 int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
221 res = hfs_brec_find(fd, hfs_find_rec_by_key);
224 if (fd->entrylength > rec_len)
226 hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
230 int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
232 struct hfs_btree *tree;
233 struct hfs_bnode *bnode;
235 u16 off, len, keylen;
242 while (cnt > fd->record) {
243 cnt -= fd->record + 1;
244 fd->record = bnode->num_recs - 1;
250 hfs_bnode_put(bnode);
251 bnode = hfs_bnode_find(tree, idx);
253 res = PTR_ERR(bnode);
260 while (cnt >= bnode->num_recs - fd->record) {
261 cnt -= bnode->num_recs - fd->record;
268 hfs_bnode_put(bnode);
269 bnode = hfs_bnode_find(tree, idx);
271 res = PTR_ERR(bnode);
279 len = hfs_brec_lenoff(bnode, fd->record, &off);
280 keylen = hfs_brec_keylen(bnode, fd->record);
286 fd->keylength = keylen;
287 fd->entryoffset = off + keylen;
288 fd->entrylength = len - keylen;
289 hfs_bnode_read(bnode, fd->key, off, keylen);