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 static int upper_bound(struct btree_node *n, uint64_t key)
68 return bsearch(n, key, 1);
71 void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
72 struct dm_btree_value_type *vt)
75 uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
77 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
78 for (i = 0; i < nr_entries; i++)
79 dm_tm_inc(tm, value64(n, i));
81 for (i = 0; i < nr_entries; i++)
82 vt->inc(vt->context, value_ptr(n, i));
85 static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
86 uint64_t key, void *value)
87 __dm_written_to_disk(value)
89 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
90 uint32_t max_entries = le32_to_cpu(node->header.max_entries);
91 __le64 key_le = cpu_to_le64(key);
93 if (index > nr_entries ||
94 index >= max_entries ||
95 nr_entries >= max_entries) {
96 DMERR("too many entries in btree node for insert");
97 __dm_unbless_for_disk(value);
101 __dm_bless_for_disk(&key_le);
103 array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
104 array_insert(value_base(node), value_size, nr_entries, index, value);
105 node->header.nr_entries = cpu_to_le32(nr_entries + 1);
110 /*----------------------------------------------------------------*/
113 * We want 3n entries (for some n). This works more nicely for repeated
114 * insert remove loops than (2n + 1).
116 static uint32_t calc_max_entries(size_t value_size, size_t block_size)
119 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
121 block_size -= sizeof(struct node_header);
122 total = block_size / elt_size;
123 n = total / 3; /* rounds down */
128 int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
132 struct btree_node *n;
134 uint32_t max_entries;
136 r = new_block(info, &b);
140 block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
141 max_entries = calc_max_entries(info->value_type.size, block_size);
143 n = dm_block_data(b);
144 memset(n, 0, block_size);
145 n->header.flags = cpu_to_le32(LEAF_NODE);
146 n->header.nr_entries = cpu_to_le32(0);
147 n->header.max_entries = cpu_to_le32(max_entries);
148 n->header.value_size = cpu_to_le32(info->value_type.size);
150 *root = dm_block_location(b);
151 unlock_block(info, b);
155 EXPORT_SYMBOL_GPL(dm_btree_empty);
157 /*----------------------------------------------------------------*/
160 * Deletion uses a recursive algorithm, since we have limited stack space
161 * we explicitly manage our own stack on the heap.
163 #define MAX_SPINE_DEPTH 64
166 struct btree_node *n;
168 unsigned nr_children;
169 unsigned current_child;
173 struct dm_btree_info *info;
174 struct dm_transaction_manager *tm;
176 struct frame spine[MAX_SPINE_DEPTH];
179 static int top_frame(struct del_stack *s, struct frame **f)
182 DMERR("btree deletion stack empty");
186 *f = s->spine + s->top;
191 static int unprocessed_frames(struct del_stack *s)
196 static void prefetch_children(struct del_stack *s, struct frame *f)
199 struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
201 for (i = 0; i < f->nr_children; i++)
202 dm_bm_prefetch(bm, value64(f->n, i));
205 static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
207 return f->level < (info->levels - 1);
210 static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
215 if (s->top >= MAX_SPINE_DEPTH - 1) {
216 DMERR("btree deletion stack out of memory");
220 r = dm_tm_ref(s->tm, b, &ref_count);
226 * This is a shared node, so we can just decrement it's
227 * reference counter and leave the children.
233 struct frame *f = s->spine + ++s->top;
235 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
241 f->n = dm_block_data(f->b);
243 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
244 f->current_child = 0;
246 flags = le32_to_cpu(f->n->header.flags);
247 if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
248 prefetch_children(s, f);
254 static void pop_frame(struct del_stack *s)
256 struct frame *f = s->spine + s->top--;
258 dm_tm_dec(s->tm, dm_block_location(f->b));
259 dm_tm_unlock(s->tm, f->b);
262 static void unlock_all_frames(struct del_stack *s)
266 while (unprocessed_frames(s)) {
267 f = s->spine + s->top--;
268 dm_tm_unlock(s->tm, f->b);
272 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
278 * dm_btree_del() is called via an ioctl, as such should be
279 * considered an FS op. We can't recurse back into the FS, so we
282 s = kmalloc(sizeof(*s), GFP_NOFS);
289 r = push_frame(s, root, 0);
293 while (unprocessed_frames(s)) {
298 r = top_frame(s, &f);
302 if (f->current_child >= f->nr_children) {
307 flags = le32_to_cpu(f->n->header.flags);
308 if (flags & INTERNAL_NODE) {
309 b = value64(f->n, f->current_child);
311 r = push_frame(s, b, f->level);
315 } else if (is_internal_level(info, f)) {
316 b = value64(f->n, f->current_child);
318 r = push_frame(s, b, f->level + 1);
323 if (info->value_type.dec) {
326 for (i = 0; i < f->nr_children; i++)
327 info->value_type.dec(info->value_type.context,
335 /* cleanup all frames of del_stack */
336 unlock_all_frames(s);
342 EXPORT_SYMBOL_GPL(dm_btree_del);
344 /*----------------------------------------------------------------*/
346 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
347 int (*search_fn)(struct btree_node *, uint64_t),
348 uint64_t *result_key, void *v, size_t value_size)
351 uint32_t flags, nr_entries;
354 r = ro_step(s, block);
358 i = search_fn(ro_node(s), key);
360 flags = le32_to_cpu(ro_node(s)->header.flags);
361 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
362 if (i < 0 || i >= nr_entries)
365 if (flags & INTERNAL_NODE)
366 block = value64(ro_node(s), i);
368 } while (!(flags & LEAF_NODE));
370 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
371 memcpy(v, value_ptr(ro_node(s), i), value_size);
376 int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
377 uint64_t *keys, void *value_le)
379 unsigned level, last_level = info->levels - 1;
382 __le64 internal_value_le;
383 struct ro_spine spine;
385 init_ro_spine(&spine, info);
386 for (level = 0; level < info->levels; level++) {
390 if (level == last_level) {
392 size = info->value_type.size;
395 value_p = &internal_value_le;
396 size = sizeof(uint64_t);
399 r = btree_lookup_raw(&spine, root, keys[level],
404 if (rkey != keys[level]) {
405 exit_ro_spine(&spine);
409 exit_ro_spine(&spine);
413 root = le64_to_cpu(internal_value_le);
415 exit_ro_spine(&spine);
419 EXPORT_SYMBOL_GPL(dm_btree_lookup);
421 static int dm_btree_lookup_next_single(struct dm_btree_info *info, dm_block_t root,
422 uint64_t key, uint64_t *rkey, void *value_le)
425 uint32_t flags, nr_entries;
426 struct dm_block *node;
427 struct btree_node *n;
429 r = bn_read_lock(info, root, &node);
433 n = dm_block_data(node);
434 flags = le32_to_cpu(n->header.flags);
435 nr_entries = le32_to_cpu(n->header.nr_entries);
437 if (flags & INTERNAL_NODE) {
438 i = lower_bound(n, key);
441 * avoid early -ENODATA return when all entries are
442 * higher than the search @key.
446 if (i >= nr_entries) {
451 r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
452 if (r == -ENODATA && i < (nr_entries - 1)) {
454 r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
458 i = upper_bound(n, key);
459 if (i < 0 || i >= nr_entries) {
464 *rkey = le64_to_cpu(n->keys[i]);
465 memcpy(value_le, value_ptr(n, i), info->value_type.size);
468 dm_tm_unlock(info->tm, node);
472 int dm_btree_lookup_next(struct dm_btree_info *info, dm_block_t root,
473 uint64_t *keys, uint64_t *rkey, void *value_le)
477 __le64 internal_value_le;
478 struct ro_spine spine;
480 init_ro_spine(&spine, info);
481 for (level = 0; level < info->levels - 1u; level++) {
482 r = btree_lookup_raw(&spine, root, keys[level],
484 &internal_value_le, sizeof(uint64_t));
488 if (*rkey != keys[level]) {
493 root = le64_to_cpu(internal_value_le);
496 r = dm_btree_lookup_next_single(info, root, keys[level], rkey, value_le);
498 exit_ro_spine(&spine);
502 EXPORT_SYMBOL_GPL(dm_btree_lookup_next);
505 * Splits a node by creating a sibling node and shifting half the nodes
506 * contents across. Assumes there is a parent node, and it has room for
528 * +---------+ +-------+
532 * Where A* is a shadow of A.
534 static int btree_split_sibling(struct shadow_spine *s, unsigned parent_index,
539 unsigned nr_left, nr_right;
540 struct dm_block *left, *right, *parent;
541 struct btree_node *ln, *rn, *pn;
544 left = shadow_current(s);
546 r = new_block(s->info, &right);
550 ln = dm_block_data(left);
551 rn = dm_block_data(right);
553 nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
554 nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
556 ln->header.nr_entries = cpu_to_le32(nr_left);
558 rn->header.flags = ln->header.flags;
559 rn->header.nr_entries = cpu_to_le32(nr_right);
560 rn->header.max_entries = ln->header.max_entries;
561 rn->header.value_size = ln->header.value_size;
562 memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
564 size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
565 sizeof(uint64_t) : s->info->value_type.size;
566 memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
570 * Patch up the parent
572 parent = shadow_parent(s);
574 pn = dm_block_data(parent);
575 location = cpu_to_le64(dm_block_location(left));
576 __dm_bless_for_disk(&location);
577 memcpy_disk(value_ptr(pn, parent_index),
578 &location, sizeof(__le64));
580 location = cpu_to_le64(dm_block_location(right));
581 __dm_bless_for_disk(&location);
583 r = insert_at(sizeof(__le64), pn, parent_index + 1,
584 le64_to_cpu(rn->keys[0]), &location);
586 unlock_block(s->info, right);
590 if (key < le64_to_cpu(rn->keys[0])) {
591 unlock_block(s->info, right);
594 unlock_block(s->info, left);
602 * Splits a node by creating two new children beneath the given node.
618 * +-------+ +-------+
619 * | B +++ | | C +++ |
620 * +-------+ +-------+
622 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
626 unsigned nr_left, nr_right;
627 struct dm_block *left, *right, *new_parent;
628 struct btree_node *pn, *ln, *rn;
631 new_parent = shadow_current(s);
633 pn = dm_block_data(new_parent);
634 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
635 sizeof(__le64) : s->info->value_type.size;
637 /* create & init the left block */
638 r = new_block(s->info, &left);
642 ln = dm_block_data(left);
643 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
645 ln->header.flags = pn->header.flags;
646 ln->header.nr_entries = cpu_to_le32(nr_left);
647 ln->header.max_entries = pn->header.max_entries;
648 ln->header.value_size = pn->header.value_size;
649 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
650 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
652 /* create & init the right block */
653 r = new_block(s->info, &right);
655 unlock_block(s->info, left);
659 rn = dm_block_data(right);
660 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
662 rn->header.flags = pn->header.flags;
663 rn->header.nr_entries = cpu_to_le32(nr_right);
664 rn->header.max_entries = pn->header.max_entries;
665 rn->header.value_size = pn->header.value_size;
666 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
667 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
670 /* new_parent should just point to l and r now */
671 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
672 pn->header.nr_entries = cpu_to_le32(2);
673 pn->header.max_entries = cpu_to_le32(
674 calc_max_entries(sizeof(__le64),
676 dm_tm_get_bm(s->info->tm))));
677 pn->header.value_size = cpu_to_le32(sizeof(__le64));
679 val = cpu_to_le64(dm_block_location(left));
680 __dm_bless_for_disk(&val);
681 pn->keys[0] = ln->keys[0];
682 memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
684 val = cpu_to_le64(dm_block_location(right));
685 __dm_bless_for_disk(&val);
686 pn->keys[1] = rn->keys[0];
687 memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
689 unlock_block(s->info, left);
690 unlock_block(s->info, right);
694 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
695 struct dm_btree_value_type *vt,
696 uint64_t key, unsigned *index)
698 int r, i = *index, top = 1;
699 struct btree_node *node;
702 r = shadow_step(s, root, vt);
706 node = dm_block_data(shadow_current(s));
709 * We have to patch up the parent node, ugly, but I don't
710 * see a way to do this automatically as part of the spine
713 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
714 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
716 __dm_bless_for_disk(&location);
717 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
718 &location, sizeof(__le64));
721 node = dm_block_data(shadow_current(s));
723 if (node->header.nr_entries == node->header.max_entries) {
725 r = btree_split_beneath(s, key);
727 r = btree_split_sibling(s, i, key);
733 node = dm_block_data(shadow_current(s));
735 i = lower_bound(node, key);
737 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
741 /* change the bounds on the lowest key */
742 node->keys[0] = cpu_to_le64(key);
746 root = value64(node, i);
750 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
757 static bool need_insert(struct btree_node *node, uint64_t *keys,
758 unsigned level, unsigned index)
760 return ((index >= le32_to_cpu(node->header.nr_entries)) ||
761 (le64_to_cpu(node->keys[index]) != keys[level]));
764 static int insert(struct dm_btree_info *info, dm_block_t root,
765 uint64_t *keys, void *value, dm_block_t *new_root,
767 __dm_written_to_disk(value)
770 unsigned level, index = -1, last_level = info->levels - 1;
771 dm_block_t block = root;
772 struct shadow_spine spine;
773 struct btree_node *n;
774 struct dm_btree_value_type le64_type;
776 init_le64_type(info->tm, &le64_type);
777 init_shadow_spine(&spine, info);
779 for (level = 0; level < (info->levels - 1); level++) {
780 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
784 n = dm_block_data(shadow_current(&spine));
786 if (need_insert(n, keys, level, index)) {
790 r = dm_btree_empty(info, &new_tree);
794 new_le = cpu_to_le64(new_tree);
795 __dm_bless_for_disk(&new_le);
797 r = insert_at(sizeof(uint64_t), n, index,
798 keys[level], &new_le);
803 if (level < last_level)
804 block = value64(n, index);
807 r = btree_insert_raw(&spine, block, &info->value_type,
808 keys[level], &index);
812 n = dm_block_data(shadow_current(&spine));
814 if (need_insert(n, keys, level, index)) {
818 r = insert_at(info->value_type.size, n, index,
826 if (info->value_type.dec &&
827 (!info->value_type.equal ||
828 !info->value_type.equal(
829 info->value_type.context,
832 info->value_type.dec(info->value_type.context,
833 value_ptr(n, index));
835 memcpy_disk(value_ptr(n, index),
836 value, info->value_type.size);
839 *new_root = shadow_root(&spine);
840 exit_shadow_spine(&spine);
845 __dm_unbless_for_disk(value);
847 exit_shadow_spine(&spine);
851 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
852 uint64_t *keys, void *value, dm_block_t *new_root)
853 __dm_written_to_disk(value)
855 return insert(info, root, keys, value, new_root, NULL);
857 EXPORT_SYMBOL_GPL(dm_btree_insert);
859 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
860 uint64_t *keys, void *value, dm_block_t *new_root,
862 __dm_written_to_disk(value)
864 return insert(info, root, keys, value, new_root, inserted);
866 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
868 /*----------------------------------------------------------------*/
870 static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
871 uint64_t *result_key, dm_block_t *next_block)
877 r = ro_step(s, block);
881 flags = le32_to_cpu(ro_node(s)->header.flags);
882 i = le32_to_cpu(ro_node(s)->header.nr_entries);
889 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
891 *result_key = le64_to_cpu(ro_node(s)->keys[0]);
893 if (next_block || flags & INTERNAL_NODE) {
895 block = value64(ro_node(s), i);
897 block = value64(ro_node(s), 0);
900 } while (flags & INTERNAL_NODE);
907 static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
908 bool find_highest, uint64_t *result_keys)
910 int r = 0, count = 0, level;
911 struct ro_spine spine;
913 init_ro_spine(&spine, info);
914 for (level = 0; level < info->levels; level++) {
915 r = find_key(&spine, root, find_highest, result_keys + level,
916 level == info->levels - 1 ? NULL : &root);
926 exit_ro_spine(&spine);
928 return r ? r : count;
931 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
932 uint64_t *result_keys)
934 return dm_btree_find_key(info, root, true, result_keys);
936 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
938 int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
939 uint64_t *result_keys)
941 return dm_btree_find_key(info, root, false, result_keys);
943 EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
945 /*----------------------------------------------------------------*/
948 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
949 * space. Also this only works for single level trees.
951 static int walk_node(struct dm_btree_info *info, dm_block_t block,
952 int (*fn)(void *context, uint64_t *keys, void *leaf),
957 struct dm_block *node;
958 struct btree_node *n;
961 r = bn_read_lock(info, block, &node);
965 n = dm_block_data(node);
967 nr = le32_to_cpu(n->header.nr_entries);
968 for (i = 0; i < nr; i++) {
969 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
970 r = walk_node(info, value64(n, i), fn, context);
974 keys = le64_to_cpu(*key_ptr(n, i));
975 r = fn(context, &keys, value_ptr(n, i));
982 dm_tm_unlock(info->tm, node);
986 int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
987 int (*fn)(void *context, uint64_t *keys, void *leaf),
990 BUG_ON(info->levels > 1);
991 return walk_node(info, root, fn, context);
993 EXPORT_SYMBOL_GPL(dm_btree_walk);
995 /*----------------------------------------------------------------*/
997 static void prefetch_values(struct dm_btree_cursor *c)
1001 struct cursor_node *n = c->nodes + c->depth - 1;
1002 struct btree_node *bn = dm_block_data(n->b);
1003 struct dm_block_manager *bm = dm_tm_get_bm(c->info->tm);
1005 BUG_ON(c->info->value_type.size != sizeof(value_le));
1007 nr = le32_to_cpu(bn->header.nr_entries);
1008 for (i = 0; i < nr; i++) {
1009 memcpy(&value_le, value_ptr(bn, i), sizeof(value_le));
1010 dm_bm_prefetch(bm, le64_to_cpu(value_le));
1014 static bool leaf_node(struct dm_btree_cursor *c)
1016 struct cursor_node *n = c->nodes + c->depth - 1;
1017 struct btree_node *bn = dm_block_data(n->b);
1019 return le32_to_cpu(bn->header.flags) & LEAF_NODE;
1022 static int push_node(struct dm_btree_cursor *c, dm_block_t b)
1025 struct cursor_node *n = c->nodes + c->depth;
1027 if (c->depth >= DM_BTREE_CURSOR_MAX_DEPTH - 1) {
1028 DMERR("couldn't push cursor node, stack depth too high");
1032 r = bn_read_lock(c->info, b, &n->b);
1039 if (c->prefetch_leaves || !leaf_node(c))
1045 static void pop_node(struct dm_btree_cursor *c)
1048 unlock_block(c->info, c->nodes[c->depth].b);
1051 static int inc_or_backtrack(struct dm_btree_cursor *c)
1053 struct cursor_node *n;
1054 struct btree_node *bn;
1060 n = c->nodes + c->depth - 1;
1061 bn = dm_block_data(n->b);
1064 if (n->index < le32_to_cpu(bn->header.nr_entries))
1073 static int find_leaf(struct dm_btree_cursor *c)
1076 struct cursor_node *n;
1077 struct btree_node *bn;
1081 n = c->nodes + c->depth - 1;
1082 bn = dm_block_data(n->b);
1084 if (le32_to_cpu(bn->header.flags) & LEAF_NODE)
1087 memcpy(&value_le, value_ptr(bn, n->index), sizeof(value_le));
1088 r = push_node(c, le64_to_cpu(value_le));
1090 DMERR("push_node failed");
1095 if (!r && (le32_to_cpu(bn->header.nr_entries) == 0))
1101 int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
1102 bool prefetch_leaves, struct dm_btree_cursor *c)
1109 c->prefetch_leaves = prefetch_leaves;
1111 r = push_node(c, root);
1115 return find_leaf(c);
1117 EXPORT_SYMBOL_GPL(dm_btree_cursor_begin);
1119 void dm_btree_cursor_end(struct dm_btree_cursor *c)
1124 EXPORT_SYMBOL_GPL(dm_btree_cursor_end);
1126 int dm_btree_cursor_next(struct dm_btree_cursor *c)
1128 int r = inc_or_backtrack(c);
1132 DMERR("find_leaf failed");
1137 EXPORT_SYMBOL_GPL(dm_btree_cursor_next);
1139 int dm_btree_cursor_skip(struct dm_btree_cursor *c, uint32_t count)
1143 while (count-- && !r)
1144 r = dm_btree_cursor_next(c);
1148 EXPORT_SYMBOL_GPL(dm_btree_cursor_skip);
1150 int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le)
1153 struct cursor_node *n = c->nodes + c->depth - 1;
1154 struct btree_node *bn = dm_block_data(n->b);
1156 if (le32_to_cpu(bn->header.flags) & INTERNAL_NODE)
1159 *key = le64_to_cpu(*key_ptr(bn, n->index));
1160 memcpy(value_le, value_ptr(bn, n->index), c->info->value_type.size);
1166 EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value);