blob: 7a75b5010f735c236de0944006ad680b95211802 [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
Joe Thornber993ceab2015-12-02 12:24:39 +000066static int upper_bound(struct btree_node *n, uint64_t key)
67{
68 return bsearch(n, key, 1);
69}
70
Mikulas Patocka550929f2012-12-21 20:23:30 +000071void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
Joe Thornber3241b1d2011-10-31 20:19:11 +000072 struct dm_btree_value_type *vt)
73{
74 unsigned i;
75 uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
76
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));
80 else if (vt->inc)
81 for (i = 0; i < nr_entries; i++)
Joe Thornbera3aefb32012-03-28 18:41:25 +010082 vt->inc(vt->context, value_ptr(n, i));
Joe Thornber3241b1d2011-10-31 20:19:11 +000083}
84
Mikulas Patocka550929f2012-12-21 20:23:30 +000085static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
Joe Thornber3241b1d2011-10-31 20:19:11 +000086 uint64_t key, void *value)
87 __dm_written_to_disk(value)
88{
89 uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
90 __le64 key_le = cpu_to_le64(key);
91
92 if (index > nr_entries ||
93 index >= le32_to_cpu(node->header.max_entries)) {
94 DMERR("too many entries in btree node for insert");
95 __dm_unbless_for_disk(value);
96 return -ENOMEM;
97 }
98
99 __dm_bless_for_disk(&key_le);
100
101 array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
102 array_insert(value_base(node), value_size, nr_entries, index, value);
103 node->header.nr_entries = cpu_to_le32(nr_entries + 1);
104
105 return 0;
106}
107
108/*----------------------------------------------------------------*/
109
110/*
111 * We want 3n entries (for some n). This works more nicely for repeated
112 * insert remove loops than (2n + 1).
113 */
114static uint32_t calc_max_entries(size_t value_size, size_t block_size)
115{
116 uint32_t total, n;
117 size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
118
119 block_size -= sizeof(struct node_header);
120 total = block_size / elt_size;
121 n = total / 3; /* rounds down */
122
123 return 3 * n;
124}
125
126int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
127{
128 int r;
129 struct dm_block *b;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000130 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000131 size_t block_size;
132 uint32_t max_entries;
133
134 r = new_block(info, &b);
135 if (r < 0)
136 return r;
137
138 block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
139 max_entries = calc_max_entries(info->value_type.size, block_size);
140
141 n = dm_block_data(b);
142 memset(n, 0, block_size);
143 n->header.flags = cpu_to_le32(LEAF_NODE);
144 n->header.nr_entries = cpu_to_le32(0);
145 n->header.max_entries = cpu_to_le32(max_entries);
146 n->header.value_size = cpu_to_le32(info->value_type.size);
147
148 *root = dm_block_location(b);
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400149 unlock_block(info, b);
150
151 return 0;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000152}
153EXPORT_SYMBOL_GPL(dm_btree_empty);
154
155/*----------------------------------------------------------------*/
156
157/*
158 * Deletion uses a recursive algorithm, since we have limited stack space
159 * we explicitly manage our own stack on the heap.
160 */
161#define MAX_SPINE_DEPTH 64
162struct frame {
163 struct dm_block *b;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000164 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000165 unsigned level;
166 unsigned nr_children;
167 unsigned current_child;
168};
169
170struct del_stack {
Joe Thornber04f17c82013-08-09 12:59:30 +0100171 struct dm_btree_info *info;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000172 struct dm_transaction_manager *tm;
173 int top;
174 struct frame spine[MAX_SPINE_DEPTH];
175};
176
177static int top_frame(struct del_stack *s, struct frame **f)
178{
179 if (s->top < 0) {
180 DMERR("btree deletion stack empty");
181 return -EINVAL;
182 }
183
184 *f = s->spine + s->top;
185
186 return 0;
187}
188
189static int unprocessed_frames(struct del_stack *s)
190{
191 return s->top >= 0;
192}
193
Joe Thornber04f17c82013-08-09 12:59:30 +0100194static void prefetch_children(struct del_stack *s, struct frame *f)
195{
196 unsigned i;
197 struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
198
199 for (i = 0; i < f->nr_children; i++)
200 dm_bm_prefetch(bm, value64(f->n, i));
201}
202
203static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
204{
205 return f->level < (info->levels - 1);
206}
207
Joe Thornber3241b1d2011-10-31 20:19:11 +0000208static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
209{
210 int r;
211 uint32_t ref_count;
212
213 if (s->top >= MAX_SPINE_DEPTH - 1) {
214 DMERR("btree deletion stack out of memory");
215 return -ENOMEM;
216 }
217
218 r = dm_tm_ref(s->tm, b, &ref_count);
219 if (r)
220 return r;
221
222 if (ref_count > 1)
223 /*
224 * This is a shared node, so we can just decrement it's
225 * reference counter and leave the children.
226 */
227 dm_tm_dec(s->tm, b);
228
229 else {
Joe Thornber04f17c82013-08-09 12:59:30 +0100230 uint32_t flags;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000231 struct frame *f = s->spine + ++s->top;
232
233 r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
234 if (r) {
235 s->top--;
236 return r;
237 }
238
239 f->n = dm_block_data(f->b);
240 f->level = level;
241 f->nr_children = le32_to_cpu(f->n->header.nr_entries);
242 f->current_child = 0;
Joe Thornber04f17c82013-08-09 12:59:30 +0100243
244 flags = le32_to_cpu(f->n->header.flags);
245 if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
246 prefetch_children(s, f);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000247 }
248
249 return 0;
250}
251
252static void pop_frame(struct del_stack *s)
253{
254 struct frame *f = s->spine + s->top--;
255
256 dm_tm_dec(s->tm, dm_block_location(f->b));
257 dm_tm_unlock(s->tm, f->b);
258}
259
Joe Thornbered8b45a2015-12-10 14:37:53 +0000260static void unlock_all_frames(struct del_stack *s)
261{
262 struct frame *f;
263
264 while (unprocessed_frames(s)) {
265 f = s->spine + s->top--;
266 dm_tm_unlock(s->tm, f->b);
267 }
268}
269
Joe Thornber3241b1d2011-10-31 20:19:11 +0000270int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
271{
272 int r;
273 struct del_stack *s;
274
Joe Thornber1c751872015-07-03 14:51:32 +0100275 s = kmalloc(sizeof(*s), GFP_NOIO);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000276 if (!s)
277 return -ENOMEM;
Joe Thornber04f17c82013-08-09 12:59:30 +0100278 s->info = info;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000279 s->tm = info->tm;
280 s->top = -1;
281
Joe Thornbere3cbf942012-12-21 20:23:32 +0000282 r = push_frame(s, root, 0);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000283 if (r)
284 goto out;
285
286 while (unprocessed_frames(s)) {
287 uint32_t flags;
288 struct frame *f;
289 dm_block_t b;
290
291 r = top_frame(s, &f);
292 if (r)
293 goto out;
294
295 if (f->current_child >= f->nr_children) {
296 pop_frame(s);
297 continue;
298 }
299
300 flags = le32_to_cpu(f->n->header.flags);
301 if (flags & INTERNAL_NODE) {
302 b = value64(f->n, f->current_child);
303 f->current_child++;
304 r = push_frame(s, b, f->level);
305 if (r)
306 goto out;
307
Joe Thornbere3cbf942012-12-21 20:23:32 +0000308 } else if (is_internal_level(info, f)) {
Joe Thornber3241b1d2011-10-31 20:19:11 +0000309 b = value64(f->n, f->current_child);
310 f->current_child++;
311 r = push_frame(s, b, f->level + 1);
312 if (r)
313 goto out;
314
315 } else {
316 if (info->value_type.dec) {
317 unsigned i;
318
319 for (i = 0; i < f->nr_children; i++)
320 info->value_type.dec(info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100321 value_ptr(f->n, i));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000322 }
Joe Thornbercd5acf02013-08-09 12:48:42 +0100323 pop_frame(s);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000324 }
325 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000326out:
Joe Thornbered8b45a2015-12-10 14:37:53 +0000327 if (r) {
328 /* cleanup all frames of del_stack */
329 unlock_all_frames(s);
330 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000331 kfree(s);
Joe Thornbered8b45a2015-12-10 14:37:53 +0000332
Joe Thornber3241b1d2011-10-31 20:19:11 +0000333 return r;
334}
335EXPORT_SYMBOL_GPL(dm_btree_del);
336
337/*----------------------------------------------------------------*/
338
339static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
Mikulas Patocka550929f2012-12-21 20:23:30 +0000340 int (*search_fn)(struct btree_node *, uint64_t),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000341 uint64_t *result_key, void *v, size_t value_size)
342{
343 int i, r;
344 uint32_t flags, nr_entries;
345
346 do {
347 r = ro_step(s, block);
348 if (r < 0)
349 return r;
350
351 i = search_fn(ro_node(s), key);
352
353 flags = le32_to_cpu(ro_node(s)->header.flags);
354 nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
355 if (i < 0 || i >= nr_entries)
356 return -ENODATA;
357
358 if (flags & INTERNAL_NODE)
359 block = value64(ro_node(s), i);
360
361 } while (!(flags & LEAF_NODE));
362
363 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100364 memcpy(v, value_ptr(ro_node(s), i), value_size);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000365
366 return 0;
367}
368
369int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
370 uint64_t *keys, void *value_le)
371{
372 unsigned level, last_level = info->levels - 1;
373 int r = -ENODATA;
374 uint64_t rkey;
375 __le64 internal_value_le;
376 struct ro_spine spine;
377
378 init_ro_spine(&spine, info);
379 for (level = 0; level < info->levels; level++) {
380 size_t size;
381 void *value_p;
382
383 if (level == last_level) {
384 value_p = value_le;
385 size = info->value_type.size;
386
387 } else {
388 value_p = &internal_value_le;
389 size = sizeof(uint64_t);
390 }
391
392 r = btree_lookup_raw(&spine, root, keys[level],
393 lower_bound, &rkey,
394 value_p, size);
395
396 if (!r) {
397 if (rkey != keys[level]) {
398 exit_ro_spine(&spine);
399 return -ENODATA;
400 }
401 } else {
402 exit_ro_spine(&spine);
403 return r;
404 }
405
406 root = le64_to_cpu(internal_value_le);
407 }
408 exit_ro_spine(&spine);
409
410 return r;
411}
412EXPORT_SYMBOL_GPL(dm_btree_lookup);
413
Joe Thornber993ceab2015-12-02 12:24:39 +0000414static int dm_btree_lookup_next_single(struct dm_btree_info *info, dm_block_t root,
415 uint64_t key, uint64_t *rkey, void *value_le)
416{
417 int r, i;
418 uint32_t flags, nr_entries;
419 struct dm_block *node;
420 struct btree_node *n;
421
422 r = bn_read_lock(info, root, &node);
423 if (r)
424 return r;
425
426 n = dm_block_data(node);
427 flags = le32_to_cpu(n->header.flags);
428 nr_entries = le32_to_cpu(n->header.nr_entries);
429
430 if (flags & INTERNAL_NODE) {
431 i = lower_bound(n, key);
Joe Thornbere7e0f732016-07-01 11:09:13 +0100432 if (i < 0) {
433 /*
434 * avoid early -ENODATA return when all entries are
435 * higher than the search @key.
436 */
437 i = 0;
438 }
439 if (i >= nr_entries) {
Joe Thornber993ceab2015-12-02 12:24:39 +0000440 r = -ENODATA;
441 goto out;
442 }
443
444 r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
445 if (r == -ENODATA && i < (nr_entries - 1)) {
446 i++;
447 r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
448 }
449
450 } else {
451 i = upper_bound(n, key);
452 if (i < 0 || i >= nr_entries) {
453 r = -ENODATA;
454 goto out;
455 }
456
457 *rkey = le64_to_cpu(n->keys[i]);
458 memcpy(value_le, value_ptr(n, i), info->value_type.size);
459 }
460out:
461 dm_tm_unlock(info->tm, node);
462 return r;
463}
464
465int dm_btree_lookup_next(struct dm_btree_info *info, dm_block_t root,
466 uint64_t *keys, uint64_t *rkey, void *value_le)
467{
468 unsigned level;
469 int r = -ENODATA;
470 __le64 internal_value_le;
471 struct ro_spine spine;
472
473 init_ro_spine(&spine, info);
474 for (level = 0; level < info->levels - 1u; level++) {
475 r = btree_lookup_raw(&spine, root, keys[level],
476 lower_bound, rkey,
477 &internal_value_le, sizeof(uint64_t));
478 if (r)
479 goto out;
480
481 if (*rkey != keys[level]) {
482 r = -ENODATA;
483 goto out;
484 }
485
486 root = le64_to_cpu(internal_value_le);
487 }
488
489 r = dm_btree_lookup_next_single(info, root, keys[level], rkey, value_le);
490out:
491 exit_ro_spine(&spine);
492 return r;
493}
494
495EXPORT_SYMBOL_GPL(dm_btree_lookup_next);
496
Joe Thornber3241b1d2011-10-31 20:19:11 +0000497/*
498 * Splits a node by creating a sibling node and shifting half the nodes
499 * contents across. Assumes there is a parent node, and it has room for
500 * another child.
501 *
502 * Before:
503 * +--------+
504 * | Parent |
505 * +--------+
506 * |
507 * v
508 * +----------+
509 * | A ++++++ |
510 * +----------+
511 *
512 *
513 * After:
514 * +--------+
515 * | Parent |
516 * +--------+
517 * | |
518 * v +------+
519 * +---------+ |
520 * | A* +++ | v
521 * +---------+ +-------+
522 * | B +++ |
523 * +-------+
524 *
525 * Where A* is a shadow of A.
526 */
Vivek Goyal0a8d4c32015-07-06 11:55:40 -0400527static int btree_split_sibling(struct shadow_spine *s, unsigned parent_index,
528 uint64_t key)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000529{
530 int r;
531 size_t size;
532 unsigned nr_left, nr_right;
533 struct dm_block *left, *right, *parent;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000534 struct btree_node *ln, *rn, *pn;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000535 __le64 location;
536
537 left = shadow_current(s);
538
539 r = new_block(s->info, &right);
540 if (r < 0)
541 return r;
542
543 ln = dm_block_data(left);
544 rn = dm_block_data(right);
545
546 nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
547 nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
548
549 ln->header.nr_entries = cpu_to_le32(nr_left);
550
551 rn->header.flags = ln->header.flags;
552 rn->header.nr_entries = cpu_to_le32(nr_right);
553 rn->header.max_entries = ln->header.max_entries;
554 rn->header.value_size = ln->header.value_size;
555 memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
556
557 size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
558 sizeof(uint64_t) : s->info->value_type.size;
Joe Thornbera3aefb32012-03-28 18:41:25 +0100559 memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000560 size * nr_right);
561
562 /*
563 * Patch up the parent
564 */
565 parent = shadow_parent(s);
566
567 pn = dm_block_data(parent);
568 location = cpu_to_le64(dm_block_location(left));
569 __dm_bless_for_disk(&location);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100570 memcpy_disk(value_ptr(pn, parent_index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000571 &location, sizeof(__le64));
572
573 location = cpu_to_le64(dm_block_location(right));
574 __dm_bless_for_disk(&location);
575
576 r = insert_at(sizeof(__le64), pn, parent_index + 1,
577 le64_to_cpu(rn->keys[0]), &location);
Mike Snitzer30ce6e12015-11-23 16:24:45 -0500578 if (r) {
579 unlock_block(s->info, right);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000580 return r;
Mike Snitzer30ce6e12015-11-23 16:24:45 -0500581 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000582
583 if (key < le64_to_cpu(rn->keys[0])) {
584 unlock_block(s->info, right);
585 s->nodes[1] = left;
586 } else {
587 unlock_block(s->info, left);
588 s->nodes[1] = right;
589 }
590
591 return 0;
592}
593
594/*
595 * Splits a node by creating two new children beneath the given node.
596 *
597 * Before:
598 * +----------+
599 * | A ++++++ |
600 * +----------+
601 *
602 *
603 * After:
604 * +------------+
605 * | A (shadow) |
606 * +------------+
607 * | |
608 * +------+ +----+
609 * | |
610 * v v
611 * +-------+ +-------+
612 * | B +++ | | C +++ |
613 * +-------+ +-------+
614 */
615static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
616{
617 int r;
618 size_t size;
619 unsigned nr_left, nr_right;
620 struct dm_block *left, *right, *new_parent;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000621 struct btree_node *pn, *ln, *rn;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000622 __le64 val;
623
624 new_parent = shadow_current(s);
625
626 r = new_block(s->info, &left);
627 if (r < 0)
628 return r;
629
630 r = new_block(s->info, &right);
631 if (r < 0) {
Mike Snitzer4dcb8b52015-10-22 10:56:40 -0400632 unlock_block(s->info, left);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000633 return r;
634 }
635
636 pn = dm_block_data(new_parent);
637 ln = dm_block_data(left);
638 rn = dm_block_data(right);
639
640 nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
641 nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
642
643 ln->header.flags = pn->header.flags;
644 ln->header.nr_entries = cpu_to_le32(nr_left);
645 ln->header.max_entries = pn->header.max_entries;
646 ln->header.value_size = pn->header.value_size;
647
648 rn->header.flags = pn->header.flags;
649 rn->header.nr_entries = cpu_to_le32(nr_right);
650 rn->header.max_entries = pn->header.max_entries;
651 rn->header.value_size = pn->header.value_size;
652
653 memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
654 memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
655
656 size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
657 sizeof(__le64) : s->info->value_type.size;
Joe Thornbera3aefb32012-03-28 18:41:25 +0100658 memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
659 memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000660 nr_right * size);
661
662 /* new_parent should just point to l and r now */
663 pn->header.flags = cpu_to_le32(INTERNAL_NODE);
664 pn->header.nr_entries = cpu_to_le32(2);
665 pn->header.max_entries = cpu_to_le32(
666 calc_max_entries(sizeof(__le64),
667 dm_bm_block_size(
668 dm_tm_get_bm(s->info->tm))));
669 pn->header.value_size = cpu_to_le32(sizeof(__le64));
670
671 val = cpu_to_le64(dm_block_location(left));
672 __dm_bless_for_disk(&val);
673 pn->keys[0] = ln->keys[0];
Joe Thornbera3aefb32012-03-28 18:41:25 +0100674 memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000675
676 val = cpu_to_le64(dm_block_location(right));
677 __dm_bless_for_disk(&val);
678 pn->keys[1] = rn->keys[0];
Joe Thornbera3aefb32012-03-28 18:41:25 +0100679 memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000680
681 /*
682 * rejig the spine. This is ugly, since it knows too
683 * much about the spine
684 */
685 if (s->nodes[0] != new_parent) {
686 unlock_block(s->info, s->nodes[0]);
687 s->nodes[0] = new_parent;
688 }
689 if (key < le64_to_cpu(rn->keys[0])) {
690 unlock_block(s->info, right);
691 s->nodes[1] = left;
692 } else {
693 unlock_block(s->info, left);
694 s->nodes[1] = right;
695 }
696 s->count = 2;
697
698 return 0;
699}
700
701static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
702 struct dm_btree_value_type *vt,
703 uint64_t key, unsigned *index)
704{
705 int r, i = *index, top = 1;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000706 struct btree_node *node;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000707
708 for (;;) {
709 r = shadow_step(s, root, vt);
710 if (r < 0)
711 return r;
712
713 node = dm_block_data(shadow_current(s));
714
715 /*
716 * We have to patch up the parent node, ugly, but I don't
717 * see a way to do this automatically as part of the spine
718 * op.
719 */
720 if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
721 __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
722
723 __dm_bless_for_disk(&location);
Joe Thornbera3aefb32012-03-28 18:41:25 +0100724 memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000725 &location, sizeof(__le64));
726 }
727
728 node = dm_block_data(shadow_current(s));
729
730 if (node->header.nr_entries == node->header.max_entries) {
731 if (top)
732 r = btree_split_beneath(s, key);
733 else
Vivek Goyal0a8d4c32015-07-06 11:55:40 -0400734 r = btree_split_sibling(s, i, key);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000735
736 if (r < 0)
737 return r;
738 }
739
740 node = dm_block_data(shadow_current(s));
741
742 i = lower_bound(node, key);
743
744 if (le32_to_cpu(node->header.flags) & LEAF_NODE)
745 break;
746
747 if (i < 0) {
748 /* change the bounds on the lowest key */
749 node->keys[0] = cpu_to_le64(key);
750 i = 0;
751 }
752
753 root = value64(node, i);
754 top = 0;
755 }
756
757 if (i < 0 || le64_to_cpu(node->keys[i]) != key)
758 i++;
759
760 *index = i;
761 return 0;
762}
763
Mike Snitzerba503832015-11-23 16:38:25 -0500764static bool need_insert(struct btree_node *node, uint64_t *keys,
765 unsigned level, unsigned index)
766{
767 return ((index >= le32_to_cpu(node->header.nr_entries)) ||
768 (le64_to_cpu(node->keys[index]) != keys[level]));
769}
770
Joe Thornber3241b1d2011-10-31 20:19:11 +0000771static int insert(struct dm_btree_info *info, dm_block_t root,
772 uint64_t *keys, void *value, dm_block_t *new_root,
773 int *inserted)
774 __dm_written_to_disk(value)
775{
Mike Snitzerba503832015-11-23 16:38:25 -0500776 int r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000777 unsigned level, index = -1, last_level = info->levels - 1;
778 dm_block_t block = root;
779 struct shadow_spine spine;
Mikulas Patocka550929f2012-12-21 20:23:30 +0000780 struct btree_node *n;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000781 struct dm_btree_value_type le64_type;
782
Joe Thornberb0dc3c82015-08-12 15:12:09 +0100783 init_le64_type(info->tm, &le64_type);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000784 init_shadow_spine(&spine, info);
785
786 for (level = 0; level < (info->levels - 1); level++) {
787 r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
788 if (r < 0)
789 goto bad;
790
791 n = dm_block_data(shadow_current(&spine));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000792
Mike Snitzerba503832015-11-23 16:38:25 -0500793 if (need_insert(n, keys, level, index)) {
Joe Thornber3241b1d2011-10-31 20:19:11 +0000794 dm_block_t new_tree;
795 __le64 new_le;
796
797 r = dm_btree_empty(info, &new_tree);
798 if (r < 0)
799 goto bad;
800
801 new_le = cpu_to_le64(new_tree);
802 __dm_bless_for_disk(&new_le);
803
804 r = insert_at(sizeof(uint64_t), n, index,
805 keys[level], &new_le);
806 if (r)
807 goto bad;
808 }
809
810 if (level < last_level)
811 block = value64(n, index);
812 }
813
814 r = btree_insert_raw(&spine, block, &info->value_type,
815 keys[level], &index);
816 if (r < 0)
817 goto bad;
818
819 n = dm_block_data(shadow_current(&spine));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000820
Mike Snitzerba503832015-11-23 16:38:25 -0500821 if (need_insert(n, keys, level, index)) {
Joe Thornber3241b1d2011-10-31 20:19:11 +0000822 if (inserted)
823 *inserted = 1;
824
825 r = insert_at(info->value_type.size, n, index,
826 keys[level], value);
827 if (r)
828 goto bad_unblessed;
829 } else {
830 if (inserted)
831 *inserted = 0;
832
833 if (info->value_type.dec &&
834 (!info->value_type.equal ||
835 !info->value_type.equal(
836 info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100837 value_ptr(n, index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000838 value))) {
839 info->value_type.dec(info->value_type.context,
Joe Thornbera3aefb32012-03-28 18:41:25 +0100840 value_ptr(n, index));
Joe Thornber3241b1d2011-10-31 20:19:11 +0000841 }
Joe Thornbera3aefb32012-03-28 18:41:25 +0100842 memcpy_disk(value_ptr(n, index),
Joe Thornber3241b1d2011-10-31 20:19:11 +0000843 value, info->value_type.size);
844 }
845
846 *new_root = shadow_root(&spine);
847 exit_shadow_spine(&spine);
848
849 return 0;
850
851bad:
852 __dm_unbless_for_disk(value);
853bad_unblessed:
854 exit_shadow_spine(&spine);
855 return r;
856}
857
858int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
859 uint64_t *keys, void *value, dm_block_t *new_root)
860 __dm_written_to_disk(value)
861{
862 return insert(info, root, keys, value, new_root, NULL);
863}
864EXPORT_SYMBOL_GPL(dm_btree_insert);
865
866int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
867 uint64_t *keys, void *value, dm_block_t *new_root,
868 int *inserted)
869 __dm_written_to_disk(value)
870{
871 return insert(info, root, keys, value, new_root, inserted);
872}
873EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
874
875/*----------------------------------------------------------------*/
876
Joe Thornberf164e692013-12-20 15:41:11 +0000877static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
878 uint64_t *result_key, dm_block_t *next_block)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000879{
880 int i, r;
881 uint32_t flags;
882
883 do {
884 r = ro_step(s, block);
885 if (r < 0)
886 return r;
887
888 flags = le32_to_cpu(ro_node(s)->header.flags);
889 i = le32_to_cpu(ro_node(s)->header.nr_entries);
890 if (!i)
891 return -ENODATA;
892 else
893 i--;
894
Joe Thornberf164e692013-12-20 15:41:11 +0000895 if (find_highest)
896 *result_key = le64_to_cpu(ro_node(s)->keys[i]);
897 else
898 *result_key = le64_to_cpu(ro_node(s)->keys[0]);
899
Vinothkumar Raja4de8ece2017-04-06 22:09:38 -0400900 if (next_block || flags & INTERNAL_NODE) {
901 if (find_highest)
902 block = value64(ro_node(s), i);
903 else
904 block = value64(ro_node(s), 0);
905 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000906
907 } while (flags & INTERNAL_NODE);
908
909 if (next_block)
910 *next_block = block;
911 return 0;
912}
913
Joe Thornberf164e692013-12-20 15:41:11 +0000914static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
915 bool find_highest, uint64_t *result_keys)
Joe Thornber3241b1d2011-10-31 20:19:11 +0000916{
917 int r = 0, count = 0, level;
918 struct ro_spine spine;
919
920 init_ro_spine(&spine, info);
921 for (level = 0; level < info->levels; level++) {
Joe Thornberf164e692013-12-20 15:41:11 +0000922 r = find_key(&spine, root, find_highest, result_keys + level,
923 level == info->levels - 1 ? NULL : &root);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000924 if (r == -ENODATA) {
925 r = 0;
926 break;
927
928 } else if (r)
929 break;
930
931 count++;
932 }
933 exit_ro_spine(&spine);
934
935 return r ? r : count;
936}
Joe Thornberf164e692013-12-20 15:41:11 +0000937
938int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
939 uint64_t *result_keys)
940{
941 return dm_btree_find_key(info, root, true, result_keys);
942}
Joe Thornber3241b1d2011-10-31 20:19:11 +0000943EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000944
Joe Thornberf164e692013-12-20 15:41:11 +0000945int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
946 uint64_t *result_keys)
947{
948 return dm_btree_find_key(info, root, false, result_keys);
949}
950EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
951
952/*----------------------------------------------------------------*/
953
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000954/*
955 * FIXME: We shouldn't use a recursive algorithm when we have limited stack
956 * space. Also this only works for single level trees.
957 */
Joe Thornber9b460d32014-11-10 15:03:24 +0000958static int walk_node(struct dm_btree_info *info, dm_block_t block,
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000959 int (*fn)(void *context, uint64_t *keys, void *leaf),
960 void *context)
961{
962 int r;
963 unsigned i, nr;
Joe Thornber9b460d32014-11-10 15:03:24 +0000964 struct dm_block *node;
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000965 struct btree_node *n;
966 uint64_t keys;
967
Joe Thornber9b460d32014-11-10 15:03:24 +0000968 r = bn_read_lock(info, block, &node);
969 if (r)
970 return r;
971
972 n = dm_block_data(node);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000973
974 nr = le32_to_cpu(n->header.nr_entries);
975 for (i = 0; i < nr; i++) {
976 if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
Joe Thornber9b460d32014-11-10 15:03:24 +0000977 r = walk_node(info, value64(n, i), fn, context);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000978 if (r)
979 goto out;
980 } else {
981 keys = le64_to_cpu(*key_ptr(n, i));
982 r = fn(context, &keys, value_ptr(n, i));
983 if (r)
984 goto out;
985 }
986 }
987
988out:
Joe Thornber9b460d32014-11-10 15:03:24 +0000989 dm_tm_unlock(info->tm, node);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000990 return r;
991}
992
993int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
994 int (*fn)(void *context, uint64_t *keys, void *leaf),
995 void *context)
996{
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000997 BUG_ON(info->levels > 1);
Joe Thornber9b460d32014-11-10 15:03:24 +0000998 return walk_node(info, root, fn, context);
Joe Thornber4e7f1f92013-03-01 22:45:50 +0000999}
1000EXPORT_SYMBOL_GPL(dm_btree_walk);
Joe Thornber7d111c82016-09-15 10:49:24 -04001001
1002/*----------------------------------------------------------------*/
1003
1004static void prefetch_values(struct dm_btree_cursor *c)
1005{
1006 unsigned i, nr;
1007 __le64 value_le;
1008 struct cursor_node *n = c->nodes + c->depth - 1;
1009 struct btree_node *bn = dm_block_data(n->b);
1010 struct dm_block_manager *bm = dm_tm_get_bm(c->info->tm);
1011
1012 BUG_ON(c->info->value_type.size != sizeof(value_le));
1013
1014 nr = le32_to_cpu(bn->header.nr_entries);
1015 for (i = 0; i < nr; i++) {
1016 memcpy(&value_le, value_ptr(bn, i), sizeof(value_le));
1017 dm_bm_prefetch(bm, le64_to_cpu(value_le));
1018 }
1019}
1020
1021static bool leaf_node(struct dm_btree_cursor *c)
1022{
1023 struct cursor_node *n = c->nodes + c->depth - 1;
1024 struct btree_node *bn = dm_block_data(n->b);
1025
1026 return le32_to_cpu(bn->header.flags) & LEAF_NODE;
1027}
1028
1029static int push_node(struct dm_btree_cursor *c, dm_block_t b)
1030{
1031 int r;
1032 struct cursor_node *n = c->nodes + c->depth;
1033
1034 if (c->depth >= DM_BTREE_CURSOR_MAX_DEPTH - 1) {
1035 DMERR("couldn't push cursor node, stack depth too high");
1036 return -EINVAL;
1037 }
1038
1039 r = bn_read_lock(c->info, b, &n->b);
1040 if (r)
1041 return r;
1042
1043 n->index = 0;
1044 c->depth++;
1045
1046 if (c->prefetch_leaves || !leaf_node(c))
1047 prefetch_values(c);
1048
1049 return 0;
1050}
1051
1052static void pop_node(struct dm_btree_cursor *c)
1053{
1054 c->depth--;
1055 unlock_block(c->info, c->nodes[c->depth].b);
1056}
1057
1058static int inc_or_backtrack(struct dm_btree_cursor *c)
1059{
1060 struct cursor_node *n;
1061 struct btree_node *bn;
1062
1063 for (;;) {
1064 if (!c->depth)
1065 return -ENODATA;
1066
1067 n = c->nodes + c->depth - 1;
1068 bn = dm_block_data(n->b);
1069
1070 n->index++;
1071 if (n->index < le32_to_cpu(bn->header.nr_entries))
1072 break;
1073
1074 pop_node(c);
1075 }
1076
1077 return 0;
1078}
1079
1080static int find_leaf(struct dm_btree_cursor *c)
1081{
1082 int r = 0;
1083 struct cursor_node *n;
1084 struct btree_node *bn;
1085 __le64 value_le;
1086
1087 for (;;) {
1088 n = c->nodes + c->depth - 1;
1089 bn = dm_block_data(n->b);
1090
1091 if (le32_to_cpu(bn->header.flags) & LEAF_NODE)
1092 break;
1093
1094 memcpy(&value_le, value_ptr(bn, n->index), sizeof(value_le));
1095 r = push_node(c, le64_to_cpu(value_le));
1096 if (r) {
1097 DMERR("push_node failed");
1098 break;
1099 }
1100 }
1101
1102 if (!r && (le32_to_cpu(bn->header.nr_entries) == 0))
1103 return -ENODATA;
1104
1105 return r;
1106}
1107
1108int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
1109 bool prefetch_leaves, struct dm_btree_cursor *c)
1110{
1111 int r;
1112
1113 c->info = info;
1114 c->root = root;
1115 c->depth = 0;
1116 c->prefetch_leaves = prefetch_leaves;
1117
1118 r = push_node(c, root);
1119 if (r)
1120 return r;
1121
1122 return find_leaf(c);
1123}
1124EXPORT_SYMBOL_GPL(dm_btree_cursor_begin);
1125
1126void dm_btree_cursor_end(struct dm_btree_cursor *c)
1127{
1128 while (c->depth)
1129 pop_node(c);
1130}
1131EXPORT_SYMBOL_GPL(dm_btree_cursor_end);
1132
1133int dm_btree_cursor_next(struct dm_btree_cursor *c)
1134{
1135 int r = inc_or_backtrack(c);
1136 if (!r) {
1137 r = find_leaf(c);
1138 if (r)
1139 DMERR("find_leaf failed");
1140 }
1141
1142 return r;
1143}
1144EXPORT_SYMBOL_GPL(dm_btree_cursor_next);
1145
1146int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le)
1147{
1148 if (c->depth) {
1149 struct cursor_node *n = c->nodes + c->depth - 1;
1150 struct btree_node *bn = dm_block_data(n->b);
1151
1152 if (le32_to_cpu(bn->header.flags) & INTERNAL_NODE)
1153 return -EINVAL;
1154
1155 *key = le64_to_cpu(*key_ptr(bn, n->index));
1156 memcpy(value_le, value_ptr(bn, n->index), c->info->value_type.size);
1157 return 0;
1158
1159 } else
1160 return -ENODATA;
1161}
1162EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value);