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)
74 uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
76 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
77 dm_tm_with_runs(tm, value_ptr(n, 0), nr_entries, dm_tm_inc_range);
80 vt->inc(vt->context, value_ptr(n, 0), nr_entries);
83 static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
84 uint64_t key, void *value)
85 __dm_written_to_disk(value)
87 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
88 __le64 key_le = cpu_to_le64(key);
90 if (index > nr_entries ||
91 index >= le32_to_cpu(node->header.max_entries)) {
92 DMERR("too many entries in btree node for insert");
93 __dm_unbless_for_disk(value);
97 __dm_bless_for_disk(&key_le);
99 array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
100 array_insert(value_base(node), value_size, nr_entries, index, value);
101 node->header.nr_entries = cpu_to_le32(nr_entries + 1);
106 /*----------------------------------------------------------------*/
109 * We want 3n entries (for some n). This works more nicely for repeated
110 * insert remove loops than (2n + 1).
112 static uint32_t calc_max_entries(size_t value_size, size_t block_size)
115 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
117 block_size -= sizeof(struct node_header);
118 total = block_size / elt_size;
119 n = total / 3; /* rounds down */
124 int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
128 struct btree_node *n;
130 uint32_t max_entries;
132 r = new_block(info, &b);
136 block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
137 max_entries = calc_max_entries(info->value_type.size, block_size);
139 n = dm_block_data(b);
140 memset(n, 0, block_size);
141 n->header.flags = cpu_to_le32(LEAF_NODE);
142 n->header.nr_entries = cpu_to_le32(0);
143 n->header.max_entries = cpu_to_le32(max_entries);
144 n->header.value_size = cpu_to_le32(info->value_type.size);
146 *root = dm_block_location(b);
147 unlock_block(info, b);
151 EXPORT_SYMBOL_GPL(dm_btree_empty);
153 /*----------------------------------------------------------------*/
156 * Deletion uses a recursive algorithm, since we have limited stack space
157 * we explicitly manage our own stack on the heap.
159 #define MAX_SPINE_DEPTH 64
162 struct btree_node *n;
164 unsigned nr_children;
165 unsigned current_child;
169 struct dm_btree_info *info;
170 struct dm_transaction_manager *tm;
172 struct frame spine[MAX_SPINE_DEPTH];
175 static int top_frame(struct del_stack *s, struct frame **f)
178 DMERR("btree deletion stack empty");
182 *f = s->spine + s->top;
187 static int unprocessed_frames(struct del_stack *s)
192 static void prefetch_children(struct del_stack *s, struct frame *f)
195 struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
197 for (i = 0; i < f->nr_children; i++)
198 dm_bm_prefetch(bm, value64(f->n, i));
201 static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
203 return f->level < (info->levels - 1);
206 static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
211 if (s->top >= MAX_SPINE_DEPTH - 1) {
212 DMERR("btree deletion stack out of memory");
216 r = dm_tm_ref(s->tm, b, &ref_count);
222 * This is a shared node, so we can just decrement it's
223 * reference counter and leave the children.
229 struct frame *f = s->spine + ++s->top;
231 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
237 f->n = dm_block_data(f->b);
239 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
240 f->current_child = 0;
242 flags = le32_to_cpu(f->n->header.flags);
243 if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
244 prefetch_children(s, f);
250 static void pop_frame(struct del_stack *s)
252 struct frame *f = s->spine + s->top--;
254 dm_tm_dec(s->tm, dm_block_location(f->b));
255 dm_tm_unlock(s->tm, f->b);
258 static void unlock_all_frames(struct del_stack *s)
262 while (unprocessed_frames(s)) {
263 f = s->spine + s->top--;
264 dm_tm_unlock(s->tm, f->b);
268 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
274 * dm_btree_del() is called via an ioctl, as such should be
275 * considered an FS op. We can't recurse back into the FS, so we
278 s = kmalloc(sizeof(*s), GFP_NOFS);
285 r = push_frame(s, root, 0);
289 while (unprocessed_frames(s)) {
294 r = top_frame(s, &f);
298 if (f->current_child >= f->nr_children) {
303 flags = le32_to_cpu(f->n->header.flags);
304 if (flags & INTERNAL_NODE) {
305 b = value64(f->n, f->current_child);
307 r = push_frame(s, b, f->level);
311 } else if (is_internal_level(info, f)) {
312 b = value64(f->n, f->current_child);
314 r = push_frame(s, b, f->level + 1);
319 if (info->value_type.dec)
320 info->value_type.dec(info->value_type.context,
321 value_ptr(f->n, 0), f->nr_children);
327 /* cleanup all frames of del_stack */
328 unlock_all_frames(s);
334 EXPORT_SYMBOL_GPL(dm_btree_del);
336 /*----------------------------------------------------------------*/
338 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
339 int (*search_fn)(struct btree_node *, uint64_t),
340 uint64_t *result_key, void *v, size_t value_size)
343 uint32_t flags, nr_entries;
346 r = ro_step(s, block);
350 i = search_fn(ro_node(s), key);
352 flags = le32_to_cpu(ro_node(s)->header.flags);
353 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
354 if (i < 0 || i >= nr_entries)
357 if (flags & INTERNAL_NODE)
358 block = value64(ro_node(s), i);
360 } while (!(flags & LEAF_NODE));
362 *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);
497 /*----------------------------------------------------------------*/
500 * Copies entries from one region of a btree node to another. The regions
503 static void copy_entries(struct btree_node *dest, unsigned dest_offset,
504 struct btree_node *src, unsigned src_offset,
507 size_t value_size = le32_to_cpu(dest->header.value_size);
508 memcpy(dest->keys + dest_offset, src->keys + src_offset, count * sizeof(uint64_t));
509 memcpy(value_ptr(dest, dest_offset), value_ptr(src, src_offset), count * value_size);
513 * Moves entries from one region fo a btree node to another. The regions
516 static void move_entries(struct btree_node *dest, unsigned dest_offset,
517 struct btree_node *src, unsigned src_offset,
520 size_t value_size = le32_to_cpu(dest->header.value_size);
521 memmove(dest->keys + dest_offset, src->keys + src_offset, count * sizeof(uint64_t));
522 memmove(value_ptr(dest, dest_offset), value_ptr(src, src_offset), count * value_size);
526 * Erases the first 'count' entries of a btree node, shifting following
527 * entries down into their place.
529 static void shift_down(struct btree_node *n, unsigned count)
531 move_entries(n, 0, n, count, le32_to_cpu(n->header.nr_entries) - count);
535 * Moves entries in a btree node up 'count' places, making space for
536 * new entries at the start of the node.
538 static void shift_up(struct btree_node *n, unsigned count)
540 move_entries(n, count, n, 0, le32_to_cpu(n->header.nr_entries));
544 * Redistributes entries between two btree nodes to make them
545 * have similar numbers of entries.
547 static void redistribute2(struct btree_node *left, struct btree_node *right)
549 unsigned nr_left = le32_to_cpu(left->header.nr_entries);
550 unsigned nr_right = le32_to_cpu(right->header.nr_entries);
551 unsigned total = nr_left + nr_right;
552 unsigned target_left = total / 2;
553 unsigned target_right = total - target_left;
555 if (nr_left < target_left) {
556 unsigned delta = target_left - nr_left;
557 copy_entries(left, nr_left, right, 0, delta);
558 shift_down(right, delta);
559 } else if (nr_left > target_left) {
560 unsigned delta = nr_left - target_left;
562 shift_up(right, delta);
563 copy_entries(right, 0, left, target_left, delta);
566 left->header.nr_entries = cpu_to_le32(target_left);
567 right->header.nr_entries = cpu_to_le32(target_right);
571 * Redistribute entries between three nodes. Assumes the central
574 static void redistribute3(struct btree_node *left, struct btree_node *center,
575 struct btree_node *right)
577 unsigned nr_left = le32_to_cpu(left->header.nr_entries);
578 unsigned nr_center = le32_to_cpu(center->header.nr_entries);
579 unsigned nr_right = le32_to_cpu(right->header.nr_entries);
580 unsigned total, target_left, target_center, target_right;
584 total = nr_left + nr_right;
585 target_left = total / 3;
586 target_center = (total - target_left) / 2;
587 target_right = (total - target_left - target_center);
589 if (nr_left < target_left) {
590 unsigned left_short = target_left - nr_left;
591 copy_entries(left, nr_left, right, 0, left_short);
592 copy_entries(center, 0, right, left_short, target_center);
593 shift_down(right, nr_right - target_right);
595 } else if (nr_left < (target_left + target_center)) {
596 unsigned left_to_center = nr_left - target_left;
597 copy_entries(center, 0, left, target_left, left_to_center);
598 copy_entries(center, left_to_center, right, 0, target_center - left_to_center);
599 shift_down(right, nr_right - target_right);
602 unsigned right_short = target_right - nr_right;
603 shift_up(right, right_short);
604 copy_entries(right, 0, left, nr_left - right_short, right_short);
605 copy_entries(center, 0, left, target_left, nr_left - target_left);
608 left->header.nr_entries = cpu_to_le32(target_left);
609 center->header.nr_entries = cpu_to_le32(target_center);
610 right->header.nr_entries = cpu_to_le32(target_right);
614 * Splits a node by creating a sibling node and shifting half the nodes
615 * contents across. Assumes there is a parent node, and it has room for
637 * +---------+ +-------+
641 * Where A* is a shadow of A.
643 static int split_one_into_two(struct shadow_spine *s, unsigned parent_index,
644 struct dm_btree_value_type *vt, uint64_t key)
647 struct dm_block *left, *right, *parent;
648 struct btree_node *ln, *rn, *pn;
651 left = shadow_current(s);
653 r = new_block(s->info, &right);
657 ln = dm_block_data(left);
658 rn = dm_block_data(right);
660 rn->header.flags = ln->header.flags;
661 rn->header.nr_entries = cpu_to_le32(0);
662 rn->header.max_entries = ln->header.max_entries;
663 rn->header.value_size = ln->header.value_size;
664 redistribute2(ln, rn);
666 /* patch up the parent */
667 parent = shadow_parent(s);
668 pn = dm_block_data(parent);
670 location = cpu_to_le64(dm_block_location(right));
671 __dm_bless_for_disk(&location);
672 r = insert_at(sizeof(__le64), pn, parent_index + 1,
673 le64_to_cpu(rn->keys[0]), &location);
675 unlock_block(s->info, right);
679 /* patch up the spine */
680 if (key < le64_to_cpu(rn->keys[0])) {
681 unlock_block(s->info, right);
684 unlock_block(s->info, left);
692 * We often need to modify a sibling node. This function shadows a particular
693 * child of the given parent node. Making sure to update the parent to point
696 static int shadow_child(struct dm_btree_info *info, struct dm_btree_value_type *vt,
697 struct btree_node *parent, unsigned index,
698 struct dm_block **result)
702 struct btree_node *node;
704 root = value64(parent, index);
706 r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
711 node = dm_block_data(*result);
714 inc_children(info->tm, node, vt);
716 *((__le64 *) value_ptr(parent, index)) =
717 cpu_to_le64(dm_block_location(*result));
723 * Splits two nodes into three. This is more work, but results in fuller
724 * nodes, so saves metadata space.
726 static int split_two_into_three(struct shadow_spine *s, unsigned parent_index,
727 struct dm_btree_value_type *vt, uint64_t key)
730 unsigned middle_index;
731 struct dm_block *left, *middle, *right, *parent;
732 struct btree_node *ln, *rn, *mn, *pn;
735 parent = shadow_parent(s);
736 pn = dm_block_data(parent);
738 if (parent_index == 0) {
740 left = shadow_current(s);
741 r = shadow_child(s->info, vt, pn, parent_index + 1, &right);
745 middle_index = parent_index;
746 right = shadow_current(s);
747 r = shadow_child(s->info, vt, pn, parent_index - 1, &left);
752 r = new_block(s->info, &middle);
756 ln = dm_block_data(left);
757 mn = dm_block_data(middle);
758 rn = dm_block_data(right);
760 mn->header.nr_entries = cpu_to_le32(0);
761 mn->header.flags = ln->header.flags;
762 mn->header.max_entries = ln->header.max_entries;
763 mn->header.value_size = ln->header.value_size;
765 redistribute3(ln, mn, rn);
767 /* patch up the parent */
768 pn->keys[middle_index] = rn->keys[0];
769 location = cpu_to_le64(dm_block_location(middle));
770 __dm_bless_for_disk(&location);
771 r = insert_at(sizeof(__le64), pn, middle_index,
772 le64_to_cpu(mn->keys[0]), &location);
774 if (shadow_current(s) != left)
775 unlock_block(s->info, left);
777 unlock_block(s->info, middle);
779 if (shadow_current(s) != right)
780 unlock_block(s->info, right);
786 /* patch up the spine */
787 if (key < le64_to_cpu(mn->keys[0])) {
788 unlock_block(s->info, middle);
789 unlock_block(s->info, right);
791 } else if (key < le64_to_cpu(rn->keys[0])) {
792 unlock_block(s->info, left);
793 unlock_block(s->info, right);
794 s->nodes[1] = middle;
796 unlock_block(s->info, left);
797 unlock_block(s->info, middle);
804 /*----------------------------------------------------------------*/
807 * Splits a node by creating two new children beneath the given node.
823 * +-------+ +-------+
824 * | B +++ | | C +++ |
825 * +-------+ +-------+
827 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
831 unsigned nr_left, nr_right;
832 struct dm_block *left, *right, *new_parent;
833 struct btree_node *pn, *ln, *rn;
836 new_parent = shadow_current(s);
838 pn = dm_block_data(new_parent);
839 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
840 sizeof(__le64) : s->info->value_type.size;
842 /* create & init the left block */
843 r = new_block(s->info, &left);
847 ln = dm_block_data(left);
848 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
850 ln->header.flags = pn->header.flags;
851 ln->header.nr_entries = cpu_to_le32(nr_left);
852 ln->header.max_entries = pn->header.max_entries;
853 ln->header.value_size = pn->header.value_size;
854 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
855 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
857 /* create & init the right block */
858 r = new_block(s->info, &right);
860 unlock_block(s->info, left);
864 rn = dm_block_data(right);
865 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
867 rn->header.flags = pn->header.flags;
868 rn->header.nr_entries = cpu_to_le32(nr_right);
869 rn->header.max_entries = pn->header.max_entries;
870 rn->header.value_size = pn->header.value_size;
871 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
872 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
875 /* new_parent should just point to l and r now */
876 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
877 pn->header.nr_entries = cpu_to_le32(2);
878 pn->header.max_entries = cpu_to_le32(
879 calc_max_entries(sizeof(__le64),
881 dm_tm_get_bm(s->info->tm))));
882 pn->header.value_size = cpu_to_le32(sizeof(__le64));
884 val = cpu_to_le64(dm_block_location(left));
885 __dm_bless_for_disk(&val);
886 pn->keys[0] = ln->keys[0];
887 memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
889 val = cpu_to_le64(dm_block_location(right));
890 __dm_bless_for_disk(&val);
891 pn->keys[1] = rn->keys[0];
892 memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
894 unlock_block(s->info, left);
895 unlock_block(s->info, right);
899 /*----------------------------------------------------------------*/
902 * Redistributes a node's entries with its left sibling.
904 static int rebalance_left(struct shadow_spine *s, struct dm_btree_value_type *vt,
905 unsigned parent_index, uint64_t key)
908 struct dm_block *sib;
909 struct btree_node *left, *right, *parent = dm_block_data(shadow_parent(s));
911 r = shadow_child(s->info, vt, parent, parent_index - 1, &sib);
915 left = dm_block_data(sib);
916 right = dm_block_data(shadow_current(s));
917 redistribute2(left, right);
918 *key_ptr(parent, parent_index) = right->keys[0];
920 if (key < le64_to_cpu(right->keys[0])) {
921 unlock_block(s->info, s->nodes[1]);
924 unlock_block(s->info, sib);
931 * Redistributes a nodes entries with its right sibling.
933 static int rebalance_right(struct shadow_spine *s, struct dm_btree_value_type *vt,
934 unsigned parent_index, uint64_t key)
937 struct dm_block *sib;
938 struct btree_node *left, *right, *parent = dm_block_data(shadow_parent(s));
940 r = shadow_child(s->info, vt, parent, parent_index + 1, &sib);
944 left = dm_block_data(shadow_current(s));
945 right = dm_block_data(sib);
946 redistribute2(left, right);
947 *key_ptr(parent, parent_index + 1) = right->keys[0];
949 if (key < le64_to_cpu(right->keys[0])) {
950 unlock_block(s->info, sib);
952 unlock_block(s->info, s->nodes[1]);
960 * Returns the number of spare entries in a node.
962 static int get_node_free_space(struct dm_btree_info *info, dm_block_t b, unsigned *space)
966 struct dm_block *block;
967 struct btree_node *node;
969 r = bn_read_lock(info, b, &block);
973 node = dm_block_data(block);
974 nr_entries = le32_to_cpu(node->header.nr_entries);
975 *space = le32_to_cpu(node->header.max_entries) - nr_entries;
977 unlock_block(info, block);
982 * Make space in a node, either by moving some entries to a sibling,
983 * or creating a new sibling node. SPACE_THRESHOLD defines the minimum
984 * number of free entries that must be in the sibling to make the move
985 * worth while. If the siblings are shared (eg, part of a snapshot),
986 * then they are not touched, since this break sharing and so consume
987 * more space than we save.
989 #define SPACE_THRESHOLD 8
990 static int rebalance_or_split(struct shadow_spine *s, struct dm_btree_value_type *vt,
991 unsigned parent_index, uint64_t key)
994 struct btree_node *parent = dm_block_data(shadow_parent(s));
995 unsigned nr_parent = le32_to_cpu(parent->header.nr_entries);
997 int left_shared = 0, right_shared = 0;
999 /* Should we move entries to the left sibling? */
1000 if (parent_index > 0) {
1001 dm_block_t left_b = value64(parent, parent_index - 1);
1002 r = dm_tm_block_is_shared(s->info->tm, left_b, &left_shared);
1007 r = get_node_free_space(s->info, left_b, &free_space);
1011 if (free_space >= SPACE_THRESHOLD)
1012 return rebalance_left(s, vt, parent_index, key);
1016 /* Should we move entries to the right sibling? */
1017 if (parent_index < (nr_parent - 1)) {
1018 dm_block_t right_b = value64(parent, parent_index + 1);
1019 r = dm_tm_block_is_shared(s->info->tm, right_b, &right_shared);
1023 if (!right_shared) {
1024 r = get_node_free_space(s->info, right_b, &free_space);
1028 if (free_space >= SPACE_THRESHOLD)
1029 return rebalance_right(s, vt, parent_index, key);
1034 * We need to split the node, normally we split two nodes
1035 * into three. But when inserting a sequence that is either
1036 * monotonically increasing or decreasing it's better to split
1037 * a single node into two.
1039 if (left_shared || right_shared || (nr_parent <= 2) ||
1040 (parent_index == 0) || (parent_index + 1 == nr_parent)) {
1041 return split_one_into_two(s, parent_index, vt, key);
1043 return split_two_into_three(s, parent_index, vt, key);
1048 * Does the node contain a particular key?
1050 static bool contains_key(struct btree_node *node, uint64_t key)
1052 int i = lower_bound(node, key);
1054 if (i >= 0 && le64_to_cpu(node->keys[i]) == key)
1061 * In general we preemptively make sure there's a free entry in every
1062 * node on the spine when doing an insert. But we can avoid that with
1063 * leaf nodes if we know it's an overwrite.
1065 static bool has_space_for_insert(struct btree_node *node, uint64_t key)
1067 if (node->header.nr_entries == node->header.max_entries) {
1068 if (le32_to_cpu(node->header.flags) & LEAF_NODE) {
1069 /* we don't need space if it's an overwrite */
1070 return contains_key(node, key);
1079 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
1080 struct dm_btree_value_type *vt,
1081 uint64_t key, unsigned *index)
1083 int r, i = *index, top = 1;
1084 struct btree_node *node;
1087 r = shadow_step(s, root, vt);
1091 node = dm_block_data(shadow_current(s));
1094 * We have to patch up the parent node, ugly, but I don't
1095 * see a way to do this automatically as part of the spine
1098 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
1099 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
1101 __dm_bless_for_disk(&location);
1102 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
1103 &location, sizeof(__le64));
1106 node = dm_block_data(shadow_current(s));
1108 if (!has_space_for_insert(node, key)) {
1110 r = btree_split_beneath(s, key);
1112 r = rebalance_or_split(s, vt, i, key);
1117 /* making space can cause the current node to change */
1118 node = dm_block_data(shadow_current(s));
1121 i = lower_bound(node, key);
1123 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
1127 /* change the bounds on the lowest key */
1128 node->keys[0] = cpu_to_le64(key);
1132 root = value64(node, i);
1136 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
1143 static int __btree_get_overwrite_leaf(struct shadow_spine *s, dm_block_t root,
1144 uint64_t key, int *index)
1147 struct btree_node *node;
1151 r = shadow_step(s, root, &s->info->value_type);
1155 node = dm_block_data(shadow_current(s));
1158 * We have to patch up the parent node, ugly, but I don't
1159 * see a way to do this automatically as part of the spine
1162 if (shadow_has_parent(s) && i >= 0) {
1163 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
1165 __dm_bless_for_disk(&location);
1166 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
1167 &location, sizeof(__le64));
1170 node = dm_block_data(shadow_current(s));
1171 i = lower_bound(node, key);
1174 BUG_ON(i >= le32_to_cpu(node->header.nr_entries));
1176 if (le32_to_cpu(node->header.flags) & LEAF_NODE) {
1177 if (key != le64_to_cpu(node->keys[i]))
1182 root = value64(node, i);
1189 int btree_get_overwrite_leaf(struct dm_btree_info *info, dm_block_t root,
1190 uint64_t key, int *index,
1191 dm_block_t *new_root, struct dm_block **leaf)
1194 struct shadow_spine spine;
1196 BUG_ON(info->levels > 1);
1197 init_shadow_spine(&spine, info);
1198 r = __btree_get_overwrite_leaf(&spine, root, key, index);
1200 *new_root = shadow_root(&spine);
1201 *leaf = shadow_current(&spine);
1204 * Decrement the count so exit_shadow_spine() doesn't
1209 exit_shadow_spine(&spine);
1214 static bool need_insert(struct btree_node *node, uint64_t *keys,
1215 unsigned level, unsigned index)
1217 return ((index >= le32_to_cpu(node->header.nr_entries)) ||
1218 (le64_to_cpu(node->keys[index]) != keys[level]));
1221 static int insert(struct dm_btree_info *info, dm_block_t root,
1222 uint64_t *keys, void *value, dm_block_t *new_root,
1224 __dm_written_to_disk(value)
1227 unsigned level, index = -1, last_level = info->levels - 1;
1228 dm_block_t block = root;
1229 struct shadow_spine spine;
1230 struct btree_node *n;
1231 struct dm_btree_value_type le64_type;
1233 init_le64_type(info->tm, &le64_type);
1234 init_shadow_spine(&spine, info);
1236 for (level = 0; level < (info->levels - 1); level++) {
1237 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
1241 n = dm_block_data(shadow_current(&spine));
1243 if (need_insert(n, keys, level, index)) {
1244 dm_block_t new_tree;
1247 r = dm_btree_empty(info, &new_tree);
1251 new_le = cpu_to_le64(new_tree);
1252 __dm_bless_for_disk(&new_le);
1254 r = insert_at(sizeof(uint64_t), n, index,
1255 keys[level], &new_le);
1260 if (level < last_level)
1261 block = value64(n, index);
1264 r = btree_insert_raw(&spine, block, &info->value_type,
1265 keys[level], &index);
1269 n = dm_block_data(shadow_current(&spine));
1271 if (need_insert(n, keys, level, index)) {
1275 r = insert_at(info->value_type.size, n, index,
1276 keys[level], value);
1283 if (info->value_type.dec &&
1284 (!info->value_type.equal ||
1285 !info->value_type.equal(
1286 info->value_type.context,
1287 value_ptr(n, index),
1289 info->value_type.dec(info->value_type.context,
1290 value_ptr(n, index), 1);
1292 memcpy_disk(value_ptr(n, index),
1293 value, info->value_type.size);
1296 *new_root = shadow_root(&spine);
1297 exit_shadow_spine(&spine);
1302 __dm_unbless_for_disk(value);
1304 exit_shadow_spine(&spine);
1308 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
1309 uint64_t *keys, void *value, dm_block_t *new_root)
1310 __dm_written_to_disk(value)
1312 return insert(info, root, keys, value, new_root, NULL);
1314 EXPORT_SYMBOL_GPL(dm_btree_insert);
1316 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
1317 uint64_t *keys, void *value, dm_block_t *new_root,
1319 __dm_written_to_disk(value)
1321 return insert(info, root, keys, value, new_root, inserted);
1323 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
1325 /*----------------------------------------------------------------*/
1327 static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
1328 uint64_t *result_key, dm_block_t *next_block)
1334 r = ro_step(s, block);
1338 flags = le32_to_cpu(ro_node(s)->header.flags);
1339 i = le32_to_cpu(ro_node(s)->header.nr_entries);
1346 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
1348 *result_key = le64_to_cpu(ro_node(s)->keys[0]);
1350 if (next_block || flags & INTERNAL_NODE) {
1352 block = value64(ro_node(s), i);
1354 block = value64(ro_node(s), 0);
1357 } while (flags & INTERNAL_NODE);
1360 *next_block = block;
1364 static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
1365 bool find_highest, uint64_t *result_keys)
1367 int r = 0, count = 0, level;
1368 struct ro_spine spine;
1370 init_ro_spine(&spine, info);
1371 for (level = 0; level < info->levels; level++) {
1372 r = find_key(&spine, root, find_highest, result_keys + level,
1373 level == info->levels - 1 ? NULL : &root);
1374 if (r == -ENODATA) {
1383 exit_ro_spine(&spine);
1385 return r ? r : count;
1388 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
1389 uint64_t *result_keys)
1391 return dm_btree_find_key(info, root, true, result_keys);
1393 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
1395 int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
1396 uint64_t *result_keys)
1398 return dm_btree_find_key(info, root, false, result_keys);
1400 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
1402 /*----------------------------------------------------------------*/
1405 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
1406 * space. Also this only works for single level trees.
1408 static int walk_node(struct dm_btree_info *info, dm_block_t block,
1409 int (*fn)(void *context, uint64_t *keys, void *leaf),
1414 struct dm_block *node;
1415 struct btree_node *n;
1418 r = bn_read_lock(info, block, &node);
1422 n = dm_block_data(node);
1424 nr = le32_to_cpu(n->header.nr_entries);
1425 for (i = 0; i < nr; i++) {
1426 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
1427 r = walk_node(info, value64(n, i), fn, context);
1431 keys = le64_to_cpu(*key_ptr(n, i));
1432 r = fn(context, &keys, value_ptr(n, i));
1439 dm_tm_unlock(info->tm, node);
1443 int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
1444 int (*fn)(void *context, uint64_t *keys, void *leaf),
1447 BUG_ON(info->levels > 1);
1448 return walk_node(info, root, fn, context);
1450 EXPORT_SYMBOL_GPL(dm_btree_walk);
1452 /*----------------------------------------------------------------*/
1454 static void prefetch_values(struct dm_btree_cursor *c)
1458 struct cursor_node *n = c->nodes + c->depth - 1;
1459 struct btree_node *bn = dm_block_data(n->b);
1460 struct dm_block_manager *bm = dm_tm_get_bm(c->info->tm);
1462 BUG_ON(c->info->value_type.size != sizeof(value_le));
1464 nr = le32_to_cpu(bn->header.nr_entries);
1465 for (i = 0; i < nr; i++) {
1466 memcpy(&value_le, value_ptr(bn, i), sizeof(value_le));
1467 dm_bm_prefetch(bm, le64_to_cpu(value_le));
1471 static bool leaf_node(struct dm_btree_cursor *c)
1473 struct cursor_node *n = c->nodes + c->depth - 1;
1474 struct btree_node *bn = dm_block_data(n->b);
1476 return le32_to_cpu(bn->header.flags) & LEAF_NODE;
1479 static int push_node(struct dm_btree_cursor *c, dm_block_t b)
1482 struct cursor_node *n = c->nodes + c->depth;
1484 if (c->depth >= DM_BTREE_CURSOR_MAX_DEPTH - 1) {
1485 DMERR("couldn't push cursor node, stack depth too high");
1489 r = bn_read_lock(c->info, b, &n->b);
1496 if (c->prefetch_leaves || !leaf_node(c))
1502 static void pop_node(struct dm_btree_cursor *c)
1505 unlock_block(c->info, c->nodes[c->depth].b);
1508 static int inc_or_backtrack(struct dm_btree_cursor *c)
1510 struct cursor_node *n;
1511 struct btree_node *bn;
1517 n = c->nodes + c->depth - 1;
1518 bn = dm_block_data(n->b);
1521 if (n->index < le32_to_cpu(bn->header.nr_entries))
1530 static int find_leaf(struct dm_btree_cursor *c)
1533 struct cursor_node *n;
1534 struct btree_node *bn;
1538 n = c->nodes + c->depth - 1;
1539 bn = dm_block_data(n->b);
1541 if (le32_to_cpu(bn->header.flags) & LEAF_NODE)
1544 memcpy(&value_le, value_ptr(bn, n->index), sizeof(value_le));
1545 r = push_node(c, le64_to_cpu(value_le));
1547 DMERR("push_node failed");
1552 if (!r && (le32_to_cpu(bn->header.nr_entries) == 0))
1558 int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
1559 bool prefetch_leaves, struct dm_btree_cursor *c)
1566 c->prefetch_leaves = prefetch_leaves;
1568 r = push_node(c, root);
1572 return find_leaf(c);
1574 EXPORT_SYMBOL_GPL(dm_btree_cursor_begin);
1576 void dm_btree_cursor_end(struct dm_btree_cursor *c)
1581 EXPORT_SYMBOL_GPL(dm_btree_cursor_end);
1583 int dm_btree_cursor_next(struct dm_btree_cursor *c)
1585 int r = inc_or_backtrack(c);
1589 DMERR("find_leaf failed");
1594 EXPORT_SYMBOL_GPL(dm_btree_cursor_next);
1596 int dm_btree_cursor_skip(struct dm_btree_cursor *c, uint32_t count)
1600 while (count-- && !r)
1601 r = dm_btree_cursor_next(c);
1605 EXPORT_SYMBOL_GPL(dm_btree_cursor_skip);
1607 int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le)
1610 struct cursor_node *n = c->nodes + c->depth - 1;
1611 struct btree_node *bn = dm_block_data(n->b);
1613 if (le32_to_cpu(bn->header.flags) & INTERNAL_NODE)
1616 *key = le64_to_cpu(*key_ptr(bn, n->index));
1617 memcpy(value_le, value_ptr(bn, n->index), c->info->value_type.size);
1623 EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value);