1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
5 * This file is released under the GPL.
8 #include "dm-cache-metadata.h"
10 #include "persistent-data/dm-array.h"
11 #include "persistent-data/dm-bitset.h"
12 #include "persistent-data/dm-space-map.h"
13 #include "persistent-data/dm-space-map-disk.h"
14 #include "persistent-data/dm-transaction-manager.h"
16 #include <linux/device-mapper.h>
17 #include <linux/refcount.h>
19 /*----------------------------------------------------------------*/
21 #define DM_MSG_PREFIX "cache metadata"
23 #define CACHE_SUPERBLOCK_MAGIC 06142003
24 #define CACHE_SUPERBLOCK_LOCATION 0
27 * defines a range of metadata versions that this module can handle.
29 #define MIN_CACHE_VERSION 1
30 #define MAX_CACHE_VERSION 2
33 * 3 for btree insert +
34 * 2 for btree lookup used within space map
36 #define CACHE_MAX_CONCURRENT_LOCKS 5
37 #define SPACE_MAP_ROOT_SIZE 128
39 enum superblock_flag_bits {
40 /* for spotting crashes that would invalidate the dirty bitset */
42 /* metadata must be checked using the tools */
47 * Each mapping from cache block -> origin block carries a set of flags.
51 * A valid mapping. Because we're using an array we clear this
52 * flag for an non existant mapping.
57 * The data on the cache is different from that on the origin.
58 * This flag is only used by metadata format 1.
63 struct cache_disk_superblock {
72 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
73 __le32 policy_hint_size;
75 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
80 __le64 discard_block_size;
81 __le64 discard_nr_blocks;
83 __le32 data_block_size;
84 __le32 metadata_block_size;
88 __le32 compat_ro_flags;
89 __le32 incompat_flags;
96 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
99 * Metadata format 2 fields.
104 struct dm_cache_metadata {
105 refcount_t ref_count;
106 struct list_head list;
108 unsigned int version;
109 struct block_device *bdev;
110 struct dm_block_manager *bm;
111 struct dm_space_map *metadata_sm;
112 struct dm_transaction_manager *tm;
114 struct dm_array_info info;
115 struct dm_array_info hint_info;
116 struct dm_disk_bitset discard_info;
118 struct rw_semaphore root_lock;
121 dm_block_t hint_root;
122 dm_block_t discard_root;
124 sector_t discard_block_size;
125 dm_dblock_t discard_nr_blocks;
127 sector_t data_block_size;
128 dm_cblock_t cache_blocks;
130 bool clean_when_opened:1;
132 char policy_name[CACHE_POLICY_NAME_SIZE];
133 unsigned int policy_version[CACHE_POLICY_VERSION_SIZE];
134 size_t policy_hint_size;
135 struct dm_cache_statistics stats;
138 * Reading the space map root can fail, so we read it into this
139 * buffer before the superblock is locked and updated.
141 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
144 * Set if a transaction has to be aborted but the attempt to roll
145 * back to the previous (good) transaction failed. The only
146 * metadata operation permissible in this state is the closing of
152 * Metadata format 2 fields.
154 dm_block_t dirty_root;
155 struct dm_disk_bitset dirty_info;
158 * These structures are used when loading metadata. They're too
159 * big to put on the stack.
161 struct dm_array_cursor mapping_cursor;
162 struct dm_array_cursor hint_cursor;
163 struct dm_bitset_cursor dirty_cursor;
167 *-----------------------------------------------------------------
168 * superblock validator
169 *-----------------------------------------------------------------
171 #define SUPERBLOCK_CSUM_XOR 9031977
173 static void sb_prepare_for_write(struct dm_block_validator *v,
175 size_t sb_block_size)
177 struct cache_disk_superblock *disk_super = dm_block_data(b);
179 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
180 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
181 sb_block_size - sizeof(__le32),
182 SUPERBLOCK_CSUM_XOR));
185 static int check_metadata_version(struct cache_disk_superblock *disk_super)
187 uint32_t metadata_version = le32_to_cpu(disk_super->version);
189 if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
190 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
191 metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
198 static int sb_check(struct dm_block_validator *v,
200 size_t sb_block_size)
202 struct cache_disk_superblock *disk_super = dm_block_data(b);
205 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
206 DMERR("%s failed: blocknr %llu: wanted %llu",
207 __func__, le64_to_cpu(disk_super->blocknr),
208 (unsigned long long)dm_block_location(b));
212 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
213 DMERR("%s failed: magic %llu: wanted %llu",
214 __func__, le64_to_cpu(disk_super->magic),
215 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
219 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
220 sb_block_size - sizeof(__le32),
221 SUPERBLOCK_CSUM_XOR));
222 if (csum_le != disk_super->csum) {
223 DMERR("%s failed: csum %u: wanted %u",
224 __func__, le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
228 return check_metadata_version(disk_super);
231 static struct dm_block_validator sb_validator = {
232 .name = "superblock",
233 .prepare_for_write = sb_prepare_for_write,
237 /*----------------------------------------------------------------*/
239 static int superblock_read_lock(struct dm_cache_metadata *cmd,
240 struct dm_block **sblock)
242 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
243 &sb_validator, sblock);
246 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
247 struct dm_block **sblock)
249 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
250 &sb_validator, sblock);
253 static int superblock_lock(struct dm_cache_metadata *cmd,
254 struct dm_block **sblock)
256 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
257 &sb_validator, sblock);
260 /*----------------------------------------------------------------*/
262 static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
267 __le64 *data_le, zero = cpu_to_le64(0);
268 unsigned int sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
271 * We can't use a validator here - it may be all zeroes.
273 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
277 data_le = dm_block_data(b);
279 for (i = 0; i < sb_block_size; i++) {
280 if (data_le[i] != zero) {
291 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
293 struct dm_btree_value_type vt;
296 vt.size = sizeof(__le64);
300 dm_array_info_init(&cmd->info, cmd->tm, &vt);
302 if (cmd->policy_hint_size) {
303 vt.size = sizeof(__le32);
304 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
308 static int __save_sm_root(struct dm_cache_metadata *cmd)
313 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
317 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
321 static void __copy_sm_root(struct dm_cache_metadata *cmd,
322 struct cache_disk_superblock *disk_super)
324 memcpy(&disk_super->metadata_space_map_root,
325 &cmd->metadata_space_map_root,
326 sizeof(cmd->metadata_space_map_root));
329 static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
331 return cmd->version >= 2;
334 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
337 struct dm_block *sblock;
338 struct cache_disk_superblock *disk_super;
339 sector_t bdev_size = bdev_nr_sectors(cmd->bdev);
341 /* FIXME: see if we can lose the max sectors limit */
342 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
343 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
345 r = dm_tm_pre_commit(cmd->tm);
350 * dm_sm_copy_root() can fail. So we need to do it before we start
351 * updating the superblock.
353 r = __save_sm_root(cmd);
357 r = superblock_lock_zero(cmd, &sblock);
361 disk_super = dm_block_data(sblock);
362 disk_super->flags = 0;
363 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
364 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
365 disk_super->version = cpu_to_le32(cmd->version);
366 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
367 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
368 disk_super->policy_hint_size = cpu_to_le32(0);
370 __copy_sm_root(cmd, disk_super);
372 disk_super->mapping_root = cpu_to_le64(cmd->root);
373 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
374 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
375 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
376 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
377 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
378 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
379 disk_super->cache_blocks = cpu_to_le32(0);
381 disk_super->read_hits = cpu_to_le32(0);
382 disk_super->read_misses = cpu_to_le32(0);
383 disk_super->write_hits = cpu_to_le32(0);
384 disk_super->write_misses = cpu_to_le32(0);
386 if (separate_dirty_bits(cmd))
387 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
389 return dm_tm_commit(cmd->tm, sblock);
392 static int __format_metadata(struct dm_cache_metadata *cmd)
396 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
397 &cmd->tm, &cmd->metadata_sm);
399 DMERR("tm_create_with_sm failed");
403 __setup_mapping_info(cmd);
405 r = dm_array_empty(&cmd->info, &cmd->root);
409 if (separate_dirty_bits(cmd)) {
410 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
411 r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
416 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
417 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
421 cmd->discard_block_size = 0;
422 cmd->discard_nr_blocks = 0;
424 r = __write_initial_superblock(cmd);
428 cmd->clean_when_opened = true;
432 dm_tm_destroy(cmd->tm);
433 dm_sm_destroy(cmd->metadata_sm);
438 static int __check_incompat_features(struct cache_disk_superblock *disk_super,
439 struct dm_cache_metadata *cmd)
441 uint32_t incompat_flags, features;
443 incompat_flags = le32_to_cpu(disk_super->incompat_flags);
444 features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
446 DMERR("could not access metadata due to unsupported optional features (%lx).",
447 (unsigned long)features);
452 * Check for read-only metadata to skip the following RDWR checks.
454 if (bdev_read_only(cmd->bdev))
457 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
459 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
460 (unsigned long)features);
467 static int __open_metadata(struct dm_cache_metadata *cmd)
470 struct dm_block *sblock;
471 struct cache_disk_superblock *disk_super;
472 unsigned long sb_flags;
474 r = superblock_read_lock(cmd, &sblock);
476 DMERR("couldn't read lock superblock");
480 disk_super = dm_block_data(sblock);
482 /* Verify the data block size hasn't changed */
483 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
484 DMERR("changing the data block size (from %u to %llu) is not supported",
485 le32_to_cpu(disk_super->data_block_size),
486 (unsigned long long)cmd->data_block_size);
491 r = __check_incompat_features(disk_super, cmd);
495 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
496 disk_super->metadata_space_map_root,
497 sizeof(disk_super->metadata_space_map_root),
498 &cmd->tm, &cmd->metadata_sm);
500 DMERR("tm_open_with_sm failed");
504 __setup_mapping_info(cmd);
505 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
506 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
507 sb_flags = le32_to_cpu(disk_super->flags);
508 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
509 dm_bm_unlock(sblock);
514 dm_bm_unlock(sblock);
518 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
522 bool unformatted = false;
524 r = __superblock_all_zeroes(cmd->bm, &unformatted);
529 return format_device ? __format_metadata(cmd) : -EPERM;
531 return __open_metadata(cmd);
534 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
535 bool may_format_device)
539 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
540 CACHE_MAX_CONCURRENT_LOCKS);
541 if (IS_ERR(cmd->bm)) {
542 DMERR("could not create block manager");
543 r = PTR_ERR(cmd->bm);
548 r = __open_or_format_metadata(cmd, may_format_device);
550 dm_block_manager_destroy(cmd->bm);
557 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd,
560 dm_sm_destroy(cmd->metadata_sm);
561 dm_tm_destroy(cmd->tm);
563 dm_block_manager_destroy(cmd->bm);
566 typedef unsigned long (*flags_mutator)(unsigned long);
568 static void update_flags(struct cache_disk_superblock *disk_super,
569 flags_mutator mutator)
571 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
573 disk_super->flags = cpu_to_le32(sb_flags);
576 static unsigned long set_clean_shutdown(unsigned long flags)
578 set_bit(CLEAN_SHUTDOWN, &flags);
582 static unsigned long clear_clean_shutdown(unsigned long flags)
584 clear_bit(CLEAN_SHUTDOWN, &flags);
588 static void read_superblock_fields(struct dm_cache_metadata *cmd,
589 struct cache_disk_superblock *disk_super)
591 cmd->version = le32_to_cpu(disk_super->version);
592 cmd->flags = le32_to_cpu(disk_super->flags);
593 cmd->root = le64_to_cpu(disk_super->mapping_root);
594 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
595 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
596 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
597 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
598 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
599 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
600 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
601 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
602 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
603 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
604 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
606 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
607 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
608 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
609 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
611 if (separate_dirty_bits(cmd))
612 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
614 cmd->changed = false;
618 * The mutator updates the superblock flags.
620 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
621 flags_mutator mutator)
624 struct cache_disk_superblock *disk_super;
625 struct dm_block *sblock;
627 r = superblock_lock(cmd, &sblock);
631 disk_super = dm_block_data(sblock);
632 update_flags(disk_super, mutator);
633 read_superblock_fields(cmd, disk_super);
634 dm_bm_unlock(sblock);
636 return dm_bm_flush(cmd->bm);
639 static int __begin_transaction(struct dm_cache_metadata *cmd)
642 struct cache_disk_superblock *disk_super;
643 struct dm_block *sblock;
646 * We re-read the superblock every time. Shouldn't need to do this
649 r = superblock_read_lock(cmd, &sblock);
653 disk_super = dm_block_data(sblock);
654 read_superblock_fields(cmd, disk_super);
655 dm_bm_unlock(sblock);
660 static int __commit_transaction(struct dm_cache_metadata *cmd,
661 flags_mutator mutator)
664 struct cache_disk_superblock *disk_super;
665 struct dm_block *sblock;
668 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
670 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
672 if (separate_dirty_bits(cmd)) {
673 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
679 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
684 r = dm_tm_pre_commit(cmd->tm);
688 r = __save_sm_root(cmd);
692 r = superblock_lock(cmd, &sblock);
696 disk_super = dm_block_data(sblock);
698 disk_super->flags = cpu_to_le32(cmd->flags);
700 update_flags(disk_super, mutator);
702 disk_super->mapping_root = cpu_to_le64(cmd->root);
703 if (separate_dirty_bits(cmd))
704 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
705 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
706 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
707 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
708 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
709 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
710 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
711 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
712 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
713 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
714 disk_super->policy_hint_size = cpu_to_le32(cmd->policy_hint_size);
716 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
717 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
718 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
719 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
720 __copy_sm_root(cmd, disk_super);
722 return dm_tm_commit(cmd->tm, sblock);
725 /*----------------------------------------------------------------*/
728 * The mappings are held in a dm-array that has 64-bit values stored in
729 * little-endian format. The index is the cblock, the high 48bits of the
730 * value are the oblock and the low 16 bit the flags.
732 #define FLAGS_MASK ((1 << 16) - 1)
734 static __le64 pack_value(dm_oblock_t block, unsigned int flags)
736 uint64_t value = from_oblock(block);
739 value = value | (flags & FLAGS_MASK);
740 return cpu_to_le64(value);
743 static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned int *flags)
745 uint64_t value = le64_to_cpu(value_le);
746 uint64_t b = value >> 16;
748 *block = to_oblock(b);
749 *flags = value & FLAGS_MASK;
752 /*----------------------------------------------------------------*/
754 static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
755 sector_t data_block_size,
756 bool may_format_device,
757 size_t policy_hint_size,
758 unsigned int metadata_version)
761 struct dm_cache_metadata *cmd;
763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
765 DMERR("could not allocate metadata struct");
766 return ERR_PTR(-ENOMEM);
769 cmd->version = metadata_version;
770 refcount_set(&cmd->ref_count, 1);
771 init_rwsem(&cmd->root_lock);
773 cmd->data_block_size = data_block_size;
774 cmd->cache_blocks = 0;
775 cmd->policy_hint_size = policy_hint_size;
777 cmd->fail_io = false;
779 r = __create_persistent_data_objects(cmd, may_format_device);
785 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
787 dm_cache_metadata_close(cmd);
795 * We keep a little list of ref counted metadata objects to prevent two
796 * different target instances creating separate bufio instances. This is
797 * an issue if a table is reloaded before the suspend.
799 static DEFINE_MUTEX(table_lock);
800 static LIST_HEAD(table);
802 static struct dm_cache_metadata *lookup(struct block_device *bdev)
804 struct dm_cache_metadata *cmd;
806 list_for_each_entry(cmd, &table, list)
807 if (cmd->bdev == bdev) {
808 refcount_inc(&cmd->ref_count);
815 static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
816 sector_t data_block_size,
817 bool may_format_device,
818 size_t policy_hint_size,
819 unsigned int metadata_version)
821 struct dm_cache_metadata *cmd, *cmd2;
823 mutex_lock(&table_lock);
825 mutex_unlock(&table_lock);
830 cmd = metadata_open(bdev, data_block_size, may_format_device,
831 policy_hint_size, metadata_version);
833 mutex_lock(&table_lock);
836 mutex_unlock(&table_lock);
837 __destroy_persistent_data_objects(cmd, true);
841 list_add(&cmd->list, &table);
842 mutex_unlock(&table_lock);
848 static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
850 if (cmd->data_block_size != data_block_size) {
851 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
852 (unsigned long long) data_block_size,
853 (unsigned long long) cmd->data_block_size);
860 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
861 sector_t data_block_size,
862 bool may_format_device,
863 size_t policy_hint_size,
864 unsigned int metadata_version)
866 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
867 policy_hint_size, metadata_version);
869 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
870 dm_cache_metadata_close(cmd);
871 return ERR_PTR(-EINVAL);
877 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
879 if (refcount_dec_and_test(&cmd->ref_count)) {
880 mutex_lock(&table_lock);
881 list_del(&cmd->list);
882 mutex_unlock(&table_lock);
885 __destroy_persistent_data_objects(cmd, true);
891 * Checks that the given cache block is either unmapped or clean.
893 static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
901 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
905 unpack_value(value, &ob, &flags);
906 *result = !((flags & M_VALID) && (flags & M_DIRTY));
911 static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
912 dm_cblock_t begin, dm_cblock_t end,
918 while (begin != end) {
919 r = block_clean_combined_dirty(cmd, begin, result);
921 DMERR("block_clean_combined_dirty failed");
926 DMERR("cache block %llu is dirty",
927 (unsigned long long) from_cblock(begin));
931 begin = to_cblock(from_cblock(begin) + 1);
937 static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
938 dm_cblock_t begin, dm_cblock_t end,
945 if (from_cblock(cmd->cache_blocks) == 0)
949 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
950 from_cblock(cmd->cache_blocks), &cmd->dirty_cursor);
952 DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
956 r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
958 DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
959 dm_bitset_cursor_end(&cmd->dirty_cursor);
963 while (begin != end) {
965 * We assume that unmapped blocks have their dirty bit
968 dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
970 DMERR("%s: cache block %llu is dirty", __func__,
971 (unsigned long long) from_cblock(begin));
972 dm_bitset_cursor_end(&cmd->dirty_cursor);
977 begin = to_cblock(from_cblock(begin) + 1);
981 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
983 DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
984 dm_bitset_cursor_end(&cmd->dirty_cursor);
989 dm_bitset_cursor_end(&cmd->dirty_cursor);
994 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
995 dm_cblock_t begin, dm_cblock_t end,
998 if (separate_dirty_bits(cmd))
999 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
1001 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
1004 static bool cmd_write_lock(struct dm_cache_metadata *cmd)
1006 down_write(&cmd->root_lock);
1007 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
1008 up_write(&cmd->root_lock);
1014 #define WRITE_LOCK(cmd) \
1016 if (!cmd_write_lock((cmd))) \
1020 #define WRITE_LOCK_VOID(cmd) \
1022 if (!cmd_write_lock((cmd))) \
1026 #define WRITE_UNLOCK(cmd) \
1027 up_write(&(cmd)->root_lock)
1029 static bool cmd_read_lock(struct dm_cache_metadata *cmd)
1031 down_read(&cmd->root_lock);
1033 up_read(&cmd->root_lock);
1039 #define READ_LOCK(cmd) \
1041 if (!cmd_read_lock((cmd))) \
1045 #define READ_LOCK_VOID(cmd) \
1047 if (!cmd_read_lock((cmd))) \
1051 #define READ_UNLOCK(cmd) \
1052 up_read(&(cmd)->root_lock)
1054 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1058 __le64 null_mapping = pack_value(0, 0);
1061 __dm_bless_for_disk(&null_mapping);
1063 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1064 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1066 __dm_unbless_for_disk(&null_mapping);
1071 DMERR("unable to shrink cache due to dirty blocks");
1073 __dm_unbless_for_disk(&null_mapping);
1078 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1079 from_cblock(new_cache_size),
1080 &null_mapping, &cmd->root);
1084 if (separate_dirty_bits(cmd)) {
1085 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1086 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1087 false, &cmd->dirty_root);
1092 cmd->cache_blocks = new_cache_size;
1093 cmd->changed = true;
1101 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1102 sector_t discard_block_size,
1103 dm_dblock_t new_nr_entries)
1108 r = dm_bitset_resize(&cmd->discard_info,
1110 from_dblock(cmd->discard_nr_blocks),
1111 from_dblock(new_nr_entries),
1112 false, &cmd->discard_root);
1114 cmd->discard_block_size = discard_block_size;
1115 cmd->discard_nr_blocks = new_nr_entries;
1118 cmd->changed = true;
1124 static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1126 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
1127 from_dblock(b), &cmd->discard_root);
1130 static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1132 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
1133 from_dblock(b), &cmd->discard_root);
1136 static int __discard(struct dm_cache_metadata *cmd,
1137 dm_dblock_t dblock, bool discard)
1141 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1145 cmd->changed = true;
1149 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
1150 dm_dblock_t dblock, bool discard)
1155 r = __discard(cmd, dblock, discard);
1161 static int __load_discards(struct dm_cache_metadata *cmd,
1162 load_discard_fn fn, void *context)
1166 struct dm_bitset_cursor c;
1168 if (from_dblock(cmd->discard_nr_blocks) == 0)
1172 if (cmd->clean_when_opened) {
1173 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1177 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1178 from_dblock(cmd->discard_nr_blocks), &c);
1182 for (b = 0; ; b++) {
1183 r = fn(context, cmd->discard_block_size, to_dblock(b),
1184 dm_bitset_cursor_get_value(&c));
1188 if (b >= (from_dblock(cmd->discard_nr_blocks) - 1))
1191 r = dm_bitset_cursor_next(&c);
1196 dm_bitset_cursor_end(&c);
1199 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1200 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
1209 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1210 load_discard_fn fn, void *context)
1215 r = __load_discards(cmd, fn, context);
1221 int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
1224 *result = cmd->cache_blocks;
1230 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1233 __le64 value = pack_value(0, 0);
1235 __dm_bless_for_disk(&value);
1236 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1237 &value, &cmd->root);
1241 cmd->changed = true;
1245 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1250 r = __remove(cmd, cblock);
1256 static int __insert(struct dm_cache_metadata *cmd,
1257 dm_cblock_t cblock, dm_oblock_t oblock)
1260 __le64 value = pack_value(oblock, M_VALID);
1262 __dm_bless_for_disk(&value);
1264 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1265 &value, &cmd->root);
1269 cmd->changed = true;
1273 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1274 dm_cblock_t cblock, dm_oblock_t oblock)
1279 r = __insert(cmd, cblock, oblock);
1289 struct dm_cache_metadata *cmd;
1290 bool respect_dirty_flags;
1294 static bool policy_unchanged(struct dm_cache_metadata *cmd,
1295 struct dm_cache_policy *policy)
1297 const char *policy_name = dm_cache_policy_get_name(policy);
1298 const unsigned int *policy_version = dm_cache_policy_get_version(policy);
1299 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1302 * Ensure policy names match.
1304 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1308 * Ensure policy major versions match.
1310 if (cmd->policy_version[0] != policy_version[0])
1314 * Ensure policy hint sizes match.
1316 if (cmd->policy_hint_size != policy_hint_size)
1322 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1324 return cmd->hint_root && cmd->policy_hint_size;
1327 static bool hints_array_available(struct dm_cache_metadata *cmd,
1328 struct dm_cache_policy *policy)
1330 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
1331 hints_array_initialized(cmd);
1334 static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1335 uint64_t cb, bool hints_valid,
1336 struct dm_array_cursor *mapping_cursor,
1337 struct dm_array_cursor *hint_cursor,
1338 load_mapping_fn fn, void *context)
1345 __le64 *mapping_value_le;
1346 __le32 *hint_value_le;
1352 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1353 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1354 unpack_value(mapping, &oblock, &flags);
1356 if (flags & M_VALID) {
1358 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1359 memcpy(&hint, hint_value_le, sizeof(hint));
1361 if (cmd->clean_when_opened)
1362 dirty = flags & M_DIRTY;
1364 r = fn(context, oblock, to_cblock(cb), dirty,
1365 le32_to_cpu(hint), hints_valid);
1367 DMERR("policy couldn't load cache block %llu",
1368 (unsigned long long) from_cblock(to_cblock(cb)));
1375 static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1376 uint64_t cb, bool hints_valid,
1377 struct dm_array_cursor *mapping_cursor,
1378 struct dm_array_cursor *hint_cursor,
1379 struct dm_bitset_cursor *dirty_cursor,
1380 load_mapping_fn fn, void *context)
1387 __le64 *mapping_value_le;
1388 __le32 *hint_value_le;
1394 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1395 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1396 unpack_value(mapping, &oblock, &flags);
1398 if (flags & M_VALID) {
1400 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1401 memcpy(&hint, hint_value_le, sizeof(hint));
1403 if (cmd->clean_when_opened)
1404 dirty = dm_bitset_cursor_get_value(dirty_cursor);
1406 r = fn(context, oblock, to_cblock(cb), dirty,
1407 le32_to_cpu(hint), hints_valid);
1409 DMERR("policy couldn't load cache block %llu",
1410 (unsigned long long) from_cblock(to_cblock(cb)));
1417 static int __load_mappings(struct dm_cache_metadata *cmd,
1418 struct dm_cache_policy *policy,
1419 load_mapping_fn fn, void *context)
1424 bool hints_valid = hints_array_available(cmd, policy);
1426 if (from_cblock(cmd->cache_blocks) == 0)
1430 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1435 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1437 dm_array_cursor_end(&cmd->mapping_cursor);
1442 if (separate_dirty_bits(cmd)) {
1443 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1444 from_cblock(cmd->cache_blocks),
1445 &cmd->dirty_cursor);
1447 dm_array_cursor_end(&cmd->hint_cursor);
1448 dm_array_cursor_end(&cmd->mapping_cursor);
1453 for (cb = 0; ; cb++) {
1454 if (separate_dirty_bits(cmd))
1455 r = __load_mapping_v2(cmd, cb, hints_valid,
1456 &cmd->mapping_cursor,
1461 r = __load_mapping_v1(cmd, cb, hints_valid,
1462 &cmd->mapping_cursor, &cmd->hint_cursor,
1468 * We need to break out before we move the cursors.
1470 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1473 r = dm_array_cursor_next(&cmd->mapping_cursor);
1475 DMERR("dm_array_cursor_next for mapping failed");
1480 r = dm_array_cursor_next(&cmd->hint_cursor);
1482 dm_array_cursor_end(&cmd->hint_cursor);
1483 hints_valid = false;
1487 if (separate_dirty_bits(cmd)) {
1488 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1490 DMERR("dm_bitset_cursor_next for dirty failed");
1496 dm_array_cursor_end(&cmd->mapping_cursor);
1498 dm_array_cursor_end(&cmd->hint_cursor);
1500 if (separate_dirty_bits(cmd))
1501 dm_bitset_cursor_end(&cmd->dirty_cursor);
1506 int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1507 struct dm_cache_policy *policy,
1508 load_mapping_fn fn, void *context)
1513 r = __load_mappings(cmd, policy, fn, context);
1519 static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1525 memcpy(&value, leaf, sizeof(value));
1526 unpack_value(value, &oblock, &flags);
1531 static int __dump_mappings(struct dm_cache_metadata *cmd)
1533 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1536 void dm_cache_dump(struct dm_cache_metadata *cmd)
1538 READ_LOCK_VOID(cmd);
1539 __dump_mappings(cmd);
1543 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1554 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1561 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1565 unpack_value(value, &oblock, &flags);
1567 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1568 /* nothing to be done */
1571 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
1572 __dm_bless_for_disk(&value);
1574 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1575 &value, &cmd->root);
1579 cmd->changed = true;
1584 static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned int nr_bits, unsigned long *bits)
1589 for (i = 0; i < nr_bits; i++) {
1590 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1598 static int is_dirty_callback(uint32_t index, bool *value, void *context)
1600 unsigned long *bits = context;
1601 *value = test_bit(index, bits);
1605 static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned int nr_bits, unsigned long *bits)
1609 /* nr_bits is really just a sanity check */
1610 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1611 DMERR("dirty bitset is wrong size");
1615 r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
1619 cmd->changed = true;
1620 return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
1623 int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1624 unsigned int nr_bits,
1625 unsigned long *bits)
1630 if (separate_dirty_bits(cmd))
1631 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1633 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
1639 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1640 struct dm_cache_statistics *stats)
1642 READ_LOCK_VOID(cmd);
1643 *stats = cmd->stats;
1647 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1648 struct dm_cache_statistics *stats)
1650 WRITE_LOCK_VOID(cmd);
1651 cmd->stats = *stats;
1655 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1658 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1659 clear_clean_shutdown);
1665 r = __commit_transaction(cmd, mutator);
1669 r = __begin_transaction(cmd);
1675 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1682 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1688 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1695 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1701 /*----------------------------------------------------------------*/
1703 static int get_hint(uint32_t index, void *value_le, void *context)
1706 struct dm_cache_policy *policy = context;
1708 value = policy_get_hint(policy, to_cblock(index));
1709 *((__le32 *) value_le) = cpu_to_le32(value);
1715 * It's quicker to always delete the hint array, and recreate with
1718 static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1722 const char *policy_name = dm_cache_policy_get_name(policy);
1723 const unsigned int *policy_version = dm_cache_policy_get_version(policy);
1725 if (!policy_name[0] ||
1726 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1729 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1730 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1732 hint_size = dm_cache_policy_get_hint_size(policy);
1734 return 0; /* short-circuit hints initialization */
1735 cmd->policy_hint_size = hint_size;
1737 if (cmd->hint_root) {
1738 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1743 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1744 from_cblock(cmd->cache_blocks),
1748 int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1753 r = write_hints(cmd, policy);
1759 int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1764 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1770 void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1772 WRITE_LOCK_VOID(cmd);
1773 dm_bm_set_read_only(cmd->bm);
1777 void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1779 WRITE_LOCK_VOID(cmd);
1780 dm_bm_set_read_write(cmd->bm);
1784 int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1787 struct dm_block *sblock;
1788 struct cache_disk_superblock *disk_super;
1791 set_bit(NEEDS_CHECK, &cmd->flags);
1793 r = superblock_lock(cmd, &sblock);
1795 DMERR("couldn't read superblock");
1799 disk_super = dm_block_data(sblock);
1800 disk_super->flags = cpu_to_le32(cmd->flags);
1802 dm_bm_unlock(sblock);
1809 int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
1812 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1818 int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1821 struct dm_block_manager *old_bm = NULL, *new_bm = NULL;
1823 /* fail_io is double-checked with cmd->root_lock held below */
1824 if (unlikely(cmd->fail_io))
1828 * Replacement block manager (new_bm) is created and old_bm destroyed outside of
1829 * cmd root_lock to avoid ABBA deadlock that would result (due to life-cycle of
1830 * shrinker associated with the block manager's bufio client vs cmd root_lock).
1831 * - must take shrinker_rwsem without holding cmd->root_lock
1833 new_bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
1834 CACHE_MAX_CONCURRENT_LOCKS);
1842 __destroy_persistent_data_objects(cmd, false);
1844 if (IS_ERR(new_bm)) {
1845 DMERR("could not create block manager during abort");
1847 r = PTR_ERR(new_bm);
1852 r = __open_or_format_metadata(cmd, false);
1860 cmd->fail_io = true;
1862 dm_block_manager_destroy(old_bm);
1864 if (new_bm && !IS_ERR(new_bm))
1865 dm_block_manager_destroy(new_bm);