blob: 0918a7c5f809766ea43faa0c5db96f1b22221325 [file] [log] [blame]
Joe Thornber3241b1d2011-10-31 20:19:11 +00001/*
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
Paul Gortmaker1944ce62011-09-28 18:29:32 -040011#include <linux/export.h>
Joe Thornber3241b1d2011-10-31 20:19:11 +000012#include <linux/device-mapper.h>
13
14#define DM_MSG_PREFIX "btree"
15
16/*----------------------------------------------------------------
17 * Array manipulation
18 *--------------------------------------------------------------*/
19static 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
26static 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. */
Mikulas Patocka550929f2012-12-21 20:23:30 +000041static int bsearch(struct btree_node *n, uint64_t key, int want_hi)
Joe Thornber3241b1d2011-10-31 20:19:11 +000042{
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
Mikulas Patocka550929f2012-12-21 20:23:30 +000061int lower_bound(struct btree_node *n, uint64_t key)
Joe Thornber3241b1d2011-10-31 20:19:11 +000062{
63 return bsearch(n, key, 0);
64}
65
Mikulas Patocka550929f2012-12-21 20:23:30 +000066void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
Joe Thornber3241b1d2011-10-31 20:19:11 +000067 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++)
Joe Thornbera3aefb32012-03-28 18:41:25 +010077 vt->inc(vt->context, value_ptr(n, i));
Joe Thornber3241b1d2011-10-31 20:19:11 +000078}
79
Mikulas Patocka550929f2012-12-21 20:23:30 +000080static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
Joe Thornber3241b1d2011-10-31 20:19:11 +000081 uint64_t key, void *value)
82 __dm_written_to_disk(value)
83{
84 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
85 __le64 key_le = cpu_to_le64(key);
86
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);
91 return -ENOMEM;
92 }
93
94 __dm_bless_for_disk(&key_le);
95
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);
99
100 return 0;
101}
102
103/*----------------------------------------------------------------*/
104
105/*
106 * We want 3n entries (for some n). This works more nicely for repeated
107 * insert remove loops than (2n + 1).
108 */
109static uint32_t calc_max_entries(size_t value_size, size_t block_size)
110{
111 uint32_t total, n;
112 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
113
114 block_size -= sizeof(struct node_header);
115 total = block_size / elt_size;
116 n = total / 3; /* rounds down */
117
118 return 3 * n;
119}
120
121int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
122{
123 int r;
124 struct dm_block *b;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000125 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000126 size_t block_size;
127 uint32_t max_entries;
128
129 r = new_block(info, &b);
130 if (r < 0)
131 return r;
132
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);
135
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);
142
143 *root = dm_block_location(b);
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400144 unlock_block(info, b);
145
146 return 0;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000147}
148EXPORT_SYMBOL_GPL(dm_btree_empty);
149
150/*----------------------------------------------------------------*/
151
152/*
153 * Deletion uses a recursive algorithm, since we have limited stack space
154 * we explicitly manage our own stack on the heap.
155 */
156#define MAX_SPINE_DEPTH 64
157struct frame {
158 struct dm_block *b;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000159 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000160 unsigned level;
161 unsigned nr_children;
162 unsigned current_child;
163};
164
165struct del_stack {
Joe Thornber04f17c82013-08-09 12:59:30 +0100166 struct dm_btree_info *info;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000167 struct dm_transaction_manager *tm;
168 int top;
169 struct frame spine[MAX_SPINE_DEPTH];
170};
171
172static int top_frame(struct del_stack *s, struct frame **f)
173{
174 if (s->top < 0) {
175 DMERR("btree deletion stack empty");
176 return -EINVAL;
177 }
178
179 *f = s->spine + s->top;
180
181 return 0;
182}
183
184static int unprocessed_frames(struct del_stack *s)
185{
186 return s->top >= 0;
187}
188
Joe Thornber04f17c82013-08-09 12:59:30 +0100189static void prefetch_children(struct del_stack *s, struct frame *f)
190{
191 unsigned i;
192 struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
193
194 for (i = 0; i < f->nr_children; i++)
195 dm_bm_prefetch(bm, value64(f->n, i));
196}
197
198static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
199{
200 return f->level < (info->levels - 1);
201}
202
Joe Thornber3241b1d2011-10-31 20:19:11 +0000203static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
204{
205 int r;
206 uint32_t ref_count;
207
208 if (s->top >= MAX_SPINE_DEPTH - 1) {
209 DMERR("btree deletion stack out of memory");
210 return -ENOMEM;
211 }
212
213 r = dm_tm_ref(s->tm, b, &ref_count);
214 if (r)
215 return r;
216
217 if (ref_count > 1)
218 /*
219 * This is a shared node, so we can just decrement it's
220 * reference counter and leave the children.
221 */
222 dm_tm_dec(s->tm, b);
223
224 else {
Joe Thornber04f17c82013-08-09 12:59:30 +0100225 uint32_t flags;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000226 struct frame *f = s->spine + ++s->top;
227
228 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
229 if (r) {
230 s->top--;
231 return r;
232 }
233
234 f->n = dm_block_data(f->b);
235 f->level = level;
236 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
237 f->current_child = 0;
Joe Thornber04f17c82013-08-09 12:59:30 +0100238
239 flags = le32_to_cpu(f->n->header.flags);
240 if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
241 prefetch_children(s, f);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000242 }
243
244 return 0;
245}
246
247static void pop_frame(struct del_stack *s)
248{
249 struct frame *f = s->spine + s->top--;
250
251 dm_tm_dec(s->tm, dm_block_location(f->b));
252 dm_tm_unlock(s->tm, f->b);
253}
254
255int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
256{
257 int r;
258 struct del_stack *s;
259
Joe Thornber1c751872015-07-03 14:51:32 +0100260 s = kmalloc(sizeof(*s), GFP_NOIO);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000261 if (!s)
262 return -ENOMEM;
Joe Thornber04f17c82013-08-09 12:59:30 +0100263 s->info = info;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000264 s->tm = info->tm;
265 s->top = -1;
266
Joe Thornbere3cbf942012-12-21 20:23:32 +0000267 r = push_frame(s, root, 0);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000268 if (r)
269 goto out;
270
271 while (unprocessed_frames(s)) {
272 uint32_t flags;
273 struct frame *f;
274 dm_block_t b;
275
276 r = top_frame(s, &f);
277 if (r)
278 goto out;
279
280 if (f->current_child >= f->nr_children) {
281 pop_frame(s);
282 continue;
283 }
284
285 flags = le32_to_cpu(f->n->header.flags);
286 if (flags & INTERNAL_NODE) {
287 b = value64(f->n, f->current_child);
288 f->current_child++;
289 r = push_frame(s, b, f->level);
290 if (r)
291 goto out;
292
Joe Thornbere3cbf942012-12-21 20:23:32 +0000293 } else if (is_internal_level(info, f)) {
Joe Thornber3241b1d2011-10-31 20:19:11 +0000294 b = value64(f->n, f->current_child);
295 f->current_child++;
296 r = push_frame(s, b, f->level + 1);
297 if (r)
298 goto out;
299
300 } else {
301 if (info->value_type.dec) {
302 unsigned i;
303
304 for (i = 0; i < f->nr_children; i++)
305 info->value_type.dec(info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100306 value_ptr(f->n, i));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000307 }
Joe Thornbercd5acf02013-08-09 12:48:42 +0100308 pop_frame(s);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000309 }
310 }
311
312out:
313 kfree(s);
314 return r;
315}
316EXPORT_SYMBOL_GPL(dm_btree_del);
317
318/*----------------------------------------------------------------*/
319
320static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
Mikulas Patocka550929f2012-12-21 20:23:30 +0000321 int (*search_fn)(struct btree_node *, uint64_t),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000322 uint64_t *result_key, void *v, size_t value_size)
323{
324 int i, r;
325 uint32_t flags, nr_entries;
326
327 do {
328 r = ro_step(s, block);
329 if (r < 0)
330 return r;
331
332 i = search_fn(ro_node(s), key);
333
334 flags = le32_to_cpu(ro_node(s)->header.flags);
335 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
336 if (i < 0 || i >= nr_entries)
337 return -ENODATA;
338
339 if (flags & INTERNAL_NODE)
340 block = value64(ro_node(s), i);
341
342 } while (!(flags & LEAF_NODE));
343
344 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100345 memcpy(v, value_ptr(ro_node(s), i), value_size);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000346
347 return 0;
348}
349
350int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
351 uint64_t *keys, void *value_le)
352{
353 unsigned level, last_level = info->levels - 1;
354 int r = -ENODATA;
355 uint64_t rkey;
356 __le64 internal_value_le;
357 struct ro_spine spine;
358
359 init_ro_spine(&spine, info);
360 for (level = 0; level < info->levels; level++) {
361 size_t size;
362 void *value_p;
363
364 if (level == last_level) {
365 value_p = value_le;
366 size = info->value_type.size;
367
368 } else {
369 value_p = &internal_value_le;
370 size = sizeof(uint64_t);
371 }
372
373 r = btree_lookup_raw(&spine, root, keys[level],
374 lower_bound, &rkey,
375 value_p, size);
376
377 if (!r) {
378 if (rkey != keys[level]) {
379 exit_ro_spine(&spine);
380 return -ENODATA;
381 }
382 } else {
383 exit_ro_spine(&spine);
384 return r;
385 }
386
387 root = le64_to_cpu(internal_value_le);
388 }
389 exit_ro_spine(&spine);
390
391 return r;
392}
393EXPORT_SYMBOL_GPL(dm_btree_lookup);
394
395/*
396 * Splits a node by creating a sibling node and shifting half the nodes
397 * contents across. Assumes there is a parent node, and it has room for
398 * another child.
399 *
400 * Before:
401 * +--------+
402 * | Parent |
403 * +--------+
404 * |
405 * v
406 * +----------+
407 * | A ++++++ |
408 * +----------+
409 *
410 *
411 * After:
412 * +--------+
413 * | Parent |
414 * +--------+
415 * | |
416 * v +------+
417 * +---------+ |
418 * | A* +++ | v
419 * +---------+ +-------+
420 * | B +++ |
421 * +-------+
422 *
423 * Where A* is a shadow of A.
424 */
Vivek Goyal0a8d4c32015-07-06 11:55:40 -0400425static int btree_split_sibling(struct shadow_spine *s, unsigned parent_index,
426 uint64_t key)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000427{
428 int r;
429 size_t size;
430 unsigned nr_left, nr_right;
431 struct dm_block *left, *right, *parent;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000432 struct btree_node *ln, *rn, *pn;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000433 __le64 location;
434
435 left = shadow_current(s);
436
437 r = new_block(s->info, &right);
438 if (r < 0)
439 return r;
440
441 ln = dm_block_data(left);
442 rn = dm_block_data(right);
443
444 nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
445 nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
446
447 ln->header.nr_entries = cpu_to_le32(nr_left);
448
449 rn->header.flags = ln->header.flags;
450 rn->header.nr_entries = cpu_to_le32(nr_right);
451 rn->header.max_entries = ln->header.max_entries;
452 rn->header.value_size = ln->header.value_size;
453 memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
454
455 size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
456 sizeof(uint64_t) : s->info->value_type.size;
Joe Thornbera3aefb32012-03-28 18:41:25 +0100457 memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000458 size * nr_right);
459
460 /*
461 * Patch up the parent
462 */
463 parent = shadow_parent(s);
464
465 pn = dm_block_data(parent);
466 location = cpu_to_le64(dm_block_location(left));
467 __dm_bless_for_disk(&location);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100468 memcpy_disk(value_ptr(pn, parent_index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000469 &location, sizeof(__le64));
470
471 location = cpu_to_le64(dm_block_location(right));
472 __dm_bless_for_disk(&location);
473
474 r = insert_at(sizeof(__le64), pn, parent_index + 1,
475 le64_to_cpu(rn->keys[0]), &location);
Mike Snitzer30ce6e12015-11-23 16:24:45 -0500476 if (r) {
477 unlock_block(s->info, right);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000478 return r;
Mike Snitzer30ce6e12015-11-23 16:24:45 -0500479 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000480
481 if (key < le64_to_cpu(rn->keys[0])) {
482 unlock_block(s->info, right);
483 s->nodes[1] = left;
484 } else {
485 unlock_block(s->info, left);
486 s->nodes[1] = right;
487 }
488
489 return 0;
490}
491
492/*
493 * Splits a node by creating two new children beneath the given node.
494 *
495 * Before:
496 * +----------+
497 * | A ++++++ |
498 * +----------+
499 *
500 *
501 * After:
502 * +------------+
503 * | A (shadow) |
504 * +------------+
505 * | |
506 * +------+ +----+
507 * | |
508 * v v
509 * +-------+ +-------+
510 * | B +++ | | C +++ |
511 * +-------+ +-------+
512 */
513static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
514{
515 int r;
516 size_t size;
517 unsigned nr_left, nr_right;
518 struct dm_block *left, *right, *new_parent;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000519 struct btree_node *pn, *ln, *rn;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000520 __le64 val;
521
522 new_parent = shadow_current(s);
523
524 r = new_block(s->info, &left);
525 if (r < 0)
526 return r;
527
528 r = new_block(s->info, &right);
529 if (r < 0) {
Mike Snitzer4dcb8b52015-10-22 10:56:40 -0400530 unlock_block(s->info, left);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000531 return r;
532 }
533
534 pn = dm_block_data(new_parent);
535 ln = dm_block_data(left);
536 rn = dm_block_data(right);
537
538 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
539 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
540
541 ln->header.flags = pn->header.flags;
542 ln->header.nr_entries = cpu_to_le32(nr_left);
543 ln->header.max_entries = pn->header.max_entries;
544 ln->header.value_size = pn->header.value_size;
545
546 rn->header.flags = pn->header.flags;
547 rn->header.nr_entries = cpu_to_le32(nr_right);
548 rn->header.max_entries = pn->header.max_entries;
549 rn->header.value_size = pn->header.value_size;
550
551 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
552 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
553
554 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
555 sizeof(__le64) : s->info->value_type.size;
Joe Thornbera3aefb32012-03-28 18:41:25 +0100556 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
557 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000558 nr_right * size);
559
560 /* new_parent should just point to l and r now */
561 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
562 pn->header.nr_entries = cpu_to_le32(2);
563 pn->header.max_entries = cpu_to_le32(
564 calc_max_entries(sizeof(__le64),
565 dm_bm_block_size(
566 dm_tm_get_bm(s->info->tm))));
567 pn->header.value_size = cpu_to_le32(sizeof(__le64));
568
569 val = cpu_to_le64(dm_block_location(left));
570 __dm_bless_for_disk(&val);
571 pn->keys[0] = ln->keys[0];
Joe Thornbera3aefb32012-03-28 18:41:25 +0100572 memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000573
574 val = cpu_to_le64(dm_block_location(right));
575 __dm_bless_for_disk(&val);
576 pn->keys[1] = rn->keys[0];
Joe Thornbera3aefb32012-03-28 18:41:25 +0100577 memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000578
579 /*
580 * rejig the spine. This is ugly, since it knows too
581 * much about the spine
582 */
583 if (s->nodes[0] != new_parent) {
584 unlock_block(s->info, s->nodes[0]);
585 s->nodes[0] = new_parent;
586 }
587 if (key < le64_to_cpu(rn->keys[0])) {
588 unlock_block(s->info, right);
589 s->nodes[1] = left;
590 } else {
591 unlock_block(s->info, left);
592 s->nodes[1] = right;
593 }
594 s->count = 2;
595
596 return 0;
597}
598
599static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
600 struct dm_btree_value_type *vt,
601 uint64_t key, unsigned *index)
602{
603 int r, i = *index, top = 1;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000604 struct btree_node *node;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000605
606 for (;;) {
607 r = shadow_step(s, root, vt);
608 if (r < 0)
609 return r;
610
611 node = dm_block_data(shadow_current(s));
612
613 /*
614 * We have to patch up the parent node, ugly, but I don't
615 * see a way to do this automatically as part of the spine
616 * op.
617 */
618 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
619 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
620
621 __dm_bless_for_disk(&location);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100622 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000623 &location, sizeof(__le64));
624 }
625
626 node = dm_block_data(shadow_current(s));
627
628 if (node->header.nr_entries == node->header.max_entries) {
629 if (top)
630 r = btree_split_beneath(s, key);
631 else
Vivek Goyal0a8d4c32015-07-06 11:55:40 -0400632 r = btree_split_sibling(s, i, key);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000633
634 if (r < 0)
635 return r;
636 }
637
638 node = dm_block_data(shadow_current(s));
639
640 i = lower_bound(node, key);
641
642 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
643 break;
644
645 if (i < 0) {
646 /* change the bounds on the lowest key */
647 node->keys[0] = cpu_to_le64(key);
648 i = 0;
649 }
650
651 root = value64(node, i);
652 top = 0;
653 }
654
655 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
656 i++;
657
658 *index = i;
659 return 0;
660}
661
662static int insert(struct dm_btree_info *info, dm_block_t root,
663 uint64_t *keys, void *value, dm_block_t *new_root,
664 int *inserted)
665 __dm_written_to_disk(value)
666{
667 int r, need_insert;
668 unsigned level, index = -1, last_level = info->levels - 1;
669 dm_block_t block = root;
670 struct shadow_spine spine;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000671 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000672 struct dm_btree_value_type le64_type;
673
Joe Thornberb0dc3c82015-08-12 15:12:09 +0100674 init_le64_type(info->tm, &le64_type);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000675 init_shadow_spine(&spine, info);
676
677 for (level = 0; level < (info->levels - 1); level++) {
678 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
679 if (r < 0)
680 goto bad;
681
682 n = dm_block_data(shadow_current(&spine));
683 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
684 (le64_to_cpu(n->keys[index]) != keys[level]));
685
686 if (need_insert) {
687 dm_block_t new_tree;
688 __le64 new_le;
689
690 r = dm_btree_empty(info, &new_tree);
691 if (r < 0)
692 goto bad;
693
694 new_le = cpu_to_le64(new_tree);
695 __dm_bless_for_disk(&new_le);
696
697 r = insert_at(sizeof(uint64_t), n, index,
698 keys[level], &new_le);
699 if (r)
700 goto bad;
701 }
702
703 if (level < last_level)
704 block = value64(n, index);
705 }
706
707 r = btree_insert_raw(&spine, block, &info->value_type,
708 keys[level], &index);
709 if (r < 0)
710 goto bad;
711
712 n = dm_block_data(shadow_current(&spine));
713 need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
714 (le64_to_cpu(n->keys[index]) != keys[level]));
715
716 if (need_insert) {
717 if (inserted)
718 *inserted = 1;
719
720 r = insert_at(info->value_type.size, n, index,
721 keys[level], value);
722 if (r)
723 goto bad_unblessed;
724 } else {
725 if (inserted)
726 *inserted = 0;
727
728 if (info->value_type.dec &&
729 (!info->value_type.equal ||
730 !info->value_type.equal(
731 info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100732 value_ptr(n, index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000733 value))) {
734 info->value_type.dec(info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100735 value_ptr(n, index));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000736 }
Joe Thornbera3aefb32012-03-28 18:41:25 +0100737 memcpy_disk(value_ptr(n, index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000738 value, info->value_type.size);
739 }
740
741 *new_root = shadow_root(&spine);
742 exit_shadow_spine(&spine);
743
744 return 0;
745
746bad:
747 __dm_unbless_for_disk(value);
748bad_unblessed:
749 exit_shadow_spine(&spine);
750 return r;
751}
752
753int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
754 uint64_t *keys, void *value, dm_block_t *new_root)
755 __dm_written_to_disk(value)
756{
757 return insert(info, root, keys, value, new_root, NULL);
758}
759EXPORT_SYMBOL_GPL(dm_btree_insert);
760
761int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
762 uint64_t *keys, void *value, dm_block_t *new_root,
763 int *inserted)
764 __dm_written_to_disk(value)
765{
766 return insert(info, root, keys, value, new_root, inserted);
767}
768EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
769
770/*----------------------------------------------------------------*/
771
Joe Thornberf164e692013-12-20 15:41:11 +0000772static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
773 uint64_t *result_key, dm_block_t *next_block)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000774{
775 int i, r;
776 uint32_t flags;
777
778 do {
779 r = ro_step(s, block);
780 if (r < 0)
781 return r;
782
783 flags = le32_to_cpu(ro_node(s)->header.flags);
784 i = le32_to_cpu(ro_node(s)->header.nr_entries);
785 if (!i)
786 return -ENODATA;
787 else
788 i--;
789
Joe Thornberf164e692013-12-20 15:41:11 +0000790 if (find_highest)
791 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
792 else
793 *result_key = le64_to_cpu(ro_node(s)->keys[0]);
794
Joe Thornber3241b1d2011-10-31 20:19:11 +0000795 if (next_block || flags & INTERNAL_NODE)
796 block = value64(ro_node(s), i);
797
798 } while (flags & INTERNAL_NODE);
799
800 if (next_block)
801 *next_block = block;
802 return 0;
803}
804
Joe Thornberf164e692013-12-20 15:41:11 +0000805static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
806 bool find_highest, uint64_t *result_keys)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000807{
808 int r = 0, count = 0, level;
809 struct ro_spine spine;
810
811 init_ro_spine(&spine, info);
812 for (level = 0; level < info->levels; level++) {
Joe Thornberf164e692013-12-20 15:41:11 +0000813 r = find_key(&spine, root, find_highest, result_keys + level,
814 level == info->levels - 1 ? NULL : &root);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000815 if (r == -ENODATA) {
816 r = 0;
817 break;
818
819 } else if (r)
820 break;
821
822 count++;
823 }
824 exit_ro_spine(&spine);
825
826 return r ? r : count;
827}
Joe Thornberf164e692013-12-20 15:41:11 +0000828
829int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
830 uint64_t *result_keys)
831{
832 return dm_btree_find_key(info, root, true, result_keys);
833}
Joe Thornber3241b1d2011-10-31 20:19:11 +0000834EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000835
Joe Thornberf164e692013-12-20 15:41:11 +0000836int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
837 uint64_t *result_keys)
838{
839 return dm_btree_find_key(info, root, false, result_keys);
840}
841EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
842
843/*----------------------------------------------------------------*/
844
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000845/*
846 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
847 * space. Also this only works for single level trees.
848 */
Joe Thornber9b460d32014-11-10 15:03:24 +0000849static int walk_node(struct dm_btree_info *info, dm_block_t block,
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000850 int (*fn)(void *context, uint64_t *keys, void *leaf),
851 void *context)
852{
853 int r;
854 unsigned i, nr;
Joe Thornber9b460d32014-11-10 15:03:24 +0000855 struct dm_block *node;
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000856 struct btree_node *n;
857 uint64_t keys;
858
Joe Thornber9b460d32014-11-10 15:03:24 +0000859 r = bn_read_lock(info, block, &node);
860 if (r)
861 return r;
862
863 n = dm_block_data(node);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000864
865 nr = le32_to_cpu(n->header.nr_entries);
866 for (i = 0; i < nr; i++) {
867 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
Joe Thornber9b460d32014-11-10 15:03:24 +0000868 r = walk_node(info, value64(n, i), fn, context);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000869 if (r)
870 goto out;
871 } else {
872 keys = le64_to_cpu(*key_ptr(n, i));
873 r = fn(context, &keys, value_ptr(n, i));
874 if (r)
875 goto out;
876 }
877 }
878
879out:
Joe Thornber9b460d32014-11-10 15:03:24 +0000880 dm_tm_unlock(info->tm, node);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000881 return r;
882}
883
884int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
885 int (*fn)(void *context, uint64_t *keys, void *leaf),
886 void *context)
887{
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000888 BUG_ON(info->levels > 1);
Joe Thornber9b460d32014-11-10 15:03:24 +0000889 return walk_node(info, root, fn, context);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000890}
891EXPORT_SYMBOL_GPL(dm_btree_walk);