2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
7 #include "dm-btree-internal.h"
8 #include "dm-space-map.h"
9 #include "dm-transaction-manager.h"
11 #include <linux/export.h>
12 #include <linux/device-mapper.h>
14 #define DM_MSG_PREFIX "btree"
16 /*----------------------------------------------------------------
18 *--------------------------------------------------------------*/
19 static void memcpy_disk(void *dest
, const void *src
, size_t len
)
20 __dm_written_to_disk(src
)
22 memcpy(dest
, src
, len
);
23 __dm_unbless_for_disk(src
);
26 static void array_insert(void *base
, size_t elt_size
, unsigned nr_elts
,
27 unsigned index
, void *elt
)
28 __dm_written_to_disk(elt
)
31 memmove(base
+ (elt_size
* (index
+ 1)),
32 base
+ (elt_size
* index
),
33 (nr_elts
- index
) * elt_size
);
35 memcpy_disk(base
+ (elt_size
* index
), elt
, elt_size
);
38 /*----------------------------------------------------------------*/
40 /* makes the assumption that no two keys are the same. */
41 static int bsearch(struct btree_node
*n
, uint64_t key
, int want_hi
)
43 int lo
= -1, hi
= le32_to_cpu(n
->header
.nr_entries
);
46 int mid
= lo
+ ((hi
- lo
) / 2);
47 uint64_t mid_key
= le64_to_cpu(n
->keys
[mid
]);
58 return want_hi
? hi
: lo
;
61 int lower_bound(struct btree_node
*n
, uint64_t key
)
63 return bsearch(n
, key
, 0);
66 static int upper_bound(struct btree_node
*n
, uint64_t key
)
68 return bsearch(n
, key
, 1);
71 void inc_children(struct dm_transaction_manager
*tm
, struct btree_node
*n
,
72 struct dm_btree_value_type
*vt
)
75 uint32_t nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
77 if (le32_to_cpu(n
->header
.flags
) & INTERNAL_NODE
)
78 for (i
= 0; i
< nr_entries
; i
++)
79 dm_tm_inc(tm
, value64(n
, i
));
81 for (i
= 0; i
< nr_entries
; i
++)
82 vt
->inc(vt
->context
, value_ptr(n
, i
));
85 static int insert_at(size_t value_size
, struct btree_node
*node
, unsigned index
,
86 uint64_t key
, void *value
)
87 __dm_written_to_disk(value
)
89 uint32_t nr_entries
= le32_to_cpu(node
->header
.nr_entries
);
90 __le64 key_le
= cpu_to_le64(key
);
92 if (index
> nr_entries
||
93 index
>= le32_to_cpu(node
->header
.max_entries
)) {
94 DMERR("too many entries in btree node for insert");
95 __dm_unbless_for_disk(value
);
99 __dm_bless_for_disk(&key_le
);
101 array_insert(node
->keys
, sizeof(*node
->keys
), nr_entries
, index
, &key_le
);
102 array_insert(value_base(node
), value_size
, nr_entries
, index
, value
);
103 node
->header
.nr_entries
= cpu_to_le32(nr_entries
+ 1);
108 /*----------------------------------------------------------------*/
111 * We want 3n entries (for some n). This works more nicely for repeated
112 * insert remove loops than (2n + 1).
114 static uint32_t calc_max_entries(size_t value_size
, size_t block_size
)
117 size_t elt_size
= sizeof(uint64_t) + value_size
; /* key + value */
119 block_size
-= sizeof(struct node_header
);
120 total
= block_size
/ elt_size
;
121 n
= total
/ 3; /* rounds down */
126 int dm_btree_empty(struct dm_btree_info
*info
, dm_block_t
*root
)
130 struct btree_node
*n
;
132 uint32_t max_entries
;
134 r
= new_block(info
, &b
);
138 block_size
= dm_bm_block_size(dm_tm_get_bm(info
->tm
));
139 max_entries
= calc_max_entries(info
->value_type
.size
, block_size
);
141 n
= dm_block_data(b
);
142 memset(n
, 0, block_size
);
143 n
->header
.flags
= cpu_to_le32(LEAF_NODE
);
144 n
->header
.nr_entries
= cpu_to_le32(0);
145 n
->header
.max_entries
= cpu_to_le32(max_entries
);
146 n
->header
.value_size
= cpu_to_le32(info
->value_type
.size
);
148 *root
= dm_block_location(b
);
149 unlock_block(info
, b
);
153 EXPORT_SYMBOL_GPL(dm_btree_empty
);
155 /*----------------------------------------------------------------*/
158 * Deletion uses a recursive algorithm, since we have limited stack space
159 * we explicitly manage our own stack on the heap.
161 #define MAX_SPINE_DEPTH 64
164 struct btree_node
*n
;
166 unsigned nr_children
;
167 unsigned current_child
;
171 struct dm_btree_info
*info
;
172 struct dm_transaction_manager
*tm
;
174 struct frame spine
[MAX_SPINE_DEPTH
];
177 static int top_frame(struct del_stack
*s
, struct frame
**f
)
180 DMERR("btree deletion stack empty");
184 *f
= s
->spine
+ s
->top
;
189 static int unprocessed_frames(struct del_stack
*s
)
194 static void prefetch_children(struct del_stack
*s
, struct frame
*f
)
197 struct dm_block_manager
*bm
= dm_tm_get_bm(s
->tm
);
199 for (i
= 0; i
< f
->nr_children
; i
++)
200 dm_bm_prefetch(bm
, value64(f
->n
, i
));
203 static bool is_internal_level(struct dm_btree_info
*info
, struct frame
*f
)
205 return f
->level
< (info
->levels
- 1);
208 static int push_frame(struct del_stack
*s
, dm_block_t b
, unsigned level
)
213 if (s
->top
>= MAX_SPINE_DEPTH
- 1) {
214 DMERR("btree deletion stack out of memory");
218 r
= dm_tm_ref(s
->tm
, b
, &ref_count
);
224 * This is a shared node, so we can just decrement it's
225 * reference counter and leave the children.
231 struct frame
*f
= s
->spine
+ ++s
->top
;
233 r
= dm_tm_read_lock(s
->tm
, b
, &btree_node_validator
, &f
->b
);
239 f
->n
= dm_block_data(f
->b
);
241 f
->nr_children
= le32_to_cpu(f
->n
->header
.nr_entries
);
242 f
->current_child
= 0;
244 flags
= le32_to_cpu(f
->n
->header
.flags
);
245 if (flags
& INTERNAL_NODE
|| is_internal_level(s
->info
, f
))
246 prefetch_children(s
, f
);
252 static void pop_frame(struct del_stack
*s
)
254 struct frame
*f
= s
->spine
+ s
->top
--;
256 dm_tm_dec(s
->tm
, dm_block_location(f
->b
));
257 dm_tm_unlock(s
->tm
, f
->b
);
260 static void unlock_all_frames(struct del_stack
*s
)
264 while (unprocessed_frames(s
)) {
265 f
= s
->spine
+ s
->top
--;
266 dm_tm_unlock(s
->tm
, f
->b
);
270 int dm_btree_del(struct dm_btree_info
*info
, dm_block_t root
)
275 s
= kmalloc(sizeof(*s
), GFP_NOIO
);
282 r
= push_frame(s
, root
, 0);
286 while (unprocessed_frames(s
)) {
291 r
= top_frame(s
, &f
);
295 if (f
->current_child
>= f
->nr_children
) {
300 flags
= le32_to_cpu(f
->n
->header
.flags
);
301 if (flags
& INTERNAL_NODE
) {
302 b
= value64(f
->n
, f
->current_child
);
304 r
= push_frame(s
, b
, f
->level
);
308 } else if (is_internal_level(info
, f
)) {
309 b
= value64(f
->n
, f
->current_child
);
311 r
= push_frame(s
, b
, f
->level
+ 1);
316 if (info
->value_type
.dec
) {
319 for (i
= 0; i
< f
->nr_children
; i
++)
320 info
->value_type
.dec(info
->value_type
.context
,
328 /* cleanup all frames of del_stack */
329 unlock_all_frames(s
);
335 EXPORT_SYMBOL_GPL(dm_btree_del
);
337 /*----------------------------------------------------------------*/
339 static int btree_lookup_raw(struct ro_spine
*s
, dm_block_t block
, uint64_t key
,
340 int (*search_fn
)(struct btree_node
*, uint64_t),
341 uint64_t *result_key
, void *v
, size_t value_size
)
344 uint32_t flags
, nr_entries
;
347 r
= ro_step(s
, block
);
351 i
= search_fn(ro_node(s
), key
);
353 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
354 nr_entries
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
355 if (i
< 0 || i
>= nr_entries
)
358 if (flags
& INTERNAL_NODE
)
359 block
= value64(ro_node(s
), i
);
361 } while (!(flags
& LEAF_NODE
));
363 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
364 memcpy(v
, value_ptr(ro_node(s
), i
), value_size
);
369 int dm_btree_lookup(struct dm_btree_info
*info
, dm_block_t root
,
370 uint64_t *keys
, void *value_le
)
372 unsigned level
, last_level
= info
->levels
- 1;
375 __le64 internal_value_le
;
376 struct ro_spine spine
;
378 init_ro_spine(&spine
, info
);
379 for (level
= 0; level
< info
->levels
; level
++) {
383 if (level
== last_level
) {
385 size
= info
->value_type
.size
;
388 value_p
= &internal_value_le
;
389 size
= sizeof(uint64_t);
392 r
= btree_lookup_raw(&spine
, root
, keys
[level
],
397 if (rkey
!= keys
[level
]) {
398 exit_ro_spine(&spine
);
402 exit_ro_spine(&spine
);
406 root
= le64_to_cpu(internal_value_le
);
408 exit_ro_spine(&spine
);
412 EXPORT_SYMBOL_GPL(dm_btree_lookup
);
414 static int dm_btree_lookup_next_single(struct dm_btree_info
*info
, dm_block_t root
,
415 uint64_t key
, uint64_t *rkey
, void *value_le
)
418 uint32_t flags
, nr_entries
;
419 struct dm_block
*node
;
420 struct btree_node
*n
;
422 r
= bn_read_lock(info
, root
, &node
);
426 n
= dm_block_data(node
);
427 flags
= le32_to_cpu(n
->header
.flags
);
428 nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
430 if (flags
& INTERNAL_NODE
) {
431 i
= lower_bound(n
, key
);
434 * avoid early -ENODATA return when all entries are
435 * higher than the search @key.
439 if (i
>= nr_entries
) {
444 r
= dm_btree_lookup_next_single(info
, value64(n
, i
), key
, rkey
, value_le
);
445 if (r
== -ENODATA
&& i
< (nr_entries
- 1)) {
447 r
= dm_btree_lookup_next_single(info
, value64(n
, i
), key
, rkey
, value_le
);
451 i
= upper_bound(n
, key
);
452 if (i
< 0 || i
>= nr_entries
) {
457 *rkey
= le64_to_cpu(n
->keys
[i
]);
458 memcpy(value_le
, value_ptr(n
, i
), info
->value_type
.size
);
461 dm_tm_unlock(info
->tm
, node
);
465 int dm_btree_lookup_next(struct dm_btree_info
*info
, dm_block_t root
,
466 uint64_t *keys
, uint64_t *rkey
, void *value_le
)
470 __le64 internal_value_le
;
471 struct ro_spine spine
;
473 init_ro_spine(&spine
, info
);
474 for (level
= 0; level
< info
->levels
- 1u; level
++) {
475 r
= btree_lookup_raw(&spine
, root
, keys
[level
],
477 &internal_value_le
, sizeof(uint64_t));
481 if (*rkey
!= keys
[level
]) {
486 root
= le64_to_cpu(internal_value_le
);
489 r
= dm_btree_lookup_next_single(info
, root
, keys
[level
], rkey
, value_le
);
491 exit_ro_spine(&spine
);
495 EXPORT_SYMBOL_GPL(dm_btree_lookup_next
);
498 * Splits a node by creating a sibling node and shifting half the nodes
499 * contents across. Assumes there is a parent node, and it has room for
521 * +---------+ +-------+
525 * Where A* is a shadow of A.
527 static int btree_split_sibling(struct shadow_spine
*s
, unsigned parent_index
,
532 unsigned nr_left
, nr_right
;
533 struct dm_block
*left
, *right
, *parent
;
534 struct btree_node
*ln
, *rn
, *pn
;
537 left
= shadow_current(s
);
539 r
= new_block(s
->info
, &right
);
543 ln
= dm_block_data(left
);
544 rn
= dm_block_data(right
);
546 nr_left
= le32_to_cpu(ln
->header
.nr_entries
) / 2;
547 nr_right
= le32_to_cpu(ln
->header
.nr_entries
) - nr_left
;
549 ln
->header
.nr_entries
= cpu_to_le32(nr_left
);
551 rn
->header
.flags
= ln
->header
.flags
;
552 rn
->header
.nr_entries
= cpu_to_le32(nr_right
);
553 rn
->header
.max_entries
= ln
->header
.max_entries
;
554 rn
->header
.value_size
= ln
->header
.value_size
;
555 memcpy(rn
->keys
, ln
->keys
+ nr_left
, nr_right
* sizeof(rn
->keys
[0]));
557 size
= le32_to_cpu(ln
->header
.flags
) & INTERNAL_NODE
?
558 sizeof(uint64_t) : s
->info
->value_type
.size
;
559 memcpy(value_ptr(rn
, 0), value_ptr(ln
, nr_left
),
563 * Patch up the parent
565 parent
= shadow_parent(s
);
567 pn
= dm_block_data(parent
);
568 location
= cpu_to_le64(dm_block_location(left
));
569 __dm_bless_for_disk(&location
);
570 memcpy_disk(value_ptr(pn
, parent_index
),
571 &location
, sizeof(__le64
));
573 location
= cpu_to_le64(dm_block_location(right
));
574 __dm_bless_for_disk(&location
);
576 r
= insert_at(sizeof(__le64
), pn
, parent_index
+ 1,
577 le64_to_cpu(rn
->keys
[0]), &location
);
579 unlock_block(s
->info
, right
);
583 if (key
< le64_to_cpu(rn
->keys
[0])) {
584 unlock_block(s
->info
, right
);
587 unlock_block(s
->info
, left
);
595 * Splits a node by creating two new children beneath the given node.
611 * +-------+ +-------+
612 * | B +++ | | C +++ |
613 * +-------+ +-------+
615 static int btree_split_beneath(struct shadow_spine
*s
, uint64_t key
)
619 unsigned nr_left
, nr_right
;
620 struct dm_block
*left
, *right
, *new_parent
;
621 struct btree_node
*pn
, *ln
, *rn
;
624 new_parent
= shadow_current(s
);
626 r
= new_block(s
->info
, &left
);
630 r
= new_block(s
->info
, &right
);
632 unlock_block(s
->info
, left
);
636 pn
= dm_block_data(new_parent
);
637 ln
= dm_block_data(left
);
638 rn
= dm_block_data(right
);
640 nr_left
= le32_to_cpu(pn
->header
.nr_entries
) / 2;
641 nr_right
= le32_to_cpu(pn
->header
.nr_entries
) - nr_left
;
643 ln
->header
.flags
= pn
->header
.flags
;
644 ln
->header
.nr_entries
= cpu_to_le32(nr_left
);
645 ln
->header
.max_entries
= pn
->header
.max_entries
;
646 ln
->header
.value_size
= pn
->header
.value_size
;
648 rn
->header
.flags
= pn
->header
.flags
;
649 rn
->header
.nr_entries
= cpu_to_le32(nr_right
);
650 rn
->header
.max_entries
= pn
->header
.max_entries
;
651 rn
->header
.value_size
= pn
->header
.value_size
;
653 memcpy(ln
->keys
, pn
->keys
, nr_left
* sizeof(pn
->keys
[0]));
654 memcpy(rn
->keys
, pn
->keys
+ nr_left
, nr_right
* sizeof(pn
->keys
[0]));
656 size
= le32_to_cpu(pn
->header
.flags
) & INTERNAL_NODE
?
657 sizeof(__le64
) : s
->info
->value_type
.size
;
658 memcpy(value_ptr(ln
, 0), value_ptr(pn
, 0), nr_left
* size
);
659 memcpy(value_ptr(rn
, 0), value_ptr(pn
, nr_left
),
662 /* new_parent should just point to l and r now */
663 pn
->header
.flags
= cpu_to_le32(INTERNAL_NODE
);
664 pn
->header
.nr_entries
= cpu_to_le32(2);
665 pn
->header
.max_entries
= cpu_to_le32(
666 calc_max_entries(sizeof(__le64
),
668 dm_tm_get_bm(s
->info
->tm
))));
669 pn
->header
.value_size
= cpu_to_le32(sizeof(__le64
));
671 val
= cpu_to_le64(dm_block_location(left
));
672 __dm_bless_for_disk(&val
);
673 pn
->keys
[0] = ln
->keys
[0];
674 memcpy_disk(value_ptr(pn
, 0), &val
, sizeof(__le64
));
676 val
= cpu_to_le64(dm_block_location(right
));
677 __dm_bless_for_disk(&val
);
678 pn
->keys
[1] = rn
->keys
[0];
679 memcpy_disk(value_ptr(pn
, 1), &val
, sizeof(__le64
));
682 * rejig the spine. This is ugly, since it knows too
683 * much about the spine
685 if (s
->nodes
[0] != new_parent
) {
686 unlock_block(s
->info
, s
->nodes
[0]);
687 s
->nodes
[0] = new_parent
;
689 if (key
< le64_to_cpu(rn
->keys
[0])) {
690 unlock_block(s
->info
, right
);
693 unlock_block(s
->info
, left
);
701 static int btree_insert_raw(struct shadow_spine
*s
, dm_block_t root
,
702 struct dm_btree_value_type
*vt
,
703 uint64_t key
, unsigned *index
)
705 int r
, i
= *index
, top
= 1;
706 struct btree_node
*node
;
709 r
= shadow_step(s
, root
, vt
);
713 node
= dm_block_data(shadow_current(s
));
716 * We have to patch up the parent node, ugly, but I don't
717 * see a way to do this automatically as part of the spine
720 if (shadow_has_parent(s
) && i
>= 0) { /* FIXME: second clause unness. */
721 __le64 location
= cpu_to_le64(dm_block_location(shadow_current(s
)));
723 __dm_bless_for_disk(&location
);
724 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s
)), i
),
725 &location
, sizeof(__le64
));
728 node
= dm_block_data(shadow_current(s
));
730 if (node
->header
.nr_entries
== node
->header
.max_entries
) {
732 r
= btree_split_beneath(s
, key
);
734 r
= btree_split_sibling(s
, i
, key
);
740 node
= dm_block_data(shadow_current(s
));
742 i
= lower_bound(node
, key
);
744 if (le32_to_cpu(node
->header
.flags
) & LEAF_NODE
)
748 /* change the bounds on the lowest key */
749 node
->keys
[0] = cpu_to_le64(key
);
753 root
= value64(node
, i
);
757 if (i
< 0 || le64_to_cpu(node
->keys
[i
]) != key
)
764 static bool need_insert(struct btree_node
*node
, uint64_t *keys
,
765 unsigned level
, unsigned index
)
767 return ((index
>= le32_to_cpu(node
->header
.nr_entries
)) ||
768 (le64_to_cpu(node
->keys
[index
]) != keys
[level
]));
771 static int insert(struct dm_btree_info
*info
, dm_block_t root
,
772 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
774 __dm_written_to_disk(value
)
777 unsigned level
, index
= -1, last_level
= info
->levels
- 1;
778 dm_block_t block
= root
;
779 struct shadow_spine spine
;
780 struct btree_node
*n
;
781 struct dm_btree_value_type le64_type
;
783 init_le64_type(info
->tm
, &le64_type
);
784 init_shadow_spine(&spine
, info
);
786 for (level
= 0; level
< (info
->levels
- 1); level
++) {
787 r
= btree_insert_raw(&spine
, block
, &le64_type
, keys
[level
], &index
);
791 n
= dm_block_data(shadow_current(&spine
));
793 if (need_insert(n
, keys
, level
, index
)) {
797 r
= dm_btree_empty(info
, &new_tree
);
801 new_le
= cpu_to_le64(new_tree
);
802 __dm_bless_for_disk(&new_le
);
804 r
= insert_at(sizeof(uint64_t), n
, index
,
805 keys
[level
], &new_le
);
810 if (level
< last_level
)
811 block
= value64(n
, index
);
814 r
= btree_insert_raw(&spine
, block
, &info
->value_type
,
815 keys
[level
], &index
);
819 n
= dm_block_data(shadow_current(&spine
));
821 if (need_insert(n
, keys
, level
, index
)) {
825 r
= insert_at(info
->value_type
.size
, n
, index
,
833 if (info
->value_type
.dec
&&
834 (!info
->value_type
.equal
||
835 !info
->value_type
.equal(
836 info
->value_type
.context
,
839 info
->value_type
.dec(info
->value_type
.context
,
840 value_ptr(n
, index
));
842 memcpy_disk(value_ptr(n
, index
),
843 value
, info
->value_type
.size
);
846 *new_root
= shadow_root(&spine
);
847 exit_shadow_spine(&spine
);
852 __dm_unbless_for_disk(value
);
854 exit_shadow_spine(&spine
);
858 int dm_btree_insert(struct dm_btree_info
*info
, dm_block_t root
,
859 uint64_t *keys
, void *value
, dm_block_t
*new_root
)
860 __dm_written_to_disk(value
)
862 return insert(info
, root
, keys
, value
, new_root
, NULL
);
864 EXPORT_SYMBOL_GPL(dm_btree_insert
);
866 int dm_btree_insert_notify(struct dm_btree_info
*info
, dm_block_t root
,
867 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
869 __dm_written_to_disk(value
)
871 return insert(info
, root
, keys
, value
, new_root
, inserted
);
873 EXPORT_SYMBOL_GPL(dm_btree_insert_notify
);
875 /*----------------------------------------------------------------*/
877 static int find_key(struct ro_spine
*s
, dm_block_t block
, bool find_highest
,
878 uint64_t *result_key
, dm_block_t
*next_block
)
884 r
= ro_step(s
, block
);
888 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
889 i
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
896 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
898 *result_key
= le64_to_cpu(ro_node(s
)->keys
[0]);
900 if (next_block
|| flags
& INTERNAL_NODE
)
901 block
= value64(ro_node(s
), i
);
903 } while (flags
& INTERNAL_NODE
);
910 static int dm_btree_find_key(struct dm_btree_info
*info
, dm_block_t root
,
911 bool find_highest
, uint64_t *result_keys
)
913 int r
= 0, count
= 0, level
;
914 struct ro_spine spine
;
916 init_ro_spine(&spine
, info
);
917 for (level
= 0; level
< info
->levels
; level
++) {
918 r
= find_key(&spine
, root
, find_highest
, result_keys
+ level
,
919 level
== info
->levels
- 1 ? NULL
: &root
);
929 exit_ro_spine(&spine
);
931 return r
? r
: count
;
934 int dm_btree_find_highest_key(struct dm_btree_info
*info
, dm_block_t root
,
935 uint64_t *result_keys
)
937 return dm_btree_find_key(info
, root
, true, result_keys
);
939 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key
);
941 int dm_btree_find_lowest_key(struct dm_btree_info
*info
, dm_block_t root
,
942 uint64_t *result_keys
)
944 return dm_btree_find_key(info
, root
, false, result_keys
);
946 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key
);
948 /*----------------------------------------------------------------*/
951 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
952 * space. Also this only works for single level trees.
954 static int walk_node(struct dm_btree_info
*info
, dm_block_t block
,
955 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
960 struct dm_block
*node
;
961 struct btree_node
*n
;
964 r
= bn_read_lock(info
, block
, &node
);
968 n
= dm_block_data(node
);
970 nr
= le32_to_cpu(n
->header
.nr_entries
);
971 for (i
= 0; i
< nr
; i
++) {
972 if (le32_to_cpu(n
->header
.flags
) & INTERNAL_NODE
) {
973 r
= walk_node(info
, value64(n
, i
), fn
, context
);
977 keys
= le64_to_cpu(*key_ptr(n
, i
));
978 r
= fn(context
, &keys
, value_ptr(n
, i
));
985 dm_tm_unlock(info
->tm
, node
);
989 int dm_btree_walk(struct dm_btree_info
*info
, dm_block_t root
,
990 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
993 BUG_ON(info
->levels
> 1);
994 return walk_node(info
, root
, fn
, context
);
996 EXPORT_SYMBOL_GPL(dm_btree_walk
);
998 /*----------------------------------------------------------------*/
1000 static void prefetch_values(struct dm_btree_cursor
*c
)
1004 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1005 struct btree_node
*bn
= dm_block_data(n
->b
);
1006 struct dm_block_manager
*bm
= dm_tm_get_bm(c
->info
->tm
);
1008 BUG_ON(c
->info
->value_type
.size
!= sizeof(value_le
));
1010 nr
= le32_to_cpu(bn
->header
.nr_entries
);
1011 for (i
= 0; i
< nr
; i
++) {
1012 memcpy(&value_le
, value_ptr(bn
, i
), sizeof(value_le
));
1013 dm_bm_prefetch(bm
, le64_to_cpu(value_le
));
1017 static bool leaf_node(struct dm_btree_cursor
*c
)
1019 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1020 struct btree_node
*bn
= dm_block_data(n
->b
);
1022 return le32_to_cpu(bn
->header
.flags
) & LEAF_NODE
;
1025 static int push_node(struct dm_btree_cursor
*c
, dm_block_t b
)
1028 struct cursor_node
*n
= c
->nodes
+ c
->depth
;
1030 if (c
->depth
>= DM_BTREE_CURSOR_MAX_DEPTH
- 1) {
1031 DMERR("couldn't push cursor node, stack depth too high");
1035 r
= bn_read_lock(c
->info
, b
, &n
->b
);
1042 if (c
->prefetch_leaves
|| !leaf_node(c
))
1048 static void pop_node(struct dm_btree_cursor
*c
)
1051 unlock_block(c
->info
, c
->nodes
[c
->depth
].b
);
1054 static int inc_or_backtrack(struct dm_btree_cursor
*c
)
1056 struct cursor_node
*n
;
1057 struct btree_node
*bn
;
1063 n
= c
->nodes
+ c
->depth
- 1;
1064 bn
= dm_block_data(n
->b
);
1067 if (n
->index
< le32_to_cpu(bn
->header
.nr_entries
))
1076 static int find_leaf(struct dm_btree_cursor
*c
)
1079 struct cursor_node
*n
;
1080 struct btree_node
*bn
;
1084 n
= c
->nodes
+ c
->depth
- 1;
1085 bn
= dm_block_data(n
->b
);
1087 if (le32_to_cpu(bn
->header
.flags
) & LEAF_NODE
)
1090 memcpy(&value_le
, value_ptr(bn
, n
->index
), sizeof(value_le
));
1091 r
= push_node(c
, le64_to_cpu(value_le
));
1093 DMERR("push_node failed");
1098 if (!r
&& (le32_to_cpu(bn
->header
.nr_entries
) == 0))
1104 int dm_btree_cursor_begin(struct dm_btree_info
*info
, dm_block_t root
,
1105 bool prefetch_leaves
, struct dm_btree_cursor
*c
)
1112 c
->prefetch_leaves
= prefetch_leaves
;
1114 r
= push_node(c
, root
);
1118 return find_leaf(c
);
1120 EXPORT_SYMBOL_GPL(dm_btree_cursor_begin
);
1122 void dm_btree_cursor_end(struct dm_btree_cursor
*c
)
1127 EXPORT_SYMBOL_GPL(dm_btree_cursor_end
);
1129 int dm_btree_cursor_next(struct dm_btree_cursor
*c
)
1131 int r
= inc_or_backtrack(c
);
1135 DMERR("find_leaf failed");
1140 EXPORT_SYMBOL_GPL(dm_btree_cursor_next
);
1142 int dm_btree_cursor_get_value(struct dm_btree_cursor
*c
, uint64_t *key
, void *value_le
)
1145 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1146 struct btree_node
*bn
= dm_block_data(n
->b
);
1148 if (le32_to_cpu(bn
->header
.flags
) & INTERNAL_NODE
)
1151 *key
= le64_to_cpu(*key_ptr(bn
, n
->index
));
1152 memcpy(value_le
, value_ptr(bn
, n
->index
), c
->info
->value_type
.size
);
1158 EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value
);