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));
28 mutex_lock(&tree
->tree_lock
);
32 void hfs_find_exit(struct hfs_find_data
*fd
)
34 hfs_bnode_put(fd
->bnode
);
35 kfree(fd
->search_key
);
36 hfs_dbg(BNODE_REFS
, "find_exit: %d (%p)\n",
37 fd
->tree
->cnid
, __builtin_return_address(0));
38 mutex_unlock(&fd
->tree
->tree_lock
);
42 /* Find the record in bnode that best matches key (not greater than...)*/
43 int __hfs_brec_find(struct hfs_bnode
*bnode
, struct hfs_find_data
*fd
)
52 e
= bnode
->num_recs
- 1;
56 len
= hfs_brec_lenoff(bnode
, rec
, &off
);
57 keylen
= hfs_brec_keylen(bnode
, rec
);
62 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
63 cmpval
= bnode
->tree
->keycmp(fd
->key
, fd
->search_key
);
74 if (rec
!= e
&& e
>= 0) {
75 len
= hfs_brec_lenoff(bnode
, e
, &off
);
76 keylen
= hfs_brec_keylen(bnode
, e
);
81 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
86 fd
->keylength
= keylen
;
87 fd
->entryoffset
= off
+ keylen
;
88 fd
->entrylength
= len
- keylen
;
93 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
94 /* Return allocated copy of node found, set recnum to best record */
95 int hfs_brec_find(struct hfs_find_data
*fd
)
97 struct hfs_btree
*tree
;
98 struct hfs_bnode
*bnode
;
105 hfs_bnode_put(fd
->bnode
);
110 height
= tree
->depth
;
114 bnode
= hfs_bnode_find(tree
, nidx
);
116 res
= PTR_ERR(bnode
);
120 if (bnode
->height
!= height
)
122 if (bnode
->type
!= (--height
? HFS_NODE_INDEX
: HFS_NODE_LEAF
))
124 bnode
->parent
= parent
;
126 res
= __hfs_brec_find(bnode
, fd
);
133 hfs_bnode_read(bnode
, &data
, fd
->entryoffset
, 4);
134 nidx
= be32_to_cpu(data
);
135 hfs_bnode_put(bnode
);
141 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
142 height
, bnode
->height
, bnode
->type
, nidx
, parent
);
145 hfs_bnode_put(bnode
);
149 int hfs_brec_read(struct hfs_find_data
*fd
, void *rec
, int rec_len
)
153 res
= hfs_brec_find(fd
);
156 if (fd
->entrylength
> rec_len
)
158 hfs_bnode_read(fd
->bnode
, rec
, fd
->entryoffset
, fd
->entrylength
);
162 int hfs_brec_goto(struct hfs_find_data
*fd
, int cnt
)
164 struct hfs_btree
*tree
;
165 struct hfs_bnode
*bnode
;
167 u16 off
, len
, keylen
;
174 while (cnt
> fd
->record
) {
175 cnt
-= fd
->record
+ 1;
176 fd
->record
= bnode
->num_recs
- 1;
182 hfs_bnode_put(bnode
);
183 bnode
= hfs_bnode_find(tree
, idx
);
185 res
= PTR_ERR(bnode
);
192 while (cnt
>= bnode
->num_recs
- fd
->record
) {
193 cnt
-= bnode
->num_recs
- fd
->record
;
200 hfs_bnode_put(bnode
);
201 bnode
= hfs_bnode_find(tree
, idx
);
203 res
= PTR_ERR(bnode
);
211 len
= hfs_brec_lenoff(bnode
, fd
->record
, &off
);
212 keylen
= hfs_brec_keylen(bnode
, fd
->record
);
218 fd
->keylength
= keylen
;
219 fd
->entryoffset
= off
+ keylen
;
220 fd
->entrylength
= len
- keylen
;
221 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);