]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/befs/btree.c | |
3 | * | |
4 | * Copyright (C) 2001-2002 Will Dyson <[email protected]> | |
5 | * | |
6 | * Licensed under the GNU GPL. See the file COPYING for details. | |
7 | * | |
25985edc | 8 | * 2002-02-05: Sergey S. Kostyliov added binary search within |
1da177e4 LT |
9 | * btree nodes. |
10 | * | |
11 | * Many thanks to: | |
12 | * | |
13 | * Dominic Giampaolo, author of "Practical File System | |
14 | * Design with the Be File System", for such a helpful book. | |
15 | * | |
16 | * Marcus J. Ranum, author of the b+tree package in | |
17 | * comp.sources.misc volume 10. This code is not copied from that | |
18 | * work, but it is partially based on it. | |
19 | * | |
20 | * Makoto Kato, author of the original BeFS for linux filesystem | |
21 | * driver. | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/string.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/buffer_head.h> | |
29 | ||
30 | #include "befs.h" | |
31 | #include "btree.h" | |
32 | #include "datastream.h" | |
1da177e4 LT |
33 | |
34 | /* | |
35 | * The btree functions in this file are built on top of the | |
36 | * datastream.c interface, which is in turn built on top of the | |
37 | * io.c interface. | |
38 | */ | |
39 | ||
40 | /* Befs B+tree structure: | |
41 | * | |
42 | * The first thing in the tree is the tree superblock. It tells you | |
43 | * all kinds of useful things about the tree, like where the rootnode | |
44 | * is located, and the size of the nodes (always 1024 with current version | |
45 | * of BeOS). | |
46 | * | |
47 | * The rest of the tree consists of a series of nodes. Nodes contain a header | |
48 | * (struct befs_btree_nodehead), the packed key data, an array of shorts | |
49 | * containing the ending offsets for each of the keys, and an array of | |
50 | * befs_off_t values. In interior nodes, the keys are the ending keys for | |
51 | * the childnode they point to, and the values are offsets into the | |
52 | * datastream containing the tree. | |
53 | */ | |
54 | ||
55 | /* Note: | |
56 | * | |
57 | * The book states 2 confusing things about befs b+trees. First, | |
58 | * it states that the overflow field of node headers is used by internal nodes | |
59 | * to point to another node that "effectively continues this one". Here is what | |
60 | * I believe that means. Each key in internal nodes points to another node that | |
61 | * contains key values less than itself. Inspection reveals that the last key | |
62 | * in the internal node is not the last key in the index. Keys that are | |
63 | * greater than the last key in the internal node go into the overflow node. | |
64 | * I imagine there is a performance reason for this. | |
65 | * | |
66 | * Second, it states that the header of a btree node is sufficient to | |
67 | * distinguish internal nodes from leaf nodes. Without saying exactly how. | |
68 | * After figuring out the first, it becomes obvious that internal nodes have | |
69 | * overflow nodes and leafnodes do not. | |
70 | */ | |
71 | ||
72 | /* | |
73 | * Currently, this code is only good for directory B+trees. | |
74 | * In order to be used for other BFS indexes, it needs to be extended to handle | |
75 | * duplicate keys and non-string keytypes (int32, int64, float, double). | |
76 | */ | |
77 | ||
78 | /* | |
79 | * In memory structure of each btree node | |
80 | */ | |
0f2a84f4 | 81 | struct befs_btree_node { |
a9721f31 | 82 | befs_host_btree_nodehead head; /* head of node converted to cpu byteorder */ |
1da177e4 LT |
83 | struct buffer_head *bh; |
84 | befs_btree_nodehead *od_node; /* on disk node */ | |
0f2a84f4 | 85 | }; |
1da177e4 LT |
86 | |
87 | /* local constants */ | |
88 | static const befs_off_t befs_bt_inval = 0xffffffffffffffffULL; | |
89 | ||
90 | /* local functions */ | |
22341d8f | 91 | static int befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, |
1da177e4 | 92 | befs_btree_super * bt_super, |
0f2a84f4 | 93 | struct befs_btree_node *this_node, |
1da177e4 LT |
94 | befs_off_t * node_off); |
95 | ||
22341d8f | 96 | static int befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds, |
1da177e4 LT |
97 | befs_btree_super * sup); |
98 | ||
22341d8f | 99 | static int befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds, |
0f2a84f4 HS |
100 | struct befs_btree_node *node, |
101 | befs_off_t node_off); | |
1da177e4 | 102 | |
0f2a84f4 | 103 | static int befs_leafnode(struct befs_btree_node *node); |
1da177e4 | 104 | |
0f2a84f4 | 105 | static fs16 *befs_bt_keylen_index(struct befs_btree_node *node); |
1da177e4 | 106 | |
0f2a84f4 | 107 | static fs64 *befs_bt_valarray(struct befs_btree_node *node); |
1da177e4 | 108 | |
0f2a84f4 | 109 | static char *befs_bt_keydata(struct befs_btree_node *node); |
1da177e4 | 110 | |
0f2a84f4 HS |
111 | static int befs_find_key(struct super_block *sb, |
112 | struct befs_btree_node *node, | |
1da177e4 LT |
113 | const char *findkey, befs_off_t * value); |
114 | ||
0f2a84f4 HS |
115 | static char *befs_bt_get_key(struct super_block *sb, |
116 | struct befs_btree_node *node, | |
1da177e4 LT |
117 | int index, u16 * keylen); |
118 | ||
119 | static int befs_compare_strings(const void *key1, int keylen1, | |
120 | const void *key2, int keylen2); | |
121 | ||
122 | /** | |
123 | * befs_bt_read_super - read in btree superblock convert to cpu byteorder | |
124 | * @sb: Filesystem superblock | |
125 | * @ds: Datastream to read from | |
126 | * @sup: Buffer in which to place the btree superblock | |
127 | * | |
128 | * Calls befs_read_datastream to read in the btree superblock and | |
129 | * makes sure it is in cpu byteorder, byteswapping if necessary. | |
130 | * | |
131 | * On success, returns BEFS_OK and *@sup contains the btree superblock, | |
132 | * in cpu byte order. | |
133 | * | |
134 | * On failure, BEFS_ERR is returned. | |
135 | */ | |
136 | static int | |
22341d8f | 137 | befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds, |
1da177e4 LT |
138 | befs_btree_super * sup) |
139 | { | |
9f0f564a FF |
140 | struct buffer_head *bh; |
141 | befs_disk_btree_super *od_sup; | |
1da177e4 | 142 | |
dac52fc1 | 143 | befs_debug(sb, "---> %s", __func__); |
1da177e4 LT |
144 | |
145 | bh = befs_read_datastream(sb, ds, 0, NULL); | |
146 | ||
147 | if (!bh) { | |
148 | befs_error(sb, "Couldn't read index header."); | |
149 | goto error; | |
150 | } | |
a9721f31 | 151 | od_sup = (befs_disk_btree_super *) bh->b_data; |
1da177e4 LT |
152 | befs_dump_index_entry(sb, od_sup); |
153 | ||
154 | sup->magic = fs32_to_cpu(sb, od_sup->magic); | |
155 | sup->node_size = fs32_to_cpu(sb, od_sup->node_size); | |
156 | sup->max_depth = fs32_to_cpu(sb, od_sup->max_depth); | |
157 | sup->data_type = fs32_to_cpu(sb, od_sup->data_type); | |
158 | sup->root_node_ptr = fs64_to_cpu(sb, od_sup->root_node_ptr); | |
159 | sup->free_node_ptr = fs64_to_cpu(sb, od_sup->free_node_ptr); | |
160 | sup->max_size = fs64_to_cpu(sb, od_sup->max_size); | |
161 | ||
162 | brelse(bh); | |
163 | if (sup->magic != BEFS_BTREE_MAGIC) { | |
164 | befs_error(sb, "Index header has bad magic."); | |
165 | goto error; | |
166 | } | |
167 | ||
dac52fc1 | 168 | befs_debug(sb, "<--- %s", __func__); |
1da177e4 LT |
169 | return BEFS_OK; |
170 | ||
171 | error: | |
dac52fc1 | 172 | befs_debug(sb, "<--- %s ERROR", __func__); |
1da177e4 LT |
173 | return BEFS_ERR; |
174 | } | |
175 | ||
176 | /** | |
177 | * befs_bt_read_node - read in btree node and convert to cpu byteorder | |
178 | * @sb: Filesystem superblock | |
179 | * @ds: Datastream to read from | |
180 | * @node: Buffer in which to place the btree node | |
181 | * @node_off: Starting offset (in bytes) of the node in @ds | |
182 | * | |
183 | * Calls befs_read_datastream to read in the indicated btree node and | |
184 | * makes sure its header fields are in cpu byteorder, byteswapping if | |
185 | * necessary. | |
186 | * Note: node->bh must be NULL when this function called first | |
187 | * time. Don't forget brelse(node->bh) after last call. | |
188 | * | |
189 | * On success, returns BEFS_OK and *@node contains the btree node that | |
190 | * starts at @node_off, with the node->head fields in cpu byte order. | |
191 | * | |
192 | * On failure, BEFS_ERR is returned. | |
193 | */ | |
194 | ||
195 | static int | |
22341d8f | 196 | befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds, |
0f2a84f4 | 197 | struct befs_btree_node *node, befs_off_t node_off) |
1da177e4 LT |
198 | { |
199 | uint off = 0; | |
200 | ||
dac52fc1 | 201 | befs_debug(sb, "---> %s", __func__); |
1da177e4 LT |
202 | |
203 | if (node->bh) | |
204 | brelse(node->bh); | |
205 | ||
206 | node->bh = befs_read_datastream(sb, ds, node_off, &off); | |
207 | if (!node->bh) { | |
dac52fc1 FF |
208 | befs_error(sb, "%s failed to read " |
209 | "node at %llu", __func__, node_off); | |
210 | befs_debug(sb, "<--- %s ERROR", __func__); | |
1da177e4 LT |
211 | |
212 | return BEFS_ERR; | |
213 | } | |
214 | node->od_node = | |
215 | (befs_btree_nodehead *) ((void *) node->bh->b_data + off); | |
216 | ||
217 | befs_dump_index_node(sb, node->od_node); | |
218 | ||
219 | node->head.left = fs64_to_cpu(sb, node->od_node->left); | |
220 | node->head.right = fs64_to_cpu(sb, node->od_node->right); | |
221 | node->head.overflow = fs64_to_cpu(sb, node->od_node->overflow); | |
222 | node->head.all_key_count = | |
223 | fs16_to_cpu(sb, node->od_node->all_key_count); | |
224 | node->head.all_key_length = | |
225 | fs16_to_cpu(sb, node->od_node->all_key_length); | |
226 | ||
dac52fc1 | 227 | befs_debug(sb, "<--- %s", __func__); |
1da177e4 LT |
228 | return BEFS_OK; |
229 | } | |
230 | ||
231 | /** | |
232 | * befs_btree_find - Find a key in a befs B+tree | |
233 | * @sb: Filesystem superblock | |
234 | * @ds: Datastream containing btree | |
235 | * @key: Key string to lookup in btree | |
236 | * @value: Value stored with @key | |
237 | * | |
c78bad11 | 238 | * On success, returns BEFS_OK and sets *@value to the value stored |
1da177e4 LT |
239 | * with @key (usually the disk block number of an inode). |
240 | * | |
241 | * On failure, returns BEFS_ERR or BEFS_BT_NOT_FOUND. | |
242 | * | |
243 | * Algorithm: | |
244 | * Read the superblock and rootnode of the b+tree. | |
245 | * Drill down through the interior nodes using befs_find_key(). | |
246 | * Once at the correct leaf node, use befs_find_key() again to get the | |
247 | * actuall value stored with the key. | |
248 | */ | |
249 | int | |
22341d8f | 250 | befs_btree_find(struct super_block *sb, const befs_data_stream *ds, |
1da177e4 LT |
251 | const char *key, befs_off_t * value) |
252 | { | |
9f0f564a | 253 | struct befs_btree_node *this_node; |
1da177e4 LT |
254 | befs_btree_super bt_super; |
255 | befs_off_t node_off; | |
256 | int res; | |
257 | ||
dac52fc1 | 258 | befs_debug(sb, "---> %s Key: %s", __func__, key); |
1da177e4 LT |
259 | |
260 | if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { | |
261 | befs_error(sb, | |
262 | "befs_btree_find() failed to read index superblock"); | |
263 | goto error; | |
264 | } | |
265 | ||
0f2a84f4 | 266 | this_node = kmalloc(sizeof(struct befs_btree_node), |
1da177e4 LT |
267 | GFP_NOFS); |
268 | if (!this_node) { | |
dac52fc1 | 269 | befs_error(sb, "befs_btree_find() failed to allocate %zu " |
0f2a84f4 | 270 | "bytes of memory", sizeof(struct befs_btree_node)); |
1da177e4 LT |
271 | goto error; |
272 | } | |
273 | ||
274 | this_node->bh = NULL; | |
275 | ||
276 | /* read in root node */ | |
277 | node_off = bt_super.root_node_ptr; | |
278 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { | |
279 | befs_error(sb, "befs_btree_find() failed to read " | |
dac52fc1 | 280 | "node at %llu", node_off); |
1da177e4 LT |
281 | goto error_alloc; |
282 | } | |
283 | ||
284 | while (!befs_leafnode(this_node)) { | |
285 | res = befs_find_key(sb, this_node, key, &node_off); | |
286 | if (res == BEFS_BT_NOT_FOUND) | |
287 | node_off = this_node->head.overflow; | |
288 | /* if no match, go to overflow node */ | |
289 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { | |
290 | befs_error(sb, "befs_btree_find() failed to read " | |
dac52fc1 | 291 | "node at %llu", node_off); |
1da177e4 LT |
292 | goto error_alloc; |
293 | } | |
294 | } | |
295 | ||
296 | /* at the correct leaf node now */ | |
297 | ||
298 | res = befs_find_key(sb, this_node, key, value); | |
299 | ||
300 | brelse(this_node->bh); | |
301 | kfree(this_node); | |
302 | ||
303 | if (res != BEFS_BT_MATCH) { | |
dac52fc1 | 304 | befs_debug(sb, "<--- %s Key %s not found", __func__, key); |
1da177e4 LT |
305 | *value = 0; |
306 | return BEFS_BT_NOT_FOUND; | |
307 | } | |
dac52fc1 | 308 | befs_debug(sb, "<--- %s Found key %s, value %llu", __func__, |
1da177e4 LT |
309 | key, *value); |
310 | return BEFS_OK; | |
311 | ||
312 | error_alloc: | |
313 | kfree(this_node); | |
314 | error: | |
315 | *value = 0; | |
dac52fc1 | 316 | befs_debug(sb, "<--- %s ERROR", __func__); |
1da177e4 LT |
317 | return BEFS_ERR; |
318 | } | |
319 | ||
320 | /** | |
321 | * befs_find_key - Search for a key within a node | |
322 | * @sb: Filesystem superblock | |
323 | * @node: Node to find the key within | |
817e1d90 | 324 | * @findkey: Keystring to search for |
1da177e4 LT |
325 | * @value: If key is found, the value stored with the key is put here |
326 | * | |
327 | * finds exact match if one exists, and returns BEFS_BT_MATCH | |
328 | * If no exact match, finds first key in node that is greater | |
329 | * (alphabetically) than the search key and returns BEFS_BT_PARMATCH | |
330 | * (for partial match, I guess). Can you think of something better to | |
331 | * call it? | |
332 | * | |
333 | * If no key was a match or greater than the search key, return | |
334 | * BEFS_BT_NOT_FOUND. | |
335 | * | |
336 | * Use binary search instead of a linear. | |
337 | */ | |
338 | static int | |
0f2a84f4 | 339 | befs_find_key(struct super_block *sb, struct befs_btree_node *node, |
1da177e4 LT |
340 | const char *findkey, befs_off_t * value) |
341 | { | |
342 | int first, last, mid; | |
343 | int eq; | |
344 | u16 keylen; | |
345 | int findkey_len; | |
346 | char *thiskey; | |
a9721f31 | 347 | fs64 *valarray; |
1da177e4 | 348 | |
dac52fc1 | 349 | befs_debug(sb, "---> %s %s", __func__, findkey); |
1da177e4 LT |
350 | |
351 | *value = 0; | |
352 | ||
353 | findkey_len = strlen(findkey); | |
354 | ||
355 | /* if node can not contain key, just skeep this node */ | |
356 | last = node->head.all_key_count - 1; | |
357 | thiskey = befs_bt_get_key(sb, node, last, &keylen); | |
358 | ||
359 | eq = befs_compare_strings(thiskey, keylen, findkey, findkey_len); | |
360 | if (eq < 0) { | |
dac52fc1 | 361 | befs_debug(sb, "<--- %s %s not found", __func__, findkey); |
1da177e4 LT |
362 | return BEFS_BT_NOT_FOUND; |
363 | } | |
364 | ||
365 | valarray = befs_bt_valarray(node); | |
366 | ||
367 | /* simple binary search */ | |
368 | first = 0; | |
369 | mid = 0; | |
370 | while (last >= first) { | |
371 | mid = (last + first) / 2; | |
372 | befs_debug(sb, "first: %d, last: %d, mid: %d", first, last, | |
373 | mid); | |
374 | thiskey = befs_bt_get_key(sb, node, mid, &keylen); | |
375 | eq = befs_compare_strings(thiskey, keylen, findkey, | |
376 | findkey_len); | |
377 | ||
378 | if (eq == 0) { | |
dac52fc1 FF |
379 | befs_debug(sb, "<--- %s found %s at %d", |
380 | __func__, thiskey, mid); | |
1da177e4 LT |
381 | |
382 | *value = fs64_to_cpu(sb, valarray[mid]); | |
383 | return BEFS_BT_MATCH; | |
384 | } | |
385 | if (eq > 0) | |
386 | last = mid - 1; | |
387 | else | |
388 | first = mid + 1; | |
389 | } | |
390 | if (eq < 0) | |
391 | *value = fs64_to_cpu(sb, valarray[mid + 1]); | |
392 | else | |
393 | *value = fs64_to_cpu(sb, valarray[mid]); | |
dac52fc1 | 394 | befs_debug(sb, "<--- %s found %s at %d", __func__, thiskey, mid); |
1da177e4 LT |
395 | return BEFS_BT_PARMATCH; |
396 | } | |
397 | ||
398 | /** | |
399 | * befs_btree_read - Traverse leafnodes of a btree | |
400 | * @sb: Filesystem superblock | |
401 | * @ds: Datastream containing btree | |
402 | * @key_no: Key number (alphabetical order) of key to read | |
403 | * @bufsize: Size of the buffer to return key in | |
404 | * @keybuf: Pointer to a buffer to put the key in | |
405 | * @keysize: Length of the returned key | |
406 | * @value: Value stored with the returned key | |
407 | * | |
408 | * Heres how it works: Key_no is the index of the key/value pair to | |
409 | * return in keybuf/value. | |
410 | * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is | |
6cb103b6 | 411 | * the number of characters in the key (just a convenience). |
1da177e4 LT |
412 | * |
413 | * Algorithm: | |
414 | * Get the first leafnode of the tree. See if the requested key is in that | |
415 | * node. If not, follow the node->right link to the next leafnode. Repeat | |
416 | * until the (key_no)th key is found or the tree is out of keys. | |
417 | */ | |
418 | int | |
22341d8f | 419 | befs_btree_read(struct super_block *sb, const befs_data_stream *ds, |
1da177e4 LT |
420 | loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize, |
421 | befs_off_t * value) | |
422 | { | |
0f2a84f4 | 423 | struct befs_btree_node *this_node; |
1da177e4 LT |
424 | befs_btree_super bt_super; |
425 | befs_off_t node_off = 0; | |
426 | int cur_key; | |
a9721f31 | 427 | fs64 *valarray; |
1da177e4 LT |
428 | char *keystart; |
429 | u16 keylen; | |
430 | int res; | |
431 | ||
432 | uint key_sum = 0; | |
433 | ||
dac52fc1 | 434 | befs_debug(sb, "---> %s", __func__); |
1da177e4 LT |
435 | |
436 | if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { | |
437 | befs_error(sb, | |
438 | "befs_btree_read() failed to read index superblock"); | |
439 | goto error; | |
440 | } | |
441 | ||
0f2a84f4 HS |
442 | this_node = kmalloc(sizeof(struct befs_btree_node), GFP_NOFS); |
443 | if (this_node == NULL) { | |
dac52fc1 | 444 | befs_error(sb, "befs_btree_read() failed to allocate %zu " |
0f2a84f4 | 445 | "bytes of memory", sizeof(struct befs_btree_node)); |
1da177e4 LT |
446 | goto error; |
447 | } | |
448 | ||
449 | node_off = bt_super.root_node_ptr; | |
450 | this_node->bh = NULL; | |
451 | ||
452 | /* seeks down to first leafnode, reads it into this_node */ | |
453 | res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off); | |
454 | if (res == BEFS_BT_EMPTY) { | |
455 | brelse(this_node->bh); | |
456 | kfree(this_node); | |
457 | *value = 0; | |
458 | *keysize = 0; | |
dac52fc1 | 459 | befs_debug(sb, "<--- %s Tree is EMPTY", __func__); |
1da177e4 LT |
460 | return BEFS_BT_EMPTY; |
461 | } else if (res == BEFS_ERR) { | |
462 | goto error_alloc; | |
463 | } | |
464 | ||
465 | /* find the leaf node containing the key_no key */ | |
466 | ||
467 | while (key_sum + this_node->head.all_key_count <= key_no) { | |
468 | ||
469 | /* no more nodes to look in: key_no is too large */ | |
470 | if (this_node->head.right == befs_bt_inval) { | |
471 | *keysize = 0; | |
472 | *value = 0; | |
473 | befs_debug(sb, | |
dac52fc1 FF |
474 | "<--- %s END of keys at %llu", __func__, |
475 | (unsigned long long) | |
1da177e4 LT |
476 | key_sum + this_node->head.all_key_count); |
477 | brelse(this_node->bh); | |
478 | kfree(this_node); | |
479 | return BEFS_BT_END; | |
480 | } | |
481 | ||
482 | key_sum += this_node->head.all_key_count; | |
483 | node_off = this_node->head.right; | |
484 | ||
485 | if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { | |
dac52fc1 FF |
486 | befs_error(sb, "%s failed to read node at %llu", |
487 | __func__, (unsigned long long)node_off); | |
1da177e4 LT |
488 | goto error_alloc; |
489 | } | |
490 | } | |
491 | ||
492 | /* how many keys into this_node is key_no */ | |
493 | cur_key = key_no - key_sum; | |
494 | ||
495 | /* get pointers to datastructures within the node body */ | |
496 | valarray = befs_bt_valarray(this_node); | |
497 | ||
498 | keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen); | |
499 | ||
dac52fc1 FF |
500 | befs_debug(sb, "Read [%llu,%d]: keysize %d", |
501 | (long long unsigned int)node_off, (int)cur_key, | |
502 | (int)keylen); | |
1da177e4 LT |
503 | |
504 | if (bufsize < keylen + 1) { | |
dac52fc1 FF |
505 | befs_error(sb, "%s keybuf too small (%zu) " |
506 | "for key of size %d", __func__, bufsize, keylen); | |
1da177e4 LT |
507 | brelse(this_node->bh); |
508 | goto error_alloc; | |
6cb103b6 | 509 | } |
1da177e4 | 510 | |
6cb103b6 | 511 | strlcpy(keybuf, keystart, keylen + 1); |
1da177e4 LT |
512 | *value = fs64_to_cpu(sb, valarray[cur_key]); |
513 | *keysize = keylen; | |
1da177e4 | 514 | |
dac52fc1 | 515 | befs_debug(sb, "Read [%llu,%d]: Key \"%.*s\", Value %llu", node_off, |
1da177e4 LT |
516 | cur_key, keylen, keybuf, *value); |
517 | ||
518 | brelse(this_node->bh); | |
519 | kfree(this_node); | |
520 | ||
dac52fc1 | 521 | befs_debug(sb, "<--- %s", __func__); |
1da177e4 LT |
522 | |
523 | return BEFS_OK; | |
524 | ||
525 | error_alloc: | |
526 | kfree(this_node); | |
527 | ||
528 | error: | |
529 | *keysize = 0; | |
530 | *value = 0; | |
dac52fc1 | 531 | befs_debug(sb, "<--- %s ERROR", __func__); |
1da177e4 LT |
532 | return BEFS_ERR; |
533 | } | |
534 | ||
535 | /** | |
536 | * befs_btree_seekleaf - Find the first leafnode in the btree | |
537 | * @sb: Filesystem superblock | |
538 | * @ds: Datastream containing btree | |
539 | * @bt_super: Pointer to the superblock of the btree | |
540 | * @this_node: Buffer to return the leafnode in | |
541 | * @node_off: Pointer to offset of current node within datastream. Modified | |
542 | * by the function. | |
543 | * | |
544 | * | |
545 | * Helper function for btree traverse. Moves the current position to the | |
546 | * start of the first leaf node. | |
547 | * | |
548 | * Also checks for an empty tree. If there are no keys, returns BEFS_BT_EMPTY. | |
549 | */ | |
550 | static int | |
22341d8f | 551 | befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds, |
0f2a84f4 HS |
552 | befs_btree_super *bt_super, |
553 | struct befs_btree_node *this_node, | |
1da177e4 LT |
554 | befs_off_t * node_off) |
555 | { | |
556 | ||
dac52fc1 | 557 | befs_debug(sb, "---> %s", __func__); |
1da177e4 LT |
558 | |
559 | if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { | |
dac52fc1 FF |
560 | befs_error(sb, "%s failed to read " |
561 | "node at %llu", __func__, *node_off); | |
1da177e4 LT |
562 | goto error; |
563 | } | |
dac52fc1 | 564 | befs_debug(sb, "Seekleaf to root node %llu", *node_off); |
1da177e4 LT |
565 | |
566 | if (this_node->head.all_key_count == 0 && befs_leafnode(this_node)) { | |
dac52fc1 | 567 | befs_debug(sb, "<--- %s Tree is EMPTY", __func__); |
1da177e4 LT |
568 | return BEFS_BT_EMPTY; |
569 | } | |
570 | ||
571 | while (!befs_leafnode(this_node)) { | |
572 | ||
573 | if (this_node->head.all_key_count == 0) { | |
dac52fc1 FF |
574 | befs_debug(sb, "%s encountered " |
575 | "an empty interior node: %llu. Using Overflow " | |
576 | "node: %llu", __func__, *node_off, | |
1da177e4 LT |
577 | this_node->head.overflow); |
578 | *node_off = this_node->head.overflow; | |
579 | } else { | |
a9721f31 | 580 | fs64 *valarray = befs_bt_valarray(this_node); |
1da177e4 LT |
581 | *node_off = fs64_to_cpu(sb, valarray[0]); |
582 | } | |
583 | if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) { | |
dac52fc1 FF |
584 | befs_error(sb, "%s failed to read " |
585 | "node at %llu", __func__, *node_off); | |
1da177e4 LT |
586 | goto error; |
587 | } | |
588 | ||
dac52fc1 | 589 | befs_debug(sb, "Seekleaf to child node %llu", *node_off); |
1da177e4 | 590 | } |
dac52fc1 | 591 | befs_debug(sb, "Node %llu is a leaf node", *node_off); |
1da177e4 LT |
592 | |
593 | return BEFS_OK; | |
594 | ||
595 | error: | |
dac52fc1 | 596 | befs_debug(sb, "<--- %s ERROR", __func__); |
1da177e4 LT |
597 | return BEFS_ERR; |
598 | } | |
599 | ||
600 | /** | |
601 | * befs_leafnode - Determine if the btree node is a leaf node or an | |
602 | * interior node | |
603 | * @node: Pointer to node structure to test | |
604 | * | |
605 | * Return 1 if leaf, 0 if interior | |
606 | */ | |
607 | static int | |
0f2a84f4 | 608 | befs_leafnode(struct befs_btree_node *node) |
1da177e4 LT |
609 | { |
610 | /* all interior nodes (and only interior nodes) have an overflow node */ | |
611 | if (node->head.overflow == befs_bt_inval) | |
612 | return 1; | |
613 | else | |
614 | return 0; | |
615 | } | |
616 | ||
617 | /** | |
618 | * befs_bt_keylen_index - Finds start of keylen index in a node | |
619 | * @node: Pointer to the node structure to find the keylen index within | |
620 | * | |
621 | * Returns a pointer to the start of the key length index array | |
622 | * of the B+tree node *@node | |
623 | * | |
624 | * "The length of all the keys in the node is added to the size of the | |
625 | * header and then rounded up to a multiple of four to get the beginning | |
626 | * of the key length index" (p.88, practical filesystem design). | |
627 | * | |
628 | * Except that rounding up to 8 works, and rounding up to 4 doesn't. | |
629 | */ | |
a9721f31 | 630 | static fs16 * |
0f2a84f4 | 631 | befs_bt_keylen_index(struct befs_btree_node *node) |
1da177e4 LT |
632 | { |
633 | const int keylen_align = 8; | |
634 | unsigned long int off = | |
635 | (sizeof (befs_btree_nodehead) + node->head.all_key_length); | |
636 | ulong tmp = off % keylen_align; | |
637 | ||
638 | if (tmp) | |
639 | off += keylen_align - tmp; | |
640 | ||
a9721f31 | 641 | return (fs16 *) ((void *) node->od_node + off); |
1da177e4 LT |
642 | } |
643 | ||
644 | /** | |
645 | * befs_bt_valarray - Finds the start of value array in a node | |
646 | * @node: Pointer to the node structure to find the value array within | |
647 | * | |
648 | * Returns a pointer to the start of the value array | |
649 | * of the node pointed to by the node header | |
650 | */ | |
a9721f31 | 651 | static fs64 * |
0f2a84f4 | 652 | befs_bt_valarray(struct befs_btree_node *node) |
1da177e4 LT |
653 | { |
654 | void *keylen_index_start = (void *) befs_bt_keylen_index(node); | |
a9721f31 | 655 | size_t keylen_index_size = node->head.all_key_count * sizeof (fs16); |
1da177e4 | 656 | |
a9721f31 | 657 | return (fs64 *) (keylen_index_start + keylen_index_size); |
1da177e4 LT |
658 | } |
659 | ||
660 | /** | |
661 | * befs_bt_keydata - Finds start of keydata array in a node | |
662 | * @node: Pointer to the node structure to find the keydata array within | |
663 | * | |
664 | * Returns a pointer to the start of the keydata array | |
665 | * of the node pointed to by the node header | |
666 | */ | |
667 | static char * | |
0f2a84f4 | 668 | befs_bt_keydata(struct befs_btree_node *node) |
1da177e4 LT |
669 | { |
670 | return (char *) ((void *) node->od_node + sizeof (befs_btree_nodehead)); | |
671 | } | |
672 | ||
673 | /** | |
674 | * befs_bt_get_key - returns a pointer to the start of a key | |
675 | * @sb: filesystem superblock | |
676 | * @node: node in which to look for the key | |
677 | * @index: the index of the key to get | |
678 | * @keylen: modified to be the length of the key at @index | |
679 | * | |
680 | * Returns a valid pointer into @node on success. | |
681 | * Returns NULL on failure (bad input) and sets *@keylen = 0 | |
682 | */ | |
683 | static char * | |
0f2a84f4 | 684 | befs_bt_get_key(struct super_block *sb, struct befs_btree_node *node, |
1da177e4 LT |
685 | int index, u16 * keylen) |
686 | { | |
687 | int prev_key_end; | |
688 | char *keystart; | |
a9721f31 | 689 | fs16 *keylen_index; |
1da177e4 LT |
690 | |
691 | if (index < 0 || index > node->head.all_key_count) { | |
692 | *keylen = 0; | |
693 | return NULL; | |
694 | } | |
695 | ||
696 | keystart = befs_bt_keydata(node); | |
697 | keylen_index = befs_bt_keylen_index(node); | |
698 | ||
699 | if (index == 0) | |
700 | prev_key_end = 0; | |
701 | else | |
702 | prev_key_end = fs16_to_cpu(sb, keylen_index[index - 1]); | |
703 | ||
704 | *keylen = fs16_to_cpu(sb, keylen_index[index]) - prev_key_end; | |
705 | ||
706 | return keystart + prev_key_end; | |
707 | } | |
708 | ||
709 | /** | |
710 | * befs_compare_strings - compare two strings | |
711 | * @key1: pointer to the first key to be compared | |
712 | * @keylen1: length in bytes of key1 | |
713 | * @key2: pointer to the second key to be compared | |
817e1d90 | 714 | * @keylen2: length in bytes of key2 |
1da177e4 LT |
715 | * |
716 | * Returns 0 if @key1 and @key2 are equal. | |
717 | * Returns >0 if @key1 is greater. | |
718 | * Returns <0 if @key2 is greater.. | |
719 | */ | |
720 | static int | |
721 | befs_compare_strings(const void *key1, int keylen1, | |
722 | const void *key2, int keylen2) | |
723 | { | |
724 | int len = min_t(int, keylen1, keylen2); | |
725 | int result = strncmp(key1, key2, len); | |
726 | if (result == 0) | |
727 | result = keylen1 - keylen2; | |
728 | return result; | |
729 | } | |
730 | ||
731 | /* These will be used for non-string keyed btrees */ | |
732 | #if 0 | |
733 | static int | |
734 | btree_compare_int32(cont void *key1, int keylen1, const void *key2, int keylen2) | |
735 | { | |
736 | return *(int32_t *) key1 - *(int32_t *) key2; | |
737 | } | |
738 | ||
739 | static int | |
740 | btree_compare_uint32(cont void *key1, int keylen1, | |
741 | const void *key2, int keylen2) | |
742 | { | |
743 | if (*(u_int32_t *) key1 == *(u_int32_t *) key2) | |
744 | return 0; | |
745 | else if (*(u_int32_t *) key1 > *(u_int32_t *) key2) | |
746 | return 1; | |
747 | ||
748 | return -1; | |
749 | } | |
750 | static int | |
751 | btree_compare_int64(cont void *key1, int keylen1, const void *key2, int keylen2) | |
752 | { | |
753 | if (*(int64_t *) key1 == *(int64_t *) key2) | |
754 | return 0; | |
755 | else if (*(int64_t *) key1 > *(int64_t *) key2) | |
756 | return 1; | |
757 | ||
758 | return -1; | |
759 | } | |
760 | ||
761 | static int | |
762 | btree_compare_uint64(cont void *key1, int keylen1, | |
763 | const void *key2, int keylen2) | |
764 | { | |
765 | if (*(u_int64_t *) key1 == *(u_int64_t *) key2) | |
766 | return 0; | |
767 | else if (*(u_int64_t *) key1 > *(u_int64_t *) key2) | |
768 | return 1; | |
769 | ||
770 | return -1; | |
771 | } | |
772 | ||
773 | static int | |
774 | btree_compare_float(cont void *key1, int keylen1, const void *key2, int keylen2) | |
775 | { | |
776 | float result = *(float *) key1 - *(float *) key2; | |
777 | if (result == 0.0f) | |
778 | return 0; | |
779 | ||
780 | return (result < 0.0f) ? -1 : 1; | |
781 | } | |
782 | ||
783 | static int | |
784 | btree_compare_double(cont void *key1, int keylen1, | |
785 | const void *key2, int keylen2) | |
786 | { | |
787 | double result = *(double *) key1 - *(double *) key2; | |
788 | if (result == 0.0) | |
789 | return 0; | |
790 | ||
791 | return (result < 0.0) ? -1 : 1; | |
792 | } | |
793 | #endif //0 |