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 void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
67 struct dm_btree_value_type *vt)
70 uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
72 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
73 for (i = 0; i < nr_entries; i++)
74 dm_tm_inc(tm, value64(n, i));
76 for (i = 0; i < nr_entries; i++)
77 vt->inc(vt->context, value_ptr(n, i));
80 static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
81 uint64_t key, void *value)
82 __dm_written_to_disk(value)
84 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
85 __le64 key_le = cpu_to_le64(key);
87 if (index > nr_entries ||
88 index >= le32_to_cpu(node->header.max_entries)) {
89 DMERR("too many entries in btree node for insert");
90 __dm_unbless_for_disk(value);
94 __dm_bless_for_disk(&key_le);
96 array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
97 array_insert(value_base(node), value_size, nr_entries, index, value);
98 node->header.nr_entries = cpu_to_le32(nr_entries + 1);
103 /*----------------------------------------------------------------*/
106 * We want 3n entries (for some n). This works more nicely for repeated
107 * insert remove loops than (2n + 1).
109 static uint32_t calc_max_entries(size_t value_size, size_t block_size)
112 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
114 block_size -= sizeof(struct node_header);
115 total = block_size / elt_size;
116 n = total / 3; /* rounds down */
121 int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
125 struct btree_node *n;
127 uint32_t max_entries;
129 r = new_block(info, &b);
133 block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
134 max_entries = calc_max_entries(info->value_type.size, block_size);
136 n = dm_block_data(b);
137 memset(n, 0, block_size);
138 n->header.flags = cpu_to_le32(LEAF_NODE);
139 n->header.nr_entries = cpu_to_le32(0);
140 n->header.max_entries = cpu_to_le32(max_entries);
141 n->header.value_size = cpu_to_le32(info->value_type.size);
143 *root = dm_block_location(b);
144 unlock_block(info, b);
148 EXPORT_SYMBOL_GPL(dm_btree_empty);
150 /*----------------------------------------------------------------*/
153 * Deletion uses a recursive algorithm, since we have limited stack space
154 * we explicitly manage our own stack on the heap.
156 #define MAX_SPINE_DEPTH 64
159 struct btree_node *n;
161 unsigned nr_children;
162 unsigned current_child;
166 struct dm_btree_info *info;
167 struct dm_transaction_manager *tm;
169 struct frame spine[MAX_SPINE_DEPTH];
172 static int top_frame(struct del_stack *s, struct frame **f)
175 DMERR("btree deletion stack empty");
179 *f = s->spine + s->top;
184 static int unprocessed_frames(struct del_stack *s)
189 static void prefetch_children(struct del_stack *s, struct frame *f)
192 struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
194 for (i = 0; i < f->nr_children; i++)
195 dm_bm_prefetch(bm, value64(f->n, i));
198 static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
200 return f->level < (info->levels - 1);
203 static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
208 if (s->top >= MAX_SPINE_DEPTH - 1) {
209 DMERR("btree deletion stack out of memory");
213 r = dm_tm_ref(s->tm, b, &ref_count);
219 * This is a shared node, so we can just decrement it's
220 * reference counter and leave the children.
226 struct frame *f = s->spine + ++s->top;
228 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
234 f->n = dm_block_data(f->b);
236 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
237 f->current_child = 0;
239 flags = le32_to_cpu(f->n->header.flags);
240 if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
241 prefetch_children(s, f);
247 static void pop_frame(struct del_stack *s)
249 struct frame *f = s->spine + s->top--;
251 dm_tm_dec(s->tm, dm_block_location(f->b));
252 dm_tm_unlock(s->tm, f->b);
255 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
260 s = kmalloc(sizeof(*s), GFP_NOIO);
267 r = push_frame(s, root, 0);
271 while (unprocessed_frames(s)) {
276 r = top_frame(s, &f);
280 if (f->current_child >= f->nr_children) {
285 flags = le32_to_cpu(f->n->header.flags);
286 if (flags & INTERNAL_NODE) {
287 b = value64(f->n, f->current_child);
289 r = push_frame(s, b, f->level);
293 } else if (is_internal_level(info, f)) {
294 b = value64(f->n, f->current_child);
296 r = push_frame(s, b, f->level + 1);
301 if (info->value_type.dec) {
304 for (i = 0; i < f->nr_children; i++)
305 info->value_type.dec(info->value_type.context,
316 EXPORT_SYMBOL_GPL(dm_btree_del);
318 /*----------------------------------------------------------------*/
320 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
321 int (*search_fn)(struct btree_node *, uint64_t),
322 uint64_t *result_key, void *v, size_t value_size)
325 uint32_t flags, nr_entries;
328 r = ro_step(s, block);
332 i = search_fn(ro_node(s), key);
334 flags = le32_to_cpu(ro_node(s)->header.flags);
335 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
336 if (i < 0 || i >= nr_entries)
339 if (flags & INTERNAL_NODE)
340 block = value64(ro_node(s), i);
342 } while (!(flags & LEAF_NODE));
344 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
345 memcpy(v, value_ptr(ro_node(s), i), value_size);
350 int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
351 uint64_t *keys, void *value_le)
353 unsigned level, last_level = info->levels - 1;
356 __le64 internal_value_le;
357 struct ro_spine spine;
359 init_ro_spine(&spine, info);
360 for (level = 0; level < info->levels; level++) {
364 if (level == last_level) {
366 size = info->value_type.size;
369 value_p = &internal_value_le;
370 size = sizeof(uint64_t);
373 r = btree_lookup_raw(&spine, root, keys[level],
378 if (rkey != keys[level]) {
379 exit_ro_spine(&spine);
383 exit_ro_spine(&spine);
387 root = le64_to_cpu(internal_value_le);
389 exit_ro_spine(&spine);
393 EXPORT_SYMBOL_GPL(dm_btree_lookup);
396 * Splits a node by creating a sibling node and shifting half the nodes
397 * contents across. Assumes there is a parent node, and it has room for
419 * +---------+ +-------+
423 * Where A* is a shadow of A.
425 static int btree_split_sibling(struct shadow_spine *s, unsigned parent_index,
430 unsigned nr_left, nr_right;
431 struct dm_block *left, *right, *parent;
432 struct btree_node *ln, *rn, *pn;
435 left = shadow_current(s);
437 r = new_block(s->info, &right);
441 ln = dm_block_data(left);
442 rn = dm_block_data(right);
444 nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
445 nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
447 ln->header.nr_entries = cpu_to_le32(nr_left);
449 rn->header.flags = ln->header.flags;
450 rn->header.nr_entries = cpu_to_le32(nr_right);
451 rn->header.max_entries = ln->header.max_entries;
452 rn->header.value_size = ln->header.value_size;
453 memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
455 size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
456 sizeof(uint64_t) : s->info->value_type.size;
457 memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
461 * Patch up the parent
463 parent = shadow_parent(s);
465 pn = dm_block_data(parent);
466 location = cpu_to_le64(dm_block_location(left));
467 __dm_bless_for_disk(&location);
468 memcpy_disk(value_ptr(pn, parent_index),
469 &location, sizeof(__le64));
471 location = cpu_to_le64(dm_block_location(right));
472 __dm_bless_for_disk(&location);
474 r = insert_at(sizeof(__le64), pn, parent_index + 1,
475 le64_to_cpu(rn->keys[0]), &location);
479 if (key < le64_to_cpu(rn->keys[0])) {
480 unlock_block(s->info, right);
483 unlock_block(s->info, left);
491 * Splits a node by creating two new children beneath the given node.
507 * +-------+ +-------+
508 * | B +++ | | C +++ |
509 * +-------+ +-------+
511 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
515 unsigned nr_left, nr_right;
516 struct dm_block *left, *right, *new_parent;
517 struct btree_node *pn, *ln, *rn;
520 new_parent = shadow_current(s);
522 r = new_block(s->info, &left);
526 r = new_block(s->info, &right);
528 /* FIXME: put left */
532 pn = dm_block_data(new_parent);
533 ln = dm_block_data(left);
534 rn = dm_block_data(right);
536 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
537 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
539 ln->header.flags = pn->header.flags;
540 ln->header.nr_entries = cpu_to_le32(nr_left);
541 ln->header.max_entries = pn->header.max_entries;
542 ln->header.value_size = pn->header.value_size;
544 rn->header.flags = pn->header.flags;
545 rn->header.nr_entries = cpu_to_le32(nr_right);
546 rn->header.max_entries = pn->header.max_entries;
547 rn->header.value_size = pn->header.value_size;
549 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
550 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
552 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
553 sizeof(__le64) : s->info->value_type.size;
554 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
555 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
558 /* new_parent should just point to l and r now */
559 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
560 pn->header.nr_entries = cpu_to_le32(2);
561 pn->header.max_entries = cpu_to_le32(
562 calc_max_entries(sizeof(__le64),
564 dm_tm_get_bm(s->info->tm))));
565 pn->header.value_size = cpu_to_le32(sizeof(__le64));
567 val = cpu_to_le64(dm_block_location(left));
568 __dm_bless_for_disk(&val);
569 pn->keys[0] = ln->keys[0];
570 memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
572 val = cpu_to_le64(dm_block_location(right));
573 __dm_bless_for_disk(&val);
574 pn->keys[1] = rn->keys[0];
575 memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
578 * rejig the spine. This is ugly, since it knows too
579 * much about the spine
581 if (s->nodes[0] != new_parent) {
582 unlock_block(s->info, s->nodes[0]);
583 s->nodes[0] = new_parent;
585 if (key < le64_to_cpu(rn->keys[0])) {
586 unlock_block(s->info, right);
589 unlock_block(s->info, left);
597 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
598 struct dm_btree_value_type *vt,
599 uint64_t key, unsigned *index)
601 int r, i = *index, top = 1;
602 struct btree_node *node;
605 r = shadow_step(s, root, vt);
609 node = dm_block_data(shadow_current(s));
612 * We have to patch up the parent node, ugly, but I don't
613 * see a way to do this automatically as part of the spine
616 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
617 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
619 __dm_bless_for_disk(&location);
620 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
621 &location, sizeof(__le64));
624 node = dm_block_data(shadow_current(s));
626 if (node->header.nr_entries == node->header.max_entries) {
628 r = btree_split_beneath(s, key);
630 r = btree_split_sibling(s, i, key);
636 node = dm_block_data(shadow_current(s));
638 i = lower_bound(node, key);
640 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
644 /* change the bounds on the lowest key */
645 node->keys[0] = cpu_to_le64(key);
649 root = value64(node, i);
653 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
660 static int insert(struct dm_btree_info *info, dm_block_t root,
661 uint64_t *keys, void *value, dm_block_t *new_root,
663 __dm_written_to_disk(value)
666 unsigned level, index = -1, last_level = info->levels - 1;
667 dm_block_t block = root;
668 struct shadow_spine spine;
669 struct btree_node *n;
670 struct dm_btree_value_type le64_type;
672 init_le64_type(info->tm, &le64_type);
673 init_shadow_spine(&spine, info);
675 for (level = 0; level < (info->levels - 1); level++) {
676 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
680 n = dm_block_data(shadow_current(&spine));
681 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
682 (le64_to_cpu(n->keys[index]) != keys[level]));
688 r = dm_btree_empty(info, &new_tree);
692 new_le = cpu_to_le64(new_tree);
693 __dm_bless_for_disk(&new_le);
695 r = insert_at(sizeof(uint64_t), n, index,
696 keys[level], &new_le);
701 if (level < last_level)
702 block = value64(n, index);
705 r = btree_insert_raw(&spine, block, &info->value_type,
706 keys[level], &index);
710 n = dm_block_data(shadow_current(&spine));
711 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
712 (le64_to_cpu(n->keys[index]) != keys[level]));
718 r = insert_at(info->value_type.size, n, index,
726 if (info->value_type.dec &&
727 (!info->value_type.equal ||
728 !info->value_type.equal(
729 info->value_type.context,
732 info->value_type.dec(info->value_type.context,
733 value_ptr(n, index));
735 memcpy_disk(value_ptr(n, index),
736 value, info->value_type.size);
739 *new_root = shadow_root(&spine);
740 exit_shadow_spine(&spine);
745 __dm_unbless_for_disk(value);
747 exit_shadow_spine(&spine);
751 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
752 uint64_t *keys, void *value, dm_block_t *new_root)
753 __dm_written_to_disk(value)
755 return insert(info, root, keys, value, new_root, NULL);
757 EXPORT_SYMBOL_GPL(dm_btree_insert);
759 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
760 uint64_t *keys, void *value, dm_block_t *new_root,
762 __dm_written_to_disk(value)
764 return insert(info, root, keys, value, new_root, inserted);
766 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
768 /*----------------------------------------------------------------*/
770 static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
771 uint64_t *result_key, dm_block_t *next_block)
777 r = ro_step(s, block);
781 flags = le32_to_cpu(ro_node(s)->header.flags);
782 i = le32_to_cpu(ro_node(s)->header.nr_entries);
789 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
791 *result_key = le64_to_cpu(ro_node(s)->keys[0]);
793 if (next_block || flags & INTERNAL_NODE)
794 block = value64(ro_node(s), i);
796 } while (flags & INTERNAL_NODE);
803 static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
804 bool find_highest, uint64_t *result_keys)
806 int r = 0, count = 0, level;
807 struct ro_spine spine;
809 init_ro_spine(&spine, info);
810 for (level = 0; level < info->levels; level++) {
811 r = find_key(&spine, root, find_highest, result_keys + level,
812 level == info->levels - 1 ? NULL : &root);
822 exit_ro_spine(&spine);
824 return r ? r : count;
827 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
828 uint64_t *result_keys)
830 return dm_btree_find_key(info, root, true, result_keys);
832 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
834 int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
835 uint64_t *result_keys)
837 return dm_btree_find_key(info, root, false, result_keys);
839 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
841 /*----------------------------------------------------------------*/
844 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
845 * space. Also this only works for single level trees.
847 static int walk_node(struct dm_btree_info *info, dm_block_t block,
848 int (*fn)(void *context, uint64_t *keys, void *leaf),
853 struct dm_block *node;
854 struct btree_node *n;
857 r = bn_read_lock(info, block, &node);
861 n = dm_block_data(node);
863 nr = le32_to_cpu(n->header.nr_entries);
864 for (i = 0; i < nr; i++) {
865 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
866 r = walk_node(info, value64(n, i), fn, context);
870 keys = le64_to_cpu(*key_ptr(n, i));
871 r = fn(context, &keys, value_ptr(n, i));
878 dm_tm_unlock(info->tm, node);
882 int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
883 int (*fn)(void *context, uint64_t *keys, void *leaf),
886 BUG_ON(info->levels > 1);
887 return walk_node(info, root, fn, context);
889 EXPORT_SYMBOL_GPL(dm_btree_walk);