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>
15 #define DM_MSG_PREFIX "space map metadata"
17 /*----------------------------------------------------------------*/
20 * An edge triggered threshold.
26 dm_block_t current_value;
27 dm_sm_threshold_fn fn;
31 static void threshold_init(struct threshold *t)
33 t->threshold_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;
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))
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
90 enum block_op_type type;
95 struct dm_space_map sm;
98 struct ll_disk old_ll;
102 unsigned recursion_count;
103 unsigned allocated_this_transaction;
104 unsigned nr_uncommitted;
105 struct block_op uncommitted[MAX_RECURSIVE_ALLOCATIONS];
107 struct threshold threshold;
110 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b)
114 if (smm->nr_uncommitted == MAX_RECURSIVE_ALLOCATIONS) {
115 DMERR("too many recursive allocations");
119 op = smm->uncommitted + smm->nr_uncommitted++;
126 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
129 enum allocation_event ev;
133 r = sm_ll_inc(&smm->ll, op->block, &ev);
137 r = sm_ll_dec(&smm->ll, op->block, &ev);
144 static void in(struct sm_metadata *smm)
146 smm->recursion_count++;
149 static int out(struct sm_metadata *smm)
154 * If we're not recursing then very bad things are happening.
156 if (!smm->recursion_count) {
157 DMERR("lost track of recursion depth");
161 if (smm->recursion_count == 1 && smm->nr_uncommitted) {
162 while (smm->nr_uncommitted && !r) {
163 smm->nr_uncommitted--;
164 r = commit_bop(smm, smm->uncommitted +
165 smm->nr_uncommitted);
171 smm->recursion_count--;
177 * When using the out() function above, we often want to combine an error
178 * code for the operation run in the recursive context with that from
181 static int combine_errors(int r1, int r2)
186 static int recursing(struct sm_metadata *smm)
188 return smm->recursion_count;
191 static void sm_metadata_destroy(struct dm_space_map *sm)
193 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
198 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
200 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
202 *count = smm->ll.nr_blocks;
207 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
209 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
211 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
212 smm->allocated_this_transaction;
217 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
221 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
222 unsigned adjustment = 0;
225 * We may have some uncommitted adjustments to add. This list
226 * should always be really short.
228 for (i = 0; i < smm->nr_uncommitted; i++) {
229 struct block_op *op = smm->uncommitted + i;
245 r = sm_ll_lookup(&smm->ll, b, result);
249 *result += adjustment;
254 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
255 dm_block_t b, int *result)
257 int r, i, adjustment = 0;
258 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
262 * We may have some uncommitted adjustments to add. This list
263 * should always be really short.
265 for (i = 0; i < smm->nr_uncommitted; i++) {
266 struct block_op *op = smm->uncommitted + i;
282 if (adjustment > 1) {
287 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
293 * We err on the side of caution, and always return true.
297 *result = rc + adjustment > 1;
302 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
306 enum allocation_event ev;
307 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
309 if (smm->recursion_count) {
310 DMERR("cannot recurse set_count()");
315 r = sm_ll_insert(&smm->ll, b, count, &ev);
318 return combine_errors(r, r2);
321 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
324 enum allocation_event ev;
325 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
328 r = add_bop(smm, BOP_INC, b);
331 r = sm_ll_inc(&smm->ll, b, &ev);
335 return combine_errors(r, r2);
338 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
341 enum allocation_event ev;
342 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
345 r = add_bop(smm, BOP_DEC, b);
348 r = sm_ll_dec(&smm->ll, b, &ev);
352 return combine_errors(r, r2);
355 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
358 enum allocation_event ev;
359 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
361 r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
368 r = add_bop(smm, BOP_INC, *b);
371 r = sm_ll_inc(&smm->ll, *b, &ev);
376 smm->allocated_this_transaction++;
378 return combine_errors(r, r2);
381 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
384 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
386 int r = sm_metadata_new_block_(sm, b);
388 DMERR("unable to allocate new metadata block");
392 r = sm_metadata_get_nr_free(sm, &count);
394 DMERR("couldn't get free block count");
398 check_threshold(&smm->threshold, count);
403 static int sm_metadata_commit(struct dm_space_map *sm)
406 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
408 r = sm_ll_commit(&smm->ll);
412 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
414 smm->allocated_this_transaction = 0;
419 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
420 dm_block_t threshold,
421 dm_sm_threshold_fn fn,
424 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
426 set_threshold(&smm->threshold, threshold, fn, context);
431 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
433 *result = sizeof(struct disk_sm_root);
438 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
440 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
441 struct disk_sm_root root_le;
443 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
444 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
445 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
446 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
448 if (max < sizeof(root_le))
451 memcpy(where_le, &root_le, sizeof(root_le));
456 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
458 static struct dm_space_map ops = {
459 .destroy = sm_metadata_destroy,
460 .extend = sm_metadata_extend,
461 .get_nr_blocks = sm_metadata_get_nr_blocks,
462 .get_nr_free = sm_metadata_get_nr_free,
463 .get_count = sm_metadata_get_count,
464 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
465 .set_count = sm_metadata_set_count,
466 .inc_block = sm_metadata_inc_block,
467 .dec_block = sm_metadata_dec_block,
468 .new_block = sm_metadata_new_block,
469 .commit = sm_metadata_commit,
470 .root_size = sm_metadata_root_size,
471 .copy_root = sm_metadata_copy_root,
472 .register_threshold_callback = sm_metadata_register_threshold_callback
475 /*----------------------------------------------------------------*/
478 * When a new space map is created that manages its own space. We use
479 * this tiny bootstrap allocator.
481 static void sm_bootstrap_destroy(struct dm_space_map *sm)
485 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
487 DMERR("bootstrap doesn't support extend");
492 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
494 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
496 *count = smm->ll.nr_blocks;
501 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
503 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
505 *count = smm->ll.nr_blocks - smm->begin;
510 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
513 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
515 return b < smm->begin ? 1 : 0;
518 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
519 dm_block_t b, int *result)
526 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
529 DMERR("bootstrap doesn't support set_count");
534 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
536 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
539 * We know the entire device is unused.
541 if (smm->begin == smm->ll.nr_blocks)
549 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
551 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
553 return add_bop(smm, BOP_INC, b);
556 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
558 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
560 return add_bop(smm, BOP_DEC, b);
563 static int sm_bootstrap_commit(struct dm_space_map *sm)
568 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
570 DMERR("bootstrap doesn't support root_size");
575 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
578 DMERR("bootstrap doesn't support copy_root");
583 static struct dm_space_map bootstrap_ops = {
584 .destroy = sm_bootstrap_destroy,
585 .extend = sm_bootstrap_extend,
586 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
587 .get_nr_free = sm_bootstrap_get_nr_free,
588 .get_count = sm_bootstrap_get_count,
589 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
590 .set_count = sm_bootstrap_set_count,
591 .inc_block = sm_bootstrap_inc_block,
592 .dec_block = sm_bootstrap_dec_block,
593 .new_block = sm_bootstrap_new_block,
594 .commit = sm_bootstrap_commit,
595 .root_size = sm_bootstrap_root_size,
596 .copy_root = sm_bootstrap_copy_root,
597 .register_threshold_callback = NULL
600 /*----------------------------------------------------------------*/
602 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
605 enum allocation_event ev;
606 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
607 dm_block_t old_len = smm->ll.nr_blocks;
610 * Flick into a mode where all blocks get allocated in the new area.
612 smm->begin = old_len;
613 memcpy(sm, &bootstrap_ops, sizeof(*sm));
618 r = sm_ll_extend(&smm->ll, extra_blocks);
623 * We repeatedly increment then commit until the commit doesn't
624 * allocate any new blocks.
627 for (i = old_len; !r && i < smm->begin; i++) {
628 r = sm_ll_inc(&smm->ll, i, &ev);
632 old_len = smm->begin;
634 r = sm_ll_commit(&smm->ll);
638 } while (old_len != smm->begin);
642 * Switch back to normal behaviour.
644 memcpy(sm, &ops, sizeof(*sm));
648 /*----------------------------------------------------------------*/
650 struct dm_space_map *dm_sm_metadata_init(void)
652 struct sm_metadata *smm;
654 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
656 return ERR_PTR(-ENOMEM);
658 memcpy(&smm->sm, &ops, sizeof(smm->sm));
663 int dm_sm_metadata_create(struct dm_space_map *sm,
664 struct dm_transaction_manager *tm,
665 dm_block_t nr_blocks,
666 dm_block_t superblock)
670 enum allocation_event ev;
671 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
673 smm->begin = superblock + 1;
674 smm->recursion_count = 0;
675 smm->allocated_this_transaction = 0;
676 smm->nr_uncommitted = 0;
677 threshold_init(&smm->threshold);
679 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
681 r = sm_ll_new_metadata(&smm->ll, tm);
685 r = sm_ll_extend(&smm->ll, nr_blocks);
689 memcpy(&smm->sm, &ops, sizeof(smm->sm));
692 * Now we need to update the newly created data structures with the
693 * allocated blocks that they were built from.
695 for (i = superblock; !r && i < smm->begin; i++)
696 r = sm_ll_inc(&smm->ll, i, &ev);
701 return sm_metadata_commit(sm);
704 int dm_sm_metadata_open(struct dm_space_map *sm,
705 struct dm_transaction_manager *tm,
706 void *root_le, size_t len)
709 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
711 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
716 smm->recursion_count = 0;
717 smm->allocated_this_transaction = 0;
718 smm->nr_uncommitted = 0;
719 threshold_init(&smm->threshold);
721 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));