1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81struct befs_btree_node {
82 befs_host_btree_nodehead head;
83 struct buffer_head *bh;
84 befs_btree_nodehead *od_node;
85};
86
87
88static const befs_off_t befs_bt_inval = 0xffffffffffffffffULL;
89
90
91static int befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds,
92 befs_btree_super * bt_super,
93 struct befs_btree_node *this_node,
94 befs_off_t * node_off);
95
96static int befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds,
97 befs_btree_super * sup);
98
99static int befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds,
100 struct befs_btree_node *node,
101 befs_off_t node_off);
102
103static int befs_leafnode(struct befs_btree_node *node);
104
105static fs16 *befs_bt_keylen_index(struct befs_btree_node *node);
106
107static fs64 *befs_bt_valarray(struct befs_btree_node *node);
108
109static char *befs_bt_keydata(struct befs_btree_node *node);
110
111static int befs_find_key(struct super_block *sb,
112 struct befs_btree_node *node,
113 const char *findkey, befs_off_t * value);
114
115static char *befs_bt_get_key(struct super_block *sb,
116 struct befs_btree_node *node,
117 int index, u16 * keylen);
118
119static int befs_compare_strings(const void *key1, int keylen1,
120 const void *key2, int keylen2);
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136static int
137befs_bt_read_super(struct super_block *sb, const befs_data_stream *ds,
138 befs_btree_super * sup)
139{
140 struct buffer_head *bh;
141 befs_disk_btree_super *od_sup;
142
143 befs_debug(sb, "---> %s", __func__);
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 }
151 od_sup = (befs_disk_btree_super *) bh->b_data;
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
168 befs_debug(sb, "<--- %s", __func__);
169 return BEFS_OK;
170
171 error:
172 befs_debug(sb, "<--- %s ERROR", __func__);
173 return BEFS_ERR;
174}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195static int
196befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds,
197 struct befs_btree_node *node, befs_off_t node_off)
198{
199 uint off = 0;
200
201 befs_debug(sb, "---> %s", __func__);
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) {
208 befs_error(sb, "%s failed to read "
209 "node at %llu", __func__, node_off);
210 befs_debug(sb, "<--- %s ERROR", __func__);
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
227 befs_debug(sb, "<--- %s", __func__);
228 return BEFS_OK;
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249int
250befs_btree_find(struct super_block *sb, const befs_data_stream *ds,
251 const char *key, befs_off_t * value)
252{
253 struct befs_btree_node *this_node;
254 befs_btree_super bt_super;
255 befs_off_t node_off;
256 int res;
257
258 befs_debug(sb, "---> %s Key: %s", __func__, key);
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
266 this_node = kmalloc(sizeof(struct befs_btree_node),
267 GFP_NOFS);
268 if (!this_node) {
269 befs_error(sb, "befs_btree_find() failed to allocate %zu "
270 "bytes of memory", sizeof(struct befs_btree_node));
271 goto error;
272 }
273
274 this_node->bh = NULL;
275
276
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 "
280 "node at %llu", node_off);
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
289 if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) {
290 befs_error(sb, "befs_btree_find() failed to read "
291 "node at %llu", node_off);
292 goto error_alloc;
293 }
294 }
295
296
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) {
304 befs_debug(sb, "<--- %s Key %s not found", __func__, key);
305 *value = 0;
306 return BEFS_BT_NOT_FOUND;
307 }
308 befs_debug(sb, "<--- %s Found key %s, value %llu", __func__,
309 key, *value);
310 return BEFS_OK;
311
312 error_alloc:
313 kfree(this_node);
314 error:
315 *value = 0;
316 befs_debug(sb, "<--- %s ERROR", __func__);
317 return BEFS_ERR;
318}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338static int
339befs_find_key(struct super_block *sb, struct befs_btree_node *node,
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;
347 fs64 *valarray;
348
349 befs_debug(sb, "---> %s %s", __func__, findkey);
350
351 *value = 0;
352
353 findkey_len = strlen(findkey);
354
355
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) {
361 befs_debug(sb, "<--- %s %s not found", __func__, findkey);
362 return BEFS_BT_NOT_FOUND;
363 }
364
365 valarray = befs_bt_valarray(node);
366
367
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) {
379 befs_debug(sb, "<--- %s found %s at %d",
380 __func__, thiskey, mid);
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]);
394 befs_debug(sb, "<--- %s found %s at %d", __func__, thiskey, mid);
395 return BEFS_BT_PARMATCH;
396}
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418int
419befs_btree_read(struct super_block *sb, const befs_data_stream *ds,
420 loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize,
421 befs_off_t * value)
422{
423 struct befs_btree_node *this_node;
424 befs_btree_super bt_super;
425 befs_off_t node_off = 0;
426 int cur_key;
427 fs64 *valarray;
428 char *keystart;
429 u16 keylen;
430 int res;
431
432 uint key_sum = 0;
433
434 befs_debug(sb, "---> %s", __func__);
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
442 this_node = kmalloc(sizeof(struct befs_btree_node), GFP_NOFS);
443 if (this_node == NULL) {
444 befs_error(sb, "befs_btree_read() failed to allocate %zu "
445 "bytes of memory", sizeof(struct befs_btree_node));
446 goto error;
447 }
448
449 node_off = bt_super.root_node_ptr;
450 this_node->bh = NULL;
451
452
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;
459 befs_debug(sb, "<--- %s Tree is EMPTY", __func__);
460 return BEFS_BT_EMPTY;
461 } else if (res == BEFS_ERR) {
462 goto error_alloc;
463 }
464
465
466
467 while (key_sum + this_node->head.all_key_count <= key_no) {
468
469
470 if (this_node->head.right == befs_bt_inval) {
471 *keysize = 0;
472 *value = 0;
473 befs_debug(sb,
474 "<--- %s END of keys at %llu", __func__,
475 (unsigned long long)
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) {
486 befs_error(sb, "%s failed to read node at %llu",
487 __func__, (unsigned long long)node_off);
488 goto error_alloc;
489 }
490 }
491
492
493 cur_key = key_no - key_sum;
494
495
496 valarray = befs_bt_valarray(this_node);
497
498 keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen);
499
500 befs_debug(sb, "Read [%llu,%d]: keysize %d",
501 (long long unsigned int)node_off, (int)cur_key,
502 (int)keylen);
503
504 if (bufsize < keylen + 1) {
505 befs_error(sb, "%s keybuf too small (%zu) "
506 "for key of size %d", __func__, bufsize, keylen);
507 brelse(this_node->bh);
508 goto error_alloc;
509 }
510
511 strlcpy(keybuf, keystart, keylen + 1);
512 *value = fs64_to_cpu(sb, valarray[cur_key]);
513 *keysize = keylen;
514
515 befs_debug(sb, "Read [%llu,%d]: Key \"%.*s\", Value %llu", node_off,
516 cur_key, keylen, keybuf, *value);
517
518 brelse(this_node->bh);
519 kfree(this_node);
520
521 befs_debug(sb, "<--- %s", __func__);
522
523 return BEFS_OK;
524
525 error_alloc:
526 kfree(this_node);
527
528 error:
529 *keysize = 0;
530 *value = 0;
531 befs_debug(sb, "<--- %s ERROR", __func__);
532 return BEFS_ERR;
533}
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550static int
551befs_btree_seekleaf(struct super_block *sb, const befs_data_stream *ds,
552 befs_btree_super *bt_super,
553 struct befs_btree_node *this_node,
554 befs_off_t * node_off)
555{
556
557 befs_debug(sb, "---> %s", __func__);
558
559 if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) {
560 befs_error(sb, "%s failed to read "
561 "node at %llu", __func__, *node_off);
562 goto error;
563 }
564 befs_debug(sb, "Seekleaf to root node %llu", *node_off);
565
566 if (this_node->head.all_key_count == 0 && befs_leafnode(this_node)) {
567 befs_debug(sb, "<--- %s Tree is EMPTY", __func__);
568 return BEFS_BT_EMPTY;
569 }
570
571 while (!befs_leafnode(this_node)) {
572
573 if (this_node->head.all_key_count == 0) {
574 befs_debug(sb, "%s encountered "
575 "an empty interior node: %llu. Using Overflow "
576 "node: %llu", __func__, *node_off,
577 this_node->head.overflow);
578 *node_off = this_node->head.overflow;
579 } else {
580 fs64 *valarray = befs_bt_valarray(this_node);
581 *node_off = fs64_to_cpu(sb, valarray[0]);
582 }
583 if (befs_bt_read_node(sb, ds, this_node, *node_off) != BEFS_OK) {
584 befs_error(sb, "%s failed to read "
585 "node at %llu", __func__, *node_off);
586 goto error;
587 }
588
589 befs_debug(sb, "Seekleaf to child node %llu", *node_off);
590 }
591 befs_debug(sb, "Node %llu is a leaf node", *node_off);
592
593 return BEFS_OK;
594
595 error:
596 befs_debug(sb, "<--- %s ERROR", __func__);
597 return BEFS_ERR;
598}
599
600
601
602
603
604
605
606
607static int
608befs_leafnode(struct befs_btree_node *node)
609{
610
611 if (node->head.overflow == befs_bt_inval)
612 return 1;
613 else
614 return 0;
615}
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630static fs16 *
631befs_bt_keylen_index(struct befs_btree_node *node)
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
641 return (fs16 *) ((void *) node->od_node + off);
642}
643
644
645
646
647
648
649
650
651static fs64 *
652befs_bt_valarray(struct befs_btree_node *node)
653{
654 void *keylen_index_start = (void *) befs_bt_keylen_index(node);
655 size_t keylen_index_size = node->head.all_key_count * sizeof (fs16);
656
657 return (fs64 *) (keylen_index_start + keylen_index_size);
658}
659
660
661
662
663
664
665
666
667static char *
668befs_bt_keydata(struct befs_btree_node *node)
669{
670 return (char *) ((void *) node->od_node + sizeof (befs_btree_nodehead));
671}
672
673
674
675
676
677
678
679
680
681
682
683static char *
684befs_bt_get_key(struct super_block *sb, struct befs_btree_node *node,
685 int index, u16 * keylen)
686{
687 int prev_key_end;
688 char *keystart;
689 fs16 *keylen_index;
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
711
712
713
714
715
716
717
718
719
720static int
721befs_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
732#if 0
733static int
734btree_compare_int32(cont void *key1, int keylen1, const void *key2, int keylen2)
735{
736 return *(int32_t *) key1 - *(int32_t *) key2;
737}
738
739static int
740btree_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}
750static int
751btree_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
761static int
762btree_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
773static int
774btree_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
783static int
784btree_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
794