dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / md / dm-era-target.c
blob32e76c5ee74172bef19f43a23b8f1cf9e5df20bc
1 #include "dm.h"
2 #include "persistent-data/dm-transaction-manager.h"
3 #include "persistent-data/dm-bitset.h"
4 #include "persistent-data/dm-space-map.h"
6 #include <linux/dm-io.h>
7 #include <linux/dm-kcopyd.h>
8 #include <linux/init.h>
9 #include <linux/mempool.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
14 #define DM_MSG_PREFIX "era"
16 #define SUPERBLOCK_LOCATION 0
17 #define SUPERBLOCK_MAGIC 2126579579
18 #define SUPERBLOCK_CSUM_XOR 146538381
19 #define MIN_ERA_VERSION 1
20 #define MAX_ERA_VERSION 1
21 #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
22 #define MIN_BLOCK_SIZE 8
24 /*----------------------------------------------------------------
25 * Writeset
26 *--------------------------------------------------------------*/
27 struct writeset_metadata {
28 uint32_t nr_bits;
29 dm_block_t root;
32 struct writeset {
33 struct writeset_metadata md;
36 * An in core copy of the bits to save constantly doing look ups on
37 * disk.
39 unsigned long *bits;
43 * This does not free off the on disk bitset as this will normally be done
44 * after digesting into the era array.
46 static void writeset_free(struct writeset *ws)
48 vfree(ws->bits);
51 static int setup_on_disk_bitset(struct dm_disk_bitset *info,
52 unsigned nr_bits, dm_block_t *root)
54 int r;
56 r = dm_bitset_empty(info, root);
57 if (r)
58 return r;
60 return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
63 static size_t bitset_size(unsigned nr_bits)
65 return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
69 * Allocates memory for the in core bitset.
71 static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
73 ws->md.nr_bits = nr_blocks;
74 ws->md.root = INVALID_WRITESET_ROOT;
75 ws->bits = vzalloc(bitset_size(nr_blocks));
76 if (!ws->bits) {
77 DMERR("%s: couldn't allocate in memory bitset", __func__);
78 return -ENOMEM;
81 return 0;
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)
89 int r;
91 memset(ws->bits, 0, bitset_size(ws->md.nr_bits));
93 r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
94 if (r) {
95 DMERR("%s: setup_on_disk_bitset failed", __func__);
96 return r;
99 return 0;
102 static bool writeset_marked(struct writeset *ws, dm_block_t block)
104 return test_bit(block, ws->bits);
107 static int writeset_marked_on_disk(struct dm_disk_bitset *info,
108 struct writeset_metadata *m, dm_block_t block,
109 bool *result)
111 dm_block_t old = m->root;
114 * The bitset was flushed when it was archived, so we know there'll
115 * be no change to the root.
117 int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
118 if (r) {
119 DMERR("%s: dm_bitset_test_bit failed", __func__);
120 return r;
123 BUG_ON(m->root != old);
125 return r;
129 * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
131 static int writeset_test_and_set(struct dm_disk_bitset *info,
132 struct writeset *ws, uint32_t block)
134 int r;
136 if (!test_and_set_bit(block, ws->bits)) {
137 r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
138 if (r) {
139 /* FIXME: fail mode */
140 return r;
143 return 0;
146 return 1;
149 /*----------------------------------------------------------------
150 * On disk metadata layout
151 *--------------------------------------------------------------*/
152 #define SPACE_MAP_ROOT_SIZE 128
153 #define UUID_LEN 16
155 struct writeset_disk {
156 __le32 nr_bits;
157 __le64 root;
158 } __packed;
160 struct superblock_disk {
161 __le32 csum;
162 __le32 flags;
163 __le64 blocknr;
165 __u8 uuid[UUID_LEN];
166 __le64 magic;
167 __le32 version;
169 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
171 __le32 data_block_size;
172 __le32 metadata_block_size;
173 __le32 nr_blocks;
175 __le32 current_era;
176 struct writeset_disk current_writeset;
179 * Only these two fields are valid within the metadata snapshot.
181 __le64 writeset_tree_root;
182 __le64 era_array_root;
184 __le64 metadata_snap;
185 } __packed;
187 /*----------------------------------------------------------------
188 * Superblock validation
189 *--------------------------------------------------------------*/
190 static void sb_prepare_for_write(struct dm_block_validator *v,
191 struct dm_block *b,
192 size_t sb_block_size)
194 struct superblock_disk *disk = dm_block_data(b);
196 disk->blocknr = cpu_to_le64(dm_block_location(b));
197 disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
198 sb_block_size - sizeof(__le32),
199 SUPERBLOCK_CSUM_XOR));
202 static int check_metadata_version(struct superblock_disk *disk)
204 uint32_t metadata_version = le32_to_cpu(disk->version);
205 if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
206 DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
207 metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
208 return -EINVAL;
211 return 0;
214 static int sb_check(struct dm_block_validator *v,
215 struct dm_block *b,
216 size_t sb_block_size)
218 struct superblock_disk *disk = dm_block_data(b);
219 __le32 csum_le;
221 if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
222 DMERR("sb_check failed: blocknr %llu: wanted %llu",
223 le64_to_cpu(disk->blocknr),
224 (unsigned long long)dm_block_location(b));
225 return -ENOTBLK;
228 if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
229 DMERR("sb_check failed: magic %llu: wanted %llu",
230 le64_to_cpu(disk->magic),
231 (unsigned long long) SUPERBLOCK_MAGIC);
232 return -EILSEQ;
235 csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
236 sb_block_size - sizeof(__le32),
237 SUPERBLOCK_CSUM_XOR));
238 if (csum_le != disk->csum) {
239 DMERR("sb_check failed: csum %u: wanted %u",
240 le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
241 return -EILSEQ;
244 return check_metadata_version(disk);
247 static struct dm_block_validator sb_validator = {
248 .name = "superblock",
249 .prepare_for_write = sb_prepare_for_write,
250 .check = sb_check
253 /*----------------------------------------------------------------
254 * Low level metadata handling
255 *--------------------------------------------------------------*/
256 #define DM_ERA_METADATA_BLOCK_SIZE 4096
257 #define DM_ERA_METADATA_CACHE_SIZE 64
258 #define ERA_MAX_CONCURRENT_LOCKS 5
260 struct era_metadata {
261 struct block_device *bdev;
262 struct dm_block_manager *bm;
263 struct dm_space_map *sm;
264 struct dm_transaction_manager *tm;
266 dm_block_t block_size;
267 uint32_t nr_blocks;
269 uint32_t current_era;
272 * We preallocate 2 writesets. When an era rolls over we
273 * switch between them. This means the allocation is done at
274 * preresume time, rather than on the io path.
276 struct writeset writesets[2];
277 struct writeset *current_writeset;
279 dm_block_t writeset_tree_root;
280 dm_block_t era_array_root;
282 struct dm_disk_bitset bitset_info;
283 struct dm_btree_info writeset_tree_info;
284 struct dm_array_info era_array_info;
286 dm_block_t metadata_snap;
289 * A flag that is set whenever a writeset has been archived.
291 bool archived_writesets;
294 * Reading the space map root can fail, so we read it into this
295 * buffer before the superblock is locked and updated.
297 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
300 static int superblock_read_lock(struct era_metadata *md,
301 struct dm_block **sblock)
303 return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
304 &sb_validator, sblock);
307 static int superblock_lock_zero(struct era_metadata *md,
308 struct dm_block **sblock)
310 return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
311 &sb_validator, sblock);
314 static int superblock_lock(struct era_metadata *md,
315 struct dm_block **sblock)
317 return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
318 &sb_validator, sblock);
321 /* FIXME: duplication with cache and thin */
322 static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
324 int r;
325 unsigned i;
326 struct dm_block *b;
327 __le64 *data_le, zero = cpu_to_le64(0);
328 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
331 * We can't use a validator here - it may be all zeroes.
333 r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
334 if (r)
335 return r;
337 data_le = dm_block_data(b);
338 *result = true;
339 for (i = 0; i < sb_block_size; i++) {
340 if (data_le[i] != zero) {
341 *result = false;
342 break;
346 dm_bm_unlock(b);
348 return 0;
351 /*----------------------------------------------------------------*/
353 static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
355 disk->nr_bits = cpu_to_le32(core->nr_bits);
356 disk->root = cpu_to_le64(core->root);
359 static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
361 core->nr_bits = le32_to_cpu(disk->nr_bits);
362 core->root = le64_to_cpu(disk->root);
365 static void ws_inc(void *context, const void *value)
367 struct era_metadata *md = context;
368 struct writeset_disk ws_d;
369 dm_block_t b;
371 memcpy(&ws_d, value, sizeof(ws_d));
372 b = le64_to_cpu(ws_d.root);
374 dm_tm_inc(md->tm, b);
377 static void ws_dec(void *context, const void *value)
379 struct era_metadata *md = context;
380 struct writeset_disk ws_d;
381 dm_block_t b;
383 memcpy(&ws_d, value, sizeof(ws_d));
384 b = le64_to_cpu(ws_d.root);
386 dm_bitset_del(&md->bitset_info, b);
389 static int ws_eq(void *context, const void *value1, const void *value2)
391 return !memcmp(value1, value2, sizeof(struct writeset_metadata));
394 /*----------------------------------------------------------------*/
396 static void setup_writeset_tree_info(struct era_metadata *md)
398 struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
399 md->writeset_tree_info.tm = md->tm;
400 md->writeset_tree_info.levels = 1;
401 vt->context = md;
402 vt->size = sizeof(struct writeset_disk);
403 vt->inc = ws_inc;
404 vt->dec = ws_dec;
405 vt->equal = ws_eq;
408 static void setup_era_array_info(struct era_metadata *md)
411 struct dm_btree_value_type vt;
412 vt.context = NULL;
413 vt.size = sizeof(__le32);
414 vt.inc = NULL;
415 vt.dec = NULL;
416 vt.equal = NULL;
418 dm_array_info_init(&md->era_array_info, md->tm, &vt);
421 static void setup_infos(struct era_metadata *md)
423 dm_disk_bitset_init(md->tm, &md->bitset_info);
424 setup_writeset_tree_info(md);
425 setup_era_array_info(md);
428 /*----------------------------------------------------------------*/
430 static int create_fresh_metadata(struct era_metadata *md)
432 int r;
434 r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
435 &md->tm, &md->sm);
436 if (r < 0) {
437 DMERR("dm_tm_create_with_sm failed");
438 return r;
441 setup_infos(md);
443 r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
444 if (r) {
445 DMERR("couldn't create new writeset tree");
446 goto bad;
449 r = dm_array_empty(&md->era_array_info, &md->era_array_root);
450 if (r) {
451 DMERR("couldn't create era array");
452 goto bad;
455 return 0;
457 bad:
458 dm_sm_destroy(md->sm);
459 dm_tm_destroy(md->tm);
461 return r;
464 static int save_sm_root(struct era_metadata *md)
466 int r;
467 size_t metadata_len;
469 r = dm_sm_root_size(md->sm, &metadata_len);
470 if (r < 0)
471 return r;
473 return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
474 metadata_len);
477 static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
479 memcpy(&disk->metadata_space_map_root,
480 &md->metadata_space_map_root,
481 sizeof(md->metadata_space_map_root));
485 * Writes a superblock, including the static fields that don't get updated
486 * with every commit (possible optimisation here). 'md' should be fully
487 * constructed when this is called.
489 static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
491 disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
492 disk->flags = cpu_to_le32(0ul);
494 /* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
495 memset(disk->uuid, 0, sizeof(disk->uuid));
496 disk->version = cpu_to_le32(MAX_ERA_VERSION);
498 copy_sm_root(md, disk);
500 disk->data_block_size = cpu_to_le32(md->block_size);
501 disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
502 disk->nr_blocks = cpu_to_le32(md->nr_blocks);
503 disk->current_era = cpu_to_le32(md->current_era);
505 ws_pack(&md->current_writeset->md, &disk->current_writeset);
506 disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
507 disk->era_array_root = cpu_to_le64(md->era_array_root);
508 disk->metadata_snap = cpu_to_le64(md->metadata_snap);
511 static int write_superblock(struct era_metadata *md)
513 int r;
514 struct dm_block *sblock;
515 struct superblock_disk *disk;
517 r = save_sm_root(md);
518 if (r) {
519 DMERR("%s: save_sm_root failed", __func__);
520 return r;
523 r = superblock_lock_zero(md, &sblock);
524 if (r)
525 return r;
527 disk = dm_block_data(sblock);
528 prepare_superblock(md, disk);
530 return dm_tm_commit(md->tm, sblock);
534 * Assumes block_size and the infos are set.
536 static int format_metadata(struct era_metadata *md)
538 int r;
540 r = create_fresh_metadata(md);
541 if (r)
542 return r;
544 r = write_superblock(md);
545 if (r) {
546 dm_sm_destroy(md->sm);
547 dm_tm_destroy(md->tm);
548 return r;
551 return 0;
554 static int open_metadata(struct era_metadata *md)
556 int r;
557 struct dm_block *sblock;
558 struct superblock_disk *disk;
560 r = superblock_read_lock(md, &sblock);
561 if (r) {
562 DMERR("couldn't read_lock superblock");
563 return r;
566 disk = dm_block_data(sblock);
567 r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
568 disk->metadata_space_map_root,
569 sizeof(disk->metadata_space_map_root),
570 &md->tm, &md->sm);
571 if (r) {
572 DMERR("dm_tm_open_with_sm failed");
573 goto bad;
576 setup_infos(md);
578 md->block_size = le32_to_cpu(disk->data_block_size);
579 md->nr_blocks = le32_to_cpu(disk->nr_blocks);
580 md->current_era = le32_to_cpu(disk->current_era);
582 md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
583 md->era_array_root = le64_to_cpu(disk->era_array_root);
584 md->metadata_snap = le64_to_cpu(disk->metadata_snap);
585 md->archived_writesets = true;
587 dm_bm_unlock(sblock);
589 return 0;
591 bad:
592 dm_bm_unlock(sblock);
593 return r;
596 static int open_or_format_metadata(struct era_metadata *md,
597 bool may_format)
599 int r;
600 bool unformatted = false;
602 r = superblock_all_zeroes(md->bm, &unformatted);
603 if (r)
604 return r;
606 if (unformatted)
607 return may_format ? format_metadata(md) : -EPERM;
609 return open_metadata(md);
612 static int create_persistent_data_objects(struct era_metadata *md,
613 bool may_format)
615 int r;
617 md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
618 DM_ERA_METADATA_CACHE_SIZE,
619 ERA_MAX_CONCURRENT_LOCKS);
620 if (IS_ERR(md->bm)) {
621 DMERR("could not create block manager");
622 return PTR_ERR(md->bm);
625 r = open_or_format_metadata(md, may_format);
626 if (r)
627 dm_block_manager_destroy(md->bm);
629 return r;
632 static void destroy_persistent_data_objects(struct era_metadata *md)
634 dm_sm_destroy(md->sm);
635 dm_tm_destroy(md->tm);
636 dm_block_manager_destroy(md->bm);
640 * This waits until all era_map threads have picked up the new filter.
642 static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
644 rcu_assign_pointer(md->current_writeset, new_writeset);
645 synchronize_rcu();
648 /*----------------------------------------------------------------
649 * Writesets get 'digested' into the main era array.
651 * We're using a coroutine here so the worker thread can do the digestion,
652 * thus avoiding synchronisation of the metadata. Digesting a whole
653 * writeset in one go would cause too much latency.
654 *--------------------------------------------------------------*/
655 struct digest {
656 uint32_t era;
657 unsigned nr_bits, current_bit;
658 struct writeset_metadata writeset;
659 __le32 value;
660 struct dm_disk_bitset info;
662 int (*step)(struct era_metadata *, struct digest *);
665 static int metadata_digest_lookup_writeset(struct era_metadata *md,
666 struct digest *d);
668 static int metadata_digest_remove_writeset(struct era_metadata *md,
669 struct digest *d)
671 int r;
672 uint64_t key = d->era;
674 r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
675 &key, &md->writeset_tree_root);
676 if (r) {
677 DMERR("%s: dm_btree_remove failed", __func__);
678 return r;
681 d->step = metadata_digest_lookup_writeset;
682 return 0;
685 #define INSERTS_PER_STEP 100
687 static int metadata_digest_transcribe_writeset(struct era_metadata *md,
688 struct digest *d)
690 int r;
691 bool marked;
692 unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
694 for (b = d->current_bit; b < e; b++) {
695 r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
696 if (r) {
697 DMERR("%s: writeset_marked_on_disk failed", __func__);
698 return r;
701 if (!marked)
702 continue;
704 __dm_bless_for_disk(&d->value);
705 r = dm_array_set_value(&md->era_array_info, md->era_array_root,
706 b, &d->value, &md->era_array_root);
707 if (r) {
708 DMERR("%s: dm_array_set_value failed", __func__);
709 return r;
713 if (b == d->nr_bits)
714 d->step = metadata_digest_remove_writeset;
715 else
716 d->current_bit = b;
718 return 0;
721 static int metadata_digest_lookup_writeset(struct era_metadata *md,
722 struct digest *d)
724 int r;
725 uint64_t key;
726 struct writeset_disk disk;
728 r = dm_btree_find_lowest_key(&md->writeset_tree_info,
729 md->writeset_tree_root, &key);
730 if (r < 0)
731 return r;
733 d->era = key;
735 r = dm_btree_lookup(&md->writeset_tree_info,
736 md->writeset_tree_root, &key, &disk);
737 if (r) {
738 if (r == -ENODATA) {
739 d->step = NULL;
740 return 0;
743 DMERR("%s: dm_btree_lookup failed", __func__);
744 return r;
747 ws_unpack(&disk, &d->writeset);
748 d->value = cpu_to_le32(key);
750 d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
751 d->current_bit = 0;
752 d->step = metadata_digest_transcribe_writeset;
754 return 0;
757 static int metadata_digest_start(struct era_metadata *md, struct digest *d)
759 if (d->step)
760 return 0;
762 memset(d, 0, sizeof(*d));
765 * We initialise another bitset info to avoid any caching side
766 * effects with the previous one.
768 dm_disk_bitset_init(md->tm, &d->info);
769 d->step = metadata_digest_lookup_writeset;
771 return 0;
774 /*----------------------------------------------------------------
775 * High level metadata interface. Target methods should use these, and not
776 * the lower level ones.
777 *--------------------------------------------------------------*/
778 static struct era_metadata *metadata_open(struct block_device *bdev,
779 sector_t block_size,
780 bool may_format)
782 int r;
783 struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
785 if (!md)
786 return NULL;
788 md->bdev = bdev;
789 md->block_size = block_size;
791 md->writesets[0].md.root = INVALID_WRITESET_ROOT;
792 md->writesets[1].md.root = INVALID_WRITESET_ROOT;
793 md->current_writeset = &md->writesets[0];
795 r = create_persistent_data_objects(md, may_format);
796 if (r) {
797 kfree(md);
798 return ERR_PTR(r);
801 return md;
804 static void metadata_close(struct era_metadata *md)
806 destroy_persistent_data_objects(md);
807 kfree(md);
810 static bool valid_nr_blocks(dm_block_t n)
813 * dm_bitset restricts us to 2^32. test_bit & co. restrict us
814 * further to 2^31 - 1
816 return n < (1ull << 31);
819 static int metadata_resize(struct era_metadata *md, void *arg)
821 int r;
822 dm_block_t *new_size = arg;
823 __le32 value;
825 if (!valid_nr_blocks(*new_size)) {
826 DMERR("Invalid number of origin blocks %llu",
827 (unsigned long long) *new_size);
828 return -EINVAL;
831 writeset_free(&md->writesets[0]);
832 writeset_free(&md->writesets[1]);
834 r = writeset_alloc(&md->writesets[0], *new_size);
835 if (r) {
836 DMERR("%s: writeset_alloc failed for writeset 0", __func__);
837 return r;
840 r = writeset_alloc(&md->writesets[1], *new_size);
841 if (r) {
842 DMERR("%s: writeset_alloc failed for writeset 1", __func__);
843 return r;
846 value = cpu_to_le32(0u);
847 __dm_bless_for_disk(&value);
848 r = dm_array_resize(&md->era_array_info, md->era_array_root,
849 md->nr_blocks, *new_size,
850 &value, &md->era_array_root);
851 if (r) {
852 DMERR("%s: dm_array_resize failed", __func__);
853 return r;
856 md->nr_blocks = *new_size;
857 return 0;
860 static int metadata_era_archive(struct era_metadata *md)
862 int r;
863 uint64_t keys[1];
864 struct writeset_disk value;
866 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
867 &md->current_writeset->md.root);
868 if (r) {
869 DMERR("%s: dm_bitset_flush failed", __func__);
870 return r;
873 ws_pack(&md->current_writeset->md, &value);
874 md->current_writeset->md.root = INVALID_WRITESET_ROOT;
876 keys[0] = md->current_era;
877 __dm_bless_for_disk(&value);
878 r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
879 keys, &value, &md->writeset_tree_root);
880 if (r) {
881 DMERR("%s: couldn't insert writeset into btree", __func__);
882 /* FIXME: fail mode */
883 return r;
886 md->archived_writesets = true;
888 return 0;
891 static struct writeset *next_writeset(struct era_metadata *md)
893 return (md->current_writeset == &md->writesets[0]) ?
894 &md->writesets[1] : &md->writesets[0];
897 static int metadata_new_era(struct era_metadata *md)
899 int r;
900 struct writeset *new_writeset = next_writeset(md);
902 r = writeset_init(&md->bitset_info, new_writeset);
903 if (r) {
904 DMERR("%s: writeset_init failed", __func__);
905 return r;
908 swap_writeset(md, new_writeset);
909 md->current_era++;
911 return 0;
914 static int metadata_era_rollover(struct era_metadata *md)
916 int r;
918 if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
919 r = metadata_era_archive(md);
920 if (r) {
921 DMERR("%s: metadata_archive_era failed", __func__);
922 /* FIXME: fail mode? */
923 return r;
927 r = metadata_new_era(md);
928 if (r) {
929 DMERR("%s: new era failed", __func__);
930 /* FIXME: fail mode */
931 return r;
934 return 0;
937 static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
939 bool r;
940 struct writeset *ws;
942 rcu_read_lock();
943 ws = rcu_dereference(md->current_writeset);
944 r = writeset_marked(ws, block);
945 rcu_read_unlock();
947 return r;
950 static int metadata_commit(struct era_metadata *md)
952 int r;
953 struct dm_block *sblock;
955 if (md->current_writeset->md.root != SUPERBLOCK_LOCATION) {
956 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
957 &md->current_writeset->md.root);
958 if (r) {
959 DMERR("%s: bitset flush failed", __func__);
960 return r;
964 r = dm_tm_pre_commit(md->tm);
965 if (r) {
966 DMERR("%s: pre commit failed", __func__);
967 return r;
970 r = save_sm_root(md);
971 if (r) {
972 DMERR("%s: save_sm_root failed", __func__);
973 return r;
976 r = superblock_lock(md, &sblock);
977 if (r) {
978 DMERR("%s: superblock lock failed", __func__);
979 return r;
982 prepare_superblock(md, dm_block_data(sblock));
984 return dm_tm_commit(md->tm, sblock);
987 static int metadata_checkpoint(struct era_metadata *md)
990 * For now we just rollover, but later I want to put a check in to
991 * avoid this if the filter is still pretty fresh.
993 return metadata_era_rollover(md);
997 * Metadata snapshots allow userland to access era data.
999 static int metadata_take_snap(struct era_metadata *md)
1001 int r, inc;
1002 struct dm_block *clone;
1004 if (md->metadata_snap != SUPERBLOCK_LOCATION) {
1005 DMERR("%s: metadata snapshot already exists", __func__);
1006 return -EINVAL;
1009 r = metadata_era_rollover(md);
1010 if (r) {
1011 DMERR("%s: era rollover failed", __func__);
1012 return r;
1015 r = metadata_commit(md);
1016 if (r) {
1017 DMERR("%s: pre commit failed", __func__);
1018 return r;
1021 r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
1022 if (r) {
1023 DMERR("%s: couldn't increment superblock", __func__);
1024 return r;
1027 r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
1028 &sb_validator, &clone, &inc);
1029 if (r) {
1030 DMERR("%s: couldn't shadow superblock", __func__);
1031 dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
1032 return r;
1034 BUG_ON(!inc);
1036 r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
1037 if (r) {
1038 DMERR("%s: couldn't inc writeset tree root", __func__);
1039 dm_tm_unlock(md->tm, clone);
1040 return r;
1043 r = dm_sm_inc_block(md->sm, md->era_array_root);
1044 if (r) {
1045 DMERR("%s: couldn't inc era tree root", __func__);
1046 dm_sm_dec_block(md->sm, md->writeset_tree_root);
1047 dm_tm_unlock(md->tm, clone);
1048 return r;
1051 md->metadata_snap = dm_block_location(clone);
1053 dm_tm_unlock(md->tm, clone);
1055 return 0;
1058 static int metadata_drop_snap(struct era_metadata *md)
1060 int r;
1061 dm_block_t location;
1062 struct dm_block *clone;
1063 struct superblock_disk *disk;
1065 if (md->metadata_snap == SUPERBLOCK_LOCATION) {
1066 DMERR("%s: no snap to drop", __func__);
1067 return -EINVAL;
1070 r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
1071 if (r) {
1072 DMERR("%s: couldn't read lock superblock clone", __func__);
1073 return r;
1077 * Whatever happens now we'll commit with no record of the metadata
1078 * snap.
1080 md->metadata_snap = SUPERBLOCK_LOCATION;
1082 disk = dm_block_data(clone);
1083 r = dm_btree_del(&md->writeset_tree_info,
1084 le64_to_cpu(disk->writeset_tree_root));
1085 if (r) {
1086 DMERR("%s: error deleting writeset tree clone", __func__);
1087 dm_tm_unlock(md->tm, clone);
1088 return r;
1091 r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
1092 if (r) {
1093 DMERR("%s: error deleting era array clone", __func__);
1094 dm_tm_unlock(md->tm, clone);
1095 return r;
1098 location = dm_block_location(clone);
1099 dm_tm_unlock(md->tm, clone);
1101 return dm_sm_dec_block(md->sm, location);
1104 struct metadata_stats {
1105 dm_block_t used;
1106 dm_block_t total;
1107 dm_block_t snap;
1108 uint32_t era;
1111 static int metadata_get_stats(struct era_metadata *md, void *ptr)
1113 int r;
1114 struct metadata_stats *s = ptr;
1115 dm_block_t nr_free, nr_total;
1117 r = dm_sm_get_nr_free(md->sm, &nr_free);
1118 if (r) {
1119 DMERR("dm_sm_get_nr_free returned %d", r);
1120 return r;
1123 r = dm_sm_get_nr_blocks(md->sm, &nr_total);
1124 if (r) {
1125 DMERR("dm_pool_get_metadata_dev_size returned %d", r);
1126 return r;
1129 s->used = nr_total - nr_free;
1130 s->total = nr_total;
1131 s->snap = md->metadata_snap;
1132 s->era = md->current_era;
1134 return 0;
1137 /*----------------------------------------------------------------*/
1139 struct era {
1140 struct dm_target *ti;
1141 struct dm_target_callbacks callbacks;
1143 struct dm_dev *metadata_dev;
1144 struct dm_dev *origin_dev;
1146 dm_block_t nr_blocks;
1147 uint32_t sectors_per_block;
1148 int sectors_per_block_shift;
1149 struct era_metadata *md;
1151 struct workqueue_struct *wq;
1152 struct work_struct worker;
1154 spinlock_t deferred_lock;
1155 struct bio_list deferred_bios;
1157 spinlock_t rpc_lock;
1158 struct list_head rpc_calls;
1160 struct digest digest;
1161 atomic_t suspended;
1164 struct rpc {
1165 struct list_head list;
1167 int (*fn0)(struct era_metadata *);
1168 int (*fn1)(struct era_metadata *, void *);
1169 void *arg;
1170 int result;
1172 struct completion complete;
1175 /*----------------------------------------------------------------
1176 * Remapping.
1177 *---------------------------------------------------------------*/
1178 static bool block_size_is_power_of_two(struct era *era)
1180 return era->sectors_per_block_shift >= 0;
1183 static dm_block_t get_block(struct era *era, struct bio *bio)
1185 sector_t block_nr = bio->bi_iter.bi_sector;
1187 if (!block_size_is_power_of_two(era))
1188 (void) sector_div(block_nr, era->sectors_per_block);
1189 else
1190 block_nr >>= era->sectors_per_block_shift;
1192 return block_nr;
1195 static void remap_to_origin(struct era *era, struct bio *bio)
1197 bio->bi_bdev = era->origin_dev->bdev;
1200 /*----------------------------------------------------------------
1201 * Worker thread
1202 *--------------------------------------------------------------*/
1203 static void wake_worker(struct era *era)
1205 if (!atomic_read(&era->suspended))
1206 queue_work(era->wq, &era->worker);
1209 static void process_old_eras(struct era *era)
1211 int r;
1213 if (!era->digest.step)
1214 return;
1216 r = era->digest.step(era->md, &era->digest);
1217 if (r < 0) {
1218 DMERR("%s: digest step failed, stopping digestion", __func__);
1219 era->digest.step = NULL;
1221 } else if (era->digest.step)
1222 wake_worker(era);
1225 static void process_deferred_bios(struct era *era)
1227 int r;
1228 struct bio_list deferred_bios, marked_bios;
1229 struct bio *bio;
1230 bool commit_needed = false;
1231 bool failed = false;
1233 bio_list_init(&deferred_bios);
1234 bio_list_init(&marked_bios);
1236 spin_lock(&era->deferred_lock);
1237 bio_list_merge(&deferred_bios, &era->deferred_bios);
1238 bio_list_init(&era->deferred_bios);
1239 spin_unlock(&era->deferred_lock);
1241 while ((bio = bio_list_pop(&deferred_bios))) {
1242 r = writeset_test_and_set(&era->md->bitset_info,
1243 era->md->current_writeset,
1244 get_block(era, bio));
1245 if (r < 0) {
1247 * This is bad news, we need to rollback.
1248 * FIXME: finish.
1250 failed = true;
1252 } else if (r == 0)
1253 commit_needed = true;
1255 bio_list_add(&marked_bios, bio);
1258 if (commit_needed) {
1259 r = metadata_commit(era->md);
1260 if (r)
1261 failed = true;
1264 if (failed)
1265 while ((bio = bio_list_pop(&marked_bios)))
1266 bio_io_error(bio);
1267 else
1268 while ((bio = bio_list_pop(&marked_bios)))
1269 generic_make_request(bio);
1272 static void process_rpc_calls(struct era *era)
1274 int r;
1275 bool need_commit = false;
1276 struct list_head calls;
1277 struct rpc *rpc, *tmp;
1279 INIT_LIST_HEAD(&calls);
1280 spin_lock(&era->rpc_lock);
1281 list_splice_init(&era->rpc_calls, &calls);
1282 spin_unlock(&era->rpc_lock);
1284 list_for_each_entry_safe(rpc, tmp, &calls, list) {
1285 rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
1286 need_commit = true;
1289 if (need_commit) {
1290 r = metadata_commit(era->md);
1291 if (r)
1292 list_for_each_entry_safe(rpc, tmp, &calls, list)
1293 rpc->result = r;
1296 list_for_each_entry_safe(rpc, tmp, &calls, list)
1297 complete(&rpc->complete);
1300 static void kick_off_digest(struct era *era)
1302 if (era->md->archived_writesets) {
1303 era->md->archived_writesets = false;
1304 metadata_digest_start(era->md, &era->digest);
1308 static void do_work(struct work_struct *ws)
1310 struct era *era = container_of(ws, struct era, worker);
1312 kick_off_digest(era);
1313 process_old_eras(era);
1314 process_deferred_bios(era);
1315 process_rpc_calls(era);
1318 static void defer_bio(struct era *era, struct bio *bio)
1320 spin_lock(&era->deferred_lock);
1321 bio_list_add(&era->deferred_bios, bio);
1322 spin_unlock(&era->deferred_lock);
1324 wake_worker(era);
1328 * Make an rpc call to the worker to change the metadata.
1330 static int perform_rpc(struct era *era, struct rpc *rpc)
1332 rpc->result = 0;
1333 init_completion(&rpc->complete);
1335 spin_lock(&era->rpc_lock);
1336 list_add(&rpc->list, &era->rpc_calls);
1337 spin_unlock(&era->rpc_lock);
1339 wake_worker(era);
1340 wait_for_completion(&rpc->complete);
1342 return rpc->result;
1345 static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
1347 struct rpc rpc;
1348 rpc.fn0 = fn;
1349 rpc.fn1 = NULL;
1351 return perform_rpc(era, &rpc);
1354 static int in_worker1(struct era *era,
1355 int (*fn)(struct era_metadata *, void *), void *arg)
1357 struct rpc rpc;
1358 rpc.fn0 = NULL;
1359 rpc.fn1 = fn;
1360 rpc.arg = arg;
1362 return perform_rpc(era, &rpc);
1365 static void start_worker(struct era *era)
1367 atomic_set(&era->suspended, 0);
1370 static void stop_worker(struct era *era)
1372 atomic_set(&era->suspended, 1);
1373 flush_workqueue(era->wq);
1376 /*----------------------------------------------------------------
1377 * Target methods
1378 *--------------------------------------------------------------*/
1379 static int dev_is_congested(struct dm_dev *dev, int bdi_bits)
1381 struct request_queue *q = bdev_get_queue(dev->bdev);
1382 return bdi_congested(&q->backing_dev_info, bdi_bits);
1385 static int era_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1387 struct era *era = container_of(cb, struct era, callbacks);
1388 return dev_is_congested(era->origin_dev, bdi_bits);
1391 static void era_destroy(struct era *era)
1393 if (era->md)
1394 metadata_close(era->md);
1396 if (era->wq)
1397 destroy_workqueue(era->wq);
1399 if (era->origin_dev)
1400 dm_put_device(era->ti, era->origin_dev);
1402 if (era->metadata_dev)
1403 dm_put_device(era->ti, era->metadata_dev);
1405 kfree(era);
1408 static dm_block_t calc_nr_blocks(struct era *era)
1410 return dm_sector_div_up(era->ti->len, era->sectors_per_block);
1413 static bool valid_block_size(dm_block_t block_size)
1415 bool greater_than_zero = block_size > 0;
1416 bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
1418 return greater_than_zero && multiple_of_min_block_size;
1422 * <metadata dev> <data dev> <data block size (sectors)>
1424 static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
1426 int r;
1427 char dummy;
1428 struct era *era;
1429 struct era_metadata *md;
1431 if (argc != 3) {
1432 ti->error = "Invalid argument count";
1433 return -EINVAL;
1436 era = kzalloc(sizeof(*era), GFP_KERNEL);
1437 if (!era) {
1438 ti->error = "Error allocating era structure";
1439 return -ENOMEM;
1442 era->ti = ti;
1444 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
1445 if (r) {
1446 ti->error = "Error opening metadata device";
1447 era_destroy(era);
1448 return -EINVAL;
1451 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
1452 if (r) {
1453 ti->error = "Error opening data device";
1454 era_destroy(era);
1455 return -EINVAL;
1458 r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
1459 if (r != 1) {
1460 ti->error = "Error parsing block size";
1461 era_destroy(era);
1462 return -EINVAL;
1465 r = dm_set_target_max_io_len(ti, era->sectors_per_block);
1466 if (r) {
1467 ti->error = "could not set max io len";
1468 era_destroy(era);
1469 return -EINVAL;
1472 if (!valid_block_size(era->sectors_per_block)) {
1473 ti->error = "Invalid block size";
1474 era_destroy(era);
1475 return -EINVAL;
1477 if (era->sectors_per_block & (era->sectors_per_block - 1))
1478 era->sectors_per_block_shift = -1;
1479 else
1480 era->sectors_per_block_shift = __ffs(era->sectors_per_block);
1482 md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
1483 if (IS_ERR(md)) {
1484 ti->error = "Error reading metadata";
1485 era_destroy(era);
1486 return PTR_ERR(md);
1488 era->md = md;
1490 era->nr_blocks = calc_nr_blocks(era);
1492 r = metadata_resize(era->md, &era->nr_blocks);
1493 if (r) {
1494 ti->error = "couldn't resize metadata";
1495 era_destroy(era);
1496 return -ENOMEM;
1499 era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1500 if (!era->wq) {
1501 ti->error = "could not create workqueue for metadata object";
1502 era_destroy(era);
1503 return -ENOMEM;
1505 INIT_WORK(&era->worker, do_work);
1507 spin_lock_init(&era->deferred_lock);
1508 bio_list_init(&era->deferred_bios);
1510 spin_lock_init(&era->rpc_lock);
1511 INIT_LIST_HEAD(&era->rpc_calls);
1513 ti->private = era;
1514 ti->num_flush_bios = 1;
1515 ti->flush_supported = true;
1517 ti->num_discard_bios = 1;
1518 ti->discards_supported = true;
1519 era->callbacks.congested_fn = era_is_congested;
1520 dm_table_add_target_callbacks(ti->table, &era->callbacks);
1522 return 0;
1525 static void era_dtr(struct dm_target *ti)
1527 era_destroy(ti->private);
1530 static int era_map(struct dm_target *ti, struct bio *bio)
1532 struct era *era = ti->private;
1533 dm_block_t block = get_block(era, bio);
1536 * All bios get remapped to the origin device. We do this now, but
1537 * it may not get issued until later. Depending on whether the
1538 * block is marked in this era.
1540 remap_to_origin(era, bio);
1543 * REQ_FLUSH bios carry no data, so we're not interested in them.
1545 if (!(bio->bi_rw & REQ_FLUSH) &&
1546 (bio_data_dir(bio) == WRITE) &&
1547 !metadata_current_marked(era->md, block)) {
1548 defer_bio(era, bio);
1549 return DM_MAPIO_SUBMITTED;
1552 return DM_MAPIO_REMAPPED;
1555 static void era_postsuspend(struct dm_target *ti)
1557 int r;
1558 struct era *era = ti->private;
1560 r = in_worker0(era, metadata_era_archive);
1561 if (r) {
1562 DMERR("%s: couldn't archive current era", __func__);
1563 /* FIXME: fail mode */
1566 stop_worker(era);
1569 static int era_preresume(struct dm_target *ti)
1571 int r;
1572 struct era *era = ti->private;
1573 dm_block_t new_size = calc_nr_blocks(era);
1575 if (era->nr_blocks != new_size) {
1576 r = in_worker1(era, metadata_resize, &new_size);
1577 if (r)
1578 return r;
1580 era->nr_blocks = new_size;
1583 start_worker(era);
1585 r = in_worker0(era, metadata_new_era);
1586 if (r) {
1587 DMERR("%s: metadata_era_rollover failed", __func__);
1588 return r;
1591 return 0;
1595 * Status format:
1597 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
1598 * <current era> <held metadata root | '-'>
1600 static void era_status(struct dm_target *ti, status_type_t type,
1601 unsigned status_flags, char *result, unsigned maxlen)
1603 int r;
1604 struct era *era = ti->private;
1605 ssize_t sz = 0;
1606 struct metadata_stats stats;
1607 char buf[BDEVNAME_SIZE];
1609 switch (type) {
1610 case STATUSTYPE_INFO:
1611 r = in_worker1(era, metadata_get_stats, &stats);
1612 if (r)
1613 goto err;
1615 DMEMIT("%u %llu/%llu %u",
1616 (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
1617 (unsigned long long) stats.used,
1618 (unsigned long long) stats.total,
1619 (unsigned) stats.era);
1621 if (stats.snap != SUPERBLOCK_LOCATION)
1622 DMEMIT(" %llu", stats.snap);
1623 else
1624 DMEMIT(" -");
1625 break;
1627 case STATUSTYPE_TABLE:
1628 format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
1629 DMEMIT("%s ", buf);
1630 format_dev_t(buf, era->origin_dev->bdev->bd_dev);
1631 DMEMIT("%s %u", buf, era->sectors_per_block);
1632 break;
1635 return;
1637 err:
1638 DMEMIT("Error");
1641 static int era_message(struct dm_target *ti, unsigned argc, char **argv)
1643 struct era *era = ti->private;
1645 if (argc != 1) {
1646 DMERR("incorrect number of message arguments");
1647 return -EINVAL;
1650 if (!strcasecmp(argv[0], "checkpoint"))
1651 return in_worker0(era, metadata_checkpoint);
1653 if (!strcasecmp(argv[0], "take_metadata_snap"))
1654 return in_worker0(era, metadata_take_snap);
1656 if (!strcasecmp(argv[0], "drop_metadata_snap"))
1657 return in_worker0(era, metadata_drop_snap);
1659 DMERR("unsupported message '%s'", argv[0]);
1660 return -EINVAL;
1663 static sector_t get_dev_size(struct dm_dev *dev)
1665 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1668 static int era_iterate_devices(struct dm_target *ti,
1669 iterate_devices_callout_fn fn, void *data)
1671 struct era *era = ti->private;
1672 return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
1675 static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
1677 struct era *era = ti->private;
1678 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
1681 * If the system-determined stacked limits are compatible with the
1682 * era device's blocksize (io_opt is a factor) do not override them.
1684 if (io_opt_sectors < era->sectors_per_block ||
1685 do_div(io_opt_sectors, era->sectors_per_block)) {
1686 blk_limits_io_min(limits, 0);
1687 blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
1691 /*----------------------------------------------------------------*/
1693 static struct target_type era_target = {
1694 .name = "era",
1695 .version = {1, 0, 0},
1696 .module = THIS_MODULE,
1697 .ctr = era_ctr,
1698 .dtr = era_dtr,
1699 .map = era_map,
1700 .postsuspend = era_postsuspend,
1701 .preresume = era_preresume,
1702 .status = era_status,
1703 .message = era_message,
1704 .iterate_devices = era_iterate_devices,
1705 .io_hints = era_io_hints
1708 static int __init dm_era_init(void)
1710 int r;
1712 r = dm_register_target(&era_target);
1713 if (r) {
1714 DMERR("era target registration failed: %d", r);
1715 return r;
1718 return 0;
1721 static void __exit dm_era_exit(void)
1723 dm_unregister_target(&era_target);
1726 module_init(dm_era_init);
1727 module_exit(dm_era_exit);
1729 MODULE_DESCRIPTION(DM_NAME " era target");
1730 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
1731 MODULE_LICENSE("GPL");