2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
7 #include "dm-cache-metadata.h"
9 #include "persistent-data/dm-array.h"
10 #include "persistent-data/dm-bitset.h"
11 #include "persistent-data/dm-space-map.h"
12 #include "persistent-data/dm-space-map-disk.h"
13 #include "persistent-data/dm-transaction-manager.h"
15 #include <linux/device-mapper.h>
17 /*----------------------------------------------------------------*/
19 #define DM_MSG_PREFIX "cache metadata"
21 #define CACHE_SUPERBLOCK_MAGIC 06142003
22 #define CACHE_SUPERBLOCK_LOCATION 0
25 * defines a range of metadata versions that this module can handle.
27 #define MIN_CACHE_VERSION 1
28 #define MAX_CACHE_VERSION 1
30 #define CACHE_METADATA_CACHE_SIZE 64
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.
62 struct cache_disk_superblock
{
71 __u8 policy_name
[CACHE_POLICY_NAME_SIZE
];
72 __le32 policy_hint_size
;
74 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
79 __le64 discard_block_size
;
80 __le64 discard_nr_blocks
;
82 __le32 data_block_size
;
83 __le32 metadata_block_size
;
87 __le32 compat_ro_flags
;
88 __le32 incompat_flags
;
95 __le32 policy_version
[CACHE_POLICY_VERSION_SIZE
];
98 struct dm_cache_metadata
{
100 struct list_head list
;
102 struct block_device
*bdev
;
103 struct dm_block_manager
*bm
;
104 struct dm_space_map
*metadata_sm
;
105 struct dm_transaction_manager
*tm
;
107 struct dm_array_info info
;
108 struct dm_array_info hint_info
;
109 struct dm_disk_bitset discard_info
;
111 struct rw_semaphore root_lock
;
114 dm_block_t hint_root
;
115 dm_block_t discard_root
;
117 sector_t discard_block_size
;
118 dm_dblock_t discard_nr_blocks
;
120 sector_t data_block_size
;
121 dm_cblock_t cache_blocks
;
123 bool clean_when_opened
:1;
125 char policy_name
[CACHE_POLICY_NAME_SIZE
];
126 unsigned policy_version
[CACHE_POLICY_VERSION_SIZE
];
127 size_t policy_hint_size
;
128 struct dm_cache_statistics stats
;
131 * Reading the space map root can fail, so we read it into this
132 * buffer before the superblock is locked and updated.
134 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
137 * Set if a transaction has to be aborted but the attempt to roll
138 * back to the previous (good) transaction failed. The only
139 * metadata operation permissible in this state is the closing of
145 /*-------------------------------------------------------------------
146 * superblock validator
147 *-----------------------------------------------------------------*/
149 #define SUPERBLOCK_CSUM_XOR 9031977
151 static void sb_prepare_for_write(struct dm_block_validator
*v
,
153 size_t sb_block_size
)
155 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
157 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
158 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
159 sb_block_size
- sizeof(__le32
),
160 SUPERBLOCK_CSUM_XOR
));
163 static int check_metadata_version(struct cache_disk_superblock
*disk_super
)
165 uint32_t metadata_version
= le32_to_cpu(disk_super
->version
);
166 if (metadata_version
< MIN_CACHE_VERSION
|| metadata_version
> MAX_CACHE_VERSION
) {
167 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
168 metadata_version
, MIN_CACHE_VERSION
, MAX_CACHE_VERSION
);
175 static int sb_check(struct dm_block_validator
*v
,
177 size_t sb_block_size
)
179 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
182 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
183 DMERR("sb_check failed: blocknr %llu: wanted %llu",
184 le64_to_cpu(disk_super
->blocknr
),
185 (unsigned long long)dm_block_location(b
));
189 if (le64_to_cpu(disk_super
->magic
) != CACHE_SUPERBLOCK_MAGIC
) {
190 DMERR("sb_check failed: magic %llu: wanted %llu",
191 le64_to_cpu(disk_super
->magic
),
192 (unsigned long long)CACHE_SUPERBLOCK_MAGIC
);
196 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
197 sb_block_size
- sizeof(__le32
),
198 SUPERBLOCK_CSUM_XOR
));
199 if (csum_le
!= disk_super
->csum
) {
200 DMERR("sb_check failed: csum %u: wanted %u",
201 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
205 return check_metadata_version(disk_super
);
208 static struct dm_block_validator sb_validator
= {
209 .name
= "superblock",
210 .prepare_for_write
= sb_prepare_for_write
,
214 /*----------------------------------------------------------------*/
216 static int superblock_read_lock(struct dm_cache_metadata
*cmd
,
217 struct dm_block
**sblock
)
219 return dm_bm_read_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
220 &sb_validator
, sblock
);
223 static int superblock_lock_zero(struct dm_cache_metadata
*cmd
,
224 struct dm_block
**sblock
)
226 return dm_bm_write_lock_zero(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
227 &sb_validator
, sblock
);
230 static int superblock_lock(struct dm_cache_metadata
*cmd
,
231 struct dm_block
**sblock
)
233 return dm_bm_write_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
234 &sb_validator
, sblock
);
237 /*----------------------------------------------------------------*/
239 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, bool *result
)
244 __le64
*data_le
, zero
= cpu_to_le64(0);
245 unsigned sb_block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
248 * We can't use a validator here - it may be all zeroes.
250 r
= dm_bm_read_lock(bm
, CACHE_SUPERBLOCK_LOCATION
, NULL
, &b
);
254 data_le
= dm_block_data(b
);
256 for (i
= 0; i
< sb_block_size
; i
++) {
257 if (data_le
[i
] != zero
) {
268 static void __setup_mapping_info(struct dm_cache_metadata
*cmd
)
270 struct dm_btree_value_type vt
;
273 vt
.size
= sizeof(__le64
);
277 dm_array_info_init(&cmd
->info
, cmd
->tm
, &vt
);
279 if (cmd
->policy_hint_size
) {
280 vt
.size
= sizeof(__le32
);
281 dm_array_info_init(&cmd
->hint_info
, cmd
->tm
, &vt
);
285 static int __save_sm_root(struct dm_cache_metadata
*cmd
)
290 r
= dm_sm_root_size(cmd
->metadata_sm
, &metadata_len
);
294 return dm_sm_copy_root(cmd
->metadata_sm
, &cmd
->metadata_space_map_root
,
298 static void __copy_sm_root(struct dm_cache_metadata
*cmd
,
299 struct cache_disk_superblock
*disk_super
)
301 memcpy(&disk_super
->metadata_space_map_root
,
302 &cmd
->metadata_space_map_root
,
303 sizeof(cmd
->metadata_space_map_root
));
306 static int __write_initial_superblock(struct dm_cache_metadata
*cmd
)
309 struct dm_block
*sblock
;
310 struct cache_disk_superblock
*disk_super
;
311 sector_t bdev_size
= i_size_read(cmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
313 /* FIXME: see if we can lose the max sectors limit */
314 if (bdev_size
> DM_CACHE_METADATA_MAX_SECTORS
)
315 bdev_size
= DM_CACHE_METADATA_MAX_SECTORS
;
317 r
= dm_tm_pre_commit(cmd
->tm
);
322 * dm_sm_copy_root() can fail. So we need to do it before we start
323 * updating the superblock.
325 r
= __save_sm_root(cmd
);
329 r
= superblock_lock_zero(cmd
, &sblock
);
333 disk_super
= dm_block_data(sblock
);
334 disk_super
->flags
= 0;
335 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
336 disk_super
->magic
= cpu_to_le64(CACHE_SUPERBLOCK_MAGIC
);
337 disk_super
->version
= cpu_to_le32(MAX_CACHE_VERSION
);
338 memset(disk_super
->policy_name
, 0, sizeof(disk_super
->policy_name
));
339 memset(disk_super
->policy_version
, 0, sizeof(disk_super
->policy_version
));
340 disk_super
->policy_hint_size
= cpu_to_le32(0);
342 __copy_sm_root(cmd
, disk_super
);
344 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
345 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
346 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
347 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
348 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
349 disk_super
->metadata_block_size
= cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE
);
350 disk_super
->data_block_size
= cpu_to_le32(cmd
->data_block_size
);
351 disk_super
->cache_blocks
= cpu_to_le32(0);
353 disk_super
->read_hits
= cpu_to_le32(0);
354 disk_super
->read_misses
= cpu_to_le32(0);
355 disk_super
->write_hits
= cpu_to_le32(0);
356 disk_super
->write_misses
= cpu_to_le32(0);
358 return dm_tm_commit(cmd
->tm
, sblock
);
361 static int __format_metadata(struct dm_cache_metadata
*cmd
)
365 r
= dm_tm_create_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
366 &cmd
->tm
, &cmd
->metadata_sm
);
368 DMERR("tm_create_with_sm failed");
372 __setup_mapping_info(cmd
);
374 r
= dm_array_empty(&cmd
->info
, &cmd
->root
);
378 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
380 r
= dm_bitset_empty(&cmd
->discard_info
, &cmd
->discard_root
);
384 cmd
->discard_block_size
= 0;
385 cmd
->discard_nr_blocks
= 0;
387 r
= __write_initial_superblock(cmd
);
391 cmd
->clean_when_opened
= true;
395 dm_tm_destroy(cmd
->tm
);
396 dm_sm_destroy(cmd
->metadata_sm
);
401 static int __check_incompat_features(struct cache_disk_superblock
*disk_super
,
402 struct dm_cache_metadata
*cmd
)
406 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP
;
408 DMERR("could not access metadata due to unsupported optional features (%lx).",
409 (unsigned long)features
);
414 * Check for read-only metadata to skip the following RDWR checks.
416 if (get_disk_ro(cmd
->bdev
->bd_disk
))
419 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP
;
421 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
422 (unsigned long)features
);
429 static int __open_metadata(struct dm_cache_metadata
*cmd
)
432 struct dm_block
*sblock
;
433 struct cache_disk_superblock
*disk_super
;
434 unsigned long sb_flags
;
436 r
= superblock_read_lock(cmd
, &sblock
);
438 DMERR("couldn't read lock superblock");
442 disk_super
= dm_block_data(sblock
);
444 /* Verify the data block size hasn't changed */
445 if (le32_to_cpu(disk_super
->data_block_size
) != cmd
->data_block_size
) {
446 DMERR("changing the data block size (from %u to %llu) is not supported",
447 le32_to_cpu(disk_super
->data_block_size
),
448 (unsigned long long)cmd
->data_block_size
);
453 r
= __check_incompat_features(disk_super
, cmd
);
457 r
= dm_tm_open_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
458 disk_super
->metadata_space_map_root
,
459 sizeof(disk_super
->metadata_space_map_root
),
460 &cmd
->tm
, &cmd
->metadata_sm
);
462 DMERR("tm_open_with_sm failed");
466 __setup_mapping_info(cmd
);
467 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
468 sb_flags
= le32_to_cpu(disk_super
->flags
);
469 cmd
->clean_when_opened
= test_bit(CLEAN_SHUTDOWN
, &sb_flags
);
470 dm_bm_unlock(sblock
);
475 dm_bm_unlock(sblock
);
479 static int __open_or_format_metadata(struct dm_cache_metadata
*cmd
,
483 bool unformatted
= false;
485 r
= __superblock_all_zeroes(cmd
->bm
, &unformatted
);
490 return format_device
? __format_metadata(cmd
) : -EPERM
;
492 return __open_metadata(cmd
);
495 static int __create_persistent_data_objects(struct dm_cache_metadata
*cmd
,
496 bool may_format_device
)
499 cmd
->bm
= dm_block_manager_create(cmd
->bdev
, DM_CACHE_METADATA_BLOCK_SIZE
<< SECTOR_SHIFT
,
500 CACHE_METADATA_CACHE_SIZE
,
501 CACHE_MAX_CONCURRENT_LOCKS
);
502 if (IS_ERR(cmd
->bm
)) {
503 DMERR("could not create block manager");
504 return PTR_ERR(cmd
->bm
);
507 r
= __open_or_format_metadata(cmd
, may_format_device
);
509 dm_block_manager_destroy(cmd
->bm
);
514 static void __destroy_persistent_data_objects(struct dm_cache_metadata
*cmd
)
516 dm_sm_destroy(cmd
->metadata_sm
);
517 dm_tm_destroy(cmd
->tm
);
518 dm_block_manager_destroy(cmd
->bm
);
521 typedef unsigned long (*flags_mutator
)(unsigned long);
523 static void update_flags(struct cache_disk_superblock
*disk_super
,
524 flags_mutator mutator
)
526 uint32_t sb_flags
= mutator(le32_to_cpu(disk_super
->flags
));
527 disk_super
->flags
= cpu_to_le32(sb_flags
);
530 static unsigned long set_clean_shutdown(unsigned long flags
)
532 set_bit(CLEAN_SHUTDOWN
, &flags
);
536 static unsigned long clear_clean_shutdown(unsigned long flags
)
538 clear_bit(CLEAN_SHUTDOWN
, &flags
);
542 static void read_superblock_fields(struct dm_cache_metadata
*cmd
,
543 struct cache_disk_superblock
*disk_super
)
545 cmd
->flags
= le32_to_cpu(disk_super
->flags
);
546 cmd
->root
= le64_to_cpu(disk_super
->mapping_root
);
547 cmd
->hint_root
= le64_to_cpu(disk_super
->hint_root
);
548 cmd
->discard_root
= le64_to_cpu(disk_super
->discard_root
);
549 cmd
->discard_block_size
= le64_to_cpu(disk_super
->discard_block_size
);
550 cmd
->discard_nr_blocks
= to_dblock(le64_to_cpu(disk_super
->discard_nr_blocks
));
551 cmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
552 cmd
->cache_blocks
= to_cblock(le32_to_cpu(disk_super
->cache_blocks
));
553 strncpy(cmd
->policy_name
, disk_super
->policy_name
, sizeof(cmd
->policy_name
));
554 cmd
->policy_version
[0] = le32_to_cpu(disk_super
->policy_version
[0]);
555 cmd
->policy_version
[1] = le32_to_cpu(disk_super
->policy_version
[1]);
556 cmd
->policy_version
[2] = le32_to_cpu(disk_super
->policy_version
[2]);
557 cmd
->policy_hint_size
= le32_to_cpu(disk_super
->policy_hint_size
);
559 cmd
->stats
.read_hits
= le32_to_cpu(disk_super
->read_hits
);
560 cmd
->stats
.read_misses
= le32_to_cpu(disk_super
->read_misses
);
561 cmd
->stats
.write_hits
= le32_to_cpu(disk_super
->write_hits
);
562 cmd
->stats
.write_misses
= le32_to_cpu(disk_super
->write_misses
);
564 cmd
->changed
= false;
568 * The mutator updates the superblock flags.
570 static int __begin_transaction_flags(struct dm_cache_metadata
*cmd
,
571 flags_mutator mutator
)
574 struct cache_disk_superblock
*disk_super
;
575 struct dm_block
*sblock
;
577 r
= superblock_lock(cmd
, &sblock
);
581 disk_super
= dm_block_data(sblock
);
582 update_flags(disk_super
, mutator
);
583 read_superblock_fields(cmd
, disk_super
);
584 dm_bm_unlock(sblock
);
586 return dm_bm_flush(cmd
->bm
);
589 static int __begin_transaction(struct dm_cache_metadata
*cmd
)
592 struct cache_disk_superblock
*disk_super
;
593 struct dm_block
*sblock
;
596 * We re-read the superblock every time. Shouldn't need to do this
599 r
= superblock_read_lock(cmd
, &sblock
);
603 disk_super
= dm_block_data(sblock
);
604 read_superblock_fields(cmd
, disk_super
);
605 dm_bm_unlock(sblock
);
610 static int __commit_transaction(struct dm_cache_metadata
*cmd
,
611 flags_mutator mutator
)
614 struct cache_disk_superblock
*disk_super
;
615 struct dm_block
*sblock
;
618 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
620 BUILD_BUG_ON(sizeof(struct cache_disk_superblock
) > 512);
622 r
= dm_bitset_flush(&cmd
->discard_info
, cmd
->discard_root
,
627 r
= dm_tm_pre_commit(cmd
->tm
);
631 r
= __save_sm_root(cmd
);
635 r
= superblock_lock(cmd
, &sblock
);
639 disk_super
= dm_block_data(sblock
);
641 disk_super
->flags
= cpu_to_le32(cmd
->flags
);
643 update_flags(disk_super
, mutator
);
645 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
646 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
647 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
648 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
649 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
650 disk_super
->cache_blocks
= cpu_to_le32(from_cblock(cmd
->cache_blocks
));
651 strncpy(disk_super
->policy_name
, cmd
->policy_name
, sizeof(disk_super
->policy_name
));
652 disk_super
->policy_version
[0] = cpu_to_le32(cmd
->policy_version
[0]);
653 disk_super
->policy_version
[1] = cpu_to_le32(cmd
->policy_version
[1]);
654 disk_super
->policy_version
[2] = cpu_to_le32(cmd
->policy_version
[2]);
655 disk_super
->policy_hint_size
= cpu_to_le32(cmd
->policy_hint_size
);
657 disk_super
->read_hits
= cpu_to_le32(cmd
->stats
.read_hits
);
658 disk_super
->read_misses
= cpu_to_le32(cmd
->stats
.read_misses
);
659 disk_super
->write_hits
= cpu_to_le32(cmd
->stats
.write_hits
);
660 disk_super
->write_misses
= cpu_to_le32(cmd
->stats
.write_misses
);
661 __copy_sm_root(cmd
, disk_super
);
663 return dm_tm_commit(cmd
->tm
, sblock
);
666 /*----------------------------------------------------------------*/
669 * The mappings are held in a dm-array that has 64-bit values stored in
670 * little-endian format. The index is the cblock, the high 48bits of the
671 * value are the oblock and the low 16 bit the flags.
673 #define FLAGS_MASK ((1 << 16) - 1)
675 static __le64
pack_value(dm_oblock_t block
, unsigned flags
)
677 uint64_t value
= from_oblock(block
);
679 value
= value
| (flags
& FLAGS_MASK
);
680 return cpu_to_le64(value
);
683 static void unpack_value(__le64 value_le
, dm_oblock_t
*block
, unsigned *flags
)
685 uint64_t value
= le64_to_cpu(value_le
);
686 uint64_t b
= value
>> 16;
687 *block
= to_oblock(b
);
688 *flags
= value
& FLAGS_MASK
;
691 /*----------------------------------------------------------------*/
693 static struct dm_cache_metadata
*metadata_open(struct block_device
*bdev
,
694 sector_t data_block_size
,
695 bool may_format_device
,
696 size_t policy_hint_size
)
699 struct dm_cache_metadata
*cmd
;
701 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
703 DMERR("could not allocate metadata struct");
704 return ERR_PTR(-ENOMEM
);
707 atomic_set(&cmd
->ref_count
, 1);
708 init_rwsem(&cmd
->root_lock
);
710 cmd
->data_block_size
= data_block_size
;
711 cmd
->cache_blocks
= 0;
712 cmd
->policy_hint_size
= policy_hint_size
;
714 cmd
->fail_io
= false;
716 r
= __create_persistent_data_objects(cmd
, may_format_device
);
722 r
= __begin_transaction_flags(cmd
, clear_clean_shutdown
);
724 dm_cache_metadata_close(cmd
);
732 * We keep a little list of ref counted metadata objects to prevent two
733 * different target instances creating separate bufio instances. This is
734 * an issue if a table is reloaded before the suspend.
736 static DEFINE_MUTEX(table_lock
);
737 static LIST_HEAD(table
);
739 static struct dm_cache_metadata
*lookup(struct block_device
*bdev
)
741 struct dm_cache_metadata
*cmd
;
743 list_for_each_entry(cmd
, &table
, list
)
744 if (cmd
->bdev
== bdev
) {
745 atomic_inc(&cmd
->ref_count
);
752 static struct dm_cache_metadata
*lookup_or_open(struct block_device
*bdev
,
753 sector_t data_block_size
,
754 bool may_format_device
,
755 size_t policy_hint_size
)
757 struct dm_cache_metadata
*cmd
, *cmd2
;
759 mutex_lock(&table_lock
);
761 mutex_unlock(&table_lock
);
766 cmd
= metadata_open(bdev
, data_block_size
, may_format_device
, policy_hint_size
);
768 mutex_lock(&table_lock
);
771 mutex_unlock(&table_lock
);
772 __destroy_persistent_data_objects(cmd
);
776 list_add(&cmd
->list
, &table
);
777 mutex_unlock(&table_lock
);
783 static bool same_params(struct dm_cache_metadata
*cmd
, sector_t data_block_size
)
785 if (cmd
->data_block_size
!= data_block_size
) {
786 DMERR("data_block_size (%llu) different from that in metadata (%llu)\n",
787 (unsigned long long) data_block_size
,
788 (unsigned long long) cmd
->data_block_size
);
795 struct dm_cache_metadata
*dm_cache_metadata_open(struct block_device
*bdev
,
796 sector_t data_block_size
,
797 bool may_format_device
,
798 size_t policy_hint_size
)
800 struct dm_cache_metadata
*cmd
= lookup_or_open(bdev
, data_block_size
,
801 may_format_device
, policy_hint_size
);
803 if (!IS_ERR(cmd
) && !same_params(cmd
, data_block_size
)) {
804 dm_cache_metadata_close(cmd
);
805 return ERR_PTR(-EINVAL
);
811 void dm_cache_metadata_close(struct dm_cache_metadata
*cmd
)
813 if (atomic_dec_and_test(&cmd
->ref_count
)) {
814 mutex_lock(&table_lock
);
815 list_del(&cmd
->list
);
816 mutex_unlock(&table_lock
);
819 __destroy_persistent_data_objects(cmd
);
825 * Checks that the given cache block is either unmapped or clean.
827 static int block_unmapped_or_clean(struct dm_cache_metadata
*cmd
, dm_cblock_t b
,
835 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(b
), &value
);
837 DMERR("block_unmapped_or_clean failed");
841 unpack_value(value
, &ob
, &flags
);
842 *result
= !((flags
& M_VALID
) && (flags
& M_DIRTY
));
847 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata
*cmd
,
848 dm_cblock_t begin
, dm_cblock_t end
,
854 while (begin
!= end
) {
855 r
= block_unmapped_or_clean(cmd
, begin
, result
);
860 DMERR("cache block %llu is dirty",
861 (unsigned long long) from_cblock(begin
));
865 begin
= to_cblock(from_cblock(begin
) + 1);
871 static bool cmd_write_lock(struct dm_cache_metadata
*cmd
)
873 down_write(&cmd
->root_lock
);
874 if (cmd
->fail_io
|| dm_bm_is_read_only(cmd
->bm
)) {
875 up_write(&cmd
->root_lock
);
881 #define WRITE_LOCK(cmd) \
883 if (!cmd_write_lock((cmd))) \
887 #define WRITE_LOCK_VOID(cmd) \
889 if (!cmd_write_lock((cmd))) \
893 #define WRITE_UNLOCK(cmd) \
894 up_write(&(cmd)->root_lock)
896 static bool cmd_read_lock(struct dm_cache_metadata
*cmd
)
898 down_read(&cmd
->root_lock
);
900 up_read(&cmd
->root_lock
);
906 #define READ_LOCK(cmd) \
908 if (!cmd_read_lock((cmd))) \
912 #define READ_LOCK_VOID(cmd) \
914 if (!cmd_read_lock((cmd))) \
918 #define READ_UNLOCK(cmd) \
919 up_read(&(cmd)->root_lock)
921 int dm_cache_resize(struct dm_cache_metadata
*cmd
, dm_cblock_t new_cache_size
)
925 __le64 null_mapping
= pack_value(0, 0);
928 __dm_bless_for_disk(&null_mapping
);
930 if (from_cblock(new_cache_size
) < from_cblock(cmd
->cache_blocks
)) {
931 r
= blocks_are_unmapped_or_clean(cmd
, new_cache_size
, cmd
->cache_blocks
, &clean
);
933 __dm_unbless_for_disk(&null_mapping
);
938 DMERR("unable to shrink cache due to dirty blocks");
940 __dm_unbless_for_disk(&null_mapping
);
945 r
= dm_array_resize(&cmd
->info
, cmd
->root
, from_cblock(cmd
->cache_blocks
),
946 from_cblock(new_cache_size
),
947 &null_mapping
, &cmd
->root
);
949 cmd
->cache_blocks
= new_cache_size
;
958 int dm_cache_discard_bitset_resize(struct dm_cache_metadata
*cmd
,
959 sector_t discard_block_size
,
960 dm_dblock_t new_nr_entries
)
965 r
= dm_bitset_resize(&cmd
->discard_info
,
967 from_dblock(cmd
->discard_nr_blocks
),
968 from_dblock(new_nr_entries
),
969 false, &cmd
->discard_root
);
971 cmd
->discard_block_size
= discard_block_size
;
972 cmd
->discard_nr_blocks
= new_nr_entries
;
981 static int __set_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
983 return dm_bitset_set_bit(&cmd
->discard_info
, cmd
->discard_root
,
984 from_dblock(b
), &cmd
->discard_root
);
987 static int __clear_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
989 return dm_bitset_clear_bit(&cmd
->discard_info
, cmd
->discard_root
,
990 from_dblock(b
), &cmd
->discard_root
);
993 static int __is_discarded(struct dm_cache_metadata
*cmd
, dm_dblock_t b
,
996 return dm_bitset_test_bit(&cmd
->discard_info
, cmd
->discard_root
,
997 from_dblock(b
), &cmd
->discard_root
,
1001 static int __discard(struct dm_cache_metadata
*cmd
,
1002 dm_dblock_t dblock
, bool discard
)
1006 r
= (discard
? __set_discard
: __clear_discard
)(cmd
, dblock
);
1010 cmd
->changed
= true;
1014 int dm_cache_set_discard(struct dm_cache_metadata
*cmd
,
1015 dm_dblock_t dblock
, bool discard
)
1020 r
= __discard(cmd
, dblock
, discard
);
1026 static int __load_discards(struct dm_cache_metadata
*cmd
,
1027 load_discard_fn fn
, void *context
)
1033 for (b
= 0; b
< from_dblock(cmd
->discard_nr_blocks
); b
++) {
1034 dm_dblock_t dblock
= to_dblock(b
);
1036 if (cmd
->clean_when_opened
) {
1037 r
= __is_discarded(cmd
, dblock
, &discard
);
1043 r
= fn(context
, cmd
->discard_block_size
, dblock
, discard
);
1051 int dm_cache_load_discards(struct dm_cache_metadata
*cmd
,
1052 load_discard_fn fn
, void *context
)
1057 r
= __load_discards(cmd
, fn
, context
);
1063 int dm_cache_size(struct dm_cache_metadata
*cmd
, dm_cblock_t
*result
)
1066 *result
= cmd
->cache_blocks
;
1072 static int __remove(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
1075 __le64 value
= pack_value(0, 0);
1077 __dm_bless_for_disk(&value
);
1078 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1079 &value
, &cmd
->root
);
1083 cmd
->changed
= true;
1087 int dm_cache_remove_mapping(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
1092 r
= __remove(cmd
, cblock
);
1098 static int __insert(struct dm_cache_metadata
*cmd
,
1099 dm_cblock_t cblock
, dm_oblock_t oblock
)
1102 __le64 value
= pack_value(oblock
, M_VALID
);
1103 __dm_bless_for_disk(&value
);
1105 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1106 &value
, &cmd
->root
);
1110 cmd
->changed
= true;
1114 int dm_cache_insert_mapping(struct dm_cache_metadata
*cmd
,
1115 dm_cblock_t cblock
, dm_oblock_t oblock
)
1120 r
= __insert(cmd
, cblock
, oblock
);
1130 struct dm_cache_metadata
*cmd
;
1131 bool respect_dirty_flags
;
1135 static bool policy_unchanged(struct dm_cache_metadata
*cmd
,
1136 struct dm_cache_policy
*policy
)
1138 const char *policy_name
= dm_cache_policy_get_name(policy
);
1139 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1140 size_t policy_hint_size
= dm_cache_policy_get_hint_size(policy
);
1143 * Ensure policy names match.
1145 if (strncmp(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
)))
1149 * Ensure policy major versions match.
1151 if (cmd
->policy_version
[0] != policy_version
[0])
1155 * Ensure policy hint sizes match.
1157 if (cmd
->policy_hint_size
!= policy_hint_size
)
1163 static bool hints_array_initialized(struct dm_cache_metadata
*cmd
)
1165 return cmd
->hint_root
&& cmd
->policy_hint_size
;
1168 static bool hints_array_available(struct dm_cache_metadata
*cmd
,
1169 struct dm_cache_policy
*policy
)
1171 return cmd
->clean_when_opened
&& policy_unchanged(cmd
, policy
) &&
1172 hints_array_initialized(cmd
);
1175 static int __load_mapping(void *context
, uint64_t cblock
, void *leaf
)
1180 __le32 hint_value
= 0;
1183 struct thunk
*thunk
= context
;
1184 struct dm_cache_metadata
*cmd
= thunk
->cmd
;
1186 memcpy(&value
, leaf
, sizeof(value
));
1187 unpack_value(value
, &oblock
, &flags
);
1189 if (flags
& M_VALID
) {
1190 if (thunk
->hints_valid
) {
1191 r
= dm_array_get_value(&cmd
->hint_info
, cmd
->hint_root
,
1192 cblock
, &hint_value
);
1193 if (r
&& r
!= -ENODATA
)
1197 dirty
= thunk
->respect_dirty_flags
? (flags
& M_DIRTY
) : true;
1198 r
= thunk
->fn(thunk
->context
, oblock
, to_cblock(cblock
),
1199 dirty
, le32_to_cpu(hint_value
), thunk
->hints_valid
);
1205 static int __load_mappings(struct dm_cache_metadata
*cmd
,
1206 struct dm_cache_policy
*policy
,
1207 load_mapping_fn fn
, void *context
)
1212 thunk
.context
= context
;
1215 thunk
.respect_dirty_flags
= cmd
->clean_when_opened
;
1216 thunk
.hints_valid
= hints_array_available(cmd
, policy
);
1218 return dm_array_walk(&cmd
->info
, cmd
->root
, __load_mapping
, &thunk
);
1221 int dm_cache_load_mappings(struct dm_cache_metadata
*cmd
,
1222 struct dm_cache_policy
*policy
,
1223 load_mapping_fn fn
, void *context
)
1228 r
= __load_mappings(cmd
, policy
, fn
, context
);
1234 static int __dump_mapping(void *context
, uint64_t cblock
, void *leaf
)
1241 memcpy(&value
, leaf
, sizeof(value
));
1242 unpack_value(value
, &oblock
, &flags
);
1247 static int __dump_mappings(struct dm_cache_metadata
*cmd
)
1249 return dm_array_walk(&cmd
->info
, cmd
->root
, __dump_mapping
, NULL
);
1252 void dm_cache_dump(struct dm_cache_metadata
*cmd
)
1254 READ_LOCK_VOID(cmd
);
1255 __dump_mappings(cmd
);
1259 int dm_cache_changed_this_transaction(struct dm_cache_metadata
*cmd
)
1270 static int __dirty(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
, bool dirty
)
1277 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
), &value
);
1281 unpack_value(value
, &oblock
, &flags
);
1283 if (((flags
& M_DIRTY
) && dirty
) || (!(flags
& M_DIRTY
) && !dirty
))
1284 /* nothing to be done */
1287 value
= pack_value(oblock
, (flags
& ~M_DIRTY
) | (dirty
? M_DIRTY
: 0));
1288 __dm_bless_for_disk(&value
);
1290 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1291 &value
, &cmd
->root
);
1295 cmd
->changed
= true;
1300 int dm_cache_set_dirty(struct dm_cache_metadata
*cmd
,
1301 dm_cblock_t cblock
, bool dirty
)
1306 r
= __dirty(cmd
, cblock
, dirty
);
1312 void dm_cache_metadata_get_stats(struct dm_cache_metadata
*cmd
,
1313 struct dm_cache_statistics
*stats
)
1315 READ_LOCK_VOID(cmd
);
1316 *stats
= cmd
->stats
;
1320 void dm_cache_metadata_set_stats(struct dm_cache_metadata
*cmd
,
1321 struct dm_cache_statistics
*stats
)
1323 WRITE_LOCK_VOID(cmd
);
1324 cmd
->stats
= *stats
;
1328 int dm_cache_commit(struct dm_cache_metadata
*cmd
, bool clean_shutdown
)
1331 flags_mutator mutator
= (clean_shutdown
? set_clean_shutdown
:
1332 clear_clean_shutdown
);
1338 r
= __commit_transaction(cmd
, mutator
);
1342 r
= __begin_transaction(cmd
);
1348 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata
*cmd
,
1355 r
= dm_sm_get_nr_free(cmd
->metadata_sm
, result
);
1361 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata
*cmd
,
1368 r
= dm_sm_get_nr_blocks(cmd
->metadata_sm
, result
);
1374 /*----------------------------------------------------------------*/
1376 static int begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1381 const char *policy_name
= dm_cache_policy_get_name(policy
);
1382 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1384 if (!policy_name
[0] ||
1385 (strlen(policy_name
) > sizeof(cmd
->policy_name
) - 1))
1388 if (!policy_unchanged(cmd
, policy
)) {
1389 strncpy(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
));
1390 memcpy(cmd
->policy_version
, policy_version
, sizeof(cmd
->policy_version
));
1392 hint_size
= dm_cache_policy_get_hint_size(policy
);
1394 return 0; /* short-circuit hints initialization */
1395 cmd
->policy_hint_size
= hint_size
;
1397 if (cmd
->hint_root
) {
1398 r
= dm_array_del(&cmd
->hint_info
, cmd
->hint_root
);
1403 r
= dm_array_empty(&cmd
->hint_info
, &cmd
->hint_root
);
1407 value
= cpu_to_le32(0);
1408 __dm_bless_for_disk(&value
);
1409 r
= dm_array_resize(&cmd
->hint_info
, cmd
->hint_root
, 0,
1410 from_cblock(cmd
->cache_blocks
),
1411 &value
, &cmd
->hint_root
);
1419 static int save_hint(void *context
, dm_cblock_t cblock
, dm_oblock_t oblock
, uint32_t hint
)
1421 struct dm_cache_metadata
*cmd
= context
;
1422 __le32 value
= cpu_to_le32(hint
);
1425 __dm_bless_for_disk(&value
);
1427 r
= dm_array_set_value(&cmd
->hint_info
, cmd
->hint_root
,
1428 from_cblock(cblock
), &value
, &cmd
->hint_root
);
1429 cmd
->changed
= true;
1434 static int write_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1438 r
= begin_hints(cmd
, policy
);
1440 DMERR("begin_hints failed");
1444 return policy_walk_mappings(policy
, save_hint
, cmd
);
1447 int dm_cache_write_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1452 r
= write_hints(cmd
, policy
);
1458 int dm_cache_metadata_all_clean(struct dm_cache_metadata
*cmd
, bool *result
)
1463 r
= blocks_are_unmapped_or_clean(cmd
, 0, cmd
->cache_blocks
, result
);
1469 void dm_cache_metadata_set_read_only(struct dm_cache_metadata
*cmd
)
1471 WRITE_LOCK_VOID(cmd
);
1472 dm_bm_set_read_only(cmd
->bm
);
1476 void dm_cache_metadata_set_read_write(struct dm_cache_metadata
*cmd
)
1478 WRITE_LOCK_VOID(cmd
);
1479 dm_bm_set_read_write(cmd
->bm
);
1483 int dm_cache_metadata_set_needs_check(struct dm_cache_metadata
*cmd
)
1486 struct dm_block
*sblock
;
1487 struct cache_disk_superblock
*disk_super
;
1490 set_bit(NEEDS_CHECK
, &cmd
->flags
);
1492 r
= superblock_lock(cmd
, &sblock
);
1494 DMERR("couldn't read superblock");
1498 disk_super
= dm_block_data(sblock
);
1499 disk_super
->flags
= cpu_to_le32(cmd
->flags
);
1501 dm_bm_unlock(sblock
);
1508 int dm_cache_metadata_needs_check(struct dm_cache_metadata
*cmd
, bool *result
)
1511 *result
= !!test_bit(NEEDS_CHECK
, &cmd
->flags
);
1517 int dm_cache_metadata_abort(struct dm_cache_metadata
*cmd
)
1522 __destroy_persistent_data_objects(cmd
);
1523 r
= __create_persistent_data_objects(cmd
, false);
1525 cmd
->fail_io
= true;