Linux 3.12.39
[linux/fpc-iii.git] / drivers / md / persistent-data / dm-space-map-metadata.c
blobd9a5aa53201790b051d8aefb77f927ba3648b216
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-space-map.h"
8 #include "dm-space-map-common.h"
9 #include "dm-space-map-metadata.h"
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/device-mapper.h>
15 #define DM_MSG_PREFIX "space map metadata"
17 /*----------------------------------------------------------------*/
20 * An edge triggered threshold.
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;
31 static void threshold_init(struct threshold *t)
33 t->threshold_set = false;
34 t->value_set = false;
37 static void set_threshold(struct threshold *t, dm_block_t value,
38 dm_sm_threshold_fn fn, void *context)
40 t->threshold_set = true;
41 t->threshold = value;
42 t->fn = fn;
43 t->context = context;
46 static bool below_threshold(struct threshold *t, dm_block_t value)
48 return t->threshold_set && value <= t->threshold;
51 static bool threshold_already_triggered(struct threshold *t)
53 return t->value_set && below_threshold(t, t->current_value);
56 static void check_threshold(struct threshold *t, dm_block_t value)
58 if (below_threshold(t, value) &&
59 !threshold_already_triggered(t))
60 t->fn(t->context);
62 t->value_set = true;
63 t->current_value = value;
66 /*----------------------------------------------------------------*/
69 * Space map interface.
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.
79 * FIXME: we should calculate this based on the size of the device.
80 * Only the metadata space map needs this functionality.
82 #define MAX_RECURSIVE_ALLOCATIONS 1024
84 enum block_op_type {
85 BOP_INC,
86 BOP_DEC
89 struct block_op {
90 enum block_op_type type;
91 dm_block_t block;
94 struct bop_ring_buffer {
95 unsigned begin;
96 unsigned end;
97 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
100 static void brb_init(struct bop_ring_buffer *brb)
102 brb->begin = 0;
103 brb->end = 0;
106 static bool brb_empty(struct bop_ring_buffer *brb)
108 return brb->begin == brb->end;
111 static unsigned brb_next(struct bop_ring_buffer *brb, unsigned old)
113 unsigned r = old + 1;
114 return (r >= (sizeof(brb->bops) / sizeof(*brb->bops))) ? 0 : r;
117 static int brb_push(struct bop_ring_buffer *brb,
118 enum block_op_type type, dm_block_t b)
120 struct block_op *bop;
121 unsigned next = brb_next(brb, brb->end);
124 * We don't allow the last bop to be filled, this way we can
125 * differentiate between full and empty.
127 if (next == brb->begin)
128 return -ENOMEM;
130 bop = brb->bops + brb->end;
131 bop->type = type;
132 bop->block = b;
134 brb->end = next;
136 return 0;
139 static int brb_pop(struct bop_ring_buffer *brb, struct block_op *result)
141 struct block_op *bop;
143 if (brb_empty(brb))
144 return -ENODATA;
146 bop = brb->bops + brb->begin;
147 result->type = bop->type;
148 result->block = bop->block;
150 brb->begin = brb_next(brb, brb->begin);
152 return 0;
155 /*----------------------------------------------------------------*/
157 struct sm_metadata {
158 struct dm_space_map sm;
160 struct ll_disk ll;
161 struct ll_disk old_ll;
163 dm_block_t begin;
165 unsigned recursion_count;
166 unsigned allocated_this_transaction;
167 struct bop_ring_buffer uncommitted;
169 struct threshold threshold;
172 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
174 int r = brb_push(&smm->uncommitted, type, b);
176 if (r) {
177 DMERR("too many recursive allocations");
178 return -ENOMEM;
181 return 0;
184 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
186 int r = 0;
187 enum allocation_event ev;
189 switch (op->type) {
190 case BOP_INC:
191 r = sm_ll_inc(&smm->ll, op->block, &ev);
192 break;
194 case BOP_DEC:
195 r = sm_ll_dec(&smm->ll, op->block, &ev);
196 break;
199 return r;
202 static void in(struct sm_metadata *smm)
204 smm->recursion_count++;
207 static int out(struct sm_metadata *smm)
209 int r = 0;
212 * If we're not recursing then very bad things are happening.
214 if (!smm->recursion_count) {
215 DMERR("lost track of recursion depth");
216 return -ENOMEM;
219 if (smm->recursion_count == 1) {
220 while (!brb_empty(&smm->uncommitted)) {
221 struct block_op bop;
223 r = brb_pop(&smm->uncommitted, &bop);
224 if (r) {
225 DMERR("bug in bop ring buffer");
226 break;
229 r = commit_bop(smm, &bop);
230 if (r)
231 break;
235 smm->recursion_count--;
237 return r;
241 * When using the out() function above, we often want to combine an error
242 * code for the operation run in the recursive context with that from
243 * out().
245 static int combine_errors(int r1, int r2)
247 return r1 ? r1 : r2;
250 static int recursing(struct sm_metadata *smm)
252 return smm->recursion_count;
255 static void sm_metadata_destroy(struct dm_space_map *sm)
257 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
259 kfree(smm);
262 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
264 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
266 *count = smm->ll.nr_blocks;
268 return 0;
271 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
273 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
275 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
276 smm->allocated_this_transaction;
278 return 0;
281 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
282 uint32_t *result)
284 int r;
285 unsigned i;
286 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
287 unsigned adjustment = 0;
290 * We may have some uncommitted adjustments to add. This list
291 * should always be really short.
293 for (i = smm->uncommitted.begin;
294 i != smm->uncommitted.end;
295 i = brb_next(&smm->uncommitted, i)) {
296 struct block_op *op = smm->uncommitted.bops + i;
298 if (op->block != b)
299 continue;
301 switch (op->type) {
302 case BOP_INC:
303 adjustment++;
304 break;
306 case BOP_DEC:
307 adjustment--;
308 break;
312 r = sm_ll_lookup(&smm->ll, b, result);
313 if (r)
314 return r;
316 *result += adjustment;
318 return 0;
321 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
322 dm_block_t b, int *result)
324 int r, adjustment = 0;
325 unsigned i;
326 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
327 uint32_t rc;
330 * We may have some uncommitted adjustments to add. This list
331 * should always be really short.
333 for (i = smm->uncommitted.begin;
334 i != smm->uncommitted.end;
335 i = brb_next(&smm->uncommitted, i)) {
337 struct block_op *op = smm->uncommitted.bops + i;
339 if (op->block != b)
340 continue;
342 switch (op->type) {
343 case BOP_INC:
344 adjustment++;
345 break;
347 case BOP_DEC:
348 adjustment--;
349 break;
353 if (adjustment > 1) {
354 *result = 1;
355 return 0;
358 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
359 if (r)
360 return r;
362 if (rc == 3)
364 * We err on the side of caution, and always return true.
366 *result = 1;
367 else
368 *result = rc + adjustment > 1;
370 return 0;
373 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
374 uint32_t count)
376 int r, r2;
377 enum allocation_event ev;
378 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
380 if (smm->recursion_count) {
381 DMERR("cannot recurse set_count()");
382 return -EINVAL;
385 in(smm);
386 r = sm_ll_insert(&smm->ll, b, count, &ev);
387 r2 = out(smm);
389 return combine_errors(r, r2);
392 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
394 int r, r2 = 0;
395 enum allocation_event ev;
396 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
398 if (recursing(smm))
399 r = add_bop(smm, BOP_INC, b);
400 else {
401 in(smm);
402 r = sm_ll_inc(&smm->ll, b, &ev);
403 r2 = out(smm);
406 return combine_errors(r, r2);
409 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
411 int r, r2 = 0;
412 enum allocation_event ev;
413 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
415 if (recursing(smm))
416 r = add_bop(smm, BOP_DEC, b);
417 else {
418 in(smm);
419 r = sm_ll_dec(&smm->ll, b, &ev);
420 r2 = out(smm);
423 return combine_errors(r, r2);
426 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
428 int r, r2 = 0;
429 enum allocation_event ev;
430 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
432 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
433 if (r)
434 return r;
436 smm->begin = *b + 1;
438 if (recursing(smm))
439 r = add_bop(smm, BOP_INC, *b);
440 else {
441 in(smm);
442 r = sm_ll_inc(&smm->ll, *b, &ev);
443 r2 = out(smm);
446 if (!r)
447 smm->allocated_this_transaction++;
449 return combine_errors(r, r2);
452 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
454 dm_block_t count;
455 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
457 int r = sm_metadata_new_block_(sm, b);
458 if (r) {
459 DMERR("unable to allocate new metadata block");
460 return r;
463 r = sm_metadata_get_nr_free(sm, &count);
464 if (r) {
465 DMERR("couldn't get free block count");
466 return r;
469 check_threshold(&smm->threshold, count);
471 return r;
474 static int sm_metadata_commit(struct dm_space_map *sm)
476 int r;
477 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
479 r = sm_ll_commit(&smm->ll);
480 if (r)
481 return r;
483 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
484 smm->begin = 0;
485 smm->allocated_this_transaction = 0;
487 return 0;
490 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
491 dm_block_t threshold,
492 dm_sm_threshold_fn fn,
493 void *context)
495 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
497 set_threshold(&smm->threshold, threshold, fn, context);
499 return 0;
502 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
504 *result = sizeof(struct disk_sm_root);
506 return 0;
509 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
511 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
512 struct disk_sm_root root_le;
514 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
515 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
516 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
517 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
519 if (max < sizeof(root_le))
520 return -ENOSPC;
522 memcpy(where_le, &root_le, sizeof(root_le));
524 return 0;
527 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
529 static struct dm_space_map ops = {
530 .destroy = sm_metadata_destroy,
531 .extend = sm_metadata_extend,
532 .get_nr_blocks = sm_metadata_get_nr_blocks,
533 .get_nr_free = sm_metadata_get_nr_free,
534 .get_count = sm_metadata_get_count,
535 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
536 .set_count = sm_metadata_set_count,
537 .inc_block = sm_metadata_inc_block,
538 .dec_block = sm_metadata_dec_block,
539 .new_block = sm_metadata_new_block,
540 .commit = sm_metadata_commit,
541 .root_size = sm_metadata_root_size,
542 .copy_root = sm_metadata_copy_root,
543 .register_threshold_callback = sm_metadata_register_threshold_callback
546 /*----------------------------------------------------------------*/
549 * When a new space map is created that manages its own space. We use
550 * this tiny bootstrap allocator.
552 static void sm_bootstrap_destroy(struct dm_space_map *sm)
556 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
558 DMERR("bootstrap doesn't support extend");
560 return -EINVAL;
563 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
565 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
567 *count = smm->ll.nr_blocks;
569 return 0;
572 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
574 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
576 *count = smm->ll.nr_blocks - smm->begin;
578 return 0;
581 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
582 uint32_t *result)
584 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
586 return b < smm->begin ? 1 : 0;
589 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
590 dm_block_t b, int *result)
592 *result = 0;
594 return 0;
597 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
598 uint32_t count)
600 DMERR("bootstrap doesn't support set_count");
602 return -EINVAL;
605 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
607 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
610 * We know the entire device is unused.
612 if (smm->begin == smm->ll.nr_blocks)
613 return -ENOSPC;
615 *b = smm->begin++;
617 return 0;
620 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
622 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
624 return add_bop(smm, BOP_INC, b);
627 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
629 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
631 return add_bop(smm, BOP_DEC, b);
634 static int sm_bootstrap_commit(struct dm_space_map *sm)
636 return 0;
639 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
641 DMERR("bootstrap doesn't support root_size");
643 return -EINVAL;
646 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
647 size_t max)
649 DMERR("bootstrap doesn't support copy_root");
651 return -EINVAL;
654 static struct dm_space_map bootstrap_ops = {
655 .destroy = sm_bootstrap_destroy,
656 .extend = sm_bootstrap_extend,
657 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
658 .get_nr_free = sm_bootstrap_get_nr_free,
659 .get_count = sm_bootstrap_get_count,
660 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
661 .set_count = sm_bootstrap_set_count,
662 .inc_block = sm_bootstrap_inc_block,
663 .dec_block = sm_bootstrap_dec_block,
664 .new_block = sm_bootstrap_new_block,
665 .commit = sm_bootstrap_commit,
666 .root_size = sm_bootstrap_root_size,
667 .copy_root = sm_bootstrap_copy_root,
668 .register_threshold_callback = NULL
671 /*----------------------------------------------------------------*/
673 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
675 int r, i;
676 enum allocation_event ev;
677 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
678 dm_block_t old_len = smm->ll.nr_blocks;
681 * Flick into a mode where all blocks get allocated in the new area.
683 smm->begin = old_len;
684 memcpy(sm, &bootstrap_ops, sizeof(*sm));
687 * Extend.
689 r = sm_ll_extend(&smm->ll, extra_blocks);
690 if (r)
691 goto out;
694 * We repeatedly increment then commit until the commit doesn't
695 * allocate any new blocks.
697 do {
698 for (i = old_len; !r && i < smm->begin; i++) {
699 r = sm_ll_inc(&smm->ll, i, &ev);
700 if (r)
701 goto out;
703 old_len = smm->begin;
705 r = sm_ll_commit(&smm->ll);
706 if (r)
707 goto out;
709 } while (old_len != smm->begin);
711 out:
713 * Switch back to normal behaviour.
715 memcpy(sm, &ops, sizeof(*sm));
716 return r;
719 /*----------------------------------------------------------------*/
721 struct dm_space_map *dm_sm_metadata_init(void)
723 struct sm_metadata *smm;
725 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
726 if (!smm)
727 return ERR_PTR(-ENOMEM);
729 memcpy(&smm->sm, &ops, sizeof(smm->sm));
731 return &smm->sm;
734 int dm_sm_metadata_create(struct dm_space_map *sm,
735 struct dm_transaction_manager *tm,
736 dm_block_t nr_blocks,
737 dm_block_t superblock)
739 int r;
740 dm_block_t i;
741 enum allocation_event ev;
742 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
744 smm->begin = superblock + 1;
745 smm->recursion_count = 0;
746 smm->allocated_this_transaction = 0;
747 brb_init(&smm->uncommitted);
748 threshold_init(&smm->threshold);
750 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
752 r = sm_ll_new_metadata(&smm->ll, tm);
753 if (r)
754 return r;
756 r = sm_ll_extend(&smm->ll, nr_blocks);
757 if (r)
758 return r;
760 memcpy(&smm->sm, &ops, sizeof(smm->sm));
763 * Now we need to update the newly created data structures with the
764 * allocated blocks that they were built from.
766 for (i = superblock; !r && i < smm->begin; i++)
767 r = sm_ll_inc(&smm->ll, i, &ev);
769 if (r)
770 return r;
772 return sm_metadata_commit(sm);
775 int dm_sm_metadata_open(struct dm_space_map *sm,
776 struct dm_transaction_manager *tm,
777 void *root_le, size_t len)
779 int r;
780 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
782 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
783 if (r)
784 return r;
786 smm->begin = 0;
787 smm->recursion_count = 0;
788 smm->allocated_this_transaction = 0;
789 brb_init(&smm->uncommitted);
790 threshold_init(&smm->threshold);
792 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
793 return 0;