Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / md / dm-era-target.c
blob73a5c198113a6a95dd7c81e4471b433a5b53fe58
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 ERA_MAX_CONCURRENT_LOCKS 5
259 struct era_metadata {
260 struct block_device *bdev;
261 struct dm_block_manager *bm;
262 struct dm_space_map *sm;
263 struct dm_transaction_manager *tm;
265 dm_block_t block_size;
266 uint32_t nr_blocks;
268 uint32_t current_era;
271 * We preallocate 2 writesets. When an era rolls over we
272 * switch between them. This means the allocation is done at
273 * preresume time, rather than on the io path.
275 struct writeset writesets[2];
276 struct writeset *current_writeset;
278 dm_block_t writeset_tree_root;
279 dm_block_t era_array_root;
281 struct dm_disk_bitset bitset_info;
282 struct dm_btree_info writeset_tree_info;
283 struct dm_array_info era_array_info;
285 dm_block_t metadata_snap;
288 * A flag that is set whenever a writeset has been archived.
290 bool archived_writesets;
293 * Reading the space map root can fail, so we read it into this
294 * buffer before the superblock is locked and updated.
296 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
299 static int superblock_read_lock(struct era_metadata *md,
300 struct dm_block **sblock)
302 return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
303 &sb_validator, sblock);
306 static int superblock_lock_zero(struct era_metadata *md,
307 struct dm_block **sblock)
309 return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
310 &sb_validator, sblock);
313 static int superblock_lock(struct era_metadata *md,
314 struct dm_block **sblock)
316 return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
317 &sb_validator, sblock);
320 /* FIXME: duplication with cache and thin */
321 static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
323 int r;
324 unsigned i;
325 struct dm_block *b;
326 __le64 *data_le, zero = cpu_to_le64(0);
327 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
330 * We can't use a validator here - it may be all zeroes.
332 r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
333 if (r)
334 return r;
336 data_le = dm_block_data(b);
337 *result = true;
338 for (i = 0; i < sb_block_size; i++) {
339 if (data_le[i] != zero) {
340 *result = false;
341 break;
345 dm_bm_unlock(b);
347 return 0;
350 /*----------------------------------------------------------------*/
352 static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
354 disk->nr_bits = cpu_to_le32(core->nr_bits);
355 disk->root = cpu_to_le64(core->root);
358 static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
360 core->nr_bits = le32_to_cpu(disk->nr_bits);
361 core->root = le64_to_cpu(disk->root);
364 static void ws_inc(void *context, const void *value)
366 struct era_metadata *md = context;
367 struct writeset_disk ws_d;
368 dm_block_t b;
370 memcpy(&ws_d, value, sizeof(ws_d));
371 b = le64_to_cpu(ws_d.root);
373 dm_tm_inc(md->tm, b);
376 static void ws_dec(void *context, const void *value)
378 struct era_metadata *md = context;
379 struct writeset_disk ws_d;
380 dm_block_t b;
382 memcpy(&ws_d, value, sizeof(ws_d));
383 b = le64_to_cpu(ws_d.root);
385 dm_bitset_del(&md->bitset_info, b);
388 static int ws_eq(void *context, const void *value1, const void *value2)
390 return !memcmp(value1, value2, sizeof(struct writeset_metadata));
393 /*----------------------------------------------------------------*/
395 static void setup_writeset_tree_info(struct era_metadata *md)
397 struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
398 md->writeset_tree_info.tm = md->tm;
399 md->writeset_tree_info.levels = 1;
400 vt->context = md;
401 vt->size = sizeof(struct writeset_disk);
402 vt->inc = ws_inc;
403 vt->dec = ws_dec;
404 vt->equal = ws_eq;
407 static void setup_era_array_info(struct era_metadata *md)
410 struct dm_btree_value_type vt;
411 vt.context = NULL;
412 vt.size = sizeof(__le32);
413 vt.inc = NULL;
414 vt.dec = NULL;
415 vt.equal = NULL;
417 dm_array_info_init(&md->era_array_info, md->tm, &vt);
420 static void setup_infos(struct era_metadata *md)
422 dm_disk_bitset_init(md->tm, &md->bitset_info);
423 setup_writeset_tree_info(md);
424 setup_era_array_info(md);
427 /*----------------------------------------------------------------*/
429 static int create_fresh_metadata(struct era_metadata *md)
431 int r;
433 r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
434 &md->tm, &md->sm);
435 if (r < 0) {
436 DMERR("dm_tm_create_with_sm failed");
437 return r;
440 setup_infos(md);
442 r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
443 if (r) {
444 DMERR("couldn't create new writeset tree");
445 goto bad;
448 r = dm_array_empty(&md->era_array_info, &md->era_array_root);
449 if (r) {
450 DMERR("couldn't create era array");
451 goto bad;
454 return 0;
456 bad:
457 dm_sm_destroy(md->sm);
458 dm_tm_destroy(md->tm);
460 return r;
463 static int save_sm_root(struct era_metadata *md)
465 int r;
466 size_t metadata_len;
468 r = dm_sm_root_size(md->sm, &metadata_len);
469 if (r < 0)
470 return r;
472 return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
473 metadata_len);
476 static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
478 memcpy(&disk->metadata_space_map_root,
479 &md->metadata_space_map_root,
480 sizeof(md->metadata_space_map_root));
484 * Writes a superblock, including the static fields that don't get updated
485 * with every commit (possible optimisation here). 'md' should be fully
486 * constructed when this is called.
488 static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
490 disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
491 disk->flags = cpu_to_le32(0ul);
493 /* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
494 memset(disk->uuid, 0, sizeof(disk->uuid));
495 disk->version = cpu_to_le32(MAX_ERA_VERSION);
497 copy_sm_root(md, disk);
499 disk->data_block_size = cpu_to_le32(md->block_size);
500 disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
501 disk->nr_blocks = cpu_to_le32(md->nr_blocks);
502 disk->current_era = cpu_to_le32(md->current_era);
504 ws_pack(&md->current_writeset->md, &disk->current_writeset);
505 disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
506 disk->era_array_root = cpu_to_le64(md->era_array_root);
507 disk->metadata_snap = cpu_to_le64(md->metadata_snap);
510 static int write_superblock(struct era_metadata *md)
512 int r;
513 struct dm_block *sblock;
514 struct superblock_disk *disk;
516 r = save_sm_root(md);
517 if (r) {
518 DMERR("%s: save_sm_root failed", __func__);
519 return r;
522 r = superblock_lock_zero(md, &sblock);
523 if (r)
524 return r;
526 disk = dm_block_data(sblock);
527 prepare_superblock(md, disk);
529 return dm_tm_commit(md->tm, sblock);
533 * Assumes block_size and the infos are set.
535 static int format_metadata(struct era_metadata *md)
537 int r;
539 r = create_fresh_metadata(md);
540 if (r)
541 return r;
543 r = write_superblock(md);
544 if (r) {
545 dm_sm_destroy(md->sm);
546 dm_tm_destroy(md->tm);
547 return r;
550 return 0;
553 static int open_metadata(struct era_metadata *md)
555 int r;
556 struct dm_block *sblock;
557 struct superblock_disk *disk;
559 r = superblock_read_lock(md, &sblock);
560 if (r) {
561 DMERR("couldn't read_lock superblock");
562 return r;
565 disk = dm_block_data(sblock);
566 r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
567 disk->metadata_space_map_root,
568 sizeof(disk->metadata_space_map_root),
569 &md->tm, &md->sm);
570 if (r) {
571 DMERR("dm_tm_open_with_sm failed");
572 goto bad;
575 setup_infos(md);
577 md->block_size = le32_to_cpu(disk->data_block_size);
578 md->nr_blocks = le32_to_cpu(disk->nr_blocks);
579 md->current_era = le32_to_cpu(disk->current_era);
581 md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
582 md->era_array_root = le64_to_cpu(disk->era_array_root);
583 md->metadata_snap = le64_to_cpu(disk->metadata_snap);
584 md->archived_writesets = true;
586 dm_bm_unlock(sblock);
588 return 0;
590 bad:
591 dm_bm_unlock(sblock);
592 return r;
595 static int open_or_format_metadata(struct era_metadata *md,
596 bool may_format)
598 int r;
599 bool unformatted = false;
601 r = superblock_all_zeroes(md->bm, &unformatted);
602 if (r)
603 return r;
605 if (unformatted)
606 return may_format ? format_metadata(md) : -EPERM;
608 return open_metadata(md);
611 static int create_persistent_data_objects(struct era_metadata *md,
612 bool may_format)
614 int r;
616 md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
617 ERA_MAX_CONCURRENT_LOCKS);
618 if (IS_ERR(md->bm)) {
619 DMERR("could not create block manager");
620 return PTR_ERR(md->bm);
623 r = open_or_format_metadata(md, may_format);
624 if (r)
625 dm_block_manager_destroy(md->bm);
627 return r;
630 static void destroy_persistent_data_objects(struct era_metadata *md)
632 dm_sm_destroy(md->sm);
633 dm_tm_destroy(md->tm);
634 dm_block_manager_destroy(md->bm);
638 * This waits until all era_map threads have picked up the new filter.
640 static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
642 rcu_assign_pointer(md->current_writeset, new_writeset);
643 synchronize_rcu();
646 /*----------------------------------------------------------------
647 * Writesets get 'digested' into the main era array.
649 * We're using a coroutine here so the worker thread can do the digestion,
650 * thus avoiding synchronisation of the metadata. Digesting a whole
651 * writeset in one go would cause too much latency.
652 *--------------------------------------------------------------*/
653 struct digest {
654 uint32_t era;
655 unsigned nr_bits, current_bit;
656 struct writeset_metadata writeset;
657 __le32 value;
658 struct dm_disk_bitset info;
660 int (*step)(struct era_metadata *, struct digest *);
663 static int metadata_digest_lookup_writeset(struct era_metadata *md,
664 struct digest *d);
666 static int metadata_digest_remove_writeset(struct era_metadata *md,
667 struct digest *d)
669 int r;
670 uint64_t key = d->era;
672 r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
673 &key, &md->writeset_tree_root);
674 if (r) {
675 DMERR("%s: dm_btree_remove failed", __func__);
676 return r;
679 d->step = metadata_digest_lookup_writeset;
680 return 0;
683 #define INSERTS_PER_STEP 100
685 static int metadata_digest_transcribe_writeset(struct era_metadata *md,
686 struct digest *d)
688 int r;
689 bool marked;
690 unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
692 for (b = d->current_bit; b < e; b++) {
693 r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
694 if (r) {
695 DMERR("%s: writeset_marked_on_disk failed", __func__);
696 return r;
699 if (!marked)
700 continue;
702 __dm_bless_for_disk(&d->value);
703 r = dm_array_set_value(&md->era_array_info, md->era_array_root,
704 b, &d->value, &md->era_array_root);
705 if (r) {
706 DMERR("%s: dm_array_set_value failed", __func__);
707 return r;
711 if (b == d->nr_bits)
712 d->step = metadata_digest_remove_writeset;
713 else
714 d->current_bit = b;
716 return 0;
719 static int metadata_digest_lookup_writeset(struct era_metadata *md,
720 struct digest *d)
722 int r;
723 uint64_t key;
724 struct writeset_disk disk;
726 r = dm_btree_find_lowest_key(&md->writeset_tree_info,
727 md->writeset_tree_root, &key);
728 if (r < 0)
729 return r;
731 d->era = key;
733 r = dm_btree_lookup(&md->writeset_tree_info,
734 md->writeset_tree_root, &key, &disk);
735 if (r) {
736 if (r == -ENODATA) {
737 d->step = NULL;
738 return 0;
741 DMERR("%s: dm_btree_lookup failed", __func__);
742 return r;
745 ws_unpack(&disk, &d->writeset);
746 d->value = cpu_to_le32(key);
748 d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
749 d->current_bit = 0;
750 d->step = metadata_digest_transcribe_writeset;
752 return 0;
755 static int metadata_digest_start(struct era_metadata *md, struct digest *d)
757 if (d->step)
758 return 0;
760 memset(d, 0, sizeof(*d));
763 * We initialise another bitset info to avoid any caching side
764 * effects with the previous one.
766 dm_disk_bitset_init(md->tm, &d->info);
767 d->step = metadata_digest_lookup_writeset;
769 return 0;
772 /*----------------------------------------------------------------
773 * High level metadata interface. Target methods should use these, and not
774 * the lower level ones.
775 *--------------------------------------------------------------*/
776 static struct era_metadata *metadata_open(struct block_device *bdev,
777 sector_t block_size,
778 bool may_format)
780 int r;
781 struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
783 if (!md)
784 return NULL;
786 md->bdev = bdev;
787 md->block_size = block_size;
789 md->writesets[0].md.root = INVALID_WRITESET_ROOT;
790 md->writesets[1].md.root = INVALID_WRITESET_ROOT;
791 md->current_writeset = &md->writesets[0];
793 r = create_persistent_data_objects(md, may_format);
794 if (r) {
795 kfree(md);
796 return ERR_PTR(r);
799 return md;
802 static void metadata_close(struct era_metadata *md)
804 destroy_persistent_data_objects(md);
805 kfree(md);
808 static bool valid_nr_blocks(dm_block_t n)
811 * dm_bitset restricts us to 2^32. test_bit & co. restrict us
812 * further to 2^31 - 1
814 return n < (1ull << 31);
817 static int metadata_resize(struct era_metadata *md, void *arg)
819 int r;
820 dm_block_t *new_size = arg;
821 __le32 value;
823 if (!valid_nr_blocks(*new_size)) {
824 DMERR("Invalid number of origin blocks %llu",
825 (unsigned long long) *new_size);
826 return -EINVAL;
829 writeset_free(&md->writesets[0]);
830 writeset_free(&md->writesets[1]);
832 r = writeset_alloc(&md->writesets[0], *new_size);
833 if (r) {
834 DMERR("%s: writeset_alloc failed for writeset 0", __func__);
835 return r;
838 r = writeset_alloc(&md->writesets[1], *new_size);
839 if (r) {
840 DMERR("%s: writeset_alloc failed for writeset 1", __func__);
841 return r;
844 value = cpu_to_le32(0u);
845 __dm_bless_for_disk(&value);
846 r = dm_array_resize(&md->era_array_info, md->era_array_root,
847 md->nr_blocks, *new_size,
848 &value, &md->era_array_root);
849 if (r) {
850 DMERR("%s: dm_array_resize failed", __func__);
851 return r;
854 md->nr_blocks = *new_size;
855 return 0;
858 static int metadata_era_archive(struct era_metadata *md)
860 int r;
861 uint64_t keys[1];
862 struct writeset_disk value;
864 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
865 &md->current_writeset->md.root);
866 if (r) {
867 DMERR("%s: dm_bitset_flush failed", __func__);
868 return r;
871 ws_pack(&md->current_writeset->md, &value);
872 md->current_writeset->md.root = INVALID_WRITESET_ROOT;
874 keys[0] = md->current_era;
875 __dm_bless_for_disk(&value);
876 r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
877 keys, &value, &md->writeset_tree_root);
878 if (r) {
879 DMERR("%s: couldn't insert writeset into btree", __func__);
880 /* FIXME: fail mode */
881 return r;
884 md->archived_writesets = true;
886 return 0;
889 static struct writeset *next_writeset(struct era_metadata *md)
891 return (md->current_writeset == &md->writesets[0]) ?
892 &md->writesets[1] : &md->writesets[0];
895 static int metadata_new_era(struct era_metadata *md)
897 int r;
898 struct writeset *new_writeset = next_writeset(md);
900 r = writeset_init(&md->bitset_info, new_writeset);
901 if (r) {
902 DMERR("%s: writeset_init failed", __func__);
903 return r;
906 swap_writeset(md, new_writeset);
907 md->current_era++;
909 return 0;
912 static int metadata_era_rollover(struct era_metadata *md)
914 int r;
916 if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
917 r = metadata_era_archive(md);
918 if (r) {
919 DMERR("%s: metadata_archive_era failed", __func__);
920 /* FIXME: fail mode? */
921 return r;
925 r = metadata_new_era(md);
926 if (r) {
927 DMERR("%s: new era failed", __func__);
928 /* FIXME: fail mode */
929 return r;
932 return 0;
935 static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
937 bool r;
938 struct writeset *ws;
940 rcu_read_lock();
941 ws = rcu_dereference(md->current_writeset);
942 r = writeset_marked(ws, block);
943 rcu_read_unlock();
945 return r;
948 static int metadata_commit(struct era_metadata *md)
950 int r;
951 struct dm_block *sblock;
953 if (md->current_writeset->md.root != SUPERBLOCK_LOCATION) {
954 r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
955 &md->current_writeset->md.root);
956 if (r) {
957 DMERR("%s: bitset flush failed", __func__);
958 return r;
962 r = dm_tm_pre_commit(md->tm);
963 if (r) {
964 DMERR("%s: pre commit failed", __func__);
965 return r;
968 r = save_sm_root(md);
969 if (r) {
970 DMERR("%s: save_sm_root failed", __func__);
971 return r;
974 r = superblock_lock(md, &sblock);
975 if (r) {
976 DMERR("%s: superblock lock failed", __func__);
977 return r;
980 prepare_superblock(md, dm_block_data(sblock));
982 return dm_tm_commit(md->tm, sblock);
985 static int metadata_checkpoint(struct era_metadata *md)
988 * For now we just rollover, but later I want to put a check in to
989 * avoid this if the filter is still pretty fresh.
991 return metadata_era_rollover(md);
995 * Metadata snapshots allow userland to access era data.
997 static int metadata_take_snap(struct era_metadata *md)
999 int r, inc;
1000 struct dm_block *clone;
1002 if (md->metadata_snap != SUPERBLOCK_LOCATION) {
1003 DMERR("%s: metadata snapshot already exists", __func__);
1004 return -EINVAL;
1007 r = metadata_era_rollover(md);
1008 if (r) {
1009 DMERR("%s: era rollover failed", __func__);
1010 return r;
1013 r = metadata_commit(md);
1014 if (r) {
1015 DMERR("%s: pre commit failed", __func__);
1016 return r;
1019 r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
1020 if (r) {
1021 DMERR("%s: couldn't increment superblock", __func__);
1022 return r;
1025 r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
1026 &sb_validator, &clone, &inc);
1027 if (r) {
1028 DMERR("%s: couldn't shadow superblock", __func__);
1029 dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
1030 return r;
1032 BUG_ON(!inc);
1034 r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
1035 if (r) {
1036 DMERR("%s: couldn't inc writeset tree root", __func__);
1037 dm_tm_unlock(md->tm, clone);
1038 return r;
1041 r = dm_sm_inc_block(md->sm, md->era_array_root);
1042 if (r) {
1043 DMERR("%s: couldn't inc era tree root", __func__);
1044 dm_sm_dec_block(md->sm, md->writeset_tree_root);
1045 dm_tm_unlock(md->tm, clone);
1046 return r;
1049 md->metadata_snap = dm_block_location(clone);
1051 dm_tm_unlock(md->tm, clone);
1053 return 0;
1056 static int metadata_drop_snap(struct era_metadata *md)
1058 int r;
1059 dm_block_t location;
1060 struct dm_block *clone;
1061 struct superblock_disk *disk;
1063 if (md->metadata_snap == SUPERBLOCK_LOCATION) {
1064 DMERR("%s: no snap to drop", __func__);
1065 return -EINVAL;
1068 r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
1069 if (r) {
1070 DMERR("%s: couldn't read lock superblock clone", __func__);
1071 return r;
1075 * Whatever happens now we'll commit with no record of the metadata
1076 * snap.
1078 md->metadata_snap = SUPERBLOCK_LOCATION;
1080 disk = dm_block_data(clone);
1081 r = dm_btree_del(&md->writeset_tree_info,
1082 le64_to_cpu(disk->writeset_tree_root));
1083 if (r) {
1084 DMERR("%s: error deleting writeset tree clone", __func__);
1085 dm_tm_unlock(md->tm, clone);
1086 return r;
1089 r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
1090 if (r) {
1091 DMERR("%s: error deleting era array clone", __func__);
1092 dm_tm_unlock(md->tm, clone);
1093 return r;
1096 location = dm_block_location(clone);
1097 dm_tm_unlock(md->tm, clone);
1099 return dm_sm_dec_block(md->sm, location);
1102 struct metadata_stats {
1103 dm_block_t used;
1104 dm_block_t total;
1105 dm_block_t snap;
1106 uint32_t era;
1109 static int metadata_get_stats(struct era_metadata *md, void *ptr)
1111 int r;
1112 struct metadata_stats *s = ptr;
1113 dm_block_t nr_free, nr_total;
1115 r = dm_sm_get_nr_free(md->sm, &nr_free);
1116 if (r) {
1117 DMERR("dm_sm_get_nr_free returned %d", r);
1118 return r;
1121 r = dm_sm_get_nr_blocks(md->sm, &nr_total);
1122 if (r) {
1123 DMERR("dm_pool_get_metadata_dev_size returned %d", r);
1124 return r;
1127 s->used = nr_total - nr_free;
1128 s->total = nr_total;
1129 s->snap = md->metadata_snap;
1130 s->era = md->current_era;
1132 return 0;
1135 /*----------------------------------------------------------------*/
1137 struct era {
1138 struct dm_target *ti;
1139 struct dm_target_callbacks callbacks;
1141 struct dm_dev *metadata_dev;
1142 struct dm_dev *origin_dev;
1144 dm_block_t nr_blocks;
1145 uint32_t sectors_per_block;
1146 int sectors_per_block_shift;
1147 struct era_metadata *md;
1149 struct workqueue_struct *wq;
1150 struct work_struct worker;
1152 spinlock_t deferred_lock;
1153 struct bio_list deferred_bios;
1155 spinlock_t rpc_lock;
1156 struct list_head rpc_calls;
1158 struct digest digest;
1159 atomic_t suspended;
1162 struct rpc {
1163 struct list_head list;
1165 int (*fn0)(struct era_metadata *);
1166 int (*fn1)(struct era_metadata *, void *);
1167 void *arg;
1168 int result;
1170 struct completion complete;
1173 /*----------------------------------------------------------------
1174 * Remapping.
1175 *---------------------------------------------------------------*/
1176 static bool block_size_is_power_of_two(struct era *era)
1178 return era->sectors_per_block_shift >= 0;
1181 static dm_block_t get_block(struct era *era, struct bio *bio)
1183 sector_t block_nr = bio->bi_iter.bi_sector;
1185 if (!block_size_is_power_of_two(era))
1186 (void) sector_div(block_nr, era->sectors_per_block);
1187 else
1188 block_nr >>= era->sectors_per_block_shift;
1190 return block_nr;
1193 static void remap_to_origin(struct era *era, struct bio *bio)
1195 bio_set_dev(bio, era->origin_dev->bdev);
1198 /*----------------------------------------------------------------
1199 * Worker thread
1200 *--------------------------------------------------------------*/
1201 static void wake_worker(struct era *era)
1203 if (!atomic_read(&era->suspended))
1204 queue_work(era->wq, &era->worker);
1207 static void process_old_eras(struct era *era)
1209 int r;
1211 if (!era->digest.step)
1212 return;
1214 r = era->digest.step(era->md, &era->digest);
1215 if (r < 0) {
1216 DMERR("%s: digest step failed, stopping digestion", __func__);
1217 era->digest.step = NULL;
1219 } else if (era->digest.step)
1220 wake_worker(era);
1223 static void process_deferred_bios(struct era *era)
1225 int r;
1226 struct bio_list deferred_bios, marked_bios;
1227 struct bio *bio;
1228 bool commit_needed = false;
1229 bool failed = false;
1231 bio_list_init(&deferred_bios);
1232 bio_list_init(&marked_bios);
1234 spin_lock(&era->deferred_lock);
1235 bio_list_merge(&deferred_bios, &era->deferred_bios);
1236 bio_list_init(&era->deferred_bios);
1237 spin_unlock(&era->deferred_lock);
1239 while ((bio = bio_list_pop(&deferred_bios))) {
1240 r = writeset_test_and_set(&era->md->bitset_info,
1241 era->md->current_writeset,
1242 get_block(era, bio));
1243 if (r < 0) {
1245 * This is bad news, we need to rollback.
1246 * FIXME: finish.
1248 failed = true;
1250 } else if (r == 0)
1251 commit_needed = true;
1253 bio_list_add(&marked_bios, bio);
1256 if (commit_needed) {
1257 r = metadata_commit(era->md);
1258 if (r)
1259 failed = true;
1262 if (failed)
1263 while ((bio = bio_list_pop(&marked_bios)))
1264 bio_io_error(bio);
1265 else
1266 while ((bio = bio_list_pop(&marked_bios)))
1267 generic_make_request(bio);
1270 static void process_rpc_calls(struct era *era)
1272 int r;
1273 bool need_commit = false;
1274 struct list_head calls;
1275 struct rpc *rpc, *tmp;
1277 INIT_LIST_HEAD(&calls);
1278 spin_lock(&era->rpc_lock);
1279 list_splice_init(&era->rpc_calls, &calls);
1280 spin_unlock(&era->rpc_lock);
1282 list_for_each_entry_safe(rpc, tmp, &calls, list) {
1283 rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
1284 need_commit = true;
1287 if (need_commit) {
1288 r = metadata_commit(era->md);
1289 if (r)
1290 list_for_each_entry_safe(rpc, tmp, &calls, list)
1291 rpc->result = r;
1294 list_for_each_entry_safe(rpc, tmp, &calls, list)
1295 complete(&rpc->complete);
1298 static void kick_off_digest(struct era *era)
1300 if (era->md->archived_writesets) {
1301 era->md->archived_writesets = false;
1302 metadata_digest_start(era->md, &era->digest);
1306 static void do_work(struct work_struct *ws)
1308 struct era *era = container_of(ws, struct era, worker);
1310 kick_off_digest(era);
1311 process_old_eras(era);
1312 process_deferred_bios(era);
1313 process_rpc_calls(era);
1316 static void defer_bio(struct era *era, struct bio *bio)
1318 spin_lock(&era->deferred_lock);
1319 bio_list_add(&era->deferred_bios, bio);
1320 spin_unlock(&era->deferred_lock);
1322 wake_worker(era);
1326 * Make an rpc call to the worker to change the metadata.
1328 static int perform_rpc(struct era *era, struct rpc *rpc)
1330 rpc->result = 0;
1331 init_completion(&rpc->complete);
1333 spin_lock(&era->rpc_lock);
1334 list_add(&rpc->list, &era->rpc_calls);
1335 spin_unlock(&era->rpc_lock);
1337 wake_worker(era);
1338 wait_for_completion(&rpc->complete);
1340 return rpc->result;
1343 static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
1345 struct rpc rpc;
1346 rpc.fn0 = fn;
1347 rpc.fn1 = NULL;
1349 return perform_rpc(era, &rpc);
1352 static int in_worker1(struct era *era,
1353 int (*fn)(struct era_metadata *, void *), void *arg)
1355 struct rpc rpc;
1356 rpc.fn0 = NULL;
1357 rpc.fn1 = fn;
1358 rpc.arg = arg;
1360 return perform_rpc(era, &rpc);
1363 static void start_worker(struct era *era)
1365 atomic_set(&era->suspended, 0);
1368 static void stop_worker(struct era *era)
1370 atomic_set(&era->suspended, 1);
1371 flush_workqueue(era->wq);
1374 /*----------------------------------------------------------------
1375 * Target methods
1376 *--------------------------------------------------------------*/
1377 static int dev_is_congested(struct dm_dev *dev, int bdi_bits)
1379 struct request_queue *q = bdev_get_queue(dev->bdev);
1380 return bdi_congested(q->backing_dev_info, bdi_bits);
1383 static int era_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1385 struct era *era = container_of(cb, struct era, callbacks);
1386 return dev_is_congested(era->origin_dev, bdi_bits);
1389 static void era_destroy(struct era *era)
1391 if (era->md)
1392 metadata_close(era->md);
1394 if (era->wq)
1395 destroy_workqueue(era->wq);
1397 if (era->origin_dev)
1398 dm_put_device(era->ti, era->origin_dev);
1400 if (era->metadata_dev)
1401 dm_put_device(era->ti, era->metadata_dev);
1403 kfree(era);
1406 static dm_block_t calc_nr_blocks(struct era *era)
1408 return dm_sector_div_up(era->ti->len, era->sectors_per_block);
1411 static bool valid_block_size(dm_block_t block_size)
1413 bool greater_than_zero = block_size > 0;
1414 bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
1416 return greater_than_zero && multiple_of_min_block_size;
1420 * <metadata dev> <data dev> <data block size (sectors)>
1422 static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
1424 int r;
1425 char dummy;
1426 struct era *era;
1427 struct era_metadata *md;
1429 if (argc != 3) {
1430 ti->error = "Invalid argument count";
1431 return -EINVAL;
1434 era = kzalloc(sizeof(*era), GFP_KERNEL);
1435 if (!era) {
1436 ti->error = "Error allocating era structure";
1437 return -ENOMEM;
1440 era->ti = ti;
1442 r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
1443 if (r) {
1444 ti->error = "Error opening metadata device";
1445 era_destroy(era);
1446 return -EINVAL;
1449 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
1450 if (r) {
1451 ti->error = "Error opening data device";
1452 era_destroy(era);
1453 return -EINVAL;
1456 r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
1457 if (r != 1) {
1458 ti->error = "Error parsing block size";
1459 era_destroy(era);
1460 return -EINVAL;
1463 r = dm_set_target_max_io_len(ti, era->sectors_per_block);
1464 if (r) {
1465 ti->error = "could not set max io len";
1466 era_destroy(era);
1467 return -EINVAL;
1470 if (!valid_block_size(era->sectors_per_block)) {
1471 ti->error = "Invalid block size";
1472 era_destroy(era);
1473 return -EINVAL;
1475 if (era->sectors_per_block & (era->sectors_per_block - 1))
1476 era->sectors_per_block_shift = -1;
1477 else
1478 era->sectors_per_block_shift = __ffs(era->sectors_per_block);
1480 md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
1481 if (IS_ERR(md)) {
1482 ti->error = "Error reading metadata";
1483 era_destroy(era);
1484 return PTR_ERR(md);
1486 era->md = md;
1488 era->nr_blocks = calc_nr_blocks(era);
1490 r = metadata_resize(era->md, &era->nr_blocks);
1491 if (r) {
1492 ti->error = "couldn't resize metadata";
1493 era_destroy(era);
1494 return -ENOMEM;
1497 era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1498 if (!era->wq) {
1499 ti->error = "could not create workqueue for metadata object";
1500 era_destroy(era);
1501 return -ENOMEM;
1503 INIT_WORK(&era->worker, do_work);
1505 spin_lock_init(&era->deferred_lock);
1506 bio_list_init(&era->deferred_bios);
1508 spin_lock_init(&era->rpc_lock);
1509 INIT_LIST_HEAD(&era->rpc_calls);
1511 ti->private = era;
1512 ti->num_flush_bios = 1;
1513 ti->flush_supported = true;
1515 ti->num_discard_bios = 1;
1516 era->callbacks.congested_fn = era_is_congested;
1517 dm_table_add_target_callbacks(ti->table, &era->callbacks);
1519 return 0;
1522 static void era_dtr(struct dm_target *ti)
1524 era_destroy(ti->private);
1527 static int era_map(struct dm_target *ti, struct bio *bio)
1529 struct era *era = ti->private;
1530 dm_block_t block = get_block(era, bio);
1533 * All bios get remapped to the origin device. We do this now, but
1534 * it may not get issued until later. Depending on whether the
1535 * block is marked in this era.
1537 remap_to_origin(era, bio);
1540 * REQ_PREFLUSH bios carry no data, so we're not interested in them.
1542 if (!(bio->bi_opf & REQ_PREFLUSH) &&
1543 (bio_data_dir(bio) == WRITE) &&
1544 !metadata_current_marked(era->md, block)) {
1545 defer_bio(era, bio);
1546 return DM_MAPIO_SUBMITTED;
1549 return DM_MAPIO_REMAPPED;
1552 static void era_postsuspend(struct dm_target *ti)
1554 int r;
1555 struct era *era = ti->private;
1557 r = in_worker0(era, metadata_era_archive);
1558 if (r) {
1559 DMERR("%s: couldn't archive current era", __func__);
1560 /* FIXME: fail mode */
1563 stop_worker(era);
1566 static int era_preresume(struct dm_target *ti)
1568 int r;
1569 struct era *era = ti->private;
1570 dm_block_t new_size = calc_nr_blocks(era);
1572 if (era->nr_blocks != new_size) {
1573 r = in_worker1(era, metadata_resize, &new_size);
1574 if (r)
1575 return r;
1577 era->nr_blocks = new_size;
1580 start_worker(era);
1582 r = in_worker0(era, metadata_new_era);
1583 if (r) {
1584 DMERR("%s: metadata_era_rollover failed", __func__);
1585 return r;
1588 return 0;
1592 * Status format:
1594 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
1595 * <current era> <held metadata root | '-'>
1597 static void era_status(struct dm_target *ti, status_type_t type,
1598 unsigned status_flags, char *result, unsigned maxlen)
1600 int r;
1601 struct era *era = ti->private;
1602 ssize_t sz = 0;
1603 struct metadata_stats stats;
1604 char buf[BDEVNAME_SIZE];
1606 switch (type) {
1607 case STATUSTYPE_INFO:
1608 r = in_worker1(era, metadata_get_stats, &stats);
1609 if (r)
1610 goto err;
1612 DMEMIT("%u %llu/%llu %u",
1613 (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
1614 (unsigned long long) stats.used,
1615 (unsigned long long) stats.total,
1616 (unsigned) stats.era);
1618 if (stats.snap != SUPERBLOCK_LOCATION)
1619 DMEMIT(" %llu", stats.snap);
1620 else
1621 DMEMIT(" -");
1622 break;
1624 case STATUSTYPE_TABLE:
1625 format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
1626 DMEMIT("%s ", buf);
1627 format_dev_t(buf, era->origin_dev->bdev->bd_dev);
1628 DMEMIT("%s %u", buf, era->sectors_per_block);
1629 break;
1632 return;
1634 err:
1635 DMEMIT("Error");
1638 static int era_message(struct dm_target *ti, unsigned argc, char **argv)
1640 struct era *era = ti->private;
1642 if (argc != 1) {
1643 DMERR("incorrect number of message arguments");
1644 return -EINVAL;
1647 if (!strcasecmp(argv[0], "checkpoint"))
1648 return in_worker0(era, metadata_checkpoint);
1650 if (!strcasecmp(argv[0], "take_metadata_snap"))
1651 return in_worker0(era, metadata_take_snap);
1653 if (!strcasecmp(argv[0], "drop_metadata_snap"))
1654 return in_worker0(era, metadata_drop_snap);
1656 DMERR("unsupported message '%s'", argv[0]);
1657 return -EINVAL;
1660 static sector_t get_dev_size(struct dm_dev *dev)
1662 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1665 static int era_iterate_devices(struct dm_target *ti,
1666 iterate_devices_callout_fn fn, void *data)
1668 struct era *era = ti->private;
1669 return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
1672 static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
1674 struct era *era = ti->private;
1675 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
1678 * If the system-determined stacked limits are compatible with the
1679 * era device's blocksize (io_opt is a factor) do not override them.
1681 if (io_opt_sectors < era->sectors_per_block ||
1682 do_div(io_opt_sectors, era->sectors_per_block)) {
1683 blk_limits_io_min(limits, 0);
1684 blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
1688 /*----------------------------------------------------------------*/
1690 static struct target_type era_target = {
1691 .name = "era",
1692 .version = {1, 0, 0},
1693 .module = THIS_MODULE,
1694 .ctr = era_ctr,
1695 .dtr = era_dtr,
1696 .map = era_map,
1697 .postsuspend = era_postsuspend,
1698 .preresume = era_preresume,
1699 .status = era_status,
1700 .message = era_message,
1701 .iterate_devices = era_iterate_devices,
1702 .io_hints = era_io_hints
1705 static int __init dm_era_init(void)
1707 int r;
1709 r = dm_register_target(&era_target);
1710 if (r) {
1711 DMERR("era target registration failed: %d", r);
1712 return r;
1715 return 0;
1718 static void __exit dm_era_exit(void)
1720 dm_unregister_target(&era_target);
1723 module_init(dm_era_init);
1724 module_exit(dm_era_exit);
1726 MODULE_DESCRIPTION(DM_NAME " era target");
1727 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
1728 MODULE_LICENSE("GPL");