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 /*----------------------------------------------------------------
26 *--------------------------------------------------------------*/
27 struct writeset_metadata
{
33 struct writeset_metadata md
;
36 * An in core copy of the bits to save constantly doing look ups on
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
)
51 static int setup_on_disk_bitset(struct dm_disk_bitset
*info
,
52 unsigned nr_bits
, dm_block_t
*root
)
56 r
= dm_bitset_empty(info
, root
);
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
));
77 DMERR("%s: couldn't allocate in memory bitset", __func__
);
85 * Wipes the in-core bitset, and creates a new on disk bitset.
87 static int writeset_init(struct dm_disk_bitset
*info
, struct writeset
*ws
)
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
);
95 DMERR("%s: setup_on_disk_bitset failed", __func__
);
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
,
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
);
119 DMERR("%s: dm_bitset_test_bit failed", __func__
);
123 BUG_ON(m
->root
!= old
);
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
)
136 if (!test_and_set_bit(block
, ws
->bits
)) {
137 r
= dm_bitset_set_bit(info
, ws
->md
.root
, block
, &ws
->md
.root
);
139 /* FIXME: fail mode */
149 /*----------------------------------------------------------------
150 * On disk metadata layout
151 *--------------------------------------------------------------*/
152 #define SPACE_MAP_ROOT_SIZE 128
155 struct writeset_disk
{
160 struct superblock_disk
{
169 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
171 __le32 data_block_size
;
172 __le32 metadata_block_size
;
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
;
187 /*----------------------------------------------------------------
188 * Superblock validation
189 *--------------------------------------------------------------*/
190 static void sb_prepare_for_write(struct dm_block_validator
*v
,
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
);
214 static int sb_check(struct dm_block_validator
*v
,
216 size_t sb_block_size
)
218 struct superblock_disk
*disk
= dm_block_data(b
);
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
));
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
);
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
));
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
,
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
;
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
)
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
);
336 data_le
= dm_block_data(b
);
338 for (i
= 0; i
< sb_block_size
; i
++) {
339 if (data_le
[i
] != zero
) {
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
;
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
;
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;
401 vt
->size
= sizeof(struct writeset_disk
);
407 static void setup_era_array_info(struct era_metadata
*md
)
410 struct dm_btree_value_type vt
;
412 vt
.size
= sizeof(__le32
);
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
)
433 r
= dm_tm_create_with_sm(md
->bm
, SUPERBLOCK_LOCATION
,
436 DMERR("dm_tm_create_with_sm failed");
442 r
= dm_btree_empty(&md
->writeset_tree_info
, &md
->writeset_tree_root
);
444 DMERR("couldn't create new writeset tree");
448 r
= dm_array_empty(&md
->era_array_info
, &md
->era_array_root
);
450 DMERR("couldn't create era array");
457 dm_sm_destroy(md
->sm
);
458 dm_tm_destroy(md
->tm
);
463 static int save_sm_root(struct era_metadata
*md
)
468 r
= dm_sm_root_size(md
->sm
, &metadata_len
);
472 return dm_sm_copy_root(md
->sm
, &md
->metadata_space_map_root
,
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
)
513 struct dm_block
*sblock
;
514 struct superblock_disk
*disk
;
516 r
= save_sm_root(md
);
518 DMERR("%s: save_sm_root failed", __func__
);
522 r
= superblock_lock_zero(md
, &sblock
);
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
)
539 r
= create_fresh_metadata(md
);
543 r
= write_superblock(md
);
545 dm_sm_destroy(md
->sm
);
546 dm_tm_destroy(md
->tm
);
553 static int open_metadata(struct era_metadata
*md
)
556 struct dm_block
*sblock
;
557 struct superblock_disk
*disk
;
559 r
= superblock_read_lock(md
, &sblock
);
561 DMERR("couldn't read_lock superblock");
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
),
571 DMERR("dm_tm_open_with_sm failed");
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
);
591 dm_bm_unlock(sblock
);
595 static int open_or_format_metadata(struct era_metadata
*md
,
599 bool unformatted
= false;
601 r
= superblock_all_zeroes(md
->bm
, &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
,
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
);
625 dm_block_manager_destroy(md
->bm
);
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
);
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 *--------------------------------------------------------------*/
655 unsigned nr_bits
, current_bit
;
656 struct writeset_metadata writeset
;
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
,
666 static int metadata_digest_remove_writeset(struct era_metadata
*md
,
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
);
675 DMERR("%s: dm_btree_remove failed", __func__
);
679 d
->step
= metadata_digest_lookup_writeset
;
683 #define INSERTS_PER_STEP 100
685 static int metadata_digest_transcribe_writeset(struct era_metadata
*md
,
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
);
695 DMERR("%s: writeset_marked_on_disk failed", __func__
);
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
);
706 DMERR("%s: dm_array_set_value failed", __func__
);
712 d
->step
= metadata_digest_remove_writeset
;
719 static int metadata_digest_lookup_writeset(struct era_metadata
*md
,
724 struct writeset_disk disk
;
726 r
= dm_btree_find_lowest_key(&md
->writeset_tree_info
,
727 md
->writeset_tree_root
, &key
);
733 r
= dm_btree_lookup(&md
->writeset_tree_info
,
734 md
->writeset_tree_root
, &key
, &disk
);
741 DMERR("%s: dm_btree_lookup failed", __func__
);
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
);
750 d
->step
= metadata_digest_transcribe_writeset
;
755 static int metadata_digest_start(struct era_metadata
*md
, struct digest
*d
)
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
;
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
,
781 struct era_metadata
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
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
);
802 static void metadata_close(struct era_metadata
*md
)
804 destroy_persistent_data_objects(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
)
820 dm_block_t
*new_size
= arg
;
823 if (!valid_nr_blocks(*new_size
)) {
824 DMERR("Invalid number of origin blocks %llu",
825 (unsigned long long) *new_size
);
829 writeset_free(&md
->writesets
[0]);
830 writeset_free(&md
->writesets
[1]);
832 r
= writeset_alloc(&md
->writesets
[0], *new_size
);
834 DMERR("%s: writeset_alloc failed for writeset 0", __func__
);
838 r
= writeset_alloc(&md
->writesets
[1], *new_size
);
840 DMERR("%s: writeset_alloc failed for writeset 1", __func__
);
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
);
850 DMERR("%s: dm_array_resize failed", __func__
);
854 md
->nr_blocks
= *new_size
;
858 static int metadata_era_archive(struct era_metadata
*md
)
862 struct writeset_disk value
;
864 r
= dm_bitset_flush(&md
->bitset_info
, md
->current_writeset
->md
.root
,
865 &md
->current_writeset
->md
.root
);
867 DMERR("%s: dm_bitset_flush failed", __func__
);
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
);
879 DMERR("%s: couldn't insert writeset into btree", __func__
);
880 /* FIXME: fail mode */
884 md
->archived_writesets
= true;
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
)
898 struct writeset
*new_writeset
= next_writeset(md
);
900 r
= writeset_init(&md
->bitset_info
, new_writeset
);
902 DMERR("%s: writeset_init failed", __func__
);
906 swap_writeset(md
, new_writeset
);
912 static int metadata_era_rollover(struct era_metadata
*md
)
916 if (md
->current_writeset
->md
.root
!= INVALID_WRITESET_ROOT
) {
917 r
= metadata_era_archive(md
);
919 DMERR("%s: metadata_archive_era failed", __func__
);
920 /* FIXME: fail mode? */
925 r
= metadata_new_era(md
);
927 DMERR("%s: new era failed", __func__
);
928 /* FIXME: fail mode */
935 static bool metadata_current_marked(struct era_metadata
*md
, dm_block_t block
)
941 ws
= rcu_dereference(md
->current_writeset
);
942 r
= writeset_marked(ws
, block
);
948 static int metadata_commit(struct era_metadata
*md
)
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
);
957 DMERR("%s: bitset flush failed", __func__
);
962 r
= dm_tm_pre_commit(md
->tm
);
964 DMERR("%s: pre commit failed", __func__
);
968 r
= save_sm_root(md
);
970 DMERR("%s: save_sm_root failed", __func__
);
974 r
= superblock_lock(md
, &sblock
);
976 DMERR("%s: superblock lock failed", __func__
);
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
)
1000 struct dm_block
*clone
;
1002 if (md
->metadata_snap
!= SUPERBLOCK_LOCATION
) {
1003 DMERR("%s: metadata snapshot already exists", __func__
);
1007 r
= metadata_era_rollover(md
);
1009 DMERR("%s: era rollover failed", __func__
);
1013 r
= metadata_commit(md
);
1015 DMERR("%s: pre commit failed", __func__
);
1019 r
= dm_sm_inc_block(md
->sm
, SUPERBLOCK_LOCATION
);
1021 DMERR("%s: couldn't increment superblock", __func__
);
1025 r
= dm_tm_shadow_block(md
->tm
, SUPERBLOCK_LOCATION
,
1026 &sb_validator
, &clone
, &inc
);
1028 DMERR("%s: couldn't shadow superblock", __func__
);
1029 dm_sm_dec_block(md
->sm
, SUPERBLOCK_LOCATION
);
1034 r
= dm_sm_inc_block(md
->sm
, md
->writeset_tree_root
);
1036 DMERR("%s: couldn't inc writeset tree root", __func__
);
1037 dm_tm_unlock(md
->tm
, clone
);
1041 r
= dm_sm_inc_block(md
->sm
, md
->era_array_root
);
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
);
1049 md
->metadata_snap
= dm_block_location(clone
);
1051 dm_tm_unlock(md
->tm
, clone
);
1056 static int metadata_drop_snap(struct era_metadata
*md
)
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__
);
1068 r
= dm_tm_read_lock(md
->tm
, md
->metadata_snap
, &sb_validator
, &clone
);
1070 DMERR("%s: couldn't read lock superblock clone", __func__
);
1075 * Whatever happens now we'll commit with no record of the metadata
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
));
1084 DMERR("%s: error deleting writeset tree clone", __func__
);
1085 dm_tm_unlock(md
->tm
, clone
);
1089 r
= dm_array_del(&md
->era_array_info
, le64_to_cpu(disk
->era_array_root
));
1091 DMERR("%s: error deleting era array clone", __func__
);
1092 dm_tm_unlock(md
->tm
, clone
);
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
{
1109 static int metadata_get_stats(struct era_metadata
*md
, void *ptr
)
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
);
1117 DMERR("dm_sm_get_nr_free returned %d", r
);
1121 r
= dm_sm_get_nr_blocks(md
->sm
, &nr_total
);
1123 DMERR("dm_pool_get_metadata_dev_size returned %d", 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
;
1135 /*----------------------------------------------------------------*/
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
;
1163 struct list_head list
;
1165 int (*fn0
)(struct era_metadata
*);
1166 int (*fn1
)(struct era_metadata
*, void *);
1170 struct completion complete
;
1173 /*----------------------------------------------------------------
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
);
1188 block_nr
>>= era
->sectors_per_block_shift
;
1193 static void remap_to_origin(struct era
*era
, struct bio
*bio
)
1195 bio_set_dev(bio
, era
->origin_dev
->bdev
);
1198 /*----------------------------------------------------------------
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
)
1211 if (!era
->digest
.step
)
1214 r
= era
->digest
.step(era
->md
, &era
->digest
);
1216 DMERR("%s: digest step failed, stopping digestion", __func__
);
1217 era
->digest
.step
= NULL
;
1219 } else if (era
->digest
.step
)
1223 static void process_deferred_bios(struct era
*era
)
1226 struct bio_list deferred_bios
, marked_bios
;
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
));
1245 * This is bad news, we need to rollback.
1251 commit_needed
= true;
1253 bio_list_add(&marked_bios
, bio
);
1256 if (commit_needed
) {
1257 r
= metadata_commit(era
->md
);
1263 while ((bio
= bio_list_pop(&marked_bios
)))
1266 while ((bio
= bio_list_pop(&marked_bios
)))
1267 generic_make_request(bio
);
1270 static void process_rpc_calls(struct era
*era
)
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
);
1288 r
= metadata_commit(era
->md
);
1290 list_for_each_entry_safe(rpc
, tmp
, &calls
, list
)
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
);
1326 * Make an rpc call to the worker to change the metadata.
1328 static int perform_rpc(struct era
*era
, struct rpc
*rpc
)
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
);
1338 wait_for_completion(&rpc
->complete
);
1343 static int in_worker0(struct era
*era
, int (*fn
)(struct era_metadata
*))
1349 return perform_rpc(era
, &rpc
);
1352 static int in_worker1(struct era
*era
,
1353 int (*fn
)(struct era_metadata
*, void *), void *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 /*----------------------------------------------------------------
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
)
1392 metadata_close(era
->md
);
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
);
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
)
1427 struct era_metadata
*md
;
1430 ti
->error
= "Invalid argument count";
1434 era
= kzalloc(sizeof(*era
), GFP_KERNEL
);
1436 ti
->error
= "Error allocating era structure";
1442 r
= dm_get_device(ti
, argv
[0], FMODE_READ
| FMODE_WRITE
, &era
->metadata_dev
);
1444 ti
->error
= "Error opening metadata device";
1449 r
= dm_get_device(ti
, argv
[1], FMODE_READ
| FMODE_WRITE
, &era
->origin_dev
);
1451 ti
->error
= "Error opening data device";
1456 r
= sscanf(argv
[2], "%u%c", &era
->sectors_per_block
, &dummy
);
1458 ti
->error
= "Error parsing block size";
1463 r
= dm_set_target_max_io_len(ti
, era
->sectors_per_block
);
1465 ti
->error
= "could not set max io len";
1470 if (!valid_block_size(era
->sectors_per_block
)) {
1471 ti
->error
= "Invalid block size";
1475 if (era
->sectors_per_block
& (era
->sectors_per_block
- 1))
1476 era
->sectors_per_block_shift
= -1;
1478 era
->sectors_per_block_shift
= __ffs(era
->sectors_per_block
);
1480 md
= metadata_open(era
->metadata_dev
->bdev
, era
->sectors_per_block
, true);
1482 ti
->error
= "Error reading metadata";
1488 era
->nr_blocks
= calc_nr_blocks(era
);
1490 r
= metadata_resize(era
->md
, &era
->nr_blocks
);
1492 ti
->error
= "couldn't resize metadata";
1497 era
->wq
= alloc_ordered_workqueue("dm-" DM_MSG_PREFIX
, WQ_MEM_RECLAIM
);
1499 ti
->error
= "could not create workqueue for metadata object";
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
);
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
);
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
)
1555 struct era
*era
= ti
->private;
1557 r
= in_worker0(era
, metadata_era_archive
);
1559 DMERR("%s: couldn't archive current era", __func__
);
1560 /* FIXME: fail mode */
1566 static int era_preresume(struct dm_target
*ti
)
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
);
1577 era
->nr_blocks
= new_size
;
1582 r
= in_worker0(era
, metadata_new_era
);
1584 DMERR("%s: metadata_era_rollover failed", __func__
);
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
)
1601 struct era
*era
= ti
->private;
1603 struct metadata_stats stats
;
1604 char buf
[BDEVNAME_SIZE
];
1607 case STATUSTYPE_INFO
:
1608 r
= in_worker1(era
, metadata_get_stats
, &stats
);
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
);
1624 case STATUSTYPE_TABLE
:
1625 format_dev_t(buf
, era
->metadata_dev
->bdev
->bd_dev
);
1627 format_dev_t(buf
, era
->origin_dev
->bdev
->bd_dev
);
1628 DMEMIT("%s %u", buf
, era
->sectors_per_block
);
1638 static int era_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
1639 char *result
, unsigned maxlen
)
1641 struct era
*era
= ti
->private;
1644 DMERR("incorrect number of message arguments");
1648 if (!strcasecmp(argv
[0], "checkpoint"))
1649 return in_worker0(era
, metadata_checkpoint
);
1651 if (!strcasecmp(argv
[0], "take_metadata_snap"))
1652 return in_worker0(era
, metadata_take_snap
);
1654 if (!strcasecmp(argv
[0], "drop_metadata_snap"))
1655 return in_worker0(era
, metadata_drop_snap
);
1657 DMERR("unsupported message '%s'", argv
[0]);
1661 static sector_t
get_dev_size(struct dm_dev
*dev
)
1663 return i_size_read(dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
1666 static int era_iterate_devices(struct dm_target
*ti
,
1667 iterate_devices_callout_fn fn
, void *data
)
1669 struct era
*era
= ti
->private;
1670 return fn(ti
, era
->origin_dev
, 0, get_dev_size(era
->origin_dev
), data
);
1673 static void era_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1675 struct era
*era
= ti
->private;
1676 uint64_t io_opt_sectors
= limits
->io_opt
>> SECTOR_SHIFT
;
1679 * If the system-determined stacked limits are compatible with the
1680 * era device's blocksize (io_opt is a factor) do not override them.
1682 if (io_opt_sectors
< era
->sectors_per_block
||
1683 do_div(io_opt_sectors
, era
->sectors_per_block
)) {
1684 blk_limits_io_min(limits
, 0);
1685 blk_limits_io_opt(limits
, era
->sectors_per_block
<< SECTOR_SHIFT
);
1689 /*----------------------------------------------------------------*/
1691 static struct target_type era_target
= {
1693 .version
= {1, 0, 0},
1694 .module
= THIS_MODULE
,
1698 .postsuspend
= era_postsuspend
,
1699 .preresume
= era_preresume
,
1700 .status
= era_status
,
1701 .message
= era_message
,
1702 .iterate_devices
= era_iterate_devices
,
1703 .io_hints
= era_io_hints
1706 static int __init
dm_era_init(void)
1710 r
= dm_register_target(&era_target
);
1712 DMERR("era target registration failed: %d", r
);
1719 static void __exit
dm_era_exit(void)
1721 dm_unregister_target(&era_target
);
1724 module_init(dm_era_init
);
1725 module_exit(dm_era_exit
);
1727 MODULE_DESCRIPTION(DM_NAME
" era target");
1728 MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
1729 MODULE_LICENSE("GPL");