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));
29 case HFSPLUS_CAT_CNID
:
30 mutex_lock_nested(&tree
->tree_lock
, CATALOG_BTREE_MUTEX
);
32 case HFSPLUS_EXT_CNID
:
33 mutex_lock_nested(&tree
->tree_lock
, EXTENTS_BTREE_MUTEX
);
35 case HFSPLUS_ATTR_CNID
:
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 int hfs_find_1st_rec_by_cnid(struct hfs_bnode
*bnode
,
55 struct hfs_find_data
*fd
,
63 if (bnode
->tree
->cnid
== HFSPLUS_EXT_CNID
) {
64 cur_cnid
= fd
->key
->ext
.cnid
;
65 search_cnid
= fd
->search_key
->ext
.cnid
;
66 } else if (bnode
->tree
->cnid
== HFSPLUS_CAT_CNID
) {
67 cur_cnid
= fd
->key
->cat
.parent
;
68 search_cnid
= fd
->search_key
->cat
.parent
;
69 } else if (bnode
->tree
->cnid
== HFSPLUS_ATTR_CNID
) {
70 cur_cnid
= fd
->key
->attr
.cnid
;
71 search_cnid
= fd
->search_key
->attr
.cnid
;
73 cur_cnid
= 0; /* used-uninitialized warning */
78 if (cur_cnid
== search_cnid
) {
80 if ((*begin
) == (*end
))
83 if (be32_to_cpu(cur_cnid
) < be32_to_cpu(search_cnid
))
84 (*begin
) = (*cur_rec
) + 1;
86 (*end
) = (*cur_rec
) - 1;
92 int hfs_find_rec_by_key(struct hfs_bnode
*bnode
,
93 struct hfs_find_data
*fd
,
100 cmpval
= bnode
->tree
->keycmp(fd
->key
, fd
->search_key
);
106 (*begin
) = (*cur_rec
) + 1;
108 *(end
) = (*cur_rec
) - 1;
113 /* Find the record in bnode that best matches key (not greater than...)*/
114 int __hfs_brec_find(struct hfs_bnode
*bnode
, struct hfs_find_data
*fd
,
115 search_strategy_t rec_found
)
117 u16 off
, len
, keylen
;
124 e
= bnode
->num_recs
- 1;
128 len
= hfs_brec_lenoff(bnode
, rec
, &off
);
129 keylen
= hfs_brec_keylen(bnode
, rec
);
134 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
135 if (rec_found(bnode
, fd
, &b
, &e
, &rec
)) {
141 if (rec
!= e
&& e
>= 0) {
142 len
= hfs_brec_lenoff(bnode
, e
, &off
);
143 keylen
= hfs_brec_keylen(bnode
, e
);
148 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);
154 fd
->keylength
= keylen
;
155 fd
->entryoffset
= off
+ keylen
;
156 fd
->entrylength
= len
- keylen
;
162 /* Traverse a B*Tree from the root to a leaf finding best fit to key */
163 /* Return allocated copy of node found, set recnum to best record */
164 int hfs_brec_find(struct hfs_find_data
*fd
, search_strategy_t do_key_compare
)
166 struct hfs_btree
*tree
;
167 struct hfs_bnode
*bnode
;
174 hfs_bnode_put(fd
->bnode
);
179 height
= tree
->depth
;
183 bnode
= hfs_bnode_find(tree
, nidx
);
185 res
= PTR_ERR(bnode
);
189 if (bnode
->height
!= height
)
191 if (bnode
->type
!= (--height
? HFS_NODE_INDEX
: HFS_NODE_LEAF
))
193 bnode
->parent
= parent
;
195 res
= __hfs_brec_find(bnode
, fd
, do_key_compare
);
202 hfs_bnode_read(bnode
, &data
, fd
->entryoffset
, 4);
203 nidx
= be32_to_cpu(data
);
204 hfs_bnode_put(bnode
);
210 pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u)\n",
211 height
, bnode
->height
, bnode
->type
, nidx
, parent
);
214 hfs_bnode_put(bnode
);
218 int hfs_brec_read(struct hfs_find_data
*fd
, void *rec
, int rec_len
)
222 res
= hfs_brec_find(fd
, hfs_find_rec_by_key
);
225 if (fd
->entrylength
> rec_len
)
227 hfs_bnode_read(fd
->bnode
, rec
, fd
->entryoffset
, fd
->entrylength
);
231 int hfs_brec_goto(struct hfs_find_data
*fd
, int cnt
)
233 struct hfs_btree
*tree
;
234 struct hfs_bnode
*bnode
;
236 u16 off
, len
, keylen
;
243 while (cnt
> fd
->record
) {
244 cnt
-= fd
->record
+ 1;
245 fd
->record
= bnode
->num_recs
- 1;
251 hfs_bnode_put(bnode
);
252 bnode
= hfs_bnode_find(tree
, idx
);
254 res
= PTR_ERR(bnode
);
261 while (cnt
>= bnode
->num_recs
- fd
->record
) {
262 cnt
-= bnode
->num_recs
- fd
->record
;
269 hfs_bnode_put(bnode
);
270 bnode
= hfs_bnode_find(tree
, idx
);
272 res
= PTR_ERR(bnode
);
280 len
= hfs_brec_lenoff(bnode
, fd
->record
, &off
);
281 keylen
= hfs_brec_keylen(bnode
, fd
->record
);
287 fd
->keylength
= keylen
;
288 fd
->entryoffset
= off
+ keylen
;
289 fd
->entrylength
= len
- keylen
;
290 hfs_bnode_read(bnode
, fd
->key
, off
, keylen
);