dm btree: fix leak of bufio-backed block in btree_split_sibling error path
[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 int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
235 {
236         int r;
237         struct del_stack *s;
238
239         s = kmalloc(sizeof(*s), GFP_NOIO);
240         if (!s)
241                 return -ENOMEM;
242         s->tm = info->tm;
243         s->top = -1;
244
245         r = push_frame(s, root, 1);
246         if (r)
247                 goto out;
248
249         while (unprocessed_frames(s)) {
250                 uint32_t flags;
251                 struct frame *f;
252                 dm_block_t b;
253
254                 r = top_frame(s, &f);
255                 if (r)
256                         goto out;
257
258                 if (f->current_child >= f->nr_children) {
259                         pop_frame(s);
260                         continue;
261                 }
262
263                 flags = le32_to_cpu(f->n->header.flags);
264                 if (flags & INTERNAL_NODE) {
265                         b = value64(f->n, f->current_child);
266                         f->current_child++;
267                         r = push_frame(s, b, f->level);
268                         if (r)
269                                 goto out;
270
271                 } else if (f->level != (info->levels - 1)) {
272                         b = value64(f->n, f->current_child);
273                         f->current_child++;
274                         r = push_frame(s, b, f->level + 1);
275                         if (r)
276                                 goto out;
277
278                 } else {
279                         if (info->value_type.dec) {
280                                 unsigned i;
281
282                                 for (i = 0; i < f->nr_children; i++)
283                                         info->value_type.dec(info->value_type.context,
284                                                              value_ptr(f->n, i, info->value_type.size));
285                         }
286                         f->current_child = f->nr_children;
287                 }
288         }
289
290 out:
291         kfree(s);
292         return r;
293 }
294 EXPORT_SYMBOL_GPL(dm_btree_del);
295
296 /*----------------------------------------------------------------*/
297
298 static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
299                             int (*search_fn)(struct btree_node *, uint64_t),
300                             uint64_t *result_key, void *v, size_t value_size)
301 {
302         int i, r;
303         uint32_t flags, nr_entries;
304
305         do {
306                 r = ro_step(s, block);
307                 if (r < 0)
308                         return r;
309
310                 i = search_fn(ro_node(s), key);
311
312                 flags = le32_to_cpu(ro_node(s)->header.flags);
313                 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
314                 if (i < 0 || i >= nr_entries)
315                         return -ENODATA;
316
317                 if (flags & INTERNAL_NODE)
318                         block = value64(ro_node(s), i);
319
320         } while (!(flags & LEAF_NODE));
321
322         *result_key = le64_to_cpu(ro_node(s)->keys[i]);
323         memcpy(v, value_ptr(ro_node(s), i, value_size), value_size);
324
325         return 0;
326 }
327
328 int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
329                     uint64_t *keys, void *value_le)
330 {
331         unsigned level, last_level = info->levels - 1;
332         int r = -ENODATA;
333         uint64_t rkey;
334         __le64 internal_value_le;
335         struct ro_spine spine;
336
337         init_ro_spine(&spine, info);
338         for (level = 0; level < info->levels; level++) {
339                 size_t size;
340                 void *value_p;
341
342                 if (level == last_level) {
343                         value_p = value_le;
344                         size = info->value_type.size;
345
346                 } else {
347                         value_p = &internal_value_le;
348                         size = sizeof(uint64_t);
349                 }
350
351                 r = btree_lookup_raw(&spine, root, keys[level],
352                                      lower_bound, &rkey,
353                                      value_p, size);
354
355                 if (!r) {
356                         if (rkey != keys[level]) {
357                                 exit_ro_spine(&spine);
358                                 return -ENODATA;
359                         }
360                 } else {
361                         exit_ro_spine(&spine);
362                         return r;
363                 }
364
365                 root = le64_to_cpu(internal_value_le);
366         }
367         exit_ro_spine(&spine);
368
369         return r;
370 }
371 EXPORT_SYMBOL_GPL(dm_btree_lookup);
372
373 /*
374  * Splits a node by creating a sibling node and shifting half the nodes
375  * contents across.  Assumes there is a parent node, and it has room for
376  * another child.
377  *
378  * Before:
379  *        +--------+
380  *        | Parent |
381  *        +--------+
382  *           |
383  *           v
384  *      +----------+
385  *      | A ++++++ |
386  *      +----------+
387  *
388  *
389  * After:
390  *              +--------+
391  *              | Parent |
392  *              +--------+
393  *                |     |
394  *                v     +------+
395  *          +---------+        |
396  *          | A* +++  |        v
397  *          +---------+   +-------+
398  *                        | B +++ |
399  *                        +-------+
400  *
401  * Where A* is a shadow of A.
402  */
403 static int btree_split_sibling(struct shadow_spine *s, dm_block_t root,
404                                unsigned parent_index, uint64_t key)
405 {
406         int r;
407         size_t size;
408         unsigned nr_left, nr_right;
409         struct dm_block *left, *right, *parent;
410         struct btree_node *ln, *rn, *pn;
411         __le64 location;
412
413         left = shadow_current(s);
414
415         r = new_block(s->info, &right);
416         if (r < 0)
417                 return r;
418
419         ln = dm_block_data(left);
420         rn = dm_block_data(right);
421
422         nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
423         nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
424
425         ln->header.nr_entries = cpu_to_le32(nr_left);
426
427         rn->header.flags = ln->header.flags;
428         rn->header.nr_entries = cpu_to_le32(nr_right);
429         rn->header.max_entries = ln->header.max_entries;
430         rn->header.value_size = ln->header.value_size;
431         memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
432
433         size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
434                 sizeof(uint64_t) : s->info->value_type.size;
435         memcpy(value_ptr(rn, 0, size), value_ptr(ln, nr_left, size),
436                size * nr_right);
437
438         /*
439          * Patch up the parent
440          */
441         parent = shadow_parent(s);
442
443         pn = dm_block_data(parent);
444         location = cpu_to_le64(dm_block_location(left));
445         __dm_bless_for_disk(&location);
446         memcpy_disk(value_ptr(pn, parent_index, sizeof(__le64)),
447                     &location, sizeof(__le64));
448
449         location = cpu_to_le64(dm_block_location(right));
450         __dm_bless_for_disk(&location);
451
452         r = insert_at(sizeof(__le64), pn, parent_index + 1,
453                       le64_to_cpu(rn->keys[0]), &location);
454         if (r) {
455                 unlock_block(s->info, right);
456                 return r;
457         }
458
459         if (key < le64_to_cpu(rn->keys[0])) {
460                 unlock_block(s->info, right);
461                 s->nodes[1] = left;
462         } else {
463                 unlock_block(s->info, left);
464                 s->nodes[1] = right;
465         }
466
467         return 0;
468 }
469
470 /*
471  * Splits a node by creating two new children beneath the given node.
472  *
473  * Before:
474  *        +----------+
475  *        | A ++++++ |
476  *        +----------+
477  *
478  *
479  * After:
480  *      +------------+
481  *      | A (shadow) |
482  *      +------------+
483  *          |   |
484  *   +------+   +----+
485  *   |               |
486  *   v               v
487  * +-------+     +-------+
488  * | B +++ |     | C +++ |
489  * +-------+     +-------+
490  */
491 static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
492 {
493         int r;
494         size_t size;
495         unsigned nr_left, nr_right;
496         struct dm_block *left, *right, *new_parent;
497         struct btree_node *pn, *ln, *rn;
498         __le64 val;
499
500         new_parent = shadow_current(s);
501
502         r = new_block(s->info, &left);
503         if (r < 0)
504                 return r;
505
506         r = new_block(s->info, &right);
507         if (r < 0) {
508                 unlock_block(s->info, left);
509                 return r;
510         }
511
512         pn = dm_block_data(new_parent);
513         ln = dm_block_data(left);
514         rn = dm_block_data(right);
515
516         nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
517         nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
518
519         ln->header.flags = pn->header.flags;
520         ln->header.nr_entries = cpu_to_le32(nr_left);
521         ln->header.max_entries = pn->header.max_entries;
522         ln->header.value_size = pn->header.value_size;
523
524         rn->header.flags = pn->header.flags;
525         rn->header.nr_entries = cpu_to_le32(nr_right);
526         rn->header.max_entries = pn->header.max_entries;
527         rn->header.value_size = pn->header.value_size;
528
529         memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
530         memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
531
532         size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
533                 sizeof(__le64) : s->info->value_type.size;
534         memcpy(value_ptr(ln, 0, size), value_ptr(pn, 0, size), nr_left * size);
535         memcpy(value_ptr(rn, 0, size), value_ptr(pn, nr_left, size),
536                nr_right * size);
537
538         /* new_parent should just point to l and r now */
539         pn->header.flags = cpu_to_le32(INTERNAL_NODE);
540         pn->header.nr_entries = cpu_to_le32(2);
541         pn->header.max_entries = cpu_to_le32(
542                 calc_max_entries(sizeof(__le64),
543                                  dm_bm_block_size(
544                                          dm_tm_get_bm(s->info->tm))));
545         pn->header.value_size = cpu_to_le32(sizeof(__le64));
546
547         val = cpu_to_le64(dm_block_location(left));
548         __dm_bless_for_disk(&val);
549         pn->keys[0] = ln->keys[0];
550         memcpy_disk(value_ptr(pn, 0, sizeof(__le64)), &val, sizeof(__le64));
551
552         val = cpu_to_le64(dm_block_location(right));
553         __dm_bless_for_disk(&val);
554         pn->keys[1] = rn->keys[0];
555         memcpy_disk(value_ptr(pn, 1, sizeof(__le64)), &val, sizeof(__le64));
556
557         /*
558          * rejig the spine.  This is ugly, since it knows too
559          * much about the spine
560          */
561         if (s->nodes[0] != new_parent) {
562                 unlock_block(s->info, s->nodes[0]);
563                 s->nodes[0] = new_parent;
564         }
565         if (key < le64_to_cpu(rn->keys[0])) {
566                 unlock_block(s->info, right);
567                 s->nodes[1] = left;
568         } else {
569                 unlock_block(s->info, left);
570                 s->nodes[1] = right;
571         }
572         s->count = 2;
573
574         return 0;
575 }
576
577 static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
578                             struct dm_btree_value_type *vt,
579                             uint64_t key, unsigned *index)
580 {
581         int r, i = *index, top = 1;
582         struct btree_node *node;
583
584         for (;;) {
585                 r = shadow_step(s, root, vt);
586                 if (r < 0)
587                         return r;
588
589                 node = dm_block_data(shadow_current(s));
590
591                 /*
592                  * We have to patch up the parent node, ugly, but I don't
593                  * see a way to do this automatically as part of the spine
594                  * op.
595                  */
596                 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
597                         __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
598
599                         __dm_bless_for_disk(&location);
600                         memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i, sizeof(uint64_t)),
601                                     &location, sizeof(__le64));
602                 }
603
604                 node = dm_block_data(shadow_current(s));
605
606                 if (node->header.nr_entries == node->header.max_entries) {
607                         if (top)
608                                 r = btree_split_beneath(s, key);
609                         else
610                                 r = btree_split_sibling(s, root, i, key);
611
612                         if (r < 0)
613                                 return r;
614                 }
615
616                 node = dm_block_data(shadow_current(s));
617
618                 i = lower_bound(node, key);
619
620                 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
621                         break;
622
623                 if (i < 0) {
624                         /* change the bounds on the lowest key */
625                         node->keys[0] = cpu_to_le64(key);
626                         i = 0;
627                 }
628
629                 root = value64(node, i);
630                 top = 0;
631         }
632
633         if (i < 0 || le64_to_cpu(node->keys[i]) != key)
634                 i++;
635
636         *index = i;
637         return 0;
638 }
639
640 static int insert(struct dm_btree_info *info, dm_block_t root,
641                   uint64_t *keys, void *value, dm_block_t *new_root,
642                   int *inserted)
643                   __dm_written_to_disk(value)
644 {
645         int r, need_insert;
646         unsigned level, index = -1, last_level = info->levels - 1;
647         dm_block_t block = root;
648         struct shadow_spine spine;
649         struct btree_node *n;
650         struct dm_btree_value_type le64_type;
651
652         init_le64_type(info->tm, &le64_type);
653         init_shadow_spine(&spine, info);
654
655         for (level = 0; level < (info->levels - 1); level++) {
656                 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
657                 if (r < 0)
658                         goto bad;
659
660                 n = dm_block_data(shadow_current(&spine));
661                 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
662                                (le64_to_cpu(n->keys[index]) != keys[level]));
663
664                 if (need_insert) {
665                         dm_block_t new_tree;
666                         __le64 new_le;
667
668                         r = dm_btree_empty(info, &new_tree);
669                         if (r < 0)
670                                 goto bad;
671
672                         new_le = cpu_to_le64(new_tree);
673                         __dm_bless_for_disk(&new_le);
674
675                         r = insert_at(sizeof(uint64_t), n, index,
676                                       keys[level], &new_le);
677                         if (r)
678                                 goto bad;
679                 }
680
681                 if (level < last_level)
682                         block = value64(n, index);
683         }
684
685         r = btree_insert_raw(&spine, block, &info->value_type,
686                              keys[level], &index);
687         if (r < 0)
688                 goto bad;
689
690         n = dm_block_data(shadow_current(&spine));
691         need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
692                        (le64_to_cpu(n->keys[index]) != keys[level]));
693
694         if (need_insert) {
695                 if (inserted)
696                         *inserted = 1;
697
698                 r = insert_at(info->value_type.size, n, index,
699                               keys[level], value);
700                 if (r)
701                         goto bad_unblessed;
702         } else {
703                 if (inserted)
704                         *inserted = 0;
705
706                 if (info->value_type.dec &&
707                     (!info->value_type.equal ||
708                      !info->value_type.equal(
709                              info->value_type.context,
710                              value_ptr(n, index, info->value_type.size),
711                              value))) {
712                         info->value_type.dec(info->value_type.context,
713                                              value_ptr(n, index, info->value_type.size));
714                 }
715                 memcpy_disk(value_ptr(n, index, info->value_type.size),
716                             value, info->value_type.size);
717         }
718
719         *new_root = shadow_root(&spine);
720         exit_shadow_spine(&spine);
721
722         return 0;
723
724 bad:
725         __dm_unbless_for_disk(value);
726 bad_unblessed:
727         exit_shadow_spine(&spine);
728         return r;
729 }
730
731 int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
732                     uint64_t *keys, void *value, dm_block_t *new_root)
733                     __dm_written_to_disk(value)
734 {
735         return insert(info, root, keys, value, new_root, NULL);
736 }
737 EXPORT_SYMBOL_GPL(dm_btree_insert);
738
739 int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
740                            uint64_t *keys, void *value, dm_block_t *new_root,
741                            int *inserted)
742                            __dm_written_to_disk(value)
743 {
744         return insert(info, root, keys, value, new_root, inserted);
745 }
746 EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
747
748 /*----------------------------------------------------------------*/
749
750 static int find_highest_key(struct ro_spine *s, dm_block_t block,
751                             uint64_t *result_key, dm_block_t *next_block)
752 {
753         int i, r;
754         uint32_t flags;
755
756         do {
757                 r = ro_step(s, block);
758                 if (r < 0)
759                         return r;
760
761                 flags = le32_to_cpu(ro_node(s)->header.flags);
762                 i = le32_to_cpu(ro_node(s)->header.nr_entries);
763                 if (!i)
764                         return -ENODATA;
765                 else
766                         i--;
767
768                 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
769                 if (next_block || flags & INTERNAL_NODE)
770                         block = value64(ro_node(s), i);
771
772         } while (flags & INTERNAL_NODE);
773
774         if (next_block)
775                 *next_block = block;
776         return 0;
777 }
778
779 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
780                               uint64_t *result_keys)
781 {
782         int r = 0, count = 0, level;
783         struct ro_spine spine;
784
785         init_ro_spine(&spine, info);
786         for (level = 0; level < info->levels; level++) {
787                 r = find_highest_key(&spine, root, result_keys + level,
788                                      level == info->levels - 1 ? NULL : &root);
789                 if (r == -ENODATA) {
790                         r = 0;
791                         break;
792
793                 } else if (r)
794                         break;
795
796                 count++;
797         }
798         exit_ro_spine(&spine);
799
800         return r ? r : count;
801 }
802 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);