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 */
45 * Each mapping from cache block -> origin block carries a set of flags.
49 * A valid mapping. Because we're using an array we clear this
50 * flag for an non existant mapping.
55 * The data on the cache is different from that on the origin.
60 struct cache_disk_superblock
{
69 __u8 policy_name
[CACHE_POLICY_NAME_SIZE
];
70 __le32 policy_hint_size
;
72 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
77 __le64 discard_block_size
;
78 __le64 discard_nr_blocks
;
80 __le32 data_block_size
;
81 __le32 metadata_block_size
;
85 __le32 compat_ro_flags
;
86 __le32 incompat_flags
;
93 __le32 policy_version
[CACHE_POLICY_VERSION_SIZE
];
96 struct dm_cache_metadata
{
98 struct list_head list
;
100 struct block_device
*bdev
;
101 struct dm_block_manager
*bm
;
102 struct dm_space_map
*metadata_sm
;
103 struct dm_transaction_manager
*tm
;
105 struct dm_array_info info
;
106 struct dm_array_info hint_info
;
107 struct dm_disk_bitset discard_info
;
109 struct rw_semaphore root_lock
;
111 dm_block_t hint_root
;
112 dm_block_t discard_root
;
114 sector_t discard_block_size
;
115 dm_dblock_t discard_nr_blocks
;
117 sector_t data_block_size
;
118 dm_cblock_t cache_blocks
;
120 bool clean_when_opened
:1;
122 char policy_name
[CACHE_POLICY_NAME_SIZE
];
123 unsigned policy_version
[CACHE_POLICY_VERSION_SIZE
];
124 size_t policy_hint_size
;
125 struct dm_cache_statistics stats
;
128 * Reading the space map root can fail, so we read it into this
129 * buffer before the superblock is locked and updated.
131 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
134 /*-------------------------------------------------------------------
135 * superblock validator
136 *-----------------------------------------------------------------*/
138 #define SUPERBLOCK_CSUM_XOR 9031977
140 static void sb_prepare_for_write(struct dm_block_validator
*v
,
142 size_t sb_block_size
)
144 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
146 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
147 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
148 sb_block_size
- sizeof(__le32
),
149 SUPERBLOCK_CSUM_XOR
));
152 static int check_metadata_version(struct cache_disk_superblock
*disk_super
)
154 uint32_t metadata_version
= le32_to_cpu(disk_super
->version
);
155 if (metadata_version
< MIN_CACHE_VERSION
|| metadata_version
> MAX_CACHE_VERSION
) {
156 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
157 metadata_version
, MIN_CACHE_VERSION
, MAX_CACHE_VERSION
);
164 static int sb_check(struct dm_block_validator
*v
,
166 size_t sb_block_size
)
168 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
171 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
172 DMERR("sb_check failed: blocknr %llu: wanted %llu",
173 le64_to_cpu(disk_super
->blocknr
),
174 (unsigned long long)dm_block_location(b
));
178 if (le64_to_cpu(disk_super
->magic
) != CACHE_SUPERBLOCK_MAGIC
) {
179 DMERR("sb_check failed: magic %llu: wanted %llu",
180 le64_to_cpu(disk_super
->magic
),
181 (unsigned long long)CACHE_SUPERBLOCK_MAGIC
);
185 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
186 sb_block_size
- sizeof(__le32
),
187 SUPERBLOCK_CSUM_XOR
));
188 if (csum_le
!= disk_super
->csum
) {
189 DMERR("sb_check failed: csum %u: wanted %u",
190 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
194 return check_metadata_version(disk_super
);
197 static struct dm_block_validator sb_validator
= {
198 .name
= "superblock",
199 .prepare_for_write
= sb_prepare_for_write
,
203 /*----------------------------------------------------------------*/
205 static int superblock_read_lock(struct dm_cache_metadata
*cmd
,
206 struct dm_block
**sblock
)
208 return dm_bm_read_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
209 &sb_validator
, sblock
);
212 static int superblock_lock_zero(struct dm_cache_metadata
*cmd
,
213 struct dm_block
**sblock
)
215 return dm_bm_write_lock_zero(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
216 &sb_validator
, sblock
);
219 static int superblock_lock(struct dm_cache_metadata
*cmd
,
220 struct dm_block
**sblock
)
222 return dm_bm_write_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
223 &sb_validator
, sblock
);
226 /*----------------------------------------------------------------*/
228 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, bool *result
)
233 __le64
*data_le
, zero
= cpu_to_le64(0);
234 unsigned sb_block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
237 * We can't use a validator here - it may be all zeroes.
239 r
= dm_bm_read_lock(bm
, CACHE_SUPERBLOCK_LOCATION
, NULL
, &b
);
243 data_le
= dm_block_data(b
);
245 for (i
= 0; i
< sb_block_size
; i
++) {
246 if (data_le
[i
] != zero
) {
252 return dm_bm_unlock(b
);
255 static void __setup_mapping_info(struct dm_cache_metadata
*cmd
)
257 struct dm_btree_value_type vt
;
260 vt
.size
= sizeof(__le64
);
264 dm_array_info_init(&cmd
->info
, cmd
->tm
, &vt
);
266 if (cmd
->policy_hint_size
) {
267 vt
.size
= sizeof(__le32
);
268 dm_array_info_init(&cmd
->hint_info
, cmd
->tm
, &vt
);
272 static int __save_sm_root(struct dm_cache_metadata
*cmd
)
277 r
= dm_sm_root_size(cmd
->metadata_sm
, &metadata_len
);
281 return dm_sm_copy_root(cmd
->metadata_sm
, &cmd
->metadata_space_map_root
,
285 static void __copy_sm_root(struct dm_cache_metadata
*cmd
,
286 struct cache_disk_superblock
*disk_super
)
288 memcpy(&disk_super
->metadata_space_map_root
,
289 &cmd
->metadata_space_map_root
,
290 sizeof(cmd
->metadata_space_map_root
));
293 static int __write_initial_superblock(struct dm_cache_metadata
*cmd
)
296 struct dm_block
*sblock
;
297 struct cache_disk_superblock
*disk_super
;
298 sector_t bdev_size
= i_size_read(cmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
300 /* FIXME: see if we can lose the max sectors limit */
301 if (bdev_size
> DM_CACHE_METADATA_MAX_SECTORS
)
302 bdev_size
= DM_CACHE_METADATA_MAX_SECTORS
;
304 r
= dm_tm_pre_commit(cmd
->tm
);
309 * dm_sm_copy_root() can fail. So we need to do it before we start
310 * updating the superblock.
312 r
= __save_sm_root(cmd
);
316 r
= superblock_lock_zero(cmd
, &sblock
);
320 disk_super
= dm_block_data(sblock
);
321 disk_super
->flags
= 0;
322 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
323 disk_super
->magic
= cpu_to_le64(CACHE_SUPERBLOCK_MAGIC
);
324 disk_super
->version
= cpu_to_le32(MAX_CACHE_VERSION
);
325 memset(disk_super
->policy_name
, 0, sizeof(disk_super
->policy_name
));
326 memset(disk_super
->policy_version
, 0, sizeof(disk_super
->policy_version
));
327 disk_super
->policy_hint_size
= 0;
329 __copy_sm_root(cmd
, disk_super
);
331 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
332 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
333 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
334 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
335 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
336 disk_super
->metadata_block_size
= cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE
);
337 disk_super
->data_block_size
= cpu_to_le32(cmd
->data_block_size
);
338 disk_super
->cache_blocks
= cpu_to_le32(0);
340 disk_super
->read_hits
= cpu_to_le32(0);
341 disk_super
->read_misses
= cpu_to_le32(0);
342 disk_super
->write_hits
= cpu_to_le32(0);
343 disk_super
->write_misses
= cpu_to_le32(0);
345 return dm_tm_commit(cmd
->tm
, sblock
);
348 static int __format_metadata(struct dm_cache_metadata
*cmd
)
352 r
= dm_tm_create_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
353 &cmd
->tm
, &cmd
->metadata_sm
);
355 DMERR("tm_create_with_sm failed");
359 __setup_mapping_info(cmd
);
361 r
= dm_array_empty(&cmd
->info
, &cmd
->root
);
365 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
367 r
= dm_bitset_empty(&cmd
->discard_info
, &cmd
->discard_root
);
371 cmd
->discard_block_size
= 0;
372 cmd
->discard_nr_blocks
= 0;
374 r
= __write_initial_superblock(cmd
);
378 cmd
->clean_when_opened
= true;
382 dm_tm_destroy(cmd
->tm
);
383 dm_sm_destroy(cmd
->metadata_sm
);
388 static int __check_incompat_features(struct cache_disk_superblock
*disk_super
,
389 struct dm_cache_metadata
*cmd
)
393 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP
;
395 DMERR("could not access metadata due to unsupported optional features (%lx).",
396 (unsigned long)features
);
401 * Check for read-only metadata to skip the following RDWR checks.
403 if (get_disk_ro(cmd
->bdev
->bd_disk
))
406 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP
;
408 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
409 (unsigned long)features
);
416 static int __open_metadata(struct dm_cache_metadata
*cmd
)
419 struct dm_block
*sblock
;
420 struct cache_disk_superblock
*disk_super
;
421 unsigned long sb_flags
;
423 r
= superblock_read_lock(cmd
, &sblock
);
425 DMERR("couldn't read lock superblock");
429 disk_super
= dm_block_data(sblock
);
431 /* Verify the data block size hasn't changed */
432 if (le32_to_cpu(disk_super
->data_block_size
) != cmd
->data_block_size
) {
433 DMERR("changing the data block size (from %u to %llu) is not supported",
434 le32_to_cpu(disk_super
->data_block_size
),
435 (unsigned long long)cmd
->data_block_size
);
440 r
= __check_incompat_features(disk_super
, cmd
);
444 r
= dm_tm_open_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
445 disk_super
->metadata_space_map_root
,
446 sizeof(disk_super
->metadata_space_map_root
),
447 &cmd
->tm
, &cmd
->metadata_sm
);
449 DMERR("tm_open_with_sm failed");
453 __setup_mapping_info(cmd
);
454 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
455 sb_flags
= le32_to_cpu(disk_super
->flags
);
456 cmd
->clean_when_opened
= test_bit(CLEAN_SHUTDOWN
, &sb_flags
);
457 return dm_bm_unlock(sblock
);
460 dm_bm_unlock(sblock
);
464 static int __open_or_format_metadata(struct dm_cache_metadata
*cmd
,
468 bool unformatted
= false;
470 r
= __superblock_all_zeroes(cmd
->bm
, &unformatted
);
475 return format_device
? __format_metadata(cmd
) : -EPERM
;
477 return __open_metadata(cmd
);
480 static int __create_persistent_data_objects(struct dm_cache_metadata
*cmd
,
481 bool may_format_device
)
484 cmd
->bm
= dm_block_manager_create(cmd
->bdev
, DM_CACHE_METADATA_BLOCK_SIZE
<< SECTOR_SHIFT
,
485 CACHE_METADATA_CACHE_SIZE
,
486 CACHE_MAX_CONCURRENT_LOCKS
);
487 if (IS_ERR(cmd
->bm
)) {
488 DMERR("could not create block manager");
489 return PTR_ERR(cmd
->bm
);
492 r
= __open_or_format_metadata(cmd
, may_format_device
);
494 dm_block_manager_destroy(cmd
->bm
);
499 static void __destroy_persistent_data_objects(struct dm_cache_metadata
*cmd
)
501 dm_sm_destroy(cmd
->metadata_sm
);
502 dm_tm_destroy(cmd
->tm
);
503 dm_block_manager_destroy(cmd
->bm
);
506 typedef unsigned long (*flags_mutator
)(unsigned long);
508 static void update_flags(struct cache_disk_superblock
*disk_super
,
509 flags_mutator mutator
)
511 uint32_t sb_flags
= mutator(le32_to_cpu(disk_super
->flags
));
512 disk_super
->flags
= cpu_to_le32(sb_flags
);
515 static unsigned long set_clean_shutdown(unsigned long flags
)
517 set_bit(CLEAN_SHUTDOWN
, &flags
);
521 static unsigned long clear_clean_shutdown(unsigned long flags
)
523 clear_bit(CLEAN_SHUTDOWN
, &flags
);
527 static void read_superblock_fields(struct dm_cache_metadata
*cmd
,
528 struct cache_disk_superblock
*disk_super
)
530 cmd
->root
= le64_to_cpu(disk_super
->mapping_root
);
531 cmd
->hint_root
= le64_to_cpu(disk_super
->hint_root
);
532 cmd
->discard_root
= le64_to_cpu(disk_super
->discard_root
);
533 cmd
->discard_block_size
= le64_to_cpu(disk_super
->discard_block_size
);
534 cmd
->discard_nr_blocks
= to_dblock(le64_to_cpu(disk_super
->discard_nr_blocks
));
535 cmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
536 cmd
->cache_blocks
= to_cblock(le32_to_cpu(disk_super
->cache_blocks
));
537 strncpy(cmd
->policy_name
, disk_super
->policy_name
, sizeof(cmd
->policy_name
));
538 cmd
->policy_version
[0] = le32_to_cpu(disk_super
->policy_version
[0]);
539 cmd
->policy_version
[1] = le32_to_cpu(disk_super
->policy_version
[1]);
540 cmd
->policy_version
[2] = le32_to_cpu(disk_super
->policy_version
[2]);
541 cmd
->policy_hint_size
= le32_to_cpu(disk_super
->policy_hint_size
);
543 cmd
->stats
.read_hits
= le32_to_cpu(disk_super
->read_hits
);
544 cmd
->stats
.read_misses
= le32_to_cpu(disk_super
->read_misses
);
545 cmd
->stats
.write_hits
= le32_to_cpu(disk_super
->write_hits
);
546 cmd
->stats
.write_misses
= le32_to_cpu(disk_super
->write_misses
);
548 cmd
->changed
= false;
552 * The mutator updates the superblock flags.
554 static int __begin_transaction_flags(struct dm_cache_metadata
*cmd
,
555 flags_mutator mutator
)
558 struct cache_disk_superblock
*disk_super
;
559 struct dm_block
*sblock
;
561 r
= superblock_lock(cmd
, &sblock
);
565 disk_super
= dm_block_data(sblock
);
566 update_flags(disk_super
, mutator
);
567 read_superblock_fields(cmd
, disk_super
);
568 dm_bm_unlock(sblock
);
570 return dm_bm_flush(cmd
->bm
);
573 static int __begin_transaction(struct dm_cache_metadata
*cmd
)
576 struct cache_disk_superblock
*disk_super
;
577 struct dm_block
*sblock
;
580 * We re-read the superblock every time. Shouldn't need to do this
583 r
= superblock_read_lock(cmd
, &sblock
);
587 disk_super
= dm_block_data(sblock
);
588 read_superblock_fields(cmd
, disk_super
);
589 dm_bm_unlock(sblock
);
594 static int __commit_transaction(struct dm_cache_metadata
*cmd
,
595 flags_mutator mutator
)
598 struct cache_disk_superblock
*disk_super
;
599 struct dm_block
*sblock
;
602 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
604 BUILD_BUG_ON(sizeof(struct cache_disk_superblock
) > 512);
606 r
= dm_bitset_flush(&cmd
->discard_info
, cmd
->discard_root
,
611 r
= dm_tm_pre_commit(cmd
->tm
);
615 r
= __save_sm_root(cmd
);
619 r
= superblock_lock(cmd
, &sblock
);
623 disk_super
= dm_block_data(sblock
);
626 update_flags(disk_super
, mutator
);
628 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
629 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
630 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
631 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
632 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
633 disk_super
->cache_blocks
= cpu_to_le32(from_cblock(cmd
->cache_blocks
));
634 strncpy(disk_super
->policy_name
, cmd
->policy_name
, sizeof(disk_super
->policy_name
));
635 disk_super
->policy_version
[0] = cpu_to_le32(cmd
->policy_version
[0]);
636 disk_super
->policy_version
[1] = cpu_to_le32(cmd
->policy_version
[1]);
637 disk_super
->policy_version
[2] = cpu_to_le32(cmd
->policy_version
[2]);
639 disk_super
->read_hits
= cpu_to_le32(cmd
->stats
.read_hits
);
640 disk_super
->read_misses
= cpu_to_le32(cmd
->stats
.read_misses
);
641 disk_super
->write_hits
= cpu_to_le32(cmd
->stats
.write_hits
);
642 disk_super
->write_misses
= cpu_to_le32(cmd
->stats
.write_misses
);
643 __copy_sm_root(cmd
, disk_super
);
645 return dm_tm_commit(cmd
->tm
, sblock
);
648 /*----------------------------------------------------------------*/
651 * The mappings are held in a dm-array that has 64-bit values stored in
652 * little-endian format. The index is the cblock, the high 48bits of the
653 * value are the oblock and the low 16 bit the flags.
655 #define FLAGS_MASK ((1 << 16) - 1)
657 static __le64
pack_value(dm_oblock_t block
, unsigned flags
)
659 uint64_t value
= from_oblock(block
);
661 value
= value
| (flags
& FLAGS_MASK
);
662 return cpu_to_le64(value
);
665 static void unpack_value(__le64 value_le
, dm_oblock_t
*block
, unsigned *flags
)
667 uint64_t value
= le64_to_cpu(value_le
);
668 uint64_t b
= value
>> 16;
669 *block
= to_oblock(b
);
670 *flags
= value
& FLAGS_MASK
;
673 /*----------------------------------------------------------------*/
675 static struct dm_cache_metadata
*metadata_open(struct block_device
*bdev
,
676 sector_t data_block_size
,
677 bool may_format_device
,
678 size_t policy_hint_size
)
681 struct dm_cache_metadata
*cmd
;
683 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
685 DMERR("could not allocate metadata struct");
686 return ERR_PTR(-ENOMEM
);
689 atomic_set(&cmd
->ref_count
, 1);
690 init_rwsem(&cmd
->root_lock
);
692 cmd
->data_block_size
= data_block_size
;
693 cmd
->cache_blocks
= 0;
694 cmd
->policy_hint_size
= policy_hint_size
;
697 r
= __create_persistent_data_objects(cmd
, may_format_device
);
703 r
= __begin_transaction_flags(cmd
, clear_clean_shutdown
);
705 dm_cache_metadata_close(cmd
);
713 * We keep a little list of ref counted metadata objects to prevent two
714 * different target instances creating separate bufio instances. This is
715 * an issue if a table is reloaded before the suspend.
717 static DEFINE_MUTEX(table_lock
);
718 static LIST_HEAD(table
);
720 static struct dm_cache_metadata
*lookup(struct block_device
*bdev
)
722 struct dm_cache_metadata
*cmd
;
724 list_for_each_entry(cmd
, &table
, list
)
725 if (cmd
->bdev
== bdev
) {
726 atomic_inc(&cmd
->ref_count
);
733 static struct dm_cache_metadata
*lookup_or_open(struct block_device
*bdev
,
734 sector_t data_block_size
,
735 bool may_format_device
,
736 size_t policy_hint_size
)
738 struct dm_cache_metadata
*cmd
, *cmd2
;
740 mutex_lock(&table_lock
);
742 mutex_unlock(&table_lock
);
747 cmd
= metadata_open(bdev
, data_block_size
, may_format_device
, policy_hint_size
);
749 mutex_lock(&table_lock
);
752 mutex_unlock(&table_lock
);
753 __destroy_persistent_data_objects(cmd
);
757 list_add(&cmd
->list
, &table
);
758 mutex_unlock(&table_lock
);
764 static bool same_params(struct dm_cache_metadata
*cmd
, sector_t data_block_size
)
766 if (cmd
->data_block_size
!= data_block_size
) {
767 DMERR("data_block_size (%llu) different from that in metadata (%llu)\n",
768 (unsigned long long) data_block_size
,
769 (unsigned long long) cmd
->data_block_size
);
776 struct dm_cache_metadata
*dm_cache_metadata_open(struct block_device
*bdev
,
777 sector_t data_block_size
,
778 bool may_format_device
,
779 size_t policy_hint_size
)
781 struct dm_cache_metadata
*cmd
= lookup_or_open(bdev
, data_block_size
,
782 may_format_device
, policy_hint_size
);
784 if (!IS_ERR(cmd
) && !same_params(cmd
, data_block_size
)) {
785 dm_cache_metadata_close(cmd
);
786 return ERR_PTR(-EINVAL
);
792 void dm_cache_metadata_close(struct dm_cache_metadata
*cmd
)
794 if (atomic_dec_and_test(&cmd
->ref_count
)) {
795 mutex_lock(&table_lock
);
796 list_del(&cmd
->list
);
797 mutex_unlock(&table_lock
);
799 __destroy_persistent_data_objects(cmd
);
805 * Checks that the given cache block is either unmapped or clean.
807 static int block_unmapped_or_clean(struct dm_cache_metadata
*cmd
, dm_cblock_t b
,
815 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(b
), &value
);
817 DMERR("block_unmapped_or_clean failed");
821 unpack_value(value
, &ob
, &flags
);
822 *result
= !((flags
& M_VALID
) && (flags
& M_DIRTY
));
827 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata
*cmd
,
828 dm_cblock_t begin
, dm_cblock_t end
,
834 while (begin
!= end
) {
835 r
= block_unmapped_or_clean(cmd
, begin
, result
);
840 DMERR("cache block %llu is dirty",
841 (unsigned long long) from_cblock(begin
));
845 begin
= to_cblock(from_cblock(begin
) + 1);
851 int dm_cache_resize(struct dm_cache_metadata
*cmd
, dm_cblock_t new_cache_size
)
855 __le64 null_mapping
= pack_value(0, 0);
857 down_write(&cmd
->root_lock
);
858 __dm_bless_for_disk(&null_mapping
);
860 if (from_cblock(new_cache_size
) < from_cblock(cmd
->cache_blocks
)) {
861 r
= blocks_are_unmapped_or_clean(cmd
, new_cache_size
, cmd
->cache_blocks
, &clean
);
863 __dm_unbless_for_disk(&null_mapping
);
868 DMERR("unable to shrink cache due to dirty blocks");
870 __dm_unbless_for_disk(&null_mapping
);
875 r
= dm_array_resize(&cmd
->info
, cmd
->root
, from_cblock(cmd
->cache_blocks
),
876 from_cblock(new_cache_size
),
877 &null_mapping
, &cmd
->root
);
879 cmd
->cache_blocks
= new_cache_size
;
883 up_write(&cmd
->root_lock
);
888 int dm_cache_discard_bitset_resize(struct dm_cache_metadata
*cmd
,
889 sector_t discard_block_size
,
890 dm_dblock_t new_nr_entries
)
894 down_write(&cmd
->root_lock
);
895 r
= dm_bitset_resize(&cmd
->discard_info
,
897 from_dblock(cmd
->discard_nr_blocks
),
898 from_dblock(new_nr_entries
),
899 false, &cmd
->discard_root
);
901 cmd
->discard_block_size
= discard_block_size
;
902 cmd
->discard_nr_blocks
= new_nr_entries
;
906 up_write(&cmd
->root_lock
);
911 static int __set_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
913 return dm_bitset_set_bit(&cmd
->discard_info
, cmd
->discard_root
,
914 from_dblock(b
), &cmd
->discard_root
);
917 static int __clear_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
919 return dm_bitset_clear_bit(&cmd
->discard_info
, cmd
->discard_root
,
920 from_dblock(b
), &cmd
->discard_root
);
923 static int __is_discarded(struct dm_cache_metadata
*cmd
, dm_dblock_t b
,
926 return dm_bitset_test_bit(&cmd
->discard_info
, cmd
->discard_root
,
927 from_dblock(b
), &cmd
->discard_root
,
931 static int __discard(struct dm_cache_metadata
*cmd
,
932 dm_dblock_t dblock
, bool discard
)
936 r
= (discard
? __set_discard
: __clear_discard
)(cmd
, dblock
);
944 int dm_cache_set_discard(struct dm_cache_metadata
*cmd
,
945 dm_dblock_t dblock
, bool discard
)
949 down_write(&cmd
->root_lock
);
950 r
= __discard(cmd
, dblock
, discard
);
951 up_write(&cmd
->root_lock
);
956 static int __load_discards(struct dm_cache_metadata
*cmd
,
957 load_discard_fn fn
, void *context
)
963 for (b
= 0; b
< from_dblock(cmd
->discard_nr_blocks
); b
++) {
964 dm_dblock_t dblock
= to_dblock(b
);
966 if (cmd
->clean_when_opened
) {
967 r
= __is_discarded(cmd
, dblock
, &discard
);
973 r
= fn(context
, cmd
->discard_block_size
, dblock
, discard
);
981 int dm_cache_load_discards(struct dm_cache_metadata
*cmd
,
982 load_discard_fn fn
, void *context
)
986 down_read(&cmd
->root_lock
);
987 r
= __load_discards(cmd
, fn
, context
);
988 up_read(&cmd
->root_lock
);
993 dm_cblock_t
dm_cache_size(struct dm_cache_metadata
*cmd
)
997 down_read(&cmd
->root_lock
);
998 r
= cmd
->cache_blocks
;
999 up_read(&cmd
->root_lock
);
1004 static int __remove(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
1007 __le64 value
= pack_value(0, 0);
1009 __dm_bless_for_disk(&value
);
1010 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1011 &value
, &cmd
->root
);
1015 cmd
->changed
= true;
1019 int dm_cache_remove_mapping(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
1023 down_write(&cmd
->root_lock
);
1024 r
= __remove(cmd
, cblock
);
1025 up_write(&cmd
->root_lock
);
1030 static int __insert(struct dm_cache_metadata
*cmd
,
1031 dm_cblock_t cblock
, dm_oblock_t oblock
)
1034 __le64 value
= pack_value(oblock
, M_VALID
);
1035 __dm_bless_for_disk(&value
);
1037 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1038 &value
, &cmd
->root
);
1042 cmd
->changed
= true;
1046 int dm_cache_insert_mapping(struct dm_cache_metadata
*cmd
,
1047 dm_cblock_t cblock
, dm_oblock_t oblock
)
1051 down_write(&cmd
->root_lock
);
1052 r
= __insert(cmd
, cblock
, oblock
);
1053 up_write(&cmd
->root_lock
);
1062 struct dm_cache_metadata
*cmd
;
1063 bool respect_dirty_flags
;
1067 static bool policy_unchanged(struct dm_cache_metadata
*cmd
,
1068 struct dm_cache_policy
*policy
)
1070 const char *policy_name
= dm_cache_policy_get_name(policy
);
1071 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1072 size_t policy_hint_size
= dm_cache_policy_get_hint_size(policy
);
1075 * Ensure policy names match.
1077 if (strncmp(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
)))
1081 * Ensure policy major versions match.
1083 if (cmd
->policy_version
[0] != policy_version
[0])
1087 * Ensure policy hint sizes match.
1089 if (cmd
->policy_hint_size
!= policy_hint_size
)
1095 static bool hints_array_initialized(struct dm_cache_metadata
*cmd
)
1097 return cmd
->hint_root
&& cmd
->policy_hint_size
;
1100 static bool hints_array_available(struct dm_cache_metadata
*cmd
,
1101 struct dm_cache_policy
*policy
)
1103 return cmd
->clean_when_opened
&& policy_unchanged(cmd
, policy
) &&
1104 hints_array_initialized(cmd
);
1107 static int __load_mapping(void *context
, uint64_t cblock
, void *leaf
)
1112 __le32 hint_value
= 0;
1115 struct thunk
*thunk
= context
;
1116 struct dm_cache_metadata
*cmd
= thunk
->cmd
;
1118 memcpy(&value
, leaf
, sizeof(value
));
1119 unpack_value(value
, &oblock
, &flags
);
1121 if (flags
& M_VALID
) {
1122 if (thunk
->hints_valid
) {
1123 r
= dm_array_get_value(&cmd
->hint_info
, cmd
->hint_root
,
1124 cblock
, &hint_value
);
1125 if (r
&& r
!= -ENODATA
)
1129 dirty
= thunk
->respect_dirty_flags
? (flags
& M_DIRTY
) : true;
1130 r
= thunk
->fn(thunk
->context
, oblock
, to_cblock(cblock
),
1131 dirty
, le32_to_cpu(hint_value
), thunk
->hints_valid
);
1137 static int __load_mappings(struct dm_cache_metadata
*cmd
,
1138 struct dm_cache_policy
*policy
,
1139 load_mapping_fn fn
, void *context
)
1144 thunk
.context
= context
;
1147 thunk
.respect_dirty_flags
= cmd
->clean_when_opened
;
1148 thunk
.hints_valid
= hints_array_available(cmd
, policy
);
1150 return dm_array_walk(&cmd
->info
, cmd
->root
, __load_mapping
, &thunk
);
1153 int dm_cache_load_mappings(struct dm_cache_metadata
*cmd
,
1154 struct dm_cache_policy
*policy
,
1155 load_mapping_fn fn
, void *context
)
1159 down_read(&cmd
->root_lock
);
1160 r
= __load_mappings(cmd
, policy
, fn
, context
);
1161 up_read(&cmd
->root_lock
);
1166 static int __dump_mapping(void *context
, uint64_t cblock
, void *leaf
)
1173 memcpy(&value
, leaf
, sizeof(value
));
1174 unpack_value(value
, &oblock
, &flags
);
1179 static int __dump_mappings(struct dm_cache_metadata
*cmd
)
1181 return dm_array_walk(&cmd
->info
, cmd
->root
, __dump_mapping
, NULL
);
1184 void dm_cache_dump(struct dm_cache_metadata
*cmd
)
1186 down_read(&cmd
->root_lock
);
1187 __dump_mappings(cmd
);
1188 up_read(&cmd
->root_lock
);
1191 int dm_cache_changed_this_transaction(struct dm_cache_metadata
*cmd
)
1195 down_read(&cmd
->root_lock
);
1197 up_read(&cmd
->root_lock
);
1202 static int __dirty(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
, bool dirty
)
1209 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
), &value
);
1213 unpack_value(value
, &oblock
, &flags
);
1215 if (((flags
& M_DIRTY
) && dirty
) || (!(flags
& M_DIRTY
) && !dirty
))
1216 /* nothing to be done */
1219 value
= pack_value(oblock
, (flags
& ~M_DIRTY
) | (dirty
? M_DIRTY
: 0));
1220 __dm_bless_for_disk(&value
);
1222 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1223 &value
, &cmd
->root
);
1227 cmd
->changed
= true;
1232 int dm_cache_set_dirty(struct dm_cache_metadata
*cmd
,
1233 dm_cblock_t cblock
, bool dirty
)
1237 down_write(&cmd
->root_lock
);
1238 r
= __dirty(cmd
, cblock
, dirty
);
1239 up_write(&cmd
->root_lock
);
1244 void dm_cache_metadata_get_stats(struct dm_cache_metadata
*cmd
,
1245 struct dm_cache_statistics
*stats
)
1247 down_read(&cmd
->root_lock
);
1248 *stats
= cmd
->stats
;
1249 up_read(&cmd
->root_lock
);
1252 void dm_cache_metadata_set_stats(struct dm_cache_metadata
*cmd
,
1253 struct dm_cache_statistics
*stats
)
1255 down_write(&cmd
->root_lock
);
1256 cmd
->stats
= *stats
;
1257 up_write(&cmd
->root_lock
);
1260 int dm_cache_commit(struct dm_cache_metadata
*cmd
, bool clean_shutdown
)
1263 flags_mutator mutator
= (clean_shutdown
? set_clean_shutdown
:
1264 clear_clean_shutdown
);
1266 down_write(&cmd
->root_lock
);
1267 r
= __commit_transaction(cmd
, mutator
);
1271 r
= __begin_transaction(cmd
);
1274 up_write(&cmd
->root_lock
);
1278 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata
*cmd
,
1283 down_read(&cmd
->root_lock
);
1284 r
= dm_sm_get_nr_free(cmd
->metadata_sm
, result
);
1285 up_read(&cmd
->root_lock
);
1290 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata
*cmd
,
1295 down_read(&cmd
->root_lock
);
1296 r
= dm_sm_get_nr_blocks(cmd
->metadata_sm
, result
);
1297 up_read(&cmd
->root_lock
);
1302 /*----------------------------------------------------------------*/
1304 static int begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1309 const char *policy_name
= dm_cache_policy_get_name(policy
);
1310 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1312 if (!policy_name
[0] ||
1313 (strlen(policy_name
) > sizeof(cmd
->policy_name
) - 1))
1316 if (!policy_unchanged(cmd
, policy
)) {
1317 strncpy(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
));
1318 memcpy(cmd
->policy_version
, policy_version
, sizeof(cmd
->policy_version
));
1320 hint_size
= dm_cache_policy_get_hint_size(policy
);
1322 return 0; /* short-circuit hints initialization */
1323 cmd
->policy_hint_size
= hint_size
;
1325 if (cmd
->hint_root
) {
1326 r
= dm_array_del(&cmd
->hint_info
, cmd
->hint_root
);
1331 r
= dm_array_empty(&cmd
->hint_info
, &cmd
->hint_root
);
1335 value
= cpu_to_le32(0);
1336 __dm_bless_for_disk(&value
);
1337 r
= dm_array_resize(&cmd
->hint_info
, cmd
->hint_root
, 0,
1338 from_cblock(cmd
->cache_blocks
),
1339 &value
, &cmd
->hint_root
);
1347 static int save_hint(void *context
, dm_cblock_t cblock
, dm_oblock_t oblock
, uint32_t hint
)
1349 struct dm_cache_metadata
*cmd
= context
;
1350 __le32 value
= cpu_to_le32(hint
);
1353 __dm_bless_for_disk(&value
);
1355 r
= dm_array_set_value(&cmd
->hint_info
, cmd
->hint_root
,
1356 from_cblock(cblock
), &value
, &cmd
->hint_root
);
1357 cmd
->changed
= true;
1362 static int write_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1366 r
= begin_hints(cmd
, policy
);
1368 DMERR("begin_hints failed");
1372 return policy_walk_mappings(policy
, save_hint
, cmd
);
1375 int dm_cache_write_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1379 down_write(&cmd
->root_lock
);
1380 r
= write_hints(cmd
, policy
);
1381 up_write(&cmd
->root_lock
);
1386 int dm_cache_metadata_all_clean(struct dm_cache_metadata
*cmd
, bool *result
)
1388 return blocks_are_unmapped_or_clean(cmd
, 0, cmd
->cache_blocks
, result
);