1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This file is released under the GPL.
8 #include "dm-btree-internal.h"
9 #include "dm-space-map.h"
10 #include "dm-transaction-manager.h"
12 #include <linux/export.h>
13 #include <linux/device-mapper.h>
15 #define DM_MSG_PREFIX "btree"
18 *--------------------------------------------------------------
20 *--------------------------------------------------------------
22 static void memcpy_disk(void *dest
, const void *src
, size_t len
)
23 __dm_written_to_disk(src
)
25 memcpy(dest
, src
, len
);
26 __dm_unbless_for_disk(src
);
29 static void array_insert(void *base
, size_t elt_size
, unsigned int nr_elts
,
30 unsigned int index
, void *elt
)
31 __dm_written_to_disk(elt
)
34 memmove(base
+ (elt_size
* (index
+ 1)),
35 base
+ (elt_size
* index
),
36 (nr_elts
- index
) * elt_size
);
38 memcpy_disk(base
+ (elt_size
* index
), elt
, elt_size
);
41 /*----------------------------------------------------------------*/
43 /* makes the assumption that no two keys are the same. */
44 static int bsearch(struct btree_node
*n
, uint64_t key
, int want_hi
)
46 int lo
= -1, hi
= le32_to_cpu(n
->header
.nr_entries
);
49 int mid
= lo
+ ((hi
- lo
) / 2);
50 uint64_t mid_key
= le64_to_cpu(n
->keys
[mid
]);
61 return want_hi
? hi
: lo
;
64 int lower_bound(struct btree_node
*n
, uint64_t key
)
66 return bsearch(n
, key
, 0);
69 static int upper_bound(struct btree_node
*n
, uint64_t key
)
71 return bsearch(n
, key
, 1);
74 void inc_children(struct dm_transaction_manager
*tm
, struct btree_node
*n
,
75 struct dm_btree_value_type
*vt
)
77 uint32_t nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
79 if (le32_to_cpu(n
->header
.flags
) & INTERNAL_NODE
)
80 dm_tm_with_runs(tm
, value_ptr(n
, 0), nr_entries
, dm_tm_inc_range
);
83 vt
->inc(vt
->context
, value_ptr(n
, 0), nr_entries
);
86 static int insert_at(size_t value_size
, struct btree_node
*node
, unsigned int index
,
87 uint64_t key
, void *value
)
88 __dm_written_to_disk(value
)
90 uint32_t nr_entries
= le32_to_cpu(node
->header
.nr_entries
);
91 uint32_t max_entries
= le32_to_cpu(node
->header
.max_entries
);
92 __le64 key_le
= cpu_to_le64(key
);
94 if (index
> nr_entries
||
95 index
>= max_entries
||
96 nr_entries
>= max_entries
) {
97 DMERR("too many entries in btree node for insert");
98 __dm_unbless_for_disk(value
);
102 __dm_bless_for_disk(&key_le
);
104 array_insert(node
->keys
, sizeof(*node
->keys
), nr_entries
, index
, &key_le
);
105 array_insert(value_base(node
), value_size
, nr_entries
, index
, value
);
106 node
->header
.nr_entries
= cpu_to_le32(nr_entries
+ 1);
111 /*----------------------------------------------------------------*/
114 * We want 3n entries (for some n). This works more nicely for repeated
115 * insert remove loops than (2n + 1).
117 static uint32_t calc_max_entries(size_t value_size
, size_t block_size
)
120 size_t elt_size
= sizeof(uint64_t) + value_size
; /* key + value */
122 block_size
-= sizeof(struct node_header
);
123 total
= block_size
/ elt_size
;
124 n
= total
/ 3; /* rounds down */
129 int dm_btree_empty(struct dm_btree_info
*info
, dm_block_t
*root
)
133 struct btree_node
*n
;
135 uint32_t max_entries
;
137 r
= new_block(info
, &b
);
141 block_size
= dm_bm_block_size(dm_tm_get_bm(info
->tm
));
142 max_entries
= calc_max_entries(info
->value_type
.size
, block_size
);
144 n
= dm_block_data(b
);
145 memset(n
, 0, block_size
);
146 n
->header
.flags
= cpu_to_le32(LEAF_NODE
);
147 n
->header
.nr_entries
= cpu_to_le32(0);
148 n
->header
.max_entries
= cpu_to_le32(max_entries
);
149 n
->header
.value_size
= cpu_to_le32(info
->value_type
.size
);
151 *root
= dm_block_location(b
);
152 unlock_block(info
, b
);
156 EXPORT_SYMBOL_GPL(dm_btree_empty
);
158 /*----------------------------------------------------------------*/
161 * Deletion uses a recursive algorithm, since we have limited stack space
162 * we explicitly manage our own stack on the heap.
164 #define MAX_SPINE_DEPTH 64
167 struct btree_node
*n
;
169 unsigned int nr_children
;
170 unsigned int current_child
;
174 struct dm_btree_info
*info
;
175 struct dm_transaction_manager
*tm
;
177 struct frame spine
[MAX_SPINE_DEPTH
];
180 static int top_frame(struct del_stack
*s
, struct frame
**f
)
183 DMERR("btree deletion stack empty");
187 *f
= s
->spine
+ s
->top
;
192 static int unprocessed_frames(struct del_stack
*s
)
197 static void prefetch_children(struct del_stack
*s
, struct frame
*f
)
200 struct dm_block_manager
*bm
= dm_tm_get_bm(s
->tm
);
202 for (i
= 0; i
< f
->nr_children
; i
++)
203 dm_bm_prefetch(bm
, value64(f
->n
, i
));
206 static bool is_internal_level(struct dm_btree_info
*info
, struct frame
*f
)
208 return f
->level
< (info
->levels
- 1);
211 static int push_frame(struct del_stack
*s
, dm_block_t b
, unsigned int level
)
216 if (s
->top
>= MAX_SPINE_DEPTH
- 1) {
217 DMERR("btree deletion stack out of memory");
221 r
= dm_tm_ref(s
->tm
, b
, &ref_count
);
227 * This is a shared node, so we can just decrement it's
228 * reference counter and leave the children.
234 struct frame
*f
= s
->spine
+ ++s
->top
;
236 r
= dm_tm_read_lock(s
->tm
, b
, &btree_node_validator
, &f
->b
);
242 f
->n
= dm_block_data(f
->b
);
244 f
->nr_children
= le32_to_cpu(f
->n
->header
.nr_entries
);
245 f
->current_child
= 0;
247 flags
= le32_to_cpu(f
->n
->header
.flags
);
248 if (flags
& INTERNAL_NODE
|| is_internal_level(s
->info
, f
))
249 prefetch_children(s
, f
);
255 static void pop_frame(struct del_stack
*s
)
257 struct frame
*f
= s
->spine
+ s
->top
--;
259 dm_tm_dec(s
->tm
, dm_block_location(f
->b
));
260 dm_tm_unlock(s
->tm
, f
->b
);
263 static void unlock_all_frames(struct del_stack
*s
)
267 while (unprocessed_frames(s
)) {
268 f
= s
->spine
+ s
->top
--;
269 dm_tm_unlock(s
->tm
, f
->b
);
273 int dm_btree_del(struct dm_btree_info
*info
, dm_block_t root
)
279 * dm_btree_del() is called via an ioctl, as such should be
280 * considered an FS op. We can't recurse back into the FS, so we
283 s
= kmalloc(sizeof(*s
), GFP_NOFS
);
290 r
= push_frame(s
, root
, 0);
294 while (unprocessed_frames(s
)) {
299 r
= top_frame(s
, &f
);
303 if (f
->current_child
>= f
->nr_children
) {
308 flags
= le32_to_cpu(f
->n
->header
.flags
);
309 if (flags
& INTERNAL_NODE
) {
310 b
= value64(f
->n
, f
->current_child
);
312 r
= push_frame(s
, b
, f
->level
);
316 } else if (is_internal_level(info
, f
)) {
317 b
= value64(f
->n
, f
->current_child
);
319 r
= push_frame(s
, b
, f
->level
+ 1);
324 if (info
->value_type
.dec
)
325 info
->value_type
.dec(info
->value_type
.context
,
326 value_ptr(f
->n
, 0), f
->nr_children
);
332 /* cleanup all frames of del_stack */
333 unlock_all_frames(s
);
339 EXPORT_SYMBOL_GPL(dm_btree_del
);
341 /*----------------------------------------------------------------*/
343 static int btree_lookup_raw(struct ro_spine
*s
, dm_block_t block
, uint64_t key
,
344 int (*search_fn
)(struct btree_node
*, uint64_t),
345 uint64_t *result_key
, void *v
, size_t value_size
)
348 uint32_t flags
, nr_entries
;
351 r
= ro_step(s
, block
);
355 i
= search_fn(ro_node(s
), key
);
357 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
358 nr_entries
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
359 if (i
< 0 || i
>= nr_entries
)
362 if (flags
& INTERNAL_NODE
)
363 block
= value64(ro_node(s
), i
);
365 } while (!(flags
& LEAF_NODE
));
367 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
369 memcpy(v
, value_ptr(ro_node(s
), i
), value_size
);
374 int dm_btree_lookup(struct dm_btree_info
*info
, dm_block_t root
,
375 uint64_t *keys
, void *value_le
)
377 unsigned int level
, last_level
= info
->levels
- 1;
380 __le64 internal_value_le
;
381 struct ro_spine spine
;
383 init_ro_spine(&spine
, info
);
384 for (level
= 0; level
< info
->levels
; level
++) {
388 if (level
== last_level
) {
390 size
= info
->value_type
.size
;
393 value_p
= &internal_value_le
;
394 size
= sizeof(uint64_t);
397 r
= btree_lookup_raw(&spine
, root
, keys
[level
],
402 if (rkey
!= keys
[level
]) {
403 exit_ro_spine(&spine
);
407 exit_ro_spine(&spine
);
411 root
= le64_to_cpu(internal_value_le
);
413 exit_ro_spine(&spine
);
417 EXPORT_SYMBOL_GPL(dm_btree_lookup
);
419 static int dm_btree_lookup_next_single(struct dm_btree_info
*info
, dm_block_t root
,
420 uint64_t key
, uint64_t *rkey
, void *value_le
)
423 uint32_t flags
, nr_entries
;
424 struct dm_block
*node
;
425 struct btree_node
*n
;
427 r
= bn_read_lock(info
, root
, &node
);
431 n
= dm_block_data(node
);
432 flags
= le32_to_cpu(n
->header
.flags
);
433 nr_entries
= le32_to_cpu(n
->header
.nr_entries
);
435 if (flags
& INTERNAL_NODE
) {
436 i
= lower_bound(n
, key
);
439 * avoid early -ENODATA return when all entries are
440 * higher than the search @key.
444 if (i
>= nr_entries
) {
449 r
= dm_btree_lookup_next_single(info
, value64(n
, i
), key
, rkey
, value_le
);
450 if (r
== -ENODATA
&& i
< (nr_entries
- 1)) {
452 r
= dm_btree_lookup_next_single(info
, value64(n
, i
), key
, rkey
, value_le
);
456 i
= upper_bound(n
, key
);
457 if (i
< 0 || i
>= nr_entries
) {
462 *rkey
= le64_to_cpu(n
->keys
[i
]);
463 memcpy(value_le
, value_ptr(n
, i
), info
->value_type
.size
);
466 dm_tm_unlock(info
->tm
, node
);
470 int dm_btree_lookup_next(struct dm_btree_info
*info
, dm_block_t root
,
471 uint64_t *keys
, uint64_t *rkey
, void *value_le
)
475 __le64 internal_value_le
;
476 struct ro_spine spine
;
478 init_ro_spine(&spine
, info
);
479 for (level
= 0; level
< info
->levels
- 1u; level
++) {
480 r
= btree_lookup_raw(&spine
, root
, keys
[level
],
482 &internal_value_le
, sizeof(uint64_t));
486 if (*rkey
!= keys
[level
]) {
491 root
= le64_to_cpu(internal_value_le
);
494 r
= dm_btree_lookup_next_single(info
, root
, keys
[level
], rkey
, value_le
);
496 exit_ro_spine(&spine
);
499 EXPORT_SYMBOL_GPL(dm_btree_lookup_next
);
501 /*----------------------------------------------------------------*/
504 * Copies entries from one region of a btree node to another. The regions
507 static void copy_entries(struct btree_node
*dest
, unsigned int dest_offset
,
508 struct btree_node
*src
, unsigned int src_offset
,
511 size_t value_size
= le32_to_cpu(dest
->header
.value_size
);
513 memcpy(dest
->keys
+ dest_offset
, src
->keys
+ src_offset
, count
* sizeof(uint64_t));
514 memcpy(value_ptr(dest
, dest_offset
), value_ptr(src
, src_offset
), count
* value_size
);
518 * Moves entries from one region fo a btree node to another. The regions
521 static void move_entries(struct btree_node
*dest
, unsigned int dest_offset
,
522 struct btree_node
*src
, unsigned int src_offset
,
525 size_t value_size
= le32_to_cpu(dest
->header
.value_size
);
527 memmove(dest
->keys
+ dest_offset
, src
->keys
+ src_offset
, count
* sizeof(uint64_t));
528 memmove(value_ptr(dest
, dest_offset
), value_ptr(src
, src_offset
), count
* value_size
);
532 * Erases the first 'count' entries of a btree node, shifting following
533 * entries down into their place.
535 static void shift_down(struct btree_node
*n
, unsigned int count
)
537 move_entries(n
, 0, n
, count
, le32_to_cpu(n
->header
.nr_entries
) - count
);
541 * Moves entries in a btree node up 'count' places, making space for
542 * new entries at the start of the node.
544 static void shift_up(struct btree_node
*n
, unsigned int count
)
546 move_entries(n
, count
, n
, 0, le32_to_cpu(n
->header
.nr_entries
));
550 * Redistributes entries between two btree nodes to make them
551 * have similar numbers of entries.
553 static void redistribute2(struct btree_node
*left
, struct btree_node
*right
)
555 unsigned int nr_left
= le32_to_cpu(left
->header
.nr_entries
);
556 unsigned int nr_right
= le32_to_cpu(right
->header
.nr_entries
);
557 unsigned int total
= nr_left
+ nr_right
;
558 unsigned int target_left
= total
/ 2;
559 unsigned int target_right
= total
- target_left
;
561 if (nr_left
< target_left
) {
562 unsigned int delta
= target_left
- nr_left
;
564 copy_entries(left
, nr_left
, right
, 0, delta
);
565 shift_down(right
, delta
);
566 } else if (nr_left
> target_left
) {
567 unsigned int delta
= nr_left
- target_left
;
570 shift_up(right
, delta
);
571 copy_entries(right
, 0, left
, target_left
, delta
);
574 left
->header
.nr_entries
= cpu_to_le32(target_left
);
575 right
->header
.nr_entries
= cpu_to_le32(target_right
);
579 * Redistribute entries between three nodes. Assumes the central
582 static void redistribute3(struct btree_node
*left
, struct btree_node
*center
,
583 struct btree_node
*right
)
585 unsigned int nr_left
= le32_to_cpu(left
->header
.nr_entries
);
586 unsigned int nr_center
= le32_to_cpu(center
->header
.nr_entries
);
587 unsigned int nr_right
= le32_to_cpu(right
->header
.nr_entries
);
588 unsigned int total
, target_left
, target_center
, target_right
;
592 total
= nr_left
+ nr_right
;
593 target_left
= total
/ 3;
594 target_center
= (total
- target_left
) / 2;
595 target_right
= (total
- target_left
- target_center
);
597 if (nr_left
< target_left
) {
598 unsigned int left_short
= target_left
- nr_left
;
600 copy_entries(left
, nr_left
, right
, 0, left_short
);
601 copy_entries(center
, 0, right
, left_short
, target_center
);
602 shift_down(right
, nr_right
- target_right
);
604 } else if (nr_left
< (target_left
+ target_center
)) {
605 unsigned int left_to_center
= nr_left
- target_left
;
607 copy_entries(center
, 0, left
, target_left
, left_to_center
);
608 copy_entries(center
, left_to_center
, right
, 0, target_center
- left_to_center
);
609 shift_down(right
, nr_right
- target_right
);
612 unsigned int right_short
= target_right
- nr_right
;
614 shift_up(right
, right_short
);
615 copy_entries(right
, 0, left
, nr_left
- right_short
, right_short
);
616 copy_entries(center
, 0, left
, target_left
, nr_left
- target_left
);
619 left
->header
.nr_entries
= cpu_to_le32(target_left
);
620 center
->header
.nr_entries
= cpu_to_le32(target_center
);
621 right
->header
.nr_entries
= cpu_to_le32(target_right
);
625 * Splits a node by creating a sibling node and shifting half the nodes
626 * contents across. Assumes there is a parent node, and it has room for
648 * +---------+ +-------+
652 * Where A* is a shadow of A.
654 static int split_one_into_two(struct shadow_spine
*s
, unsigned int parent_index
,
655 struct dm_btree_value_type
*vt
, uint64_t key
)
658 struct dm_block
*left
, *right
, *parent
;
659 struct btree_node
*ln
, *rn
, *pn
;
662 left
= shadow_current(s
);
664 r
= new_block(s
->info
, &right
);
668 ln
= dm_block_data(left
);
669 rn
= dm_block_data(right
);
671 rn
->header
.flags
= ln
->header
.flags
;
672 rn
->header
.nr_entries
= cpu_to_le32(0);
673 rn
->header
.max_entries
= ln
->header
.max_entries
;
674 rn
->header
.value_size
= ln
->header
.value_size
;
675 redistribute2(ln
, rn
);
677 /* patch up the parent */
678 parent
= shadow_parent(s
);
679 pn
= dm_block_data(parent
);
681 location
= cpu_to_le64(dm_block_location(right
));
682 __dm_bless_for_disk(&location
);
683 r
= insert_at(sizeof(__le64
), pn
, parent_index
+ 1,
684 le64_to_cpu(rn
->keys
[0]), &location
);
686 unlock_block(s
->info
, right
);
690 /* patch up the spine */
691 if (key
< le64_to_cpu(rn
->keys
[0])) {
692 unlock_block(s
->info
, right
);
695 unlock_block(s
->info
, left
);
703 * We often need to modify a sibling node. This function shadows a particular
704 * child of the given parent node. Making sure to update the parent to point
707 static int shadow_child(struct dm_btree_info
*info
, struct dm_btree_value_type
*vt
,
708 struct btree_node
*parent
, unsigned int index
,
709 struct dm_block
**result
)
713 struct btree_node
*node
;
715 root
= value64(parent
, index
);
717 r
= dm_tm_shadow_block(info
->tm
, root
, &btree_node_validator
,
722 node
= dm_block_data(*result
);
725 inc_children(info
->tm
, node
, vt
);
727 *((__le64
*) value_ptr(parent
, index
)) =
728 cpu_to_le64(dm_block_location(*result
));
734 * Splits two nodes into three. This is more work, but results in fuller
735 * nodes, so saves metadata space.
737 static int split_two_into_three(struct shadow_spine
*s
, unsigned int parent_index
,
738 struct dm_btree_value_type
*vt
, uint64_t key
)
741 unsigned int middle_index
;
742 struct dm_block
*left
, *middle
, *right
, *parent
;
743 struct btree_node
*ln
, *rn
, *mn
, *pn
;
746 parent
= shadow_parent(s
);
747 pn
= dm_block_data(parent
);
749 if (parent_index
== 0) {
751 left
= shadow_current(s
);
752 r
= shadow_child(s
->info
, vt
, pn
, parent_index
+ 1, &right
);
756 middle_index
= parent_index
;
757 right
= shadow_current(s
);
758 r
= shadow_child(s
->info
, vt
, pn
, parent_index
- 1, &left
);
763 r
= new_block(s
->info
, &middle
);
767 ln
= dm_block_data(left
);
768 mn
= dm_block_data(middle
);
769 rn
= dm_block_data(right
);
771 mn
->header
.nr_entries
= cpu_to_le32(0);
772 mn
->header
.flags
= ln
->header
.flags
;
773 mn
->header
.max_entries
= ln
->header
.max_entries
;
774 mn
->header
.value_size
= ln
->header
.value_size
;
776 redistribute3(ln
, mn
, rn
);
778 /* patch up the parent */
779 pn
->keys
[middle_index
] = rn
->keys
[0];
780 location
= cpu_to_le64(dm_block_location(middle
));
781 __dm_bless_for_disk(&location
);
782 r
= insert_at(sizeof(__le64
), pn
, middle_index
,
783 le64_to_cpu(mn
->keys
[0]), &location
);
785 if (shadow_current(s
) != left
)
786 unlock_block(s
->info
, left
);
788 unlock_block(s
->info
, middle
);
790 if (shadow_current(s
) != right
)
791 unlock_block(s
->info
, right
);
797 /* patch up the spine */
798 if (key
< le64_to_cpu(mn
->keys
[0])) {
799 unlock_block(s
->info
, middle
);
800 unlock_block(s
->info
, right
);
802 } else if (key
< le64_to_cpu(rn
->keys
[0])) {
803 unlock_block(s
->info
, left
);
804 unlock_block(s
->info
, right
);
805 s
->nodes
[1] = middle
;
807 unlock_block(s
->info
, left
);
808 unlock_block(s
->info
, middle
);
815 /*----------------------------------------------------------------*/
818 * Splits a node by creating two new children beneath the given node.
834 * +-------+ +-------+
835 * | B +++ | | C +++ |
836 * +-------+ +-------+
838 static int btree_split_beneath(struct shadow_spine
*s
, uint64_t key
)
842 unsigned int nr_left
, nr_right
;
843 struct dm_block
*left
, *right
, *new_parent
;
844 struct btree_node
*pn
, *ln
, *rn
;
847 new_parent
= shadow_current(s
);
849 pn
= dm_block_data(new_parent
);
850 size
= le32_to_cpu(pn
->header
.flags
) & INTERNAL_NODE
?
851 sizeof(__le64
) : s
->info
->value_type
.size
;
853 /* create & init the left block */
854 r
= new_block(s
->info
, &left
);
858 ln
= dm_block_data(left
);
859 nr_left
= le32_to_cpu(pn
->header
.nr_entries
) / 2;
861 ln
->header
.flags
= pn
->header
.flags
;
862 ln
->header
.nr_entries
= cpu_to_le32(nr_left
);
863 ln
->header
.max_entries
= pn
->header
.max_entries
;
864 ln
->header
.value_size
= pn
->header
.value_size
;
865 memcpy(ln
->keys
, pn
->keys
, nr_left
* sizeof(pn
->keys
[0]));
866 memcpy(value_ptr(ln
, 0), value_ptr(pn
, 0), nr_left
* size
);
868 /* create & init the right block */
869 r
= new_block(s
->info
, &right
);
871 unlock_block(s
->info
, left
);
875 rn
= dm_block_data(right
);
876 nr_right
= le32_to_cpu(pn
->header
.nr_entries
) - nr_left
;
878 rn
->header
.flags
= pn
->header
.flags
;
879 rn
->header
.nr_entries
= cpu_to_le32(nr_right
);
880 rn
->header
.max_entries
= pn
->header
.max_entries
;
881 rn
->header
.value_size
= pn
->header
.value_size
;
882 memcpy(rn
->keys
, pn
->keys
+ nr_left
, nr_right
* sizeof(pn
->keys
[0]));
883 memcpy(value_ptr(rn
, 0), value_ptr(pn
, nr_left
),
886 /* new_parent should just point to l and r now */
887 pn
->header
.flags
= cpu_to_le32(INTERNAL_NODE
);
888 pn
->header
.nr_entries
= cpu_to_le32(2);
889 pn
->header
.max_entries
= cpu_to_le32(
890 calc_max_entries(sizeof(__le64
),
892 dm_tm_get_bm(s
->info
->tm
))));
893 pn
->header
.value_size
= cpu_to_le32(sizeof(__le64
));
895 val
= cpu_to_le64(dm_block_location(left
));
896 __dm_bless_for_disk(&val
);
897 pn
->keys
[0] = ln
->keys
[0];
898 memcpy_disk(value_ptr(pn
, 0), &val
, sizeof(__le64
));
900 val
= cpu_to_le64(dm_block_location(right
));
901 __dm_bless_for_disk(&val
);
902 pn
->keys
[1] = rn
->keys
[0];
903 memcpy_disk(value_ptr(pn
, 1), &val
, sizeof(__le64
));
905 unlock_block(s
->info
, left
);
906 unlock_block(s
->info
, right
);
910 /*----------------------------------------------------------------*/
913 * Redistributes a node's entries with its left sibling.
915 static int rebalance_left(struct shadow_spine
*s
, struct dm_btree_value_type
*vt
,
916 unsigned int parent_index
, uint64_t key
)
919 struct dm_block
*sib
;
920 struct btree_node
*left
, *right
, *parent
= dm_block_data(shadow_parent(s
));
922 r
= shadow_child(s
->info
, vt
, parent
, parent_index
- 1, &sib
);
926 left
= dm_block_data(sib
);
927 right
= dm_block_data(shadow_current(s
));
928 redistribute2(left
, right
);
929 *key_ptr(parent
, parent_index
) = right
->keys
[0];
931 if (key
< le64_to_cpu(right
->keys
[0])) {
932 unlock_block(s
->info
, s
->nodes
[1]);
935 unlock_block(s
->info
, sib
);
942 * Redistributes a nodes entries with its right sibling.
944 static int rebalance_right(struct shadow_spine
*s
, struct dm_btree_value_type
*vt
,
945 unsigned int parent_index
, uint64_t key
)
948 struct dm_block
*sib
;
949 struct btree_node
*left
, *right
, *parent
= dm_block_data(shadow_parent(s
));
951 r
= shadow_child(s
->info
, vt
, parent
, parent_index
+ 1, &sib
);
955 left
= dm_block_data(shadow_current(s
));
956 right
= dm_block_data(sib
);
957 redistribute2(left
, right
);
958 *key_ptr(parent
, parent_index
+ 1) = right
->keys
[0];
960 if (key
< le64_to_cpu(right
->keys
[0])) {
961 unlock_block(s
->info
, sib
);
963 unlock_block(s
->info
, s
->nodes
[1]);
971 * Returns the number of spare entries in a node.
973 static int get_node_free_space(struct dm_btree_info
*info
, dm_block_t b
, unsigned int *space
)
976 unsigned int nr_entries
;
977 struct dm_block
*block
;
978 struct btree_node
*node
;
980 r
= bn_read_lock(info
, b
, &block
);
984 node
= dm_block_data(block
);
985 nr_entries
= le32_to_cpu(node
->header
.nr_entries
);
986 *space
= le32_to_cpu(node
->header
.max_entries
) - nr_entries
;
988 unlock_block(info
, block
);
993 * Make space in a node, either by moving some entries to a sibling,
994 * or creating a new sibling node. SPACE_THRESHOLD defines the minimum
995 * number of free entries that must be in the sibling to make the move
996 * worth while. If the siblings are shared (eg, part of a snapshot),
997 * then they are not touched, since this break sharing and so consume
998 * more space than we save.
1000 #define SPACE_THRESHOLD 8
1001 static int rebalance_or_split(struct shadow_spine
*s
, struct dm_btree_value_type
*vt
,
1002 unsigned int parent_index
, uint64_t key
)
1005 struct btree_node
*parent
= dm_block_data(shadow_parent(s
));
1006 unsigned int nr_parent
= le32_to_cpu(parent
->header
.nr_entries
);
1007 unsigned int free_space
;
1008 int left_shared
= 0, right_shared
= 0;
1010 /* Should we move entries to the left sibling? */
1011 if (parent_index
> 0) {
1012 dm_block_t left_b
= value64(parent
, parent_index
- 1);
1014 r
= dm_tm_block_is_shared(s
->info
->tm
, left_b
, &left_shared
);
1019 r
= get_node_free_space(s
->info
, left_b
, &free_space
);
1023 if (free_space
>= SPACE_THRESHOLD
)
1024 return rebalance_left(s
, vt
, parent_index
, key
);
1028 /* Should we move entries to the right sibling? */
1029 if (parent_index
< (nr_parent
- 1)) {
1030 dm_block_t right_b
= value64(parent
, parent_index
+ 1);
1032 r
= dm_tm_block_is_shared(s
->info
->tm
, right_b
, &right_shared
);
1036 if (!right_shared
) {
1037 r
= get_node_free_space(s
->info
, right_b
, &free_space
);
1041 if (free_space
>= SPACE_THRESHOLD
)
1042 return rebalance_right(s
, vt
, parent_index
, key
);
1047 * We need to split the node, normally we split two nodes
1048 * into three. But when inserting a sequence that is either
1049 * monotonically increasing or decreasing it's better to split
1050 * a single node into two.
1052 if (left_shared
|| right_shared
|| (nr_parent
<= 2) ||
1053 (parent_index
== 0) || (parent_index
+ 1 == nr_parent
)) {
1054 return split_one_into_two(s
, parent_index
, vt
, key
);
1056 return split_two_into_three(s
, parent_index
, vt
, key
);
1061 * Does the node contain a particular key?
1063 static bool contains_key(struct btree_node
*node
, uint64_t key
)
1065 int i
= lower_bound(node
, key
);
1067 if (i
>= 0 && le64_to_cpu(node
->keys
[i
]) == key
)
1074 * In general we preemptively make sure there's a free entry in every
1075 * node on the spine when doing an insert. But we can avoid that with
1076 * leaf nodes if we know it's an overwrite.
1078 static bool has_space_for_insert(struct btree_node
*node
, uint64_t key
)
1080 if (node
->header
.nr_entries
== node
->header
.max_entries
) {
1081 if (le32_to_cpu(node
->header
.flags
) & LEAF_NODE
) {
1082 /* we don't need space if it's an overwrite */
1083 return contains_key(node
, key
);
1092 static int btree_insert_raw(struct shadow_spine
*s
, dm_block_t root
,
1093 struct dm_btree_value_type
*vt
,
1094 uint64_t key
, unsigned int *index
)
1096 int r
, i
= *index
, top
= 1;
1097 struct btree_node
*node
;
1100 r
= shadow_step(s
, root
, vt
);
1104 node
= dm_block_data(shadow_current(s
));
1107 * We have to patch up the parent node, ugly, but I don't
1108 * see a way to do this automatically as part of the spine
1111 if (shadow_has_parent(s
) && i
>= 0) { /* FIXME: second clause unness. */
1112 __le64 location
= cpu_to_le64(dm_block_location(shadow_current(s
)));
1114 __dm_bless_for_disk(&location
);
1115 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s
)), i
),
1116 &location
, sizeof(__le64
));
1119 node
= dm_block_data(shadow_current(s
));
1121 if (!has_space_for_insert(node
, key
)) {
1123 r
= btree_split_beneath(s
, key
);
1125 r
= rebalance_or_split(s
, vt
, i
, key
);
1130 /* making space can cause the current node to change */
1131 node
= dm_block_data(shadow_current(s
));
1134 i
= lower_bound(node
, key
);
1136 if (le32_to_cpu(node
->header
.flags
) & LEAF_NODE
)
1140 /* change the bounds on the lowest key */
1141 node
->keys
[0] = cpu_to_le64(key
);
1145 root
= value64(node
, i
);
1149 if (i
< 0 || le64_to_cpu(node
->keys
[i
]) != key
)
1156 static int __btree_get_overwrite_leaf(struct shadow_spine
*s
, dm_block_t root
,
1157 uint64_t key
, int *index
)
1160 struct btree_node
*node
;
1164 r
= shadow_step(s
, root
, &s
->info
->value_type
);
1168 node
= dm_block_data(shadow_current(s
));
1171 * We have to patch up the parent node, ugly, but I don't
1172 * see a way to do this automatically as part of the spine
1175 if (shadow_has_parent(s
) && i
>= 0) {
1176 __le64 location
= cpu_to_le64(dm_block_location(shadow_current(s
)));
1178 __dm_bless_for_disk(&location
);
1179 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s
)), i
),
1180 &location
, sizeof(__le64
));
1183 node
= dm_block_data(shadow_current(s
));
1184 i
= lower_bound(node
, key
);
1187 BUG_ON(i
>= le32_to_cpu(node
->header
.nr_entries
));
1189 if (le32_to_cpu(node
->header
.flags
) & LEAF_NODE
) {
1190 if (key
!= le64_to_cpu(node
->keys
[i
]))
1195 root
= value64(node
, i
);
1202 int btree_get_overwrite_leaf(struct dm_btree_info
*info
, dm_block_t root
,
1203 uint64_t key
, int *index
,
1204 dm_block_t
*new_root
, struct dm_block
**leaf
)
1207 struct shadow_spine spine
;
1209 BUG_ON(info
->levels
> 1);
1210 init_shadow_spine(&spine
, info
);
1211 r
= __btree_get_overwrite_leaf(&spine
, root
, key
, index
);
1213 *new_root
= shadow_root(&spine
);
1214 *leaf
= shadow_current(&spine
);
1217 * Decrement the count so exit_shadow_spine() doesn't
1222 exit_shadow_spine(&spine
);
1227 static bool need_insert(struct btree_node
*node
, uint64_t *keys
,
1228 unsigned int level
, unsigned int index
)
1230 return ((index
>= le32_to_cpu(node
->header
.nr_entries
)) ||
1231 (le64_to_cpu(node
->keys
[index
]) != keys
[level
]));
1234 static int insert(struct dm_btree_info
*info
, dm_block_t root
,
1235 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
1237 __dm_written_to_disk(value
)
1240 unsigned int level
, index
= -1, last_level
= info
->levels
- 1;
1241 dm_block_t block
= root
;
1242 struct shadow_spine spine
;
1243 struct btree_node
*n
;
1244 struct dm_btree_value_type le64_type
;
1246 init_le64_type(info
->tm
, &le64_type
);
1247 init_shadow_spine(&spine
, info
);
1249 for (level
= 0; level
< (info
->levels
- 1); level
++) {
1250 r
= btree_insert_raw(&spine
, block
, &le64_type
, keys
[level
], &index
);
1254 n
= dm_block_data(shadow_current(&spine
));
1256 if (need_insert(n
, keys
, level
, index
)) {
1257 dm_block_t new_tree
;
1260 r
= dm_btree_empty(info
, &new_tree
);
1264 new_le
= cpu_to_le64(new_tree
);
1265 __dm_bless_for_disk(&new_le
);
1267 r
= insert_at(sizeof(uint64_t), n
, index
,
1268 keys
[level
], &new_le
);
1273 if (level
< last_level
)
1274 block
= value64(n
, index
);
1277 r
= btree_insert_raw(&spine
, block
, &info
->value_type
,
1278 keys
[level
], &index
);
1282 n
= dm_block_data(shadow_current(&spine
));
1284 if (need_insert(n
, keys
, level
, index
)) {
1288 r
= insert_at(info
->value_type
.size
, n
, index
,
1289 keys
[level
], value
);
1296 if (info
->value_type
.dec
&&
1297 (!info
->value_type
.equal
||
1298 !info
->value_type
.equal(
1299 info
->value_type
.context
,
1300 value_ptr(n
, index
),
1302 info
->value_type
.dec(info
->value_type
.context
,
1303 value_ptr(n
, index
), 1);
1305 memcpy_disk(value_ptr(n
, index
),
1306 value
, info
->value_type
.size
);
1309 *new_root
= shadow_root(&spine
);
1310 exit_shadow_spine(&spine
);
1315 __dm_unbless_for_disk(value
);
1317 exit_shadow_spine(&spine
);
1321 int dm_btree_insert(struct dm_btree_info
*info
, dm_block_t root
,
1322 uint64_t *keys
, void *value
, dm_block_t
*new_root
)
1323 __dm_written_to_disk(value
)
1325 return insert(info
, root
, keys
, value
, new_root
, NULL
);
1327 EXPORT_SYMBOL_GPL(dm_btree_insert
);
1329 int dm_btree_insert_notify(struct dm_btree_info
*info
, dm_block_t root
,
1330 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
1332 __dm_written_to_disk(value
)
1334 return insert(info
, root
, keys
, value
, new_root
, inserted
);
1336 EXPORT_SYMBOL_GPL(dm_btree_insert_notify
);
1338 /*----------------------------------------------------------------*/
1340 static int find_key(struct ro_spine
*s
, dm_block_t block
, bool find_highest
,
1341 uint64_t *result_key
, dm_block_t
*next_block
)
1347 r
= ro_step(s
, block
);
1351 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
1352 i
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
1359 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
1361 *result_key
= le64_to_cpu(ro_node(s
)->keys
[0]);
1363 if (next_block
|| flags
& INTERNAL_NODE
) {
1365 block
= value64(ro_node(s
), i
);
1367 block
= value64(ro_node(s
), 0);
1370 } while (flags
& INTERNAL_NODE
);
1373 *next_block
= block
;
1377 static int dm_btree_find_key(struct dm_btree_info
*info
, dm_block_t root
,
1378 bool find_highest
, uint64_t *result_keys
)
1380 int r
= 0, count
= 0, level
;
1381 struct ro_spine spine
;
1383 init_ro_spine(&spine
, info
);
1384 for (level
= 0; level
< info
->levels
; level
++) {
1385 r
= find_key(&spine
, root
, find_highest
, result_keys
+ level
,
1386 level
== info
->levels
- 1 ? NULL
: &root
);
1387 if (r
== -ENODATA
) {
1396 exit_ro_spine(&spine
);
1398 return r
? r
: count
;
1401 int dm_btree_find_highest_key(struct dm_btree_info
*info
, dm_block_t root
,
1402 uint64_t *result_keys
)
1404 return dm_btree_find_key(info
, root
, true, result_keys
);
1406 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key
);
1408 int dm_btree_find_lowest_key(struct dm_btree_info
*info
, dm_block_t root
,
1409 uint64_t *result_keys
)
1411 return dm_btree_find_key(info
, root
, false, result_keys
);
1413 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key
);
1415 /*----------------------------------------------------------------*/
1418 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
1419 * space. Also this only works for single level trees.
1421 static int walk_node(struct dm_btree_info
*info
, dm_block_t block
,
1422 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
1427 struct dm_block
*node
;
1428 struct btree_node
*n
;
1431 r
= bn_read_lock(info
, block
, &node
);
1435 n
= dm_block_data(node
);
1437 nr
= le32_to_cpu(n
->header
.nr_entries
);
1438 for (i
= 0; i
< nr
; i
++) {
1439 if (le32_to_cpu(n
->header
.flags
) & INTERNAL_NODE
) {
1440 r
= walk_node(info
, value64(n
, i
), fn
, context
);
1444 keys
= le64_to_cpu(*key_ptr(n
, i
));
1445 r
= fn(context
, &keys
, value_ptr(n
, i
));
1452 dm_tm_unlock(info
->tm
, node
);
1456 int dm_btree_walk(struct dm_btree_info
*info
, dm_block_t root
,
1457 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
1460 BUG_ON(info
->levels
> 1);
1461 return walk_node(info
, root
, fn
, context
);
1463 EXPORT_SYMBOL_GPL(dm_btree_walk
);
1465 /*----------------------------------------------------------------*/
1467 static void prefetch_values(struct dm_btree_cursor
*c
)
1471 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1472 struct btree_node
*bn
= dm_block_data(n
->b
);
1473 struct dm_block_manager
*bm
= dm_tm_get_bm(c
->info
->tm
);
1475 BUG_ON(c
->info
->value_type
.size
!= sizeof(value_le
));
1477 nr
= le32_to_cpu(bn
->header
.nr_entries
);
1478 for (i
= 0; i
< nr
; i
++) {
1479 memcpy(&value_le
, value_ptr(bn
, i
), sizeof(value_le
));
1480 dm_bm_prefetch(bm
, le64_to_cpu(value_le
));
1484 static bool leaf_node(struct dm_btree_cursor
*c
)
1486 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1487 struct btree_node
*bn
= dm_block_data(n
->b
);
1489 return le32_to_cpu(bn
->header
.flags
) & LEAF_NODE
;
1492 static int push_node(struct dm_btree_cursor
*c
, dm_block_t b
)
1495 struct cursor_node
*n
= c
->nodes
+ c
->depth
;
1497 if (c
->depth
>= DM_BTREE_CURSOR_MAX_DEPTH
- 1) {
1498 DMERR("couldn't push cursor node, stack depth too high");
1502 r
= bn_read_lock(c
->info
, b
, &n
->b
);
1509 if (c
->prefetch_leaves
|| !leaf_node(c
))
1515 static void pop_node(struct dm_btree_cursor
*c
)
1518 unlock_block(c
->info
, c
->nodes
[c
->depth
].b
);
1521 static int inc_or_backtrack(struct dm_btree_cursor
*c
)
1523 struct cursor_node
*n
;
1524 struct btree_node
*bn
;
1530 n
= c
->nodes
+ c
->depth
- 1;
1531 bn
= dm_block_data(n
->b
);
1534 if (n
->index
< le32_to_cpu(bn
->header
.nr_entries
))
1543 static int find_leaf(struct dm_btree_cursor
*c
)
1546 struct cursor_node
*n
;
1547 struct btree_node
*bn
;
1551 n
= c
->nodes
+ c
->depth
- 1;
1552 bn
= dm_block_data(n
->b
);
1554 if (le32_to_cpu(bn
->header
.flags
) & LEAF_NODE
)
1557 memcpy(&value_le
, value_ptr(bn
, n
->index
), sizeof(value_le
));
1558 r
= push_node(c
, le64_to_cpu(value_le
));
1560 DMERR("push_node failed");
1565 if (!r
&& (le32_to_cpu(bn
->header
.nr_entries
) == 0))
1571 int dm_btree_cursor_begin(struct dm_btree_info
*info
, dm_block_t root
,
1572 bool prefetch_leaves
, struct dm_btree_cursor
*c
)
1579 c
->prefetch_leaves
= prefetch_leaves
;
1581 r
= push_node(c
, root
);
1585 return find_leaf(c
);
1587 EXPORT_SYMBOL_GPL(dm_btree_cursor_begin
);
1589 void dm_btree_cursor_end(struct dm_btree_cursor
*c
)
1594 EXPORT_SYMBOL_GPL(dm_btree_cursor_end
);
1596 int dm_btree_cursor_next(struct dm_btree_cursor
*c
)
1598 int r
= inc_or_backtrack(c
);
1603 DMERR("find_leaf failed");
1608 EXPORT_SYMBOL_GPL(dm_btree_cursor_next
);
1610 int dm_btree_cursor_skip(struct dm_btree_cursor
*c
, uint32_t count
)
1614 while (count
-- && !r
)
1615 r
= dm_btree_cursor_next(c
);
1619 EXPORT_SYMBOL_GPL(dm_btree_cursor_skip
);
1621 int dm_btree_cursor_get_value(struct dm_btree_cursor
*c
, uint64_t *key
, void *value_le
)
1624 struct cursor_node
*n
= c
->nodes
+ c
->depth
- 1;
1625 struct btree_node
*bn
= dm_block_data(n
->b
);
1627 if (le32_to_cpu(bn
->header
.flags
) & INTERNAL_NODE
)
1630 *key
= le64_to_cpu(*key_ptr(bn
, n
->index
));
1631 memcpy(value_le
, value_ptr(bn
, n
->index
), c
->info
->value_type
.size
);
1637 EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value
);