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;
88 struct dm_cache_metadata {
89 struct block_device *bdev;
90 struct dm_block_manager *bm;
91 struct dm_space_map *metadata_sm;
92 struct dm_transaction_manager *tm;
94 struct dm_array_info info;
95 struct dm_array_info hint_info;
96 struct dm_disk_bitset discard_info;
98 struct rw_semaphore root_lock;
100 dm_block_t hint_root;
101 dm_block_t discard_root;
103 sector_t discard_block_size;
104 dm_dblock_t discard_nr_blocks;
106 sector_t data_block_size;
107 dm_cblock_t cache_blocks;
109 bool clean_when_opened:1;
111 char policy_name[CACHE_POLICY_NAME_SIZE];
112 size_t policy_hint_size;
113 struct dm_cache_statistics stats;
116 /*-------------------------------------------------------------------
117 * superblock validator
118 *-----------------------------------------------------------------*/
120 #define SUPERBLOCK_CSUM_XOR 9031977
122 static void sb_prepare_for_write(struct dm_block_validator *v,
124 size_t sb_block_size)
126 struct cache_disk_superblock *disk_super = dm_block_data(b);
128 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
129 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
130 sb_block_size - sizeof(__le32),
131 SUPERBLOCK_CSUM_XOR));
134 static int sb_check(struct dm_block_validator *v,
136 size_t sb_block_size)
138 struct cache_disk_superblock *disk_super = dm_block_data(b);
141 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
142 DMERR("sb_check failed: blocknr %llu: wanted %llu",
143 le64_to_cpu(disk_super->blocknr),
144 (unsigned long long)dm_block_location(b));
148 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
149 DMERR("sb_check failed: magic %llu: wanted %llu",
150 le64_to_cpu(disk_super->magic),
151 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
155 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
156 sb_block_size - sizeof(__le32),
157 SUPERBLOCK_CSUM_XOR));
158 if (csum_le != disk_super->csum) {
159 DMERR("sb_check failed: csum %u: wanted %u",
160 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
167 static struct dm_block_validator sb_validator = {
168 .name = "superblock",
169 .prepare_for_write = sb_prepare_for_write,
173 /*----------------------------------------------------------------*/
175 static int superblock_read_lock(struct dm_cache_metadata *cmd,
176 struct dm_block **sblock)
178 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
179 &sb_validator, sblock);
182 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
183 struct dm_block **sblock)
185 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
186 &sb_validator, sblock);
189 static int superblock_lock(struct dm_cache_metadata *cmd,
190 struct dm_block **sblock)
192 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
193 &sb_validator, sblock);
196 /*----------------------------------------------------------------*/
198 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
203 __le64 *data_le, zero = cpu_to_le64(0);
204 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
207 * We can't use a validator here - it may be all zeroes.
209 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
213 data_le = dm_block_data(b);
215 for (i = 0; i < sb_block_size; i++) {
216 if (data_le[i] != zero) {
222 return dm_bm_unlock(b);
225 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
227 struct dm_btree_value_type vt;
230 vt.size = sizeof(__le64);
234 dm_array_info_init(&cmd->info, cmd->tm, &vt);
236 if (cmd->policy_hint_size) {
237 vt.size = sizeof(__le32);
238 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
242 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
245 struct dm_block *sblock;
247 struct cache_disk_superblock *disk_super;
248 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
250 /* FIXME: see if we can lose the max sectors limit */
251 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
252 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
254 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
258 r = dm_tm_pre_commit(cmd->tm);
262 r = superblock_lock_zero(cmd, &sblock);
266 disk_super = dm_block_data(sblock);
267 disk_super->flags = 0;
268 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
269 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
270 disk_super->version = cpu_to_le32(CACHE_VERSION);
271 memset(disk_super->policy_name, 0, CACHE_POLICY_NAME_SIZE);
272 disk_super->policy_hint_size = 0;
274 r = dm_sm_copy_root(cmd->metadata_sm, &disk_super->metadata_space_map_root,
279 disk_super->mapping_root = cpu_to_le64(cmd->root);
280 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
281 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
282 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
283 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
284 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
285 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
286 disk_super->cache_blocks = cpu_to_le32(0);
287 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
289 disk_super->read_hits = cpu_to_le32(0);
290 disk_super->read_misses = cpu_to_le32(0);
291 disk_super->write_hits = cpu_to_le32(0);
292 disk_super->write_misses = cpu_to_le32(0);
294 return dm_tm_commit(cmd->tm, sblock);
297 dm_bm_unlock(sblock);
301 static int __format_metadata(struct dm_cache_metadata *cmd)
305 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
306 &cmd->tm, &cmd->metadata_sm);
308 DMERR("tm_create_with_sm failed");
312 __setup_mapping_info(cmd);
314 r = dm_array_empty(&cmd->info, &cmd->root);
318 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
320 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
324 cmd->discard_block_size = 0;
325 cmd->discard_nr_blocks = 0;
327 r = __write_initial_superblock(cmd);
331 cmd->clean_when_opened = true;
335 dm_tm_destroy(cmd->tm);
336 dm_sm_destroy(cmd->metadata_sm);
341 static int __check_incompat_features(struct cache_disk_superblock *disk_super,
342 struct dm_cache_metadata *cmd)
346 features = le32_to_cpu(disk_super->incompat_flags) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
348 DMERR("could not access metadata due to unsupported optional features (%lx).",
349 (unsigned long)features);
354 * Check for read-only metadata to skip the following RDWR checks.
356 if (get_disk_ro(cmd->bdev->bd_disk))
359 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
361 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
362 (unsigned long)features);
369 static int __open_metadata(struct dm_cache_metadata *cmd)
372 struct dm_block *sblock;
373 struct cache_disk_superblock *disk_super;
374 unsigned long sb_flags;
376 r = superblock_read_lock(cmd, &sblock);
378 DMERR("couldn't read lock superblock");
382 disk_super = dm_block_data(sblock);
384 r = __check_incompat_features(disk_super, cmd);
388 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
389 disk_super->metadata_space_map_root,
390 sizeof(disk_super->metadata_space_map_root),
391 &cmd->tm, &cmd->metadata_sm);
393 DMERR("tm_open_with_sm failed");
397 __setup_mapping_info(cmd);
398 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
399 sb_flags = le32_to_cpu(disk_super->flags);
400 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
401 return dm_bm_unlock(sblock);
404 dm_bm_unlock(sblock);
408 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
413 r = __superblock_all_zeroes(cmd->bm, &unformatted);
418 return format_device ? __format_metadata(cmd) : -EPERM;
420 return __open_metadata(cmd);
423 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
424 bool may_format_device)
427 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE,
428 CACHE_METADATA_CACHE_SIZE,
429 CACHE_MAX_CONCURRENT_LOCKS);
430 if (IS_ERR(cmd->bm)) {
431 DMERR("could not create block manager");
432 return PTR_ERR(cmd->bm);
435 r = __open_or_format_metadata(cmd, may_format_device);
437 dm_block_manager_destroy(cmd->bm);
442 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
444 dm_sm_destroy(cmd->metadata_sm);
445 dm_tm_destroy(cmd->tm);
446 dm_block_manager_destroy(cmd->bm);
449 typedef unsigned long (*flags_mutator)(unsigned long);
451 static void update_flags(struct cache_disk_superblock *disk_super,
452 flags_mutator mutator)
454 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
455 disk_super->flags = cpu_to_le32(sb_flags);
458 static unsigned long set_clean_shutdown(unsigned long flags)
460 set_bit(CLEAN_SHUTDOWN, &flags);
464 static unsigned long clear_clean_shutdown(unsigned long flags)
466 clear_bit(CLEAN_SHUTDOWN, &flags);
470 static void read_superblock_fields(struct dm_cache_metadata *cmd,
471 struct cache_disk_superblock *disk_super)
473 cmd->root = le64_to_cpu(disk_super->mapping_root);
474 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
475 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
476 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
477 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
478 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
479 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
480 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
481 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
483 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
484 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
485 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
486 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
488 cmd->changed = false;
492 * The mutator updates the superblock flags.
494 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
495 flags_mutator mutator)
498 struct cache_disk_superblock *disk_super;
499 struct dm_block *sblock;
501 r = superblock_lock(cmd, &sblock);
505 disk_super = dm_block_data(sblock);
506 update_flags(disk_super, mutator);
507 read_superblock_fields(cmd, disk_super);
509 return dm_bm_flush_and_unlock(cmd->bm, sblock);
512 static int __begin_transaction(struct dm_cache_metadata *cmd)
515 struct cache_disk_superblock *disk_super;
516 struct dm_block *sblock;
519 * We re-read the superblock every time. Shouldn't need to do this
522 r = superblock_read_lock(cmd, &sblock);
526 disk_super = dm_block_data(sblock);
527 read_superblock_fields(cmd, disk_super);
528 dm_bm_unlock(sblock);
533 static int __commit_transaction(struct dm_cache_metadata *cmd,
534 flags_mutator mutator)
538 struct cache_disk_superblock *disk_super;
539 struct dm_block *sblock;
542 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
544 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
546 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
551 r = dm_tm_pre_commit(cmd->tm);
555 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
559 r = superblock_lock(cmd, &sblock);
563 disk_super = dm_block_data(sblock);
566 update_flags(disk_super, mutator);
568 disk_super->mapping_root = cpu_to_le64(cmd->root);
569 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
570 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
571 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
572 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
573 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
574 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
576 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
577 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
578 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
579 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
581 r = dm_sm_copy_root(cmd->metadata_sm, &disk_super->metadata_space_map_root,
584 dm_bm_unlock(sblock);
588 return dm_tm_commit(cmd->tm, sblock);
591 /*----------------------------------------------------------------*/
594 * The mappings are held in a dm-array that has 64-bit values stored in
595 * little-endian format. The index is the cblock, the high 48bits of the
596 * value are the oblock and the low 16 bit the flags.
598 #define FLAGS_MASK ((1 << 16) - 1)
600 static __le64 pack_value(dm_oblock_t block, unsigned flags)
602 uint64_t value = from_oblock(block);
604 value = value | (flags & FLAGS_MASK);
605 return cpu_to_le64(value);
608 static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
610 uint64_t value = le64_to_cpu(value_le);
611 uint64_t b = value >> 16;
612 *block = to_oblock(b);
613 *flags = value & FLAGS_MASK;
616 /*----------------------------------------------------------------*/
618 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
619 sector_t data_block_size,
620 bool may_format_device,
621 size_t policy_hint_size)
624 struct dm_cache_metadata *cmd;
626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
628 DMERR("could not allocate metadata struct");
632 init_rwsem(&cmd->root_lock);
634 cmd->data_block_size = data_block_size;
635 cmd->cache_blocks = 0;
636 cmd->policy_hint_size = policy_hint_size;
639 r = __create_persistent_data_objects(cmd, may_format_device);
645 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
647 dm_cache_metadata_close(cmd);
654 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
656 __destroy_persistent_data_objects(cmd);
660 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
663 __le64 null_mapping = pack_value(0, 0);
665 down_write(&cmd->root_lock);
666 __dm_bless_for_disk(&null_mapping);
667 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
668 from_cblock(new_cache_size),
669 &null_mapping, &cmd->root);
671 cmd->cache_blocks = new_cache_size;
673 up_write(&cmd->root_lock);
678 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
679 sector_t discard_block_size,
680 dm_dblock_t new_nr_entries)
684 down_write(&cmd->root_lock);
685 r = dm_bitset_resize(&cmd->discard_info,
687 from_dblock(cmd->discard_nr_blocks),
688 from_dblock(new_nr_entries),
689 false, &cmd->discard_root);
691 cmd->discard_block_size = discard_block_size;
692 cmd->discard_nr_blocks = new_nr_entries;
696 up_write(&cmd->root_lock);
701 static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
703 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
704 from_dblock(b), &cmd->discard_root);
707 static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
709 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
710 from_dblock(b), &cmd->discard_root);
713 static int __is_discarded(struct dm_cache_metadata *cmd, dm_dblock_t b,
716 return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
717 from_dblock(b), &cmd->discard_root,
721 static int __discard(struct dm_cache_metadata *cmd,
722 dm_dblock_t dblock, bool discard)
726 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
734 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
735 dm_dblock_t dblock, bool discard)
739 down_write(&cmd->root_lock);
740 r = __discard(cmd, dblock, discard);
741 up_write(&cmd->root_lock);
746 static int __load_discards(struct dm_cache_metadata *cmd,
747 load_discard_fn fn, void *context)
753 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
754 dm_dblock_t dblock = to_dblock(b);
756 if (cmd->clean_when_opened) {
757 r = __is_discarded(cmd, dblock, &discard);
763 r = fn(context, cmd->discard_block_size, dblock, discard);
771 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
772 load_discard_fn fn, void *context)
776 down_read(&cmd->root_lock);
777 r = __load_discards(cmd, fn, context);
778 up_read(&cmd->root_lock);
783 dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd)
787 down_read(&cmd->root_lock);
788 r = cmd->cache_blocks;
789 up_read(&cmd->root_lock);
794 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
797 __le64 value = pack_value(0, 0);
799 __dm_bless_for_disk(&value);
800 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
809 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
813 down_write(&cmd->root_lock);
814 r = __remove(cmd, cblock);
815 up_write(&cmd->root_lock);
820 static int __insert(struct dm_cache_metadata *cmd,
821 dm_cblock_t cblock, dm_oblock_t oblock)
824 __le64 value = pack_value(oblock, M_VALID);
825 __dm_bless_for_disk(&value);
827 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
836 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
837 dm_cblock_t cblock, dm_oblock_t oblock)
841 down_write(&cmd->root_lock);
842 r = __insert(cmd, cblock, oblock);
843 up_write(&cmd->root_lock);
852 struct dm_cache_metadata *cmd;
853 bool respect_dirty_flags;
857 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
859 return cmd->hint_root && cmd->policy_hint_size;
862 static bool hints_array_available(struct dm_cache_metadata *cmd,
863 const char *policy_name)
865 bool policy_names_match = !strncmp(cmd->policy_name, policy_name,
866 sizeof(cmd->policy_name));
868 return cmd->clean_when_opened && policy_names_match &&
869 hints_array_initialized(cmd);
872 static int __load_mapping(void *context, uint64_t cblock, void *leaf)
877 __le32 hint_value = 0;
880 struct thunk *thunk = context;
881 struct dm_cache_metadata *cmd = thunk->cmd;
883 memcpy(&value, leaf, sizeof(value));
884 unpack_value(value, &oblock, &flags);
886 if (flags & M_VALID) {
887 if (thunk->hints_valid) {
888 r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
889 cblock, &hint_value);
890 if (r && r != -ENODATA)
894 dirty = thunk->respect_dirty_flags ? (flags & M_DIRTY) : true;
895 r = thunk->fn(thunk->context, oblock, to_cblock(cblock),
896 dirty, le32_to_cpu(hint_value), thunk->hints_valid);
902 static int __load_mappings(struct dm_cache_metadata *cmd, const char *policy_name,
903 load_mapping_fn fn, void *context)
908 thunk.context = context;
911 thunk.respect_dirty_flags = cmd->clean_when_opened;
912 thunk.hints_valid = hints_array_available(cmd, policy_name);
914 return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
917 int dm_cache_load_mappings(struct dm_cache_metadata *cmd, const char *policy_name,
918 load_mapping_fn fn, void *context)
922 down_read(&cmd->root_lock);
923 r = __load_mappings(cmd, policy_name, fn, context);
924 up_read(&cmd->root_lock);
929 static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
936 memcpy(&value, leaf, sizeof(value));
937 unpack_value(value, &oblock, &flags);
942 static int __dump_mappings(struct dm_cache_metadata *cmd)
944 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
947 void dm_cache_dump(struct dm_cache_metadata *cmd)
949 down_read(&cmd->root_lock);
950 __dump_mappings(cmd);
951 up_read(&cmd->root_lock);
954 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
958 down_read(&cmd->root_lock);
960 up_read(&cmd->root_lock);
965 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
972 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
976 unpack_value(value, &oblock, &flags);
978 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
979 /* nothing to be done */
982 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
983 __dm_bless_for_disk(&value);
985 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
995 int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
996 dm_cblock_t cblock, bool dirty)
1000 down_write(&cmd->root_lock);
1001 r = __dirty(cmd, cblock, dirty);
1002 up_write(&cmd->root_lock);
1007 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1008 struct dm_cache_statistics *stats)
1010 down_read(&cmd->root_lock);
1011 memcpy(stats, &cmd->stats, sizeof(*stats));
1012 up_read(&cmd->root_lock);
1015 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1016 struct dm_cache_statistics *stats)
1018 down_write(&cmd->root_lock);
1019 memcpy(&cmd->stats, stats, sizeof(*stats));
1020 up_write(&cmd->root_lock);
1023 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1026 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1027 clear_clean_shutdown);
1029 down_write(&cmd->root_lock);
1030 r = __commit_transaction(cmd, mutator);
1034 r = __begin_transaction(cmd);
1037 up_write(&cmd->root_lock);
1041 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1046 down_read(&cmd->root_lock);
1047 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1048 up_read(&cmd->root_lock);
1053 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1058 down_read(&cmd->root_lock);
1059 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1060 up_read(&cmd->root_lock);
1065 /*----------------------------------------------------------------*/
1067 static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1072 const char *policy_name = dm_cache_policy_get_name(policy);
1074 if (!policy_name[0] ||
1075 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1078 if (strcmp(cmd->policy_name, policy_name)) {
1079 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1081 hint_size = dm_cache_policy_get_hint_size(policy);
1083 return 0; /* short-circuit hints initialization */
1084 cmd->policy_hint_size = hint_size;
1086 if (cmd->hint_root) {
1087 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1092 r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
1096 value = cpu_to_le32(0);
1097 __dm_bless_for_disk(&value);
1098 r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
1099 from_cblock(cmd->cache_blocks),
1100 &value, &cmd->hint_root);
1108 int dm_cache_begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1112 down_write(&cmd->root_lock);
1113 r = begin_hints(cmd, policy);
1114 up_write(&cmd->root_lock);
1119 static int save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1123 __le32 value = cpu_to_le32(hint);
1124 __dm_bless_for_disk(&value);
1126 r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
1127 from_cblock(cblock), &value, &cmd->hint_root);
1128 cmd->changed = true;
1133 int dm_cache_save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1138 if (!hints_array_initialized(cmd))
1141 down_write(&cmd->root_lock);
1142 r = save_hint(cmd, cblock, hint);
1143 up_write(&cmd->root_lock);