blob: 20557e2c60c6236bf16cc35e33d3f9eb0e318779 [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-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/*
Joe Thornber2fc48022013-05-10 14:37:20 +010020 * An edge triggered threshold.
21 */
22struct 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
31static void threshold_init(struct threshold *t)
32{
33 t->threshold_set = false;
34 t->value_set = false;
35}
36
37static 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
46static bool below_threshold(struct threshold *t, dm_block_t value)
47{
48 return t->threshold_set && value <= t->threshold;
49}
50
51static bool threshold_already_triggered(struct threshold *t)
52{
53 return t->value_set && below_threshold(t, t->current_value);
54}
55
56static 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/*
Joe Thornber3241b1d2011-10-31 20:19:11 +000069 * 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
84enum block_op_type {
85 BOP_INC,
86 BOP_DEC
87};
88
89struct block_op {
90 enum block_op_type type;
91 dm_block_t block;
92};
93
Joe Thornbercebc2de2014-03-07 14:57:19 +000094struct bop_ring_buffer {
95 unsigned begin;
96 unsigned end;
97 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
98};
99
100static void brb_init(struct bop_ring_buffer *brb)
101{
102 brb->begin = 0;
103 brb->end = 0;
104}
105
106static bool brb_empty(struct bop_ring_buffer *brb)
107{
108 return brb->begin == brb->end;
109}
110
111static 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
117static 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
Joe Thornber50dd8422015-12-09 16:38:12 +0000139static int brb_peek(struct bop_ring_buffer *brb, struct block_op *result)
Joe Thornbercebc2de2014-03-07 14:57:19 +0000140{
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
Joe Thornber50dd8422015-12-09 16:38:12 +0000150 return 0;
151}
152
153static int brb_pop(struct bop_ring_buffer *brb)
154{
Joe Thornber50dd8422015-12-09 16:38:12 +0000155 if (brb_empty(brb))
156 return -ENODATA;
157
Joe Thornbercebc2de2014-03-07 14:57:19 +0000158 brb->begin = brb_next(brb, brb->begin);
159
160 return 0;
161}
162
163/*----------------------------------------------------------------*/
164
Joe Thornber3241b1d2011-10-31 20:19:11 +0000165struct 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;
Joe Thornbercebc2de2014-03-07 14:57:19 +0000175 struct bop_ring_buffer uncommitted;
Joe Thornber2fc48022013-05-10 14:37:20 +0100176
177 struct threshold threshold;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000178};
179
180static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
181{
Joe Thornbercebc2de2014-03-07 14:57:19 +0000182 int r = brb_push(&smm->uncommitted, type, b);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000183
Joe Thornbercebc2de2014-03-07 14:57:19 +0000184 if (r) {
Joe Thornber3241b1d2011-10-31 20:19:11 +0000185 DMERR("too many recursive allocations");
186 return -ENOMEM;
187 }
188
Joe Thornber3241b1d2011-10-31 20:19:11 +0000189 return 0;
190}
191
192static 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
210static void in(struct sm_metadata *smm)
211{
212 smm->recursion_count++;
213}
214
Joe Thornber6096d912015-06-17 13:35:19 +0100215static 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
Joe Thornber50dd8422015-12-09 16:38:12 +0000222 r = brb_peek(&smm->uncommitted, &bop);
Joe Thornber6096d912015-06-17 13:35:19 +0100223 if (r) {
224 DMERR("bug in bop ring buffer");
225 break;
226 }
227
228 r = commit_bop(smm, &bop);
229 if (r)
230 break;
Joe Thornber50dd8422015-12-09 16:38:12 +0000231
232 brb_pop(&smm->uncommitted);
Joe Thornber6096d912015-06-17 13:35:19 +0100233 }
234
235 return r;
236}
237
Joe Thornber3241b1d2011-10-31 20:19:11 +0000238static 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
Joe Thornber6096d912015-06-17 13:35:19 +0100250 if (smm->recursion_count == 1)
251 apply_bops(smm);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000252
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 */
263static int combine_errors(int r1, int r2)
264{
265 return r1 ? r1 : r2;
266}
267
268static int recursing(struct sm_metadata *smm)
269{
270 return smm->recursion_count;
271}
272
273static 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
Joe Thornber3241b1d2011-10-31 20:19:11 +0000280static 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
289static 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
299static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
300 uint32_t *result)
301{
Joe Thornbercebc2de2014-03-07 14:57:19 +0000302 int r;
303 unsigned i;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000304 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 */
Joe Thornbercebc2de2014-03-07 14:57:19 +0000311 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;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000315
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
339static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
340 dm_block_t b, int *result)
341{
Joe Thornbercebc2de2014-03-07 14:57:19 +0000342 int r, adjustment = 0;
343 unsigned i;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000344 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 */
Joe Thornbercebc2de2014-03-07 14:57:19 +0000351 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;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000356
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
391static 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
410static 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
427static 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
444static 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 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
451 if (r)
452 return r;
453
454 smm->begin = *b + 1;
455
456 if (recursing(smm))
457 r = add_bop(smm, BOP_INC, *b);
458 else {
459 in(smm);
460 r = sm_ll_inc(&smm->ll, *b, &ev);
461 r2 = out(smm);
462 }
463
464 if (!r)
465 smm->allocated_this_transaction++;
466
467 return combine_errors(r, r2);
468}
469
470static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
471{
Joe Thornber2fc48022013-05-10 14:37:20 +0100472 dm_block_t count;
473 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
474
Joe Thornber3241b1d2011-10-31 20:19:11 +0000475 int r = sm_metadata_new_block_(sm, b);
Mike Snitzerf62b6b82013-12-02 16:47:01 -0500476 if (r) {
Mike Snitzerc46985e2013-12-13 09:58:46 -0500477 DMERR_LIMIT("unable to allocate new metadata block");
Mike Snitzerf62b6b82013-12-02 16:47:01 -0500478 return r;
479 }
Joe Thornber2fc48022013-05-10 14:37:20 +0100480
481 r = sm_metadata_get_nr_free(sm, &count);
Mike Snitzerf62b6b82013-12-02 16:47:01 -0500482 if (r) {
Mike Snitzerc46985e2013-12-13 09:58:46 -0500483 DMERR_LIMIT("couldn't get free block count");
Mike Snitzerf62b6b82013-12-02 16:47:01 -0500484 return r;
485 }
Joe Thornber2fc48022013-05-10 14:37:20 +0100486
487 check_threshold(&smm->threshold, count);
488
Joe Thornber3241b1d2011-10-31 20:19:11 +0000489 return r;
490}
491
492static int sm_metadata_commit(struct dm_space_map *sm)
493{
494 int r;
495 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
496
497 r = sm_ll_commit(&smm->ll);
498 if (r)
499 return r;
500
501 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
502 smm->begin = 0;
503 smm->allocated_this_transaction = 0;
504
505 return 0;
506}
507
Joe Thornber2fc48022013-05-10 14:37:20 +0100508static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
509 dm_block_t threshold,
510 dm_sm_threshold_fn fn,
511 void *context)
512{
513 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
514
515 set_threshold(&smm->threshold, threshold, fn, context);
516
517 return 0;
518}
519
Joe Thornber3241b1d2011-10-31 20:19:11 +0000520static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
521{
522 *result = sizeof(struct disk_sm_root);
523
524 return 0;
525}
526
527static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
528{
529 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
530 struct disk_sm_root root_le;
531
532 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
533 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
534 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
535 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
536
537 if (max < sizeof(root_le))
538 return -ENOSPC;
539
540 memcpy(where_le, &root_le, sizeof(root_le));
541
542 return 0;
543}
544
Joe Thornber1921c562013-05-10 14:37:19 +0100545static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
546
Joe Thornber3241b1d2011-10-31 20:19:11 +0000547static struct dm_space_map ops = {
548 .destroy = sm_metadata_destroy,
549 .extend = sm_metadata_extend,
550 .get_nr_blocks = sm_metadata_get_nr_blocks,
551 .get_nr_free = sm_metadata_get_nr_free,
552 .get_count = sm_metadata_get_count,
553 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
554 .set_count = sm_metadata_set_count,
555 .inc_block = sm_metadata_inc_block,
556 .dec_block = sm_metadata_dec_block,
557 .new_block = sm_metadata_new_block,
558 .commit = sm_metadata_commit,
559 .root_size = sm_metadata_root_size,
Joe Thornber7c3d3f22013-05-10 14:37:20 +0100560 .copy_root = sm_metadata_copy_root,
Joe Thornber2fc48022013-05-10 14:37:20 +0100561 .register_threshold_callback = sm_metadata_register_threshold_callback
Joe Thornber3241b1d2011-10-31 20:19:11 +0000562};
563
564/*----------------------------------------------------------------*/
565
566/*
567 * When a new space map is created that manages its own space. We use
568 * this tiny bootstrap allocator.
569 */
570static void sm_bootstrap_destroy(struct dm_space_map *sm)
571{
572}
573
574static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
575{
Joe Thornber88a488f2013-05-10 14:37:17 +0100576 DMERR("bootstrap doesn't support extend");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000577
578 return -EINVAL;
579}
580
581static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
582{
583 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
584
Dan Carpenterc1c61562014-11-29 15:50:21 +0300585 *count = smm->ll.nr_blocks;
586
587 return 0;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000588}
589
590static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
591{
592 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
593
594 *count = smm->ll.nr_blocks - smm->begin;
595
596 return 0;
597}
598
599static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
600 uint32_t *result)
601{
602 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
603
Joe Thornber02717d92014-12-01 14:38:11 +0000604 *result = (b < smm->begin) ? 1 : 0;
605
606 return 0;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000607}
608
609static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
610 dm_block_t b, int *result)
611{
612 *result = 0;
613
614 return 0;
615}
616
617static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
618 uint32_t count)
619{
Joe Thornber88a488f2013-05-10 14:37:17 +0100620 DMERR("bootstrap doesn't support set_count");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000621
622 return -EINVAL;
623}
624
625static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
626{
627 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
628
629 /*
630 * We know the entire device is unused.
631 */
632 if (smm->begin == smm->ll.nr_blocks)
633 return -ENOSPC;
634
635 *b = smm->begin++;
636
637 return 0;
638}
639
640static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
641{
642 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
643
644 return add_bop(smm, BOP_INC, b);
645}
646
647static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
648{
649 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
650
651 return add_bop(smm, BOP_DEC, b);
652}
653
654static int sm_bootstrap_commit(struct dm_space_map *sm)
655{
656 return 0;
657}
658
659static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
660{
Joe Thornber88a488f2013-05-10 14:37:17 +0100661 DMERR("bootstrap doesn't support root_size");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000662
663 return -EINVAL;
664}
665
666static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
667 size_t max)
668{
Joe Thornber88a488f2013-05-10 14:37:17 +0100669 DMERR("bootstrap doesn't support copy_root");
Joe Thornber3241b1d2011-10-31 20:19:11 +0000670
671 return -EINVAL;
672}
673
674static struct dm_space_map bootstrap_ops = {
675 .destroy = sm_bootstrap_destroy,
676 .extend = sm_bootstrap_extend,
677 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
678 .get_nr_free = sm_bootstrap_get_nr_free,
679 .get_count = sm_bootstrap_get_count,
680 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
681 .set_count = sm_bootstrap_set_count,
682 .inc_block = sm_bootstrap_inc_block,
683 .dec_block = sm_bootstrap_dec_block,
684 .new_block = sm_bootstrap_new_block,
685 .commit = sm_bootstrap_commit,
686 .root_size = sm_bootstrap_root_size,
Joe Thornber7c3d3f22013-05-10 14:37:20 +0100687 .copy_root = sm_bootstrap_copy_root,
688 .register_threshold_callback = NULL
Joe Thornber3241b1d2011-10-31 20:19:11 +0000689};
690
691/*----------------------------------------------------------------*/
692
Joe Thornber1921c562013-05-10 14:37:19 +0100693static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
694{
695 int r, i;
Joe Thornber1921c562013-05-10 14:37:19 +0100696 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
697 dm_block_t old_len = smm->ll.nr_blocks;
698
699 /*
700 * Flick into a mode where all blocks get allocated in the new area.
701 */
702 smm->begin = old_len;
Joe Thornber7e664b32014-01-07 15:49:02 +0000703 memcpy(sm, &bootstrap_ops, sizeof(*sm));
Joe Thornber1921c562013-05-10 14:37:19 +0100704
705 /*
706 * Extend.
707 */
708 r = sm_ll_extend(&smm->ll, extra_blocks);
Joe Thornber7e664b32014-01-07 15:49:02 +0000709 if (r)
710 goto out;
Joe Thornber1921c562013-05-10 14:37:19 +0100711
Joe Thornberfca02842014-01-21 11:07:32 +0000712 /*
713 * We repeatedly increment then commit until the commit doesn't
714 * allocate any new blocks.
715 */
716 do {
Joe Thornber50dd8422015-12-09 16:38:12 +0000717 for (i = old_len; !r && i < smm->begin; i++)
718 r = add_bop(smm, BOP_INC, i);
719
720 if (r)
721 goto out;
722
Joe Thornberfca02842014-01-21 11:07:32 +0000723 old_len = smm->begin;
724
Joe Thornber6096d912015-06-17 13:35:19 +0100725 r = apply_bops(smm);
726 if (r) {
727 DMERR("%s: apply_bops failed", __func__);
728 goto out;
729 }
730
Joe Thornberfca02842014-01-21 11:07:32 +0000731 r = sm_ll_commit(&smm->ll);
Joe Thornber7e664b32014-01-07 15:49:02 +0000732 if (r)
733 goto out;
Joe Thornber7e664b32014-01-07 15:49:02 +0000734
Joe Thornberfca02842014-01-21 11:07:32 +0000735 } while (old_len != smm->begin);
Joe Thornber7e664b32014-01-07 15:49:02 +0000736
737out:
Joe Thornber1921c562013-05-10 14:37:19 +0100738 /*
739 * Switch back to normal behaviour.
740 */
Joe Thornber7e664b32014-01-07 15:49:02 +0000741 memcpy(sm, &ops, sizeof(*sm));
Joe Thornber1921c562013-05-10 14:37:19 +0100742 return r;
743}
744
745/*----------------------------------------------------------------*/
746
Joe Thornber3241b1d2011-10-31 20:19:11 +0000747struct dm_space_map *dm_sm_metadata_init(void)
748{
749 struct sm_metadata *smm;
750
751 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
752 if (!smm)
753 return ERR_PTR(-ENOMEM);
754
755 memcpy(&smm->sm, &ops, sizeof(smm->sm));
756
757 return &smm->sm;
758}
759
760int dm_sm_metadata_create(struct dm_space_map *sm,
761 struct dm_transaction_manager *tm,
762 dm_block_t nr_blocks,
763 dm_block_t superblock)
764{
765 int r;
766 dm_block_t i;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000767 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
768
769 smm->begin = superblock + 1;
770 smm->recursion_count = 0;
771 smm->allocated_this_transaction = 0;
Joe Thornbercebc2de2014-03-07 14:57:19 +0000772 brb_init(&smm->uncommitted);
Joe Thornber2fc48022013-05-10 14:37:20 +0100773 threshold_init(&smm->threshold);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000774
775 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
776
777 r = sm_ll_new_metadata(&smm->ll, tm);
Benjamin Marzinskif5dca482016-11-30 17:56:14 -0600778 if (!r) {
779 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
780 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
781 r = sm_ll_extend(&smm->ll, nr_blocks);
782 }
Joe Thornber3241b1d2011-10-31 20:19:11 +0000783 memcpy(&smm->sm, &ops, sizeof(smm->sm));
Benjamin Marzinskif5dca482016-11-30 17:56:14 -0600784 if (r)
785 return r;
Joe Thornber3241b1d2011-10-31 20:19:11 +0000786
787 /*
788 * Now we need to update the newly created data structures with the
789 * allocated blocks that they were built from.
790 */
791 for (i = superblock; !r && i < smm->begin; i++)
Joe Thornber50dd8422015-12-09 16:38:12 +0000792 r = add_bop(smm, BOP_INC, i);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000793
794 if (r)
795 return r;
796
Joe Thornber6096d912015-06-17 13:35:19 +0100797 r = apply_bops(smm);
798 if (r) {
799 DMERR("%s: apply_bops failed", __func__);
800 return r;
801 }
802
Joe Thornber3241b1d2011-10-31 20:19:11 +0000803 return sm_metadata_commit(sm);
804}
805
806int dm_sm_metadata_open(struct dm_space_map *sm,
807 struct dm_transaction_manager *tm,
808 void *root_le, size_t len)
809{
810 int r;
811 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
812
813 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
814 if (r)
815 return r;
816
817 smm->begin = 0;
818 smm->recursion_count = 0;
819 smm->allocated_this_transaction = 0;
Joe Thornbercebc2de2014-03-07 14:57:19 +0000820 brb_init(&smm->uncommitted);
Joe Thornber2fc48022013-05-10 14:37:20 +0100821 threshold_init(&smm->threshold);
Joe Thornber3241b1d2011-10-31 20:19:11 +0000822
823 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
824 return 0;
825}