109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2eec40579SJoe Thornber #include "dm.h" 3eec40579SJoe Thornber #include "persistent-data/dm-transaction-manager.h" 4eec40579SJoe Thornber #include "persistent-data/dm-bitset.h" 5eec40579SJoe Thornber #include "persistent-data/dm-space-map.h" 6eec40579SJoe Thornber 7eec40579SJoe Thornber #include <linux/dm-io.h> 8eec40579SJoe Thornber #include <linux/dm-kcopyd.h> 9eec40579SJoe Thornber #include <linux/init.h> 10eec40579SJoe Thornber #include <linux/mempool.h> 11eec40579SJoe Thornber #include <linux/module.h> 12eec40579SJoe Thornber #include <linux/slab.h> 13eec40579SJoe Thornber #include <linux/vmalloc.h> 14eec40579SJoe Thornber 15eec40579SJoe Thornber #define DM_MSG_PREFIX "era" 16eec40579SJoe Thornber 17eec40579SJoe Thornber #define SUPERBLOCK_LOCATION 0 18eec40579SJoe Thornber #define SUPERBLOCK_MAGIC 2126579579 19eec40579SJoe Thornber #define SUPERBLOCK_CSUM_XOR 146538381 20eec40579SJoe Thornber #define MIN_ERA_VERSION 1 21eec40579SJoe Thornber #define MAX_ERA_VERSION 1 22eec40579SJoe Thornber #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION 23eec40579SJoe Thornber #define MIN_BLOCK_SIZE 8 24eec40579SJoe Thornber 25a4a82ce3SHeinz Mauelshagen /* 26a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 27eec40579SJoe Thornber * Writeset 28a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 29a4a82ce3SHeinz Mauelshagen */ 30eec40579SJoe Thornber struct writeset_metadata { 31eec40579SJoe Thornber uint32_t nr_bits; 32eec40579SJoe Thornber dm_block_t root; 33eec40579SJoe Thornber }; 34eec40579SJoe Thornber 35eec40579SJoe Thornber struct writeset { 36eec40579SJoe Thornber struct writeset_metadata md; 37eec40579SJoe Thornber 38eec40579SJoe Thornber /* 39eec40579SJoe Thornber * An in core copy of the bits to save constantly doing look ups on 40eec40579SJoe Thornber * disk. 41eec40579SJoe Thornber */ 42eec40579SJoe Thornber unsigned long *bits; 43eec40579SJoe Thornber }; 44eec40579SJoe Thornber 45eec40579SJoe Thornber /* 46eec40579SJoe Thornber * This does not free off the on disk bitset as this will normally be done 47eec40579SJoe Thornber * after digesting into the era array. 48eec40579SJoe Thornber */ 49eec40579SJoe Thornber static void writeset_free(struct writeset *ws) 50eec40579SJoe Thornber { 51eec40579SJoe Thornber vfree(ws->bits); 52904e6b26SNikos Tsironis ws->bits = NULL; 53eec40579SJoe Thornber } 54eec40579SJoe Thornber 55eec40579SJoe Thornber static int setup_on_disk_bitset(struct dm_disk_bitset *info, 5686a3238cSHeinz Mauelshagen unsigned int nr_bits, dm_block_t *root) 57eec40579SJoe Thornber { 58eec40579SJoe Thornber int r; 59eec40579SJoe Thornber 60eec40579SJoe Thornber r = dm_bitset_empty(info, root); 61eec40579SJoe Thornber if (r) 62eec40579SJoe Thornber return r; 63eec40579SJoe Thornber 64eec40579SJoe Thornber return dm_bitset_resize(info, *root, 0, nr_bits, false, root); 65eec40579SJoe Thornber } 66eec40579SJoe Thornber 6786a3238cSHeinz Mauelshagen static size_t bitset_size(unsigned int nr_bits) 68eec40579SJoe Thornber { 69eec40579SJoe Thornber return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG); 70eec40579SJoe Thornber } 71eec40579SJoe Thornber 72eec40579SJoe Thornber /* 73eec40579SJoe Thornber * Allocates memory for the in core bitset. 74eec40579SJoe Thornber */ 75eec40579SJoe Thornber static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks) 76eec40579SJoe Thornber { 77eec40579SJoe Thornber ws->bits = vzalloc(bitset_size(nr_blocks)); 78eec40579SJoe Thornber if (!ws->bits) { 79eec40579SJoe Thornber DMERR("%s: couldn't allocate in memory bitset", __func__); 80eec40579SJoe Thornber return -ENOMEM; 81eec40579SJoe Thornber } 82eec40579SJoe Thornber 83eec40579SJoe Thornber return 0; 84eec40579SJoe Thornber } 85eec40579SJoe Thornber 86eec40579SJoe Thornber /* 87eec40579SJoe Thornber * Wipes the in-core bitset, and creates a new on disk bitset. 88eec40579SJoe Thornber */ 89de89afc1SNikos Tsironis static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws, 90de89afc1SNikos Tsironis dm_block_t nr_blocks) 91eec40579SJoe Thornber { 92eec40579SJoe Thornber int r; 93eec40579SJoe Thornber 94de89afc1SNikos Tsironis memset(ws->bits, 0, bitset_size(nr_blocks)); 95eec40579SJoe Thornber 96de89afc1SNikos Tsironis ws->md.nr_bits = nr_blocks; 97eec40579SJoe Thornber r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root); 98eec40579SJoe Thornber if (r) { 99eec40579SJoe Thornber DMERR("%s: setup_on_disk_bitset failed", __func__); 100eec40579SJoe Thornber return r; 101eec40579SJoe Thornber } 102eec40579SJoe Thornber 103eec40579SJoe Thornber return 0; 104eec40579SJoe Thornber } 105eec40579SJoe Thornber 106eec40579SJoe Thornber static bool writeset_marked(struct writeset *ws, dm_block_t block) 107eec40579SJoe Thornber { 108eec40579SJoe Thornber return test_bit(block, ws->bits); 109eec40579SJoe Thornber } 110eec40579SJoe Thornber 111eec40579SJoe Thornber static int writeset_marked_on_disk(struct dm_disk_bitset *info, 112eec40579SJoe Thornber struct writeset_metadata *m, dm_block_t block, 113eec40579SJoe Thornber bool *result) 114eec40579SJoe Thornber { 115eec40579SJoe Thornber dm_block_t old = m->root; 116eec40579SJoe Thornber 117eec40579SJoe Thornber /* 118eec40579SJoe Thornber * The bitset was flushed when it was archived, so we know there'll 119eec40579SJoe Thornber * be no change to the root. 120eec40579SJoe Thornber */ 121eec40579SJoe Thornber int r = dm_bitset_test_bit(info, m->root, block, &m->root, result); 122eec40579SJoe Thornber if (r) { 123eec40579SJoe Thornber DMERR("%s: dm_bitset_test_bit failed", __func__); 124eec40579SJoe Thornber return r; 125eec40579SJoe Thornber } 126eec40579SJoe Thornber 127eec40579SJoe Thornber BUG_ON(m->root != old); 128eec40579SJoe Thornber 129eec40579SJoe Thornber return r; 130eec40579SJoe Thornber } 131eec40579SJoe Thornber 132eec40579SJoe Thornber /* 133eec40579SJoe Thornber * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was. 134eec40579SJoe Thornber */ 135eec40579SJoe Thornber static int writeset_test_and_set(struct dm_disk_bitset *info, 136eec40579SJoe Thornber struct writeset *ws, uint32_t block) 137eec40579SJoe Thornber { 138eec40579SJoe Thornber int r; 139eec40579SJoe Thornber 1402099b145SNikos Tsironis if (!test_bit(block, ws->bits)) { 141eec40579SJoe Thornber r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root); 142eec40579SJoe Thornber if (r) { 143eec40579SJoe Thornber /* FIXME: fail mode */ 144eec40579SJoe Thornber return r; 145eec40579SJoe Thornber } 146eec40579SJoe Thornber 147eec40579SJoe Thornber return 0; 148eec40579SJoe Thornber } 149eec40579SJoe Thornber 150eec40579SJoe Thornber return 1; 151eec40579SJoe Thornber } 152eec40579SJoe Thornber 153a4a82ce3SHeinz Mauelshagen /* 154a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 155eec40579SJoe Thornber * On disk metadata layout 156a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 157a4a82ce3SHeinz Mauelshagen */ 158eec40579SJoe Thornber #define SPACE_MAP_ROOT_SIZE 128 159eec40579SJoe Thornber #define UUID_LEN 16 160eec40579SJoe Thornber 161eec40579SJoe Thornber struct writeset_disk { 162eec40579SJoe Thornber __le32 nr_bits; 163eec40579SJoe Thornber __le64 root; 164eec40579SJoe Thornber } __packed; 165eec40579SJoe Thornber 166eec40579SJoe Thornber struct superblock_disk { 167eec40579SJoe Thornber __le32 csum; 168eec40579SJoe Thornber __le32 flags; 169eec40579SJoe Thornber __le64 blocknr; 170eec40579SJoe Thornber 171eec40579SJoe Thornber __u8 uuid[UUID_LEN]; 172eec40579SJoe Thornber __le64 magic; 173eec40579SJoe Thornber __le32 version; 174eec40579SJoe Thornber 175eec40579SJoe Thornber __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; 176eec40579SJoe Thornber 177eec40579SJoe Thornber __le32 data_block_size; 178eec40579SJoe Thornber __le32 metadata_block_size; 179eec40579SJoe Thornber __le32 nr_blocks; 180eec40579SJoe Thornber 181eec40579SJoe Thornber __le32 current_era; 182eec40579SJoe Thornber struct writeset_disk current_writeset; 183eec40579SJoe Thornber 184eec40579SJoe Thornber /* 185eec40579SJoe Thornber * Only these two fields are valid within the metadata snapshot. 186eec40579SJoe Thornber */ 187eec40579SJoe Thornber __le64 writeset_tree_root; 188eec40579SJoe Thornber __le64 era_array_root; 189eec40579SJoe Thornber 190eec40579SJoe Thornber __le64 metadata_snap; 191eec40579SJoe Thornber } __packed; 192eec40579SJoe Thornber 193a4a82ce3SHeinz Mauelshagen /* 194a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 195eec40579SJoe Thornber * Superblock validation 196a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 197a4a82ce3SHeinz Mauelshagen */ 198eec40579SJoe Thornber static void sb_prepare_for_write(struct dm_block_validator *v, 199eec40579SJoe Thornber struct dm_block *b, 200eec40579SJoe Thornber size_t sb_block_size) 201eec40579SJoe Thornber { 202eec40579SJoe Thornber struct superblock_disk *disk = dm_block_data(b); 203eec40579SJoe Thornber 204eec40579SJoe Thornber disk->blocknr = cpu_to_le64(dm_block_location(b)); 205eec40579SJoe Thornber disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags, 206eec40579SJoe Thornber sb_block_size - sizeof(__le32), 207eec40579SJoe Thornber SUPERBLOCK_CSUM_XOR)); 208eec40579SJoe Thornber } 209eec40579SJoe Thornber 210eec40579SJoe Thornber static int check_metadata_version(struct superblock_disk *disk) 211eec40579SJoe Thornber { 212eec40579SJoe Thornber uint32_t metadata_version = le32_to_cpu(disk->version); 213eec40579SJoe Thornber if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) { 214eec40579SJoe Thornber DMERR("Era metadata version %u found, but only versions between %u and %u supported.", 215eec40579SJoe Thornber metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION); 216eec40579SJoe Thornber return -EINVAL; 217eec40579SJoe Thornber } 218eec40579SJoe Thornber 219eec40579SJoe Thornber return 0; 220eec40579SJoe Thornber } 221eec40579SJoe Thornber 222eec40579SJoe Thornber static int sb_check(struct dm_block_validator *v, 223eec40579SJoe Thornber struct dm_block *b, 224eec40579SJoe Thornber size_t sb_block_size) 225eec40579SJoe Thornber { 226eec40579SJoe Thornber struct superblock_disk *disk = dm_block_data(b); 227eec40579SJoe Thornber __le32 csum_le; 228eec40579SJoe Thornber 229eec40579SJoe Thornber if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) { 230eec40579SJoe Thornber DMERR("sb_check failed: blocknr %llu: wanted %llu", 231eec40579SJoe Thornber le64_to_cpu(disk->blocknr), 232eec40579SJoe Thornber (unsigned long long)dm_block_location(b)); 233eec40579SJoe Thornber return -ENOTBLK; 234eec40579SJoe Thornber } 235eec40579SJoe Thornber 236eec40579SJoe Thornber if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) { 237eec40579SJoe Thornber DMERR("sb_check failed: magic %llu: wanted %llu", 238eec40579SJoe Thornber le64_to_cpu(disk->magic), 239eec40579SJoe Thornber (unsigned long long) SUPERBLOCK_MAGIC); 240eec40579SJoe Thornber return -EILSEQ; 241eec40579SJoe Thornber } 242eec40579SJoe Thornber 243eec40579SJoe Thornber csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags, 244eec40579SJoe Thornber sb_block_size - sizeof(__le32), 245eec40579SJoe Thornber SUPERBLOCK_CSUM_XOR)); 246eec40579SJoe Thornber if (csum_le != disk->csum) { 247eec40579SJoe Thornber DMERR("sb_check failed: csum %u: wanted %u", 248eec40579SJoe Thornber le32_to_cpu(csum_le), le32_to_cpu(disk->csum)); 249eec40579SJoe Thornber return -EILSEQ; 250eec40579SJoe Thornber } 251eec40579SJoe Thornber 252eec40579SJoe Thornber return check_metadata_version(disk); 253eec40579SJoe Thornber } 254eec40579SJoe Thornber 255eec40579SJoe Thornber static struct dm_block_validator sb_validator = { 256eec40579SJoe Thornber .name = "superblock", 257eec40579SJoe Thornber .prepare_for_write = sb_prepare_for_write, 258eec40579SJoe Thornber .check = sb_check 259eec40579SJoe Thornber }; 260eec40579SJoe Thornber 261a4a82ce3SHeinz Mauelshagen /* 262a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 263eec40579SJoe Thornber * Low level metadata handling 264a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 265a4a82ce3SHeinz Mauelshagen */ 266eec40579SJoe Thornber #define DM_ERA_METADATA_BLOCK_SIZE 4096 267eec40579SJoe Thornber #define ERA_MAX_CONCURRENT_LOCKS 5 268eec40579SJoe Thornber 269eec40579SJoe Thornber struct era_metadata { 270eec40579SJoe Thornber struct block_device *bdev; 271eec40579SJoe Thornber struct dm_block_manager *bm; 272eec40579SJoe Thornber struct dm_space_map *sm; 273eec40579SJoe Thornber struct dm_transaction_manager *tm; 274eec40579SJoe Thornber 275eec40579SJoe Thornber dm_block_t block_size; 276eec40579SJoe Thornber uint32_t nr_blocks; 277eec40579SJoe Thornber 278eec40579SJoe Thornber uint32_t current_era; 279eec40579SJoe Thornber 280eec40579SJoe Thornber /* 281eec40579SJoe Thornber * We preallocate 2 writesets. When an era rolls over we 282eec40579SJoe Thornber * switch between them. This means the allocation is done at 283eec40579SJoe Thornber * preresume time, rather than on the io path. 284eec40579SJoe Thornber */ 285eec40579SJoe Thornber struct writeset writesets[2]; 286eec40579SJoe Thornber struct writeset *current_writeset; 287eec40579SJoe Thornber 288eec40579SJoe Thornber dm_block_t writeset_tree_root; 289eec40579SJoe Thornber dm_block_t era_array_root; 290eec40579SJoe Thornber 291eec40579SJoe Thornber struct dm_disk_bitset bitset_info; 292eec40579SJoe Thornber struct dm_btree_info writeset_tree_info; 293eec40579SJoe Thornber struct dm_array_info era_array_info; 294eec40579SJoe Thornber 295eec40579SJoe Thornber dm_block_t metadata_snap; 296eec40579SJoe Thornber 297eec40579SJoe Thornber /* 298eec40579SJoe Thornber * A flag that is set whenever a writeset has been archived. 299eec40579SJoe Thornber */ 300eec40579SJoe Thornber bool archived_writesets; 3015a32083dSJoe Thornber 3025a32083dSJoe Thornber /* 3035a32083dSJoe Thornber * Reading the space map root can fail, so we read it into this 3045a32083dSJoe Thornber * buffer before the superblock is locked and updated. 3055a32083dSJoe Thornber */ 3065a32083dSJoe Thornber __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; 307eec40579SJoe Thornber }; 308eec40579SJoe Thornber 309eec40579SJoe Thornber static int superblock_read_lock(struct era_metadata *md, 310eec40579SJoe Thornber struct dm_block **sblock) 311eec40579SJoe Thornber { 312eec40579SJoe Thornber return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION, 313eec40579SJoe Thornber &sb_validator, sblock); 314eec40579SJoe Thornber } 315eec40579SJoe Thornber 316eec40579SJoe Thornber static int superblock_lock_zero(struct era_metadata *md, 317eec40579SJoe Thornber struct dm_block **sblock) 318eec40579SJoe Thornber { 319eec40579SJoe Thornber return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION, 320eec40579SJoe Thornber &sb_validator, sblock); 321eec40579SJoe Thornber } 322eec40579SJoe Thornber 323eec40579SJoe Thornber static int superblock_lock(struct era_metadata *md, 324eec40579SJoe Thornber struct dm_block **sblock) 325eec40579SJoe Thornber { 326eec40579SJoe Thornber return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION, 327eec40579SJoe Thornber &sb_validator, sblock); 328eec40579SJoe Thornber } 329eec40579SJoe Thornber 330eec40579SJoe Thornber /* FIXME: duplication with cache and thin */ 331eec40579SJoe Thornber static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result) 332eec40579SJoe Thornber { 333eec40579SJoe Thornber int r; 33486a3238cSHeinz Mauelshagen unsigned int i; 335eec40579SJoe Thornber struct dm_block *b; 336eec40579SJoe Thornber __le64 *data_le, zero = cpu_to_le64(0); 33786a3238cSHeinz Mauelshagen unsigned int sb_block_size = dm_bm_block_size(bm) / sizeof(__le64); 338eec40579SJoe Thornber 339eec40579SJoe Thornber /* 340eec40579SJoe Thornber * We can't use a validator here - it may be all zeroes. 341eec40579SJoe Thornber */ 342eec40579SJoe Thornber r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b); 343eec40579SJoe Thornber if (r) 344eec40579SJoe Thornber return r; 345eec40579SJoe Thornber 346eec40579SJoe Thornber data_le = dm_block_data(b); 347eec40579SJoe Thornber *result = true; 348eec40579SJoe Thornber for (i = 0; i < sb_block_size; i++) { 349eec40579SJoe Thornber if (data_le[i] != zero) { 350eec40579SJoe Thornber *result = false; 351eec40579SJoe Thornber break; 352eec40579SJoe Thornber } 353eec40579SJoe Thornber } 354eec40579SJoe Thornber 3554c7da06fSMikulas Patocka dm_bm_unlock(b); 3564c7da06fSMikulas Patocka 3574c7da06fSMikulas Patocka return 0; 358eec40579SJoe Thornber } 359eec40579SJoe Thornber 360eec40579SJoe Thornber /*----------------------------------------------------------------*/ 361eec40579SJoe Thornber 362eec40579SJoe Thornber static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk) 363eec40579SJoe Thornber { 364eec40579SJoe Thornber disk->nr_bits = cpu_to_le32(core->nr_bits); 365eec40579SJoe Thornber disk->root = cpu_to_le64(core->root); 366eec40579SJoe Thornber } 367eec40579SJoe Thornber 368eec40579SJoe Thornber static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core) 369eec40579SJoe Thornber { 370eec40579SJoe Thornber core->nr_bits = le32_to_cpu(disk->nr_bits); 371eec40579SJoe Thornber core->root = le64_to_cpu(disk->root); 372eec40579SJoe Thornber } 373eec40579SJoe Thornber 37486a3238cSHeinz Mauelshagen static void ws_inc(void *context, const void *value, unsigned int count) 375eec40579SJoe Thornber { 376eec40579SJoe Thornber struct era_metadata *md = context; 377eec40579SJoe Thornber struct writeset_disk ws_d; 378eec40579SJoe Thornber dm_block_t b; 37986a3238cSHeinz Mauelshagen unsigned int i; 380eec40579SJoe Thornber 381be500ed7SJoe Thornber for (i = 0; i < count; i++) { 382be500ed7SJoe Thornber memcpy(&ws_d, value + (i * sizeof(ws_d)), sizeof(ws_d)); 383eec40579SJoe Thornber b = le64_to_cpu(ws_d.root); 384eec40579SJoe Thornber dm_tm_inc(md->tm, b); 385eec40579SJoe Thornber } 386be500ed7SJoe Thornber } 387eec40579SJoe Thornber 38886a3238cSHeinz Mauelshagen static void ws_dec(void *context, const void *value, unsigned int count) 389eec40579SJoe Thornber { 390eec40579SJoe Thornber struct era_metadata *md = context; 391eec40579SJoe Thornber struct writeset_disk ws_d; 392eec40579SJoe Thornber dm_block_t b; 39386a3238cSHeinz Mauelshagen unsigned int i; 394eec40579SJoe Thornber 395be500ed7SJoe Thornber for (i = 0; i < count; i++) { 396be500ed7SJoe Thornber memcpy(&ws_d, value + (i * sizeof(ws_d)), sizeof(ws_d)); 397eec40579SJoe Thornber b = le64_to_cpu(ws_d.root); 398eec40579SJoe Thornber dm_bitset_del(&md->bitset_info, b); 399eec40579SJoe Thornber } 400be500ed7SJoe Thornber } 401eec40579SJoe Thornber 402eec40579SJoe Thornber static int ws_eq(void *context, const void *value1, const void *value2) 403eec40579SJoe Thornber { 40464f2d15aSNikos Tsironis return !memcmp(value1, value2, sizeof(struct writeset_disk)); 405eec40579SJoe Thornber } 406eec40579SJoe Thornber 407eec40579SJoe Thornber /*----------------------------------------------------------------*/ 408eec40579SJoe Thornber 409eec40579SJoe Thornber static void setup_writeset_tree_info(struct era_metadata *md) 410eec40579SJoe Thornber { 411eec40579SJoe Thornber struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type; 412eec40579SJoe Thornber md->writeset_tree_info.tm = md->tm; 413eec40579SJoe Thornber md->writeset_tree_info.levels = 1; 414eec40579SJoe Thornber vt->context = md; 415eec40579SJoe Thornber vt->size = sizeof(struct writeset_disk); 416eec40579SJoe Thornber vt->inc = ws_inc; 417eec40579SJoe Thornber vt->dec = ws_dec; 418eec40579SJoe Thornber vt->equal = ws_eq; 419eec40579SJoe Thornber } 420eec40579SJoe Thornber 421eec40579SJoe Thornber static void setup_era_array_info(struct era_metadata *md) 422eec40579SJoe Thornber 423eec40579SJoe Thornber { 424eec40579SJoe Thornber struct dm_btree_value_type vt; 425eec40579SJoe Thornber vt.context = NULL; 426eec40579SJoe Thornber vt.size = sizeof(__le32); 427eec40579SJoe Thornber vt.inc = NULL; 428eec40579SJoe Thornber vt.dec = NULL; 429eec40579SJoe Thornber vt.equal = NULL; 430eec40579SJoe Thornber 431eec40579SJoe Thornber dm_array_info_init(&md->era_array_info, md->tm, &vt); 432eec40579SJoe Thornber } 433eec40579SJoe Thornber 434eec40579SJoe Thornber static void setup_infos(struct era_metadata *md) 435eec40579SJoe Thornber { 436eec40579SJoe Thornber dm_disk_bitset_init(md->tm, &md->bitset_info); 437eec40579SJoe Thornber setup_writeset_tree_info(md); 438eec40579SJoe Thornber setup_era_array_info(md); 439eec40579SJoe Thornber } 440eec40579SJoe Thornber 441eec40579SJoe Thornber /*----------------------------------------------------------------*/ 442eec40579SJoe Thornber 443eec40579SJoe Thornber static int create_fresh_metadata(struct era_metadata *md) 444eec40579SJoe Thornber { 445eec40579SJoe Thornber int r; 446eec40579SJoe Thornber 447eec40579SJoe Thornber r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION, 448eec40579SJoe Thornber &md->tm, &md->sm); 449eec40579SJoe Thornber if (r < 0) { 450eec40579SJoe Thornber DMERR("dm_tm_create_with_sm failed"); 451eec40579SJoe Thornber return r; 452eec40579SJoe Thornber } 453eec40579SJoe Thornber 454eec40579SJoe Thornber setup_infos(md); 455eec40579SJoe Thornber 456eec40579SJoe Thornber r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root); 457eec40579SJoe Thornber if (r) { 458eec40579SJoe Thornber DMERR("couldn't create new writeset tree"); 459eec40579SJoe Thornber goto bad; 460eec40579SJoe Thornber } 461eec40579SJoe Thornber 462eec40579SJoe Thornber r = dm_array_empty(&md->era_array_info, &md->era_array_root); 463eec40579SJoe Thornber if (r) { 464eec40579SJoe Thornber DMERR("couldn't create era array"); 465eec40579SJoe Thornber goto bad; 466eec40579SJoe Thornber } 467eec40579SJoe Thornber 468eec40579SJoe Thornber return 0; 469eec40579SJoe Thornber 470eec40579SJoe Thornber bad: 471eec40579SJoe Thornber dm_sm_destroy(md->sm); 472eec40579SJoe Thornber dm_tm_destroy(md->tm); 473eec40579SJoe Thornber 474eec40579SJoe Thornber return r; 475eec40579SJoe Thornber } 476eec40579SJoe Thornber 4775a32083dSJoe Thornber static int save_sm_root(struct era_metadata *md) 4785a32083dSJoe Thornber { 4795a32083dSJoe Thornber int r; 4805a32083dSJoe Thornber size_t metadata_len; 4815a32083dSJoe Thornber 4825a32083dSJoe Thornber r = dm_sm_root_size(md->sm, &metadata_len); 4835a32083dSJoe Thornber if (r < 0) 4845a32083dSJoe Thornber return r; 4855a32083dSJoe Thornber 4865a32083dSJoe Thornber return dm_sm_copy_root(md->sm, &md->metadata_space_map_root, 4875a32083dSJoe Thornber metadata_len); 4885a32083dSJoe Thornber } 4895a32083dSJoe Thornber 4905a32083dSJoe Thornber static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk) 4915a32083dSJoe Thornber { 4925a32083dSJoe Thornber memcpy(&disk->metadata_space_map_root, 4935a32083dSJoe Thornber &md->metadata_space_map_root, 4945a32083dSJoe Thornber sizeof(md->metadata_space_map_root)); 4955a32083dSJoe Thornber } 4965a32083dSJoe Thornber 497eec40579SJoe Thornber /* 498eec40579SJoe Thornber * Writes a superblock, including the static fields that don't get updated 499eec40579SJoe Thornber * with every commit (possible optimisation here). 'md' should be fully 500eec40579SJoe Thornber * constructed when this is called. 501eec40579SJoe Thornber */ 5025a32083dSJoe Thornber static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk) 503eec40579SJoe Thornber { 504eec40579SJoe Thornber disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC); 505eec40579SJoe Thornber disk->flags = cpu_to_le32(0ul); 506eec40579SJoe Thornber 507eec40579SJoe Thornber /* FIXME: can't keep blanking the uuid (uuid is currently unused though) */ 508eec40579SJoe Thornber memset(disk->uuid, 0, sizeof(disk->uuid)); 509eec40579SJoe Thornber disk->version = cpu_to_le32(MAX_ERA_VERSION); 510eec40579SJoe Thornber 5115a32083dSJoe Thornber copy_sm_root(md, disk); 512eec40579SJoe Thornber 513eec40579SJoe Thornber disk->data_block_size = cpu_to_le32(md->block_size); 514eec40579SJoe Thornber disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT); 515eec40579SJoe Thornber disk->nr_blocks = cpu_to_le32(md->nr_blocks); 516eec40579SJoe Thornber disk->current_era = cpu_to_le32(md->current_era); 517eec40579SJoe Thornber 518eec40579SJoe Thornber ws_pack(&md->current_writeset->md, &disk->current_writeset); 519eec40579SJoe Thornber disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root); 520eec40579SJoe Thornber disk->era_array_root = cpu_to_le64(md->era_array_root); 521eec40579SJoe Thornber disk->metadata_snap = cpu_to_le64(md->metadata_snap); 522eec40579SJoe Thornber } 523eec40579SJoe Thornber 524eec40579SJoe Thornber static int write_superblock(struct era_metadata *md) 525eec40579SJoe Thornber { 526eec40579SJoe Thornber int r; 527eec40579SJoe Thornber struct dm_block *sblock; 528eec40579SJoe Thornber struct superblock_disk *disk; 529eec40579SJoe Thornber 5305a32083dSJoe Thornber r = save_sm_root(md); 5315a32083dSJoe Thornber if (r) { 5325a32083dSJoe Thornber DMERR("%s: save_sm_root failed", __func__); 5335a32083dSJoe Thornber return r; 5345a32083dSJoe Thornber } 5355a32083dSJoe Thornber 536eec40579SJoe Thornber r = superblock_lock_zero(md, &sblock); 537eec40579SJoe Thornber if (r) 538eec40579SJoe Thornber return r; 539eec40579SJoe Thornber 540eec40579SJoe Thornber disk = dm_block_data(sblock); 5415a32083dSJoe Thornber prepare_superblock(md, disk); 542eec40579SJoe Thornber 543eec40579SJoe Thornber return dm_tm_commit(md->tm, sblock); 544eec40579SJoe Thornber } 545eec40579SJoe Thornber 546eec40579SJoe Thornber /* 547eec40579SJoe Thornber * Assumes block_size and the infos are set. 548eec40579SJoe Thornber */ 549eec40579SJoe Thornber static int format_metadata(struct era_metadata *md) 550eec40579SJoe Thornber { 551eec40579SJoe Thornber int r; 552eec40579SJoe Thornber 553eec40579SJoe Thornber r = create_fresh_metadata(md); 554eec40579SJoe Thornber if (r) 555eec40579SJoe Thornber return r; 556eec40579SJoe Thornber 557eec40579SJoe Thornber r = write_superblock(md); 558eec40579SJoe Thornber if (r) { 559eec40579SJoe Thornber dm_sm_destroy(md->sm); 560eec40579SJoe Thornber dm_tm_destroy(md->tm); 561eec40579SJoe Thornber return r; 562eec40579SJoe Thornber } 563eec40579SJoe Thornber 564eec40579SJoe Thornber return 0; 565eec40579SJoe Thornber } 566eec40579SJoe Thornber 567eec40579SJoe Thornber static int open_metadata(struct era_metadata *md) 568eec40579SJoe Thornber { 569eec40579SJoe Thornber int r; 570eec40579SJoe Thornber struct dm_block *sblock; 571eec40579SJoe Thornber struct superblock_disk *disk; 572eec40579SJoe Thornber 573eec40579SJoe Thornber r = superblock_read_lock(md, &sblock); 574eec40579SJoe Thornber if (r) { 575eec40579SJoe Thornber DMERR("couldn't read_lock superblock"); 576eec40579SJoe Thornber return r; 577eec40579SJoe Thornber } 578eec40579SJoe Thornber 579eec40579SJoe Thornber disk = dm_block_data(sblock); 580c8e846ffSNikos Tsironis 581c8e846ffSNikos Tsironis /* Verify the data block size hasn't changed */ 582c8e846ffSNikos Tsironis if (le32_to_cpu(disk->data_block_size) != md->block_size) { 583c8e846ffSNikos Tsironis DMERR("changing the data block size (from %u to %llu) is not supported", 584c8e846ffSNikos Tsironis le32_to_cpu(disk->data_block_size), md->block_size); 585c8e846ffSNikos Tsironis r = -EINVAL; 586c8e846ffSNikos Tsironis goto bad; 587c8e846ffSNikos Tsironis } 588c8e846ffSNikos Tsironis 589eec40579SJoe Thornber r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION, 590eec40579SJoe Thornber disk->metadata_space_map_root, 591eec40579SJoe Thornber sizeof(disk->metadata_space_map_root), 592eec40579SJoe Thornber &md->tm, &md->sm); 593eec40579SJoe Thornber if (r) { 594eec40579SJoe Thornber DMERR("dm_tm_open_with_sm failed"); 595eec40579SJoe Thornber goto bad; 596eec40579SJoe Thornber } 597eec40579SJoe Thornber 598eec40579SJoe Thornber setup_infos(md); 599eec40579SJoe Thornber 600eec40579SJoe Thornber md->nr_blocks = le32_to_cpu(disk->nr_blocks); 601eec40579SJoe Thornber md->current_era = le32_to_cpu(disk->current_era); 602eec40579SJoe Thornber 603de89afc1SNikos Tsironis ws_unpack(&disk->current_writeset, &md->current_writeset->md); 604eec40579SJoe Thornber md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root); 605eec40579SJoe Thornber md->era_array_root = le64_to_cpu(disk->era_array_root); 606eec40579SJoe Thornber md->metadata_snap = le64_to_cpu(disk->metadata_snap); 607eec40579SJoe Thornber md->archived_writesets = true; 608eec40579SJoe Thornber 6094c7da06fSMikulas Patocka dm_bm_unlock(sblock); 6104c7da06fSMikulas Patocka 6114c7da06fSMikulas Patocka return 0; 612eec40579SJoe Thornber 613eec40579SJoe Thornber bad: 614eec40579SJoe Thornber dm_bm_unlock(sblock); 615eec40579SJoe Thornber return r; 616eec40579SJoe Thornber } 617eec40579SJoe Thornber 618eec40579SJoe Thornber static int open_or_format_metadata(struct era_metadata *md, 619eec40579SJoe Thornber bool may_format) 620eec40579SJoe Thornber { 621eec40579SJoe Thornber int r; 622eec40579SJoe Thornber bool unformatted = false; 623eec40579SJoe Thornber 624eec40579SJoe Thornber r = superblock_all_zeroes(md->bm, &unformatted); 625eec40579SJoe Thornber if (r) 626eec40579SJoe Thornber return r; 627eec40579SJoe Thornber 628eec40579SJoe Thornber if (unformatted) 629eec40579SJoe Thornber return may_format ? format_metadata(md) : -EPERM; 630eec40579SJoe Thornber 631eec40579SJoe Thornber return open_metadata(md); 632eec40579SJoe Thornber } 633eec40579SJoe Thornber 634eec40579SJoe Thornber static int create_persistent_data_objects(struct era_metadata *md, 635eec40579SJoe Thornber bool may_format) 636eec40579SJoe Thornber { 637eec40579SJoe Thornber int r; 638eec40579SJoe Thornber 639eec40579SJoe Thornber md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE, 640eec40579SJoe Thornber ERA_MAX_CONCURRENT_LOCKS); 641eec40579SJoe Thornber if (IS_ERR(md->bm)) { 642eec40579SJoe Thornber DMERR("could not create block manager"); 643eec40579SJoe Thornber return PTR_ERR(md->bm); 644eec40579SJoe Thornber } 645eec40579SJoe Thornber 646eec40579SJoe Thornber r = open_or_format_metadata(md, may_format); 647eec40579SJoe Thornber if (r) 648eec40579SJoe Thornber dm_block_manager_destroy(md->bm); 649eec40579SJoe Thornber 650eec40579SJoe Thornber return r; 651eec40579SJoe Thornber } 652eec40579SJoe Thornber 653eec40579SJoe Thornber static void destroy_persistent_data_objects(struct era_metadata *md) 654eec40579SJoe Thornber { 655eec40579SJoe Thornber dm_sm_destroy(md->sm); 656eec40579SJoe Thornber dm_tm_destroy(md->tm); 657eec40579SJoe Thornber dm_block_manager_destroy(md->bm); 658eec40579SJoe Thornber } 659eec40579SJoe Thornber 660eec40579SJoe Thornber /* 661eec40579SJoe Thornber * This waits until all era_map threads have picked up the new filter. 662eec40579SJoe Thornber */ 663eec40579SJoe Thornber static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset) 664eec40579SJoe Thornber { 665eec40579SJoe Thornber rcu_assign_pointer(md->current_writeset, new_writeset); 666eec40579SJoe Thornber synchronize_rcu(); 667eec40579SJoe Thornber } 668eec40579SJoe Thornber 669a4a82ce3SHeinz Mauelshagen /* 670a4a82ce3SHeinz Mauelshagen *------------------------------------------------------------------------ 671eec40579SJoe Thornber * Writesets get 'digested' into the main era array. 672eec40579SJoe Thornber * 673eec40579SJoe Thornber * We're using a coroutine here so the worker thread can do the digestion, 674eec40579SJoe Thornber * thus avoiding synchronisation of the metadata. Digesting a whole 675eec40579SJoe Thornber * writeset in one go would cause too much latency. 676a4a82ce3SHeinz Mauelshagen *------------------------------------------------------------------------ 677a4a82ce3SHeinz Mauelshagen */ 678eec40579SJoe Thornber struct digest { 679eec40579SJoe Thornber uint32_t era; 68086a3238cSHeinz Mauelshagen unsigned int nr_bits, current_bit; 681eec40579SJoe Thornber struct writeset_metadata writeset; 682eec40579SJoe Thornber __le32 value; 683eec40579SJoe Thornber struct dm_disk_bitset info; 684eec40579SJoe Thornber 685*02f10ba1SHeinz Mauelshagen int (*step)(struct era_metadata *md, struct digest *d); 686eec40579SJoe Thornber }; 687eec40579SJoe Thornber 688eec40579SJoe Thornber static int metadata_digest_lookup_writeset(struct era_metadata *md, 689eec40579SJoe Thornber struct digest *d); 690eec40579SJoe Thornber 691eec40579SJoe Thornber static int metadata_digest_remove_writeset(struct era_metadata *md, 692eec40579SJoe Thornber struct digest *d) 693eec40579SJoe Thornber { 694eec40579SJoe Thornber int r; 695eec40579SJoe Thornber uint64_t key = d->era; 696eec40579SJoe Thornber 697eec40579SJoe Thornber r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root, 698eec40579SJoe Thornber &key, &md->writeset_tree_root); 699eec40579SJoe Thornber if (r) { 700eec40579SJoe Thornber DMERR("%s: dm_btree_remove failed", __func__); 701eec40579SJoe Thornber return r; 702eec40579SJoe Thornber } 703eec40579SJoe Thornber 704eec40579SJoe Thornber d->step = metadata_digest_lookup_writeset; 705eec40579SJoe Thornber return 0; 706eec40579SJoe Thornber } 707eec40579SJoe Thornber 708eec40579SJoe Thornber #define INSERTS_PER_STEP 100 709eec40579SJoe Thornber 710eec40579SJoe Thornber static int metadata_digest_transcribe_writeset(struct era_metadata *md, 711eec40579SJoe Thornber struct digest *d) 712eec40579SJoe Thornber { 713eec40579SJoe Thornber int r; 714eec40579SJoe Thornber bool marked; 71586a3238cSHeinz Mauelshagen unsigned int b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits); 716eec40579SJoe Thornber 717eec40579SJoe Thornber for (b = d->current_bit; b < e; b++) { 718eec40579SJoe Thornber r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked); 719eec40579SJoe Thornber if (r) { 720eec40579SJoe Thornber DMERR("%s: writeset_marked_on_disk failed", __func__); 721eec40579SJoe Thornber return r; 722eec40579SJoe Thornber } 723eec40579SJoe Thornber 724eec40579SJoe Thornber if (!marked) 725eec40579SJoe Thornber continue; 726eec40579SJoe Thornber 727eec40579SJoe Thornber __dm_bless_for_disk(&d->value); 728eec40579SJoe Thornber r = dm_array_set_value(&md->era_array_info, md->era_array_root, 729eec40579SJoe Thornber b, &d->value, &md->era_array_root); 730eec40579SJoe Thornber if (r) { 731eec40579SJoe Thornber DMERR("%s: dm_array_set_value failed", __func__); 732eec40579SJoe Thornber return r; 733eec40579SJoe Thornber } 734eec40579SJoe Thornber } 735eec40579SJoe Thornber 736eec40579SJoe Thornber if (b == d->nr_bits) 737eec40579SJoe Thornber d->step = metadata_digest_remove_writeset; 738eec40579SJoe Thornber else 739eec40579SJoe Thornber d->current_bit = b; 740eec40579SJoe Thornber 741eec40579SJoe Thornber return 0; 742eec40579SJoe Thornber } 743eec40579SJoe Thornber 744eec40579SJoe Thornber static int metadata_digest_lookup_writeset(struct era_metadata *md, 745eec40579SJoe Thornber struct digest *d) 746eec40579SJoe Thornber { 747eec40579SJoe Thornber int r; 748eec40579SJoe Thornber uint64_t key; 749eec40579SJoe Thornber struct writeset_disk disk; 750eec40579SJoe Thornber 751eec40579SJoe Thornber r = dm_btree_find_lowest_key(&md->writeset_tree_info, 752eec40579SJoe Thornber md->writeset_tree_root, &key); 753eec40579SJoe Thornber if (r < 0) 754eec40579SJoe Thornber return r; 755eec40579SJoe Thornber 756eec40579SJoe Thornber d->era = key; 757eec40579SJoe Thornber 758eec40579SJoe Thornber r = dm_btree_lookup(&md->writeset_tree_info, 759eec40579SJoe Thornber md->writeset_tree_root, &key, &disk); 760eec40579SJoe Thornber if (r) { 761eec40579SJoe Thornber if (r == -ENODATA) { 762eec40579SJoe Thornber d->step = NULL; 763eec40579SJoe Thornber return 0; 764eec40579SJoe Thornber } 765eec40579SJoe Thornber 766eec40579SJoe Thornber DMERR("%s: dm_btree_lookup failed", __func__); 767eec40579SJoe Thornber return r; 768eec40579SJoe Thornber } 769eec40579SJoe Thornber 770eec40579SJoe Thornber ws_unpack(&disk, &d->writeset); 771eec40579SJoe Thornber d->value = cpu_to_le32(key); 772eec40579SJoe Thornber 77325249333SNikos Tsironis /* 77425249333SNikos Tsironis * We initialise another bitset info to avoid any caching side effects 77525249333SNikos Tsironis * with the previous one. 77625249333SNikos Tsironis */ 77725249333SNikos Tsironis dm_disk_bitset_init(md->tm, &d->info); 77825249333SNikos Tsironis 779eec40579SJoe Thornber d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks); 780eec40579SJoe Thornber d->current_bit = 0; 781eec40579SJoe Thornber d->step = metadata_digest_transcribe_writeset; 782eec40579SJoe Thornber 783eec40579SJoe Thornber return 0; 784eec40579SJoe Thornber } 785eec40579SJoe Thornber 786eec40579SJoe Thornber static int metadata_digest_start(struct era_metadata *md, struct digest *d) 787eec40579SJoe Thornber { 788eec40579SJoe Thornber if (d->step) 789eec40579SJoe Thornber return 0; 790eec40579SJoe Thornber 791eec40579SJoe Thornber memset(d, 0, sizeof(*d)); 792eec40579SJoe Thornber d->step = metadata_digest_lookup_writeset; 793eec40579SJoe Thornber 794eec40579SJoe Thornber return 0; 795eec40579SJoe Thornber } 796eec40579SJoe Thornber 797a4a82ce3SHeinz Mauelshagen /* 798a4a82ce3SHeinz Mauelshagen *----------------------------------------------------------------- 799a4a82ce3SHeinz Mauelshagen * High level metadata interface. Target methods should use these, 800a4a82ce3SHeinz Mauelshagen * and not the lower level ones. 801a4a82ce3SHeinz Mauelshagen *----------------------------------------------------------------- 802a4a82ce3SHeinz Mauelshagen */ 803eec40579SJoe Thornber static struct era_metadata *metadata_open(struct block_device *bdev, 804eec40579SJoe Thornber sector_t block_size, 805eec40579SJoe Thornber bool may_format) 806eec40579SJoe Thornber { 807eec40579SJoe Thornber int r; 808eec40579SJoe Thornber struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL); 809eec40579SJoe Thornber 810eec40579SJoe Thornber if (!md) 811eec40579SJoe Thornber return NULL; 812eec40579SJoe Thornber 813eec40579SJoe Thornber md->bdev = bdev; 814eec40579SJoe Thornber md->block_size = block_size; 815eec40579SJoe Thornber 816eec40579SJoe Thornber md->writesets[0].md.root = INVALID_WRITESET_ROOT; 817eec40579SJoe Thornber md->writesets[1].md.root = INVALID_WRITESET_ROOT; 818eec40579SJoe Thornber md->current_writeset = &md->writesets[0]; 819eec40579SJoe Thornber 820eec40579SJoe Thornber r = create_persistent_data_objects(md, may_format); 821eec40579SJoe Thornber if (r) { 822eec40579SJoe Thornber kfree(md); 823eec40579SJoe Thornber return ERR_PTR(r); 824eec40579SJoe Thornber } 825eec40579SJoe Thornber 826eec40579SJoe Thornber return md; 827eec40579SJoe Thornber } 828eec40579SJoe Thornber 829eec40579SJoe Thornber static void metadata_close(struct era_metadata *md) 830eec40579SJoe Thornber { 831904e6b26SNikos Tsironis writeset_free(&md->writesets[0]); 832904e6b26SNikos Tsironis writeset_free(&md->writesets[1]); 833eec40579SJoe Thornber destroy_persistent_data_objects(md); 834eec40579SJoe Thornber kfree(md); 835eec40579SJoe Thornber } 836eec40579SJoe Thornber 837eec40579SJoe Thornber static bool valid_nr_blocks(dm_block_t n) 838eec40579SJoe Thornber { 839eec40579SJoe Thornber /* 840eec40579SJoe Thornber * dm_bitset restricts us to 2^32. test_bit & co. restrict us 841eec40579SJoe Thornber * further to 2^31 - 1 842eec40579SJoe Thornber */ 843eec40579SJoe Thornber return n < (1ull << 31); 844eec40579SJoe Thornber } 845eec40579SJoe Thornber 846eec40579SJoe Thornber static int metadata_resize(struct era_metadata *md, void *arg) 847eec40579SJoe Thornber { 848eec40579SJoe Thornber int r; 849eec40579SJoe Thornber dm_block_t *new_size = arg; 850eec40579SJoe Thornber __le32 value; 851eec40579SJoe Thornber 852eec40579SJoe Thornber if (!valid_nr_blocks(*new_size)) { 853eec40579SJoe Thornber DMERR("Invalid number of origin blocks %llu", 854eec40579SJoe Thornber (unsigned long long) *new_size); 855eec40579SJoe Thornber return -EINVAL; 856eec40579SJoe Thornber } 857eec40579SJoe Thornber 858eec40579SJoe Thornber writeset_free(&md->writesets[0]); 859eec40579SJoe Thornber writeset_free(&md->writesets[1]); 860eec40579SJoe Thornber 861eec40579SJoe Thornber r = writeset_alloc(&md->writesets[0], *new_size); 862eec40579SJoe Thornber if (r) { 863eec40579SJoe Thornber DMERR("%s: writeset_alloc failed for writeset 0", __func__); 864eec40579SJoe Thornber return r; 865eec40579SJoe Thornber } 866eec40579SJoe Thornber 867eec40579SJoe Thornber r = writeset_alloc(&md->writesets[1], *new_size); 868eec40579SJoe Thornber if (r) { 869eec40579SJoe Thornber DMERR("%s: writeset_alloc failed for writeset 1", __func__); 870904e6b26SNikos Tsironis writeset_free(&md->writesets[0]); 871eec40579SJoe Thornber return r; 872eec40579SJoe Thornber } 873eec40579SJoe Thornber 874eec40579SJoe Thornber value = cpu_to_le32(0u); 875eec40579SJoe Thornber __dm_bless_for_disk(&value); 876eec40579SJoe Thornber r = dm_array_resize(&md->era_array_info, md->era_array_root, 877eec40579SJoe Thornber md->nr_blocks, *new_size, 878eec40579SJoe Thornber &value, &md->era_array_root); 879eec40579SJoe Thornber if (r) { 880eec40579SJoe Thornber DMERR("%s: dm_array_resize failed", __func__); 881904e6b26SNikos Tsironis writeset_free(&md->writesets[0]); 882904e6b26SNikos Tsironis writeset_free(&md->writesets[1]); 883eec40579SJoe Thornber return r; 884eec40579SJoe Thornber } 885eec40579SJoe Thornber 886eec40579SJoe Thornber md->nr_blocks = *new_size; 887eec40579SJoe Thornber return 0; 888eec40579SJoe Thornber } 889eec40579SJoe Thornber 890eec40579SJoe Thornber static int metadata_era_archive(struct era_metadata *md) 891eec40579SJoe Thornber { 892eec40579SJoe Thornber int r; 893eec40579SJoe Thornber uint64_t keys[1]; 894eec40579SJoe Thornber struct writeset_disk value; 895eec40579SJoe Thornber 896eec40579SJoe Thornber r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root, 897eec40579SJoe Thornber &md->current_writeset->md.root); 898eec40579SJoe Thornber if (r) { 899eec40579SJoe Thornber DMERR("%s: dm_bitset_flush failed", __func__); 900eec40579SJoe Thornber return r; 901eec40579SJoe Thornber } 902eec40579SJoe Thornber 903eec40579SJoe Thornber ws_pack(&md->current_writeset->md, &value); 904eec40579SJoe Thornber 905eec40579SJoe Thornber keys[0] = md->current_era; 906eec40579SJoe Thornber __dm_bless_for_disk(&value); 907eec40579SJoe Thornber r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root, 908eec40579SJoe Thornber keys, &value, &md->writeset_tree_root); 909eec40579SJoe Thornber if (r) { 910eec40579SJoe Thornber DMERR("%s: couldn't insert writeset into btree", __func__); 911eec40579SJoe Thornber /* FIXME: fail mode */ 912eec40579SJoe Thornber return r; 913eec40579SJoe Thornber } 914eec40579SJoe Thornber 915de89afc1SNikos Tsironis md->current_writeset->md.root = INVALID_WRITESET_ROOT; 916eec40579SJoe Thornber md->archived_writesets = true; 917eec40579SJoe Thornber 918eec40579SJoe Thornber return 0; 919eec40579SJoe Thornber } 920eec40579SJoe Thornber 921eec40579SJoe Thornber static struct writeset *next_writeset(struct era_metadata *md) 922eec40579SJoe Thornber { 923eec40579SJoe Thornber return (md->current_writeset == &md->writesets[0]) ? 924eec40579SJoe Thornber &md->writesets[1] : &md->writesets[0]; 925eec40579SJoe Thornber } 926eec40579SJoe Thornber 927eec40579SJoe Thornber static int metadata_new_era(struct era_metadata *md) 928eec40579SJoe Thornber { 929eec40579SJoe Thornber int r; 930eec40579SJoe Thornber struct writeset *new_writeset = next_writeset(md); 931eec40579SJoe Thornber 932de89afc1SNikos Tsironis r = writeset_init(&md->bitset_info, new_writeset, md->nr_blocks); 933eec40579SJoe Thornber if (r) { 934eec40579SJoe Thornber DMERR("%s: writeset_init failed", __func__); 935eec40579SJoe Thornber return r; 936eec40579SJoe Thornber } 937eec40579SJoe Thornber 938eec40579SJoe Thornber swap_writeset(md, new_writeset); 939eec40579SJoe Thornber md->current_era++; 940eec40579SJoe Thornber 941eec40579SJoe Thornber return 0; 942eec40579SJoe Thornber } 943eec40579SJoe Thornber 944eec40579SJoe Thornber static int metadata_era_rollover(struct era_metadata *md) 945eec40579SJoe Thornber { 946eec40579SJoe Thornber int r; 947eec40579SJoe Thornber 948eec40579SJoe Thornber if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) { 949eec40579SJoe Thornber r = metadata_era_archive(md); 950eec40579SJoe Thornber if (r) { 951eec40579SJoe Thornber DMERR("%s: metadata_archive_era failed", __func__); 952eec40579SJoe Thornber /* FIXME: fail mode? */ 953eec40579SJoe Thornber return r; 954eec40579SJoe Thornber } 955eec40579SJoe Thornber } 956eec40579SJoe Thornber 957eec40579SJoe Thornber r = metadata_new_era(md); 958eec40579SJoe Thornber if (r) { 959eec40579SJoe Thornber DMERR("%s: new era failed", __func__); 960eec40579SJoe Thornber /* FIXME: fail mode */ 961eec40579SJoe Thornber return r; 962eec40579SJoe Thornber } 963eec40579SJoe Thornber 964eec40579SJoe Thornber return 0; 965eec40579SJoe Thornber } 966eec40579SJoe Thornber 967eec40579SJoe Thornber static bool metadata_current_marked(struct era_metadata *md, dm_block_t block) 968eec40579SJoe Thornber { 969eec40579SJoe Thornber bool r; 970eec40579SJoe Thornber struct writeset *ws; 971eec40579SJoe Thornber 972eec40579SJoe Thornber rcu_read_lock(); 973eec40579SJoe Thornber ws = rcu_dereference(md->current_writeset); 974eec40579SJoe Thornber r = writeset_marked(ws, block); 975eec40579SJoe Thornber rcu_read_unlock(); 976eec40579SJoe Thornber 977eec40579SJoe Thornber return r; 978eec40579SJoe Thornber } 979eec40579SJoe Thornber 980eec40579SJoe Thornber static int metadata_commit(struct era_metadata *md) 981eec40579SJoe Thornber { 982eec40579SJoe Thornber int r; 983eec40579SJoe Thornber struct dm_block *sblock; 984eec40579SJoe Thornber 985de89afc1SNikos Tsironis if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) { 986eec40579SJoe Thornber r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root, 987eec40579SJoe Thornber &md->current_writeset->md.root); 988eec40579SJoe Thornber if (r) { 989eec40579SJoe Thornber DMERR("%s: bitset flush failed", __func__); 990eec40579SJoe Thornber return r; 991eec40579SJoe Thornber } 992eec40579SJoe Thornber } 993eec40579SJoe Thornber 994eec40579SJoe Thornber r = dm_tm_pre_commit(md->tm); 995eec40579SJoe Thornber if (r) { 996eec40579SJoe Thornber DMERR("%s: pre commit failed", __func__); 997eec40579SJoe Thornber return r; 998eec40579SJoe Thornber } 999eec40579SJoe Thornber 1000117aceb0SSomasundaram Krishnasamy r = save_sm_root(md); 1001117aceb0SSomasundaram Krishnasamy if (r) { 1002117aceb0SSomasundaram Krishnasamy DMERR("%s: save_sm_root failed", __func__); 1003117aceb0SSomasundaram Krishnasamy return r; 1004117aceb0SSomasundaram Krishnasamy } 1005117aceb0SSomasundaram Krishnasamy 1006eec40579SJoe Thornber r = superblock_lock(md, &sblock); 1007eec40579SJoe Thornber if (r) { 1008eec40579SJoe Thornber DMERR("%s: superblock lock failed", __func__); 1009eec40579SJoe Thornber return r; 1010eec40579SJoe Thornber } 1011eec40579SJoe Thornber 10125a32083dSJoe Thornber prepare_superblock(md, dm_block_data(sblock)); 1013eec40579SJoe Thornber 1014eec40579SJoe Thornber return dm_tm_commit(md->tm, sblock); 1015eec40579SJoe Thornber } 1016eec40579SJoe Thornber 1017eec40579SJoe Thornber static int metadata_checkpoint(struct era_metadata *md) 1018eec40579SJoe Thornber { 1019eec40579SJoe Thornber /* 1020eec40579SJoe Thornber * For now we just rollover, but later I want to put a check in to 1021eec40579SJoe Thornber * avoid this if the filter is still pretty fresh. 1022eec40579SJoe Thornber */ 1023eec40579SJoe Thornber return metadata_era_rollover(md); 1024eec40579SJoe Thornber } 1025eec40579SJoe Thornber 1026eec40579SJoe Thornber /* 1027eec40579SJoe Thornber * Metadata snapshots allow userland to access era data. 1028eec40579SJoe Thornber */ 1029eec40579SJoe Thornber static int metadata_take_snap(struct era_metadata *md) 1030eec40579SJoe Thornber { 1031eec40579SJoe Thornber int r, inc; 1032eec40579SJoe Thornber struct dm_block *clone; 1033eec40579SJoe Thornber 1034eec40579SJoe Thornber if (md->metadata_snap != SUPERBLOCK_LOCATION) { 1035eec40579SJoe Thornber DMERR("%s: metadata snapshot already exists", __func__); 1036eec40579SJoe Thornber return -EINVAL; 1037eec40579SJoe Thornber } 1038eec40579SJoe Thornber 1039eec40579SJoe Thornber r = metadata_era_rollover(md); 1040eec40579SJoe Thornber if (r) { 1041eec40579SJoe Thornber DMERR("%s: era rollover failed", __func__); 1042eec40579SJoe Thornber return r; 1043eec40579SJoe Thornber } 1044eec40579SJoe Thornber 1045eec40579SJoe Thornber r = metadata_commit(md); 1046eec40579SJoe Thornber if (r) { 1047eec40579SJoe Thornber DMERR("%s: pre commit failed", __func__); 1048eec40579SJoe Thornber return r; 1049eec40579SJoe Thornber } 1050eec40579SJoe Thornber 1051eec40579SJoe Thornber r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION); 1052eec40579SJoe Thornber if (r) { 1053eec40579SJoe Thornber DMERR("%s: couldn't increment superblock", __func__); 1054eec40579SJoe Thornber return r; 1055eec40579SJoe Thornber } 1056eec40579SJoe Thornber 1057eec40579SJoe Thornber r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION, 1058eec40579SJoe Thornber &sb_validator, &clone, &inc); 1059eec40579SJoe Thornber if (r) { 1060eec40579SJoe Thornber DMERR("%s: couldn't shadow superblock", __func__); 1061eec40579SJoe Thornber dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION); 1062eec40579SJoe Thornber return r; 1063eec40579SJoe Thornber } 1064eec40579SJoe Thornber BUG_ON(!inc); 1065eec40579SJoe Thornber 1066eec40579SJoe Thornber r = dm_sm_inc_block(md->sm, md->writeset_tree_root); 1067eec40579SJoe Thornber if (r) { 1068eec40579SJoe Thornber DMERR("%s: couldn't inc writeset tree root", __func__); 1069eec40579SJoe Thornber dm_tm_unlock(md->tm, clone); 1070eec40579SJoe Thornber return r; 1071eec40579SJoe Thornber } 1072eec40579SJoe Thornber 1073eec40579SJoe Thornber r = dm_sm_inc_block(md->sm, md->era_array_root); 1074eec40579SJoe Thornber if (r) { 1075eec40579SJoe Thornber DMERR("%s: couldn't inc era tree root", __func__); 1076eec40579SJoe Thornber dm_sm_dec_block(md->sm, md->writeset_tree_root); 1077eec40579SJoe Thornber dm_tm_unlock(md->tm, clone); 1078eec40579SJoe Thornber return r; 1079eec40579SJoe Thornber } 1080eec40579SJoe Thornber 1081eec40579SJoe Thornber md->metadata_snap = dm_block_location(clone); 1082eec40579SJoe Thornber 10834c7da06fSMikulas Patocka dm_tm_unlock(md->tm, clone); 1084eec40579SJoe Thornber 1085eec40579SJoe Thornber return 0; 1086eec40579SJoe Thornber } 1087eec40579SJoe Thornber 1088eec40579SJoe Thornber static int metadata_drop_snap(struct era_metadata *md) 1089eec40579SJoe Thornber { 1090eec40579SJoe Thornber int r; 1091eec40579SJoe Thornber dm_block_t location; 1092eec40579SJoe Thornber struct dm_block *clone; 1093eec40579SJoe Thornber struct superblock_disk *disk; 1094eec40579SJoe Thornber 1095eec40579SJoe Thornber if (md->metadata_snap == SUPERBLOCK_LOCATION) { 1096eec40579SJoe Thornber DMERR("%s: no snap to drop", __func__); 1097eec40579SJoe Thornber return -EINVAL; 1098eec40579SJoe Thornber } 1099eec40579SJoe Thornber 1100eec40579SJoe Thornber r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone); 1101eec40579SJoe Thornber if (r) { 1102eec40579SJoe Thornber DMERR("%s: couldn't read lock superblock clone", __func__); 1103eec40579SJoe Thornber return r; 1104eec40579SJoe Thornber } 1105eec40579SJoe Thornber 1106eec40579SJoe Thornber /* 1107eec40579SJoe Thornber * Whatever happens now we'll commit with no record of the metadata 1108eec40579SJoe Thornber * snap. 1109eec40579SJoe Thornber */ 1110eec40579SJoe Thornber md->metadata_snap = SUPERBLOCK_LOCATION; 1111eec40579SJoe Thornber 1112eec40579SJoe Thornber disk = dm_block_data(clone); 1113eec40579SJoe Thornber r = dm_btree_del(&md->writeset_tree_info, 1114eec40579SJoe Thornber le64_to_cpu(disk->writeset_tree_root)); 1115eec40579SJoe Thornber if (r) { 1116eec40579SJoe Thornber DMERR("%s: error deleting writeset tree clone", __func__); 1117eec40579SJoe Thornber dm_tm_unlock(md->tm, clone); 1118eec40579SJoe Thornber return r; 1119eec40579SJoe Thornber } 1120eec40579SJoe Thornber 1121eec40579SJoe Thornber r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root)); 1122eec40579SJoe Thornber if (r) { 1123eec40579SJoe Thornber DMERR("%s: error deleting era array clone", __func__); 1124eec40579SJoe Thornber dm_tm_unlock(md->tm, clone); 1125eec40579SJoe Thornber return r; 1126eec40579SJoe Thornber } 1127eec40579SJoe Thornber 1128eec40579SJoe Thornber location = dm_block_location(clone); 1129eec40579SJoe Thornber dm_tm_unlock(md->tm, clone); 1130eec40579SJoe Thornber 1131eec40579SJoe Thornber return dm_sm_dec_block(md->sm, location); 1132eec40579SJoe Thornber } 1133eec40579SJoe Thornber 1134eec40579SJoe Thornber struct metadata_stats { 1135eec40579SJoe Thornber dm_block_t used; 1136eec40579SJoe Thornber dm_block_t total; 1137eec40579SJoe Thornber dm_block_t snap; 1138eec40579SJoe Thornber uint32_t era; 1139eec40579SJoe Thornber }; 1140eec40579SJoe Thornber 1141eec40579SJoe Thornber static int metadata_get_stats(struct era_metadata *md, void *ptr) 1142eec40579SJoe Thornber { 1143eec40579SJoe Thornber int r; 1144eec40579SJoe Thornber struct metadata_stats *s = ptr; 1145eec40579SJoe Thornber dm_block_t nr_free, nr_total; 1146eec40579SJoe Thornber 1147eec40579SJoe Thornber r = dm_sm_get_nr_free(md->sm, &nr_free); 1148eec40579SJoe Thornber if (r) { 1149eec40579SJoe Thornber DMERR("dm_sm_get_nr_free returned %d", r); 1150eec40579SJoe Thornber return r; 1151eec40579SJoe Thornber } 1152eec40579SJoe Thornber 1153eec40579SJoe Thornber r = dm_sm_get_nr_blocks(md->sm, &nr_total); 1154eec40579SJoe Thornber if (r) { 1155eec40579SJoe Thornber DMERR("dm_pool_get_metadata_dev_size returned %d", r); 1156eec40579SJoe Thornber return r; 1157eec40579SJoe Thornber } 1158eec40579SJoe Thornber 1159eec40579SJoe Thornber s->used = nr_total - nr_free; 1160eec40579SJoe Thornber s->total = nr_total; 1161eec40579SJoe Thornber s->snap = md->metadata_snap; 1162eec40579SJoe Thornber s->era = md->current_era; 1163eec40579SJoe Thornber 1164eec40579SJoe Thornber return 0; 1165eec40579SJoe Thornber } 1166eec40579SJoe Thornber 1167eec40579SJoe Thornber /*----------------------------------------------------------------*/ 1168eec40579SJoe Thornber 1169eec40579SJoe Thornber struct era { 1170eec40579SJoe Thornber struct dm_target *ti; 1171eec40579SJoe Thornber 1172eec40579SJoe Thornber struct dm_dev *metadata_dev; 1173eec40579SJoe Thornber struct dm_dev *origin_dev; 1174eec40579SJoe Thornber 1175eec40579SJoe Thornber dm_block_t nr_blocks; 1176eec40579SJoe Thornber uint32_t sectors_per_block; 1177eec40579SJoe Thornber int sectors_per_block_shift; 1178eec40579SJoe Thornber struct era_metadata *md; 1179eec40579SJoe Thornber 1180eec40579SJoe Thornber struct workqueue_struct *wq; 1181eec40579SJoe Thornber struct work_struct worker; 1182eec40579SJoe Thornber 1183eec40579SJoe Thornber spinlock_t deferred_lock; 1184eec40579SJoe Thornber struct bio_list deferred_bios; 1185eec40579SJoe Thornber 1186eec40579SJoe Thornber spinlock_t rpc_lock; 1187eec40579SJoe Thornber struct list_head rpc_calls; 1188eec40579SJoe Thornber 1189eec40579SJoe Thornber struct digest digest; 1190eec40579SJoe Thornber atomic_t suspended; 1191eec40579SJoe Thornber }; 1192eec40579SJoe Thornber 1193eec40579SJoe Thornber struct rpc { 1194eec40579SJoe Thornber struct list_head list; 1195eec40579SJoe Thornber 1196*02f10ba1SHeinz Mauelshagen int (*fn0)(struct era_metadata *md); 1197*02f10ba1SHeinz Mauelshagen int (*fn1)(struct era_metadata *md, void *ref); 1198eec40579SJoe Thornber void *arg; 1199eec40579SJoe Thornber int result; 1200eec40579SJoe Thornber 1201eec40579SJoe Thornber struct completion complete; 1202eec40579SJoe Thornber }; 1203eec40579SJoe Thornber 1204a4a82ce3SHeinz Mauelshagen /* 1205a4a82ce3SHeinz Mauelshagen *--------------------------------------------------------------- 1206eec40579SJoe Thornber * Remapping. 1207a4a82ce3SHeinz Mauelshagen *--------------------------------------------------------------- 1208a4a82ce3SHeinz Mauelshagen */ 1209eec40579SJoe Thornber static bool block_size_is_power_of_two(struct era *era) 1210eec40579SJoe Thornber { 1211eec40579SJoe Thornber return era->sectors_per_block_shift >= 0; 1212eec40579SJoe Thornber } 1213eec40579SJoe Thornber 1214eec40579SJoe Thornber static dm_block_t get_block(struct era *era, struct bio *bio) 1215eec40579SJoe Thornber { 1216eec40579SJoe Thornber sector_t block_nr = bio->bi_iter.bi_sector; 1217eec40579SJoe Thornber 1218eec40579SJoe Thornber if (!block_size_is_power_of_two(era)) 1219eec40579SJoe Thornber (void) sector_div(block_nr, era->sectors_per_block); 1220eec40579SJoe Thornber else 1221eec40579SJoe Thornber block_nr >>= era->sectors_per_block_shift; 1222eec40579SJoe Thornber 1223eec40579SJoe Thornber return block_nr; 1224eec40579SJoe Thornber } 1225eec40579SJoe Thornber 1226eec40579SJoe Thornber static void remap_to_origin(struct era *era, struct bio *bio) 1227eec40579SJoe Thornber { 122874d46992SChristoph Hellwig bio_set_dev(bio, era->origin_dev->bdev); 1229eec40579SJoe Thornber } 1230eec40579SJoe Thornber 1231a4a82ce3SHeinz Mauelshagen /* 1232a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 1233eec40579SJoe Thornber * Worker thread 1234a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 1235a4a82ce3SHeinz Mauelshagen */ 1236eec40579SJoe Thornber static void wake_worker(struct era *era) 1237eec40579SJoe Thornber { 1238eec40579SJoe Thornber if (!atomic_read(&era->suspended)) 1239eec40579SJoe Thornber queue_work(era->wq, &era->worker); 1240eec40579SJoe Thornber } 1241eec40579SJoe Thornber 1242eec40579SJoe Thornber static void process_old_eras(struct era *era) 1243eec40579SJoe Thornber { 1244eec40579SJoe Thornber int r; 1245eec40579SJoe Thornber 1246eec40579SJoe Thornber if (!era->digest.step) 1247eec40579SJoe Thornber return; 1248eec40579SJoe Thornber 1249eec40579SJoe Thornber r = era->digest.step(era->md, &era->digest); 1250eec40579SJoe Thornber if (r < 0) { 1251eec40579SJoe Thornber DMERR("%s: digest step failed, stopping digestion", __func__); 1252eec40579SJoe Thornber era->digest.step = NULL; 1253eec40579SJoe Thornber 1254eec40579SJoe Thornber } else if (era->digest.step) 1255eec40579SJoe Thornber wake_worker(era); 1256eec40579SJoe Thornber } 1257eec40579SJoe Thornber 1258eec40579SJoe Thornber static void process_deferred_bios(struct era *era) 1259eec40579SJoe Thornber { 1260eec40579SJoe Thornber int r; 1261eec40579SJoe Thornber struct bio_list deferred_bios, marked_bios; 1262eec40579SJoe Thornber struct bio *bio; 12632099b145SNikos Tsironis struct blk_plug plug; 1264eec40579SJoe Thornber bool commit_needed = false; 1265eec40579SJoe Thornber bool failed = false; 12662099b145SNikos Tsironis struct writeset *ws = era->md->current_writeset; 1267eec40579SJoe Thornber 1268eec40579SJoe Thornber bio_list_init(&deferred_bios); 1269eec40579SJoe Thornber bio_list_init(&marked_bios); 1270eec40579SJoe Thornber 1271eec40579SJoe Thornber spin_lock(&era->deferred_lock); 1272eec40579SJoe Thornber bio_list_merge(&deferred_bios, &era->deferred_bios); 1273eec40579SJoe Thornber bio_list_init(&era->deferred_bios); 1274eec40579SJoe Thornber spin_unlock(&era->deferred_lock); 1275eec40579SJoe Thornber 12762099b145SNikos Tsironis if (bio_list_empty(&deferred_bios)) 12772099b145SNikos Tsironis return; 12782099b145SNikos Tsironis 1279eec40579SJoe Thornber while ((bio = bio_list_pop(&deferred_bios))) { 12802099b145SNikos Tsironis r = writeset_test_and_set(&era->md->bitset_info, ws, 1281eec40579SJoe Thornber get_block(era, bio)); 1282eec40579SJoe Thornber if (r < 0) { 1283eec40579SJoe Thornber /* 1284eec40579SJoe Thornber * This is bad news, we need to rollback. 1285eec40579SJoe Thornber * FIXME: finish. 1286eec40579SJoe Thornber */ 1287eec40579SJoe Thornber failed = true; 1288eec40579SJoe Thornber } else if (r == 0) 1289eec40579SJoe Thornber commit_needed = true; 1290eec40579SJoe Thornber 1291eec40579SJoe Thornber bio_list_add(&marked_bios, bio); 1292eec40579SJoe Thornber } 1293eec40579SJoe Thornber 1294eec40579SJoe Thornber if (commit_needed) { 1295eec40579SJoe Thornber r = metadata_commit(era->md); 1296eec40579SJoe Thornber if (r) 1297eec40579SJoe Thornber failed = true; 1298eec40579SJoe Thornber } 1299eec40579SJoe Thornber 1300eec40579SJoe Thornber if (failed) 1301eec40579SJoe Thornber while ((bio = bio_list_pop(&marked_bios))) 1302eec40579SJoe Thornber bio_io_error(bio); 13032099b145SNikos Tsironis else { 13042099b145SNikos Tsironis blk_start_plug(&plug); 13052099b145SNikos Tsironis while ((bio = bio_list_pop(&marked_bios))) { 13062099b145SNikos Tsironis /* 13072099b145SNikos Tsironis * Only update the in-core writeset if the on-disk one 13082099b145SNikos Tsironis * was updated too. 13092099b145SNikos Tsironis */ 13102099b145SNikos Tsironis if (commit_needed) 13112099b145SNikos Tsironis set_bit(get_block(era, bio), ws->bits); 1312ed00aabdSChristoph Hellwig submit_bio_noacct(bio); 1313eec40579SJoe Thornber } 13142099b145SNikos Tsironis blk_finish_plug(&plug); 13152099b145SNikos Tsironis } 13162099b145SNikos Tsironis } 1317eec40579SJoe Thornber 1318eec40579SJoe Thornber static void process_rpc_calls(struct era *era) 1319eec40579SJoe Thornber { 1320eec40579SJoe Thornber int r; 1321eec40579SJoe Thornber bool need_commit = false; 1322eec40579SJoe Thornber struct list_head calls; 1323eec40579SJoe Thornber struct rpc *rpc, *tmp; 1324eec40579SJoe Thornber 1325eec40579SJoe Thornber INIT_LIST_HEAD(&calls); 1326eec40579SJoe Thornber spin_lock(&era->rpc_lock); 1327eec40579SJoe Thornber list_splice_init(&era->rpc_calls, &calls); 1328eec40579SJoe Thornber spin_unlock(&era->rpc_lock); 1329eec40579SJoe Thornber 1330eec40579SJoe Thornber list_for_each_entry_safe(rpc, tmp, &calls, list) { 1331eec40579SJoe Thornber rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg); 1332eec40579SJoe Thornber need_commit = true; 1333eec40579SJoe Thornber } 1334eec40579SJoe Thornber 1335eec40579SJoe Thornber if (need_commit) { 1336eec40579SJoe Thornber r = metadata_commit(era->md); 1337eec40579SJoe Thornber if (r) 1338eec40579SJoe Thornber list_for_each_entry_safe(rpc, tmp, &calls, list) 1339eec40579SJoe Thornber rpc->result = r; 1340eec40579SJoe Thornber } 1341eec40579SJoe Thornber 1342eec40579SJoe Thornber list_for_each_entry_safe(rpc, tmp, &calls, list) 1343eec40579SJoe Thornber complete(&rpc->complete); 1344eec40579SJoe Thornber } 1345eec40579SJoe Thornber 1346eec40579SJoe Thornber static void kick_off_digest(struct era *era) 1347eec40579SJoe Thornber { 1348eec40579SJoe Thornber if (era->md->archived_writesets) { 1349eec40579SJoe Thornber era->md->archived_writesets = false; 1350eec40579SJoe Thornber metadata_digest_start(era->md, &era->digest); 1351eec40579SJoe Thornber } 1352eec40579SJoe Thornber } 1353eec40579SJoe Thornber 1354eec40579SJoe Thornber static void do_work(struct work_struct *ws) 1355eec40579SJoe Thornber { 1356eec40579SJoe Thornber struct era *era = container_of(ws, struct era, worker); 1357eec40579SJoe Thornber 1358eec40579SJoe Thornber kick_off_digest(era); 1359eec40579SJoe Thornber process_old_eras(era); 1360eec40579SJoe Thornber process_deferred_bios(era); 1361eec40579SJoe Thornber process_rpc_calls(era); 1362eec40579SJoe Thornber } 1363eec40579SJoe Thornber 1364eec40579SJoe Thornber static void defer_bio(struct era *era, struct bio *bio) 1365eec40579SJoe Thornber { 1366eec40579SJoe Thornber spin_lock(&era->deferred_lock); 1367eec40579SJoe Thornber bio_list_add(&era->deferred_bios, bio); 1368eec40579SJoe Thornber spin_unlock(&era->deferred_lock); 1369eec40579SJoe Thornber 1370eec40579SJoe Thornber wake_worker(era); 1371eec40579SJoe Thornber } 1372eec40579SJoe Thornber 1373eec40579SJoe Thornber /* 1374eec40579SJoe Thornber * Make an rpc call to the worker to change the metadata. 1375eec40579SJoe Thornber */ 1376eec40579SJoe Thornber static int perform_rpc(struct era *era, struct rpc *rpc) 1377eec40579SJoe Thornber { 1378eec40579SJoe Thornber rpc->result = 0; 1379eec40579SJoe Thornber init_completion(&rpc->complete); 1380eec40579SJoe Thornber 1381eec40579SJoe Thornber spin_lock(&era->rpc_lock); 1382eec40579SJoe Thornber list_add(&rpc->list, &era->rpc_calls); 1383eec40579SJoe Thornber spin_unlock(&era->rpc_lock); 1384eec40579SJoe Thornber 1385eec40579SJoe Thornber wake_worker(era); 1386eec40579SJoe Thornber wait_for_completion(&rpc->complete); 1387eec40579SJoe Thornber 1388eec40579SJoe Thornber return rpc->result; 1389eec40579SJoe Thornber } 1390eec40579SJoe Thornber 1391*02f10ba1SHeinz Mauelshagen static int in_worker0(struct era *era, int (*fn)(struct era_metadata *md)) 1392eec40579SJoe Thornber { 1393eec40579SJoe Thornber struct rpc rpc; 1394eec40579SJoe Thornber rpc.fn0 = fn; 1395eec40579SJoe Thornber rpc.fn1 = NULL; 1396eec40579SJoe Thornber 1397eec40579SJoe Thornber return perform_rpc(era, &rpc); 1398eec40579SJoe Thornber } 1399eec40579SJoe Thornber 1400eec40579SJoe Thornber static int in_worker1(struct era *era, 1401*02f10ba1SHeinz Mauelshagen int (*fn)(struct era_metadata *md, void *ref), void *arg) 1402eec40579SJoe Thornber { 1403eec40579SJoe Thornber struct rpc rpc; 1404eec40579SJoe Thornber rpc.fn0 = NULL; 1405eec40579SJoe Thornber rpc.fn1 = fn; 1406eec40579SJoe Thornber rpc.arg = arg; 1407eec40579SJoe Thornber 1408eec40579SJoe Thornber return perform_rpc(era, &rpc); 1409eec40579SJoe Thornber } 1410eec40579SJoe Thornber 1411eec40579SJoe Thornber static void start_worker(struct era *era) 1412eec40579SJoe Thornber { 1413eec40579SJoe Thornber atomic_set(&era->suspended, 0); 1414eec40579SJoe Thornber } 1415eec40579SJoe Thornber 1416eec40579SJoe Thornber static void stop_worker(struct era *era) 1417eec40579SJoe Thornber { 1418eec40579SJoe Thornber atomic_set(&era->suspended, 1); 14199ae6e8b1SNikos Tsironis drain_workqueue(era->wq); 1420eec40579SJoe Thornber } 1421eec40579SJoe Thornber 1422a4a82ce3SHeinz Mauelshagen /* 1423a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 1424eec40579SJoe Thornber * Target methods 1425a4a82ce3SHeinz Mauelshagen *-------------------------------------------------------------- 1426a4a82ce3SHeinz Mauelshagen */ 1427eec40579SJoe Thornber static void era_destroy(struct era *era) 1428eec40579SJoe Thornber { 1429989f26f5SJoe Thornber if (era->md) 1430eec40579SJoe Thornber metadata_close(era->md); 1431eec40579SJoe Thornber 1432eec40579SJoe Thornber if (era->wq) 1433eec40579SJoe Thornber destroy_workqueue(era->wq); 1434eec40579SJoe Thornber 1435eec40579SJoe Thornber if (era->origin_dev) 1436eec40579SJoe Thornber dm_put_device(era->ti, era->origin_dev); 1437eec40579SJoe Thornber 1438eec40579SJoe Thornber if (era->metadata_dev) 1439eec40579SJoe Thornber dm_put_device(era->ti, era->metadata_dev); 1440eec40579SJoe Thornber 1441eec40579SJoe Thornber kfree(era); 1442eec40579SJoe Thornber } 1443eec40579SJoe Thornber 1444eec40579SJoe Thornber static dm_block_t calc_nr_blocks(struct era *era) 1445eec40579SJoe Thornber { 1446eec40579SJoe Thornber return dm_sector_div_up(era->ti->len, era->sectors_per_block); 1447eec40579SJoe Thornber } 1448eec40579SJoe Thornber 1449eec40579SJoe Thornber static bool valid_block_size(dm_block_t block_size) 1450eec40579SJoe Thornber { 1451eec40579SJoe Thornber bool greater_than_zero = block_size > 0; 1452eec40579SJoe Thornber bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0; 1453eec40579SJoe Thornber 1454eec40579SJoe Thornber return greater_than_zero && multiple_of_min_block_size; 1455eec40579SJoe Thornber } 1456eec40579SJoe Thornber 1457eec40579SJoe Thornber /* 1458eec40579SJoe Thornber * <metadata dev> <data dev> <data block size (sectors)> 1459eec40579SJoe Thornber */ 146086a3238cSHeinz Mauelshagen static int era_ctr(struct dm_target *ti, unsigned int argc, char **argv) 1461eec40579SJoe Thornber { 1462eec40579SJoe Thornber int r; 1463eec40579SJoe Thornber char dummy; 1464eec40579SJoe Thornber struct era *era; 1465eec40579SJoe Thornber struct era_metadata *md; 1466eec40579SJoe Thornber 1467eec40579SJoe Thornber if (argc != 3) { 1468eec40579SJoe Thornber ti->error = "Invalid argument count"; 1469eec40579SJoe Thornber return -EINVAL; 1470eec40579SJoe Thornber } 1471eec40579SJoe Thornber 1472eec40579SJoe Thornber era = kzalloc(sizeof(*era), GFP_KERNEL); 1473eec40579SJoe Thornber if (!era) { 1474eec40579SJoe Thornber ti->error = "Error allocating era structure"; 1475eec40579SJoe Thornber return -ENOMEM; 1476eec40579SJoe Thornber } 1477eec40579SJoe Thornber 1478eec40579SJoe Thornber era->ti = ti; 1479eec40579SJoe Thornber 1480eec40579SJoe Thornber r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev); 1481eec40579SJoe Thornber if (r) { 1482eec40579SJoe Thornber ti->error = "Error opening metadata device"; 1483eec40579SJoe Thornber era_destroy(era); 1484eec40579SJoe Thornber return -EINVAL; 1485eec40579SJoe Thornber } 1486eec40579SJoe Thornber 1487eec40579SJoe Thornber r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev); 1488eec40579SJoe Thornber if (r) { 1489eec40579SJoe Thornber ti->error = "Error opening data device"; 1490eec40579SJoe Thornber era_destroy(era); 1491eec40579SJoe Thornber return -EINVAL; 1492eec40579SJoe Thornber } 1493eec40579SJoe Thornber 1494eec40579SJoe Thornber r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy); 1495eec40579SJoe Thornber if (r != 1) { 1496eec40579SJoe Thornber ti->error = "Error parsing block size"; 1497eec40579SJoe Thornber era_destroy(era); 1498eec40579SJoe Thornber return -EINVAL; 1499eec40579SJoe Thornber } 1500eec40579SJoe Thornber 1501eec40579SJoe Thornber r = dm_set_target_max_io_len(ti, era->sectors_per_block); 1502eec40579SJoe Thornber if (r) { 1503eec40579SJoe Thornber ti->error = "could not set max io len"; 1504eec40579SJoe Thornber era_destroy(era); 1505eec40579SJoe Thornber return -EINVAL; 1506eec40579SJoe Thornber } 1507eec40579SJoe Thornber 1508eec40579SJoe Thornber if (!valid_block_size(era->sectors_per_block)) { 1509eec40579SJoe Thornber ti->error = "Invalid block size"; 1510eec40579SJoe Thornber era_destroy(era); 1511eec40579SJoe Thornber return -EINVAL; 1512eec40579SJoe Thornber } 1513eec40579SJoe Thornber if (era->sectors_per_block & (era->sectors_per_block - 1)) 1514eec40579SJoe Thornber era->sectors_per_block_shift = -1; 1515eec40579SJoe Thornber else 1516eec40579SJoe Thornber era->sectors_per_block_shift = __ffs(era->sectors_per_block); 1517eec40579SJoe Thornber 1518eec40579SJoe Thornber md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true); 1519eec40579SJoe Thornber if (IS_ERR(md)) { 1520eec40579SJoe Thornber ti->error = "Error reading metadata"; 1521eec40579SJoe Thornber era_destroy(era); 1522eec40579SJoe Thornber return PTR_ERR(md); 1523eec40579SJoe Thornber } 1524eec40579SJoe Thornber era->md = md; 1525eec40579SJoe Thornber 1526eec40579SJoe Thornber era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); 1527eec40579SJoe Thornber if (!era->wq) { 1528eec40579SJoe Thornber ti->error = "could not create workqueue for metadata object"; 1529eec40579SJoe Thornber era_destroy(era); 1530eec40579SJoe Thornber return -ENOMEM; 1531eec40579SJoe Thornber } 1532eec40579SJoe Thornber INIT_WORK(&era->worker, do_work); 1533eec40579SJoe Thornber 1534eec40579SJoe Thornber spin_lock_init(&era->deferred_lock); 1535eec40579SJoe Thornber bio_list_init(&era->deferred_bios); 1536eec40579SJoe Thornber 1537eec40579SJoe Thornber spin_lock_init(&era->rpc_lock); 1538eec40579SJoe Thornber INIT_LIST_HEAD(&era->rpc_calls); 1539eec40579SJoe Thornber 1540eec40579SJoe Thornber ti->private = era; 1541eec40579SJoe Thornber ti->num_flush_bios = 1; 1542eec40579SJoe Thornber ti->flush_supported = true; 1543eec40579SJoe Thornber 1544eec40579SJoe Thornber ti->num_discard_bios = 1; 1545eec40579SJoe Thornber 1546eec40579SJoe Thornber return 0; 1547eec40579SJoe Thornber } 1548eec40579SJoe Thornber 1549eec40579SJoe Thornber static void era_dtr(struct dm_target *ti) 1550eec40579SJoe Thornber { 1551eec40579SJoe Thornber era_destroy(ti->private); 1552eec40579SJoe Thornber } 1553eec40579SJoe Thornber 1554eec40579SJoe Thornber static int era_map(struct dm_target *ti, struct bio *bio) 1555eec40579SJoe Thornber { 1556eec40579SJoe Thornber struct era *era = ti->private; 1557eec40579SJoe Thornber dm_block_t block = get_block(era, bio); 1558eec40579SJoe Thornber 1559eec40579SJoe Thornber /* 1560eec40579SJoe Thornber * All bios get remapped to the origin device. We do this now, but 1561eec40579SJoe Thornber * it may not get issued until later. Depending on whether the 1562eec40579SJoe Thornber * block is marked in this era. 1563eec40579SJoe Thornber */ 1564eec40579SJoe Thornber remap_to_origin(era, bio); 1565eec40579SJoe Thornber 1566eec40579SJoe Thornber /* 156728a8f0d3SMike Christie * REQ_PREFLUSH bios carry no data, so we're not interested in them. 1568eec40579SJoe Thornber */ 15691eff9d32SJens Axboe if (!(bio->bi_opf & REQ_PREFLUSH) && 1570eec40579SJoe Thornber (bio_data_dir(bio) == WRITE) && 1571eec40579SJoe Thornber !metadata_current_marked(era->md, block)) { 1572eec40579SJoe Thornber defer_bio(era, bio); 1573eec40579SJoe Thornber return DM_MAPIO_SUBMITTED; 1574eec40579SJoe Thornber } 1575eec40579SJoe Thornber 1576eec40579SJoe Thornber return DM_MAPIO_REMAPPED; 1577eec40579SJoe Thornber } 1578eec40579SJoe Thornber 1579eec40579SJoe Thornber static void era_postsuspend(struct dm_target *ti) 1580eec40579SJoe Thornber { 1581eec40579SJoe Thornber int r; 1582eec40579SJoe Thornber struct era *era = ti->private; 1583eec40579SJoe Thornber 1584eec40579SJoe Thornber r = in_worker0(era, metadata_era_archive); 1585eec40579SJoe Thornber if (r) { 1586eec40579SJoe Thornber DMERR("%s: couldn't archive current era", __func__); 1587eec40579SJoe Thornber /* FIXME: fail mode */ 1588eec40579SJoe Thornber } 1589eec40579SJoe Thornber 1590eec40579SJoe Thornber stop_worker(era); 15919ae6e8b1SNikos Tsironis 15929ae6e8b1SNikos Tsironis r = metadata_commit(era->md); 15939ae6e8b1SNikos Tsironis if (r) { 15949ae6e8b1SNikos Tsironis DMERR("%s: metadata_commit failed", __func__); 15959ae6e8b1SNikos Tsironis /* FIXME: fail mode */ 15969ae6e8b1SNikos Tsironis } 1597eec40579SJoe Thornber } 1598eec40579SJoe Thornber 1599eec40579SJoe Thornber static int era_preresume(struct dm_target *ti) 1600eec40579SJoe Thornber { 1601eec40579SJoe Thornber int r; 1602eec40579SJoe Thornber struct era *era = ti->private; 1603eec40579SJoe Thornber dm_block_t new_size = calc_nr_blocks(era); 1604eec40579SJoe Thornber 1605eec40579SJoe Thornber if (era->nr_blocks != new_size) { 1606cca2c6aeSNikos Tsironis r = metadata_resize(era->md, &new_size); 1607cca2c6aeSNikos Tsironis if (r) { 1608cca2c6aeSNikos Tsironis DMERR("%s: metadata_resize failed", __func__); 1609eec40579SJoe Thornber return r; 1610cca2c6aeSNikos Tsironis } 1611cca2c6aeSNikos Tsironis 1612cca2c6aeSNikos Tsironis r = metadata_commit(era->md); 1613cca2c6aeSNikos Tsironis if (r) { 1614cca2c6aeSNikos Tsironis DMERR("%s: metadata_commit failed", __func__); 1615cca2c6aeSNikos Tsironis return r; 1616cca2c6aeSNikos Tsironis } 1617eec40579SJoe Thornber 1618eec40579SJoe Thornber era->nr_blocks = new_size; 1619eec40579SJoe Thornber } 1620eec40579SJoe Thornber 1621eec40579SJoe Thornber start_worker(era); 1622eec40579SJoe Thornber 1623de89afc1SNikos Tsironis r = in_worker0(era, metadata_era_rollover); 1624eec40579SJoe Thornber if (r) { 1625eec40579SJoe Thornber DMERR("%s: metadata_era_rollover failed", __func__); 1626eec40579SJoe Thornber return r; 1627eec40579SJoe Thornber } 1628eec40579SJoe Thornber 1629eec40579SJoe Thornber return 0; 1630eec40579SJoe Thornber } 1631eec40579SJoe Thornber 1632eec40579SJoe Thornber /* 1633eec40579SJoe Thornber * Status format: 1634eec40579SJoe Thornber * 1635eec40579SJoe Thornber * <metadata block size> <#used metadata blocks>/<#total metadata blocks> 1636eec40579SJoe Thornber * <current era> <held metadata root | '-'> 1637eec40579SJoe Thornber */ 1638eec40579SJoe Thornber static void era_status(struct dm_target *ti, status_type_t type, 163986a3238cSHeinz Mauelshagen unsigned int status_flags, char *result, unsigned int maxlen) 1640eec40579SJoe Thornber { 1641eec40579SJoe Thornber int r; 1642eec40579SJoe Thornber struct era *era = ti->private; 1643eec40579SJoe Thornber ssize_t sz = 0; 1644eec40579SJoe Thornber struct metadata_stats stats; 1645eec40579SJoe Thornber char buf[BDEVNAME_SIZE]; 1646eec40579SJoe Thornber 1647eec40579SJoe Thornber switch (type) { 1648eec40579SJoe Thornber case STATUSTYPE_INFO: 1649eec40579SJoe Thornber r = in_worker1(era, metadata_get_stats, &stats); 1650eec40579SJoe Thornber if (r) 1651eec40579SJoe Thornber goto err; 1652eec40579SJoe Thornber 1653eec40579SJoe Thornber DMEMIT("%u %llu/%llu %u", 165486a3238cSHeinz Mauelshagen (unsigned int) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT), 1655eec40579SJoe Thornber (unsigned long long) stats.used, 1656eec40579SJoe Thornber (unsigned long long) stats.total, 165786a3238cSHeinz Mauelshagen (unsigned int) stats.era); 1658eec40579SJoe Thornber 1659eec40579SJoe Thornber if (stats.snap != SUPERBLOCK_LOCATION) 1660eec40579SJoe Thornber DMEMIT(" %llu", stats.snap); 1661eec40579SJoe Thornber else 1662eec40579SJoe Thornber DMEMIT(" -"); 1663eec40579SJoe Thornber break; 1664eec40579SJoe Thornber 1665eec40579SJoe Thornber case STATUSTYPE_TABLE: 1666eec40579SJoe Thornber format_dev_t(buf, era->metadata_dev->bdev->bd_dev); 1667eec40579SJoe Thornber DMEMIT("%s ", buf); 1668eec40579SJoe Thornber format_dev_t(buf, era->origin_dev->bdev->bd_dev); 1669eec40579SJoe Thornber DMEMIT("%s %u", buf, era->sectors_per_block); 1670eec40579SJoe Thornber break; 16718ec45662STushar Sugandhi 16728ec45662STushar Sugandhi case STATUSTYPE_IMA: 16738ec45662STushar Sugandhi *result = '\0'; 16748ec45662STushar Sugandhi break; 1675eec40579SJoe Thornber } 1676eec40579SJoe Thornber 1677eec40579SJoe Thornber return; 1678eec40579SJoe Thornber 1679eec40579SJoe Thornber err: 1680eec40579SJoe Thornber DMEMIT("Error"); 1681eec40579SJoe Thornber } 1682eec40579SJoe Thornber 168386a3238cSHeinz Mauelshagen static int era_message(struct dm_target *ti, unsigned int argc, char **argv, 168486a3238cSHeinz Mauelshagen char *result, unsigned int maxlen) 1685eec40579SJoe Thornber { 1686eec40579SJoe Thornber struct era *era = ti->private; 1687eec40579SJoe Thornber 1688eec40579SJoe Thornber if (argc != 1) { 1689eec40579SJoe Thornber DMERR("incorrect number of message arguments"); 1690eec40579SJoe Thornber return -EINVAL; 1691eec40579SJoe Thornber } 1692eec40579SJoe Thornber 1693eec40579SJoe Thornber if (!strcasecmp(argv[0], "checkpoint")) 1694eec40579SJoe Thornber return in_worker0(era, metadata_checkpoint); 1695eec40579SJoe Thornber 1696eec40579SJoe Thornber if (!strcasecmp(argv[0], "take_metadata_snap")) 1697eec40579SJoe Thornber return in_worker0(era, metadata_take_snap); 1698eec40579SJoe Thornber 1699eec40579SJoe Thornber if (!strcasecmp(argv[0], "drop_metadata_snap")) 1700eec40579SJoe Thornber return in_worker0(era, metadata_drop_snap); 1701eec40579SJoe Thornber 1702eec40579SJoe Thornber DMERR("unsupported message '%s'", argv[0]); 1703eec40579SJoe Thornber return -EINVAL; 1704eec40579SJoe Thornber } 1705eec40579SJoe Thornber 1706eec40579SJoe Thornber static sector_t get_dev_size(struct dm_dev *dev) 1707eec40579SJoe Thornber { 17086dcbb52cSChristoph Hellwig return bdev_nr_sectors(dev->bdev); 1709eec40579SJoe Thornber } 1710eec40579SJoe Thornber 1711eec40579SJoe Thornber static int era_iterate_devices(struct dm_target *ti, 1712eec40579SJoe Thornber iterate_devices_callout_fn fn, void *data) 1713eec40579SJoe Thornber { 1714eec40579SJoe Thornber struct era *era = ti->private; 1715eec40579SJoe Thornber return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data); 1716eec40579SJoe Thornber } 1717eec40579SJoe Thornber 1718eec40579SJoe Thornber static void era_io_hints(struct dm_target *ti, struct queue_limits *limits) 1719eec40579SJoe Thornber { 1720eec40579SJoe Thornber struct era *era = ti->private; 1721eec40579SJoe Thornber uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; 1722eec40579SJoe Thornber 1723eec40579SJoe Thornber /* 1724eec40579SJoe Thornber * If the system-determined stacked limits are compatible with the 1725eec40579SJoe Thornber * era device's blocksize (io_opt is a factor) do not override them. 1726eec40579SJoe Thornber */ 1727eec40579SJoe Thornber if (io_opt_sectors < era->sectors_per_block || 1728eec40579SJoe Thornber do_div(io_opt_sectors, era->sectors_per_block)) { 1729eec40579SJoe Thornber blk_limits_io_min(limits, 0); 1730eec40579SJoe Thornber blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT); 1731eec40579SJoe Thornber } 1732eec40579SJoe Thornber } 1733eec40579SJoe Thornber 1734eec40579SJoe Thornber /*----------------------------------------------------------------*/ 1735eec40579SJoe Thornber 1736eec40579SJoe Thornber static struct target_type era_target = { 1737eec40579SJoe Thornber .name = "era", 1738eec40579SJoe Thornber .version = {1, 0, 0}, 1739eec40579SJoe Thornber .module = THIS_MODULE, 1740eec40579SJoe Thornber .ctr = era_ctr, 1741eec40579SJoe Thornber .dtr = era_dtr, 1742eec40579SJoe Thornber .map = era_map, 1743eec40579SJoe Thornber .postsuspend = era_postsuspend, 1744eec40579SJoe Thornber .preresume = era_preresume, 1745eec40579SJoe Thornber .status = era_status, 1746eec40579SJoe Thornber .message = era_message, 1747eec40579SJoe Thornber .iterate_devices = era_iterate_devices, 1748eec40579SJoe Thornber .io_hints = era_io_hints 1749eec40579SJoe Thornber }; 1750eec40579SJoe Thornber 1751eec40579SJoe Thornber static int __init dm_era_init(void) 1752eec40579SJoe Thornber { 1753eec40579SJoe Thornber int r; 1754eec40579SJoe Thornber 1755eec40579SJoe Thornber r = dm_register_target(&era_target); 1756eec40579SJoe Thornber if (r) { 1757eec40579SJoe Thornber DMERR("era target registration failed: %d", r); 1758eec40579SJoe Thornber return r; 1759eec40579SJoe Thornber } 1760eec40579SJoe Thornber 1761eec40579SJoe Thornber return 0; 1762eec40579SJoe Thornber } 1763eec40579SJoe Thornber 1764eec40579SJoe Thornber static void __exit dm_era_exit(void) 1765eec40579SJoe Thornber { 1766eec40579SJoe Thornber dm_unregister_target(&era_target); 1767eec40579SJoe Thornber } 1768eec40579SJoe Thornber 1769eec40579SJoe Thornber module_init(dm_era_init); 1770eec40579SJoe Thornber module_exit(dm_era_exit); 1771eec40579SJoe Thornber 1772eec40579SJoe Thornber MODULE_DESCRIPTION(DM_NAME " era target"); 1773eec40579SJoe Thornber MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>"); 1774eec40579SJoe Thornber MODULE_LICENSE("GPL"); 1775