11 #include <linux/export.h>
14 #define DM_MSG_PREFIX "btree"
19 static void memcpy_disk(
void *
dest,
const void *
src,
size_t len)
26 static void array_insert(
void *base,
size_t elt_size,
unsigned nr_elts,
27 unsigned index,
void *elt)
32 base + (elt_size *
index),
33 (nr_elts - index) * elt_size);
35 memcpy_disk(base + (elt_size * index), elt, elt_size);
46 int mid = lo + ((
hi - lo) / 2);
58 return want_hi ?
hi : lo;
63 return bsearch(n, key, 0);
73 for (i = 0; i < nr_entries; i++)
76 for (i = 0; i < nr_entries; i++)
87 if (index > nr_entries ||
89 DMERR(
"too many entries in btree node for insert");
112 size_t elt_size =
sizeof(
uint64_t) + value_size;
115 total = block_size / elt_size;
134 max_entries = calc_max_entries(info->
value_type.size, block_size);
154 #define MAX_SPINE_DEPTH 64
172 DMERR(
"btree deletion stack empty");
181 static int unprocessed_frames(
struct del_stack *s)
192 DMERR(
"btree deletion stack out of memory");
225 static void pop_frame(
struct del_stack *s)
244 r = push_frame(s, root, 1);
248 while (unprocessed_frames(s)) {
253 r = top_frame(s, &f);
266 r = push_frame(s, b, f->
level);
273 r = push_frame(s, b, f->
level + 1);
309 i = search_fn(
ro_node(s), key);
313 if (i < 0 || i >= nr_entries)
337 for (level = 0; level < info->
levels; level++) {
341 if (level == last_level) {
346 value_p = &internal_value_le;
350 r = btree_lookup_raw(&spine, root, keys[level],
355 if (rkey != keys[level]) {
403 unsigned parent_index,
uint64_t key)
407 unsigned nr_left, nr_right;
409 struct node *ln, *rn, *
pn;
434 memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
445 memcpy_disk(value_ptr(pn, parent_index),
446 &location,
sizeof(
__le64));
451 r = insert_at(
sizeof(
__le64), pn, parent_index + 1,
492 unsigned nr_left, nr_right;
493 struct dm_block *
left, *
right, *new_parent;
494 struct node *
pn, *ln, *rn;
531 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left *
size);
532 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
539 calc_max_entries(
sizeof(
__le64),
547 memcpy_disk(value_ptr(pn, 0), &val,
sizeof(
__le64));
552 memcpy_disk(value_ptr(pn, 1), &val,
sizeof(
__le64));
558 if (s->
nodes[0] != new_parent) {
560 s->
nodes[0] = new_parent;
598 &location,
sizeof(
__le64));
603 if (node->
header.nr_entries == node->
header.max_entries) {
605 r = btree_split_beneath(s, key);
607 r = btree_split_sibling(s, root, i, key);
643 unsigned level, index = -1, last_level =
info->levels - 1;
650 le64_type.size =
sizeof(
__le64);
651 le64_type.inc =
NULL;
652 le64_type.dec =
NULL;
653 le64_type.equal =
NULL;
657 for (level = 0; level < (
info->levels - 1); level++) {
658 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
677 r = insert_at(
sizeof(
uint64_t), n, index,
678 keys[level], &new_le);
683 if (level < last_level)
687 r = btree_insert_raw(&spine, block, &
info->value_type,
688 keys[level], &index);
700 r = insert_at(
info->value_type.size, n, index,
708 if (
info->value_type.dec &&
709 (!
info->value_type.equal ||
710 !
info->value_type.equal(
711 info->value_type.context,
714 info->value_type.dec(
info->value_type.context,
715 value_ptr(n, index));
717 memcpy_disk(value_ptr(n, index),
771 if (next_block || flags & INTERNAL_NODE)
774 }
while (flags & INTERNAL_NODE);
788 for (level = 0; level < info->
levels; level++) {
789 r = find_highest_key(&spine, root, result_keys + level,
802 return r ? r :
count;