1 // SPDX-License-Identifier: GPL-2.0-only
3 #include "persistent-data/dm-transaction-manager.h"
4 #include "persistent-data/dm-bitset.h"
5 #include "persistent-data/dm-space-map.h"
7 #include <linux/dm-io.h>
8 #include <linux/dm-kcopyd.h>
9 #include <linux/init.h>
10 #include <linux/mempool.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
15 #define DM_MSG_PREFIX "era"
17 #define SUPERBLOCK_LOCATION 0
18 #define SUPERBLOCK_MAGIC 2126579579
19 #define SUPERBLOCK_CSUM_XOR 146538381
20 #define MIN_ERA_VERSION 1
21 #define MAX_ERA_VERSION 1
22 #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
23 #define MIN_BLOCK_SIZE 8
25 /*----------------------------------------------------------------
27 *--------------------------------------------------------------*/
28 struct writeset_metadata {
34 struct writeset_metadata md;
37 * An in core copy of the bits to save constantly doing look ups on
44 * This does not free off the on disk bitset as this will normally be done
45 * after digesting into the era array.
47 static void writeset_free(struct writeset *ws)
53 static int setup_on_disk_bitset(struct dm_disk_bitset *info,
54 unsigned nr_bits, dm_block_t *root)
58 r = dm_bitset_empty(info, root);
62 return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
65 static size_t bitset_size(unsigned nr_bits)
67 return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
71 * Allocates memory for the in core bitset.
73 static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
75 ws->bits = vzalloc(bitset_size(nr_blocks));
77 DMERR("%s: couldn't allocate in memory bitset", __func__);
85 * Wipes the in-core bitset, and creates a new on disk bitset.
87 static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws,
92 memset(ws->bits, 0, bitset_size(nr_blocks));
94 ws->md.nr_bits = nr_blocks;
95 r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
97 DMERR("%s: setup_on_disk_bitset failed", __func__);
104 static bool writeset_marked(struct writeset *ws, dm_block_t block)
106 return test_bit(block, ws->bits);
109 static int writeset_marked_on_disk(struct dm_disk_bitset *info,
110 struct writeset_metadata *m, dm_block_t block,
113 dm_block_t old = m->root;
116 * The bitset was flushed when it was archived, so we know there'll
117 * be no change to the root.
119 int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
121 DMERR("%s: dm_bitset_test_bit failed", __func__);
125 BUG_ON(m->root != old);
131 * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
133 static int writeset_test_and_set(struct dm_disk_bitset *info,
134 struct writeset *ws, uint32_t block)
138 if (!test_bit(block, ws->bits)) {
139 r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
141 /* FIXME: fail mode */
151 /*----------------------------------------------------------------
152 * On disk metadata layout
153 *--------------------------------------------------------------*/
154 #define SPACE_MAP_ROOT_SIZE 128
157 struct writeset_disk {
162 struct superblock_disk {
171 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
173 __le32 data_block_size;
174 __le32 metadata_block_size;
178 struct writeset_disk current_writeset;
181 * Only these two fields are valid within the metadata snapshot.
183 __le64 writeset_tree_root;
184 __le64 era_array_root;
186 __le64 metadata_snap;
189 /*----------------------------------------------------------------
190 * Superblock validation
191 *--------------------------------------------------------------*/
192 static void sb_prepare_for_write(struct dm_block_validator *v,
194 size_t sb_block_size)
196 struct superblock_disk *disk = dm_block_data(b);
198 disk->blocknr = cpu_to_le64(dm_block_location(b));
199 disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
200 sb_block_size - sizeof(__le32),
201 SUPERBLOCK_CSUM_XOR));
204 static int check_metadata_version(struct superblock_disk *disk)
206 uint32_t metadata_version = le32_to_cpu(disk->version);
207 if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
208 DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
209 metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
216 static int sb_check(struct dm_block_validator *v,
218 size_t sb_block_size)
220 struct superblock_disk *disk = dm_block_data(b);
223 if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
224 DMERR("sb_check failed: blocknr %llu: wanted %llu",
225 le64_to_cpu(disk->blocknr),
226 (unsigned long long)dm_block_location(b));
230 if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
231 DMERR("sb_check failed: magic %llu: wanted %llu",
232 le64_to_cpu(disk->magic),
233 (unsigned long long) SUPERBLOCK_MAGIC);
237 csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
238 sb_block_size - sizeof(__le32),
239 SUPERBLOCK_CSUM_XOR));
240 if (csum_le != disk->csum) {
241 DMERR("sb_check failed: csum %u: wanted %u",
242 le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
246 return check_metadata_version(disk);
249 static struct dm_block_validator sb_validator = {
250 .name = "superblock",
251 .prepare_for_write = sb_prepare_for_write,
255 /*----------------------------------------------------------------
256 * Low level metadata handling
257 *--------------------------------------------------------------*/
258 #define DM_ERA_METADATA_BLOCK_SIZE 4096
259 #define ERA_MAX_CONCURRENT_LOCKS 5
261 struct era_metadata {
262 struct block_device *bdev;
263 struct dm_block_manager *bm;
264 struct dm_space_map *sm;
265 struct dm_transaction_manager *tm;
267 dm_block_t block_size;
270 uint32_t current_era;
273 * We preallocate 2 writesets. When an era rolls over we
274 * switch between them. This means the allocation is done at
275 * preresume time, rather than on the io path.
277 struct writeset writesets[2];
278 struct writeset *current_writeset;
280 dm_block_t writeset_tree_root;
281 dm_block_t era_array_root;
283 struct dm_disk_bitset bitset_info;
284 struct dm_btree_info writeset_tree_info;
285 struct dm_array_info era_array_info;
287 dm_block_t metadata_snap;
290 * A flag that is set whenever a writeset has been archived.
292 bool archived_writesets;
295 * Reading the space map root can fail, so we read it into this
296 * buffer before the superblock is locked and updated.
298 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
301 static int superblock_read_lock(struct era_metadata *md,
302 struct dm_block **sblock)
304 return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
305 &sb_validator, sblock);
308 static int superblock_lock_zero(struct era_metadata *md,
309 struct dm_block **sblock)
311 return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
312 &sb_validator, sblock);
315 static int superblock_lock(struct era_metadata *md,
316 struct dm_block **sblock)
318 return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
319 &sb_validator, sblock);
322 /* FIXME: duplication with cache and thin */
323 static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
328 __le64 *data_le, zero = cpu_to_le64(0);
329 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
332 * We can't use a validator here - it may be all zeroes.
334 r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
338 data_le = dm_block_data(b);
340 for (i = 0; i < sb_block_size; i++) {
341 if (data_le[i] != zero) {
352 /*----------------------------------------------------------------*/
354 static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
356 disk->nr_bits = cpu_to_le32(core->nr_bits);
357 disk->root = cpu_to_le64(core->root);
360 static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
362 core->nr_bits = le32_to_cpu(disk->nr_bits);
363 core->root = le64_to_cpu(disk->root);
366 static void ws_inc(void *context, const void *value, unsigned count)
368 struct era_metadata *md = context;
369 struct writeset_disk ws_d;
373 for (i = 0; i < count; i++) {
374 memcpy(&ws_d, value + (i * sizeof(ws_d)), sizeof(ws_d));
375 b = le64_to_cpu(ws_d.root);
376 dm_tm_inc(md->tm, b);
380 static void ws_dec(void *context, const void *value, unsigned count)
382 struct era_metadata *md = context;
383 struct writeset_disk ws_d;
387 for (i = 0; i < count; i++) {
388 memcpy(&ws_d, value + (i * sizeof(ws_d)), sizeof(ws_d));
389 b = le64_to_cpu(ws_d.root);
390 dm_bitset_del(&md->bitset_info, b);
394 static int ws_eq(void *context, const void *value1, const void *value2)
396 return !memcmp(value1, value2, sizeof(struct writeset_disk));
399 /*----------------------------------------------------------------*/
401 static void setup_writeset_tree_info(struct era_metadata *md)
403 struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
404 md->writeset_tree_info.tm = md->tm;
405 md->writeset_tree_info.levels = 1;
407 vt->size = sizeof(struct writeset_disk);
413 static void setup_era_array_info(struct era_metadata *md)
416 struct dm_btree_value_type vt;
418 vt.size = sizeof(__le32);
423 dm_array_info_init(&md->era_array_info, md->tm, &vt);
426 static void setup_infos(struct era_metadata *md)
428 dm_disk_bitset_init(md->tm, &md->bitset_info);
429 setup_writeset_tree_info(md);
430 setup_era_array_info(md);
433 /*----------------------------------------------------------------*/
435 static int create_fresh_metadata(struct era_metadata *md)
439 r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
442 DMERR("dm_tm_create_with_sm failed");
448 r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
450 DMERR("couldn't create new writeset tree");
454 r = dm_array_empty(&md->era_array_info, &md->era_array_root);
456 DMERR("couldn't create era array");
463 dm_sm_destroy(md->sm);
464 dm_tm_destroy(md->tm);
469 static int save_sm_root(struct era_metadata *md)
474 r = dm_sm_root_size(md->sm, &metadata_len);
478 return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
482 static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
484 memcpy(&disk->metadata_space_map_root,
485 &md->metadata_space_map_root,
486 sizeof(md->metadata_space_map_root));
490 * Writes a superblock, including the static fields that don't get updated
491 * with every commit (possible optimisation here). 'md' should be fully
492 * constructed when this is called.
494 static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
496 disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
497 disk->flags = cpu_to_le32(0ul);
499 /* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
500 memset(disk->uuid, 0, sizeof(disk->uuid));
501 disk->version = cpu_to_le32(MAX_ERA_VERSION);
503 copy_sm_root(md, disk);
505 disk->data_block_size = cpu_to_le32(md->block_size);
506 disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
507 disk->nr_blocks = cpu_to_le32(md->nr_blocks);
508 disk->current_era = cpu_to_le32(md->current_era);
510 ws_pack(&md->current_writeset->md, &disk->current_writeset);
511 disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
512 disk->era_array_root = cpu_to_le64(md->era_array_root);
513 disk->metadata_snap = cpu_to_le64(md->metadata_snap);
516 static int write_superblock(struct era_metadata *md)
519 struct dm_block *sblock;
520 struct superblock_disk *disk;
522 r = save_sm_root(md);
524 DMERR("%s: save_sm_root failed", __func__);
528 r = superblock_lock_zero(md, &sblock);
532 disk = dm_block_data(sblock);
533 prepare_superblock(md, disk);
535 return dm_tm_commit(md->tm, sblock);
539 * Assumes block_size and the infos are set.
541 static int format_metadata(struct era_metadata *md)
545 r = create_fresh_metadata(md);
549 r = write_superblock(md);
551 dm_sm_destroy(md->sm);
552 dm_tm_destroy(md->tm);
559 static int open_metadata(struct era_metadata *md)
562 struct dm_block *sblock;
563 struct superblock_disk *disk;
565 r = superblock_read_lock(md, &sblock);
567 DMERR("couldn't read_lock superblock");
571 disk = dm_block_data(sblock);
573 /* Verify the data block size hasn't changed */
574 if (le32_to_cpu(disk->data_block_size) != md->block_size) {
575 DMERR("changing the data block size (from %u to %llu) is not supported",
576 le32_to_cpu(disk->data_block_size), md->block_size);
581 r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
582 disk->metadata_space_map_root,
583 sizeof(disk->metadata_space_map_root),
586 DMERR("dm_tm_open_with_sm failed");
592 md->nr_blocks = le32_to_cpu(disk->nr_blocks);
593 md->current_era = le32_to_cpu(disk->current_era);
595 ws_unpack(&disk->current_writeset, &md->current_writeset->md);
596 md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
597 md->era_array_root = le64_to_cpu(disk->era_array_root);
598 md->metadata_snap = le64_to_cpu(disk->metadata_snap);
599 md->archived_writesets = true;
601 dm_bm_unlock(sblock);
606 dm_bm_unlock(sblock);
610 static int open_or_format_metadata(struct era_metadata *md,
614 bool unformatted = false;
616 r = superblock_all_zeroes(md->bm, &unformatted);
621 return may_format ? format_metadata(md) : -EPERM;
623 return open_metadata(md);
626 static int create_persistent_data_objects(struct era_metadata *md,
631 md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
632 ERA_MAX_CONCURRENT_LOCKS);
633 if (IS_ERR(md->bm)) {
634 DMERR("could not create block manager");
635 return PTR_ERR(md->bm);
638 r = open_or_format_metadata(md, may_format);
640 dm_block_manager_destroy(md->bm);
645 static void destroy_persistent_data_objects(struct era_metadata *md)
647 dm_sm_destroy(md->sm);
648 dm_tm_destroy(md->tm);
649 dm_block_manager_destroy(md->bm);
653 * This waits until all era_map threads have picked up the new filter.
655 static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
657 rcu_assign_pointer(md->current_writeset, new_writeset);
661 /*----------------------------------------------------------------
662 * Writesets get 'digested' into the main era array.
664 * We're using a coroutine here so the worker thread can do the digestion,
665 * thus avoiding synchronisation of the metadata. Digesting a whole
666 * writeset in one go would cause too much latency.
667 *--------------------------------------------------------------*/
670 unsigned nr_bits, current_bit;
671 struct writeset_metadata writeset;
673 struct dm_disk_bitset info;
675 int (*step)(struct era_metadata *, struct digest *);
678 static int metadata_digest_lookup_writeset(struct era_metadata *md,
681 static int metadata_digest_remove_writeset(struct era_metadata *md,
685 uint64_t key = d->era;
687 r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
688 &key, &md->writeset_tree_root);
690 DMERR("%s: dm_btree_remove failed", __func__);
694 d->step = metadata_digest_lookup_writeset;
698 #define INSERTS_PER_STEP 100
700 static int metadata_digest_transcribe_writeset(struct era_metadata *md,
705 unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
707 for (b = d->current_bit; b < e; b++) {
708 r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
710 DMERR("%s: writeset_marked_on_disk failed", __func__);
717 __dm_bless_for_disk(&d->value);
718 r = dm_array_set_value(&md->era_array_info, md->era_array_root,
719 b, &d->value, &md->era_array_root);
721 DMERR("%s: dm_array_set_value failed", __func__);
727 d->step = metadata_digest_remove_writeset;
734 static int metadata_digest_lookup_writeset(struct era_metadata *md,
739 struct writeset_disk disk;
741 r = dm_btree_find_lowest_key(&md->writeset_tree_info,
742 md->writeset_tree_root, &key);
748 r = dm_btree_lookup(&md->writeset_tree_info,
749 md->writeset_tree_root, &key, &disk);
756 DMERR("%s: dm_btree_lookup failed", __func__);
760 ws_unpack(&disk, &d->writeset);
761 d->value = cpu_to_le32(key);
764 * We initialise another bitset info to avoid any caching side effects
765 * with the previous one.
767 dm_disk_bitset_init(md->tm, &d->info);
769 d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
771 d->step = metadata_digest_transcribe_writeset;
776 static int metadata_digest_start(struct era_metadata *md, struct digest *d)
781 memset(d, 0, sizeof(*d));
782 d->step = metadata_digest_lookup_writeset;
787 /*----------------------------------------------------------------
788 * High level metadata interface. Target methods should use these, and not
789 * the lower level ones.
790 *--------------------------------------------------------------*/
791 static struct era_metadata *metadata_open(struct block_device *bdev,
796 struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
802 md->block_size = block_size;
804 md->writesets[0].md.root = INVALID_WRITESET_ROOT;
805 md->writesets[1].md.root = INVALID_WRITESET_ROOT;
806 md->current_writeset = &md->writesets[0];
808 r = create_persistent_data_objects(md, may_format);
817 static void metadata_close(struct era_metadata *md)
819 writeset_free(&md->writesets[0]);
820 writeset_free(&md->writesets[1]);
821 destroy_persistent_data_objects(md);
825 static bool valid_nr_blocks(dm_block_t n)
828 * dm_bitset restricts us to 2^32. test_bit & co. restrict us
829 * further to 2^31 - 1
831 return n < (1ull << 31);
834 static int metadata_resize(struct era_metadata *md, void *arg)
837 dm_block_t *new_size = arg;
840 if (!valid_nr_blocks(*new_size)) {
841 DMERR("Invalid number of origin blocks %llu",
842 (unsigned long long) *new_size);
846 writeset_free(&md->writesets[0]);
847 writeset_free(&md->writesets[1]);
849 r = writeset_alloc(&md->writesets[0], *new_size);
851 DMERR("%s: writeset_alloc failed for writeset 0", __func__);
855 r = writeset_alloc(&md->writesets[1], *new_size);
857 DMERR("%s: writeset_alloc failed for writeset 1", __func__);
858 writeset_free(&md->writesets[0]);
862 value = cpu_to_le32(0u);
863 __dm_bless_for_disk(&value);
864 r = dm_array_resize(&md->era_array_info, md->era_array_root,
865 md->nr_blocks, *new_size,
866 &value, &md->era_array_root);
868 DMERR("%s: dm_array_resize failed", __func__);
869 writeset_free(&md->writesets[0]);
870 writeset_free(&md->writesets[1]);
874 md->nr_blocks = *new_size;
878 static int metadata_era_archive(struct era_metadata *md)
882 struct writeset_disk value;
884 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
885 &md->current_writeset->md.root);
887 DMERR("%s: dm_bitset_flush failed", __func__);
891 ws_pack(&md->current_writeset->md, &value);
893 keys[0] = md->current_era;
894 __dm_bless_for_disk(&value);
895 r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
896 keys, &value, &md->writeset_tree_root);
898 DMERR("%s: couldn't insert writeset into btree", __func__);
899 /* FIXME: fail mode */
903 md->current_writeset->md.root = INVALID_WRITESET_ROOT;
904 md->archived_writesets = true;
909 static struct writeset *next_writeset(struct era_metadata *md)
911 return (md->current_writeset == &md->writesets[0]) ?
912 &md->writesets[1] : &md->writesets[0];
915 static int metadata_new_era(struct era_metadata *md)
918 struct writeset *new_writeset = next_writeset(md);
920 r = writeset_init(&md->bitset_info, new_writeset, md->nr_blocks);
922 DMERR("%s: writeset_init failed", __func__);
926 swap_writeset(md, new_writeset);
932 static int metadata_era_rollover(struct era_metadata *md)
936 if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
937 r = metadata_era_archive(md);
939 DMERR("%s: metadata_archive_era failed", __func__);
940 /* FIXME: fail mode? */
945 r = metadata_new_era(md);
947 DMERR("%s: new era failed", __func__);
948 /* FIXME: fail mode */
955 static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
961 ws = rcu_dereference(md->current_writeset);
962 r = writeset_marked(ws, block);
968 static int metadata_commit(struct era_metadata *md)
971 struct dm_block *sblock;
973 if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
974 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
975 &md->current_writeset->md.root);
977 DMERR("%s: bitset flush failed", __func__);
982 r = dm_tm_pre_commit(md->tm);
984 DMERR("%s: pre commit failed", __func__);
988 r = save_sm_root(md);
990 DMERR("%s: save_sm_root failed", __func__);
994 r = superblock_lock(md, &sblock);
996 DMERR("%s: superblock lock failed", __func__);
1000 prepare_superblock(md, dm_block_data(sblock));
1002 return dm_tm_commit(md->tm, sblock);
1005 static int metadata_checkpoint(struct era_metadata *md)
1008 * For now we just rollover, but later I want to put a check in to
1009 * avoid this if the filter is still pretty fresh.
1011 return metadata_era_rollover(md);
1015 * Metadata snapshots allow userland to access era data.
1017 static int metadata_take_snap(struct era_metadata *md)
1020 struct dm_block *clone;
1022 if (md->metadata_snap != SUPERBLOCK_LOCATION) {
1023 DMERR("%s: metadata snapshot already exists", __func__);
1027 r = metadata_era_rollover(md);
1029 DMERR("%s: era rollover failed", __func__);
1033 r = metadata_commit(md);
1035 DMERR("%s: pre commit failed", __func__);
1039 r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
1041 DMERR("%s: couldn't increment superblock", __func__);
1045 r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
1046 &sb_validator, &clone, &inc);
1048 DMERR("%s: couldn't shadow superblock", __func__);
1049 dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
1054 r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
1056 DMERR("%s: couldn't inc writeset tree root", __func__);
1057 dm_tm_unlock(md->tm, clone);
1061 r = dm_sm_inc_block(md->sm, md->era_array_root);
1063 DMERR("%s: couldn't inc era tree root", __func__);
1064 dm_sm_dec_block(md->sm, md->writeset_tree_root);
1065 dm_tm_unlock(md->tm, clone);
1069 md->metadata_snap = dm_block_location(clone);
1071 dm_tm_unlock(md->tm, clone);
1076 static int metadata_drop_snap(struct era_metadata *md)
1079 dm_block_t location;
1080 struct dm_block *clone;
1081 struct superblock_disk *disk;
1083 if (md->metadata_snap == SUPERBLOCK_LOCATION) {
1084 DMERR("%s: no snap to drop", __func__);
1088 r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
1090 DMERR("%s: couldn't read lock superblock clone", __func__);
1095 * Whatever happens now we'll commit with no record of the metadata
1098 md->metadata_snap = SUPERBLOCK_LOCATION;
1100 disk = dm_block_data(clone);
1101 r = dm_btree_del(&md->writeset_tree_info,
1102 le64_to_cpu(disk->writeset_tree_root));
1104 DMERR("%s: error deleting writeset tree clone", __func__);
1105 dm_tm_unlock(md->tm, clone);
1109 r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
1111 DMERR("%s: error deleting era array clone", __func__);
1112 dm_tm_unlock(md->tm, clone);
1116 location = dm_block_location(clone);
1117 dm_tm_unlock(md->tm, clone);
1119 return dm_sm_dec_block(md->sm, location);
1122 struct metadata_stats {
1129 static int metadata_get_stats(struct era_metadata *md, void *ptr)
1132 struct metadata_stats *s = ptr;
1133 dm_block_t nr_free, nr_total;
1135 r = dm_sm_get_nr_free(md->sm, &nr_free);
1137 DMERR("dm_sm_get_nr_free returned %d", r);
1141 r = dm_sm_get_nr_blocks(md->sm, &nr_total);
1143 DMERR("dm_pool_get_metadata_dev_size returned %d", r);
1147 s->used = nr_total - nr_free;
1148 s->total = nr_total;
1149 s->snap = md->metadata_snap;
1150 s->era = md->current_era;
1155 /*----------------------------------------------------------------*/
1158 struct dm_target *ti;
1160 struct dm_dev *metadata_dev;
1161 struct dm_dev *origin_dev;
1163 dm_block_t nr_blocks;
1164 uint32_t sectors_per_block;
1165 int sectors_per_block_shift;
1166 struct era_metadata *md;
1168 struct workqueue_struct *wq;
1169 struct work_struct worker;
1171 spinlock_t deferred_lock;
1172 struct bio_list deferred_bios;
1174 spinlock_t rpc_lock;
1175 struct list_head rpc_calls;
1177 struct digest digest;
1182 struct list_head list;
1184 int (*fn0)(struct era_metadata *);
1185 int (*fn1)(struct era_metadata *, void *);
1189 struct completion complete;
1192 /*----------------------------------------------------------------
1194 *---------------------------------------------------------------*/
1195 static bool block_size_is_power_of_two(struct era *era)
1197 return era->sectors_per_block_shift >= 0;
1200 static dm_block_t get_block(struct era *era, struct bio *bio)
1202 sector_t block_nr = bio->bi_iter.bi_sector;
1204 if (!block_size_is_power_of_two(era))
1205 (void) sector_div(block_nr, era->sectors_per_block);
1207 block_nr >>= era->sectors_per_block_shift;
1212 static void remap_to_origin(struct era *era, struct bio *bio)
1214 bio_set_dev(bio, era->origin_dev->bdev);
1217 /*----------------------------------------------------------------
1219 *--------------------------------------------------------------*/
1220 static void wake_worker(struct era *era)
1222 if (!atomic_read(&era->suspended))
1223 queue_work(era->wq, &era->worker);
1226 static void process_old_eras(struct era *era)
1230 if (!era->digest.step)
1233 r = era->digest.step(era->md, &era->digest);
1235 DMERR("%s: digest step failed, stopping digestion", __func__);
1236 era->digest.step = NULL;
1238 } else if (era->digest.step)
1242 static void process_deferred_bios(struct era *era)
1245 struct bio_list deferred_bios, marked_bios;
1247 struct blk_plug plug;
1248 bool commit_needed = false;
1249 bool failed = false;
1250 struct writeset *ws = era->md->current_writeset;
1252 bio_list_init(&deferred_bios);
1253 bio_list_init(&marked_bios);
1255 spin_lock(&era->deferred_lock);
1256 bio_list_merge(&deferred_bios, &era->deferred_bios);
1257 bio_list_init(&era->deferred_bios);
1258 spin_unlock(&era->deferred_lock);
1260 if (bio_list_empty(&deferred_bios))
1263 while ((bio = bio_list_pop(&deferred_bios))) {
1264 r = writeset_test_and_set(&era->md->bitset_info, ws,
1265 get_block(era, bio));
1268 * This is bad news, we need to rollback.
1273 commit_needed = true;
1275 bio_list_add(&marked_bios, bio);
1278 if (commit_needed) {
1279 r = metadata_commit(era->md);
1285 while ((bio = bio_list_pop(&marked_bios)))
1288 blk_start_plug(&plug);
1289 while ((bio = bio_list_pop(&marked_bios))) {
1291 * Only update the in-core writeset if the on-disk one
1295 set_bit(get_block(era, bio), ws->bits);
1296 submit_bio_noacct(bio);
1298 blk_finish_plug(&plug);
1302 static void process_rpc_calls(struct era *era)
1305 bool need_commit = false;
1306 struct list_head calls;
1307 struct rpc *rpc, *tmp;
1309 INIT_LIST_HEAD(&calls);
1310 spin_lock(&era->rpc_lock);
1311 list_splice_init(&era->rpc_calls, &calls);
1312 spin_unlock(&era->rpc_lock);
1314 list_for_each_entry_safe(rpc, tmp, &calls, list) {
1315 rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
1320 r = metadata_commit(era->md);
1322 list_for_each_entry_safe(rpc, tmp, &calls, list)
1326 list_for_each_entry_safe(rpc, tmp, &calls, list)
1327 complete(&rpc->complete);
1330 static void kick_off_digest(struct era *era)
1332 if (era->md->archived_writesets) {
1333 era->md->archived_writesets = false;
1334 metadata_digest_start(era->md, &era->digest);
1338 static void do_work(struct work_struct *ws)
1340 struct era *era = container_of(ws, struct era, worker);
1342 kick_off_digest(era);
1343 process_old_eras(era);
1344 process_deferred_bios(era);
1345 process_rpc_calls(era);
1348 static void defer_bio(struct era *era, struct bio *bio)
1350 spin_lock(&era->deferred_lock);
1351 bio_list_add(&era->deferred_bios, bio);
1352 spin_unlock(&era->deferred_lock);
1358 * Make an rpc call to the worker to change the metadata.
1360 static int perform_rpc(struct era *era, struct rpc *rpc)
1363 init_completion(&rpc->complete);
1365 spin_lock(&era->rpc_lock);
1366 list_add(&rpc->list, &era->rpc_calls);
1367 spin_unlock(&era->rpc_lock);
1370 wait_for_completion(&rpc->complete);
1375 static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
1381 return perform_rpc(era, &rpc);
1384 static int in_worker1(struct era *era,
1385 int (*fn)(struct era_metadata *, void *), void *arg)
1392 return perform_rpc(era, &rpc);
1395 static void start_worker(struct era *era)
1397 atomic_set(&era->suspended, 0);
1400 static void stop_worker(struct era *era)
1402 atomic_set(&era->suspended, 1);
1403 drain_workqueue(era->wq);
1406 /*----------------------------------------------------------------
1408 *--------------------------------------------------------------*/
1409 static void era_destroy(struct era *era)
1412 metadata_close(era->md);
1415 destroy_workqueue(era->wq);
1417 if (era->origin_dev)
1418 dm_put_device(era->ti, era->origin_dev);
1420 if (era->metadata_dev)
1421 dm_put_device(era->ti, era->metadata_dev);
1426 static dm_block_t calc_nr_blocks(struct era *era)
1428 return dm_sector_div_up(era->ti->len, era->sectors_per_block);
1431 static bool valid_block_size(dm_block_t block_size)
1433 bool greater_than_zero = block_size > 0;
1434 bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
1436 return greater_than_zero && multiple_of_min_block_size;
1440 * <metadata dev> <data dev> <data block size (sectors)>
1442 static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
1447 struct era_metadata *md;
1450 ti->error = "Invalid argument count";
1454 era = kzalloc(sizeof(*era), GFP_KERNEL);
1456 ti->error = "Error allocating era structure";
1462 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
1464 ti->error = "Error opening metadata device";
1469 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
1471 ti->error = "Error opening data device";
1476 r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
1478 ti->error = "Error parsing block size";
1483 r = dm_set_target_max_io_len(ti, era->sectors_per_block);
1485 ti->error = "could not set max io len";
1490 if (!valid_block_size(era->sectors_per_block)) {
1491 ti->error = "Invalid block size";
1495 if (era->sectors_per_block & (era->sectors_per_block - 1))
1496 era->sectors_per_block_shift = -1;
1498 era->sectors_per_block_shift = __ffs(era->sectors_per_block);
1500 md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
1502 ti->error = "Error reading metadata";
1508 era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1510 ti->error = "could not create workqueue for metadata object";
1514 INIT_WORK(&era->worker, do_work);
1516 spin_lock_init(&era->deferred_lock);
1517 bio_list_init(&era->deferred_bios);
1519 spin_lock_init(&era->rpc_lock);
1520 INIT_LIST_HEAD(&era->rpc_calls);
1523 ti->num_flush_bios = 1;
1524 ti->flush_supported = true;
1526 ti->num_discard_bios = 1;
1531 static void era_dtr(struct dm_target *ti)
1533 era_destroy(ti->private);
1536 static int era_map(struct dm_target *ti, struct bio *bio)
1538 struct era *era = ti->private;
1539 dm_block_t block = get_block(era, bio);
1542 * All bios get remapped to the origin device. We do this now, but
1543 * it may not get issued until later. Depending on whether the
1544 * block is marked in this era.
1546 remap_to_origin(era, bio);
1549 * REQ_PREFLUSH bios carry no data, so we're not interested in them.
1551 if (!(bio->bi_opf & REQ_PREFLUSH) &&
1552 (bio_data_dir(bio) == WRITE) &&
1553 !metadata_current_marked(era->md, block)) {
1554 defer_bio(era, bio);
1555 return DM_MAPIO_SUBMITTED;
1558 return DM_MAPIO_REMAPPED;
1561 static void era_postsuspend(struct dm_target *ti)
1564 struct era *era = ti->private;
1566 r = in_worker0(era, metadata_era_archive);
1568 DMERR("%s: couldn't archive current era", __func__);
1569 /* FIXME: fail mode */
1574 r = metadata_commit(era->md);
1576 DMERR("%s: metadata_commit failed", __func__);
1577 /* FIXME: fail mode */
1581 static int era_preresume(struct dm_target *ti)
1584 struct era *era = ti->private;
1585 dm_block_t new_size = calc_nr_blocks(era);
1587 if (era->nr_blocks != new_size) {
1588 r = metadata_resize(era->md, &new_size);
1590 DMERR("%s: metadata_resize failed", __func__);
1594 r = metadata_commit(era->md);
1596 DMERR("%s: metadata_commit failed", __func__);
1600 era->nr_blocks = new_size;
1605 r = in_worker0(era, metadata_era_rollover);
1607 DMERR("%s: metadata_era_rollover failed", __func__);
1617 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
1618 * <current era> <held metadata root | '-'>
1620 static void era_status(struct dm_target *ti, status_type_t type,
1621 unsigned status_flags, char *result, unsigned maxlen)
1624 struct era *era = ti->private;
1626 struct metadata_stats stats;
1627 char buf[BDEVNAME_SIZE];
1630 case STATUSTYPE_INFO:
1631 r = in_worker1(era, metadata_get_stats, &stats);
1635 DMEMIT("%u %llu/%llu %u",
1636 (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
1637 (unsigned long long) stats.used,
1638 (unsigned long long) stats.total,
1639 (unsigned) stats.era);
1641 if (stats.snap != SUPERBLOCK_LOCATION)
1642 DMEMIT(" %llu", stats.snap);
1647 case STATUSTYPE_TABLE:
1648 format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
1650 format_dev_t(buf, era->origin_dev->bdev->bd_dev);
1651 DMEMIT("%s %u", buf, era->sectors_per_block);
1654 case STATUSTYPE_IMA:
1665 static int era_message(struct dm_target *ti, unsigned argc, char **argv,
1666 char *result, unsigned maxlen)
1668 struct era *era = ti->private;
1671 DMERR("incorrect number of message arguments");
1675 if (!strcasecmp(argv[0], "checkpoint"))
1676 return in_worker0(era, metadata_checkpoint);
1678 if (!strcasecmp(argv[0], "take_metadata_snap"))
1679 return in_worker0(era, metadata_take_snap);
1681 if (!strcasecmp(argv[0], "drop_metadata_snap"))
1682 return in_worker0(era, metadata_drop_snap);
1684 DMERR("unsupported message '%s'", argv[0]);
1688 static sector_t get_dev_size(struct dm_dev *dev)
1690 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1693 static int era_iterate_devices(struct dm_target *ti,
1694 iterate_devices_callout_fn fn, void *data)
1696 struct era *era = ti->private;
1697 return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
1700 static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
1702 struct era *era = ti->private;
1703 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
1706 * If the system-determined stacked limits are compatible with the
1707 * era device's blocksize (io_opt is a factor) do not override them.
1709 if (io_opt_sectors < era->sectors_per_block ||
1710 do_div(io_opt_sectors, era->sectors_per_block)) {
1711 blk_limits_io_min(limits, 0);
1712 blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
1716 /*----------------------------------------------------------------*/
1718 static struct target_type era_target = {
1720 .version = {1, 0, 0},
1721 .module = THIS_MODULE,
1725 .postsuspend = era_postsuspend,
1726 .preresume = era_preresume,
1727 .status = era_status,
1728 .message = era_message,
1729 .iterate_devices = era_iterate_devices,
1730 .io_hints = era_io_hints
1733 static int __init dm_era_init(void)
1737 r = dm_register_target(&era_target);
1739 DMERR("era target registration failed: %d", r);
1746 static void __exit dm_era_exit(void)
1748 dm_unregister_target(&era_target);
1751 module_init(dm_era_init);
1752 module_exit(dm_era_exit);
1754 MODULE_DESCRIPTION(DM_NAME " era target");
1755 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
1756 MODULE_LICENSE("GPL");