2 * linux/fs/befs/btree.c
4 * Copyright (C) 2001-2002 Will Dyson <will_dyson@pobox.com>
6 * Licensed under the GNU GPL. See the file COPYING for details.
8 * 2002-02-05: Sergey S. Kostyliov added binary search within
13 * Dominic Giampaolo, author of "Practical File System
14 * Design with the Be File System", for such a helpful book.
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.
20 * Makoto Kato, author of the original BeFS for linux filesystem
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
28 #include <linux/buffer_head.h>
32 #include "datastream.h"
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
40 /* Befs B+tree structure:
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
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.
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.
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.
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).
79 * In memory structure of each btree node
82 befs_host_btree_nodehead head
; /* head of node converted to cpu byteorder */
83 struct buffer_head
*bh
;
84 befs_btree_nodehead
*od_node
; /* on disk node */
88 static const befs_off_t befs_bt_inval
= 0xffffffffffffffffULL
;
91 static int befs_btree_seekleaf(struct super_block
*sb
, befs_data_stream
* ds
,
92 befs_btree_super
* bt_super
,
93 befs_btree_node
* this_node
,
94 befs_off_t
* node_off
);
96 static int befs_bt_read_super(struct super_block
*sb
, befs_data_stream
* ds
,
97 befs_btree_super
* sup
);
99 static int befs_bt_read_node(struct super_block
*sb
, befs_data_stream
* ds
,
100 befs_btree_node
* node
, befs_off_t node_off
);
102 static int befs_leafnode(befs_btree_node
* node
);
104 static fs16
*befs_bt_keylen_index(befs_btree_node
* node
);
106 static fs64
*befs_bt_valarray(befs_btree_node
* node
);
108 static char *befs_bt_keydata(befs_btree_node
* node
);
110 static int befs_find_key(struct super_block
*sb
, befs_btree_node
* node
,
111 const char *findkey
, befs_off_t
* value
);
113 static char *befs_bt_get_key(struct super_block
*sb
, befs_btree_node
* node
,
114 int index
, u16
* keylen
);
116 static int befs_compare_strings(const void *key1
, int keylen1
,
117 const void *key2
, int keylen2
);
120 * befs_bt_read_super - read in btree superblock convert to cpu byteorder
121 * @sb: Filesystem superblock
122 * @ds: Datastream to read from
123 * @sup: Buffer in which to place the btree superblock
125 * Calls befs_read_datastream to read in the btree superblock and
126 * makes sure it is in cpu byteorder, byteswapping if necessary.
128 * On success, returns BEFS_OK and *@sup contains the btree superblock,
131 * On failure, BEFS_ERR is returned.
134 befs_bt_read_super(struct super_block
*sb
, befs_data_stream
* ds
,
135 befs_btree_super
* sup
)
137 struct buffer_head
*bh
= NULL
;
138 befs_disk_btree_super
*od_sup
= NULL
;
140 befs_debug(sb
, "---> befs_btree_read_super()");
142 bh
= befs_read_datastream(sb
, ds
, 0, NULL
);
145 befs_error(sb
, "Couldn't read index header.");
148 od_sup
= (befs_disk_btree_super
*) bh
->b_data
;
149 befs_dump_index_entry(sb
, od_sup
);
151 sup
->magic
= fs32_to_cpu(sb
, od_sup
->magic
);
152 sup
->node_size
= fs32_to_cpu(sb
, od_sup
->node_size
);
153 sup
->max_depth
= fs32_to_cpu(sb
, od_sup
->max_depth
);
154 sup
->data_type
= fs32_to_cpu(sb
, od_sup
->data_type
);
155 sup
->root_node_ptr
= fs64_to_cpu(sb
, od_sup
->root_node_ptr
);
156 sup
->free_node_ptr
= fs64_to_cpu(sb
, od_sup
->free_node_ptr
);
157 sup
->max_size
= fs64_to_cpu(sb
, od_sup
->max_size
);
160 if (sup
->magic
!= BEFS_BTREE_MAGIC
) {
161 befs_error(sb
, "Index header has bad magic.");
165 befs_debug(sb
, "<--- befs_btree_read_super()");
169 befs_debug(sb
, "<--- befs_btree_read_super() ERROR");
174 * befs_bt_read_node - read in btree node and convert to cpu byteorder
175 * @sb: Filesystem superblock
176 * @ds: Datastream to read from
177 * @node: Buffer in which to place the btree node
178 * @node_off: Starting offset (in bytes) of the node in @ds
180 * Calls befs_read_datastream to read in the indicated btree node and
181 * makes sure its header fields are in cpu byteorder, byteswapping if
183 * Note: node->bh must be NULL when this function called first
184 * time. Don't forget brelse(node->bh) after last call.
186 * On success, returns BEFS_OK and *@node contains the btree node that
187 * starts at @node_off, with the node->head fields in cpu byte order.
189 * On failure, BEFS_ERR is returned.
193 befs_bt_read_node(struct super_block
*sb
, befs_data_stream
* ds
,
194 befs_btree_node
* node
, befs_off_t node_off
)
198 befs_debug(sb
, "---> befs_bt_read_node()");
203 node
->bh
= befs_read_datastream(sb
, ds
, node_off
, &off
);
205 befs_error(sb
, "befs_bt_read_node() failed to read "
206 "node at %Lu", node_off
);
207 befs_debug(sb
, "<--- befs_bt_read_node() ERROR");
212 (befs_btree_nodehead
*) ((void *) node
->bh
->b_data
+ off
);
214 befs_dump_index_node(sb
, node
->od_node
);
216 node
->head
.left
= fs64_to_cpu(sb
, node
->od_node
->left
);
217 node
->head
.right
= fs64_to_cpu(sb
, node
->od_node
->right
);
218 node
->head
.overflow
= fs64_to_cpu(sb
, node
->od_node
->overflow
);
219 node
->head
.all_key_count
=
220 fs16_to_cpu(sb
, node
->od_node
->all_key_count
);
221 node
->head
.all_key_length
=
222 fs16_to_cpu(sb
, node
->od_node
->all_key_length
);
224 befs_debug(sb
, "<--- befs_btree_read_node()");
229 * befs_btree_find - Find a key in a befs B+tree
230 * @sb: Filesystem superblock
231 * @ds: Datastream containing btree
232 * @key: Key string to lookup in btree
233 * @value: Value stored with @key
235 * On success, returns BEFS_OK and sets *@value to the value stored
236 * with @key (usually the disk block number of an inode).
238 * On failure, returns BEFS_ERR or BEFS_BT_NOT_FOUND.
241 * Read the superblock and rootnode of the b+tree.
242 * Drill down through the interior nodes using befs_find_key().
243 * Once at the correct leaf node, use befs_find_key() again to get the
244 * actuall value stored with the key.
247 befs_btree_find(struct super_block
*sb
, befs_data_stream
* ds
,
248 const char *key
, befs_off_t
* value
)
250 befs_btree_node
*this_node
= NULL
;
251 befs_btree_super bt_super
;
255 befs_debug(sb
, "---> befs_btree_find() Key: %s", key
);
257 if (befs_bt_read_super(sb
, ds
, &bt_super
) != BEFS_OK
) {
259 "befs_btree_find() failed to read index superblock");
263 this_node
= kmalloc(sizeof (befs_btree_node
),
266 befs_error(sb
, "befs_btree_find() failed to allocate %u "
267 "bytes of memory", sizeof (befs_btree_node
));
271 this_node
->bh
= NULL
;
273 /* read in root node */
274 node_off
= bt_super
.root_node_ptr
;
275 if (befs_bt_read_node(sb
, ds
, this_node
, node_off
) != BEFS_OK
) {
276 befs_error(sb
, "befs_btree_find() failed to read "
277 "node at %Lu", node_off
);
281 while (!befs_leafnode(this_node
)) {
282 res
= befs_find_key(sb
, this_node
, key
, &node_off
);
283 if (res
== BEFS_BT_NOT_FOUND
)
284 node_off
= this_node
->head
.overflow
;
285 /* if no match, go to overflow node */
286 if (befs_bt_read_node(sb
, ds
, this_node
, node_off
) != BEFS_OK
) {
287 befs_error(sb
, "befs_btree_find() failed to read "
288 "node at %Lu", node_off
);
293 /* at the correct leaf node now */
295 res
= befs_find_key(sb
, this_node
, key
, value
);
297 brelse(this_node
->bh
);
300 if (res
!= BEFS_BT_MATCH
) {
301 befs_debug(sb
, "<--- befs_btree_find() Key %s not found", key
);
303 return BEFS_BT_NOT_FOUND
;
305 befs_debug(sb
, "<--- befs_btree_find() Found key %s, value %Lu",
313 befs_debug(sb
, "<--- befs_btree_find() ERROR");
318 * befs_find_key - Search for a key within a node
319 * @sb: Filesystem superblock
320 * @node: Node to find the key within
321 * @key: Keystring to search for
322 * @value: If key is found, the value stored with the key is put here
324 * finds exact match if one exists, and returns BEFS_BT_MATCH
325 * If no exact match, finds first key in node that is greater
326 * (alphabetically) than the search key and returns BEFS_BT_PARMATCH
327 * (for partial match, I guess). Can you think of something better to
330 * If no key was a match or greater than the search key, return
333 * Use binary search instead of a linear.
336 befs_find_key(struct super_block
*sb
, befs_btree_node
* node
,
337 const char *findkey
, befs_off_t
* value
)
339 int first
, last
, mid
;
346 befs_debug(sb
, "---> befs_find_key() %s", findkey
);
350 findkey_len
= strlen(findkey
);
352 /* if node can not contain key, just skeep this node */
353 last
= node
->head
.all_key_count
- 1;
354 thiskey
= befs_bt_get_key(sb
, node
, last
, &keylen
);
356 eq
= befs_compare_strings(thiskey
, keylen
, findkey
, findkey_len
);
358 befs_debug(sb
, "<--- befs_find_key() %s not found", findkey
);
359 return BEFS_BT_NOT_FOUND
;
362 valarray
= befs_bt_valarray(node
);
364 /* simple binary search */
367 while (last
>= first
) {
368 mid
= (last
+ first
) / 2;
369 befs_debug(sb
, "first: %d, last: %d, mid: %d", first
, last
,
371 thiskey
= befs_bt_get_key(sb
, node
, mid
, &keylen
);
372 eq
= befs_compare_strings(thiskey
, keylen
, findkey
,
376 befs_debug(sb
, "<--- befs_find_key() found %s at %d",
379 *value
= fs64_to_cpu(sb
, valarray
[mid
]);
380 return BEFS_BT_MATCH
;
388 *value
= fs64_to_cpu(sb
, valarray
[mid
+ 1]);
390 *value
= fs64_to_cpu(sb
, valarray
[mid
]);
391 befs_debug(sb
, "<--- befs_find_key() found %s at %d", thiskey
, mid
);
392 return BEFS_BT_PARMATCH
;
396 * befs_btree_read - Traverse leafnodes of a btree
397 * @sb: Filesystem superblock
398 * @ds: Datastream containing btree
399 * @key_no: Key number (alphabetical order) of key to read
400 * @bufsize: Size of the buffer to return key in
401 * @keybuf: Pointer to a buffer to put the key in
402 * @keysize: Length of the returned key
403 * @value: Value stored with the returned key
405 * Heres how it works: Key_no is the index of the key/value pair to
406 * return in keybuf/value.
407 * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is
408 * the number of charecters in the key (just a convenience).
411 * Get the first leafnode of the tree. See if the requested key is in that
412 * node. If not, follow the node->right link to the next leafnode. Repeat
413 * until the (key_no)th key is found or the tree is out of keys.
416 befs_btree_read(struct super_block
*sb
, befs_data_stream
* ds
,
417 loff_t key_no
, size_t bufsize
, char *keybuf
, size_t * keysize
,
420 befs_btree_node
*this_node
;
421 befs_btree_super bt_super
;
422 befs_off_t node_off
= 0;
431 befs_debug(sb
, "---> befs_btree_read()");
433 if (befs_bt_read_super(sb
, ds
, &bt_super
) != BEFS_OK
) {
435 "befs_btree_read() failed to read index superblock");
439 if ((this_node
= (befs_btree_node
*)
440 kmalloc(sizeof (befs_btree_node
), GFP_NOFS
)) == NULL
) {
441 befs_error(sb
, "befs_btree_read() failed to allocate %u "
442 "bytes of memory", sizeof (befs_btree_node
));
446 node_off
= bt_super
.root_node_ptr
;
447 this_node
->bh
= NULL
;
449 /* seeks down to first leafnode, reads it into this_node */
450 res
= befs_btree_seekleaf(sb
, ds
, &bt_super
, this_node
, &node_off
);
451 if (res
== BEFS_BT_EMPTY
) {
452 brelse(this_node
->bh
);
456 befs_debug(sb
, "<--- befs_btree_read() Tree is EMPTY");
457 return BEFS_BT_EMPTY
;
458 } else if (res
== BEFS_ERR
) {
462 /* find the leaf node containing the key_no key */
464 while (key_sum
+ this_node
->head
.all_key_count
<= key_no
) {
466 /* no more nodes to look in: key_no is too large */
467 if (this_node
->head
.right
== befs_bt_inval
) {
471 "<--- befs_btree_read() END of keys at %Lu",
472 key_sum
+ this_node
->head
.all_key_count
);
473 brelse(this_node
->bh
);
478 key_sum
+= this_node
->head
.all_key_count
;
479 node_off
= this_node
->head
.right
;
481 if (befs_bt_read_node(sb
, ds
, this_node
, node_off
) != BEFS_OK
) {
482 befs_error(sb
, "befs_btree_read() failed to read "
483 "node at %Lu", node_off
);
488 /* how many keys into this_node is key_no */
489 cur_key
= key_no
- key_sum
;
491 /* get pointers to datastructures within the node body */
492 valarray
= befs_bt_valarray(this_node
);
494 keystart
= befs_bt_get_key(sb
, this_node
, cur_key
, &keylen
);
496 befs_debug(sb
, "Read [%Lu,%d]: keysize %d", node_off
, cur_key
, keylen
);
498 if (bufsize
< keylen
+ 1) {
499 befs_error(sb
, "befs_btree_read() keybuf too small (%u) "
500 "for key of size %d", bufsize
, keylen
);
501 brelse(this_node
->bh
);
505 strncpy(keybuf
, keystart
, keylen
);
506 *value
= fs64_to_cpu(sb
, valarray
[cur_key
]);
508 keybuf
[keylen
] = '\0';
510 befs_debug(sb
, "Read [%Lu,%d]: Key \"%.*s\", Value %Lu", node_off
,
511 cur_key
, keylen
, keybuf
, *value
);
513 brelse(this_node
->bh
);
516 befs_debug(sb
, "<--- befs_btree_read()");
526 befs_debug(sb
, "<--- befs_btree_read() ERROR");
531 * befs_btree_seekleaf - Find the first leafnode in the btree
532 * @sb: Filesystem superblock
533 * @ds: Datastream containing btree
534 * @bt_super: Pointer to the superblock of the btree
535 * @this_node: Buffer to return the leafnode in
536 * @node_off: Pointer to offset of current node within datastream. Modified
540 * Helper function for btree traverse. Moves the current position to the
541 * start of the first leaf node.
543 * Also checks for an empty tree. If there are no keys, returns BEFS_BT_EMPTY.
546 befs_btree_seekleaf(struct super_block
*sb
, befs_data_stream
* ds
,
547 befs_btree_super
* bt_super
, befs_btree_node
* this_node
,
548 befs_off_t
* node_off
)
551 befs_debug(sb
, "---> befs_btree_seekleaf()");
553 if (befs_bt_read_node(sb
, ds
, this_node
, *node_off
) != BEFS_OK
) {
554 befs_error(sb
, "befs_btree_seekleaf() failed to read "
555 "node at %Lu", *node_off
);
558 befs_debug(sb
, "Seekleaf to root node %Lu", *node_off
);
560 if (this_node
->head
.all_key_count
== 0 && befs_leafnode(this_node
)) {
561 befs_debug(sb
, "<--- befs_btree_seekleaf() Tree is EMPTY");
562 return BEFS_BT_EMPTY
;
565 while (!befs_leafnode(this_node
)) {
567 if (this_node
->head
.all_key_count
== 0) {
568 befs_debug(sb
, "befs_btree_seekleaf() encountered "
569 "an empty interior node: %Lu. Using Overflow "
570 "node: %Lu", *node_off
,
571 this_node
->head
.overflow
);
572 *node_off
= this_node
->head
.overflow
;
574 fs64
*valarray
= befs_bt_valarray(this_node
);
575 *node_off
= fs64_to_cpu(sb
, valarray
[0]);
577 if (befs_bt_read_node(sb
, ds
, this_node
, *node_off
) != BEFS_OK
) {
578 befs_error(sb
, "befs_btree_seekleaf() failed to read "
579 "node at %Lu", *node_off
);
583 befs_debug(sb
, "Seekleaf to child node %Lu", *node_off
);
585 befs_debug(sb
, "Node %Lu is a leaf node", *node_off
);
590 befs_debug(sb
, "<--- befs_btree_seekleaf() ERROR");
595 * befs_leafnode - Determine if the btree node is a leaf node or an
597 * @node: Pointer to node structure to test
599 * Return 1 if leaf, 0 if interior
602 befs_leafnode(befs_btree_node
* node
)
604 /* all interior nodes (and only interior nodes) have an overflow node */
605 if (node
->head
.overflow
== befs_bt_inval
)
612 * befs_bt_keylen_index - Finds start of keylen index in a node
613 * @node: Pointer to the node structure to find the keylen index within
615 * Returns a pointer to the start of the key length index array
616 * of the B+tree node *@node
618 * "The length of all the keys in the node is added to the size of the
619 * header and then rounded up to a multiple of four to get the beginning
620 * of the key length index" (p.88, practical filesystem design).
622 * Except that rounding up to 8 works, and rounding up to 4 doesn't.
625 befs_bt_keylen_index(befs_btree_node
* node
)
627 const int keylen_align
= 8;
628 unsigned long int off
=
629 (sizeof (befs_btree_nodehead
) + node
->head
.all_key_length
);
630 ulong tmp
= off
% keylen_align
;
633 off
+= keylen_align
- tmp
;
635 return (fs16
*) ((void *) node
->od_node
+ off
);
639 * befs_bt_valarray - Finds the start of value array in a node
640 * @node: Pointer to the node structure to find the value array within
642 * Returns a pointer to the start of the value array
643 * of the node pointed to by the node header
646 befs_bt_valarray(befs_btree_node
* node
)
648 void *keylen_index_start
= (void *) befs_bt_keylen_index(node
);
649 size_t keylen_index_size
= node
->head
.all_key_count
* sizeof (fs16
);
651 return (fs64
*) (keylen_index_start
+ keylen_index_size
);
655 * befs_bt_keydata - Finds start of keydata array in a node
656 * @node: Pointer to the node structure to find the keydata array within
658 * Returns a pointer to the start of the keydata array
659 * of the node pointed to by the node header
662 befs_bt_keydata(befs_btree_node
* node
)
664 return (char *) ((void *) node
->od_node
+ sizeof (befs_btree_nodehead
));
668 * befs_bt_get_key - returns a pointer to the start of a key
669 * @sb: filesystem superblock
670 * @node: node in which to look for the key
671 * @index: the index of the key to get
672 * @keylen: modified to be the length of the key at @index
674 * Returns a valid pointer into @node on success.
675 * Returns NULL on failure (bad input) and sets *@keylen = 0
678 befs_bt_get_key(struct super_block
*sb
, befs_btree_node
* node
,
679 int index
, u16
* keylen
)
685 if (index
< 0 || index
> node
->head
.all_key_count
) {
690 keystart
= befs_bt_keydata(node
);
691 keylen_index
= befs_bt_keylen_index(node
);
696 prev_key_end
= fs16_to_cpu(sb
, keylen_index
[index
- 1]);
698 *keylen
= fs16_to_cpu(sb
, keylen_index
[index
]) - prev_key_end
;
700 return keystart
+ prev_key_end
;
704 * befs_compare_strings - compare two strings
705 * @key1: pointer to the first key to be compared
706 * @keylen1: length in bytes of key1
707 * @key2: pointer to the second key to be compared
708 * @kelen2: length in bytes of key2
710 * Returns 0 if @key1 and @key2 are equal.
711 * Returns >0 if @key1 is greater.
712 * Returns <0 if @key2 is greater..
715 befs_compare_strings(const void *key1
, int keylen1
,
716 const void *key2
, int keylen2
)
718 int len
= min_t(int, keylen1
, keylen2
);
719 int result
= strncmp(key1
, key2
, len
);
721 result
= keylen1
- keylen2
;
725 /* These will be used for non-string keyed btrees */
728 btree_compare_int32(cont
void *key1
, int keylen1
, const void *key2
, int keylen2
)
730 return *(int32_t *) key1
- *(int32_t *) key2
;
734 btree_compare_uint32(cont
void *key1
, int keylen1
,
735 const void *key2
, int keylen2
)
737 if (*(u_int32_t
*) key1
== *(u_int32_t
*) key2
)
739 else if (*(u_int32_t
*) key1
> *(u_int32_t
*) key2
)
745 btree_compare_int64(cont
void *key1
, int keylen1
, const void *key2
, int keylen2
)
747 if (*(int64_t *) key1
== *(int64_t *) key2
)
749 else if (*(int64_t *) key1
> *(int64_t *) key2
)
756 btree_compare_uint64(cont
void *key1
, int keylen1
,
757 const void *key2
, int keylen2
)
759 if (*(u_int64_t
*) key1
== *(u_int64_t
*) key2
)
761 else if (*(u_int64_t
*) key1
> *(u_int64_t
*) key2
)
768 btree_compare_float(cont
void *key1
, int keylen1
, const void *key2
, int keylen2
)
770 float result
= *(float *) key1
- *(float *) key2
;
774 return (result
< 0.0f
) ? -1 : 1;
778 btree_compare_double(cont
void *key1
, int keylen1
,
779 const void *key2
, int keylen2
)
781 double result
= *(double *) key1
- *(double *) key2
;
785 return (result
< 0.0) ? -1 : 1;