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>
14 * Removing an entry from a btree
15 * ==============================
17 * A very important constraint for our btree is that no node, except the
18 * root, may have fewer than a certain number of entries.
19 * (MIN_ENTRIES <= nr_entries <= MAX_ENTRIES).
21 * Ensuring this is complicated by the way we want to only ever hold the
22 * locks on 2 nodes concurrently, and only change nodes in a top to bottom
25 * Each node may have a left or right sibling. When decending the spine,
26 * if a node contains only MIN_ENTRIES then we try and increase this to at
27 * least MIN_ENTRIES + 1. We do this in the following ways:
29 * [A] No siblings => this can only happen if the node is the root, in which
30 * case we copy the childs contents over the root.
33 * ==> rebalance(node, right sibling)
35 * [C] No right sibling
36 * ==> rebalance(left sibling, node)
38 * [D] Both siblings, total_entries(left, node, right) <= DEL_THRESHOLD
39 * ==> delete node adding it's contents to left and right
41 * [E] Both siblings, total_entries(left, node, right) > DEL_THRESHOLD
42 * ==> rebalance(left, node, right)
44 * After these operations it's possible that the our original node no
45 * longer contains the desired sub tree. For this reason this rebalancing
46 * is performed on the children of the current node. This also avoids
47 * having a special case for the root.
49 * Once this rebalancing has occurred we can then step into the child node
50 * for internal nodes. Or delete the entry for leaf nodes.
54 * Some little utilities for moving node data around.
56 static void node_shift(struct btree_node
*n
, int shift
)
58 uint32_t nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
59 uint32_t value_size
= le32_to_cpu(n
->header
.value_size
);
63 BUG_ON(shift
> nr_entries
);
64 BUG_ON((void *) key_ptr(n
, shift
) >= value_ptr(n
, shift
));
65 memmove(key_ptr(n
, 0),
67 (nr_entries
- shift
) * sizeof(__le64
));
68 memmove(value_ptr(n
, 0),
70 (nr_entries
- shift
) * value_size
);
72 BUG_ON(nr_entries
+ shift
> le32_to_cpu(n
->header
.max_entries
));
73 memmove(key_ptr(n
, shift
),
75 nr_entries
* sizeof(__le64
));
76 memmove(value_ptr(n
, shift
),
78 nr_entries
* value_size
);
82 static void node_copy(struct btree_node
*left
, struct btree_node
*right
, int shift
)
84 uint32_t nr_left
= le32_to_cpu(left
->header
.nr_entries
);
85 uint32_t value_size
= le32_to_cpu(left
->header
.value_size
);
86 BUG_ON(value_size
!= le32_to_cpu(right
->header
.value_size
));
90 BUG_ON(nr_left
+ shift
> le32_to_cpu(left
->header
.max_entries
));
91 memcpy(key_ptr(left
, nr_left
),
93 shift
* sizeof(__le64
));
94 memcpy(value_ptr(left
, nr_left
),
98 BUG_ON(shift
> le32_to_cpu(right
->header
.max_entries
));
99 memcpy(key_ptr(right
, 0),
100 key_ptr(left
, nr_left
- shift
),
101 shift
* sizeof(__le64
));
102 memcpy(value_ptr(right
, 0),
103 value_ptr(left
, nr_left
- shift
),
109 * Delete a specific entry from a leaf node.
111 static void delete_at(struct btree_node
*n
, unsigned index
)
113 unsigned nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
114 unsigned nr_to_copy
= nr_entries
- (index
+ 1);
115 uint32_t value_size
= le32_to_cpu(n
->header
.value_size
);
116 BUG_ON(index
>= nr_entries
);
119 memmove(key_ptr(n
, index
),
120 key_ptr(n
, index
+ 1),
121 nr_to_copy
* sizeof(__le64
));
123 memmove(value_ptr(n
, index
),
124 value_ptr(n
, index
+ 1),
125 nr_to_copy
* value_size
);
128 n
->header
.nr_entries
= cpu_to_le32(nr_entries
- 1);
131 static unsigned merge_threshold(struct btree_node
*n
)
133 return le32_to_cpu(n
->header
.max_entries
) / 3;
138 struct dm_block
*block
;
139 struct btree_node
*n
;
142 static struct dm_btree_value_type le64_type
= {
144 .size
= sizeof(__le64
),
150 static int init_child(struct dm_btree_info
*info
, struct btree_node
*parent
,
151 unsigned index
, struct child
*result
)
156 result
->index
= index
;
157 root
= value64(parent
, index
);
159 r
= dm_tm_shadow_block(info
->tm
, root
, &btree_node_validator
,
160 &result
->block
, &inc
);
164 result
->n
= dm_block_data(result
->block
);
167 inc_children(info
->tm
, result
->n
, &le64_type
);
169 *((__le64
*) value_ptr(parent
, index
)) =
170 cpu_to_le64(dm_block_location(result
->block
));
175 static int exit_child(struct dm_btree_info
*info
, struct child
*c
)
177 return dm_tm_unlock(info
->tm
, c
->block
);
180 static void shift(struct btree_node
*left
, struct btree_node
*right
, int count
)
182 uint32_t nr_left
= le32_to_cpu(left
->header
.nr_entries
);
183 uint32_t nr_right
= le32_to_cpu(right
->header
.nr_entries
);
184 uint32_t max_entries
= le32_to_cpu(left
->header
.max_entries
);
185 uint32_t r_max_entries
= le32_to_cpu(right
->header
.max_entries
);
187 BUG_ON(max_entries
!= r_max_entries
);
188 BUG_ON(nr_left
- count
> max_entries
);
189 BUG_ON(nr_right
+ count
> max_entries
);
195 node_shift(right
, count
);
196 node_copy(left
, right
, count
);
198 node_copy(left
, right
, count
);
199 node_shift(right
, count
);
202 left
->header
.nr_entries
= cpu_to_le32(nr_left
- count
);
203 right
->header
.nr_entries
= cpu_to_le32(nr_right
+ count
);
206 static void __rebalance2(struct dm_btree_info
*info
, struct btree_node
*parent
,
207 struct child
*l
, struct child
*r
)
209 struct btree_node
*left
= l
->n
;
210 struct btree_node
*right
= r
->n
;
211 uint32_t nr_left
= le32_to_cpu(left
->header
.nr_entries
);
212 uint32_t nr_right
= le32_to_cpu(right
->header
.nr_entries
);
213 unsigned threshold
= 2 * merge_threshold(left
) + 1;
215 if (nr_left
+ nr_right
< threshold
) {
219 node_copy(left
, right
, -nr_right
);
220 left
->header
.nr_entries
= cpu_to_le32(nr_left
+ nr_right
);
221 delete_at(parent
, r
->index
);
224 * We need to decrement the right block, but not it's
225 * children, since they're still referenced by left.
227 dm_tm_dec(info
->tm
, dm_block_location(r
->block
));
232 unsigned target_left
= (nr_left
+ nr_right
) / 2;
233 shift(left
, right
, nr_left
- target_left
);
234 *key_ptr(parent
, r
->index
) = right
->keys
[0];
238 static int rebalance2(struct shadow_spine
*s
, struct dm_btree_info
*info
,
242 struct btree_node
*parent
;
243 struct child left
, right
;
245 parent
= dm_block_data(shadow_current(s
));
247 r
= init_child(info
, parent
, left_index
, &left
);
251 r
= init_child(info
, parent
, left_index
+ 1, &right
);
253 exit_child(info
, &left
);
257 __rebalance2(info
, parent
, &left
, &right
);
259 r
= exit_child(info
, &left
);
261 exit_child(info
, &right
);
265 return exit_child(info
, &right
);
269 * We dump as many entries from center as possible into left, then the rest
270 * in right, then rebalance2. This wastes some cpu, but I want something
273 static void delete_center_node(struct dm_btree_info
*info
, struct btree_node
*parent
,
274 struct child
*l
, struct child
*c
, struct child
*r
,
275 struct btree_node
*left
, struct btree_node
*center
, struct btree_node
*right
,
276 uint32_t nr_left
, uint32_t nr_center
, uint32_t nr_right
)
278 uint32_t max_entries
= le32_to_cpu(left
->header
.max_entries
);
279 unsigned shift
= min(max_entries
- nr_left
, nr_center
);
281 BUG_ON(nr_left
+ shift
> max_entries
);
282 node_copy(left
, center
, -shift
);
283 left
->header
.nr_entries
= cpu_to_le32(nr_left
+ shift
);
285 if (shift
!= nr_center
) {
286 shift
= nr_center
- shift
;
287 BUG_ON((nr_right
+ shift
) > max_entries
);
288 node_shift(right
, shift
);
289 node_copy(center
, right
, shift
);
290 right
->header
.nr_entries
= cpu_to_le32(nr_right
+ shift
);
292 *key_ptr(parent
, r
->index
) = right
->keys
[0];
294 delete_at(parent
, c
->index
);
297 dm_tm_dec(info
->tm
, dm_block_location(c
->block
));
298 __rebalance2(info
, parent
, l
, r
);
302 * Redistributes entries among 3 sibling nodes.
304 static void redistribute3(struct dm_btree_info
*info
, struct btree_node
*parent
,
305 struct child
*l
, struct child
*c
, struct child
*r
,
306 struct btree_node
*left
, struct btree_node
*center
, struct btree_node
*right
,
307 uint32_t nr_left
, uint32_t nr_center
, uint32_t nr_right
)
310 uint32_t max_entries
= le32_to_cpu(left
->header
.max_entries
);
311 unsigned target
= (nr_left
+ nr_center
+ nr_right
) / 3;
312 BUG_ON(target
> max_entries
);
314 if (nr_left
< nr_right
) {
315 s
= nr_left
- target
;
317 if (s
< 0 && nr_center
< -s
) {
318 /* not enough in central node */
319 shift(left
, center
, nr_center
);
320 s
= nr_center
- target
;
321 shift(left
, right
, s
);
324 shift(left
, center
, s
);
326 shift(center
, right
, target
- nr_right
);
329 s
= target
- nr_right
;
330 if (s
> 0 && nr_center
< s
) {
331 /* not enough in central node */
332 shift(center
, right
, nr_center
);
333 s
= target
- nr_center
;
334 shift(left
, right
, s
);
337 shift(center
, right
, s
);
339 shift(left
, center
, nr_left
- target
);
342 *key_ptr(parent
, c
->index
) = center
->keys
[0];
343 *key_ptr(parent
, r
->index
) = right
->keys
[0];
346 static void __rebalance3(struct dm_btree_info
*info
, struct btree_node
*parent
,
347 struct child
*l
, struct child
*c
, struct child
*r
)
349 struct btree_node
*left
= l
->n
;
350 struct btree_node
*center
= c
->n
;
351 struct btree_node
*right
= r
->n
;
353 uint32_t nr_left
= le32_to_cpu(left
->header
.nr_entries
);
354 uint32_t nr_center
= le32_to_cpu(center
->header
.nr_entries
);
355 uint32_t nr_right
= le32_to_cpu(right
->header
.nr_entries
);
357 unsigned threshold
= merge_threshold(left
) * 4 + 1;
359 BUG_ON(left
->header
.max_entries
!= center
->header
.max_entries
);
360 BUG_ON(center
->header
.max_entries
!= right
->header
.max_entries
);
362 if ((nr_left
+ nr_center
+ nr_right
) < threshold
)
363 delete_center_node(info
, parent
, l
, c
, r
, left
, center
, right
,
364 nr_left
, nr_center
, nr_right
);
366 redistribute3(info
, parent
, l
, c
, r
, left
, center
, right
,
367 nr_left
, nr_center
, nr_right
);
370 static int rebalance3(struct shadow_spine
*s
, struct dm_btree_info
*info
,
374 struct btree_node
*parent
= dm_block_data(shadow_current(s
));
375 struct child left
, center
, right
;
378 * FIXME: fill out an array?
380 r
= init_child(info
, parent
, left_index
, &left
);
384 r
= init_child(info
, parent
, left_index
+ 1, ¢er
);
386 exit_child(info
, &left
);
390 r
= init_child(info
, parent
, left_index
+ 2, &right
);
392 exit_child(info
, &left
);
393 exit_child(info
, ¢er
);
397 __rebalance3(info
, parent
, &left
, ¢er
, &right
);
399 r
= exit_child(info
, &left
);
401 exit_child(info
, ¢er
);
402 exit_child(info
, &right
);
406 r
= exit_child(info
, ¢er
);
408 exit_child(info
, &right
);
412 r
= exit_child(info
, &right
);
419 static int get_nr_entries(struct dm_transaction_manager
*tm
,
420 dm_block_t b
, uint32_t *result
)
423 struct dm_block
*block
;
424 struct btree_node
*n
;
426 r
= dm_tm_read_lock(tm
, b
, &btree_node_validator
, &block
);
430 n
= dm_block_data(block
);
431 *result
= le32_to_cpu(n
->header
.nr_entries
);
433 return dm_tm_unlock(tm
, block
);
436 static int rebalance_children(struct shadow_spine
*s
,
437 struct dm_btree_info
*info
, uint64_t key
)
439 int i
, r
, has_left_sibling
, has_right_sibling
;
440 uint32_t child_entries
;
441 struct btree_node
*n
;
443 n
= dm_block_data(shadow_current(s
));
445 if (le32_to_cpu(n
->header
.nr_entries
) == 1) {
446 struct dm_block
*child
;
447 dm_block_t b
= value64(n
, 0);
449 r
= dm_tm_read_lock(info
->tm
, b
, &btree_node_validator
, &child
);
453 memcpy(n
, dm_block_data(child
),
454 dm_bm_block_size(dm_tm_get_bm(info
->tm
)));
455 r
= dm_tm_unlock(info
->tm
, child
);
459 dm_tm_dec(info
->tm
, dm_block_location(child
));
463 i
= lower_bound(n
, key
);
467 r
= get_nr_entries(info
->tm
, value64(n
, i
), &child_entries
);
471 has_left_sibling
= i
> 0;
472 has_right_sibling
= i
< (le32_to_cpu(n
->header
.nr_entries
) - 1);
474 if (!has_left_sibling
)
475 r
= rebalance2(s
, info
, i
);
477 else if (!has_right_sibling
)
478 r
= rebalance2(s
, info
, i
- 1);
481 r
= rebalance3(s
, info
, i
- 1);
486 static int do_leaf(struct btree_node
*n
, uint64_t key
, unsigned *index
)
488 int i
= lower_bound(n
, key
);
491 (i
>= le32_to_cpu(n
->header
.nr_entries
)) ||
492 (le64_to_cpu(n
->keys
[i
]) != key
))
501 * Prepares for removal from one level of the hierarchy. The caller must
502 * call delete_at() to remove the entry at index.
504 static int remove_raw(struct shadow_spine
*s
, struct dm_btree_info
*info
,
505 struct dm_btree_value_type
*vt
, dm_block_t root
,
506 uint64_t key
, unsigned *index
)
509 struct btree_node
*n
;
512 r
= shadow_step(s
, root
, vt
);
517 * We have to patch up the parent node, ugly, but I don't
518 * see a way to do this automatically as part of the spine
521 if (shadow_has_parent(s
)) {
522 __le64 location
= cpu_to_le64(dm_block_location(shadow_current(s
)));
523 memcpy(value_ptr(dm_block_data(shadow_parent(s
)), i
),
524 &location
, sizeof(__le64
));
527 n
= dm_block_data(shadow_current(s
));
529 if (le32_to_cpu(n
->header
.flags
) & LEAF_NODE
)
530 return do_leaf(n
, key
, index
);
532 r
= rebalance_children(s
, info
, key
);
536 n
= dm_block_data(shadow_current(s
));
537 if (le32_to_cpu(n
->header
.flags
) & LEAF_NODE
)
538 return do_leaf(n
, key
, index
);
540 i
= lower_bound(n
, key
);
543 * We know the key is present, or else
544 * rebalance_children would have returned
547 root
= value64(n
, i
);
553 int dm_btree_remove(struct dm_btree_info
*info
, dm_block_t root
,
554 uint64_t *keys
, dm_block_t
*new_root
)
556 unsigned level
, last_level
= info
->levels
- 1;
557 int index
= 0, r
= 0;
558 struct shadow_spine spine
;
559 struct btree_node
*n
;
561 init_shadow_spine(&spine
, info
);
562 for (level
= 0; level
< info
->levels
; level
++) {
563 r
= remove_raw(&spine
, info
,
564 (level
== last_level
?
565 &info
->value_type
: &le64_type
),
566 root
, keys
[level
], (unsigned *)&index
);
570 n
= dm_block_data(shadow_current(&spine
));
571 if (level
!= last_level
) {
572 root
= value64(n
, index
);
576 BUG_ON(index
< 0 || index
>= le32_to_cpu(n
->header
.nr_entries
));
578 if (info
->value_type
.dec
)
579 info
->value_type
.dec(info
->value_type
.context
,
580 value_ptr(n
, index
));
585 *new_root
= shadow_root(&spine
);
586 exit_shadow_spine(&spine
);
590 EXPORT_SYMBOL_GPL(dm_btree_remove
);