2 * Copyright (C) 2011 Red Hat, Inc.
4 * This file is released under the GPL.
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>
14 #include <linux/kernel.h>
16 #define DM_MSG_PREFIX "space map metadata"
18 /*----------------------------------------------------------------*/
21 * An edge triggered threshold.
27 dm_block_t current_value;
28 dm_sm_threshold_fn fn;
32 static void threshold_init(struct threshold *t)
34 t->threshold_set = false;
38 static void set_threshold(struct threshold *t, dm_block_t value,
39 dm_sm_threshold_fn fn, void *context)
41 t->threshold_set = true;
47 static bool below_threshold(struct threshold *t, dm_block_t value)
49 return t->threshold_set && value <= t->threshold;
52 static bool threshold_already_triggered(struct threshold *t)
54 return t->value_set && below_threshold(t, t->current_value);
57 static void check_threshold(struct threshold *t, dm_block_t value)
59 if (below_threshold(t, value) &&
60 !threshold_already_triggered(t))
64 t->current_value = value;
67 /*----------------------------------------------------------------*/
70 * Space map interface.
72 * The low level disk format is written using the standard btree and
73 * transaction manager. This means that performing disk operations may
74 * cause us to recurse into the space map in order to allocate new blocks.
75 * For this reason we have a pool of pre-allocated blocks large enough to
76 * service any metadata_ll_disk operation.
80 * FIXME: we should calculate this based on the size of the device.
81 * Only the metadata space map needs this functionality.
83 #define MAX_RECURSIVE_ALLOCATIONS 1024
91 enum block_op_type type;
96 struct bop_ring_buffer {
99 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
102 static void brb_init(struct bop_ring_buffer *brb)
108 static bool brb_empty(struct bop_ring_buffer *brb)
110 return brb->begin == brb->end;
113 static unsigned brb_next(struct bop_ring_buffer *brb, unsigned old)
115 unsigned r = old + 1;
116 return r >= ARRAY_SIZE(brb->bops) ? 0 : r;
119 static int brb_push(struct bop_ring_buffer *brb,
120 enum block_op_type type, dm_block_t b, dm_block_t e)
122 struct block_op *bop;
123 unsigned next = brb_next(brb, brb->end);
126 * We don't allow the last bop to be filled, this way we can
127 * differentiate between full and empty.
129 if (next == brb->begin)
132 bop = brb->bops + brb->end;
142 static int brb_peek(struct bop_ring_buffer *brb, struct block_op *result)
144 struct block_op *bop;
149 bop = brb->bops + brb->begin;
150 memcpy(result, bop, sizeof(*result));
154 static int brb_pop(struct bop_ring_buffer *brb)
159 brb->begin = brb_next(brb, brb->begin);
164 /*----------------------------------------------------------------*/
167 struct dm_space_map sm;
170 struct ll_disk old_ll;
174 unsigned recursion_count;
175 unsigned allocated_this_transaction;
176 struct bop_ring_buffer uncommitted;
178 struct threshold threshold;
181 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b, dm_block_t e)
183 int r = brb_push(&smm->uncommitted, type, b, e);
185 DMERR("too many recursive allocations");
192 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
195 int32_t nr_allocations;
199 r = sm_ll_inc(&smm->ll, op->b, op->e, &nr_allocations);
203 r = sm_ll_dec(&smm->ll, op->b, op->e, &nr_allocations);
210 static void in(struct sm_metadata *smm)
212 smm->recursion_count++;
215 static int apply_bops(struct sm_metadata *smm)
219 while (!brb_empty(&smm->uncommitted)) {
222 r = brb_peek(&smm->uncommitted, &bop);
224 DMERR("bug in bop ring buffer");
228 r = commit_bop(smm, &bop);
232 brb_pop(&smm->uncommitted);
238 static int out(struct sm_metadata *smm)
243 * If we're not recursing then very bad things are happening.
245 if (!smm->recursion_count) {
246 DMERR("lost track of recursion depth");
250 if (smm->recursion_count == 1)
253 smm->recursion_count--;
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
263 static int combine_errors(int r1, int r2)
268 static int recursing(struct sm_metadata *smm)
270 return smm->recursion_count;
273 static void sm_metadata_destroy(struct dm_space_map *sm)
275 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
280 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
282 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
284 *count = smm->ll.nr_blocks;
289 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
291 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
293 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
294 smm->allocated_this_transaction;
299 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
304 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
305 unsigned adjustment = 0;
308 * We may have some uncommitted adjustments to add. This list
309 * should always be really short.
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;
316 if (b < op->b || b >= op->e)
330 r = sm_ll_lookup(&smm->ll, b, result);
334 *result += adjustment;
339 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
340 dm_block_t b, int *result)
342 int r, adjustment = 0;
344 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
348 * We may have some uncommitted adjustments to add. This list
349 * should always be really short.
351 for (i = smm->uncommitted.begin;
352 i != smm->uncommitted.end;
353 i = brb_next(&smm->uncommitted, i)) {
355 struct block_op *op = smm->uncommitted.bops + i;
357 if (b < op->b || b >= op->e)
371 if (adjustment > 1) {
376 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
382 * We err on the side of caution, and always return true.
386 *result = rc + adjustment > 1;
391 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
395 int32_t nr_allocations;
396 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
398 if (smm->recursion_count) {
399 DMERR("cannot recurse set_count()");
404 r = sm_ll_insert(&smm->ll, b, count, &nr_allocations);
407 return combine_errors(r, r2);
410 static int sm_metadata_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
413 int32_t nr_allocations;
414 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
416 if (recursing(smm)) {
417 r = add_bop(smm, BOP_INC, b, e);
422 r = sm_ll_inc(&smm->ll, b, e, &nr_allocations);
426 return combine_errors(r, r2);
429 static int sm_metadata_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
432 int32_t nr_allocations;
433 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
436 r = add_bop(smm, BOP_DEC, b, e);
439 r = sm_ll_dec(&smm->ll, b, e, &nr_allocations);
443 return combine_errors(r, r2);
446 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
449 int32_t nr_allocations;
450 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
453 * Any block we allocate has to be free in both the old and current ll.
455 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
458 * There's no free block between smm->begin and the end of the metadata device.
459 * We search before smm->begin in case something has been freed.
461 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, 0, smm->begin, b);
470 r = add_bop(smm, BOP_INC, *b, *b + 1);
473 r = sm_ll_inc(&smm->ll, *b, *b + 1, &nr_allocations);
478 smm->allocated_this_transaction++;
480 return combine_errors(r, r2);
483 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
486 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
488 int r = sm_metadata_new_block_(sm, b);
490 DMERR_LIMIT("unable to allocate new metadata block");
494 r = sm_metadata_get_nr_free(sm, &count);
496 DMERR_LIMIT("couldn't get free block count");
500 check_threshold(&smm->threshold, count);
505 static int sm_metadata_commit(struct dm_space_map *sm)
508 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
510 r = sm_ll_commit(&smm->ll);
514 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
515 smm->allocated_this_transaction = 0;
520 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
521 dm_block_t threshold,
522 dm_sm_threshold_fn fn,
525 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
527 set_threshold(&smm->threshold, threshold, fn, context);
532 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
534 *result = sizeof(struct disk_sm_root);
539 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
541 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
542 struct disk_sm_root root_le;
544 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
545 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
546 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
547 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
549 if (max < sizeof(root_le))
552 memcpy(where_le, &root_le, sizeof(root_le));
557 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
559 static const struct dm_space_map ops = {
560 .destroy = sm_metadata_destroy,
561 .extend = sm_metadata_extend,
562 .get_nr_blocks = sm_metadata_get_nr_blocks,
563 .get_nr_free = sm_metadata_get_nr_free,
564 .get_count = sm_metadata_get_count,
565 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
566 .set_count = sm_metadata_set_count,
567 .inc_blocks = sm_metadata_inc_blocks,
568 .dec_blocks = sm_metadata_dec_blocks,
569 .new_block = sm_metadata_new_block,
570 .commit = sm_metadata_commit,
571 .root_size = sm_metadata_root_size,
572 .copy_root = sm_metadata_copy_root,
573 .register_threshold_callback = sm_metadata_register_threshold_callback
576 /*----------------------------------------------------------------*/
579 * When a new space map is created that manages its own space. We use
580 * this tiny bootstrap allocator.
582 static void sm_bootstrap_destroy(struct dm_space_map *sm)
586 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
588 DMERR("bootstrap doesn't support extend");
593 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
595 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
597 *count = smm->ll.nr_blocks;
602 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
604 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
606 *count = smm->ll.nr_blocks - smm->begin;
611 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
614 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
616 *result = (b < smm->begin) ? 1 : 0;
621 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
622 dm_block_t b, int *result)
629 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
632 DMERR("bootstrap doesn't support set_count");
637 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
639 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
642 * We know the entire device is unused.
644 if (smm->begin == smm->ll.nr_blocks)
652 static int sm_bootstrap_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
655 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
657 r = add_bop(smm, BOP_INC, b, e);
664 static int sm_bootstrap_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
667 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
669 r = add_bop(smm, BOP_DEC, b, e);
676 static int sm_bootstrap_commit(struct dm_space_map *sm)
681 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
683 DMERR("bootstrap doesn't support root_size");
688 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
691 DMERR("bootstrap doesn't support copy_root");
696 static const struct dm_space_map bootstrap_ops = {
697 .destroy = sm_bootstrap_destroy,
698 .extend = sm_bootstrap_extend,
699 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
700 .get_nr_free = sm_bootstrap_get_nr_free,
701 .get_count = sm_bootstrap_get_count,
702 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
703 .set_count = sm_bootstrap_set_count,
704 .inc_blocks = sm_bootstrap_inc_blocks,
705 .dec_blocks = sm_bootstrap_dec_blocks,
706 .new_block = sm_bootstrap_new_block,
707 .commit = sm_bootstrap_commit,
708 .root_size = sm_bootstrap_root_size,
709 .copy_root = sm_bootstrap_copy_root,
710 .register_threshold_callback = NULL
713 /*----------------------------------------------------------------*/
715 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
718 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
719 dm_block_t old_len = smm->ll.nr_blocks;
722 * Flick into a mode where all blocks get allocated in the new area.
724 smm->begin = old_len;
725 memcpy(sm, &bootstrap_ops, sizeof(*sm));
730 r = sm_ll_extend(&smm->ll, extra_blocks);
735 * We repeatedly increment then commit until the commit doesn't
736 * allocate any new blocks.
739 r = add_bop(smm, BOP_INC, old_len, smm->begin);
743 old_len = smm->begin;
747 DMERR("%s: apply_bops failed", __func__);
751 r = sm_ll_commit(&smm->ll);
755 } while (old_len != smm->begin);
759 * Switch back to normal behaviour.
761 memcpy(sm, &ops, sizeof(*sm));
765 /*----------------------------------------------------------------*/
767 struct dm_space_map *dm_sm_metadata_init(void)
769 struct sm_metadata *smm;
771 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
773 return ERR_PTR(-ENOMEM);
775 memcpy(&smm->sm, &ops, sizeof(smm->sm));
780 int dm_sm_metadata_create(struct dm_space_map *sm,
781 struct dm_transaction_manager *tm,
782 dm_block_t nr_blocks,
783 dm_block_t superblock)
786 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
788 smm->begin = superblock + 1;
789 smm->recursion_count = 0;
790 smm->allocated_this_transaction = 0;
791 brb_init(&smm->uncommitted);
792 threshold_init(&smm->threshold);
794 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
796 r = sm_ll_new_metadata(&smm->ll, tm);
798 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
799 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
800 r = sm_ll_extend(&smm->ll, nr_blocks);
802 memcpy(&smm->sm, &ops, sizeof(smm->sm));
807 * Now we need to update the newly created data structures with the
808 * allocated blocks that they were built from.
810 r = add_bop(smm, BOP_INC, superblock, smm->begin);
816 DMERR("%s: apply_bops failed", __func__);
820 return sm_metadata_commit(sm);
823 int dm_sm_metadata_open(struct dm_space_map *sm,
824 struct dm_transaction_manager *tm,
825 void *root_le, size_t len)
828 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
830 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
835 smm->recursion_count = 0;
836 smm->allocated_this_transaction = 0;
837 brb_init(&smm->uncommitted);
838 threshold_init(&smm->threshold);
840 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));