Commit | Line | Data |
---|---|---|
3241b1d3 JT |
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 | ||
1944ce60 | 11 | #include <linux/export.h> |
3241b1d3 JT |
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. */ | |
550929fa | 41 | static int bsearch(struct btree_node *n, uint64_t key, int want_hi) |
3241b1d3 JT |
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 | ||
550929fa | 61 | int lower_bound(struct btree_node *n, uint64_t key) |
3241b1d3 JT |
62 | { |
63 | return bsearch(n, key, 0); | |
64 | } | |
65 | ||
550929fa | 66 | void inc_children(struct dm_transaction_manager *tm, struct btree_node *n, |
3241b1d3 JT |
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++) | |
a3aefb39 | 77 | vt->inc(vt->context, value_ptr(n, i)); |
3241b1d3 JT |
78 | } |
79 | ||
550929fa | 80 | static int insert_at(size_t value_size, struct btree_node *node, unsigned index, |
3241b1d3 JT |
81 | 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 | */ | |
109 | static 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 | ||
121 | int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root) | |
122 | { | |
123 | int r; | |
124 | struct dm_block *b; | |
550929fa | 125 | struct btree_node *n; |
3241b1d3 JT |
126 | 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); | |
144 | return unlock_block(info, b); | |
145 | } | |
146 | EXPORT_SYMBOL_GPL(dm_btree_empty); | |
147 | ||
148 | /*----------------------------------------------------------------*/ | |
149 | ||
150 | /* | |
151 | * Deletion uses a recursive algorithm, since we have limited stack space | |
152 | * we explicitly manage our own stack on the heap. | |
153 | */ | |
154 | #define MAX_SPINE_DEPTH 64 | |
155 | struct frame { | |
156 | struct dm_block *b; | |
550929fa | 157 | struct btree_node *n; |
3241b1d3 JT |
158 | unsigned level; |
159 | unsigned nr_children; | |
160 | unsigned current_child; | |
161 | }; | |
162 | ||
163 | struct del_stack { | |
164 | struct dm_transaction_manager *tm; | |
165 | int top; | |
166 | struct frame spine[MAX_SPINE_DEPTH]; | |
167 | }; | |
168 | ||
169 | static int top_frame(struct del_stack *s, struct frame **f) | |
170 | { | |
171 | if (s->top < 0) { | |
172 | DMERR("btree deletion stack empty"); | |
173 | return -EINVAL; | |
174 | } | |
175 | ||
176 | *f = s->spine + s->top; | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static int unprocessed_frames(struct del_stack *s) | |
182 | { | |
183 | return s->top >= 0; | |
184 | } | |
185 | ||
186 | static int push_frame(struct del_stack *s, dm_block_t b, unsigned level) | |
187 | { | |
188 | int r; | |
189 | uint32_t ref_count; | |
190 | ||
191 | if (s->top >= MAX_SPINE_DEPTH - 1) { | |
192 | DMERR("btree deletion stack out of memory"); | |
193 | return -ENOMEM; | |
194 | } | |
195 | ||
196 | r = dm_tm_ref(s->tm, b, &ref_count); | |
197 | if (r) | |
198 | return r; | |
199 | ||
200 | if (ref_count > 1) | |
201 | /* | |
202 | * This is a shared node, so we can just decrement it's | |
203 | * reference counter and leave the children. | |
204 | */ | |
205 | dm_tm_dec(s->tm, b); | |
206 | ||
207 | else { | |
208 | struct frame *f = s->spine + ++s->top; | |
209 | ||
210 | r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b); | |
211 | if (r) { | |
212 | s->top--; | |
213 | return r; | |
214 | } | |
215 | ||
216 | f->n = dm_block_data(f->b); | |
217 | f->level = level; | |
218 | f->nr_children = le32_to_cpu(f->n->header.nr_entries); | |
219 | f->current_child = 0; | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | static void pop_frame(struct del_stack *s) | |
226 | { | |
227 | struct frame *f = s->spine + s->top--; | |
228 | ||
229 | dm_tm_dec(s->tm, dm_block_location(f->b)); | |
230 | dm_tm_unlock(s->tm, f->b); | |
231 | } | |
232 | ||
e3cbf945 JT |
233 | static bool is_internal_level(struct dm_btree_info *info, struct frame *f) |
234 | { | |
235 | return f->level < (info->levels - 1); | |
236 | } | |
237 | ||
3241b1d3 JT |
238 | int dm_btree_del(struct dm_btree_info *info, dm_block_t root) |
239 | { | |
240 | int r; | |
241 | struct del_stack *s; | |
242 | ||
2554f6d1 | 243 | s = kmalloc(sizeof(*s), GFP_NOIO); |
3241b1d3 JT |
244 | if (!s) |
245 | return -ENOMEM; | |
246 | s->tm = info->tm; | |
247 | s->top = -1; | |
248 | ||
e3cbf945 | 249 | r = push_frame(s, root, 0); |
3241b1d3 JT |
250 | if (r) |
251 | goto out; | |
252 | ||
253 | while (unprocessed_frames(s)) { | |
254 | uint32_t flags; | |
255 | struct frame *f; | |
256 | dm_block_t b; | |
257 | ||
258 | r = top_frame(s, &f); | |
259 | if (r) | |
260 | goto out; | |
261 | ||
262 | if (f->current_child >= f->nr_children) { | |
263 | pop_frame(s); | |
264 | continue; | |
265 | } | |
266 | ||
267 | flags = le32_to_cpu(f->n->header.flags); | |
268 | if (flags & INTERNAL_NODE) { | |
269 | b = value64(f->n, f->current_child); | |
270 | f->current_child++; | |
271 | r = push_frame(s, b, f->level); | |
272 | if (r) | |
273 | goto out; | |
274 | ||
e3cbf945 | 275 | } else if (is_internal_level(info, f)) { |
3241b1d3 JT |
276 | b = value64(f->n, f->current_child); |
277 | f->current_child++; | |
278 | r = push_frame(s, b, f->level + 1); | |
279 | if (r) | |
280 | goto out; | |
281 | ||
282 | } else { | |
283 | if (info->value_type.dec) { | |
284 | unsigned i; | |
285 | ||
286 | for (i = 0; i < f->nr_children; i++) | |
287 | info->value_type.dec(info->value_type.context, | |
a3aefb39 | 288 | value_ptr(f->n, i)); |
3241b1d3 JT |
289 | } |
290 | f->current_child = f->nr_children; | |
291 | } | |
292 | } | |
293 | ||
294 | out: | |
295 | kfree(s); | |
296 | return r; | |
297 | } | |
298 | EXPORT_SYMBOL_GPL(dm_btree_del); | |
299 | ||
300 | /*----------------------------------------------------------------*/ | |
301 | ||
302 | static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key, | |
550929fa | 303 | int (*search_fn)(struct btree_node *, uint64_t), |
3241b1d3 JT |
304 | uint64_t *result_key, void *v, size_t value_size) |
305 | { | |
306 | int i, r; | |
307 | uint32_t flags, nr_entries; | |
308 | ||
309 | do { | |
310 | r = ro_step(s, block); | |
311 | if (r < 0) | |
312 | return r; | |
313 | ||
314 | i = search_fn(ro_node(s), key); | |
315 | ||
316 | flags = le32_to_cpu(ro_node(s)->header.flags); | |
317 | nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries); | |
318 | if (i < 0 || i >= nr_entries) | |
319 | return -ENODATA; | |
320 | ||
321 | if (flags & INTERNAL_NODE) | |
322 | block = value64(ro_node(s), i); | |
323 | ||
324 | } while (!(flags & LEAF_NODE)); | |
325 | ||
326 | *result_key = le64_to_cpu(ro_node(s)->keys[i]); | |
a3aefb39 | 327 | memcpy(v, value_ptr(ro_node(s), i), value_size); |
3241b1d3 JT |
328 | |
329 | return 0; | |
330 | } | |
331 | ||
332 | int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root, | |
333 | uint64_t *keys, void *value_le) | |
334 | { | |
335 | unsigned level, last_level = info->levels - 1; | |
336 | int r = -ENODATA; | |
337 | uint64_t rkey; | |
338 | __le64 internal_value_le; | |
339 | struct ro_spine spine; | |
340 | ||
341 | init_ro_spine(&spine, info); | |
342 | for (level = 0; level < info->levels; level++) { | |
343 | size_t size; | |
344 | void *value_p; | |
345 | ||
346 | if (level == last_level) { | |
347 | value_p = value_le; | |
348 | size = info->value_type.size; | |
349 | ||
350 | } else { | |
351 | value_p = &internal_value_le; | |
352 | size = sizeof(uint64_t); | |
353 | } | |
354 | ||
355 | r = btree_lookup_raw(&spine, root, keys[level], | |
356 | lower_bound, &rkey, | |
357 | value_p, size); | |
358 | ||
359 | if (!r) { | |
360 | if (rkey != keys[level]) { | |
361 | exit_ro_spine(&spine); | |
362 | return -ENODATA; | |
363 | } | |
364 | } else { | |
365 | exit_ro_spine(&spine); | |
366 | return r; | |
367 | } | |
368 | ||
369 | root = le64_to_cpu(internal_value_le); | |
370 | } | |
371 | exit_ro_spine(&spine); | |
372 | ||
373 | return r; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(dm_btree_lookup); | |
376 | ||
377 | /* | |
378 | * Splits a node by creating a sibling node and shifting half the nodes | |
379 | * contents across. Assumes there is a parent node, and it has room for | |
380 | * another child. | |
381 | * | |
382 | * Before: | |
383 | * +--------+ | |
384 | * | Parent | | |
385 | * +--------+ | |
386 | * | | |
387 | * v | |
388 | * +----------+ | |
389 | * | A ++++++ | | |
390 | * +----------+ | |
391 | * | |
392 | * | |
393 | * After: | |
394 | * +--------+ | |
395 | * | Parent | | |
396 | * +--------+ | |
397 | * | | | |
398 | * v +------+ | |
399 | * +---------+ | | |
400 | * | A* +++ | v | |
401 | * +---------+ +-------+ | |
402 | * | B +++ | | |
403 | * +-------+ | |
404 | * | |
405 | * Where A* is a shadow of A. | |
406 | */ | |
407 | static int btree_split_sibling(struct shadow_spine *s, dm_block_t root, | |
408 | unsigned parent_index, uint64_t key) | |
409 | { | |
410 | int r; | |
411 | size_t size; | |
412 | unsigned nr_left, nr_right; | |
413 | struct dm_block *left, *right, *parent; | |
550929fa | 414 | struct btree_node *ln, *rn, *pn; |
3241b1d3 JT |
415 | __le64 location; |
416 | ||
417 | left = shadow_current(s); | |
418 | ||
419 | r = new_block(s->info, &right); | |
420 | if (r < 0) | |
421 | return r; | |
422 | ||
423 | ln = dm_block_data(left); | |
424 | rn = dm_block_data(right); | |
425 | ||
426 | nr_left = le32_to_cpu(ln->header.nr_entries) / 2; | |
427 | nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left; | |
428 | ||
429 | ln->header.nr_entries = cpu_to_le32(nr_left); | |
430 | ||
431 | rn->header.flags = ln->header.flags; | |
432 | rn->header.nr_entries = cpu_to_le32(nr_right); | |
433 | rn->header.max_entries = ln->header.max_entries; | |
434 | rn->header.value_size = ln->header.value_size; | |
435 | memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0])); | |
436 | ||
437 | size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ? | |
438 | sizeof(uint64_t) : s->info->value_type.size; | |
a3aefb39 | 439 | memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left), |
3241b1d3 JT |
440 | size * nr_right); |
441 | ||
442 | /* | |
443 | * Patch up the parent | |
444 | */ | |
445 | parent = shadow_parent(s); | |
446 | ||
447 | pn = dm_block_data(parent); | |
448 | location = cpu_to_le64(dm_block_location(left)); | |
449 | __dm_bless_for_disk(&location); | |
a3aefb39 | 450 | memcpy_disk(value_ptr(pn, parent_index), |
3241b1d3 JT |
451 | &location, sizeof(__le64)); |
452 | ||
453 | location = cpu_to_le64(dm_block_location(right)); | |
454 | __dm_bless_for_disk(&location); | |
455 | ||
456 | r = insert_at(sizeof(__le64), pn, parent_index + 1, | |
457 | le64_to_cpu(rn->keys[0]), &location); | |
d6671b05 MS |
458 | if (r) { |
459 | unlock_block(s->info, right); | |
3241b1d3 | 460 | return r; |
d6671b05 | 461 | } |
3241b1d3 JT |
462 | |
463 | if (key < le64_to_cpu(rn->keys[0])) { | |
464 | unlock_block(s->info, right); | |
465 | s->nodes[1] = left; | |
466 | } else { | |
467 | unlock_block(s->info, left); | |
468 | s->nodes[1] = right; | |
469 | } | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | /* | |
475 | * Splits a node by creating two new children beneath the given node. | |
476 | * | |
477 | * Before: | |
478 | * +----------+ | |
479 | * | A ++++++ | | |
480 | * +----------+ | |
481 | * | |
482 | * | |
483 | * After: | |
484 | * +------------+ | |
485 | * | A (shadow) | | |
486 | * +------------+ | |
487 | * | | | |
488 | * +------+ +----+ | |
489 | * | | | |
490 | * v v | |
491 | * +-------+ +-------+ | |
492 | * | B +++ | | C +++ | | |
493 | * +-------+ +-------+ | |
494 | */ | |
495 | static int btree_split_beneath(struct shadow_spine *s, uint64_t key) | |
496 | { | |
497 | int r; | |
498 | size_t size; | |
499 | unsigned nr_left, nr_right; | |
500 | struct dm_block *left, *right, *new_parent; | |
550929fa | 501 | struct btree_node *pn, *ln, *rn; |
3241b1d3 JT |
502 | __le64 val; |
503 | ||
504 | new_parent = shadow_current(s); | |
505 | ||
506 | r = new_block(s->info, &left); | |
507 | if (r < 0) | |
508 | return r; | |
509 | ||
510 | r = new_block(s->info, &right); | |
511 | if (r < 0) { | |
0dcb59bb | 512 | unlock_block(s->info, left); |
3241b1d3 JT |
513 | return r; |
514 | } | |
515 | ||
516 | pn = dm_block_data(new_parent); | |
517 | ln = dm_block_data(left); | |
518 | rn = dm_block_data(right); | |
519 | ||
520 | nr_left = le32_to_cpu(pn->header.nr_entries) / 2; | |
521 | nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left; | |
522 | ||
523 | ln->header.flags = pn->header.flags; | |
524 | ln->header.nr_entries = cpu_to_le32(nr_left); | |
525 | ln->header.max_entries = pn->header.max_entries; | |
526 | ln->header.value_size = pn->header.value_size; | |
527 | ||
528 | rn->header.flags = pn->header.flags; | |
529 | rn->header.nr_entries = cpu_to_le32(nr_right); | |
530 | rn->header.max_entries = pn->header.max_entries; | |
531 | rn->header.value_size = pn->header.value_size; | |
532 | ||
533 | memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0])); | |
534 | memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0])); | |
535 | ||
536 | size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ? | |
537 | sizeof(__le64) : s->info->value_type.size; | |
a3aefb39 JT |
538 | memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size); |
539 | memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left), | |
3241b1d3 JT |
540 | nr_right * size); |
541 | ||
542 | /* new_parent should just point to l and r now */ | |
543 | pn->header.flags = cpu_to_le32(INTERNAL_NODE); | |
544 | pn->header.nr_entries = cpu_to_le32(2); | |
545 | pn->header.max_entries = cpu_to_le32( | |
546 | calc_max_entries(sizeof(__le64), | |
547 | dm_bm_block_size( | |
548 | dm_tm_get_bm(s->info->tm)))); | |
549 | pn->header.value_size = cpu_to_le32(sizeof(__le64)); | |
550 | ||
551 | val = cpu_to_le64(dm_block_location(left)); | |
552 | __dm_bless_for_disk(&val); | |
553 | pn->keys[0] = ln->keys[0]; | |
a3aefb39 | 554 | memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64)); |
3241b1d3 JT |
555 | |
556 | val = cpu_to_le64(dm_block_location(right)); | |
557 | __dm_bless_for_disk(&val); | |
558 | pn->keys[1] = rn->keys[0]; | |
a3aefb39 | 559 | memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64)); |
3241b1d3 JT |
560 | |
561 | /* | |
562 | * rejig the spine. This is ugly, since it knows too | |
563 | * much about the spine | |
564 | */ | |
565 | if (s->nodes[0] != new_parent) { | |
566 | unlock_block(s->info, s->nodes[0]); | |
567 | s->nodes[0] = new_parent; | |
568 | } | |
569 | if (key < le64_to_cpu(rn->keys[0])) { | |
570 | unlock_block(s->info, right); | |
571 | s->nodes[1] = left; | |
572 | } else { | |
573 | unlock_block(s->info, left); | |
574 | s->nodes[1] = right; | |
575 | } | |
576 | s->count = 2; | |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static int btree_insert_raw(struct shadow_spine *s, dm_block_t root, | |
582 | struct dm_btree_value_type *vt, | |
583 | uint64_t key, unsigned *index) | |
584 | { | |
585 | int r, i = *index, top = 1; | |
550929fa | 586 | struct btree_node *node; |
3241b1d3 JT |
587 | |
588 | for (;;) { | |
589 | r = shadow_step(s, root, vt); | |
590 | if (r < 0) | |
591 | return r; | |
592 | ||
593 | node = dm_block_data(shadow_current(s)); | |
594 | ||
595 | /* | |
596 | * We have to patch up the parent node, ugly, but I don't | |
597 | * see a way to do this automatically as part of the spine | |
598 | * op. | |
599 | */ | |
600 | if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */ | |
601 | __le64 location = cpu_to_le64(dm_block_location(shadow_current(s))); | |
602 | ||
603 | __dm_bless_for_disk(&location); | |
a3aefb39 | 604 | memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i), |
3241b1d3 JT |
605 | &location, sizeof(__le64)); |
606 | } | |
607 | ||
608 | node = dm_block_data(shadow_current(s)); | |
609 | ||
610 | if (node->header.nr_entries == node->header.max_entries) { | |
611 | if (top) | |
612 | r = btree_split_beneath(s, key); | |
613 | else | |
614 | r = btree_split_sibling(s, root, i, key); | |
615 | ||
616 | if (r < 0) | |
617 | return r; | |
618 | } | |
619 | ||
620 | node = dm_block_data(shadow_current(s)); | |
621 | ||
622 | i = lower_bound(node, key); | |
623 | ||
624 | if (le32_to_cpu(node->header.flags) & LEAF_NODE) | |
625 | break; | |
626 | ||
627 | if (i < 0) { | |
628 | /* change the bounds on the lowest key */ | |
629 | node->keys[0] = cpu_to_le64(key); | |
630 | i = 0; | |
631 | } | |
632 | ||
633 | root = value64(node, i); | |
634 | top = 0; | |
635 | } | |
636 | ||
637 | if (i < 0 || le64_to_cpu(node->keys[i]) != key) | |
638 | i++; | |
639 | ||
640 | *index = i; | |
641 | return 0; | |
642 | } | |
643 | ||
644 | static int insert(struct dm_btree_info *info, dm_block_t root, | |
645 | uint64_t *keys, void *value, dm_block_t *new_root, | |
646 | int *inserted) | |
647 | __dm_written_to_disk(value) | |
648 | { | |
649 | int r, need_insert; | |
650 | unsigned level, index = -1, last_level = info->levels - 1; | |
651 | dm_block_t block = root; | |
652 | struct shadow_spine spine; | |
550929fa | 653 | struct btree_node *n; |
3241b1d3 JT |
654 | struct dm_btree_value_type le64_type; |
655 | ||
249fbae3 | 656 | init_le64_type(info->tm, &le64_type); |
3241b1d3 JT |
657 | init_shadow_spine(&spine, info); |
658 | ||
659 | for (level = 0; level < (info->levels - 1); level++) { | |
660 | r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index); | |
661 | if (r < 0) | |
662 | goto bad; | |
663 | ||
664 | n = dm_block_data(shadow_current(&spine)); | |
665 | need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) || | |
666 | (le64_to_cpu(n->keys[index]) != keys[level])); | |
667 | ||
668 | if (need_insert) { | |
669 | dm_block_t new_tree; | |
670 | __le64 new_le; | |
671 | ||
672 | r = dm_btree_empty(info, &new_tree); | |
673 | if (r < 0) | |
674 | goto bad; | |
675 | ||
676 | new_le = cpu_to_le64(new_tree); | |
677 | __dm_bless_for_disk(&new_le); | |
678 | ||
679 | r = insert_at(sizeof(uint64_t), n, index, | |
680 | keys[level], &new_le); | |
681 | if (r) | |
682 | goto bad; | |
683 | } | |
684 | ||
685 | if (level < last_level) | |
686 | block = value64(n, index); | |
687 | } | |
688 | ||
689 | r = btree_insert_raw(&spine, block, &info->value_type, | |
690 | keys[level], &index); | |
691 | if (r < 0) | |
692 | goto bad; | |
693 | ||
694 | n = dm_block_data(shadow_current(&spine)); | |
695 | need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) || | |
696 | (le64_to_cpu(n->keys[index]) != keys[level])); | |
697 | ||
698 | if (need_insert) { | |
699 | if (inserted) | |
700 | *inserted = 1; | |
701 | ||
702 | r = insert_at(info->value_type.size, n, index, | |
703 | keys[level], value); | |
704 | if (r) | |
705 | goto bad_unblessed; | |
706 | } else { | |
707 | if (inserted) | |
708 | *inserted = 0; | |
709 | ||
710 | if (info->value_type.dec && | |
711 | (!info->value_type.equal || | |
712 | !info->value_type.equal( | |
713 | info->value_type.context, | |
a3aefb39 | 714 | value_ptr(n, index), |
3241b1d3 JT |
715 | value))) { |
716 | info->value_type.dec(info->value_type.context, | |
a3aefb39 | 717 | value_ptr(n, index)); |
3241b1d3 | 718 | } |
a3aefb39 | 719 | memcpy_disk(value_ptr(n, index), |
3241b1d3 JT |
720 | value, info->value_type.size); |
721 | } | |
722 | ||
723 | *new_root = shadow_root(&spine); | |
724 | exit_shadow_spine(&spine); | |
725 | ||
726 | return 0; | |
727 | ||
728 | bad: | |
729 | __dm_unbless_for_disk(value); | |
730 | bad_unblessed: | |
731 | exit_shadow_spine(&spine); | |
732 | return r; | |
733 | } | |
734 | ||
735 | int dm_btree_insert(struct dm_btree_info *info, dm_block_t root, | |
736 | uint64_t *keys, void *value, dm_block_t *new_root) | |
737 | __dm_written_to_disk(value) | |
738 | { | |
739 | return insert(info, root, keys, value, new_root, NULL); | |
740 | } | |
741 | EXPORT_SYMBOL_GPL(dm_btree_insert); | |
742 | ||
743 | int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root, | |
744 | uint64_t *keys, void *value, dm_block_t *new_root, | |
745 | int *inserted) | |
746 | __dm_written_to_disk(value) | |
747 | { | |
748 | return insert(info, root, keys, value, new_root, inserted); | |
749 | } | |
750 | EXPORT_SYMBOL_GPL(dm_btree_insert_notify); | |
751 | ||
752 | /*----------------------------------------------------------------*/ | |
753 | ||
754 | static int find_highest_key(struct ro_spine *s, dm_block_t block, | |
755 | uint64_t *result_key, dm_block_t *next_block) | |
756 | { | |
757 | int i, r; | |
758 | uint32_t flags; | |
759 | ||
760 | do { | |
761 | r = ro_step(s, block); | |
762 | if (r < 0) | |
763 | return r; | |
764 | ||
765 | flags = le32_to_cpu(ro_node(s)->header.flags); | |
766 | i = le32_to_cpu(ro_node(s)->header.nr_entries); | |
767 | if (!i) | |
768 | return -ENODATA; | |
769 | else | |
770 | i--; | |
771 | ||
772 | *result_key = le64_to_cpu(ro_node(s)->keys[i]); | |
773 | if (next_block || flags & INTERNAL_NODE) | |
774 | block = value64(ro_node(s), i); | |
775 | ||
776 | } while (flags & INTERNAL_NODE); | |
777 | ||
778 | if (next_block) | |
779 | *next_block = block; | |
780 | return 0; | |
781 | } | |
782 | ||
783 | int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root, | |
784 | uint64_t *result_keys) | |
785 | { | |
786 | int r = 0, count = 0, level; | |
787 | struct ro_spine spine; | |
788 | ||
789 | init_ro_spine(&spine, info); | |
790 | for (level = 0; level < info->levels; level++) { | |
791 | r = find_highest_key(&spine, root, result_keys + level, | |
792 | level == info->levels - 1 ? NULL : &root); | |
793 | if (r == -ENODATA) { | |
794 | r = 0; | |
795 | break; | |
796 | ||
797 | } else if (r) | |
798 | break; | |
799 | ||
800 | count++; | |
801 | } | |
802 | exit_ro_spine(&spine); | |
803 | ||
804 | return r ? r : count; | |
805 | } | |
806 | EXPORT_SYMBOL_GPL(dm_btree_find_highest_key); | |
4e7f1f90 JT |
807 | |
808 | /* | |
809 | * FIXME: We shouldn't use a recursive algorithm when we have limited stack | |
810 | * space. Also this only works for single level trees. | |
811 | */ | |
fe30b804 | 812 | static int walk_node(struct dm_btree_info *info, dm_block_t block, |
4e7f1f90 JT |
813 | int (*fn)(void *context, uint64_t *keys, void *leaf), |
814 | void *context) | |
815 | { | |
816 | int r; | |
817 | unsigned i, nr; | |
fe30b804 | 818 | struct dm_block *node; |
4e7f1f90 JT |
819 | struct btree_node *n; |
820 | uint64_t keys; | |
821 | ||
fe30b804 JT |
822 | r = bn_read_lock(info, block, &node); |
823 | if (r) | |
824 | return r; | |
825 | ||
826 | n = dm_block_data(node); | |
4e7f1f90 JT |
827 | |
828 | nr = le32_to_cpu(n->header.nr_entries); | |
829 | for (i = 0; i < nr; i++) { | |
830 | if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) { | |
fe30b804 | 831 | r = walk_node(info, value64(n, i), fn, context); |
4e7f1f90 JT |
832 | if (r) |
833 | goto out; | |
834 | } else { | |
835 | keys = le64_to_cpu(*key_ptr(n, i)); | |
836 | r = fn(context, &keys, value_ptr(n, i)); | |
837 | if (r) | |
838 | goto out; | |
839 | } | |
840 | } | |
841 | ||
842 | out: | |
fe30b804 | 843 | dm_tm_unlock(info->tm, node); |
4e7f1f90 JT |
844 | return r; |
845 | } | |
846 | ||
847 | int dm_btree_walk(struct dm_btree_info *info, dm_block_t root, | |
848 | int (*fn)(void *context, uint64_t *keys, void *leaf), | |
849 | void *context) | |
850 | { | |
4e7f1f90 | 851 | BUG_ON(info->levels > 1); |
fe30b804 | 852 | return walk_node(info, root, fn, context); |
4e7f1f90 JT |
853 | } |
854 | EXPORT_SYMBOL_GPL(dm_btree_walk); |