Blame view
fs/hfsplus/bfind.c
6.09 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* * linux/fs/hfsplus/bfind.c * * Copyright (C) 2001 * Brad Boyer (flar@allandria.com) * (C) 2003 Ardis Technologies <roman@ardistech.com> * * Search routines for btrees */ #include <linux/slab.h> #include "hfsplus_fs.h" int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) { void *ptr; fd->tree = tree; fd->bnode = NULL; ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL); if (!ptr) return -ENOMEM; fd->search_key = ptr; fd->key = ptr + tree->max_key_len + 2; |
c2b3e1f76 hfs/hfsplus: conv... |
26 27 |
hfs_dbg(BNODE_REFS, "find_init: %d (%p) ", |
2753cc281 hfsplus: over 80 ... |
28 |
tree->cnid, __builtin_return_address(0)); |
324ef39a8 hfsplus: add supp... |
29 30 31 32 33 34 35 36 37 38 39 40 41 |
switch (tree->cnid) { case HFSPLUS_CAT_CNID: mutex_lock_nested(&tree->tree_lock, CATALOG_BTREE_MUTEX); break; case HFSPLUS_EXT_CNID: mutex_lock_nested(&tree->tree_lock, EXTENTS_BTREE_MUTEX); break; case HFSPLUS_ATTR_CNID: mutex_lock_nested(&tree->tree_lock, ATTR_BTREE_MUTEX); break; default: BUG(); } |
1da177e4c Linux-2.6.12-rc2 |
42 43 44 45 46 47 48 |
return 0; } void hfs_find_exit(struct hfs_find_data *fd) { hfs_bnode_put(fd->bnode); kfree(fd->search_key); |
c2b3e1f76 hfs/hfsplus: conv... |
49 50 |
hfs_dbg(BNODE_REFS, "find_exit: %d (%p) ", |
2753cc281 hfsplus: over 80 ... |
51 |
fd->tree->cnid, __builtin_return_address(0)); |
467c3d9cd hfsplus: convert ... |
52 |
mutex_unlock(&fd->tree->tree_lock); |
1da177e4c Linux-2.6.12-rc2 |
53 54 |
fd->tree = NULL; } |
324ef39a8 hfsplus: add supp... |
55 56 57 58 59 60 |
int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode, struct hfs_find_data *fd, int *begin, int *end, int *cur_rec) { |
5f3726f94 hfsplus: fix warn... |
61 62 |
__be32 cur_cnid; __be32 search_cnid; |
324ef39a8 hfsplus: add supp... |
63 64 65 66 67 68 69 70 71 72 |
if (bnode->tree->cnid == HFSPLUS_EXT_CNID) { cur_cnid = fd->key->ext.cnid; search_cnid = fd->search_key->ext.cnid; } else if (bnode->tree->cnid == HFSPLUS_CAT_CNID) { cur_cnid = fd->key->cat.parent; search_cnid = fd->search_key->cat.parent; } else if (bnode->tree->cnid == HFSPLUS_ATTR_CNID) { cur_cnid = fd->key->attr.cnid; search_cnid = fd->search_key->attr.cnid; |
5f3726f94 hfsplus: fix warn... |
73 74 75 |
} else { cur_cnid = 0; /* used-uninitialized warning */ search_cnid = 0; |
324ef39a8 hfsplus: add supp... |
76 |
BUG(); |
5f3726f94 hfsplus: fix warn... |
77 |
} |
324ef39a8 hfsplus: add supp... |
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
if (cur_cnid == search_cnid) { (*end) = (*cur_rec); if ((*begin) == (*end)) return 1; } else { if (be32_to_cpu(cur_cnid) < be32_to_cpu(search_cnid)) (*begin) = (*cur_rec) + 1; else (*end) = (*cur_rec) - 1; } return 0; } int hfs_find_rec_by_key(struct hfs_bnode *bnode, struct hfs_find_data *fd, int *begin, int *end, int *cur_rec) |
1da177e4c Linux-2.6.12-rc2 |
98 99 |
{ int cmpval; |
324ef39a8 hfsplus: add supp... |
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
cmpval = bnode->tree->keycmp(fd->key, fd->search_key); if (!cmpval) { (*end) = (*cur_rec); return 1; } if (cmpval < 0) (*begin) = (*cur_rec) + 1; else *(end) = (*cur_rec) - 1; return 0; } /* Find the record in bnode that best matches key (not greater than...)*/ int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd, search_strategy_t rec_found) { |
1da177e4c Linux-2.6.12-rc2 |
118 119 120 121 |
u16 off, len, keylen; int rec; int b, e; int res; |
7ce844a20 fs/hfsplus: repla... |
122 |
BUG_ON(!rec_found); |
1da177e4c Linux-2.6.12-rc2 |
123 124 125 126 127 128 129 |
b = 0; e = bnode->num_recs - 1; res = -ENOENT; do { rec = (e + b) / 2; len = hfs_brec_lenoff(bnode, rec, &off); keylen = hfs_brec_keylen(bnode, rec); |
9250f9259 hfsplus: handle m... |
130 131 132 133 |
if (keylen == 0) { res = -EINVAL; goto fail; } |
1da177e4c Linux-2.6.12-rc2 |
134 |
hfs_bnode_read(bnode, fd->key, off, keylen); |
324ef39a8 hfsplus: add supp... |
135 |
if (rec_found(bnode, fd, &b, &e, &rec)) { |
1da177e4c Linux-2.6.12-rc2 |
136 137 138 |
res = 0; goto done; } |
1da177e4c Linux-2.6.12-rc2 |
139 |
} while (b <= e); |
324ef39a8 hfsplus: add supp... |
140 |
|
1da177e4c Linux-2.6.12-rc2 |
141 142 143 |
if (rec != e && e >= 0) { len = hfs_brec_lenoff(bnode, e, &off); keylen = hfs_brec_keylen(bnode, e); |
9250f9259 hfsplus: handle m... |
144 145 146 147 |
if (keylen == 0) { res = -EINVAL; goto fail; } |
1da177e4c Linux-2.6.12-rc2 |
148 149 |
hfs_bnode_read(bnode, fd->key, off, keylen); } |
324ef39a8 hfsplus: add supp... |
150 |
|
1da177e4c Linux-2.6.12-rc2 |
151 152 153 154 155 156 |
done: fd->record = e; fd->keyoffset = off; fd->keylength = keylen; fd->entryoffset = off + keylen; fd->entrylength = len - keylen; |
324ef39a8 hfsplus: add supp... |
157 |
|
9250f9259 hfsplus: handle m... |
158 |
fail: |
1da177e4c Linux-2.6.12-rc2 |
159 160 161 162 163 |
return res; } /* Traverse a B*Tree from the root to a leaf finding best fit to key */ /* Return allocated copy of node found, set recnum to best record */ |
324ef39a8 hfsplus: add supp... |
164 |
int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare) |
1da177e4c Linux-2.6.12-rc2 |
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
{ struct hfs_btree *tree; struct hfs_bnode *bnode; u32 nidx, parent; __be32 data; int height, res; tree = fd->tree; if (fd->bnode) hfs_bnode_put(fd->bnode); fd->bnode = NULL; nidx = tree->root; if (!nidx) return -ENOENT; height = tree->depth; res = 0; parent = 0; for (;;) { bnode = hfs_bnode_find(tree, nidx); if (IS_ERR(bnode)) { res = PTR_ERR(bnode); bnode = NULL; break; } if (bnode->height != height) goto invalid; if (bnode->type != (--height ? HFS_NODE_INDEX : HFS_NODE_LEAF)) goto invalid; bnode->parent = parent; |
324ef39a8 hfsplus: add supp... |
194 |
res = __hfs_brec_find(bnode, fd, do_key_compare); |
1da177e4c Linux-2.6.12-rc2 |
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
if (!height) break; if (fd->record < 0) goto release; parent = nidx; hfs_bnode_read(bnode, &data, fd->entryoffset, 4); nidx = be32_to_cpu(data); hfs_bnode_put(bnode); } fd->bnode = bnode; return res; invalid: |
d61426732 hfs/hfsplus: conv... |
209 210 |
pr_err("inconsistency in B*Tree (%d,%d,%d,%u,%u) ", |
1da177e4c Linux-2.6.12-rc2 |
211 212 213 214 215 216 217 218 219 220 |
height, bnode->height, bnode->type, nidx, parent); res = -EIO; release: hfs_bnode_put(bnode); return res; } int hfs_brec_read(struct hfs_find_data *fd, void *rec, int rec_len) { int res; |
324ef39a8 hfsplus: add supp... |
221 |
res = hfs_brec_find(fd, hfs_find_rec_by_key); |
1da177e4c Linux-2.6.12-rc2 |
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
if (res) return res; if (fd->entrylength > rec_len) return -EINVAL; hfs_bnode_read(fd->bnode, rec, fd->entryoffset, fd->entrylength); return 0; } int hfs_brec_goto(struct hfs_find_data *fd, int cnt) { struct hfs_btree *tree; struct hfs_bnode *bnode; int idx, res = 0; u16 off, len, keylen; bnode = fd->bnode; tree = bnode->tree; if (cnt < 0) { cnt = -cnt; while (cnt > fd->record) { cnt -= fd->record + 1; fd->record = bnode->num_recs - 1; idx = bnode->prev; if (!idx) { res = -ENOENT; goto out; } hfs_bnode_put(bnode); bnode = hfs_bnode_find(tree, idx); if (IS_ERR(bnode)) { res = PTR_ERR(bnode); bnode = NULL; goto out; } } fd->record -= cnt; } else { while (cnt >= bnode->num_recs - fd->record) { cnt -= bnode->num_recs - fd->record; fd->record = 0; idx = bnode->next; if (!idx) { res = -ENOENT; goto out; } hfs_bnode_put(bnode); bnode = hfs_bnode_find(tree, idx); if (IS_ERR(bnode)) { res = PTR_ERR(bnode); bnode = NULL; goto out; } } fd->record += cnt; } len = hfs_brec_lenoff(bnode, fd->record, &off); keylen = hfs_brec_keylen(bnode, fd->record); |
9250f9259 hfsplus: handle m... |
281 282 283 284 |
if (keylen == 0) { res = -EINVAL; goto out; } |
1da177e4c Linux-2.6.12-rc2 |
285 286 287 288 289 290 291 292 293 |
fd->keyoffset = off; fd->keylength = keylen; fd->entryoffset = off + keylen; fd->entrylength = len - keylen; hfs_bnode_read(bnode, fd->key, off, keylen); out: fd->bnode = bnode; return res; } |