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 return unlock_block(info
, b
);
146 EXPORT_SYMBOL_GPL(dm_btree_empty
);
148 /*----------------------------------------------------------------*/
151 * Deletion uses a recursive algorithm, since we have limited stack space
152 * we explicitly manage our own stack on the heap.
154 #define MAX_SPINE_DEPTH 64
157 struct btree_node
*n
;
159 unsigned nr_children
;
160 unsigned current_child
;
164 struct dm_btree_info
*info
;
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 void prefetch_children(struct del_stack
*s
, struct frame
*f
)
190 struct dm_block_manager
*bm
= dm_tm_get_bm(s
->tm
);
192 for (i
= 0; i
< f
->nr_children
; i
++)
193 dm_bm_prefetch(bm
, value64(f
->n
, i
));
196 static bool is_internal_level(struct dm_btree_info
*info
, struct frame
*f
)
198 return f
->level
< (info
->levels
- 1);
201 static int push_frame(struct del_stack
*s
, dm_block_t b
, unsigned level
)
206 if (s
->top
>= MAX_SPINE_DEPTH
- 1) {
207 DMERR("btree deletion stack out of memory");
211 r
= dm_tm_ref(s
->tm
, b
, &ref_count
);
217 * This is a shared node, so we can just decrement it's
218 * reference counter and leave the children.
224 struct frame
*f
= s
->spine
+ ++s
->top
;
226 r
= dm_tm_read_lock(s
->tm
, b
, &btree_node_validator
, &f
->b
);
232 f
->n
= dm_block_data(f
->b
);
234 f
->nr_children
= le32_to_cpu(f
->n
->header
.nr_entries
);
235 f
->current_child
= 0;
237 flags
= le32_to_cpu(f
->n
->header
.flags
);
238 if (flags
& INTERNAL_NODE
|| is_internal_level(s
->info
, f
))
239 prefetch_children(s
, f
);
245 static void pop_frame(struct del_stack
*s
)
247 struct frame
*f
= s
->spine
+ s
->top
--;
249 dm_tm_dec(s
->tm
, dm_block_location(f
->b
));
250 dm_tm_unlock(s
->tm
, f
->b
);
253 int dm_btree_del(struct dm_btree_info
*info
, dm_block_t root
)
258 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
265 r
= push_frame(s
, root
, 0);
269 while (unprocessed_frames(s
)) {
274 r
= top_frame(s
, &f
);
278 if (f
->current_child
>= f
->nr_children
) {
283 flags
= le32_to_cpu(f
->n
->header
.flags
);
284 if (flags
& INTERNAL_NODE
) {
285 b
= value64(f
->n
, f
->current_child
);
287 r
= push_frame(s
, b
, f
->level
);
291 } else if (is_internal_level(info
, f
)) {
292 b
= value64(f
->n
, f
->current_child
);
294 r
= push_frame(s
, b
, f
->level
+ 1);
299 if (info
->value_type
.dec
) {
302 for (i
= 0; i
< f
->nr_children
; i
++)
303 info
->value_type
.dec(info
->value_type
.context
,
314 EXPORT_SYMBOL_GPL(dm_btree_del
);
316 /*----------------------------------------------------------------*/
318 static int btree_lookup_raw(struct ro_spine
*s
, dm_block_t block
, uint64_t key
,
319 int (*search_fn
)(struct btree_node
*, uint64_t),
320 uint64_t *result_key
, void *v
, size_t value_size
)
323 uint32_t flags
, nr_entries
;
326 r
= ro_step(s
, block
);
330 i
= search_fn(ro_node(s
), key
);
332 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
333 nr_entries
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
334 if (i
< 0 || i
>= nr_entries
)
337 if (flags
& INTERNAL_NODE
)
338 block
= value64(ro_node(s
), i
);
340 } while (!(flags
& LEAF_NODE
));
342 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
343 memcpy(v
, value_ptr(ro_node(s
), i
), value_size
);
348 int dm_btree_lookup(struct dm_btree_info
*info
, dm_block_t root
,
349 uint64_t *keys
, void *value_le
)
351 unsigned level
, last_level
= info
->levels
- 1;
354 __le64 internal_value_le
;
355 struct ro_spine spine
;
357 init_ro_spine(&spine
, info
);
358 for (level
= 0; level
< info
->levels
; level
++) {
362 if (level
== last_level
) {
364 size
= info
->value_type
.size
;
367 value_p
= &internal_value_le
;
368 size
= sizeof(uint64_t);
371 r
= btree_lookup_raw(&spine
, root
, keys
[level
],
376 if (rkey
!= keys
[level
]) {
377 exit_ro_spine(&spine
);
381 exit_ro_spine(&spine
);
385 root
= le64_to_cpu(internal_value_le
);
387 exit_ro_spine(&spine
);
391 EXPORT_SYMBOL_GPL(dm_btree_lookup
);
394 * Splits a node by creating a sibling node and shifting half the nodes
395 * contents across. Assumes there is a parent node, and it has room for
417 * +---------+ +-------+
421 * Where A* is a shadow of A.
423 static int btree_split_sibling(struct shadow_spine
*s
, dm_block_t root
,
424 unsigned parent_index
, uint64_t key
)
428 unsigned nr_left
, nr_right
;
429 struct dm_block
*left
, *right
, *parent
;
430 struct btree_node
*ln
, *rn
, *pn
;
433 left
= shadow_current(s
);
435 r
= new_block(s
->info
, &right
);
439 ln
= dm_block_data(left
);
440 rn
= dm_block_data(right
);
442 nr_left
= le32_to_cpu(ln
->header
.nr_entries
) / 2;
443 nr_right
= le32_to_cpu(ln
->header
.nr_entries
) - nr_left
;
445 ln
->header
.nr_entries
= cpu_to_le32(nr_left
);
447 rn
->header
.flags
= ln
->header
.flags
;
448 rn
->header
.nr_entries
= cpu_to_le32(nr_right
);
449 rn
->header
.max_entries
= ln
->header
.max_entries
;
450 rn
->header
.value_size
= ln
->header
.value_size
;
451 memcpy(rn
->keys
, ln
->keys
+ nr_left
, nr_right
* sizeof(rn
->keys
[0]));
453 size
= le32_to_cpu(ln
->header
.flags
) & INTERNAL_NODE
?
454 sizeof(uint64_t) : s
->info
->value_type
.size
;
455 memcpy(value_ptr(rn
, 0), value_ptr(ln
, nr_left
),
459 * Patch up the parent
461 parent
= shadow_parent(s
);
463 pn
= dm_block_data(parent
);
464 location
= cpu_to_le64(dm_block_location(left
));
465 __dm_bless_for_disk(&location
);
466 memcpy_disk(value_ptr(pn
, parent_index
),
467 &location
, sizeof(__le64
));
469 location
= cpu_to_le64(dm_block_location(right
));
470 __dm_bless_for_disk(&location
);
472 r
= insert_at(sizeof(__le64
), pn
, parent_index
+ 1,
473 le64_to_cpu(rn
->keys
[0]), &location
);
477 if (key
< le64_to_cpu(rn
->keys
[0])) {
478 unlock_block(s
->info
, right
);
481 unlock_block(s
->info
, left
);
489 * Splits a node by creating two new children beneath the given node.
505 * +-------+ +-------+
506 * | B +++ | | C +++ |
507 * +-------+ +-------+
509 static int btree_split_beneath(struct shadow_spine
*s
, uint64_t key
)
513 unsigned nr_left
, nr_right
;
514 struct dm_block
*left
, *right
, *new_parent
;
515 struct btree_node
*pn
, *ln
, *rn
;
518 new_parent
= shadow_current(s
);
520 r
= new_block(s
->info
, &left
);
524 r
= new_block(s
->info
, &right
);
526 /* FIXME: put left */
530 pn
= dm_block_data(new_parent
);
531 ln
= dm_block_data(left
);
532 rn
= dm_block_data(right
);
534 nr_left
= le32_to_cpu(pn
->header
.nr_entries
) / 2;
535 nr_right
= le32_to_cpu(pn
->header
.nr_entries
) - nr_left
;
537 ln
->header
.flags
= pn
->header
.flags
;
538 ln
->header
.nr_entries
= cpu_to_le32(nr_left
);
539 ln
->header
.max_entries
= pn
->header
.max_entries
;
540 ln
->header
.value_size
= pn
->header
.value_size
;
542 rn
->header
.flags
= pn
->header
.flags
;
543 rn
->header
.nr_entries
= cpu_to_le32(nr_right
);
544 rn
->header
.max_entries
= pn
->header
.max_entries
;
545 rn
->header
.value_size
= pn
->header
.value_size
;
547 memcpy(ln
->keys
, pn
->keys
, nr_left
* sizeof(pn
->keys
[0]));
548 memcpy(rn
->keys
, pn
->keys
+ nr_left
, nr_right
* sizeof(pn
->keys
[0]));
550 size
= le32_to_cpu(pn
->header
.flags
) & INTERNAL_NODE
?
551 sizeof(__le64
) : s
->info
->value_type
.size
;
552 memcpy(value_ptr(ln
, 0), value_ptr(pn
, 0), nr_left
* size
);
553 memcpy(value_ptr(rn
, 0), value_ptr(pn
, nr_left
),
556 /* new_parent should just point to l and r now */
557 pn
->header
.flags
= cpu_to_le32(INTERNAL_NODE
);
558 pn
->header
.nr_entries
= cpu_to_le32(2);
559 pn
->header
.max_entries
= cpu_to_le32(
560 calc_max_entries(sizeof(__le64
),
562 dm_tm_get_bm(s
->info
->tm
))));
563 pn
->header
.value_size
= cpu_to_le32(sizeof(__le64
));
565 val
= cpu_to_le64(dm_block_location(left
));
566 __dm_bless_for_disk(&val
);
567 pn
->keys
[0] = ln
->keys
[0];
568 memcpy_disk(value_ptr(pn
, 0), &val
, sizeof(__le64
));
570 val
= cpu_to_le64(dm_block_location(right
));
571 __dm_bless_for_disk(&val
);
572 pn
->keys
[1] = rn
->keys
[0];
573 memcpy_disk(value_ptr(pn
, 1), &val
, sizeof(__le64
));
576 * rejig the spine. This is ugly, since it knows too
577 * much about the spine
579 if (s
->nodes
[0] != new_parent
) {
580 unlock_block(s
->info
, s
->nodes
[0]);
581 s
->nodes
[0] = new_parent
;
583 if (key
< le64_to_cpu(rn
->keys
[0])) {
584 unlock_block(s
->info
, right
);
587 unlock_block(s
->info
, left
);
595 static int btree_insert_raw(struct shadow_spine
*s
, dm_block_t root
,
596 struct dm_btree_value_type
*vt
,
597 uint64_t key
, unsigned *index
)
599 int r
, i
= *index
, top
= 1;
600 struct btree_node
*node
;
603 r
= shadow_step(s
, root
, vt
);
607 node
= dm_block_data(shadow_current(s
));
610 * We have to patch up the parent node, ugly, but I don't
611 * see a way to do this automatically as part of the spine
614 if (shadow_has_parent(s
) && i
>= 0) { /* FIXME: second clause unness. */
615 __le64 location
= cpu_to_le64(dm_block_location(shadow_current(s
)));
617 __dm_bless_for_disk(&location
);
618 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s
)), i
),
619 &location
, sizeof(__le64
));
622 node
= dm_block_data(shadow_current(s
));
624 if (node
->header
.nr_entries
== node
->header
.max_entries
) {
626 r
= btree_split_beneath(s
, key
);
628 r
= btree_split_sibling(s
, root
, i
, key
);
634 node
= dm_block_data(shadow_current(s
));
636 i
= lower_bound(node
, key
);
638 if (le32_to_cpu(node
->header
.flags
) & LEAF_NODE
)
642 /* change the bounds on the lowest key */
643 node
->keys
[0] = cpu_to_le64(key
);
647 root
= value64(node
, i
);
651 if (i
< 0 || le64_to_cpu(node
->keys
[i
]) != key
)
658 static int insert(struct dm_btree_info
*info
, dm_block_t root
,
659 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
661 __dm_written_to_disk(value
)
664 unsigned level
, index
= -1, last_level
= info
->levels
- 1;
665 dm_block_t block
= root
;
666 struct shadow_spine spine
;
667 struct btree_node
*n
;
668 struct dm_btree_value_type le64_type
;
670 le64_type
.context
= NULL
;
671 le64_type
.size
= sizeof(__le64
);
672 le64_type
.inc
= NULL
;
673 le64_type
.dec
= NULL
;
674 le64_type
.equal
= NULL
;
676 init_shadow_spine(&spine
, info
);
678 for (level
= 0; level
< (info
->levels
- 1); level
++) {
679 r
= btree_insert_raw(&spine
, block
, &le64_type
, keys
[level
], &index
);
683 n
= dm_block_data(shadow_current(&spine
));
684 need_insert
= ((index
>= le32_to_cpu(n
->header
.nr_entries
)) ||
685 (le64_to_cpu(n
->keys
[index
]) != keys
[level
]));
691 r
= dm_btree_empty(info
, &new_tree
);
695 new_le
= cpu_to_le64(new_tree
);
696 __dm_bless_for_disk(&new_le
);
698 r
= insert_at(sizeof(uint64_t), n
, index
,
699 keys
[level
], &new_le
);
704 if (level
< last_level
)
705 block
= value64(n
, index
);
708 r
= btree_insert_raw(&spine
, block
, &info
->value_type
,
709 keys
[level
], &index
);
713 n
= dm_block_data(shadow_current(&spine
));
714 need_insert
= ((index
>= le32_to_cpu(n
->header
.nr_entries
)) ||
715 (le64_to_cpu(n
->keys
[index
]) != keys
[level
]));
721 r
= insert_at(info
->value_type
.size
, n
, index
,
729 if (info
->value_type
.dec
&&
730 (!info
->value_type
.equal
||
731 !info
->value_type
.equal(
732 info
->value_type
.context
,
735 info
->value_type
.dec(info
->value_type
.context
,
736 value_ptr(n
, index
));
738 memcpy_disk(value_ptr(n
, index
),
739 value
, info
->value_type
.size
);
742 *new_root
= shadow_root(&spine
);
743 exit_shadow_spine(&spine
);
748 __dm_unbless_for_disk(value
);
750 exit_shadow_spine(&spine
);
754 int dm_btree_insert(struct dm_btree_info
*info
, dm_block_t root
,
755 uint64_t *keys
, void *value
, dm_block_t
*new_root
)
756 __dm_written_to_disk(value
)
758 return insert(info
, root
, keys
, value
, new_root
, NULL
);
760 EXPORT_SYMBOL_GPL(dm_btree_insert
);
762 int dm_btree_insert_notify(struct dm_btree_info
*info
, dm_block_t root
,
763 uint64_t *keys
, void *value
, dm_block_t
*new_root
,
765 __dm_written_to_disk(value
)
767 return insert(info
, root
, keys
, value
, new_root
, inserted
);
769 EXPORT_SYMBOL_GPL(dm_btree_insert_notify
);
771 /*----------------------------------------------------------------*/
773 static int find_key(struct ro_spine
*s
, dm_block_t block
, bool find_highest
,
774 uint64_t *result_key
, dm_block_t
*next_block
)
780 r
= ro_step(s
, block
);
784 flags
= le32_to_cpu(ro_node(s
)->header
.flags
);
785 i
= le32_to_cpu(ro_node(s
)->header
.nr_entries
);
792 *result_key
= le64_to_cpu(ro_node(s
)->keys
[i
]);
794 *result_key
= le64_to_cpu(ro_node(s
)->keys
[0]);
796 if (next_block
|| flags
& INTERNAL_NODE
)
797 block
= value64(ro_node(s
), i
);
799 } while (flags
& INTERNAL_NODE
);
806 static int dm_btree_find_key(struct dm_btree_info
*info
, dm_block_t root
,
807 bool find_highest
, uint64_t *result_keys
)
809 int r
= 0, count
= 0, level
;
810 struct ro_spine spine
;
812 init_ro_spine(&spine
, info
);
813 for (level
= 0; level
< info
->levels
; level
++) {
814 r
= find_key(&spine
, root
, find_highest
, result_keys
+ level
,
815 level
== info
->levels
- 1 ? NULL
: &root
);
825 exit_ro_spine(&spine
);
827 return r
? r
: count
;
830 int dm_btree_find_highest_key(struct dm_btree_info
*info
, dm_block_t root
,
831 uint64_t *result_keys
)
833 return dm_btree_find_key(info
, root
, true, result_keys
);
835 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key
);
837 int dm_btree_find_lowest_key(struct dm_btree_info
*info
, dm_block_t root
,
838 uint64_t *result_keys
)
840 return dm_btree_find_key(info
, root
, false, result_keys
);
842 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key
);
844 /*----------------------------------------------------------------*/
847 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
848 * space. Also this only works for single level trees.
850 static int walk_node(struct ro_spine
*s
, dm_block_t block
,
851 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
856 struct btree_node
*n
;
859 r
= ro_step(s
, block
);
862 nr
= le32_to_cpu(n
->header
.nr_entries
);
863 for (i
= 0; i
< nr
; i
++) {
864 if (le32_to_cpu(n
->header
.flags
) & INTERNAL_NODE
) {
865 r
= walk_node(s
, value64(n
, i
), fn
, context
);
869 keys
= le64_to_cpu(*key_ptr(n
, i
));
870 r
= fn(context
, &keys
, value_ptr(n
, i
));
881 int dm_btree_walk(struct dm_btree_info
*info
, dm_block_t root
,
882 int (*fn
)(void *context
, uint64_t *keys
, void *leaf
),
886 struct ro_spine spine
;
888 BUG_ON(info
->levels
> 1);
890 init_ro_spine(&spine
, info
);
891 r
= walk_node(&spine
, root
, fn
, context
);
892 exit_ro_spine(&spine
);
896 EXPORT_SYMBOL_GPL(dm_btree_walk
);