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 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 node *n, uint64_t key)
63 return bsearch(n, key, 0);
66 void inc_children(struct dm_transaction_manager *tm, struct 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++)
78 value_ptr(n, i, vt->size));
81 static int insert_at(size_t value_size, struct node *node, unsigned index,
82 uint64_t key, void *value)
83 __dm_written_to_disk(value)
85 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
86 __le64 key_le = cpu_to_le64(key);
88 if (index > nr_entries ||
89 index >= le32_to_cpu(node->header.max_entries)) {
90 DMERR("too many entries in btree node for insert");
91 __dm_unbless_for_disk(value);
95 __dm_bless_for_disk(&key_le);
97 array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
98 array_insert(value_base(node), value_size, nr_entries, index, value);
99 node->header.nr_entries = cpu_to_le32(nr_entries + 1);
104 /*----------------------------------------------------------------*/
107 * We want 3n entries (for some n). This works more nicely for repeated
108 * insert remove loops than (2n + 1).
110 static uint32_t calc_max_entries(size_t value_size, size_t block_size)
113 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
115 block_size -= sizeof(struct node_header);
116 total = block_size / elt_size;
117 n = total / 3; /* rounds down */
122 int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
128 uint32_t max_entries;
130 r = new_block(info, &b);
134 block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
135 max_entries = calc_max_entries(info->value_type.size, block_size);
137 n = dm_block_data(b);
138 memset(n, 0, block_size);
139 n->header.flags = cpu_to_le32(LEAF_NODE);
140 n->header.nr_entries = cpu_to_le32(0);
141 n->header.max_entries = cpu_to_le32(max_entries);
142 n->header.value_size = cpu_to_le32(info->value_type.size);
144 *root = dm_block_location(b);
145 return unlock_block(info, b);
147 EXPORT_SYMBOL_GPL(dm_btree_empty);
149 /*----------------------------------------------------------------*/
152 * Deletion uses a recursive algorithm, since we have limited stack space
153 * we explicitly manage our own stack on the heap.
155 #define MAX_SPINE_DEPTH 64
160 unsigned nr_children;
161 unsigned current_child;
165 struct dm_transaction_manager *tm;
167 struct frame spine[MAX_SPINE_DEPTH];
170 static int top_frame(struct del_stack *s, struct frame **f)
173 DMERR("btree deletion stack empty");
177 *f = s->spine + s->top;
182 static int unprocessed_frames(struct del_stack *s)
187 static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
192 if (s->top >= MAX_SPINE_DEPTH - 1) {
193 DMERR("btree deletion stack out of memory");
197 r = dm_tm_ref(s->tm, b, &ref_count);
203 * This is a shared node, so we can just decrement it's
204 * reference counter and leave the children.
209 struct frame *f = s->spine + ++s->top;
211 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
217 f->n = dm_block_data(f->b);
219 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
220 f->current_child = 0;
226 static void pop_frame(struct del_stack *s)
228 struct frame *f = s->spine + s->top--;
230 dm_tm_dec(s->tm, dm_block_location(f->b));
231 dm_tm_unlock(s->tm, f->b);
234 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
239 s = kmalloc(sizeof(*s), GFP_KERNEL);
245 r = push_frame(s, root, 1);
249 while (unprocessed_frames(s)) {
254 r = top_frame(s, &f);
258 if (f->current_child >= f->nr_children) {
263 flags = le32_to_cpu(f->n->header.flags);
264 if (flags & INTERNAL_NODE) {
265 b = value64(f->n, f->current_child);
267 r = push_frame(s, b, f->level);
271 } else if (f->level != (info->levels - 1)) {
272 b = value64(f->n, f->current_child);
274 r = push_frame(s, b, f->level + 1);
279 if (info->value_type.dec) {
282 for (i = 0; i < f->nr_children; i++)
283 info->value_type.dec(info->value_type.context,
284 value_ptr(f->n, i, info->value_type.size));
286 f->current_child = f->nr_children;
294 EXPORT_SYMBOL_GPL(dm_btree_del);
296 /*----------------------------------------------------------------*/
298 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
299 int (*search_fn)(struct node *, uint64_t),
300 uint64_t *result_key, void *v, size_t value_size)
303 uint32_t flags, nr_entries;
306 r = ro_step(s, block);
310 i = search_fn(ro_node(s), key);
312 flags = le32_to_cpu(ro_node(s)->header.flags);
313 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
314 if (i < 0 || i >= nr_entries)
317 if (flags & INTERNAL_NODE)
318 block = value64(ro_node(s), i);
320 } while (!(flags & LEAF_NODE));
322 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
323 memcpy(v, value_ptr(ro_node(s), i, value_size), value_size);
328 int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
329 uint64_t *keys, void *value_le)
331 unsigned level, last_level = info->levels - 1;
334 __le64 internal_value_le;
335 struct ro_spine spine;
337 init_ro_spine(&spine, info);
338 for (level = 0; level < info->levels; level++) {
342 if (level == last_level) {
344 size = info->value_type.size;
347 value_p = &internal_value_le;
348 size = sizeof(uint64_t);
351 r = btree_lookup_raw(&spine, root, keys[level],
356 if (rkey != keys[level]) {
357 exit_ro_spine(&spine);
361 exit_ro_spine(&spine);
365 root = le64_to_cpu(internal_value_le);
367 exit_ro_spine(&spine);
371 EXPORT_SYMBOL_GPL(dm_btree_lookup);
374 * Splits a node by creating a sibling node and shifting half the nodes
375 * contents across. Assumes there is a parent node, and it has room for
397 * +---------+ +-------+
401 * Where A* is a shadow of A.
403 static int btree_split_sibling(struct shadow_spine *s, dm_block_t root,
404 unsigned parent_index, uint64_t key)
408 unsigned nr_left, nr_right;
409 struct dm_block *left, *right, *parent;
410 struct node *ln, *rn, *pn;
413 left = shadow_current(s);
415 r = new_block(s->info, &right);
419 ln = dm_block_data(left);
420 rn = dm_block_data(right);
422 nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
423 nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
425 ln->header.nr_entries = cpu_to_le32(nr_left);
427 rn->header.flags = ln->header.flags;
428 rn->header.nr_entries = cpu_to_le32(nr_right);
429 rn->header.max_entries = ln->header.max_entries;
430 rn->header.value_size = ln->header.value_size;
431 memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
433 size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
434 sizeof(uint64_t) : s->info->value_type.size;
435 memcpy(value_ptr(rn, 0, size), value_ptr(ln, nr_left, size),
439 * Patch up the parent
441 parent = shadow_parent(s);
443 pn = dm_block_data(parent);
444 location = cpu_to_le64(dm_block_location(left));
445 __dm_bless_for_disk(&location);
446 memcpy_disk(value_ptr(pn, parent_index, sizeof(__le64)),
447 &location, sizeof(__le64));
449 location = cpu_to_le64(dm_block_location(right));
450 __dm_bless_for_disk(&location);
452 r = insert_at(sizeof(__le64), pn, parent_index + 1,
453 le64_to_cpu(rn->keys[0]), &location);
457 if (key < le64_to_cpu(rn->keys[0])) {
458 unlock_block(s->info, right);
461 unlock_block(s->info, left);
469 * Splits a node by creating two new children beneath the given node.
485 * +-------+ +-------+
486 * | B +++ | | C +++ |
487 * +-------+ +-------+
489 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
493 unsigned nr_left, nr_right;
494 struct dm_block *left, *right, *new_parent;
495 struct node *pn, *ln, *rn;
498 new_parent = shadow_current(s);
500 r = new_block(s->info, &left);
504 r = new_block(s->info, &right);
506 /* FIXME: put left */
510 pn = dm_block_data(new_parent);
511 ln = dm_block_data(left);
512 rn = dm_block_data(right);
514 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
515 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
517 ln->header.flags = pn->header.flags;
518 ln->header.nr_entries = cpu_to_le32(nr_left);
519 ln->header.max_entries = pn->header.max_entries;
520 ln->header.value_size = pn->header.value_size;
522 rn->header.flags = pn->header.flags;
523 rn->header.nr_entries = cpu_to_le32(nr_right);
524 rn->header.max_entries = pn->header.max_entries;
525 rn->header.value_size = pn->header.value_size;
527 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
528 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
530 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
531 sizeof(__le64) : s->info->value_type.size;
532 memcpy(value_ptr(ln, 0, size), value_ptr(pn, 0, size), nr_left * size);
533 memcpy(value_ptr(rn, 0, size), value_ptr(pn, nr_left, size),
536 /* new_parent should just point to l and r now */
537 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
538 pn->header.nr_entries = cpu_to_le32(2);
539 pn->header.max_entries = cpu_to_le32(
540 calc_max_entries(sizeof(__le64),
542 dm_tm_get_bm(s->info->tm))));
543 pn->header.value_size = cpu_to_le32(sizeof(__le64));
545 val = cpu_to_le64(dm_block_location(left));
546 __dm_bless_for_disk(&val);
547 pn->keys[0] = ln->keys[0];
548 memcpy_disk(value_ptr(pn, 0, sizeof(__le64)), &val, sizeof(__le64));
550 val = cpu_to_le64(dm_block_location(right));
551 __dm_bless_for_disk(&val);
552 pn->keys[1] = rn->keys[0];
553 memcpy_disk(value_ptr(pn, 1, sizeof(__le64)), &val, sizeof(__le64));
556 * rejig the spine. This is ugly, since it knows too
557 * much about the spine
559 if (s->nodes[0] != new_parent) {
560 unlock_block(s->info, s->nodes[0]);
561 s->nodes[0] = new_parent;
563 if (key < le64_to_cpu(rn->keys[0])) {
564 unlock_block(s->info, right);
567 unlock_block(s->info, left);
575 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
576 struct dm_btree_value_type *vt,
577 uint64_t key, unsigned *index)
579 int r, i = *index, top = 1;
583 r = shadow_step(s, root, vt);
587 node = dm_block_data(shadow_current(s));
590 * We have to patch up the parent node, ugly, but I don't
591 * see a way to do this automatically as part of the spine
594 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
595 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
597 __dm_bless_for_disk(&location);
598 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i, sizeof(uint64_t)),
599 &location, sizeof(__le64));
602 node = dm_block_data(shadow_current(s));
604 if (node->header.nr_entries == node->header.max_entries) {
606 r = btree_split_beneath(s, key);
608 r = btree_split_sibling(s, root, i, key);
614 node = dm_block_data(shadow_current(s));
616 i = lower_bound(node, key);
618 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
622 /* change the bounds on the lowest key */
623 node->keys[0] = cpu_to_le64(key);
627 root = value64(node, i);
631 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
638 static int insert(struct dm_btree_info *info, dm_block_t root,
639 uint64_t *keys, void *value, dm_block_t *new_root,
641 __dm_written_to_disk(value)
644 unsigned level, index = -1, last_level = info->levels - 1;
645 dm_block_t block = root;
646 struct shadow_spine spine;
648 struct dm_btree_value_type le64_type;
650 le64_type.context = NULL;
651 le64_type.size = sizeof(__le64);
652 le64_type.inc = NULL;
653 le64_type.dec = NULL;
654 le64_type.equal = NULL;
656 init_shadow_spine(&spine, info);
658 for (level = 0; level < (info->levels - 1); level++) {
659 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
663 n = dm_block_data(shadow_current(&spine));
664 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
665 (le64_to_cpu(n->keys[index]) != keys[level]));
671 r = dm_btree_empty(info, &new_tree);
675 new_le = cpu_to_le64(new_tree);
676 __dm_bless_for_disk(&new_le);
678 r = insert_at(sizeof(uint64_t), n, index,
679 keys[level], &new_le);
684 if (level < last_level)
685 block = value64(n, index);
688 r = btree_insert_raw(&spine, block, &info->value_type,
689 keys[level], &index);
693 n = dm_block_data(shadow_current(&spine));
694 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
695 (le64_to_cpu(n->keys[index]) != keys[level]));
701 r = insert_at(info->value_type.size, n, index,
709 if (info->value_type.dec &&
710 (!info->value_type.equal ||
711 !info->value_type.equal(
712 info->value_type.context,
713 value_ptr(n, index, info->value_type.size),
715 info->value_type.dec(info->value_type.context,
716 value_ptr(n, index, info->value_type.size));
718 memcpy_disk(value_ptr(n, index, info->value_type.size),
719 value, info->value_type.size);
722 *new_root = shadow_root(&spine);
723 exit_shadow_spine(&spine);
728 __dm_unbless_for_disk(value);
730 exit_shadow_spine(&spine);
734 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
735 uint64_t *keys, void *value, dm_block_t *new_root)
736 __dm_written_to_disk(value)
738 return insert(info, root, keys, value, new_root, NULL);
740 EXPORT_SYMBOL_GPL(dm_btree_insert);
742 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
743 uint64_t *keys, void *value, dm_block_t *new_root,
745 __dm_written_to_disk(value)
747 return insert(info, root, keys, value, new_root, inserted);
749 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
751 /*----------------------------------------------------------------*/
753 static int find_highest_key(struct ro_spine *s, dm_block_t block,
754 uint64_t *result_key, dm_block_t *next_block)
760 r = ro_step(s, block);
764 flags = le32_to_cpu(ro_node(s)->header.flags);
765 i = le32_to_cpu(ro_node(s)->header.nr_entries);
771 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
772 if (next_block || flags & INTERNAL_NODE)
773 block = value64(ro_node(s), i);
775 } while (flags & INTERNAL_NODE);
782 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
783 uint64_t *result_keys)
785 int r = 0, count = 0, level;
786 struct ro_spine spine;
788 init_ro_spine(&spine, info);
789 for (level = 0; level < info->levels; level++) {
790 r = find_highest_key(&spine, root, result_keys + level,
791 level == info->levels - 1 ? NULL : &root);
801 exit_ro_spine(&spine);
803 return r ? r : count;
805 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);