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
23 #define CACHE_VERSION 1
24 #define CACHE_METADATA_CACHE_SIZE 64
27 * 3 for btree insert +
28 * 2 for btree lookup used within space map
30 #define CACHE_MAX_CONCURRENT_LOCKS 5
31 #define SPACE_MAP_ROOT_SIZE 128
33 enum superblock_flag_bits
{
34 /* for spotting crashes that would invalidate the dirty bitset */
39 * Each mapping from cache block -> origin block carries a set of flags.
43 * A valid mapping. Because we're using an array we clear this
44 * flag for an non existant mapping.
49 * The data on the cache is different from that on the origin.
54 struct cache_disk_superblock
{
63 __u8 policy_name
[CACHE_POLICY_NAME_SIZE
];
64 __le32 policy_hint_size
;
66 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
71 __le64 discard_block_size
;
72 __le64 discard_nr_blocks
;
74 __le32 data_block_size
;
75 __le32 metadata_block_size
;
79 __le32 compat_ro_flags
;
80 __le32 incompat_flags
;
87 __le32 policy_version
[CACHE_POLICY_VERSION_SIZE
];
90 struct dm_cache_metadata
{
92 struct list_head list
;
94 struct block_device
*bdev
;
95 struct dm_block_manager
*bm
;
96 struct dm_space_map
*metadata_sm
;
97 struct dm_transaction_manager
*tm
;
99 struct dm_array_info info
;
100 struct dm_array_info hint_info
;
101 struct dm_disk_bitset discard_info
;
103 struct rw_semaphore root_lock
;
105 dm_block_t hint_root
;
106 dm_block_t discard_root
;
108 sector_t discard_block_size
;
109 dm_dblock_t discard_nr_blocks
;
111 sector_t data_block_size
;
112 dm_cblock_t cache_blocks
;
114 bool clean_when_opened
:1;
116 char policy_name
[CACHE_POLICY_NAME_SIZE
];
117 unsigned policy_version
[CACHE_POLICY_VERSION_SIZE
];
118 size_t policy_hint_size
;
119 struct dm_cache_statistics stats
;
122 /*-------------------------------------------------------------------
123 * superblock validator
124 *-----------------------------------------------------------------*/
126 #define SUPERBLOCK_CSUM_XOR 9031977
128 static void sb_prepare_for_write(struct dm_block_validator
*v
,
130 size_t sb_block_size
)
132 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
134 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
135 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
136 sb_block_size
- sizeof(__le32
),
137 SUPERBLOCK_CSUM_XOR
));
140 static int sb_check(struct dm_block_validator
*v
,
142 size_t sb_block_size
)
144 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
147 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
148 DMERR("sb_check failed: blocknr %llu: wanted %llu",
149 le64_to_cpu(disk_super
->blocknr
),
150 (unsigned long long)dm_block_location(b
));
154 if (le64_to_cpu(disk_super
->magic
) != CACHE_SUPERBLOCK_MAGIC
) {
155 DMERR("sb_check failed: magic %llu: wanted %llu",
156 le64_to_cpu(disk_super
->magic
),
157 (unsigned long long)CACHE_SUPERBLOCK_MAGIC
);
161 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
162 sb_block_size
- sizeof(__le32
),
163 SUPERBLOCK_CSUM_XOR
));
164 if (csum_le
!= disk_super
->csum
) {
165 DMERR("sb_check failed: csum %u: wanted %u",
166 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
173 static struct dm_block_validator sb_validator
= {
174 .name
= "superblock",
175 .prepare_for_write
= sb_prepare_for_write
,
179 /*----------------------------------------------------------------*/
181 static int superblock_read_lock(struct dm_cache_metadata
*cmd
,
182 struct dm_block
**sblock
)
184 return dm_bm_read_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
185 &sb_validator
, sblock
);
188 static int superblock_lock_zero(struct dm_cache_metadata
*cmd
,
189 struct dm_block
**sblock
)
191 return dm_bm_write_lock_zero(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
192 &sb_validator
, sblock
);
195 static int superblock_lock(struct dm_cache_metadata
*cmd
,
196 struct dm_block
**sblock
)
198 return dm_bm_write_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
199 &sb_validator
, sblock
);
202 /*----------------------------------------------------------------*/
204 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
209 __le64
*data_le
, zero
= cpu_to_le64(0);
210 unsigned sb_block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
213 * We can't use a validator here - it may be all zeroes.
215 r
= dm_bm_read_lock(bm
, CACHE_SUPERBLOCK_LOCATION
, NULL
, &b
);
219 data_le
= dm_block_data(b
);
221 for (i
= 0; i
< sb_block_size
; i
++) {
222 if (data_le
[i
] != zero
) {
228 return dm_bm_unlock(b
);
231 static void __setup_mapping_info(struct dm_cache_metadata
*cmd
)
233 struct dm_btree_value_type vt
;
236 vt
.size
= sizeof(__le64
);
240 dm_array_info_init(&cmd
->info
, cmd
->tm
, &vt
);
242 if (cmd
->policy_hint_size
) {
243 vt
.size
= sizeof(__le32
);
244 dm_array_info_init(&cmd
->hint_info
, cmd
->tm
, &vt
);
248 static int __write_initial_superblock(struct dm_cache_metadata
*cmd
)
251 struct dm_block
*sblock
;
253 struct cache_disk_superblock
*disk_super
;
254 sector_t bdev_size
= i_size_read(cmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
256 /* FIXME: see if we can lose the max sectors limit */
257 if (bdev_size
> DM_CACHE_METADATA_MAX_SECTORS
)
258 bdev_size
= DM_CACHE_METADATA_MAX_SECTORS
;
260 r
= dm_sm_root_size(cmd
->metadata_sm
, &metadata_len
);
264 r
= dm_tm_pre_commit(cmd
->tm
);
268 r
= superblock_lock_zero(cmd
, &sblock
);
272 disk_super
= dm_block_data(sblock
);
273 disk_super
->flags
= 0;
274 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
275 disk_super
->magic
= cpu_to_le64(CACHE_SUPERBLOCK_MAGIC
);
276 disk_super
->version
= cpu_to_le32(CACHE_VERSION
);
277 memset(disk_super
->policy_name
, 0, sizeof(disk_super
->policy_name
));
278 memset(disk_super
->policy_version
, 0, sizeof(disk_super
->policy_version
));
279 disk_super
->policy_hint_size
= 0;
281 r
= dm_sm_copy_root(cmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
286 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
287 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
288 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
289 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
290 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
291 disk_super
->metadata_block_size
= cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE
>> SECTOR_SHIFT
);
292 disk_super
->data_block_size
= cpu_to_le32(cmd
->data_block_size
);
293 disk_super
->cache_blocks
= cpu_to_le32(0);
295 disk_super
->read_hits
= cpu_to_le32(0);
296 disk_super
->read_misses
= cpu_to_le32(0);
297 disk_super
->write_hits
= cpu_to_le32(0);
298 disk_super
->write_misses
= cpu_to_le32(0);
300 return dm_tm_commit(cmd
->tm
, sblock
);
303 dm_bm_unlock(sblock
);
307 static int __format_metadata(struct dm_cache_metadata
*cmd
)
311 r
= dm_tm_create_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
312 &cmd
->tm
, &cmd
->metadata_sm
);
314 DMERR("tm_create_with_sm failed");
318 __setup_mapping_info(cmd
);
320 r
= dm_array_empty(&cmd
->info
, &cmd
->root
);
324 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
326 r
= dm_bitset_empty(&cmd
->discard_info
, &cmd
->discard_root
);
330 cmd
->discard_block_size
= 0;
331 cmd
->discard_nr_blocks
= 0;
333 r
= __write_initial_superblock(cmd
);
337 cmd
->clean_when_opened
= true;
341 dm_tm_destroy(cmd
->tm
);
342 dm_sm_destroy(cmd
->metadata_sm
);
347 static int __check_incompat_features(struct cache_disk_superblock
*disk_super
,
348 struct dm_cache_metadata
*cmd
)
352 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP
;
354 DMERR("could not access metadata due to unsupported optional features (%lx).",
355 (unsigned long)features
);
360 * Check for read-only metadata to skip the following RDWR checks.
362 if (get_disk_ro(cmd
->bdev
->bd_disk
))
365 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP
;
367 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
368 (unsigned long)features
);
375 static int __open_metadata(struct dm_cache_metadata
*cmd
)
378 struct dm_block
*sblock
;
379 struct cache_disk_superblock
*disk_super
;
380 unsigned long sb_flags
;
382 r
= superblock_read_lock(cmd
, &sblock
);
384 DMERR("couldn't read lock superblock");
388 disk_super
= dm_block_data(sblock
);
390 /* Verify the data block size hasn't changed */
391 if (le32_to_cpu(disk_super
->data_block_size
) != cmd
->data_block_size
) {
392 DMERR("changing the data block size (from %u to %llu) is not supported",
393 le32_to_cpu(disk_super
->data_block_size
),
394 (unsigned long long)cmd
->data_block_size
);
399 r
= __check_incompat_features(disk_super
, cmd
);
403 r
= dm_tm_open_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
404 disk_super
->metadata_space_map_root
,
405 sizeof(disk_super
->metadata_space_map_root
),
406 &cmd
->tm
, &cmd
->metadata_sm
);
408 DMERR("tm_open_with_sm failed");
412 __setup_mapping_info(cmd
);
413 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
414 sb_flags
= le32_to_cpu(disk_super
->flags
);
415 cmd
->clean_when_opened
= test_bit(CLEAN_SHUTDOWN
, &sb_flags
);
416 return dm_bm_unlock(sblock
);
419 dm_bm_unlock(sblock
);
423 static int __open_or_format_metadata(struct dm_cache_metadata
*cmd
,
428 r
= __superblock_all_zeroes(cmd
->bm
, &unformatted
);
433 return format_device
? __format_metadata(cmd
) : -EPERM
;
435 return __open_metadata(cmd
);
438 static int __create_persistent_data_objects(struct dm_cache_metadata
*cmd
,
439 bool may_format_device
)
442 cmd
->bm
= dm_block_manager_create(cmd
->bdev
, DM_CACHE_METADATA_BLOCK_SIZE
,
443 CACHE_METADATA_CACHE_SIZE
,
444 CACHE_MAX_CONCURRENT_LOCKS
);
445 if (IS_ERR(cmd
->bm
)) {
446 DMERR("could not create block manager");
447 return PTR_ERR(cmd
->bm
);
450 r
= __open_or_format_metadata(cmd
, may_format_device
);
452 dm_block_manager_destroy(cmd
->bm
);
457 static void __destroy_persistent_data_objects(struct dm_cache_metadata
*cmd
)
459 dm_sm_destroy(cmd
->metadata_sm
);
460 dm_tm_destroy(cmd
->tm
);
461 dm_block_manager_destroy(cmd
->bm
);
464 typedef unsigned long (*flags_mutator
)(unsigned long);
466 static void update_flags(struct cache_disk_superblock
*disk_super
,
467 flags_mutator mutator
)
469 uint32_t sb_flags
= mutator(le32_to_cpu(disk_super
->flags
));
470 disk_super
->flags
= cpu_to_le32(sb_flags
);
473 static unsigned long set_clean_shutdown(unsigned long flags
)
475 set_bit(CLEAN_SHUTDOWN
, &flags
);
479 static unsigned long clear_clean_shutdown(unsigned long flags
)
481 clear_bit(CLEAN_SHUTDOWN
, &flags
);
485 static void read_superblock_fields(struct dm_cache_metadata
*cmd
,
486 struct cache_disk_superblock
*disk_super
)
488 cmd
->root
= le64_to_cpu(disk_super
->mapping_root
);
489 cmd
->hint_root
= le64_to_cpu(disk_super
->hint_root
);
490 cmd
->discard_root
= le64_to_cpu(disk_super
->discard_root
);
491 cmd
->discard_block_size
= le64_to_cpu(disk_super
->discard_block_size
);
492 cmd
->discard_nr_blocks
= to_dblock(le64_to_cpu(disk_super
->discard_nr_blocks
));
493 cmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
494 cmd
->cache_blocks
= to_cblock(le32_to_cpu(disk_super
->cache_blocks
));
495 strncpy(cmd
->policy_name
, disk_super
->policy_name
, sizeof(cmd
->policy_name
));
496 cmd
->policy_version
[0] = le32_to_cpu(disk_super
->policy_version
[0]);
497 cmd
->policy_version
[1] = le32_to_cpu(disk_super
->policy_version
[1]);
498 cmd
->policy_version
[2] = le32_to_cpu(disk_super
->policy_version
[2]);
499 cmd
->policy_hint_size
= le32_to_cpu(disk_super
->policy_hint_size
);
501 cmd
->stats
.read_hits
= le32_to_cpu(disk_super
->read_hits
);
502 cmd
->stats
.read_misses
= le32_to_cpu(disk_super
->read_misses
);
503 cmd
->stats
.write_hits
= le32_to_cpu(disk_super
->write_hits
);
504 cmd
->stats
.write_misses
= le32_to_cpu(disk_super
->write_misses
);
506 cmd
->changed
= false;
510 * The mutator updates the superblock flags.
512 static int __begin_transaction_flags(struct dm_cache_metadata
*cmd
,
513 flags_mutator mutator
)
516 struct cache_disk_superblock
*disk_super
;
517 struct dm_block
*sblock
;
519 r
= superblock_lock(cmd
, &sblock
);
523 disk_super
= dm_block_data(sblock
);
524 update_flags(disk_super
, mutator
);
525 read_superblock_fields(cmd
, disk_super
);
526 dm_bm_unlock(sblock
);
528 return dm_bm_flush(cmd
->bm
);
531 static int __begin_transaction(struct dm_cache_metadata
*cmd
)
534 struct cache_disk_superblock
*disk_super
;
535 struct dm_block
*sblock
;
538 * We re-read the superblock every time. Shouldn't need to do this
541 r
= superblock_read_lock(cmd
, &sblock
);
545 disk_super
= dm_block_data(sblock
);
546 read_superblock_fields(cmd
, disk_super
);
547 dm_bm_unlock(sblock
);
552 static int __commit_transaction(struct dm_cache_metadata
*cmd
,
553 flags_mutator mutator
)
557 struct cache_disk_superblock
*disk_super
;
558 struct dm_block
*sblock
;
561 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
563 BUILD_BUG_ON(sizeof(struct cache_disk_superblock
) > 512);
565 r
= dm_bitset_flush(&cmd
->discard_info
, cmd
->discard_root
,
570 r
= dm_tm_pre_commit(cmd
->tm
);
574 r
= dm_sm_root_size(cmd
->metadata_sm
, &metadata_len
);
578 r
= superblock_lock(cmd
, &sblock
);
582 disk_super
= dm_block_data(sblock
);
585 update_flags(disk_super
, mutator
);
587 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
588 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
589 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
590 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
591 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
592 disk_super
->cache_blocks
= cpu_to_le32(from_cblock(cmd
->cache_blocks
));
593 strncpy(disk_super
->policy_name
, cmd
->policy_name
, sizeof(disk_super
->policy_name
));
594 disk_super
->policy_version
[0] = cpu_to_le32(cmd
->policy_version
[0]);
595 disk_super
->policy_version
[1] = cpu_to_le32(cmd
->policy_version
[1]);
596 disk_super
->policy_version
[2] = cpu_to_le32(cmd
->policy_version
[2]);
598 disk_super
->read_hits
= cpu_to_le32(cmd
->stats
.read_hits
);
599 disk_super
->read_misses
= cpu_to_le32(cmd
->stats
.read_misses
);
600 disk_super
->write_hits
= cpu_to_le32(cmd
->stats
.write_hits
);
601 disk_super
->write_misses
= cpu_to_le32(cmd
->stats
.write_misses
);
603 r
= dm_sm_copy_root(cmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
606 dm_bm_unlock(sblock
);
610 return dm_tm_commit(cmd
->tm
, sblock
);
613 /*----------------------------------------------------------------*/
616 * The mappings are held in a dm-array that has 64-bit values stored in
617 * little-endian format. The index is the cblock, the high 48bits of the
618 * value are the oblock and the low 16 bit the flags.
620 #define FLAGS_MASK ((1 << 16) - 1)
622 static __le64
pack_value(dm_oblock_t block
, unsigned flags
)
624 uint64_t value
= from_oblock(block
);
626 value
= value
| (flags
& FLAGS_MASK
);
627 return cpu_to_le64(value
);
630 static void unpack_value(__le64 value_le
, dm_oblock_t
*block
, unsigned *flags
)
632 uint64_t value
= le64_to_cpu(value_le
);
633 uint64_t b
= value
>> 16;
634 *block
= to_oblock(b
);
635 *flags
= value
& FLAGS_MASK
;
638 /*----------------------------------------------------------------*/
640 static struct dm_cache_metadata
*metadata_open(struct block_device
*bdev
,
641 sector_t data_block_size
,
642 bool may_format_device
,
643 size_t policy_hint_size
)
646 struct dm_cache_metadata
*cmd
;
648 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
650 DMERR("could not allocate metadata struct");
651 return ERR_PTR(-ENOMEM
);
654 atomic_set(&cmd
->ref_count
, 1);
655 init_rwsem(&cmd
->root_lock
);
657 cmd
->data_block_size
= data_block_size
;
658 cmd
->cache_blocks
= 0;
659 cmd
->policy_hint_size
= policy_hint_size
;
662 r
= __create_persistent_data_objects(cmd
, may_format_device
);
668 r
= __begin_transaction_flags(cmd
, clear_clean_shutdown
);
670 dm_cache_metadata_close(cmd
);
678 * We keep a little list of ref counted metadata objects to prevent two
679 * different target instances creating separate bufio instances. This is
680 * an issue if a table is reloaded before the suspend.
682 static DEFINE_MUTEX(table_lock
);
683 static LIST_HEAD(table
);
685 static struct dm_cache_metadata
*lookup(struct block_device
*bdev
)
687 struct dm_cache_metadata
*cmd
;
689 list_for_each_entry(cmd
, &table
, list
)
690 if (cmd
->bdev
== bdev
) {
691 atomic_inc(&cmd
->ref_count
);
698 static struct dm_cache_metadata
*lookup_or_open(struct block_device
*bdev
,
699 sector_t data_block_size
,
700 bool may_format_device
,
701 size_t policy_hint_size
)
703 struct dm_cache_metadata
*cmd
, *cmd2
;
705 mutex_lock(&table_lock
);
707 mutex_unlock(&table_lock
);
712 cmd
= metadata_open(bdev
, data_block_size
, may_format_device
, policy_hint_size
);
714 mutex_lock(&table_lock
);
717 mutex_unlock(&table_lock
);
718 __destroy_persistent_data_objects(cmd
);
722 list_add(&cmd
->list
, &table
);
723 mutex_unlock(&table_lock
);
729 static bool same_params(struct dm_cache_metadata
*cmd
, sector_t data_block_size
)
731 if (cmd
->data_block_size
!= data_block_size
) {
732 DMERR("data_block_size (%llu) different from that in metadata (%llu)\n",
733 (unsigned long long) data_block_size
,
734 (unsigned long long) cmd
->data_block_size
);
741 struct dm_cache_metadata
*dm_cache_metadata_open(struct block_device
*bdev
,
742 sector_t data_block_size
,
743 bool may_format_device
,
744 size_t policy_hint_size
)
746 struct dm_cache_metadata
*cmd
= lookup_or_open(bdev
, data_block_size
,
747 may_format_device
, policy_hint_size
);
749 if (!IS_ERR(cmd
) && !same_params(cmd
, data_block_size
)) {
750 dm_cache_metadata_close(cmd
);
751 return ERR_PTR(-EINVAL
);
757 void dm_cache_metadata_close(struct dm_cache_metadata
*cmd
)
759 if (atomic_dec_and_test(&cmd
->ref_count
)) {
760 mutex_lock(&table_lock
);
761 list_del(&cmd
->list
);
762 mutex_unlock(&table_lock
);
764 __destroy_persistent_data_objects(cmd
);
769 int dm_cache_resize(struct dm_cache_metadata
*cmd
, dm_cblock_t new_cache_size
)
772 __le64 null_mapping
= pack_value(0, 0);
774 down_write(&cmd
->root_lock
);
775 __dm_bless_for_disk(&null_mapping
);
776 r
= dm_array_resize(&cmd
->info
, cmd
->root
, from_cblock(cmd
->cache_blocks
),
777 from_cblock(new_cache_size
),
778 &null_mapping
, &cmd
->root
);
780 cmd
->cache_blocks
= new_cache_size
;
782 up_write(&cmd
->root_lock
);
787 int dm_cache_discard_bitset_resize(struct dm_cache_metadata
*cmd
,
788 sector_t discard_block_size
,
789 dm_dblock_t new_nr_entries
)
793 down_write(&cmd
->root_lock
);
794 r
= dm_bitset_resize(&cmd
->discard_info
,
796 from_dblock(cmd
->discard_nr_blocks
),
797 from_dblock(new_nr_entries
),
798 false, &cmd
->discard_root
);
800 cmd
->discard_block_size
= discard_block_size
;
801 cmd
->discard_nr_blocks
= new_nr_entries
;
805 up_write(&cmd
->root_lock
);
810 static int __set_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
812 return dm_bitset_set_bit(&cmd
->discard_info
, cmd
->discard_root
,
813 from_dblock(b
), &cmd
->discard_root
);
816 static int __clear_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
818 return dm_bitset_clear_bit(&cmd
->discard_info
, cmd
->discard_root
,
819 from_dblock(b
), &cmd
->discard_root
);
822 static int __is_discarded(struct dm_cache_metadata
*cmd
, dm_dblock_t b
,
825 return dm_bitset_test_bit(&cmd
->discard_info
, cmd
->discard_root
,
826 from_dblock(b
), &cmd
->discard_root
,
830 static int __discard(struct dm_cache_metadata
*cmd
,
831 dm_dblock_t dblock
, bool discard
)
835 r
= (discard
? __set_discard
: __clear_discard
)(cmd
, dblock
);
843 int dm_cache_set_discard(struct dm_cache_metadata
*cmd
,
844 dm_dblock_t dblock
, bool discard
)
848 down_write(&cmd
->root_lock
);
849 r
= __discard(cmd
, dblock
, discard
);
850 up_write(&cmd
->root_lock
);
855 static int __load_discards(struct dm_cache_metadata
*cmd
,
856 load_discard_fn fn
, void *context
)
862 for (b
= 0; b
< from_dblock(cmd
->discard_nr_blocks
); b
++) {
863 dm_dblock_t dblock
= to_dblock(b
);
865 if (cmd
->clean_when_opened
) {
866 r
= __is_discarded(cmd
, dblock
, &discard
);
872 r
= fn(context
, cmd
->discard_block_size
, dblock
, discard
);
880 int dm_cache_load_discards(struct dm_cache_metadata
*cmd
,
881 load_discard_fn fn
, void *context
)
885 down_read(&cmd
->root_lock
);
886 r
= __load_discards(cmd
, fn
, context
);
887 up_read(&cmd
->root_lock
);
892 dm_cblock_t
dm_cache_size(struct dm_cache_metadata
*cmd
)
896 down_read(&cmd
->root_lock
);
897 r
= cmd
->cache_blocks
;
898 up_read(&cmd
->root_lock
);
903 static int __remove(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
906 __le64 value
= pack_value(0, 0);
908 __dm_bless_for_disk(&value
);
909 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
918 int dm_cache_remove_mapping(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
922 down_write(&cmd
->root_lock
);
923 r
= __remove(cmd
, cblock
);
924 up_write(&cmd
->root_lock
);
929 static int __insert(struct dm_cache_metadata
*cmd
,
930 dm_cblock_t cblock
, dm_oblock_t oblock
)
933 __le64 value
= pack_value(oblock
, M_VALID
);
934 __dm_bless_for_disk(&value
);
936 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
945 int dm_cache_insert_mapping(struct dm_cache_metadata
*cmd
,
946 dm_cblock_t cblock
, dm_oblock_t oblock
)
950 down_write(&cmd
->root_lock
);
951 r
= __insert(cmd
, cblock
, oblock
);
952 up_write(&cmd
->root_lock
);
961 struct dm_cache_metadata
*cmd
;
962 bool respect_dirty_flags
;
966 static bool policy_unchanged(struct dm_cache_metadata
*cmd
,
967 struct dm_cache_policy
*policy
)
969 const char *policy_name
= dm_cache_policy_get_name(policy
);
970 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
971 size_t policy_hint_size
= dm_cache_policy_get_hint_size(policy
);
974 * Ensure policy names match.
976 if (strncmp(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
)))
980 * Ensure policy major versions match.
982 if (cmd
->policy_version
[0] != policy_version
[0])
986 * Ensure policy hint sizes match.
988 if (cmd
->policy_hint_size
!= policy_hint_size
)
994 static bool hints_array_initialized(struct dm_cache_metadata
*cmd
)
996 return cmd
->hint_root
&& cmd
->policy_hint_size
;
999 static bool hints_array_available(struct dm_cache_metadata
*cmd
,
1000 struct dm_cache_policy
*policy
)
1002 return cmd
->clean_when_opened
&& policy_unchanged(cmd
, policy
) &&
1003 hints_array_initialized(cmd
);
1006 static int __load_mapping(void *context
, uint64_t cblock
, void *leaf
)
1011 __le32 hint_value
= 0;
1014 struct thunk
*thunk
= context
;
1015 struct dm_cache_metadata
*cmd
= thunk
->cmd
;
1017 memcpy(&value
, leaf
, sizeof(value
));
1018 unpack_value(value
, &oblock
, &flags
);
1020 if (flags
& M_VALID
) {
1021 if (thunk
->hints_valid
) {
1022 r
= dm_array_get_value(&cmd
->hint_info
, cmd
->hint_root
,
1023 cblock
, &hint_value
);
1024 if (r
&& r
!= -ENODATA
)
1028 dirty
= thunk
->respect_dirty_flags
? (flags
& M_DIRTY
) : true;
1029 r
= thunk
->fn(thunk
->context
, oblock
, to_cblock(cblock
),
1030 dirty
, le32_to_cpu(hint_value
), thunk
->hints_valid
);
1036 static int __load_mappings(struct dm_cache_metadata
*cmd
,
1037 struct dm_cache_policy
*policy
,
1038 load_mapping_fn fn
, void *context
)
1043 thunk
.context
= context
;
1046 thunk
.respect_dirty_flags
= cmd
->clean_when_opened
;
1047 thunk
.hints_valid
= hints_array_available(cmd
, policy
);
1049 return dm_array_walk(&cmd
->info
, cmd
->root
, __load_mapping
, &thunk
);
1052 int dm_cache_load_mappings(struct dm_cache_metadata
*cmd
,
1053 struct dm_cache_policy
*policy
,
1054 load_mapping_fn fn
, void *context
)
1058 down_read(&cmd
->root_lock
);
1059 r
= __load_mappings(cmd
, policy
, fn
, context
);
1060 up_read(&cmd
->root_lock
);
1065 static int __dump_mapping(void *context
, uint64_t cblock
, void *leaf
)
1072 memcpy(&value
, leaf
, sizeof(value
));
1073 unpack_value(value
, &oblock
, &flags
);
1078 static int __dump_mappings(struct dm_cache_metadata
*cmd
)
1080 return dm_array_walk(&cmd
->info
, cmd
->root
, __dump_mapping
, NULL
);
1083 void dm_cache_dump(struct dm_cache_metadata
*cmd
)
1085 down_read(&cmd
->root_lock
);
1086 __dump_mappings(cmd
);
1087 up_read(&cmd
->root_lock
);
1090 int dm_cache_changed_this_transaction(struct dm_cache_metadata
*cmd
)
1094 down_read(&cmd
->root_lock
);
1096 up_read(&cmd
->root_lock
);
1101 static int __dirty(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
, bool dirty
)
1108 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
), &value
);
1112 unpack_value(value
, &oblock
, &flags
);
1114 if (((flags
& M_DIRTY
) && dirty
) || (!(flags
& M_DIRTY
) && !dirty
))
1115 /* nothing to be done */
1118 value
= pack_value(oblock
, (flags
& ~M_DIRTY
) | (dirty
? M_DIRTY
: 0));
1119 __dm_bless_for_disk(&value
);
1121 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1122 &value
, &cmd
->root
);
1126 cmd
->changed
= true;
1131 int dm_cache_set_dirty(struct dm_cache_metadata
*cmd
,
1132 dm_cblock_t cblock
, bool dirty
)
1136 down_write(&cmd
->root_lock
);
1137 r
= __dirty(cmd
, cblock
, dirty
);
1138 up_write(&cmd
->root_lock
);
1143 void dm_cache_metadata_get_stats(struct dm_cache_metadata
*cmd
,
1144 struct dm_cache_statistics
*stats
)
1146 down_read(&cmd
->root_lock
);
1147 *stats
= cmd
->stats
;
1148 up_read(&cmd
->root_lock
);
1151 void dm_cache_metadata_set_stats(struct dm_cache_metadata
*cmd
,
1152 struct dm_cache_statistics
*stats
)
1154 down_write(&cmd
->root_lock
);
1155 cmd
->stats
= *stats
;
1156 up_write(&cmd
->root_lock
);
1159 int dm_cache_commit(struct dm_cache_metadata
*cmd
, bool clean_shutdown
)
1162 flags_mutator mutator
= (clean_shutdown
? set_clean_shutdown
:
1163 clear_clean_shutdown
);
1165 down_write(&cmd
->root_lock
);
1166 r
= __commit_transaction(cmd
, mutator
);
1170 r
= __begin_transaction(cmd
);
1173 up_write(&cmd
->root_lock
);
1177 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata
*cmd
,
1182 down_read(&cmd
->root_lock
);
1183 r
= dm_sm_get_nr_free(cmd
->metadata_sm
, result
);
1184 up_read(&cmd
->root_lock
);
1189 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata
*cmd
,
1194 down_read(&cmd
->root_lock
);
1195 r
= dm_sm_get_nr_blocks(cmd
->metadata_sm
, result
);
1196 up_read(&cmd
->root_lock
);
1201 /*----------------------------------------------------------------*/
1203 static int begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1208 const char *policy_name
= dm_cache_policy_get_name(policy
);
1209 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1211 if (!policy_name
[0] ||
1212 (strlen(policy_name
) > sizeof(cmd
->policy_name
) - 1))
1215 if (!policy_unchanged(cmd
, policy
)) {
1216 strncpy(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
));
1217 memcpy(cmd
->policy_version
, policy_version
, sizeof(cmd
->policy_version
));
1219 hint_size
= dm_cache_policy_get_hint_size(policy
);
1221 return 0; /* short-circuit hints initialization */
1222 cmd
->policy_hint_size
= hint_size
;
1224 if (cmd
->hint_root
) {
1225 r
= dm_array_del(&cmd
->hint_info
, cmd
->hint_root
);
1230 r
= dm_array_empty(&cmd
->hint_info
, &cmd
->hint_root
);
1234 value
= cpu_to_le32(0);
1235 __dm_bless_for_disk(&value
);
1236 r
= dm_array_resize(&cmd
->hint_info
, cmd
->hint_root
, 0,
1237 from_cblock(cmd
->cache_blocks
),
1238 &value
, &cmd
->hint_root
);
1246 int dm_cache_begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1250 down_write(&cmd
->root_lock
);
1251 r
= begin_hints(cmd
, policy
);
1252 up_write(&cmd
->root_lock
);
1257 static int save_hint(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
,
1261 __le32 value
= cpu_to_le32(hint
);
1262 __dm_bless_for_disk(&value
);
1264 r
= dm_array_set_value(&cmd
->hint_info
, cmd
->hint_root
,
1265 from_cblock(cblock
), &value
, &cmd
->hint_root
);
1266 cmd
->changed
= true;
1271 int dm_cache_save_hint(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
,
1276 if (!hints_array_initialized(cmd
))
1279 down_write(&cmd
->root_lock
);
1280 r
= save_hint(cmd
, cblock
, hint
);
1281 up_write(&cmd
->root_lock
);