1 // SPDX-License-Identifier: GPL-2.0
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Search routines for btrees
12 #include <linux/slab.h>
15 int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
21 ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
25 fd->key = ptr + tree->max_key_len + 2;
26 hfs_dbg(BNODE_REFS, "find_init: %d (%p)\n",
27 tree->cnid, __builtin_return_address(0));
30 mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX);
33 mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX);
36 mutex_lock_nested(&tree->tree_lock, ATTR_BTREE_MUTEX);
44 void hfs_find_exit(struct hfs_find_data *fd)
46 hfs_bnode_put(fd->bnode);
47 kfree(fd->search_key);
48 hfs_dbg(BNODE_REFS, "find_exit: %d (%p)\n",
49 fd->tree->cnid, __builtin_return_address(0));
50 mutex_unlock(&fd->tree->tree_lock);
54 /* Find the record in bnode that best matches key (not greater than...)*/
55 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd)
64 e = bnode->num_recs - 1;
68 len = hfs_brec_lenoff(bnode, rec, &off);
69 keylen = hfs_brec_keylen(bnode, rec);
74 hfs_bnode_read(bnode, fd->key, off, keylen);
75 cmpval = bnode->tree->keycmp(fd->key, fd->search_key);
86 if (rec != e && e >= 0) {
87 len = hfs_brec_lenoff(bnode, e, &off);
88 keylen = hfs_brec_keylen(bnode, e);
93 hfs_bnode_read(bnode, fd->key, off, keylen);
98 fd->keylength = keylen;
99 fd->entryoffset = off + keylen;
100 fd->entrylength = len - keylen;
105 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
106 /* Return allocated copy of node found, set recnum to best record */
107 int hfs_brec_find(struct hfs_find_data *fd)
109 struct hfs_btree *tree;
110 struct hfs_bnode *bnode;
117 hfs_bnode_put(fd->bnode);
122 height = tree->depth;
126 bnode = hfs_bnode_find(tree, nidx);
128 res = PTR_ERR(bnode);
132 if (bnode->height != height)
134 if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF))
136 bnode->parent = parent;
138 res = __hfs_brec_find(bnode, fd);
145 hfs_bnode_read(bnode, &data, fd->entryoffset, 4);
146 nidx = be32_to_cpu(data);
147 hfs_bnode_put(bnode);
153 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
154 height, bnode->height, bnode->type, nidx, parent);
157 hfs_bnode_put(bnode);
161 int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len)
165 res = hfs_brec_find(fd);
168 if (fd->entrylength > rec_len)
170 hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength);
174 int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
176 struct hfs_btree *tree;
177 struct hfs_bnode *bnode;
179 u16 off, len, keylen;
186 while (cnt > fd->record) {
187 cnt -= fd->record + 1;
188 fd->record = bnode->num_recs - 1;
194 hfs_bnode_put(bnode);
195 bnode = hfs_bnode_find(tree, idx);
197 res = PTR_ERR(bnode);
204 while (cnt >= bnode->num_recs - fd->record) {
205 cnt -= bnode->num_recs - fd->record;
212 hfs_bnode_put(bnode);
213 bnode = hfs_bnode_find(tree, idx);
215 res = PTR_ERR(bnode);
223 len = hfs_brec_lenoff(bnode, fd->record, &off);
224 keylen = hfs_brec_keylen(bnode, fd->record);
230 fd->keylength = keylen;
231 fd->entryoffset = off + keylen;
232 fd->entrylength = len - keylen;
233 hfs_bnode_read(bnode, fd->key, off, keylen);