1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This file is released under the GPL.
8 #include "dm-space-map.h"
9 #include "dm-space-map-common.h"
10 #include "dm-space-map-metadata.h"
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 #include <linux/device-mapper.h>
15 #include <linux/kernel.h>
17 #define DM_MSG_PREFIX "space map metadata"
19 /*----------------------------------------------------------------*/
22 * An edge triggered threshold.
28 dm_block_t current_value;
29 dm_sm_threshold_fn fn;
33 static void threshold_init(struct threshold *t)
35 t->threshold_set = false;
39 static void set_threshold(struct threshold *t, dm_block_t value,
40 dm_sm_threshold_fn fn, void *context)
42 t->threshold_set = true;
48 static bool below_threshold(struct threshold *t, dm_block_t value)
50 return t->threshold_set && value <= t->threshold;
53 static bool threshold_already_triggered(struct threshold *t)
55 return t->value_set && below_threshold(t, t->current_value);
58 static void check_threshold(struct threshold *t, dm_block_t value)
60 if (below_threshold(t, value) &&
61 !threshold_already_triggered(t))
65 t->current_value = value;
68 /*----------------------------------------------------------------*/
71 * Space map interface.
73 * The low level disk format is written using the standard btree and
74 * transaction manager. This means that performing disk operations may
75 * cause us to recurse into the space map in order to allocate new blocks.
76 * For this reason we have a pool of pre-allocated blocks large enough to
77 * service any metadata_ll_disk operation.
81 * FIXME: we should calculate this based on the size of the device.
82 * Only the metadata space map needs this functionality.
84 #define MAX_RECURSIVE_ALLOCATIONS 1024
92 enum block_op_type type;
97 struct bop_ring_buffer {
100 struct block_op bops[MAX_RECURSIVE_ALLOCATIONS + 1];
103 static void brb_init(struct bop_ring_buffer *brb)
109 static bool brb_empty(struct bop_ring_buffer *brb)
111 return brb->begin == brb->end;
114 static unsigned int brb_next(struct bop_ring_buffer *brb, unsigned int old)
116 unsigned int r = old + 1;
118 return r >= ARRAY_SIZE(brb->bops) ? 0 : r;
121 static int brb_push(struct bop_ring_buffer *brb,
122 enum block_op_type type, dm_block_t b, dm_block_t e)
124 struct block_op *bop;
125 unsigned int next = brb_next(brb, brb->end);
128 * We don't allow the last bop to be filled, this way we can
129 * differentiate between full and empty.
131 if (next == brb->begin)
134 bop = brb->bops + brb->end;
144 static int brb_peek(struct bop_ring_buffer *brb, struct block_op *result)
146 struct block_op *bop;
151 bop = brb->bops + brb->begin;
152 memcpy(result, bop, sizeof(*result));
156 static int brb_pop(struct bop_ring_buffer *brb)
161 brb->begin = brb_next(brb, brb->begin);
166 /*----------------------------------------------------------------*/
169 struct dm_space_map sm;
172 struct ll_disk old_ll;
176 unsigned int recursion_count;
177 unsigned int allocated_this_transaction;
178 struct bop_ring_buffer uncommitted;
180 struct threshold threshold;
183 static int add_bop(struct sm_metadata *smm, enum block_op_type type, dm_block_t b, dm_block_t e)
185 int r = brb_push(&smm->uncommitted, type, b, e);
188 DMERR("too many recursive allocations");
195 static int commit_bop(struct sm_metadata *smm, struct block_op *op)
198 int32_t nr_allocations;
202 r = sm_ll_inc(&smm->ll, op->b, op->e, &nr_allocations);
206 r = sm_ll_dec(&smm->ll, op->b, op->e, &nr_allocations);
213 static void in(struct sm_metadata *smm)
215 smm->recursion_count++;
218 static int apply_bops(struct sm_metadata *smm)
222 while (!brb_empty(&smm->uncommitted)) {
225 r = brb_peek(&smm->uncommitted, &bop);
227 DMERR("bug in bop ring buffer");
231 r = commit_bop(smm, &bop);
235 brb_pop(&smm->uncommitted);
241 static int out(struct sm_metadata *smm)
246 * If we're not recursing then very bad things are happening.
248 if (!smm->recursion_count) {
249 DMERR("lost track of recursion depth");
253 if (smm->recursion_count == 1)
256 smm->recursion_count--;
262 * When using the out() function above, we often want to combine an error
263 * code for the operation run in the recursive context with that from
266 static int combine_errors(int r1, int r2)
271 static int recursing(struct sm_metadata *smm)
273 return smm->recursion_count;
276 static void sm_metadata_destroy(struct dm_space_map *sm)
278 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
283 static int sm_metadata_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
285 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
287 *count = smm->ll.nr_blocks;
292 static int sm_metadata_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
294 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
296 *count = smm->old_ll.nr_blocks - smm->old_ll.nr_allocated -
297 smm->allocated_this_transaction;
302 static int sm_metadata_get_count(struct dm_space_map *sm, dm_block_t b,
307 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
308 unsigned int adjustment = 0;
311 * We may have some uncommitted adjustments to add. This list
312 * should always be really short.
314 for (i = smm->uncommitted.begin;
315 i != smm->uncommitted.end;
316 i = brb_next(&smm->uncommitted, i)) {
317 struct block_op *op = smm->uncommitted.bops + i;
319 if (b < op->b || b >= op->e)
333 r = sm_ll_lookup(&smm->ll, b, result);
337 *result += adjustment;
342 static int sm_metadata_count_is_more_than_one(struct dm_space_map *sm,
343 dm_block_t b, int *result)
345 int r, adjustment = 0;
347 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
351 * We may have some uncommitted adjustments to add. This list
352 * should always be really short.
354 for (i = smm->uncommitted.begin;
355 i != smm->uncommitted.end;
356 i = brb_next(&smm->uncommitted, i)) {
358 struct block_op *op = smm->uncommitted.bops + i;
360 if (b < op->b || b >= op->e)
374 if (adjustment > 1) {
379 r = sm_ll_lookup_bitmap(&smm->ll, b, &rc);
385 * We err on the side of caution, and always return true.
389 *result = rc + adjustment > 1;
394 static int sm_metadata_set_count(struct dm_space_map *sm, dm_block_t b,
398 int32_t nr_allocations;
399 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
401 if (smm->recursion_count) {
402 DMERR("cannot recurse set_count()");
407 r = sm_ll_insert(&smm->ll, b, count, &nr_allocations);
410 return combine_errors(r, r2);
413 static int sm_metadata_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
416 int32_t nr_allocations;
417 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
419 if (recursing(smm)) {
420 r = add_bop(smm, BOP_INC, b, e);
425 r = sm_ll_inc(&smm->ll, b, e, &nr_allocations);
429 return combine_errors(r, r2);
432 static int sm_metadata_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
435 int32_t nr_allocations;
436 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
439 r = add_bop(smm, BOP_DEC, b, e);
442 r = sm_ll_dec(&smm->ll, b, e, &nr_allocations);
446 return combine_errors(r, r2);
449 static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
452 int32_t nr_allocations;
453 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
456 * Any block we allocate has to be free in both the old and current ll.
458 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
461 * There's no free block between smm->begin and the end of the metadata device.
462 * We search before smm->begin in case something has been freed.
464 r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, 0, smm->begin, b);
473 r = add_bop(smm, BOP_INC, *b, *b + 1);
476 r = sm_ll_inc(&smm->ll, *b, *b + 1, &nr_allocations);
481 smm->allocated_this_transaction++;
483 return combine_errors(r, r2);
486 static int sm_metadata_new_block(struct dm_space_map *sm, dm_block_t *b)
489 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
491 int r = sm_metadata_new_block_(sm, b);
494 DMERR_LIMIT("unable to allocate new metadata block");
498 r = sm_metadata_get_nr_free(sm, &count);
500 DMERR_LIMIT("couldn't get free block count");
504 check_threshold(&smm->threshold, count);
509 static int sm_metadata_commit(struct dm_space_map *sm)
512 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
514 r = sm_ll_commit(&smm->ll);
518 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));
519 smm->allocated_this_transaction = 0;
524 static int sm_metadata_register_threshold_callback(struct dm_space_map *sm,
525 dm_block_t threshold,
526 dm_sm_threshold_fn fn,
529 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
531 set_threshold(&smm->threshold, threshold, fn, context);
536 static int sm_metadata_root_size(struct dm_space_map *sm, size_t *result)
538 *result = sizeof(struct disk_sm_root);
543 static int sm_metadata_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
545 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
546 struct disk_sm_root root_le;
548 root_le.nr_blocks = cpu_to_le64(smm->ll.nr_blocks);
549 root_le.nr_allocated = cpu_to_le64(smm->ll.nr_allocated);
550 root_le.bitmap_root = cpu_to_le64(smm->ll.bitmap_root);
551 root_le.ref_count_root = cpu_to_le64(smm->ll.ref_count_root);
553 if (max < sizeof(root_le))
556 memcpy(where_le, &root_le, sizeof(root_le));
561 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks);
563 static const struct dm_space_map ops = {
564 .destroy = sm_metadata_destroy,
565 .extend = sm_metadata_extend,
566 .get_nr_blocks = sm_metadata_get_nr_blocks,
567 .get_nr_free = sm_metadata_get_nr_free,
568 .get_count = sm_metadata_get_count,
569 .count_is_more_than_one = sm_metadata_count_is_more_than_one,
570 .set_count = sm_metadata_set_count,
571 .inc_blocks = sm_metadata_inc_blocks,
572 .dec_blocks = sm_metadata_dec_blocks,
573 .new_block = sm_metadata_new_block,
574 .commit = sm_metadata_commit,
575 .root_size = sm_metadata_root_size,
576 .copy_root = sm_metadata_copy_root,
577 .register_threshold_callback = sm_metadata_register_threshold_callback
580 /*----------------------------------------------------------------*/
583 * When a new space map is created that manages its own space. We use
584 * this tiny bootstrap allocator.
586 static void sm_bootstrap_destroy(struct dm_space_map *sm)
590 static int sm_bootstrap_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
592 DMERR("bootstrap doesn't support extend");
597 static int sm_bootstrap_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
599 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
601 *count = smm->ll.nr_blocks;
606 static int sm_bootstrap_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
608 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
610 *count = smm->ll.nr_blocks - smm->begin;
615 static int sm_bootstrap_get_count(struct dm_space_map *sm, dm_block_t b,
618 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
620 *result = (b < smm->begin) ? 1 : 0;
625 static int sm_bootstrap_count_is_more_than_one(struct dm_space_map *sm,
626 dm_block_t b, int *result)
633 static int sm_bootstrap_set_count(struct dm_space_map *sm, dm_block_t b,
636 DMERR("bootstrap doesn't support set_count");
641 static int sm_bootstrap_new_block(struct dm_space_map *sm, dm_block_t *b)
643 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
646 * We know the entire device is unused.
648 if (smm->begin == smm->ll.nr_blocks)
656 static int sm_bootstrap_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
659 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
661 r = add_bop(smm, BOP_INC, b, e);
668 static int sm_bootstrap_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
671 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
673 r = add_bop(smm, BOP_DEC, b, e);
680 static int sm_bootstrap_commit(struct dm_space_map *sm)
685 static int sm_bootstrap_root_size(struct dm_space_map *sm, size_t *result)
687 DMERR("bootstrap doesn't support root_size");
692 static int sm_bootstrap_copy_root(struct dm_space_map *sm, void *where,
695 DMERR("bootstrap doesn't support copy_root");
700 static const struct dm_space_map bootstrap_ops = {
701 .destroy = sm_bootstrap_destroy,
702 .extend = sm_bootstrap_extend,
703 .get_nr_blocks = sm_bootstrap_get_nr_blocks,
704 .get_nr_free = sm_bootstrap_get_nr_free,
705 .get_count = sm_bootstrap_get_count,
706 .count_is_more_than_one = sm_bootstrap_count_is_more_than_one,
707 .set_count = sm_bootstrap_set_count,
708 .inc_blocks = sm_bootstrap_inc_blocks,
709 .dec_blocks = sm_bootstrap_dec_blocks,
710 .new_block = sm_bootstrap_new_block,
711 .commit = sm_bootstrap_commit,
712 .root_size = sm_bootstrap_root_size,
713 .copy_root = sm_bootstrap_copy_root,
714 .register_threshold_callback = NULL
717 /*----------------------------------------------------------------*/
719 static int sm_metadata_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
722 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
723 dm_block_t old_len = smm->ll.nr_blocks;
726 * Flick into a mode where all blocks get allocated in the new area.
728 smm->begin = old_len;
729 memcpy(sm, &bootstrap_ops, sizeof(*sm));
734 r = sm_ll_extend(&smm->ll, extra_blocks);
739 * We repeatedly increment then commit until the commit doesn't
740 * allocate any new blocks.
743 r = add_bop(smm, BOP_INC, old_len, smm->begin);
747 old_len = smm->begin;
751 DMERR("%s: apply_bops failed", __func__);
755 r = sm_ll_commit(&smm->ll);
759 } while (old_len != smm->begin);
763 * Switch back to normal behaviour.
765 memcpy(sm, &ops, sizeof(*sm));
769 /*----------------------------------------------------------------*/
771 struct dm_space_map *dm_sm_metadata_init(void)
773 struct sm_metadata *smm;
775 smm = kmalloc(sizeof(*smm), GFP_KERNEL);
777 return ERR_PTR(-ENOMEM);
779 memcpy(&smm->sm, &ops, sizeof(smm->sm));
784 int dm_sm_metadata_create(struct dm_space_map *sm,
785 struct dm_transaction_manager *tm,
786 dm_block_t nr_blocks,
787 dm_block_t superblock)
790 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
792 smm->begin = superblock + 1;
793 smm->recursion_count = 0;
794 smm->allocated_this_transaction = 0;
795 brb_init(&smm->uncommitted);
796 threshold_init(&smm->threshold);
798 memcpy(&smm->sm, &bootstrap_ops, sizeof(smm->sm));
800 r = sm_ll_new_metadata(&smm->ll, tm);
802 if (nr_blocks > DM_SM_METADATA_MAX_BLOCKS)
803 nr_blocks = DM_SM_METADATA_MAX_BLOCKS;
804 r = sm_ll_extend(&smm->ll, nr_blocks);
806 memcpy(&smm->sm, &ops, sizeof(smm->sm));
811 * Now we need to update the newly created data structures with the
812 * allocated blocks that they were built from.
814 r = add_bop(smm, BOP_INC, superblock, smm->begin);
820 DMERR("%s: apply_bops failed", __func__);
824 return sm_metadata_commit(sm);
827 int dm_sm_metadata_open(struct dm_space_map *sm,
828 struct dm_transaction_manager *tm,
829 void *root_le, size_t len)
832 struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
834 r = sm_ll_open_metadata(&smm->ll, tm, root_le, len);
839 smm->recursion_count = 0;
840 smm->allocated_this_transaction = 0;
841 brb_init(&smm->uncommitted);
842 threshold_init(&smm->threshold);
844 memcpy(&smm->old_ll, &smm->ll, sizeof(smm->old_ll));