pandora: defconfig: update
[pandora-kernel.git] / drivers / md / persistent-data / dm-btree.c
1 /*
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm-btree-internal.h"
8 #include "dm-space-map.h"
9 #include "dm-transaction-manager.h"
10
11 #include <linux/export.h>
12 #include <linux/device-mapper.h>
13
14 #define DM_MSG_PREFIX "btree"
15
16 /*----------------------------------------------------------------
17  * Array manipulation
18  *--------------------------------------------------------------*/
19 static void memcpy_disk(void *dest, const void *src, size_t len)
20         __dm_written_to_disk(src)
21 {
22         memcpy(dest, src, len);
23         __dm_unbless_for_disk(src);
24 }
25
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)
29 {
30         if (index < nr_elts)
31                 memmove(base + (elt_size * (index + 1)),
32                         base + (elt_size * index),
33                         (nr_elts - index) * elt_size);
34
35         memcpy_disk(base + (elt_size * index), elt, elt_size);
36 }
37
38 /*----------------------------------------------------------------*/
39
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)
42 {
43         int lo = -1, hi = le32_to_cpu(n->header.nr_entries);
44
45         while (hi - lo > 1) {
46                 int mid = lo + ((hi - lo) / 2);
47                 uint64_t mid_key = le64_to_cpu(n->keys[mid]);
48
49                 if (mid_key == key)
50                         return mid;
51
52                 if (mid_key < key)
53                         lo = mid;
54                 else
55                         hi = mid;
56         }
57
58         return want_hi ? hi : lo;
59 }
60
61 int lower_bound(struct btree_node *n, uint64_t key)
62 {
63         return bsearch(n, key, 0);
64 }
65
66 void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
67                   struct dm_btree_value_type *vt)
68 {
69         unsigned i;
70         uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
71
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));
75         else if (vt->inc)
76                 for (i = 0; i < nr_entries; i++)
77                         vt->inc(vt->context,
78                                 value_ptr(n, i, vt->size));
79 }
80
81 static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
82                       uint64_t key, void *value)
83                       __dm_written_to_disk(value)
84 {
85         uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
86         __le64 key_le = cpu_to_le64(key);
87
88         if (index > nr_entries ||
89             index >= le32_to_cpu(node->header.max_entries)) {
90                 DMERR("too many entries in btree node for insert");
91                 __dm_unbless_for_disk(value);
92                 return -ENOMEM;
93         }
94
95         __dm_bless_for_disk(&key_le);
96
97         array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
98         array_insert(value_base(node), value_size, nr_entries, index, value);
99         node->header.nr_entries = cpu_to_le32(nr_entries + 1);
100
101         return 0;
102 }
103
104 /*----------------------------------------------------------------*/
105
106 /*
107  * We want 3n entries (for some n).  This works more nicely for repeated
108  * insert remove loops than (2n + 1).
109  */
110 static uint32_t calc_max_entries(size_t value_size, size_t block_size)
111 {
112         uint32_t total, n;
113         size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
114
115         block_size -= sizeof(struct node_header);
116         total = block_size / elt_size;
117         n = total / 3;          /* rounds down */
118
119         return 3 * n;
120 }
121
122 int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
123 {
124         int r;
125         struct dm_block *b;
126         struct btree_node *n;
127         size_t block_size;
128         uint32_t max_entries;
129
130         r = new_block(info, &b);
131         if (r < 0)
132                 return r;
133
134         block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
135         max_entries = calc_max_entries(info->value_type.size, block_size);
136
137         n = dm_block_data(b);
138         memset(n, 0, block_size);
139         n->header.flags = cpu_to_le32(LEAF_NODE);
140         n->header.nr_entries = cpu_to_le32(0);
141         n->header.max_entries = cpu_to_le32(max_entries);
142         n->header.value_size = cpu_to_le32(info->value_type.size);
143
144         *root = dm_block_location(b);
145         return unlock_block(info, b);
146 }
147 EXPORT_SYMBOL_GPL(dm_btree_empty);
148
149 /*----------------------------------------------------------------*/
150
151 /*
152  * Deletion uses a recursive algorithm, since we have limited stack space
153  * we explicitly manage our own stack on the heap.
154  */
155 #define MAX_SPINE_DEPTH 64
156 struct frame {
157         struct dm_block *b;
158         struct btree_node *n;
159         unsigned level;
160         unsigned nr_children;
161         unsigned current_child;
162 };
163
164 struct del_stack {
165         struct dm_transaction_manager *tm;
166         int top;
167         struct frame spine[MAX_SPINE_DEPTH];
168 };
169
170 static int top_frame(struct del_stack *s, struct frame **f)
171 {
172         if (s->top < 0) {
173                 DMERR("btree deletion stack empty");
174                 return -EINVAL;
175         }
176
177         *f = s->spine + s->top;
178
179         return 0;
180 }
181
182 static int unprocessed_frames(struct del_stack *s)
183 {
184         return s->top >= 0;
185 }
186
187 static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
188 {
189         int r;
190         uint32_t ref_count;
191
192         if (s->top >= MAX_SPINE_DEPTH - 1) {
193                 DMERR("btree deletion stack out of memory");
194                 return -ENOMEM;
195         }
196
197         r = dm_tm_ref(s->tm, b, &ref_count);
198         if (r)
199                 return r;
200
201         if (ref_count > 1)
202                 /*
203                  * This is a shared node, so we can just decrement it's
204                  * reference counter and leave the children.
205                  */
206                 dm_tm_dec(s->tm, b);
207
208         else {
209                 struct frame *f = s->spine + ++s->top;
210
211                 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
212                 if (r) {
213                         s->top--;
214                         return r;
215                 }
216
217                 f->n = dm_block_data(f->b);
218                 f->level = level;
219                 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
220                 f->current_child = 0;
221         }
222
223         return 0;
224 }
225
226 static void pop_frame(struct del_stack *s)
227 {
228         struct frame *f = s->spine + s->top--;
229
230         dm_tm_dec(s->tm, dm_block_location(f->b));
231         dm_tm_unlock(s->tm, f->b);
232 }
233
234 static void unlock_all_frames(struct del_stack *s)
235 {
236         struct frame *f;
237
238         while (unprocessed_frames(s)) {
239                 f = s->spine + s->top--;
240                 dm_tm_unlock(s->tm, f->b);
241         }
242 }
243
244 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
245 {
246         int r;
247         struct del_stack *s;
248
249         s = kmalloc(sizeof(*s), GFP_NOIO);
250         if (!s)
251                 return -ENOMEM;
252         s->tm = info->tm;
253         s->top = -1;
254
255         r = push_frame(s, root, 1);
256         if (r)
257                 goto out;
258
259         while (unprocessed_frames(s)) {
260                 uint32_t flags;
261                 struct frame *f;
262                 dm_block_t b;
263
264                 r = top_frame(s, &f);
265                 if (r)
266                         goto out;
267
268                 if (f->current_child >= f->nr_children) {
269                         pop_frame(s);
270                         continue;
271                 }
272
273                 flags = le32_to_cpu(f->n->header.flags);
274                 if (flags & INTERNAL_NODE) {
275                         b = value64(f->n, f->current_child);
276                         f->current_child++;
277                         r = push_frame(s, b, f->level);
278                         if (r)
279                                 goto out;
280
281                 } else if (f->level != (info->levels - 1)) {
282                         b = value64(f->n, f->current_child);
283                         f->current_child++;
284                         r = push_frame(s, b, f->level + 1);
285                         if (r)
286                                 goto out;
287
288                 } else {
289                         if (info->value_type.dec) {
290                                 unsigned i;
291
292                                 for (i = 0; i < f->nr_children; i++)
293                                         info->value_type.dec(info->value_type.context,
294                                                              value_ptr(f->n, i, info->value_type.size));
295                         }
296                         f->current_child = f->nr_children;
297                 }
298         }
299 out:
300         if (r) {
301                 /* cleanup all frames of del_stack */
302                 unlock_all_frames(s);
303         }
304         kfree(s);
305
306         return r;
307 }
308 EXPORT_SYMBOL_GPL(dm_btree_del);
309
310 /*----------------------------------------------------------------*/
311
312 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
313                             int (*search_fn)(struct btree_node *, uint64_t),
314                             uint64_t *result_key, void *v, size_t value_size)
315 {
316         int i, r;
317         uint32_t flags, nr_entries;
318
319         do {
320                 r = ro_step(s, block);
321                 if (r < 0)
322                         return r;
323
324                 i = search_fn(ro_node(s), key);
325
326                 flags = le32_to_cpu(ro_node(s)->header.flags);
327                 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
328                 if (i < 0 || i >= nr_entries)
329                         return -ENODATA;
330
331                 if (flags & INTERNAL_NODE)
332                         block = value64(ro_node(s), i);
333
334         } while (!(flags & LEAF_NODE));
335
336         *result_key = le64_to_cpu(ro_node(s)->keys[i]);
337         memcpy(v, value_ptr(ro_node(s), i, value_size), value_size);
338
339         return 0;
340 }
341
342 int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
343                     uint64_t *keys, void *value_le)
344 {
345         unsigned level, last_level = info->levels - 1;
346         int r = -ENODATA;
347         uint64_t rkey;
348         __le64 internal_value_le;
349         struct ro_spine spine;
350
351         init_ro_spine(&spine, info);
352         for (level = 0; level < info->levels; level++) {
353                 size_t size;
354                 void *value_p;
355
356                 if (level == last_level) {
357                         value_p = value_le;
358                         size = info->value_type.size;
359
360                 } else {
361                         value_p = &internal_value_le;
362                         size = sizeof(uint64_t);
363                 }
364
365                 r = btree_lookup_raw(&spine, root, keys[level],
366                                      lower_bound, &rkey,
367                                      value_p, size);
368
369                 if (!r) {
370                         if (rkey != keys[level]) {
371                                 exit_ro_spine(&spine);
372                                 return -ENODATA;
373                         }
374                 } else {
375                         exit_ro_spine(&spine);
376                         return r;
377                 }
378
379                 root = le64_to_cpu(internal_value_le);
380         }
381         exit_ro_spine(&spine);
382
383         return r;
384 }
385 EXPORT_SYMBOL_GPL(dm_btree_lookup);
386
387 /*
388  * Splits a node by creating a sibling node and shifting half the nodes
389  * contents across.  Assumes there is a parent node, and it has room for
390  * another child.
391  *
392  * Before:
393  *        +--------+
394  *        | Parent |
395  *        +--------+
396  *           |
397  *           v
398  *      +----------+
399  *      | A ++++++ |
400  *      +----------+
401  *
402  *
403  * After:
404  *              +--------+
405  *              | Parent |
406  *              +--------+
407  *                |     |
408  *                v     +------+
409  *          +---------+        |
410  *          | A* +++  |        v
411  *          +---------+   +-------+
412  *                        | B +++ |
413  *                        +-------+
414  *
415  * Where A* is a shadow of A.
416  */
417 static int btree_split_sibling(struct shadow_spine *s, dm_block_t root,
418                                unsigned parent_index, uint64_t key)
419 {
420         int r;
421         size_t size;
422         unsigned nr_left, nr_right;
423         struct dm_block *left, *right, *parent;
424         struct btree_node *ln, *rn, *pn;
425         __le64 location;
426
427         left = shadow_current(s);
428
429         r = new_block(s->info, &right);
430         if (r < 0)
431                 return r;
432
433         ln = dm_block_data(left);
434         rn = dm_block_data(right);
435
436         nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
437         nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
438
439         ln->header.nr_entries = cpu_to_le32(nr_left);
440
441         rn->header.flags = ln->header.flags;
442         rn->header.nr_entries = cpu_to_le32(nr_right);
443         rn->header.max_entries = ln->header.max_entries;
444         rn->header.value_size = ln->header.value_size;
445         memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
446
447         size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
448                 sizeof(uint64_t) : s->info->value_type.size;
449         memcpy(value_ptr(rn, 0, size), value_ptr(ln, nr_left, size),
450                size * nr_right);
451
452         /*
453          * Patch up the parent
454          */
455         parent = shadow_parent(s);
456
457         pn = dm_block_data(parent);
458         location = cpu_to_le64(dm_block_location(left));
459         __dm_bless_for_disk(&location);
460         memcpy_disk(value_ptr(pn, parent_index, sizeof(__le64)),
461                     &location, sizeof(__le64));
462
463         location = cpu_to_le64(dm_block_location(right));
464         __dm_bless_for_disk(&location);
465
466         r = insert_at(sizeof(__le64), pn, parent_index + 1,
467                       le64_to_cpu(rn->keys[0]), &location);
468         if (r) {
469                 unlock_block(s->info, right);
470                 return r;
471         }
472
473         if (key < le64_to_cpu(rn->keys[0])) {
474                 unlock_block(s->info, right);
475                 s->nodes[1] = left;
476         } else {
477                 unlock_block(s->info, left);
478                 s->nodes[1] = right;
479         }
480
481         return 0;
482 }
483
484 /*
485  * Splits a node by creating two new children beneath the given node.
486  *
487  * Before:
488  *        +----------+
489  *        | A ++++++ |
490  *        +----------+
491  *
492  *
493  * After:
494  *      +------------+
495  *      | A (shadow) |
496  *      +------------+
497  *          |   |
498  *   +------+   +----+
499  *   |               |
500  *   v               v
501  * +-------+     +-------+
502  * | B +++ |     | C +++ |
503  * +-------+     +-------+
504  */
505 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
506 {
507         int r;
508         size_t size;
509         unsigned nr_left, nr_right;
510         struct dm_block *left, *right, *new_parent;
511         struct btree_node *pn, *ln, *rn;
512         __le64 val;
513
514         new_parent = shadow_current(s);
515
516         r = new_block(s->info, &left);
517         if (r < 0)
518                 return r;
519
520         r = new_block(s->info, &right);
521         if (r < 0) {
522                 unlock_block(s->info, left);
523                 return r;
524         }
525
526         pn = dm_block_data(new_parent);
527         ln = dm_block_data(left);
528         rn = dm_block_data(right);
529
530         nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
531         nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
532
533         ln->header.flags = pn->header.flags;
534         ln->header.nr_entries = cpu_to_le32(nr_left);
535         ln->header.max_entries = pn->header.max_entries;
536         ln->header.value_size = pn->header.value_size;
537
538         rn->header.flags = pn->header.flags;
539         rn->header.nr_entries = cpu_to_le32(nr_right);
540         rn->header.max_entries = pn->header.max_entries;
541         rn->header.value_size = pn->header.value_size;
542
543         memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
544         memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
545
546         size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
547                 sizeof(__le64) : s->info->value_type.size;
548         memcpy(value_ptr(ln, 0, size), value_ptr(pn, 0, size), nr_left * size);
549         memcpy(value_ptr(rn, 0, size), value_ptr(pn, nr_left, size),
550                nr_right * size);
551
552         /* new_parent should just point to l and r now */
553         pn->header.flags = cpu_to_le32(INTERNAL_NODE);
554         pn->header.nr_entries = cpu_to_le32(2);
555         pn->header.max_entries = cpu_to_le32(
556                 calc_max_entries(sizeof(__le64),
557                                  dm_bm_block_size(
558                                          dm_tm_get_bm(s->info->tm))));
559         pn->header.value_size = cpu_to_le32(sizeof(__le64));
560
561         val = cpu_to_le64(dm_block_location(left));
562         __dm_bless_for_disk(&val);
563         pn->keys[0] = ln->keys[0];
564         memcpy_disk(value_ptr(pn, 0, sizeof(__le64)), &val, sizeof(__le64));
565
566         val = cpu_to_le64(dm_block_location(right));
567         __dm_bless_for_disk(&val);
568         pn->keys[1] = rn->keys[0];
569         memcpy_disk(value_ptr(pn, 1, sizeof(__le64)), &val, sizeof(__le64));
570
571         unlock_block(s->info, left);
572         unlock_block(s->info, right);
573         return 0;
574 }
575
576 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
577                             struct dm_btree_value_type *vt,
578                             uint64_t key, unsigned *index)
579 {
580         int r, i = *index, top = 1;
581         struct btree_node *node;
582
583         for (;;) {
584                 r = shadow_step(s, root, vt);
585                 if (r < 0)
586                         return r;
587
588                 node = dm_block_data(shadow_current(s));
589
590                 /*
591                  * We have to patch up the parent node, ugly, but I don't
592                  * see a way to do this automatically as part of the spine
593                  * op.
594                  */
595                 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
596                         __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
597
598                         __dm_bless_for_disk(&location);
599                         memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i, sizeof(uint64_t)),
600                                     &location, sizeof(__le64));
601                 }
602
603                 node = dm_block_data(shadow_current(s));
604
605                 if (node->header.nr_entries == node->header.max_entries) {
606                         if (top)
607                                 r = btree_split_beneath(s, key);
608                         else
609                                 r = btree_split_sibling(s, root, i, key);
610
611                         if (r < 0)
612                                 return r;
613                 }
614
615                 node = dm_block_data(shadow_current(s));
616
617                 i = lower_bound(node, key);
618
619                 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
620                         break;
621
622                 if (i < 0) {
623                         /* change the bounds on the lowest key */
624                         node->keys[0] = cpu_to_le64(key);
625                         i = 0;
626                 }
627
628                 root = value64(node, i);
629                 top = 0;
630         }
631
632         if (i < 0 || le64_to_cpu(node->keys[i]) != key)
633                 i++;
634
635         *index = i;
636         return 0;
637 }
638
639 static int insert(struct dm_btree_info *info, dm_block_t root,
640                   uint64_t *keys, void *value, dm_block_t *new_root,
641                   int *inserted)
642                   __dm_written_to_disk(value)
643 {
644         int r, need_insert;
645         unsigned level, index = -1, last_level = info->levels - 1;
646         dm_block_t block = root;
647         struct shadow_spine spine;
648         struct btree_node *n;
649         struct dm_btree_value_type le64_type;
650
651         init_le64_type(info->tm, &le64_type);
652         init_shadow_spine(&spine, info);
653
654         for (level = 0; level < (info->levels - 1); level++) {
655                 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
656                 if (r < 0)
657                         goto bad;
658
659                 n = dm_block_data(shadow_current(&spine));
660                 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
661                                (le64_to_cpu(n->keys[index]) != keys[level]));
662
663                 if (need_insert) {
664                         dm_block_t new_tree;
665                         __le64 new_le;
666
667                         r = dm_btree_empty(info, &new_tree);
668                         if (r < 0)
669                                 goto bad;
670
671                         new_le = cpu_to_le64(new_tree);
672                         __dm_bless_for_disk(&new_le);
673
674                         r = insert_at(sizeof(uint64_t), n, index,
675                                       keys[level], &new_le);
676                         if (r)
677                                 goto bad;
678                 }
679
680                 if (level < last_level)
681                         block = value64(n, index);
682         }
683
684         r = btree_insert_raw(&spine, block, &info->value_type,
685                              keys[level], &index);
686         if (r < 0)
687                 goto bad;
688
689         n = dm_block_data(shadow_current(&spine));
690         need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
691                        (le64_to_cpu(n->keys[index]) != keys[level]));
692
693         if (need_insert) {
694                 if (inserted)
695                         *inserted = 1;
696
697                 r = insert_at(info->value_type.size, n, index,
698                               keys[level], value);
699                 if (r)
700                         goto bad_unblessed;
701         } else {
702                 if (inserted)
703                         *inserted = 0;
704
705                 if (info->value_type.dec &&
706                     (!info->value_type.equal ||
707                      !info->value_type.equal(
708                              info->value_type.context,
709                              value_ptr(n, index, info->value_type.size),
710                              value))) {
711                         info->value_type.dec(info->value_type.context,
712                                              value_ptr(n, index, info->value_type.size));
713                 }
714                 memcpy_disk(value_ptr(n, index, info->value_type.size),
715                             value, info->value_type.size);
716         }
717
718         *new_root = shadow_root(&spine);
719         exit_shadow_spine(&spine);
720
721         return 0;
722
723 bad:
724         __dm_unbless_for_disk(value);
725 bad_unblessed:
726         exit_shadow_spine(&spine);
727         return r;
728 }
729
730 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
731                     uint64_t *keys, void *value, dm_block_t *new_root)
732                     __dm_written_to_disk(value)
733 {
734         return insert(info, root, keys, value, new_root, NULL);
735 }
736 EXPORT_SYMBOL_GPL(dm_btree_insert);
737
738 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
739                            uint64_t *keys, void *value, dm_block_t *new_root,
740                            int *inserted)
741                            __dm_written_to_disk(value)
742 {
743         return insert(info, root, keys, value, new_root, inserted);
744 }
745 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
746
747 /*----------------------------------------------------------------*/
748
749 static int find_highest_key(struct ro_spine *s, dm_block_t block,
750                             uint64_t *result_key, dm_block_t *next_block)
751 {
752         int i, r;
753         uint32_t flags;
754
755         do {
756                 r = ro_step(s, block);
757                 if (r < 0)
758                         return r;
759
760                 flags = le32_to_cpu(ro_node(s)->header.flags);
761                 i = le32_to_cpu(ro_node(s)->header.nr_entries);
762                 if (!i)
763                         return -ENODATA;
764                 else
765                         i--;
766
767                 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
768                 if (next_block || flags & INTERNAL_NODE)
769                         block = value64(ro_node(s), i);
770
771         } while (flags & INTERNAL_NODE);
772
773         if (next_block)
774                 *next_block = block;
775         return 0;
776 }
777
778 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
779                               uint64_t *result_keys)
780 {
781         int r = 0, count = 0, level;
782         struct ro_spine spine;
783
784         init_ro_spine(&spine, info);
785         for (level = 0; level < info->levels; level++) {
786                 r = find_highest_key(&spine, root, result_keys + level,
787                                      level == info->levels - 1 ? NULL : &root);
788                 if (r == -ENODATA) {
789                         r = 0;
790                         break;
791
792                 } else if (r)
793                         break;
794
795                 count++;
796         }
797         exit_ro_spine(&spine);
798
799         return r ? r : count;
800 }
801 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);