1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7 #include "dm-space-map.h"
8 #include "dm-space-map-common.h"
9 #include "dm-space-map-metadata.h"
10
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/device-mapper.h>
14
15 #define DM_MSG_PREFIX "space map metadata"
16
17 /*----------------------------------------------------------------*/
18
19 /*
20 * An edge triggered threshold.
21 */
22 struct threshold {
23 bool threshold_set;
24 bool value_set;
25 dm_block_t threshold;
26 dm_block_t current_value;
27 dm_sm_threshold_fn fn;
28 void *context;
29 };
30
threshold_init(struct threshold * t)31 static void threshold_init(struct threshold *t)
32 {
33 t->threshold_set = false;
34 t->value_set = false;
35 }
36
set_threshold(struct threshold * t,dm_block_t value,dm_sm_threshold_fn fn,void * context)37 static void set_threshold(struct threshold *t, dm_block_t value,
38 dm_sm_threshold_fn fn, void *context)
39 {
40 t->threshold_set = true;
41 t->threshold = value;
42 t->fn = fn;
43 t->context = context;
44 }
45
below_threshold(struct threshold * t,dm_block_t value)46 static bool below_threshold(struct threshold *t, dm_block_t value)
47 {
48 return t->threshold_set && value <= t->threshold;
49 }
50
threshold_already_triggered(struct threshold * t)51 static bool threshold_already_triggered(struct threshold *t)
52 {
53 return t->value_set && below_threshold(t, t->current_value);
54 }
55
check_threshold(struct threshold * t,dm_block_t value)56 static void check_threshold(struct threshold *t, dm_block_t value)
57 {
58 if (below_threshold(t, value) &&
59 !threshold_already_triggered(t))
60 t->fn(t->context);
61
62 t->value_set = true;
63 t->current_value = value;
64 }
65
66 /*----------------------------------------------------------------*/
67
68 /*
69 * Space map interface.
70 *
71 * The low level disk format is written using the standard btree and
72 * transaction manager. This means that performing disk operations may
73 * cause us to recurse into the space map in order to allocate new blocks.
74 * For this reason we have a pool of pre-allocated blocks large enough to
75 * service any metadata_ll_disk operation.
76 */
77
78 /*
79 * FIXME: we should calculate this based on the size of the device.
80 * Only the metadata space map needs this functionality.
81 */
82 #define MAX_RECURSIVE_ALLOCATIONS 1024
83
84 enum block_op_type {
85 BOP_INC,
86 BOP_DEC
87 };
88
89 struct block_op {
90 enum block_op_type type;
91 dm_block_t block;
92 };
93
94 struct bop_ring_buffer {
95 unsigned begin;
96 unsigned end;
97 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
98 };
99
brb_init(struct bop_ring_buffer * brb)100 static void brb_init(struct bop_ring_buffer *brb)
101 {
102 brb->begin = 0;
103 brb->end = 0;
104 }
105
brb_empty(struct bop_ring_buffer * brb)106 static bool brb_empty(struct bop_ring_buffer *brb)
107 {
108 return brb->begin == brb->end;
109 }
110
brb_next(struct bop_ring_buffer * brb,unsigned old)111 static unsigned brb_next(struct bop_ring_buffer *brb, unsigned old)
112 {
113 unsigned r = old + 1;
114 return (r >= (sizeof(brb->bops) / sizeof(*brb->bops))) ? 0 : r;
115 }
116
brb_push(struct bop_ring_buffer * brb,enum block_op_type type,dm_block_t b)117 static int brb_push(struct bop_ring_buffer *brb,
118 enum block_op_type type, dm_block_t b)
119 {
120 struct block_op *bop;
121 unsigned next = brb_next(brb, brb->end);
122
123 /*
124 * We don't allow the last bop to be filled, this way we can
125 * differentiate between full and empty.
126 */
127 if (next == brb->begin)
128 return -ENOMEM;
129
130 bop = brb->bops + brb->end;
131 bop->type = type;
132 bop->block = b;
133
134 brb->end = next;
135
136 return 0;
137 }
138
brb_peek(struct bop_ring_buffer * brb,struct block_op * result)139 static int brb_peek(struct bop_ring_buffer *brb, struct block_op *result)
140 {
141 struct block_op *bop;
142
143 if (brb_empty(brb))
144 return -ENODATA;
145
146 bop = brb->bops + brb->begin;
147 result->type = bop->type;
148 result->block = bop->block;
149
150 return 0;
151 }
152
brb_pop(struct bop_ring_buffer * brb)153 static int brb_pop(struct bop_ring_buffer *brb)
154 {
155 if (brb_empty(brb))
156 return -ENODATA;
157
158 brb->begin = brb_next(brb, brb->begin);
159
160 return 0;
161 }
162
163 /*----------------------------------------------------------------*/
164
165 struct sm_metadata {
166 struct dm_space_map sm;
167
168 struct ll_disk ll;
169 struct ll_disk old_ll;
170
171 dm_block_t begin;
172
173 unsigned recursion_count;
174 unsigned allocated_this_transaction;
175 struct bop_ring_buffer uncommitted;
176
177 struct threshold threshold;
178 };
179
add_bop(struct sm_metadata * smm,enum block_op_type type,dm_block_t b)180 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
181 {
182 int r = brb_push(&smm->uncommitted, type, b);
183
184 if (r) {
185 DMERR("too many recursive allocations");
186 return -ENOMEM;
187 }
188
189 return 0;
190 }
191
commit_bop(struct sm_metadata * smm,struct block_op * op)192 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
193 {
194 int r = 0;
195 enum allocation_event ev;
196
197 switch (op->type) {
198 case BOP_INC:
199 r = sm_ll_inc(&smm->ll, op->block, &ev);
200 break;
201
202 case BOP_DEC:
203 r = sm_ll_dec(&smm->ll, op->block, &ev);
204 break;
205 }
206
207 return r;
208 }
209
in(struct sm_metadata * smm)210 static void in(struct sm_metadata *smm)
211 {
212 smm->recursion_count++;
213 }
214
apply_bops(struct sm_metadata * smm)215 static int apply_bops(struct sm_metadata *smm)
216 {
217 int r = 0;
218
219 while (!brb_empty(&smm->uncommitted)) {
220 struct block_op bop;
221
222 r = brb_peek(&smm->uncommitted, &bop);
223 if (r) {
224 DMERR("bug in bop ring buffer");
225 break;
226 }
227
228 r = commit_bop(smm, &bop);
229 if (r)
230 break;
231
232 brb_pop(&smm->uncommitted);
233 }
234
235 return r;
236 }
237
out(struct sm_metadata * smm)238 static int out(struct sm_metadata *smm)
239 {
240 int r = 0;
241
242 /*
243 * If we're not recursing then very bad things are happening.
244 */
245 if (!smm->recursion_count) {
246 DMERR("lost track of recursion depth");
247 return -ENOMEM;
248 }
249
250 if (smm->recursion_count == 1)
251 r = apply_bops(smm);
252
253 smm->recursion_count--;
254
255 return r;
256 }
257
258 /*
259 * When using the out() function above, we often want to combine an error
260 * code for the operation run in the recursive context with that from
261 * out().
262 */
combine_errors(int r1,int r2)263 static int combine_errors(int r1, int r2)
264 {
265 return r1 ? r1 : r2;
266 }
267
recursing(struct sm_metadata * smm)268 static int recursing(struct sm_metadata *smm)
269 {
270 return smm->recursion_count;
271 }
272
sm_metadata_destroy(struct dm_space_map * sm)273 static void sm_metadata_destroy(struct dm_space_map *sm)
274 {
275 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
276
277 kfree(smm);
278 }
279
sm_metadata_get_nr_blocks(struct dm_space_map * sm,dm_block_t * count)280 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
281 {
282 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
283
284 *count = smm->ll.nr_blocks;
285
286 return 0;
287 }
288
sm_metadata_get_nr_free(struct dm_space_map * sm,dm_block_t * count)289 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
290 {
291 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
292
293 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
294 smm->allocated_this_transaction;
295
296 return 0;
297 }
298
sm_metadata_get_count(struct dm_space_map * sm,dm_block_t b,uint32_t * result)299 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
300 uint32_t *result)
301 {
302 int r;
303 unsigned i;
304 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
305 unsigned adjustment = 0;
306
307 /*
308 * We may have some uncommitted adjustments to add. This list
309 * should always be really short.
310 */
311 for (i = smm->uncommitted.begin;
312 i != smm->uncommitted.end;
313 i = brb_next(&smm->uncommitted, i)) {
314 struct block_op *op = smm->uncommitted.bops + i;
315
316 if (op->block != b)
317 continue;
318
319 switch (op->type) {
320 case BOP_INC:
321 adjustment++;
322 break;
323
324 case BOP_DEC:
325 adjustment--;
326 break;
327 }
328 }
329
330 r = sm_ll_lookup(&smm->ll, b, result);
331 if (r)
332 return r;
333
334 *result += adjustment;
335
336 return 0;
337 }
338
sm_metadata_count_is_more_than_one(struct dm_space_map * sm,dm_block_t b,int * result)339 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
340 dm_block_t b, int *result)
341 {
342 int r, adjustment = 0;
343 unsigned i;
344 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
345 uint32_t rc;
346
347 /*
348 * We may have some uncommitted adjustments to add. This list
349 * should always be really short.
350 */
351 for (i = smm->uncommitted.begin;
352 i != smm->uncommitted.end;
353 i = brb_next(&smm->uncommitted, i)) {
354
355 struct block_op *op = smm->uncommitted.bops + i;
356
357 if (op->block != b)
358 continue;
359
360 switch (op->type) {
361 case BOP_INC:
362 adjustment++;
363 break;
364
365 case BOP_DEC:
366 adjustment--;
367 break;
368 }
369 }
370
371 if (adjustment > 1) {
372 *result = 1;
373 return 0;
374 }
375
376 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
377 if (r)
378 return r;
379
380 if (rc == 3)
381 /*
382 * We err on the side of caution, and always return true.
383 */
384 *result = 1;
385 else
386 *result = rc + adjustment > 1;
387
388 return 0;
389 }
390
sm_metadata_set_count(struct dm_space_map * sm,dm_block_t b,uint32_t count)391 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
392 uint32_t count)
393 {
394 int r, r2;
395 enum allocation_event ev;
396 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
397
398 if (smm->recursion_count) {
399 DMERR("cannot recurse set_count()");
400 return -EINVAL;
401 }
402
403 in(smm);
404 r = sm_ll_insert(&smm->ll, b, count, &ev);
405 r2 = out(smm);
406
407 return combine_errors(r, r2);
408 }
409
sm_metadata_inc_block(struct dm_space_map * sm,dm_block_t b)410 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
411 {
412 int r, r2 = 0;
413 enum allocation_event ev;
414 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
415
416 if (recursing(smm))
417 r = add_bop(smm, BOP_INC, b);
418 else {
419 in(smm);
420 r = sm_ll_inc(&smm->ll, b, &ev);
421 r2 = out(smm);
422 }
423
424 return combine_errors(r, r2);
425 }
426
sm_metadata_dec_block(struct dm_space_map * sm,dm_block_t b)427 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
428 {
429 int r, r2 = 0;
430 enum allocation_event ev;
431 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
432
433 if (recursing(smm))
434 r = add_bop(smm, BOP_DEC, b);
435 else {
436 in(smm);
437 r = sm_ll_dec(&smm->ll, b, &ev);
438 r2 = out(smm);
439 }
440
441 return combine_errors(r, r2);
442 }
443
sm_metadata_new_block_(struct dm_space_map * sm,dm_block_t * b)444 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
445 {
446 int r, r2 = 0;
447 enum allocation_event ev;
448 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
449
450 /*
451 * Any block we allocate has to be free in both the old and current ll.
452 */
453 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
454 if (r)
455 return r;
456
457 smm->begin = *b + 1;
458
459 if (recursing(smm))
460 r = add_bop(smm, BOP_INC, *b);
461 else {
462 in(smm);
463 r = sm_ll_inc(&smm->ll, *b, &ev);
464 r2 = out(smm);
465 }
466
467 if (!r)
468 smm->allocated_this_transaction++;
469
470 return combine_errors(r, r2);
471 }
472
sm_metadata_new_block(struct dm_space_map * sm,dm_block_t * b)473 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
474 {
475 dm_block_t count;
476 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
477
478 int r = sm_metadata_new_block_(sm, b);
479 if (r) {
480 DMERR_LIMIT("unable to allocate new metadata block");
481 return r;
482 }
483
484 r = sm_metadata_get_nr_free(sm, &count);
485 if (r) {
486 DMERR_LIMIT("couldn't get free block count");
487 return r;
488 }
489
490 check_threshold(&smm->threshold, count);
491
492 return r;
493 }
494
sm_metadata_commit(struct dm_space_map * sm)495 static int sm_metadata_commit(struct dm_space_map *sm)
496 {
497 int r;
498 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
499
500 r = sm_ll_commit(&smm->ll);
501 if (r)
502 return r;
503
504 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
505 smm->begin = 0;
506 smm->allocated_this_transaction = 0;
507
508 return 0;
509 }
510
sm_metadata_register_threshold_callback(struct dm_space_map * sm,dm_block_t threshold,dm_sm_threshold_fn fn,void * context)511 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
512 dm_block_t threshold,
513 dm_sm_threshold_fn fn,
514 void *context)
515 {
516 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
517
518 set_threshold(&smm->threshold, threshold, fn, context);
519
520 return 0;
521 }
522
sm_metadata_root_size(struct dm_space_map * sm,size_t * result)523 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
524 {
525 *result = sizeof(struct disk_sm_root);
526
527 return 0;
528 }
529
sm_metadata_copy_root(struct dm_space_map * sm,void * where_le,size_t max)530 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
531 {
532 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
533 struct disk_sm_root root_le;
534
535 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
536 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
537 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
538 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
539
540 if (max < sizeof(root_le))
541 return -ENOSPC;
542
543 memcpy(where_le, &root_le, sizeof(root_le));
544
545 return 0;
546 }
547
548 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
549
550 static const struct dm_space_map ops = {
551 .destroy = sm_metadata_destroy,
552 .extend = sm_metadata_extend,
553 .get_nr_blocks = sm_metadata_get_nr_blocks,
554 .get_nr_free = sm_metadata_get_nr_free,
555 .get_count = sm_metadata_get_count,
556 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
557 .set_count = sm_metadata_set_count,
558 .inc_block = sm_metadata_inc_block,
559 .dec_block = sm_metadata_dec_block,
560 .new_block = sm_metadata_new_block,
561 .commit = sm_metadata_commit,
562 .root_size = sm_metadata_root_size,
563 .copy_root = sm_metadata_copy_root,
564 .register_threshold_callback = sm_metadata_register_threshold_callback
565 };
566
567 /*----------------------------------------------------------------*/
568
569 /*
570 * When a new space map is created that manages its own space. We use
571 * this tiny bootstrap allocator.
572 */
sm_bootstrap_destroy(struct dm_space_map * sm)573 static void sm_bootstrap_destroy(struct dm_space_map *sm)
574 {
575 }
576
sm_bootstrap_extend(struct dm_space_map * sm,dm_block_t extra_blocks)577 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
578 {
579 DMERR("bootstrap doesn't support extend");
580
581 return -EINVAL;
582 }
583
sm_bootstrap_get_nr_blocks(struct dm_space_map * sm,dm_block_t * count)584 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
585 {
586 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
587
588 *count = smm->ll.nr_blocks;
589
590 return 0;
591 }
592
sm_bootstrap_get_nr_free(struct dm_space_map * sm,dm_block_t * count)593 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
594 {
595 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
596
597 *count = smm->ll.nr_blocks - smm->begin;
598
599 return 0;
600 }
601
sm_bootstrap_get_count(struct dm_space_map * sm,dm_block_t b,uint32_t * result)602 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
603 uint32_t *result)
604 {
605 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
606
607 *result = (b < smm->begin) ? 1 : 0;
608
609 return 0;
610 }
611
sm_bootstrap_count_is_more_than_one(struct dm_space_map * sm,dm_block_t b,int * result)612 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
613 dm_block_t b, int *result)
614 {
615 *result = 0;
616
617 return 0;
618 }
619
sm_bootstrap_set_count(struct dm_space_map * sm,dm_block_t b,uint32_t count)620 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
621 uint32_t count)
622 {
623 DMERR("bootstrap doesn't support set_count");
624
625 return -EINVAL;
626 }
627
sm_bootstrap_new_block(struct dm_space_map * sm,dm_block_t * b)628 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
629 {
630 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
631
632 /*
633 * We know the entire device is unused.
634 */
635 if (smm->begin == smm->ll.nr_blocks)
636 return -ENOSPC;
637
638 *b = smm->begin++;
639
640 return 0;
641 }
642
sm_bootstrap_inc_block(struct dm_space_map * sm,dm_block_t b)643 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
644 {
645 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
646
647 return add_bop(smm, BOP_INC, b);
648 }
649
sm_bootstrap_dec_block(struct dm_space_map * sm,dm_block_t b)650 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
651 {
652 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
653
654 return add_bop(smm, BOP_DEC, b);
655 }
656
sm_bootstrap_commit(struct dm_space_map * sm)657 static int sm_bootstrap_commit(struct dm_space_map *sm)
658 {
659 return 0;
660 }
661
sm_bootstrap_root_size(struct dm_space_map * sm,size_t * result)662 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
663 {
664 DMERR("bootstrap doesn't support root_size");
665
666 return -EINVAL;
667 }
668
sm_bootstrap_copy_root(struct dm_space_map * sm,void * where,size_t max)669 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
670 size_t max)
671 {
672 DMERR("bootstrap doesn't support copy_root");
673
674 return -EINVAL;
675 }
676
677 static const struct dm_space_map bootstrap_ops = {
678 .destroy = sm_bootstrap_destroy,
679 .extend = sm_bootstrap_extend,
680 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
681 .get_nr_free = sm_bootstrap_get_nr_free,
682 .get_count = sm_bootstrap_get_count,
683 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
684 .set_count = sm_bootstrap_set_count,
685 .inc_block = sm_bootstrap_inc_block,
686 .dec_block = sm_bootstrap_dec_block,
687 .new_block = sm_bootstrap_new_block,
688 .commit = sm_bootstrap_commit,
689 .root_size = sm_bootstrap_root_size,
690 .copy_root = sm_bootstrap_copy_root,
691 .register_threshold_callback = NULL
692 };
693
694 /*----------------------------------------------------------------*/
695
sm_metadata_extend(struct dm_space_map * sm,dm_block_t extra_blocks)696 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
697 {
698 int r, i;
699 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
700 dm_block_t old_len = smm->ll.nr_blocks;
701
702 /*
703 * Flick into a mode where all blocks get allocated in the new area.
704 */
705 smm->begin = old_len;
706 memcpy(sm, &bootstrap_ops, sizeof(*sm));
707
708 /*
709 * Extend.
710 */
711 r = sm_ll_extend(&smm->ll, extra_blocks);
712 if (r)
713 goto out;
714
715 /*
716 * We repeatedly increment then commit until the commit doesn't
717 * allocate any new blocks.
718 */
719 do {
720 for (i = old_len; !r && i < smm->begin; i++)
721 r = add_bop(smm, BOP_INC, i);
722
723 if (r)
724 goto out;
725
726 old_len = smm->begin;
727
728 r = apply_bops(smm);
729 if (r) {
730 DMERR("%s: apply_bops failed", __func__);
731 goto out;
732 }
733
734 r = sm_ll_commit(&smm->ll);
735 if (r)
736 goto out;
737
738 } while (old_len != smm->begin);
739
740 out:
741 /*
742 * Switch back to normal behaviour.
743 */
744 memcpy(sm, &ops, sizeof(*sm));
745 return r;
746 }
747
748 /*----------------------------------------------------------------*/
749
dm_sm_metadata_init(void)750 struct dm_space_map *dm_sm_metadata_init(void)
751 {
752 struct sm_metadata *smm;
753
754 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
755 if (!smm)
756 return ERR_PTR(-ENOMEM);
757
758 memcpy(&smm->sm, &ops, sizeof(smm->sm));
759
760 return &smm->sm;
761 }
762
dm_sm_metadata_create(struct dm_space_map * sm,struct dm_transaction_manager * tm,dm_block_t nr_blocks,dm_block_t superblock)763 int dm_sm_metadata_create(struct dm_space_map *sm,
764 struct dm_transaction_manager *tm,
765 dm_block_t nr_blocks,
766 dm_block_t superblock)
767 {
768 int r;
769 dm_block_t i;
770 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
771
772 smm->begin = superblock + 1;
773 smm->recursion_count = 0;
774 smm->allocated_this_transaction = 0;
775 brb_init(&smm->uncommitted);
776 threshold_init(&smm->threshold);
777
778 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
779
780 r = sm_ll_new_metadata(&smm->ll, tm);
781 if (!r) {
782 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
783 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
784 r = sm_ll_extend(&smm->ll, nr_blocks);
785 }
786 memcpy(&smm->sm, &ops, sizeof(smm->sm));
787 if (r)
788 return r;
789
790 /*
791 * Now we need to update the newly created data structures with the
792 * allocated blocks that they were built from.
793 */
794 for (i = superblock; !r && i < smm->begin; i++)
795 r = add_bop(smm, BOP_INC, i);
796
797 if (r)
798 return r;
799
800 r = apply_bops(smm);
801 if (r) {
802 DMERR("%s: apply_bops failed", __func__);
803 return r;
804 }
805
806 return sm_metadata_commit(sm);
807 }
808
dm_sm_metadata_open(struct dm_space_map * sm,struct dm_transaction_manager * tm,void * root_le,size_t len)809 int dm_sm_metadata_open(struct dm_space_map *sm,
810 struct dm_transaction_manager *tm,
811 void *root_le, size_t len)
812 {
813 int r;
814 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
815
816 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
817 if (r)
818 return r;
819
820 smm->begin = 0;
821 smm->recursion_count = 0;
822 smm->allocated_this_transaction = 0;
823 brb_init(&smm->uncommitted);
824 threshold_init(&smm->threshold);
825
826 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
827 return 0;
828 }
829