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;
94 struct bop_ring_buffer {
97 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
100 static void brb_init(struct bop_ring_buffer *brb)
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)
130 bop = brb->bops + brb->end;
139 static int brb_pop(struct bop_ring_buffer *brb, struct block_op *result)
141 struct block_op *bop;
146 bop = brb->bops + brb->begin;
147 result->type = bop->type;
148 result->block = bop->block;
150 brb->begin = brb_next(brb, brb->begin);
155 /*----------------------------------------------------------------*/
158 struct dm_space_map sm;
161 struct ll_disk old_ll;
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);
177 DMERR("too many recursive allocations");
184 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
187 enum allocation_event ev;
191 r = sm_ll_inc(&smm->ll, op->block, &ev);
195 r = sm_ll_dec(&smm->ll, op->block, &ev);
202 static void in(struct sm_metadata *smm)
204 smm->recursion_count++;
207 static int out(struct sm_metadata *smm)
212 * If we're not recursing then very bad things are happening.
214 if (!smm->recursion_count) {
215 DMERR("lost track of recursion depth");
219 if (smm->recursion_count == 1) {
220 while (!brb_empty(&smm->uncommitted)) {
223 r = brb_pop(&smm->uncommitted, &bop);
225 DMERR("bug in bop ring buffer");
229 r = commit_bop(smm, &bop);
235 smm->recursion_count--;
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
245 static int combine_errors(int r1, int 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);
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;
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;
281 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
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;
312 r = sm_ll_lookup(&smm->ll, b, result);
316 *result += adjustment;
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;
326 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
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;
353 if (adjustment > 1) {
358 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
364 * We err on the side of caution, and always return true.
368 *result = rc + adjustment > 1;
373 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
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()");
386 r = sm_ll_insert(&smm->ll, b, count, &ev);
389 return combine_errors(r, r2);
392 static int sm_metadata_inc_block(struct dm_space_map *sm, dm_block_t b)
395 enum allocation_event ev;
396 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
399 r = add_bop(smm, BOP_INC, b);
402 r = sm_ll_inc(&smm->ll, b, &ev);
406 return combine_errors(r, r2);
409 static int sm_metadata_dec_block(struct dm_space_map *sm, dm_block_t b)
412 enum allocation_event ev;
413 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
416 r = add_bop(smm, BOP_DEC, b);
419 r = sm_ll_dec(&smm->ll, b, &ev);
423 return combine_errors(r, r2);
426 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
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);
439 r = add_bop(smm, BOP_INC, *b);
442 r = sm_ll_inc(&smm->ll, *b, &ev);
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)
455 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
457 int r = sm_metadata_new_block_(sm, b);
459 DMERR_LIMIT("unable to allocate new metadata block");
463 r = sm_metadata_get_nr_free(sm, &count);
465 DMERR_LIMIT("couldn't get free block count");
469 check_threshold(&smm->threshold, count);
474 static int sm_metadata_commit(struct dm_space_map *sm)
477 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
479 r = sm_ll_commit(&smm->ll);
483 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
485 smm->allocated_this_transaction = 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,
495 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
497 set_threshold(&smm->threshold, threshold, fn, context);
502 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
504 *result = sizeof(struct disk_sm_root);
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))
522 memcpy(where_le, &root_le, sizeof(root_le));
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");
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 return smm->ll.nr_blocks;
570 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
572 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
574 *count = smm->ll.nr_blocks - smm->begin;
579 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
582 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
584 return b < smm->begin ? 1 : 0;
587 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
588 dm_block_t b, int *result)
595 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
598 DMERR("bootstrap doesn't support set_count");
603 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
605 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
608 * We know the entire device is unused.
610 if (smm->begin == smm->ll.nr_blocks)
618 static int sm_bootstrap_inc_block(struct dm_space_map *sm, dm_block_t b)
620 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
622 return add_bop(smm, BOP_INC, b);
625 static int sm_bootstrap_dec_block(struct dm_space_map *sm, dm_block_t b)
627 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
629 return add_bop(smm, BOP_DEC, b);
632 static int sm_bootstrap_commit(struct dm_space_map *sm)
637 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
639 DMERR("bootstrap doesn't support root_size");
644 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
647 DMERR("bootstrap doesn't support copy_root");
652 static struct dm_space_map bootstrap_ops = {
653 .destroy = sm_bootstrap_destroy,
654 .extend = sm_bootstrap_extend,
655 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
656 .get_nr_free = sm_bootstrap_get_nr_free,
657 .get_count = sm_bootstrap_get_count,
658 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
659 .set_count = sm_bootstrap_set_count,
660 .inc_block = sm_bootstrap_inc_block,
661 .dec_block = sm_bootstrap_dec_block,
662 .new_block = sm_bootstrap_new_block,
663 .commit = sm_bootstrap_commit,
664 .root_size = sm_bootstrap_root_size,
665 .copy_root = sm_bootstrap_copy_root,
666 .register_threshold_callback = NULL
669 /*----------------------------------------------------------------*/
671 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
674 enum allocation_event ev;
675 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
676 dm_block_t old_len = smm->ll.nr_blocks;
679 * Flick into a mode where all blocks get allocated in the new area.
681 smm->begin = old_len;
682 memcpy(sm, &bootstrap_ops, sizeof(*sm));
687 r = sm_ll_extend(&smm->ll, extra_blocks);
692 * We repeatedly increment then commit until the commit doesn't
693 * allocate any new blocks.
696 for (i = old_len; !r && i < smm->begin; i++) {
697 r = sm_ll_inc(&smm->ll, i, &ev);
701 old_len = smm->begin;
703 r = sm_ll_commit(&smm->ll);
707 } while (old_len != smm->begin);
711 * Switch back to normal behaviour.
713 memcpy(sm, &ops, sizeof(*sm));
717 /*----------------------------------------------------------------*/
719 struct dm_space_map *dm_sm_metadata_init(void)
721 struct sm_metadata *smm;
723 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
725 return ERR_PTR(-ENOMEM);
727 memcpy(&smm->sm, &ops, sizeof(smm->sm));
732 int dm_sm_metadata_create(struct dm_space_map *sm,
733 struct dm_transaction_manager *tm,
734 dm_block_t nr_blocks,
735 dm_block_t superblock)
739 enum allocation_event ev;
740 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
742 smm->begin = superblock + 1;
743 smm->recursion_count = 0;
744 smm->allocated_this_transaction = 0;
745 brb_init(&smm->uncommitted);
746 threshold_init(&smm->threshold);
748 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
750 r = sm_ll_new_metadata(&smm->ll, tm);
754 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
755 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
756 r = sm_ll_extend(&smm->ll, nr_blocks);
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);
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)
780 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
782 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
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));