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
{
91 struct block_device
*bdev
;
92 struct dm_block_manager
*bm
;
93 struct dm_space_map
*metadata_sm
;
94 struct dm_transaction_manager
*tm
;
96 struct dm_array_info info
;
97 struct dm_array_info hint_info
;
98 struct dm_disk_bitset discard_info
;
100 struct rw_semaphore root_lock
;
102 dm_block_t hint_root
;
103 dm_block_t discard_root
;
105 sector_t discard_block_size
;
106 dm_dblock_t discard_nr_blocks
;
108 sector_t data_block_size
;
109 dm_cblock_t cache_blocks
;
111 bool clean_when_opened
:1;
113 char policy_name
[CACHE_POLICY_NAME_SIZE
];
114 unsigned policy_version
[CACHE_POLICY_VERSION_SIZE
];
115 size_t policy_hint_size
;
116 struct dm_cache_statistics stats
;
119 * Reading the space map root can fail, so we read it into this
120 * buffer before the superblock is locked and updated.
122 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
125 /*-------------------------------------------------------------------
126 * superblock validator
127 *-----------------------------------------------------------------*/
129 #define SUPERBLOCK_CSUM_XOR 9031977
131 static void sb_prepare_for_write(struct dm_block_validator
*v
,
133 size_t sb_block_size
)
135 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
137 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
138 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
139 sb_block_size
- sizeof(__le32
),
140 SUPERBLOCK_CSUM_XOR
));
143 static int sb_check(struct dm_block_validator
*v
,
145 size_t sb_block_size
)
147 struct cache_disk_superblock
*disk_super
= dm_block_data(b
);
150 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
151 DMERR("sb_check failed: blocknr %llu: wanted %llu",
152 le64_to_cpu(disk_super
->blocknr
),
153 (unsigned long long)dm_block_location(b
));
157 if (le64_to_cpu(disk_super
->magic
) != CACHE_SUPERBLOCK_MAGIC
) {
158 DMERR("sb_check failed: magic %llu: wanted %llu",
159 le64_to_cpu(disk_super
->magic
),
160 (unsigned long long)CACHE_SUPERBLOCK_MAGIC
);
164 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
165 sb_block_size
- sizeof(__le32
),
166 SUPERBLOCK_CSUM_XOR
));
167 if (csum_le
!= disk_super
->csum
) {
168 DMERR("sb_check failed: csum %u: wanted %u",
169 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
176 static struct dm_block_validator sb_validator
= {
177 .name
= "superblock",
178 .prepare_for_write
= sb_prepare_for_write
,
182 /*----------------------------------------------------------------*/
184 static int superblock_read_lock(struct dm_cache_metadata
*cmd
,
185 struct dm_block
**sblock
)
187 return dm_bm_read_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
188 &sb_validator
, sblock
);
191 static int superblock_lock_zero(struct dm_cache_metadata
*cmd
,
192 struct dm_block
**sblock
)
194 return dm_bm_write_lock_zero(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
195 &sb_validator
, sblock
);
198 static int superblock_lock(struct dm_cache_metadata
*cmd
,
199 struct dm_block
**sblock
)
201 return dm_bm_write_lock(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
202 &sb_validator
, sblock
);
205 /*----------------------------------------------------------------*/
207 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
212 __le64
*data_le
, zero
= cpu_to_le64(0);
213 unsigned sb_block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
216 * We can't use a validator here - it may be all zeroes.
218 r
= dm_bm_read_lock(bm
, CACHE_SUPERBLOCK_LOCATION
, NULL
, &b
);
222 data_le
= dm_block_data(b
);
224 for (i
= 0; i
< sb_block_size
; i
++) {
225 if (data_le
[i
] != zero
) {
231 return dm_bm_unlock(b
);
234 static void __setup_mapping_info(struct dm_cache_metadata
*cmd
)
236 struct dm_btree_value_type vt
;
239 vt
.size
= sizeof(__le64
);
243 dm_array_info_init(&cmd
->info
, cmd
->tm
, &vt
);
245 if (cmd
->policy_hint_size
) {
246 vt
.size
= sizeof(__le32
);
247 dm_array_info_init(&cmd
->hint_info
, cmd
->tm
, &vt
);
251 static int __save_sm_root(struct dm_cache_metadata
*cmd
)
256 r
= dm_sm_root_size(cmd
->metadata_sm
, &metadata_len
);
260 return dm_sm_copy_root(cmd
->metadata_sm
, &cmd
->metadata_space_map_root
,
264 static void __copy_sm_root(struct dm_cache_metadata
*cmd
,
265 struct cache_disk_superblock
*disk_super
)
267 memcpy(&disk_super
->metadata_space_map_root
,
268 &cmd
->metadata_space_map_root
,
269 sizeof(cmd
->metadata_space_map_root
));
272 static int __write_initial_superblock(struct dm_cache_metadata
*cmd
)
275 struct dm_block
*sblock
;
276 struct cache_disk_superblock
*disk_super
;
277 sector_t bdev_size
= i_size_read(cmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
279 /* FIXME: see if we can lose the max sectors limit */
280 if (bdev_size
> DM_CACHE_METADATA_MAX_SECTORS
)
281 bdev_size
= DM_CACHE_METADATA_MAX_SECTORS
;
283 r
= dm_tm_pre_commit(cmd
->tm
);
288 * dm_sm_copy_root() can fail. So we need to do it before we start
289 * updating the superblock.
291 r
= __save_sm_root(cmd
);
295 r
= superblock_lock_zero(cmd
, &sblock
);
299 disk_super
= dm_block_data(sblock
);
300 disk_super
->flags
= 0;
301 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
302 disk_super
->magic
= cpu_to_le64(CACHE_SUPERBLOCK_MAGIC
);
303 disk_super
->version
= cpu_to_le32(CACHE_VERSION
);
304 memset(disk_super
->policy_name
, 0, sizeof(disk_super
->policy_name
));
305 memset(disk_super
->policy_version
, 0, sizeof(disk_super
->policy_version
));
306 disk_super
->policy_hint_size
= 0;
308 __copy_sm_root(cmd
, disk_super
);
310 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
311 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
312 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
313 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
314 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
315 disk_super
->metadata_block_size
= cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE
>> SECTOR_SHIFT
);
316 disk_super
->data_block_size
= cpu_to_le32(cmd
->data_block_size
);
317 disk_super
->cache_blocks
= cpu_to_le32(0);
319 disk_super
->read_hits
= cpu_to_le32(0);
320 disk_super
->read_misses
= cpu_to_le32(0);
321 disk_super
->write_hits
= cpu_to_le32(0);
322 disk_super
->write_misses
= cpu_to_le32(0);
324 return dm_tm_commit(cmd
->tm
, sblock
);
327 static int __format_metadata(struct dm_cache_metadata
*cmd
)
331 r
= dm_tm_create_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
332 &cmd
->tm
, &cmd
->metadata_sm
);
334 DMERR("tm_create_with_sm failed");
338 __setup_mapping_info(cmd
);
340 r
= dm_array_empty(&cmd
->info
, &cmd
->root
);
344 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
346 r
= dm_bitset_empty(&cmd
->discard_info
, &cmd
->discard_root
);
350 cmd
->discard_block_size
= 0;
351 cmd
->discard_nr_blocks
= 0;
353 r
= __write_initial_superblock(cmd
);
357 cmd
->clean_when_opened
= true;
361 dm_tm_destroy(cmd
->tm
);
362 dm_sm_destroy(cmd
->metadata_sm
);
367 static int __check_incompat_features(struct cache_disk_superblock
*disk_super
,
368 struct dm_cache_metadata
*cmd
)
372 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP
;
374 DMERR("could not access metadata due to unsupported optional features (%lx).",
375 (unsigned long)features
);
380 * Check for read-only metadata to skip the following RDWR checks.
382 if (get_disk_ro(cmd
->bdev
->bd_disk
))
385 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP
;
387 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
388 (unsigned long)features
);
395 static int __open_metadata(struct dm_cache_metadata
*cmd
)
398 struct dm_block
*sblock
;
399 struct cache_disk_superblock
*disk_super
;
400 unsigned long sb_flags
;
402 r
= superblock_read_lock(cmd
, &sblock
);
404 DMERR("couldn't read lock superblock");
408 disk_super
= dm_block_data(sblock
);
410 /* Verify the data block size hasn't changed */
411 if (le32_to_cpu(disk_super
->data_block_size
) != cmd
->data_block_size
) {
412 DMERR("changing the data block size (from %u to %llu) is not supported",
413 le32_to_cpu(disk_super
->data_block_size
),
414 (unsigned long long)cmd
->data_block_size
);
419 r
= __check_incompat_features(disk_super
, cmd
);
423 r
= dm_tm_open_with_sm(cmd
->bm
, CACHE_SUPERBLOCK_LOCATION
,
424 disk_super
->metadata_space_map_root
,
425 sizeof(disk_super
->metadata_space_map_root
),
426 &cmd
->tm
, &cmd
->metadata_sm
);
428 DMERR("tm_open_with_sm failed");
432 __setup_mapping_info(cmd
);
433 dm_disk_bitset_init(cmd
->tm
, &cmd
->discard_info
);
434 sb_flags
= le32_to_cpu(disk_super
->flags
);
435 cmd
->clean_when_opened
= test_bit(CLEAN_SHUTDOWN
, &sb_flags
);
436 return dm_bm_unlock(sblock
);
439 dm_bm_unlock(sblock
);
443 static int __open_or_format_metadata(struct dm_cache_metadata
*cmd
,
448 r
= __superblock_all_zeroes(cmd
->bm
, &unformatted
);
453 return format_device
? __format_metadata(cmd
) : -EPERM
;
455 return __open_metadata(cmd
);
458 static int __create_persistent_data_objects(struct dm_cache_metadata
*cmd
,
459 bool may_format_device
)
462 cmd
->bm
= dm_block_manager_create(cmd
->bdev
, DM_CACHE_METADATA_BLOCK_SIZE
,
463 CACHE_METADATA_CACHE_SIZE
,
464 CACHE_MAX_CONCURRENT_LOCKS
);
465 if (IS_ERR(cmd
->bm
)) {
466 DMERR("could not create block manager");
467 return PTR_ERR(cmd
->bm
);
470 r
= __open_or_format_metadata(cmd
, may_format_device
);
472 dm_block_manager_destroy(cmd
->bm
);
477 static void __destroy_persistent_data_objects(struct dm_cache_metadata
*cmd
)
479 dm_sm_destroy(cmd
->metadata_sm
);
480 dm_tm_destroy(cmd
->tm
);
481 dm_block_manager_destroy(cmd
->bm
);
484 typedef unsigned long (*flags_mutator
)(unsigned long);
486 static void update_flags(struct cache_disk_superblock
*disk_super
,
487 flags_mutator mutator
)
489 uint32_t sb_flags
= mutator(le32_to_cpu(disk_super
->flags
));
490 disk_super
->flags
= cpu_to_le32(sb_flags
);
493 static unsigned long set_clean_shutdown(unsigned long flags
)
495 set_bit(CLEAN_SHUTDOWN
, &flags
);
499 static unsigned long clear_clean_shutdown(unsigned long flags
)
501 clear_bit(CLEAN_SHUTDOWN
, &flags
);
505 static void read_superblock_fields(struct dm_cache_metadata
*cmd
,
506 struct cache_disk_superblock
*disk_super
)
508 cmd
->root
= le64_to_cpu(disk_super
->mapping_root
);
509 cmd
->hint_root
= le64_to_cpu(disk_super
->hint_root
);
510 cmd
->discard_root
= le64_to_cpu(disk_super
->discard_root
);
511 cmd
->discard_block_size
= le64_to_cpu(disk_super
->discard_block_size
);
512 cmd
->discard_nr_blocks
= to_dblock(le64_to_cpu(disk_super
->discard_nr_blocks
));
513 cmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
514 cmd
->cache_blocks
= to_cblock(le32_to_cpu(disk_super
->cache_blocks
));
515 strncpy(cmd
->policy_name
, disk_super
->policy_name
, sizeof(cmd
->policy_name
));
516 cmd
->policy_version
[0] = le32_to_cpu(disk_super
->policy_version
[0]);
517 cmd
->policy_version
[1] = le32_to_cpu(disk_super
->policy_version
[1]);
518 cmd
->policy_version
[2] = le32_to_cpu(disk_super
->policy_version
[2]);
519 cmd
->policy_hint_size
= le32_to_cpu(disk_super
->policy_hint_size
);
521 cmd
->stats
.read_hits
= le32_to_cpu(disk_super
->read_hits
);
522 cmd
->stats
.read_misses
= le32_to_cpu(disk_super
->read_misses
);
523 cmd
->stats
.write_hits
= le32_to_cpu(disk_super
->write_hits
);
524 cmd
->stats
.write_misses
= le32_to_cpu(disk_super
->write_misses
);
526 cmd
->changed
= false;
530 * The mutator updates the superblock flags.
532 static int __begin_transaction_flags(struct dm_cache_metadata
*cmd
,
533 flags_mutator mutator
)
536 struct cache_disk_superblock
*disk_super
;
537 struct dm_block
*sblock
;
539 r
= superblock_lock(cmd
, &sblock
);
543 disk_super
= dm_block_data(sblock
);
544 update_flags(disk_super
, mutator
);
545 read_superblock_fields(cmd
, disk_super
);
546 dm_bm_unlock(sblock
);
548 return dm_bm_flush(cmd
->bm
);
551 static int __begin_transaction(struct dm_cache_metadata
*cmd
)
554 struct cache_disk_superblock
*disk_super
;
555 struct dm_block
*sblock
;
558 * We re-read the superblock every time. Shouldn't need to do this
561 r
= superblock_read_lock(cmd
, &sblock
);
565 disk_super
= dm_block_data(sblock
);
566 read_superblock_fields(cmd
, disk_super
);
567 dm_bm_unlock(sblock
);
572 static int __commit_transaction(struct dm_cache_metadata
*cmd
,
573 flags_mutator mutator
)
576 struct cache_disk_superblock
*disk_super
;
577 struct dm_block
*sblock
;
580 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
582 BUILD_BUG_ON(sizeof(struct cache_disk_superblock
) > 512);
584 r
= dm_bitset_flush(&cmd
->discard_info
, cmd
->discard_root
,
589 r
= dm_tm_pre_commit(cmd
->tm
);
593 r
= __save_sm_root(cmd
);
597 r
= superblock_lock(cmd
, &sblock
);
601 disk_super
= dm_block_data(sblock
);
604 update_flags(disk_super
, mutator
);
606 disk_super
->mapping_root
= cpu_to_le64(cmd
->root
);
607 disk_super
->hint_root
= cpu_to_le64(cmd
->hint_root
);
608 disk_super
->discard_root
= cpu_to_le64(cmd
->discard_root
);
609 disk_super
->discard_block_size
= cpu_to_le64(cmd
->discard_block_size
);
610 disk_super
->discard_nr_blocks
= cpu_to_le64(from_dblock(cmd
->discard_nr_blocks
));
611 disk_super
->cache_blocks
= cpu_to_le32(from_cblock(cmd
->cache_blocks
));
612 strncpy(disk_super
->policy_name
, cmd
->policy_name
, sizeof(disk_super
->policy_name
));
613 disk_super
->policy_version
[0] = cpu_to_le32(cmd
->policy_version
[0]);
614 disk_super
->policy_version
[1] = cpu_to_le32(cmd
->policy_version
[1]);
615 disk_super
->policy_version
[2] = cpu_to_le32(cmd
->policy_version
[2]);
617 disk_super
->read_hits
= cpu_to_le32(cmd
->stats
.read_hits
);
618 disk_super
->read_misses
= cpu_to_le32(cmd
->stats
.read_misses
);
619 disk_super
->write_hits
= cpu_to_le32(cmd
->stats
.write_hits
);
620 disk_super
->write_misses
= cpu_to_le32(cmd
->stats
.write_misses
);
621 __copy_sm_root(cmd
, disk_super
);
623 return dm_tm_commit(cmd
->tm
, sblock
);
626 /*----------------------------------------------------------------*/
629 * The mappings are held in a dm-array that has 64-bit values stored in
630 * little-endian format. The index is the cblock, the high 48bits of the
631 * value are the oblock and the low 16 bit the flags.
633 #define FLAGS_MASK ((1 << 16) - 1)
635 static __le64
pack_value(dm_oblock_t block
, unsigned flags
)
637 uint64_t value
= from_oblock(block
);
639 value
= value
| (flags
& FLAGS_MASK
);
640 return cpu_to_le64(value
);
643 static void unpack_value(__le64 value_le
, dm_oblock_t
*block
, unsigned *flags
)
645 uint64_t value
= le64_to_cpu(value_le
);
646 uint64_t b
= value
>> 16;
647 *block
= to_oblock(b
);
648 *flags
= value
& FLAGS_MASK
;
651 /*----------------------------------------------------------------*/
653 struct dm_cache_metadata
*dm_cache_metadata_open(struct block_device
*bdev
,
654 sector_t data_block_size
,
655 bool may_format_device
,
656 size_t policy_hint_size
)
659 struct dm_cache_metadata
*cmd
;
661 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
663 DMERR("could not allocate metadata struct");
667 init_rwsem(&cmd
->root_lock
);
669 cmd
->data_block_size
= data_block_size
;
670 cmd
->cache_blocks
= 0;
671 cmd
->policy_hint_size
= policy_hint_size
;
674 r
= __create_persistent_data_objects(cmd
, may_format_device
);
680 r
= __begin_transaction_flags(cmd
, clear_clean_shutdown
);
682 dm_cache_metadata_close(cmd
);
689 void dm_cache_metadata_close(struct dm_cache_metadata
*cmd
)
691 __destroy_persistent_data_objects(cmd
);
695 int dm_cache_resize(struct dm_cache_metadata
*cmd
, dm_cblock_t new_cache_size
)
698 __le64 null_mapping
= pack_value(0, 0);
700 down_write(&cmd
->root_lock
);
701 __dm_bless_for_disk(&null_mapping
);
702 r
= dm_array_resize(&cmd
->info
, cmd
->root
, from_cblock(cmd
->cache_blocks
),
703 from_cblock(new_cache_size
),
704 &null_mapping
, &cmd
->root
);
706 cmd
->cache_blocks
= new_cache_size
;
708 up_write(&cmd
->root_lock
);
713 int dm_cache_discard_bitset_resize(struct dm_cache_metadata
*cmd
,
714 sector_t discard_block_size
,
715 dm_dblock_t new_nr_entries
)
719 down_write(&cmd
->root_lock
);
720 r
= dm_bitset_resize(&cmd
->discard_info
,
722 from_dblock(cmd
->discard_nr_blocks
),
723 from_dblock(new_nr_entries
),
724 false, &cmd
->discard_root
);
726 cmd
->discard_block_size
= discard_block_size
;
727 cmd
->discard_nr_blocks
= new_nr_entries
;
731 up_write(&cmd
->root_lock
);
736 static int __set_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
738 return dm_bitset_set_bit(&cmd
->discard_info
, cmd
->discard_root
,
739 from_dblock(b
), &cmd
->discard_root
);
742 static int __clear_discard(struct dm_cache_metadata
*cmd
, dm_dblock_t b
)
744 return dm_bitset_clear_bit(&cmd
->discard_info
, cmd
->discard_root
,
745 from_dblock(b
), &cmd
->discard_root
);
748 static int __is_discarded(struct dm_cache_metadata
*cmd
, dm_dblock_t b
,
751 return dm_bitset_test_bit(&cmd
->discard_info
, cmd
->discard_root
,
752 from_dblock(b
), &cmd
->discard_root
,
756 static int __discard(struct dm_cache_metadata
*cmd
,
757 dm_dblock_t dblock
, bool discard
)
761 r
= (discard
? __set_discard
: __clear_discard
)(cmd
, dblock
);
769 int dm_cache_set_discard(struct dm_cache_metadata
*cmd
,
770 dm_dblock_t dblock
, bool discard
)
774 down_write(&cmd
->root_lock
);
775 r
= __discard(cmd
, dblock
, discard
);
776 up_write(&cmd
->root_lock
);
781 static int __load_discards(struct dm_cache_metadata
*cmd
,
782 load_discard_fn fn
, void *context
)
788 for (b
= 0; b
< from_dblock(cmd
->discard_nr_blocks
); b
++) {
789 dm_dblock_t dblock
= to_dblock(b
);
791 if (cmd
->clean_when_opened
) {
792 r
= __is_discarded(cmd
, dblock
, &discard
);
798 r
= fn(context
, cmd
->discard_block_size
, dblock
, discard
);
806 int dm_cache_load_discards(struct dm_cache_metadata
*cmd
,
807 load_discard_fn fn
, void *context
)
811 down_read(&cmd
->root_lock
);
812 r
= __load_discards(cmd
, fn
, context
);
813 up_read(&cmd
->root_lock
);
818 dm_cblock_t
dm_cache_size(struct dm_cache_metadata
*cmd
)
822 down_read(&cmd
->root_lock
);
823 r
= cmd
->cache_blocks
;
824 up_read(&cmd
->root_lock
);
829 static int __remove(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
832 __le64 value
= pack_value(0, 0);
834 __dm_bless_for_disk(&value
);
835 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
844 int dm_cache_remove_mapping(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
)
848 down_write(&cmd
->root_lock
);
849 r
= __remove(cmd
, cblock
);
850 up_write(&cmd
->root_lock
);
855 static int __insert(struct dm_cache_metadata
*cmd
,
856 dm_cblock_t cblock
, dm_oblock_t oblock
)
859 __le64 value
= pack_value(oblock
, M_VALID
);
860 __dm_bless_for_disk(&value
);
862 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
871 int dm_cache_insert_mapping(struct dm_cache_metadata
*cmd
,
872 dm_cblock_t cblock
, dm_oblock_t oblock
)
876 down_write(&cmd
->root_lock
);
877 r
= __insert(cmd
, cblock
, oblock
);
878 up_write(&cmd
->root_lock
);
887 struct dm_cache_metadata
*cmd
;
888 bool respect_dirty_flags
;
892 static bool policy_unchanged(struct dm_cache_metadata
*cmd
,
893 struct dm_cache_policy
*policy
)
895 const char *policy_name
= dm_cache_policy_get_name(policy
);
896 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
897 size_t policy_hint_size
= dm_cache_policy_get_hint_size(policy
);
900 * Ensure policy names match.
902 if (strncmp(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
)))
906 * Ensure policy major versions match.
908 if (cmd
->policy_version
[0] != policy_version
[0])
912 * Ensure policy hint sizes match.
914 if (cmd
->policy_hint_size
!= policy_hint_size
)
920 static bool hints_array_initialized(struct dm_cache_metadata
*cmd
)
922 return cmd
->hint_root
&& cmd
->policy_hint_size
;
925 static bool hints_array_available(struct dm_cache_metadata
*cmd
,
926 struct dm_cache_policy
*policy
)
928 return cmd
->clean_when_opened
&& policy_unchanged(cmd
, policy
) &&
929 hints_array_initialized(cmd
);
932 static int __load_mapping(void *context
, uint64_t cblock
, void *leaf
)
937 __le32 hint_value
= 0;
940 struct thunk
*thunk
= context
;
941 struct dm_cache_metadata
*cmd
= thunk
->cmd
;
943 memcpy(&value
, leaf
, sizeof(value
));
944 unpack_value(value
, &oblock
, &flags
);
946 if (flags
& M_VALID
) {
947 if (thunk
->hints_valid
) {
948 r
= dm_array_get_value(&cmd
->hint_info
, cmd
->hint_root
,
949 cblock
, &hint_value
);
950 if (r
&& r
!= -ENODATA
)
954 dirty
= thunk
->respect_dirty_flags
? (flags
& M_DIRTY
) : true;
955 r
= thunk
->fn(thunk
->context
, oblock
, to_cblock(cblock
),
956 dirty
, le32_to_cpu(hint_value
), thunk
->hints_valid
);
962 static int __load_mappings(struct dm_cache_metadata
*cmd
,
963 struct dm_cache_policy
*policy
,
964 load_mapping_fn fn
, void *context
)
969 thunk
.context
= context
;
972 thunk
.respect_dirty_flags
= cmd
->clean_when_opened
;
973 thunk
.hints_valid
= hints_array_available(cmd
, policy
);
975 return dm_array_walk(&cmd
->info
, cmd
->root
, __load_mapping
, &thunk
);
978 int dm_cache_load_mappings(struct dm_cache_metadata
*cmd
,
979 struct dm_cache_policy
*policy
,
980 load_mapping_fn fn
, void *context
)
984 down_read(&cmd
->root_lock
);
985 r
= __load_mappings(cmd
, policy
, fn
, context
);
986 up_read(&cmd
->root_lock
);
991 static int __dump_mapping(void *context
, uint64_t cblock
, void *leaf
)
998 memcpy(&value
, leaf
, sizeof(value
));
999 unpack_value(value
, &oblock
, &flags
);
1004 static int __dump_mappings(struct dm_cache_metadata
*cmd
)
1006 return dm_array_walk(&cmd
->info
, cmd
->root
, __dump_mapping
, NULL
);
1009 void dm_cache_dump(struct dm_cache_metadata
*cmd
)
1011 down_read(&cmd
->root_lock
);
1012 __dump_mappings(cmd
);
1013 up_read(&cmd
->root_lock
);
1016 int dm_cache_changed_this_transaction(struct dm_cache_metadata
*cmd
)
1020 down_read(&cmd
->root_lock
);
1022 up_read(&cmd
->root_lock
);
1027 static int __dirty(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
, bool dirty
)
1034 r
= dm_array_get_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
), &value
);
1038 unpack_value(value
, &oblock
, &flags
);
1040 if (((flags
& M_DIRTY
) && dirty
) || (!(flags
& M_DIRTY
) && !dirty
))
1041 /* nothing to be done */
1044 value
= pack_value(oblock
, (flags
& ~M_DIRTY
) | (dirty
? M_DIRTY
: 0));
1045 __dm_bless_for_disk(&value
);
1047 r
= dm_array_set_value(&cmd
->info
, cmd
->root
, from_cblock(cblock
),
1048 &value
, &cmd
->root
);
1052 cmd
->changed
= true;
1057 int dm_cache_set_dirty(struct dm_cache_metadata
*cmd
,
1058 dm_cblock_t cblock
, bool dirty
)
1062 down_write(&cmd
->root_lock
);
1063 r
= __dirty(cmd
, cblock
, dirty
);
1064 up_write(&cmd
->root_lock
);
1069 void dm_cache_metadata_get_stats(struct dm_cache_metadata
*cmd
,
1070 struct dm_cache_statistics
*stats
)
1072 down_read(&cmd
->root_lock
);
1073 *stats
= cmd
->stats
;
1074 up_read(&cmd
->root_lock
);
1077 void dm_cache_metadata_set_stats(struct dm_cache_metadata
*cmd
,
1078 struct dm_cache_statistics
*stats
)
1080 down_write(&cmd
->root_lock
);
1081 cmd
->stats
= *stats
;
1082 up_write(&cmd
->root_lock
);
1085 int dm_cache_commit(struct dm_cache_metadata
*cmd
, bool clean_shutdown
)
1088 flags_mutator mutator
= (clean_shutdown
? set_clean_shutdown
:
1089 clear_clean_shutdown
);
1091 down_write(&cmd
->root_lock
);
1092 r
= __commit_transaction(cmd
, mutator
);
1096 r
= __begin_transaction(cmd
);
1099 up_write(&cmd
->root_lock
);
1103 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata
*cmd
,
1108 down_read(&cmd
->root_lock
);
1109 r
= dm_sm_get_nr_free(cmd
->metadata_sm
, result
);
1110 up_read(&cmd
->root_lock
);
1115 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata
*cmd
,
1120 down_read(&cmd
->root_lock
);
1121 r
= dm_sm_get_nr_blocks(cmd
->metadata_sm
, result
);
1122 up_read(&cmd
->root_lock
);
1127 /*----------------------------------------------------------------*/
1129 static int begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1134 const char *policy_name
= dm_cache_policy_get_name(policy
);
1135 const unsigned *policy_version
= dm_cache_policy_get_version(policy
);
1137 if (!policy_name
[0] ||
1138 (strlen(policy_name
) > sizeof(cmd
->policy_name
) - 1))
1141 if (!policy_unchanged(cmd
, policy
)) {
1142 strncpy(cmd
->policy_name
, policy_name
, sizeof(cmd
->policy_name
));
1143 memcpy(cmd
->policy_version
, policy_version
, sizeof(cmd
->policy_version
));
1145 hint_size
= dm_cache_policy_get_hint_size(policy
);
1147 return 0; /* short-circuit hints initialization */
1148 cmd
->policy_hint_size
= hint_size
;
1150 if (cmd
->hint_root
) {
1151 r
= dm_array_del(&cmd
->hint_info
, cmd
->hint_root
);
1156 r
= dm_array_empty(&cmd
->hint_info
, &cmd
->hint_root
);
1160 value
= cpu_to_le32(0);
1161 __dm_bless_for_disk(&value
);
1162 r
= dm_array_resize(&cmd
->hint_info
, cmd
->hint_root
, 0,
1163 from_cblock(cmd
->cache_blocks
),
1164 &value
, &cmd
->hint_root
);
1172 int dm_cache_begin_hints(struct dm_cache_metadata
*cmd
, struct dm_cache_policy
*policy
)
1176 down_write(&cmd
->root_lock
);
1177 r
= begin_hints(cmd
, policy
);
1178 up_write(&cmd
->root_lock
);
1183 static int save_hint(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
,
1187 __le32 value
= cpu_to_le32(hint
);
1188 __dm_bless_for_disk(&value
);
1190 r
= dm_array_set_value(&cmd
->hint_info
, cmd
->hint_root
,
1191 from_cblock(cblock
), &value
, &cmd
->hint_root
);
1192 cmd
->changed
= true;
1197 int dm_cache_save_hint(struct dm_cache_metadata
*cmd
, dm_cblock_t cblock
,
1202 if (!hints_array_initialized(cmd
))
1205 down_write(&cmd
->root_lock
);
1206 r
= save_hint(cmd
, cblock
, hint
);
1207 up_write(&cmd
->root_lock
);