1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hfsplus/bfind.c
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Search routines for btrees
12 #include <linux/slab.h>
13 #include "hfsplus_fs.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_nested(&tree
->tree_lock
,
29 hfsplus_btree_lock_class(tree
));
33 void hfs_find_exit(struct hfs_find_data
*fd
)
35 hfs_bnode_put(fd
->bnode
);
36 kfree(fd
->search_key
);
37 hfs_dbg(BNODE_REFS
, "find_exit: %d (%p)\n",
38 fd
->tree
->cnid
, __builtin_return_address(0));
39 mutex_unlock(&fd
->tree
->tree_lock
);
43 int hfs_find_1st_rec_by_cnid(struct hfs_bnode
*bnode
,
44 struct hfs_find_data
*fd
,
52 if (bnode
->tree
->cnid
== HFSPLUS_EXT_CNID
) {
53 cur_cnid
= fd
->key
->ext
.cnid
;
54 search_cnid
= fd
->search_key
->ext
.cnid
;
55 } else if (bnode
->tree
->cnid
== HFSPLUS_CAT_CNID
) {
56 cur_cnid
= fd
->key
->cat
.parent
;
57 search_cnid
= fd
->search_key
->cat
.parent
;
58 } else if (bnode
->tree
->cnid
== HFSPLUS_ATTR_CNID
) {
59 cur_cnid
= fd
->key
->attr
.cnid
;
60 search_cnid
= fd
->search_key
->attr
.cnid
;
62 cur_cnid
= 0; /* used-uninitialized warning */
67 if (cur_cnid
== search_cnid
) {
69 if ((*begin
) == (*end
))
72 if (be32_to_cpu(cur_cnid
) < be32_to_cpu(search_cnid
))
73 (*begin
) = (*cur_rec
) + 1;
75 (*end
) = (*cur_rec
) - 1;
81 int hfs_find_rec_by_key(struct hfs_bnode
*bnode
,
82 struct hfs_find_data
*fd
,
89 cmpval
= bnode
->tree
->keycmp(fd
->key
, fd
->search_key
);
95 (*begin
) = (*cur_rec
) + 1;
97 *(end
) = (*cur_rec
) - 1;
102 /* Find the record in bnode that best matches key (not greater than...)*/
103 int __hfs_brec_find(struct hfs_bnode
*bnode
, struct hfs_find_data
*fd
,
104 search_strategy_t rec_found
)
106 u16 off
, len
, keylen
;
113 e
= bnode
->num_recs
- 1;
117 len
= hfs_brec_lenoff(bnode
, rec
, &off
);
118 keylen
= hfs_brec_keylen(bnode
, rec
);
123 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
124 if (rec_found(bnode
, fd
, &b
, &e
, &rec
)) {
130 if (rec
!= e
&& e
>= 0) {
131 len
= hfs_brec_lenoff(bnode
, e
, &off
);
132 keylen
= hfs_brec_keylen(bnode
, e
);
137 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
143 fd
->keylength
= keylen
;
144 fd
->entryoffset
= off
+ keylen
;
145 fd
->entrylength
= len
- keylen
;
151 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
152 /* Return allocated copy of node found, set recnum to best record */
153 int hfs_brec_find(struct hfs_find_data
*fd
, search_strategy_t do_key_compare
)
155 struct hfs_btree
*tree
;
156 struct hfs_bnode
*bnode
;
163 hfs_bnode_put(fd
->bnode
);
168 height
= tree
->depth
;
172 bnode
= hfs_bnode_find(tree
, nidx
);
174 res
= PTR_ERR(bnode
);
178 if (bnode
->height
!= height
)
180 if (bnode
->type
!= (--height
? HFS_NODE_INDEX
: HFS_NODE_LEAF
))
182 bnode
->parent
= parent
;
184 res
= __hfs_brec_find(bnode
, fd
, do_key_compare
);
191 hfs_bnode_read(bnode
, &data
, fd
->entryoffset
, 4);
192 nidx
= be32_to_cpu(data
);
193 hfs_bnode_put(bnode
);
199 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
200 height
, bnode
->height
, bnode
->type
, nidx
, parent
);
203 hfs_bnode_put(bnode
);
207 int hfs_brec_read(struct hfs_find_data
*fd
, void *rec
, int rec_len
)
211 res
= hfs_brec_find(fd
, hfs_find_rec_by_key
);
214 if (fd
->entrylength
> rec_len
)
216 hfs_bnode_read(fd
->bnode
, rec
, fd
->entryoffset
, fd
->entrylength
);
220 int hfs_brec_goto(struct hfs_find_data
*fd
, int cnt
)
222 struct hfs_btree
*tree
;
223 struct hfs_bnode
*bnode
;
225 u16 off
, len
, keylen
;
232 while (cnt
> fd
->record
) {
233 cnt
-= fd
->record
+ 1;
234 fd
->record
= bnode
->num_recs
- 1;
240 hfs_bnode_put(bnode
);
241 bnode
= hfs_bnode_find(tree
, idx
);
243 res
= PTR_ERR(bnode
);
250 while (cnt
>= bnode
->num_recs
- fd
->record
) {
251 cnt
-= bnode
->num_recs
- fd
->record
;
258 hfs_bnode_put(bnode
);
259 bnode
= hfs_bnode_find(tree
, idx
);
261 res
= PTR_ERR(bnode
);
269 len
= hfs_brec_lenoff(bnode
, fd
->record
, &off
);
270 keylen
= hfs_brec_keylen(bnode
, fd
->record
);
276 fd
->keylength
= keylen
;
277 fd
->entryoffset
= off
+ keylen
;
278 fd
->entrylength
= len
- keylen
;
279 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);