This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_tree.c
2985 lines (2724 loc) · 112 KB
/
block_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <lk/compiler.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"
#include "block_allocator.h"
#include "block_cache.h"
#include "block_tree.h"
#include "crypt.h"
#include "debug.h"
#include "transaction.h"
#if BUILD_STORAGE_TEST
#include <math.h>
#endif
bool print_lookup;
static bool print_ops;
static bool print_node_changes;
static bool print_internal_changes;
static bool print_changes;
static bool print_changes_full_tree;
/**
* struct block_tree_node - On-disk B+ tree node header and payload start
* @iv: initial value used for encrypt/decrypt
* @is_leaf: 0 for internal nodes, 1 for leaf nodes.
* @data: Key, child and data payload, size of entries is tree specific
* so accessor functions are needed to get to specific entries.
*
* On-disk B+ tree node format.
*
* If %is_leaf is 0, the node is an internal B+ tree node and @data contains n
* keys and n + 1 child block-macs. If %is_leaf is 1, the node is a leaf node
* and @data contains n keys and n data entries. For either node type
* n <= block_tree_max_key_count. For root leaf nodes n >= 0, for root internal
* nodes n >= 1, otherwise n >= block_tree_min_key_count. While updating the
* tree, n can temporarily exceed these limits by 1 for a single node. There is
* no room in this struct to store extra keys, so the extra key and child/data
* is stored in the in-memory tree struct.
*
* The value of block_tree_max_key_count and block_tree_min_key_count depends
* on the block, key, child and data sizes for the tree, and may not be the
* same for leaf and internal nodes.
*
* Keys are stored starting at @data, child and data values are stored starting
* at data + block_tree_max_key_count * key-size.
*
* TODO: store n in this struct? Currently a 0 key value us used to determine
* how full a node is, which prevents storing 0 keys in the tree.
*/
struct block_tree_node {
struct iv iv;
uint64_t is_leaf;
uint8_t data[0];
};
/**
* block_tree_node_is_leaf - Check if node is a leaf or an internal node
* @node_ro: Node pointer.
*
* Return: %true if @node_ro is a leaf node, %false if @node_ro is an internal
* node.
*/
static bool block_tree_node_is_leaf(const struct block_tree_node *node_ro)
{
assert(node_ro);
assert(node_ro->is_leaf <= 1);
return node_ro->is_leaf;
}
/**
* block_tree_set_sizes - Configure tree block and entry sizes
* @tree: Tree object.
* @block_size: Block size.
* @key_size: Key size. [1-8].
* @child_size: Child size. [@key_size-24].
* @data_size: Data size. [1-24].
*
* Store tree block and entry sizes and calculate key counts.
*/
static void block_tree_set_sizes(struct block_tree *tree,
size_t block_size,
size_t key_size,
size_t child_size,
size_t data_size)
{
size_t payload_size = block_size - sizeof(struct block_tree_node);
assert(payload_size < block_size);
assert(key_size);
assert(key_size <= sizeof(data_block_t));
assert(child_size >= key_size);
assert(child_size <= sizeof(struct block_mac));
assert(data_size);
assert(data_size <= sizeof(struct block_mac));
tree->key_size = key_size;
tree->child_data_size[0] = child_size;
tree->child_data_size[1] = data_size;
tree->key_count[0] = (payload_size - child_size) / (key_size + child_size);
tree->key_count[1] = (payload_size) / (key_size + data_size); /* TODO: allow next pointer when mac is not needed? */
tree->block_size = block_size;
assert(tree->key_count[0] >= 2);
assert(tree->key_count[1] >= 2);
}
/**
* is_zero - Helper function to check that buffer only contain 0 bytes
* @data: Data pointer.
* @size: Number of bytes to check.
*
* Return: %true if @size is 0 or @data[0..@size - 1] is all 0, %false
* otherwise.
*/
static bool is_zero(const void *data, size_t size)
{
if (!size) {
return true;
}
assert(size >= 1);
return !*(char *)data && !memcmp(data, data + 1, size - 1);
}
/**
* block_tree_max_key_count - Return max key count for leaf or internal nodes
* @tree: Tree object.
* @leaf: %true for leaf node, %false for internal node.
*
* Return: Maximum number of keys that fit in a leaf node or in and internal
* node.
*/
static uint block_tree_max_key_count(const struct block_tree *tree, bool leaf)
{
uint key_count = tree->key_count[leaf];
assert(key_count);
assert(key_count * tree->key_size < tree->block_size);
return key_count;
}
/**
* block_tree_node_max_key_count - Return max key count for specific node
* @tree: Tree object.
* @node_ro: Node pointer.
*
* Return: Maximum number of keys that fit @node_ro.
*/
static uint block_tree_node_max_key_count(const struct block_tree *tree,
const struct block_tree_node *node_ro)
{
return block_tree_max_key_count(tree, block_tree_node_is_leaf(node_ro));
}
/**
* enum block_tree_shift_mode - Child selection for block_tree_node_shift
* @SHIFT_LEAF_OR_LEFT_CHILD: Required value for leaf nodes, selects left
* child for internal nodes.
* @SHIFT_RIGHT_CHILD: Select right child.
* @SHIFT_LEFT_CHILD: Select left child (for src_index).
* @SHIFT_BOTH: Select left child at start and right child at
* end. Only valid when inserting data at the end
* of a node.
*/
enum block_tree_shift_mode {
SHIFT_LEAF_OR_LEFT_CHILD,
SHIFT_RIGHT_CHILD = 1,
SHIFT_LEFT_CHILD = 2,
SHIFT_BOTH = SHIFT_RIGHT_CHILD | SHIFT_LEFT_CHILD,
};
/**
* block_tree_node_shift - Helper function to insert or remove entries in a node
* @tree: Tree object.
* @node_rw: Node pointer.
* @dest_index: Destination index for existing entries to shift.
* @src_index: Source index for existing entries to shift.
* @shift_mode: Specifies which child to shift for internal nodes.
* @new_key: When shifting up (inserting), keys to insert.
* @new_data: When shifting up (inserting), data (or child) to insert.
* @overflow_key: When shifting up (inserting), location to store keys that
* was shifted out of range. Can be %NULL if all those keys are
* 0.
* @overflow_data: When shifting up (inserting), location to store data (or
* child) that was shifted out of range. Can be %NULL if that
* data is all 0.
*/
static void block_tree_node_shift(const struct block_tree *tree,
struct block_tree_node *node_rw,
uint dest_index, uint src_index,
enum block_tree_shift_mode shift_mode,
const void *new_key,
const void *new_data,
void *overflow_key,
void *overflow_data)
{
bool is_leaf = block_tree_node_is_leaf(node_rw);
uint max_count = tree->key_count[is_leaf];
int i;
void *base;
size_t entry_size;
const void *src;
void *dest;
size_t size;
uint clear_index;
assert(max_count);
assert(dest_index <= max_count + !is_leaf + 1);
for (i = 0; i < 2; i++) {
if (i == 0) {
/* key */
base = node_rw->data;
entry_size = tree->key_size;
} else {
/* data/child */
base = node_rw->data + tree->key_size * max_count;
entry_size = tree->child_data_size[is_leaf];
if (!is_leaf) {
/* internal nodes have one more child than keys */
max_count++;
}
if (shift_mode & SHIFT_RIGHT_CHILD) {
assert(!is_leaf);
if (!(shift_mode & SHIFT_LEFT_CHILD) && src_index != ~0U) {
src_index++;
}
dest_index++;
}
}
if (src_index < dest_index) {
/* Inserting, copy entries that will be overwritten to overflow_* */
size = (dest_index - src_index) * entry_size;
if (src_index == max_count) {
src = i == 0 ? new_key : new_data;
} else {
src = base + max_count * entry_size - size;
}
dest = i == 0 ? overflow_key : overflow_data;
if (dest) {
if (print_node_changes) {
printf("%s: copy %p, index %d/%d, to overflow, %p, size %zd, is_zero %d\n",
__func__, src, max_count - (dest_index - src_index),
max_count, dest, size, is_zero(src, size));
}
memcpy(dest, src, size);
} else {
assert(is_zero(src, size));
}
}
if (src_index < max_count) {
/* Inserting or deleting, shift up or down */
src = base + src_index * entry_size;
dest = base + dest_index * entry_size;
size = (max_count - MAX(src_index, dest_index)) * entry_size;
if (print_node_changes) {
printf("%s: move %p, index %d, to %p, index %d, size %zd, is_zero %d\n",
__func__, src, src_index, dest, dest_index, size, is_zero(src, size));
}
memmove(dest, src, size);
if (src_index >= dest_index) {
clear_index = max_count + dest_index - src_index;
}
} else {
clear_index = dest_index;
}
if (src_index < dest_index) {
/* Inserting, copy new entries passed in */
assert(new_key);
assert(new_data);
src = i == 0 ? new_key : new_data;
dest = base + src_index * entry_size;
size = (dest_index - src_index) * entry_size;
if (src_index == max_count) {
/* NOP, data was copied to overflow_* above */
} else {
assert(src);
if (print_node_changes) {
printf("%s: copy new data %p, to %p, index %d, size %zd, is_zero %d\n",
__func__, src, dest, src_index, size, is_zero(src, size));
}
memcpy(dest, src, size);
}
} else {
/* Deleting, clear unused space at end */
assert(dest_index <= max_count);
dest = base + clear_index * entry_size;
size = (max_count - clear_index) * entry_size;
if (print_node_changes) {
printf("%s: clear %p, index %d/%d, size %zd\n",
__func__, dest, clear_index, max_count, size);
}
memset(dest, 0, size);
}
}
}
/**
* block_tree_node_merge_entries - Helper function to merge nodes
* @tree: Tree object.
* @node_rw: Destination node.
* @src_node_ro: Source node.
* @dest_index: Index to insert at in @node_rw.
* @count: Number of entries to copy from start of @src_node_ro.
* @merge_key: For internal nodes, key to insert between nodes.
*/
static void block_tree_node_merge_entries(const struct block_tree *tree,
struct block_tree_node *node_rw,
const struct block_tree_node *src_node_ro,
uint dest_index, uint count,
const void *merge_key)
{
bool is_leaf = block_tree_node_is_leaf(node_rw);
uint max_count = tree->key_count[is_leaf];
void *dest_key;
enum block_tree_shift_mode shift_mode = SHIFT_LEAF_OR_LEFT_CHILD;
if (!is_leaf) {
dest_key = node_rw->data + tree->key_size * dest_index;
assert(is_zero(dest_key, tree->key_size));
memcpy(dest_key, merge_key, tree->key_size);
dest_index++;
shift_mode = SHIFT_BOTH;
}
block_tree_node_shift(tree, node_rw, dest_index + count, dest_index,
shift_mode,
src_node_ro->data,
src_node_ro->data + tree->key_size * max_count,
NULL, NULL);
}
/**
* block_tree_node_shift_down - Helper function to delete entries from node
* @tree: Tree object.
* @node_rw: Node.
* @dest_index: Destination index for existing entries to shift (or
* first entry to remove).
* @src_index: Source index for existing entries to shift (or first entry
* after @dest_index not to remove).
* @shift_mode: Specifies which child to shift for internal nodes.
*/
static void block_tree_node_shift_down(const struct block_tree *tree,
struct block_tree_node *node_rw,
uint dest_index, uint src_index,
enum block_tree_shift_mode shift_mode)
{
assert(dest_index < src_index);
block_tree_node_shift(tree, node_rw, dest_index, src_index, shift_mode,
NULL, NULL, NULL, NULL);
}
/**
* block_tree_node_shift_down - Helper function to delete entries from end of node
* @tree: Tree object.
* @node_rw: Node.
* @start_index: Index of first entry to remove. For internal nodes the
* the right child is removed, so @start_index refers to the
* key index, not the child index.
*/
static void block_tree_node_clear_end(const struct block_tree *tree,
struct block_tree_node *node_rw,
uint start_index)
{
block_tree_node_shift_down(tree, node_rw, start_index, ~0,
block_tree_node_is_leaf(node_rw) ?
SHIFT_LEAF_OR_LEFT_CHILD : SHIFT_RIGHT_CHILD);
}
/**
* block_tree_node_insert - Helper function to insert one entry in a node
* @tree: Tree object.
* @node_rw: Node.
* @index: Index to insert at.
* @shift_mode: Specifies which child to insert for internal nodes.
* @new_key: Key to insert.
* @new_data: Data or child to insert.
* @overflow_key: Location to store key that was shifted out of range. Can be
* %NULL if that key is always 0.
* @overflow_data: Location to store data (or child) that was shifted out of
* range. Can be %NULL if that data is all 0.
*/
static void block_tree_node_insert(const struct block_tree *tree,
struct block_tree_node *node_rw,
uint index,
enum block_tree_shift_mode shift_mode,
const void *new_key,
const void *new_data,
void *overflow_key,
void *overflow_data)
{
block_tree_node_shift(tree, node_rw, index + 1, index, shift_mode,
new_key, new_data, overflow_key, overflow_data);
}
/**
* block_tree_node_get_key - Get key from node or in-memory pending update
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Node.
* @index: Index of key to get.
*
* Return: key at @index, or 0 if there is no key at @index. If @index is
* equal to max key count, check for a matching entry in @tree->inserting.
*/
static data_block_t block_tree_node_get_key(const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro,
uint index)
{
data_block_t key = 0;
const void *keyp;
const size_t key_count = block_tree_node_max_key_count(tree, node_ro);
const size_t key_size = tree->key_size;
assert(node_ro);
if (index < key_count) {
keyp = node_ro->data + index * tree->key_size;
assert(sizeof(key) >= key_size);
memcpy(&key, keyp, key_size);
}
if (!key && node_block == tree->inserting.block) {
assert(index >= key_count);
if (index <= key_count) {
key = tree->inserting.key;
}
}
return key;
}
/**
* block_tree_node_set_key - Set key in node
* @tree: Tree object.
* @node_rw: Node.
* @index: Index of key to set.
* @new_key: Key value to write.
*/
static void block_tree_node_set_key(const struct block_tree *tree,
struct block_tree_node *node_rw,
uint index,
data_block_t new_key)
{
const size_t key_size = tree->key_size;
const size_t key_count = block_tree_node_max_key_count(tree, node_rw);
assert(node_rw);
assert(index < key_count);
assert(key_size <= sizeof(new_key));
memcpy(node_rw->data + index * tree->key_size, &new_key, key_size); /* TODO: support big-endian host */
}
/**
* block_tree_node_get_child_data - Get pointer to child or data
* @tree: Tree object.
* @node_ro: Node.
* @index: Index of child or data entry to get.
*
* Return: pointer to child or data entry at index @index in @node_ro.
*/
static const void *block_tree_node_get_child_data(const struct block_tree *tree,
const struct block_tree_node *node_ro,
uint index)
{
bool is_leaf = block_tree_node_is_leaf(node_ro);
const size_t max_key_count = tree->key_count[is_leaf];
const size_t child_data_size = tree->child_data_size[is_leaf];
const void *child_data;
assert(index < max_key_count + !is_leaf);
child_data = node_ro->data + tree->key_size * max_key_count + child_data_size * index;
assert(child_data > (void *)node_ro->data);
assert(child_data < (void *)node_ro + tree->block_size);
return child_data;
}
/**
* block_tree_node_get_child_data_rw - Get pointer to child or data
* @tree: Tree object.
* @node_rw: Node.
* @index: Index of child or data entry to get.
*
* Return: pointer to child or data entry at index @index in @node_rw.
*/
static void *block_tree_node_get_child_data_rw(const struct block_tree *tree,
struct block_tree_node *node_rw,
int index)
{
return (void *)block_tree_node_get_child_data(tree, node_rw, index);
}
/**
* block_tree_node_get_child - Get child from node or in-memory pending update
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Internal node.
* @index: Index of child to get.
*
* Return: child at @index, or 0 if there is no child at @index. If @index is
* equal to max child count, check for a matching entry in @tree->inserting.
*/
static const struct block_mac *block_tree_node_get_child(const struct transaction *tr,
const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro,
uint index)
{
const struct block_mac *child = NULL;
const size_t key_count = block_tree_node_max_key_count(tree, node_ro);
assert(!block_tree_node_is_leaf(node_ro));
if (index <= key_count) {
child = block_tree_node_get_child_data(tree, node_ro, index);
if (!block_mac_to_block(tr, child)) {
child = NULL;
}
}
if (!child && node_block == tree->inserting.block) {
assert(index > key_count);
if (index <= key_count + 1) {
child = &tree->inserting.child;
}
}
return child;
}
/**
* block_tree_node_get_data - Get data from node or in-memory pending update
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Leaf node.
* @index: Index of data to get.
*
* Return: data at @index, or 0 if there is no data at @index. If @index is
* equal to max key count, check for a matching entry in @tree->inserting.
*/
static struct block_mac block_tree_node_get_data(const struct transaction *tr,
const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro,
uint index)
{
struct block_mac block_mac_ret = BLOCK_MAC_INITIAL_VALUE(block_mac_ret);
const struct block_mac *datap = NULL;
const size_t max_key_count = block_tree_node_max_key_count(tree, node_ro);
assert(block_tree_node_is_leaf(node_ro));
if (index < max_key_count) {
datap = block_tree_node_get_child_data(tree, node_ro, index);
if (!block_mac_to_block(tr, datap)) {
datap = NULL;
}
}
if (!datap && node_block == tree->inserting.block) {
assert(index >= max_key_count);
if (index <= max_key_count) {
datap = &tree->inserting.data;
}
}
if (datap) {
block_mac_copy(tr, &block_mac_ret, datap);
}
return block_mac_ret;
}
#define block_tree_node_for_each_child(tr, tree, block, node_ro, child, i) \
for (i = 0; (child = block_tree_node_get_child(tr, tree, block, node_ro, i)); i++)
/**
* block_tree_node_print_internal - Print internal node
* @tr: Transaction object.
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Node.
*/
static void block_tree_node_print_internal(const struct transaction *tr,
const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro)
{
uint i;
const struct block_mac *child;
const size_t key_count = block_tree_node_max_key_count(tree, node_ro);
assert(!block_tree_node_is_leaf(node_ro));
for (i = 0; i <= key_count + 1; i++) {
child = block_tree_node_get_child(tr, tree, node_block, node_ro, i);
if (child) {
printf(" %lld", block_mac_to_block(tr, child));
} else if (i < key_count + 1) {
printf(" .");
}
if (block_tree_node_get_key(tree, node_block, node_ro, i)) {
if (i == key_count) {
printf(" inserting");
}
printf(" [%lld-]", block_tree_node_get_key(tree, node_block, node_ro, i));
}
}
assert(!block_tree_node_get_child(tr, tree, node_block, node_ro, i));
printf("\n");
}
/**
* block_tree_node_print_leaf - Print leaf node
* @tr: Transaction object.
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Node.
*/
static void block_tree_node_print_leaf(const struct transaction *tr,
const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro)
{
uint i;
data_block_t key;
struct block_mac data;
const size_t key_count = block_tree_node_max_key_count(tree, node_ro);
assert(block_tree_node_is_leaf(node_ro));
for (i = 0; i <= key_count; i++) {
key = block_tree_node_get_key(tree, node_block, node_ro, i);
data = block_tree_node_get_data(tr, tree, node_block, node_ro, i);
if (key || block_mac_valid(tr, &data)) {
if (i == key_count) {
printf(" inserting");
}
printf(" [%lld: %lld]", key, block_mac_to_block(tr, &data));
} else if (i < key_count){
printf(" .");
}
}
printf("\n");
}
/**
* block_tree_node_print - Print node
* @tr: Transaction object.
* @tree: Tree object.
* @node_block: Block number where @node_ro data is stored.
* @node_ro: Node.
*/
static void block_tree_node_print(const struct transaction *tr,
const struct block_tree *tree,
data_block_t node_block,
const struct block_tree_node *node_ro)
{
printf(" %3lld:", node_block);
if (node_ro->is_leaf == true) {
block_tree_node_print_leaf(tr, tree, node_block, node_ro);
} else if (!node_ro->is_leaf) {
block_tree_node_print_internal(tr, tree, node_block, node_ro);
} else {
printf(" bad node header %llx\n", (long long)node_ro->is_leaf);
}
}
/**
* block_tree_print_sub_tree - Print tree or a branch of a tree
* @tr: Transaction object.
* @tree: Tree object.
* @block_mac: Root of tree or branch to print.
*/
static void block_tree_print_sub_tree(struct transaction *tr,
const struct block_tree *tree,
const struct block_mac *block_mac)
{
int i;
const struct block_tree_node *node_ro;
obj_ref_t node_ref = OBJ_REF_INITIAL_VALUE(node_ref);
const struct block_mac *child;
if (!block_mac || !block_mac_valid(tr, block_mac)) {
printf("empty\n");
return;
}
node_ro = block_get(tr, block_mac, NULL, &node_ref);
if (!node_ro) {
printf(" %3lld: unreadable\n", block_mac_to_block(tr, block_mac));
return;
}
block_tree_node_print(tr, tree, block_mac_to_block(tr, block_mac), node_ro);
if (!node_ro->is_leaf) {
block_tree_node_for_each_child(tr, tree, block_mac_to_block(tr, block_mac),
node_ro, child, i) {
block_tree_print_sub_tree(tr, tree, child);
}
}
block_put(node_ro, &node_ref);
}
/**
* block_tree_print - Print tree
* @tr: Transaction object.
* @tree: Tree object.
*/
void block_tree_print(struct transaction *tr, const struct block_tree *tree)
{
block_tree_print_sub_tree(tr, tree, &tree->root);
}
/**
* block_tree_node_check - Check node for errors
* @tr: Transaction object.
* @tree: Tree object.
* @node_ro: Node.
* @node_block: Block number where @node_ro data is stored.
* @min_key: Minimum allowed key value.
* @max_key: Maximum allowed key value.
*
* Return: %false is an error is detected, %true otherwise.
*/
static bool block_tree_node_check(const struct transaction *tr,
const struct block_tree *tree,
const struct block_tree_node *node_ro,
data_block_t node_block,
data_block_t min_key,
data_block_t max_key)
{
uint i;
data_block_t key;
data_block_t prev_key = 0;
int empty_count;
const void *child_data;
size_t key_count = block_tree_node_max_key_count(tree, node_ro);
bool is_leaf;
if (node_ro->is_leaf && node_ro->is_leaf != true) {
printf("%s: bad node header %llx\n", __func__, (long long)node_ro->is_leaf);
goto err;
}
is_leaf = block_tree_node_is_leaf(node_ro);
empty_count = 0;
for (i = 0; i < key_count; i++) {
key = block_tree_node_get_key(tree, node_block, node_ro, i);
if (!key) {
empty_count++;
}
if (empty_count) {
if (key) {
printf("%s: %lld: expected zero key at %d, found %lld\n",
__func__, node_block, i, key);
goto err;
}
child_data = block_tree_node_get_child_data(tree, node_ro, i + !is_leaf);
if (!is_zero(child_data, tree->child_data_size[is_leaf])) {
printf("%s: %lld: expected zero data/right child value at %d\n",
__func__, node_block, i);
goto err;
}
continue;
}
if (key < prev_key || key < min_key || key > max_key) {
printf("%s: %lld: bad key at %d, %lld not in [%lld/%lld-%lld]\n",
__func__, node_block, i, key, min_key, prev_key, max_key);
if (tr->failed && key >= prev_key) {
printf("%s: transaction failed, ignore\n", __func__);
}
else {
goto err;
}
}
prev_key = key;
}
return true;
err:
block_tree_node_print(tr, tree, node_block, node_ro);
return false;
}
/**
* block_tree_check_sub_tree - Check tree or a branch of a tree for errros
* @tr: Transaction object.
* @tree: Tree object.
* @block_mac: Root of tree or branch to check.
* @is_root: %true if @block_mac refers to the root of the tree, %false
* otherwise.
* @min_key: Minimum allowed key value.
* @max_key: Maximum allowed key value.
* @updating: %true if @tree is currently updating and nodes below
* min-full should be allowed, %false otherwise.
*
* Return: Depth of tree/branch, -1 if an error was detected or -2 if @block_mac
* could not be read.
*
* TODO: Reduce overlap with and move more checks to block_tree_node_check.
*/
static int block_tree_check_sub_tree(struct transaction *tr,
const struct block_tree *tree,
const struct block_mac *block_mac,
bool is_root,
data_block_t min_key,
data_block_t max_key,
bool updating)
{
const struct block_tree_node *node_ro;
uint i;
int last_child = 0;
int empty_count;
int depth;
int sub_tree_depth;
data_block_t child_min_key;
data_block_t child_max_key;
data_block_t key;
int max_empty_count;
size_t key_count;
const void *child_data;
struct block_mac child_block_mac;
obj_ref_t ref = OBJ_REF_INITIAL_VALUE(ref);
bool is_leaf;
if (!block_mac || !block_mac_to_block(tr, block_mac))
return 0;
depth = 1;
if (block_mac_to_block(tr, block_mac) >= tr->fs->dev->block_count) {
printf("%s: %3lld: invalid\n",
__func__, block_mac_to_block(tr, block_mac));
return -1;
}
node_ro = block_get_no_tr_fail(tr, block_mac, NULL, &ref);
if (!node_ro) {
if (tr->failed) {
/*
* Failed transactions discards dirty cache entries so parts of the
* tree may now be unreadable.
*/
pr_warn("ignore unreadable block %lld, transaction failed\n",
block_mac_to_block(tr, block_mac));
return -2;
}
pr_warn("%3lld: unreadable\n", block_mac_to_block(tr, block_mac));
return -2;
}
if (!block_tree_node_check(tr, tree, node_ro, block_mac_to_block(tr, block_mac),
min_key, max_key)) {
goto err;
}
if (node_ro->is_leaf && node_ro->is_leaf != true) {
printf("%s: bad node header %llx\n", __func__, (long long)node_ro->is_leaf);
goto err;
}
is_leaf = block_tree_node_is_leaf(node_ro);
key_count = block_tree_node_max_key_count(tree, node_ro);
max_empty_count = is_root ? (key_count /*- 1*/) : /* TODO: don't allow empty root */
((key_count + 1) / 2 + updating);
child_min_key = min_key;
empty_count = 0;
for (i = 0; i < key_count; i++) {
key = block_tree_node_get_key(tree, block_mac_to_block(tr, block_mac),
node_ro, i);
if (!key) {
empty_count++;
}
if (empty_count) {
if (key) {
printf("%s: %lld: expected zero key at %d, found %lld\n",
__func__, block_mac_to_block(tr, block_mac), i, key);
goto err;
}
child_data = block_tree_node_get_child_data(tree, node_ro, i + !is_leaf);
if (!is_zero(child_data, tree->child_data_size[is_leaf])) {
printf("%s: %lld: expected zero data/right child value at %d\n",
__func__, block_mac_to_block(tr, block_mac), i);
goto err;
}
continue;
}
if (i == 0 && min_key && is_leaf && key != min_key) {
printf("%s: %lld: bad key at %d, %lld not start of [%lld-%lld]\n",
__func__, block_mac_to_block(tr, block_mac),
i, key, min_key, max_key);
if (tr->failed) {
printf("%s: transaction failed, ignore\n", __func__);
} else if (!key) {
printf("%s: ignore empty node error\n", __func__); // warn only for now
} else {
goto err;
}
}
min_key = key;
child_max_key = key;
if (!is_leaf) {
child_data = block_tree_node_get_child_data(tree, node_ro, i);
block_mac_copy(tr, &child_block_mac, child_data);
block_put(node_ro, &ref);
sub_tree_depth = block_tree_check_sub_tree(tr, tree,
&child_block_mac, false,
child_min_key,
child_max_key,
updating);
node_ro = block_get_no_tr_fail(tr, block_mac, NULL, &ref);
if (!node_ro) {
pr_warn("%3lld: unreadable\n",
block_mac_to_block(tr, block_mac));
return -2;
}
if (sub_tree_depth == -1) {
goto err;
}
if (sub_tree_depth == -2) {
pr_warn("%lld: unreadable subtree at %d\n",
block_mac_to_block(tr, block_mac), i);
if (depth == 1) {
depth = -2;
}
} else if (depth == 1 || depth == -2) {
depth = sub_tree_depth + 1;
} else if (sub_tree_depth != depth - 1) {
printf("%s: %lld: bad subtree depth at %d, %d != %d\n",
__func__, block_mac_to_block(tr, block_mac),
i, sub_tree_depth, depth - 1);
goto err;
}
}
child_min_key = key;
min_key = key;
last_child = i + 1;
}
child_max_key = max_key;
if (!is_leaf) {
child_data = block_tree_node_get_child_data(tree, node_ro, last_child);
block_mac_copy(tr, &child_block_mac, child_data);
block_put(node_ro, &ref);
sub_tree_depth = block_tree_check_sub_tree(tr, tree,
&child_block_mac, false,
child_min_key, child_max_key,
updating);
node_ro = block_get_no_tr_fail(tr, block_mac, NULL, &ref);
if (!node_ro) {
pr_warn("%3lld: unreadable\n", block_mac_to_block(tr, block_mac));
return -2;
}
if (sub_tree_depth == -1) {
goto err;
}
if (sub_tree_depth == -2) {
pr_warn("%lld: unreadable subtree at %d\n",
block_mac_to_block(tr, block_mac), last_child);
if (depth == 1) {
depth = -2;
}
} else if (depth == 1 || depth == -2) {
depth = sub_tree_depth + 1;
} else if (sub_tree_depth != depth - 1) {
printf("%s: %lld: bad subtree depth at %d, %d != %d\n",
__func__, block_mac_to_block(tr, block_mac),
last_child, sub_tree_depth, depth - 1);
goto err;
}
}
if (empty_count > max_empty_count) {