2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
8 #include "dm-btree-internal.h"
9 #include "dm-transaction-manager.h"
11 #include <linux/export.h>
12 #include <linux/device-mapper.h>
14 #define DM_MSG_PREFIX "btree"
17 * Removing an entry from a btree
18 * ==============================
20 * A very important constraint for our btree is that no node, except the
21 * root, may have fewer than a certain number of entries.
22 * (MIN_ENTRIES <= nr_entries <= MAX_ENTRIES).
24 * Ensuring this is complicated by the way we want to only ever hold the
25 * locks on 2 nodes concurrently, and only change nodes in a top to bottom
28 * Each node may have a left or right sibling. When decending the spine,
29 * if a node contains only MIN_ENTRIES then we try and increase this to at
30 * least MIN_ENTRIES + 1. We do this in the following ways:
32 * [A] No siblings => this can only happen if the node is the root, in which
33 * case we copy the childs contents over the root.
36 * ==> rebalance(node, right sibling)
38 * [C] No right sibling
39 * ==> rebalance(left sibling, node)
41 * [D] Both siblings, total_entries(left, node, right) <= DEL_THRESHOLD
42 * ==> delete node adding it's contents to left and right
44 * [E] Both siblings, total_entries(left, node, right) > DEL_THRESHOLD
45 * ==> rebalance(left, node, right)
47 * After these operations it's possible that the our original node no
48 * longer contains the desired sub tree. For this reason this rebalancing
49 * is performed on the children of the current node. This also avoids
50 * having a special case for the root.
52 * Once this rebalancing has occurred we can then step into the child node
53 * for internal nodes. Or delete the entry for leaf nodes.
57 * Some little utilities for moving node data around.
59 static void node_shift(struct btree_node *n, int shift)
61 uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
62 uint32_t value_size = le32_to_cpu(n->header.value_size);
66 BUG_ON(shift > nr_entries);
67 BUG_ON((void *) key_ptr(n, shift) >= value_ptr(n, shift));
68 memmove(key_ptr(n, 0),
70 (nr_entries - shift) * sizeof(__le64));
71 memmove(value_ptr(n, 0),
73 (nr_entries - shift) * value_size);
75 BUG_ON(nr_entries + shift > le32_to_cpu(n->header.max_entries));
76 memmove(key_ptr(n, shift),
78 nr_entries * sizeof(__le64));
79 memmove(value_ptr(n, shift),
81 nr_entries * value_size);
85 static int node_copy(struct btree_node *left, struct btree_node *right, int shift)
87 uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
88 uint32_t value_size = le32_to_cpu(left->header.value_size);
89 if (value_size != le32_to_cpu(right->header.value_size)) {
90 DMERR("mismatched value size");
97 if (nr_left + shift > le32_to_cpu(left->header.max_entries)) {
102 memcpy(key_ptr(left, nr_left),
104 shift * sizeof(__le64));
105 memcpy(value_ptr(left, nr_left),
109 if (shift > le32_to_cpu(right->header.max_entries)) {
114 memcpy(key_ptr(right, 0),
115 key_ptr(left, nr_left - shift),
116 shift * sizeof(__le64));
117 memcpy(value_ptr(right, 0),
118 value_ptr(left, nr_left - shift),
125 * Delete a specific entry from a leaf node.
127 static void delete_at(struct btree_node *n, unsigned index)
129 unsigned nr_entries = le32_to_cpu(n->header.nr_entries);
130 unsigned nr_to_copy = nr_entries - (index + 1);
131 uint32_t value_size = le32_to_cpu(n->header.value_size);
132 BUG_ON(index >= nr_entries);
135 memmove(key_ptr(n, index),
136 key_ptr(n, index + 1),
137 nr_to_copy * sizeof(__le64));
139 memmove(value_ptr(n, index),
140 value_ptr(n, index + 1),
141 nr_to_copy * value_size);
144 n->header.nr_entries = cpu_to_le32(nr_entries - 1);
147 static unsigned merge_threshold(struct btree_node *n)
149 return le32_to_cpu(n->header.max_entries) / 3;
154 struct dm_block *block;
155 struct btree_node *n;
158 static int init_child(struct dm_btree_info *info, struct dm_btree_value_type *vt,
159 struct btree_node *parent,
160 unsigned index, struct child *result)
165 result->index = index;
166 root = value64(parent, index);
168 r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
169 &result->block, &inc);
173 result->n = dm_block_data(result->block);
176 inc_children(info->tm, result->n, vt);
178 *((__le64 *) value_ptr(parent, index)) =
179 cpu_to_le64(dm_block_location(result->block));
184 static void exit_child(struct dm_btree_info *info, struct child *c)
186 dm_tm_unlock(info->tm, c->block);
189 static int shift(struct btree_node *left, struct btree_node *right, int count)
192 uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
193 uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
194 uint32_t max_entries = le32_to_cpu(left->header.max_entries);
195 uint32_t r_max_entries = le32_to_cpu(right->header.max_entries);
197 if (max_entries != r_max_entries) {
198 DMERR("node max_entries mismatch");
202 if (nr_left - count > max_entries) {
203 DMERR("node shift out of bounds");
207 if (nr_right + count > max_entries) {
208 DMERR("node shift out of bounds");
216 node_shift(right, count);
217 r = node_copy(left, right, count);
221 r = node_copy(left, right, count);
224 node_shift(right, count);
227 left->header.nr_entries = cpu_to_le32(nr_left - count);
228 right->header.nr_entries = cpu_to_le32(nr_right + count);
233 static int __rebalance2(struct dm_btree_info *info, struct btree_node *parent,
234 struct child *l, struct child *r)
237 struct btree_node *left = l->n;
238 struct btree_node *right = r->n;
239 uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
240 uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
242 * Ensure the number of entries in each child will be greater
243 * than or equal to (max_entries / 3 + 1), so no matter which
244 * child is used for removal, the number will still be not
245 * less than (max_entries / 3).
247 unsigned int threshold = 2 * (merge_threshold(left) + 1);
249 if (nr_left + nr_right < threshold) {
253 node_copy(left, right, -nr_right);
254 left->header.nr_entries = cpu_to_le32(nr_left + nr_right);
255 delete_at(parent, r->index);
258 * We need to decrement the right block, but not it's
259 * children, since they're still referenced by left.
261 dm_tm_dec(info->tm, dm_block_location(r->block));
266 unsigned target_left = (nr_left + nr_right) / 2;
267 ret = shift(left, right, nr_left - target_left);
270 *key_ptr(parent, r->index) = right->keys[0];
275 static int rebalance2(struct shadow_spine *s, struct dm_btree_info *info,
276 struct dm_btree_value_type *vt, unsigned left_index)
279 struct btree_node *parent;
280 struct child left, right;
282 parent = dm_block_data(shadow_current(s));
284 r = init_child(info, vt, parent, left_index, &left);
288 r = init_child(info, vt, parent, left_index + 1, &right);
290 exit_child(info, &left);
294 r = __rebalance2(info, parent, &left, &right);
296 exit_child(info, &left);
297 exit_child(info, &right);
303 * We dump as many entries from center as possible into left, then the rest
304 * in right, then rebalance2. This wastes some cpu, but I want something
307 static int delete_center_node(struct dm_btree_info *info, struct btree_node *parent,
308 struct child *l, struct child *c, struct child *r,
309 struct btree_node *left, struct btree_node *center, struct btree_node *right,
310 uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
312 uint32_t max_entries = le32_to_cpu(left->header.max_entries);
313 unsigned shift = min(max_entries - nr_left, nr_center);
315 if (nr_left + shift > max_entries) {
316 DMERR("node shift out of bounds");
320 node_copy(left, center, -shift);
321 left->header.nr_entries = cpu_to_le32(nr_left + shift);
323 if (shift != nr_center) {
324 shift = nr_center - shift;
326 if ((nr_right + shift) > max_entries) {
327 DMERR("node shift out of bounds");
331 node_shift(right, shift);
332 node_copy(center, right, shift);
333 right->header.nr_entries = cpu_to_le32(nr_right + shift);
335 *key_ptr(parent, r->index) = right->keys[0];
337 delete_at(parent, c->index);
340 dm_tm_dec(info->tm, dm_block_location(c->block));
341 return __rebalance2(info, parent, l, r);
345 * Redistributes entries among 3 sibling nodes.
347 static int redistribute3(struct dm_btree_info *info, struct btree_node *parent,
348 struct child *l, struct child *c, struct child *r,
349 struct btree_node *left, struct btree_node *center, struct btree_node *right,
350 uint32_t nr_left, uint32_t nr_center, uint32_t nr_right)
353 uint32_t max_entries = le32_to_cpu(left->header.max_entries);
354 unsigned total = nr_left + nr_center + nr_right;
355 unsigned target_right = total / 3;
356 unsigned remainder = (target_right * 3) != total;
357 unsigned target_left = target_right + remainder;
359 BUG_ON(target_left > max_entries);
360 BUG_ON(target_right > max_entries);
362 if (nr_left < nr_right) {
363 s = nr_left - target_left;
365 if (s < 0 && nr_center < -s) {
366 /* not enough in central node */
367 ret = shift(left, center, -nr_center);
372 ret = shift(left, right, s);
378 ret = shift(left, center, s);
383 ret = shift(center, right, target_right - nr_right);
387 s = target_right - nr_right;
388 if (s > 0 && nr_center < s) {
389 /* not enough in central node */
390 ret = shift(center, right, nr_center);
394 ret = shift(left, right, s);
399 ret = shift(center, right, s);
404 ret = shift(left, center, nr_left - target_left);
409 *key_ptr(parent, c->index) = center->keys[0];
410 *key_ptr(parent, r->index) = right->keys[0];
414 static int __rebalance3(struct dm_btree_info *info, struct btree_node *parent,
415 struct child *l, struct child *c, struct child *r)
417 struct btree_node *left = l->n;
418 struct btree_node *center = c->n;
419 struct btree_node *right = r->n;
421 uint32_t nr_left = le32_to_cpu(left->header.nr_entries);
422 uint32_t nr_center = le32_to_cpu(center->header.nr_entries);
423 uint32_t nr_right = le32_to_cpu(right->header.nr_entries);
425 unsigned threshold = merge_threshold(left) * 4 + 1;
427 if ((left->header.max_entries != center->header.max_entries) ||
428 (center->header.max_entries != right->header.max_entries)) {
429 DMERR("bad btree metadata, max_entries differ");
433 if ((nr_left + nr_center + nr_right) < threshold) {
434 return delete_center_node(info, parent, l, c, r, left, center, right,
435 nr_left, nr_center, nr_right);
438 return redistribute3(info, parent, l, c, r, left, center, right,
439 nr_left, nr_center, nr_right);
442 static int rebalance3(struct shadow_spine *s, struct dm_btree_info *info,
443 struct dm_btree_value_type *vt, unsigned left_index)
446 struct btree_node *parent = dm_block_data(shadow_current(s));
447 struct child left, center, right;
450 * FIXME: fill out an array?
452 r = init_child(info, vt, parent, left_index, &left);
456 r = init_child(info, vt, parent, left_index + 1, ¢er);
458 exit_child(info, &left);
462 r = init_child(info, vt, parent, left_index + 2, &right);
464 exit_child(info, &left);
465 exit_child(info, ¢er);
469 r = __rebalance3(info, parent, &left, ¢er, &right);
471 exit_child(info, &left);
472 exit_child(info, ¢er);
473 exit_child(info, &right);
478 static int rebalance_children(struct shadow_spine *s,
479 struct dm_btree_info *info,
480 struct dm_btree_value_type *vt, uint64_t key)
482 int i, r, has_left_sibling, has_right_sibling;
483 struct btree_node *n;
485 n = dm_block_data(shadow_current(s));
487 if (le32_to_cpu(n->header.nr_entries) == 1) {
488 struct dm_block *child;
489 dm_block_t b = value64(n, 0);
491 r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child);
495 memcpy(n, dm_block_data(child),
496 dm_bm_block_size(dm_tm_get_bm(info->tm)));
498 dm_tm_dec(info->tm, dm_block_location(child));
499 dm_tm_unlock(info->tm, child);
503 i = lower_bound(n, key);
507 has_left_sibling = i > 0;
508 has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1);
510 if (!has_left_sibling)
511 r = rebalance2(s, info, vt, i);
513 else if (!has_right_sibling)
514 r = rebalance2(s, info, vt, i - 1);
517 r = rebalance3(s, info, vt, i - 1);
522 static int do_leaf(struct btree_node *n, uint64_t key, unsigned *index)
524 int i = lower_bound(n, key);
527 (i >= le32_to_cpu(n->header.nr_entries)) ||
528 (le64_to_cpu(n->keys[i]) != key))
537 * Prepares for removal from one level of the hierarchy. The caller must
538 * call delete_at() to remove the entry at index.
540 static int remove_raw(struct shadow_spine *s, struct dm_btree_info *info,
541 struct dm_btree_value_type *vt, dm_block_t root,
542 uint64_t key, unsigned *index)
545 struct btree_node *n;
548 r = shadow_step(s, root, vt);
553 * We have to patch up the parent node, ugly, but I don't
554 * see a way to do this automatically as part of the spine
557 if (shadow_has_parent(s)) {
558 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
559 memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
560 &location, sizeof(__le64));
563 n = dm_block_data(shadow_current(s));
565 if (le32_to_cpu(n->header.flags) & LEAF_NODE)
566 return do_leaf(n, key, index);
568 r = rebalance_children(s, info, vt, key);
572 n = dm_block_data(shadow_current(s));
573 if (le32_to_cpu(n->header.flags) & LEAF_NODE)
574 return do_leaf(n, key, index);
576 i = lower_bound(n, key);
579 * We know the key is present, or else
580 * rebalance_children would have returned
583 root = value64(n, i);
589 int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
590 uint64_t *keys, dm_block_t *new_root)
592 unsigned level, last_level = info->levels - 1;
593 int index = 0, r = 0;
594 struct shadow_spine spine;
595 struct btree_node *n;
596 struct dm_btree_value_type le64_vt;
598 init_le64_type(info->tm, &le64_vt);
599 init_shadow_spine(&spine, info);
600 for (level = 0; level < info->levels; level++) {
601 r = remove_raw(&spine, info,
602 (level == last_level ?
603 &info->value_type : &le64_vt),
604 root, keys[level], (unsigned *)&index);
608 n = dm_block_data(shadow_current(&spine));
609 if (level != last_level) {
610 root = value64(n, index);
614 BUG_ON(index < 0 || index >= le32_to_cpu(n->header.nr_entries));
616 if (info->value_type.dec)
617 info->value_type.dec(info->value_type.context,
618 value_ptr(n, index), 1);
624 *new_root = shadow_root(&spine);
625 exit_shadow_spine(&spine);
629 EXPORT_SYMBOL_GPL(dm_btree_remove);
631 /*----------------------------------------------------------------*/
633 static int remove_nearest(struct shadow_spine *s, struct dm_btree_info *info,
634 struct dm_btree_value_type *vt, dm_block_t root,
635 uint64_t key, int *index)
638 struct btree_node *n;
641 r = shadow_step(s, root, vt);
646 * We have to patch up the parent node, ugly, but I don't
647 * see a way to do this automatically as part of the spine
650 if (shadow_has_parent(s)) {
651 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
652 memcpy(value_ptr(dm_block_data(shadow_parent(s)), i),
653 &location, sizeof(__le64));
656 n = dm_block_data(shadow_current(s));
658 if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
659 *index = lower_bound(n, key);
663 r = rebalance_children(s, info, vt, key);
667 n = dm_block_data(shadow_current(s));
668 if (le32_to_cpu(n->header.flags) & LEAF_NODE) {
669 *index = lower_bound(n, key);
673 i = lower_bound(n, key);
676 * We know the key is present, or else
677 * rebalance_children would have returned
680 root = value64(n, i);
686 static int remove_one(struct dm_btree_info *info, dm_block_t root,
687 uint64_t *keys, uint64_t end_key,
688 dm_block_t *new_root, unsigned *nr_removed)
690 unsigned level, last_level = info->levels - 1;
691 int index = 0, r = 0;
692 struct shadow_spine spine;
693 struct btree_node *n;
694 struct dm_btree_value_type le64_vt;
697 init_le64_type(info->tm, &le64_vt);
698 init_shadow_spine(&spine, info);
699 for (level = 0; level < last_level; level++) {
700 r = remove_raw(&spine, info, &le64_vt,
701 root, keys[level], (unsigned *) &index);
705 n = dm_block_data(shadow_current(&spine));
706 root = value64(n, index);
709 r = remove_nearest(&spine, info, &info->value_type,
710 root, keys[last_level], &index);
714 n = dm_block_data(shadow_current(&spine));
719 if (index >= le32_to_cpu(n->header.nr_entries)) {
724 k = le64_to_cpu(n->keys[index]);
725 if (k >= keys[last_level] && k < end_key) {
726 if (info->value_type.dec)
727 info->value_type.dec(info->value_type.context,
728 value_ptr(n, index), 1);
731 keys[last_level] = k + 1ull;
737 *new_root = shadow_root(&spine);
738 exit_shadow_spine(&spine);
743 int dm_btree_remove_leaves(struct dm_btree_info *info, dm_block_t root,
744 uint64_t *first_key, uint64_t end_key,
745 dm_block_t *new_root, unsigned *nr_removed)
751 r = remove_one(info, root, first_key, end_key, &root, nr_removed);
757 return r == -ENODATA ? 0 : r;
759 EXPORT_SYMBOL_GPL(dm_btree_remove_leaves);