24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
82 befs_host_btree_nodehead
head;
83 struct buffer_head *
bh;
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,
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,
116 static int befs_compare_strings(
const void *key1,
int keylen1,
117 const void *key2,
int keylen2);
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()");
145 befs_error(sb,
"Couldn't read index header.");
148 od_sup = (befs_disk_btree_super *) bh->b_data;
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);
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");
193 befs_bt_read_node(
struct super_block *sb, befs_data_stream * ds,
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);
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()");
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");
266 befs_error(sb,
"befs_btree_find() failed to allocate %u "
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);
284 node_off = this_node->
head.overflow;
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);
295 res = befs_find_key(sb, this_node, key, value);
297 brelse(this_node->
bh);
301 befs_debug(sb,
"<--- befs_btree_find() Key %s not found", key);
305 befs_debug(sb,
"<--- befs_btree_find() Found key %s, value %Lu",
313 befs_debug(sb,
"<--- befs_btree_find() ERROR");
346 befs_debug(sb,
"---> befs_find_key() %s", findkey);
350 findkey_len =
strlen(findkey);
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);
362 valarray = befs_bt_valarray(node);
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]);
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);
421 befs_btree_super bt_super;
433 if (befs_bt_read_super(sb, ds, &bt_super) !=
BEFS_OK) {
435 "befs_btree_read() failed to read index superblock");
441 befs_error(sb,
"befs_btree_read() failed to allocate %u "
446 node_off = bt_super.root_node_ptr;
450 res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off);
452 brelse(this_node->
bh);
456 befs_debug(sb,
"<--- befs_btree_read() Tree is EMPTY");
464 while (key_sum + this_node->
head.all_key_count <= key_no) {
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);
489 cur_key = key_no - key_sum;
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);
526 befs_debug(sb,
"<--- befs_btree_read() ERROR");
546 befs_btree_seekleaf(
struct super_block *sb, befs_data_stream * ds,
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");
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");
605 if (node->
head.overflow == befs_bt_inval)
627 const int keylen_align = 8;
628 unsigned long int off =
629 (
sizeof (befs_btree_nodehead) + node->
head.all_key_length);
633 off += keylen_align -
tmp;
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);
664 return (
char *) ((
void *) node->
od_node +
sizeof (befs_btree_nodehead));
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;
715 befs_compare_strings(
const void *key1,
int keylen1,
716 const void *key2,
int keylen2)
718 int len =
min_t(
int, keylen1, keylen2);
721 result = keylen1 - keylen2;
728 btree_compare_int32(
cont void *key1,
int keylen1,
const void *key2,
int keylen2)
734 btree_compare_uint32(
cont void *key1,
int keylen1,
735 const void *key2,
int keylen2)
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.0
f) ? -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;