Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / md / dm-cache-metadata.c
bloba9a1ab284076a989cd31aea4b971c67fdcb59f20
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Red Hat, Inc.
5 * This file is released under the GPL.
6 */
8 #include "dm-cache-metadata.h"
10 #include "persistent-data/dm-array.h"
11 #include "persistent-data/dm-bitset.h"
12 #include "persistent-data/dm-space-map.h"
13 #include "persistent-data/dm-space-map-disk.h"
14 #include "persistent-data/dm-transaction-manager.h"
16 #include <linux/device-mapper.h>
17 #include <linux/refcount.h>
19 /*----------------------------------------------------------------*/
21 #define DM_MSG_PREFIX "cache metadata"
23 #define CACHE_SUPERBLOCK_MAGIC 06142003
24 #define CACHE_SUPERBLOCK_LOCATION 0
27 * defines a range of metadata versions that this module can handle.
29 #define MIN_CACHE_VERSION 1
30 #define MAX_CACHE_VERSION 2
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 */
41 CLEAN_SHUTDOWN,
42 /* metadata must be checked using the tools */
43 NEEDS_CHECK,
47 * Each mapping from cache block -> origin block carries a set of flags.
49 enum mapping_bits {
51 * A valid mapping. Because we're using an array we clear this
52 * flag for an non existant mapping.
54 M_VALID = 1,
57 * The data on the cache is different from that on the origin.
58 * This flag is only used by metadata format 1.
60 M_DIRTY = 2
63 struct cache_disk_superblock {
64 __le32 csum;
65 __le32 flags;
66 __le64 blocknr;
68 __u8 uuid[16];
69 __le64 magic;
70 __le32 version;
72 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
73 __le32 policy_hint_size;
75 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
76 __le64 mapping_root;
77 __le64 hint_root;
79 __le64 discard_root;
80 __le64 discard_block_size;
81 __le64 discard_nr_blocks;
83 __le32 data_block_size;
84 __le32 metadata_block_size;
85 __le32 cache_blocks;
87 __le32 compat_flags;
88 __le32 compat_ro_flags;
89 __le32 incompat_flags;
91 __le32 read_hits;
92 __le32 read_misses;
93 __le32 write_hits;
94 __le32 write_misses;
96 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
99 * Metadata format 2 fields.
101 __le64 dirty_root;
102 } __packed;
104 struct dm_cache_metadata {
105 refcount_t ref_count;
106 struct list_head list;
108 unsigned int version;
109 struct block_device *bdev;
110 struct dm_block_manager *bm;
111 struct dm_space_map *metadata_sm;
112 struct dm_transaction_manager *tm;
114 struct dm_array_info info;
115 struct dm_array_info hint_info;
116 struct dm_disk_bitset discard_info;
118 struct rw_semaphore root_lock;
119 unsigned long flags;
120 dm_block_t root;
121 dm_block_t hint_root;
122 dm_block_t discard_root;
124 sector_t discard_block_size;
125 dm_dblock_t discard_nr_blocks;
127 sector_t data_block_size;
128 dm_cblock_t cache_blocks;
129 bool changed:1;
130 bool clean_when_opened:1;
132 char policy_name[CACHE_POLICY_NAME_SIZE];
133 unsigned int policy_version[CACHE_POLICY_VERSION_SIZE];
134 size_t policy_hint_size;
135 struct dm_cache_statistics stats;
138 * Reading the space map root can fail, so we read it into this
139 * buffer before the superblock is locked and updated.
141 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
144 * Set if a transaction has to be aborted but the attempt to roll
145 * back to the previous (good) transaction failed. The only
146 * metadata operation permissible in this state is the closing of
147 * the device.
149 bool fail_io:1;
152 * Metadata format 2 fields.
154 dm_block_t dirty_root;
155 struct dm_disk_bitset dirty_info;
158 * These structures are used when loading metadata. They're too
159 * big to put on the stack.
161 struct dm_array_cursor mapping_cursor;
162 struct dm_array_cursor hint_cursor;
163 struct dm_bitset_cursor dirty_cursor;
167 *-----------------------------------------------------------------
168 * superblock validator
169 *-----------------------------------------------------------------
171 #define SUPERBLOCK_CSUM_XOR 9031977
173 static void sb_prepare_for_write(const struct dm_block_validator *v,
174 struct dm_block *b,
175 size_t sb_block_size)
177 struct cache_disk_superblock *disk_super = dm_block_data(b);
179 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
180 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
181 sb_block_size - sizeof(__le32),
182 SUPERBLOCK_CSUM_XOR));
185 static int check_metadata_version(struct cache_disk_superblock *disk_super)
187 uint32_t metadata_version = le32_to_cpu(disk_super->version);
189 if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
190 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
191 metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
192 return -EINVAL;
195 return 0;
198 static int sb_check(const struct dm_block_validator *v,
199 struct dm_block *b,
200 size_t sb_block_size)
202 struct cache_disk_superblock *disk_super = dm_block_data(b);
203 __le32 csum_le;
205 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
206 DMERR("%s failed: blocknr %llu: wanted %llu",
207 __func__, le64_to_cpu(disk_super->blocknr),
208 (unsigned long long)dm_block_location(b));
209 return -ENOTBLK;
212 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
213 DMERR("%s failed: magic %llu: wanted %llu",
214 __func__, le64_to_cpu(disk_super->magic),
215 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
216 return -EILSEQ;
219 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
220 sb_block_size - sizeof(__le32),
221 SUPERBLOCK_CSUM_XOR));
222 if (csum_le != disk_super->csum) {
223 DMERR("%s failed: csum %u: wanted %u",
224 __func__, le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
225 return -EILSEQ;
228 return check_metadata_version(disk_super);
231 static const struct dm_block_validator sb_validator = {
232 .name = "superblock",
233 .prepare_for_write = sb_prepare_for_write,
234 .check = sb_check
237 /*----------------------------------------------------------------*/
239 static int superblock_read_lock(struct dm_cache_metadata *cmd,
240 struct dm_block **sblock)
242 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
243 &sb_validator, sblock);
246 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
247 struct dm_block **sblock)
249 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
250 &sb_validator, sblock);
253 static int superblock_lock(struct dm_cache_metadata *cmd,
254 struct dm_block **sblock)
256 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
257 &sb_validator, sblock);
260 /*----------------------------------------------------------------*/
262 static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
264 int r;
265 unsigned int i;
266 struct dm_block *b;
267 __le64 *data_le, zero = cpu_to_le64(0);
268 unsigned int sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
271 * We can't use a validator here - it may be all zeroes.
273 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
274 if (r)
275 return r;
277 data_le = dm_block_data(b);
278 *result = true;
279 for (i = 0; i < sb_block_size; i++) {
280 if (data_le[i] != zero) {
281 *result = false;
282 break;
286 dm_bm_unlock(b);
288 return 0;
291 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
293 struct dm_btree_value_type vt;
295 vt.context = NULL;
296 vt.size = sizeof(__le64);
297 vt.inc = NULL;
298 vt.dec = NULL;
299 vt.equal = NULL;
300 dm_array_info_init(&cmd->info, cmd->tm, &vt);
302 if (cmd->policy_hint_size) {
303 vt.size = sizeof(__le32);
304 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
308 static int __save_sm_root(struct dm_cache_metadata *cmd)
310 int r;
311 size_t metadata_len;
313 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
314 if (r < 0)
315 return r;
317 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
318 metadata_len);
321 static void __copy_sm_root(struct dm_cache_metadata *cmd,
322 struct cache_disk_superblock *disk_super)
324 memcpy(&disk_super->metadata_space_map_root,
325 &cmd->metadata_space_map_root,
326 sizeof(cmd->metadata_space_map_root));
329 static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
331 return cmd->version >= 2;
334 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
336 int r;
337 struct dm_block *sblock;
338 struct cache_disk_superblock *disk_super;
339 sector_t bdev_size = bdev_nr_sectors(cmd->bdev);
341 /* FIXME: see if we can lose the max sectors limit */
342 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
343 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
345 r = dm_tm_pre_commit(cmd->tm);
346 if (r < 0)
347 return r;
350 * dm_sm_copy_root() can fail. So we need to do it before we start
351 * updating the superblock.
353 r = __save_sm_root(cmd);
354 if (r)
355 return r;
357 r = superblock_lock_zero(cmd, &sblock);
358 if (r)
359 return r;
361 disk_super = dm_block_data(sblock);
362 disk_super->flags = 0;
363 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
364 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
365 disk_super->version = cpu_to_le32(cmd->version);
366 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
367 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
368 disk_super->policy_hint_size = cpu_to_le32(0);
370 __copy_sm_root(cmd, disk_super);
372 disk_super->mapping_root = cpu_to_le64(cmd->root);
373 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
374 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
375 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
376 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
377 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
378 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
379 disk_super->cache_blocks = cpu_to_le32(0);
381 disk_super->read_hits = cpu_to_le32(0);
382 disk_super->read_misses = cpu_to_le32(0);
383 disk_super->write_hits = cpu_to_le32(0);
384 disk_super->write_misses = cpu_to_le32(0);
386 if (separate_dirty_bits(cmd))
387 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
389 return dm_tm_commit(cmd->tm, sblock);
392 static int __format_metadata(struct dm_cache_metadata *cmd)
394 int r;
396 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
397 &cmd->tm, &cmd->metadata_sm);
398 if (r < 0) {
399 DMERR("tm_create_with_sm failed");
400 return r;
403 __setup_mapping_info(cmd);
405 r = dm_array_empty(&cmd->info, &cmd->root);
406 if (r < 0)
407 goto bad;
409 if (separate_dirty_bits(cmd)) {
410 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
411 r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
412 if (r < 0)
413 goto bad;
416 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
417 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
418 if (r < 0)
419 goto bad;
421 cmd->discard_block_size = 0;
422 cmd->discard_nr_blocks = 0;
424 r = __write_initial_superblock(cmd);
425 if (r)
426 goto bad;
428 cmd->clean_when_opened = true;
429 return 0;
431 bad:
432 dm_tm_destroy(cmd->tm);
433 dm_sm_destroy(cmd->metadata_sm);
435 return r;
438 static int __check_incompat_features(struct cache_disk_superblock *disk_super,
439 struct dm_cache_metadata *cmd)
441 uint32_t incompat_flags, features;
443 incompat_flags = le32_to_cpu(disk_super->incompat_flags);
444 features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
445 if (features) {
446 DMERR("could not access metadata due to unsupported optional features (%lx).",
447 (unsigned long)features);
448 return -EINVAL;
452 * Check for read-only metadata to skip the following RDWR checks.
454 if (bdev_read_only(cmd->bdev))
455 return 0;
457 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
458 if (features) {
459 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
460 (unsigned long)features);
461 return -EINVAL;
464 return 0;
467 static int __open_metadata(struct dm_cache_metadata *cmd)
469 int r;
470 struct dm_block *sblock;
471 struct cache_disk_superblock *disk_super;
472 unsigned long sb_flags;
474 r = superblock_read_lock(cmd, &sblock);
475 if (r < 0) {
476 DMERR("couldn't read lock superblock");
477 return r;
480 disk_super = dm_block_data(sblock);
482 /* Verify the data block size hasn't changed */
483 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
484 DMERR("changing the data block size (from %u to %llu) is not supported",
485 le32_to_cpu(disk_super->data_block_size),
486 (unsigned long long)cmd->data_block_size);
487 r = -EINVAL;
488 goto bad;
491 r = __check_incompat_features(disk_super, cmd);
492 if (r < 0)
493 goto bad;
495 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
496 disk_super->metadata_space_map_root,
497 sizeof(disk_super->metadata_space_map_root),
498 &cmd->tm, &cmd->metadata_sm);
499 if (r < 0) {
500 DMERR("tm_open_with_sm failed");
501 goto bad;
504 __setup_mapping_info(cmd);
505 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
506 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
507 sb_flags = le32_to_cpu(disk_super->flags);
508 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
509 dm_bm_unlock(sblock);
511 return 0;
513 bad:
514 dm_bm_unlock(sblock);
515 return r;
518 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
519 bool format_device)
521 int r;
522 bool unformatted = false;
524 r = __superblock_all_zeroes(cmd->bm, &unformatted);
525 if (r)
526 return r;
528 if (unformatted)
529 return format_device ? __format_metadata(cmd) : -EPERM;
531 return __open_metadata(cmd);
534 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
535 bool may_format_device)
537 int r;
539 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
540 CACHE_MAX_CONCURRENT_LOCKS);
541 if (IS_ERR(cmd->bm)) {
542 DMERR("could not create block manager");
543 r = PTR_ERR(cmd->bm);
544 cmd->bm = NULL;
545 return r;
548 r = __open_or_format_metadata(cmd, may_format_device);
549 if (r) {
550 dm_block_manager_destroy(cmd->bm);
551 cmd->bm = NULL;
554 return r;
557 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd,
558 bool destroy_bm)
560 dm_sm_destroy(cmd->metadata_sm);
561 dm_tm_destroy(cmd->tm);
562 if (destroy_bm)
563 dm_block_manager_destroy(cmd->bm);
566 typedef unsigned long (*flags_mutator)(unsigned long);
568 static void update_flags(struct cache_disk_superblock *disk_super,
569 flags_mutator mutator)
571 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
573 disk_super->flags = cpu_to_le32(sb_flags);
576 static unsigned long set_clean_shutdown(unsigned long flags)
578 set_bit(CLEAN_SHUTDOWN, &flags);
579 return flags;
582 static unsigned long clear_clean_shutdown(unsigned long flags)
584 clear_bit(CLEAN_SHUTDOWN, &flags);
585 return flags;
588 static void read_superblock_fields(struct dm_cache_metadata *cmd,
589 struct cache_disk_superblock *disk_super)
591 cmd->version = le32_to_cpu(disk_super->version);
592 cmd->flags = le32_to_cpu(disk_super->flags);
593 cmd->root = le64_to_cpu(disk_super->mapping_root);
594 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
595 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
596 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
597 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
598 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
599 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
600 strscpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
601 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
602 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
603 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
604 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
606 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
607 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
608 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
609 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
611 if (separate_dirty_bits(cmd))
612 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
614 cmd->changed = false;
618 * The mutator updates the superblock flags.
620 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
621 flags_mutator mutator)
623 int r;
624 struct cache_disk_superblock *disk_super;
625 struct dm_block *sblock;
627 r = superblock_lock(cmd, &sblock);
628 if (r)
629 return r;
631 disk_super = dm_block_data(sblock);
632 update_flags(disk_super, mutator);
633 read_superblock_fields(cmd, disk_super);
634 dm_bm_unlock(sblock);
636 return dm_bm_flush(cmd->bm);
639 static int __begin_transaction(struct dm_cache_metadata *cmd)
641 int r;
642 struct cache_disk_superblock *disk_super;
643 struct dm_block *sblock;
646 * We re-read the superblock every time. Shouldn't need to do this
647 * really.
649 r = superblock_read_lock(cmd, &sblock);
650 if (r)
651 return r;
653 disk_super = dm_block_data(sblock);
654 read_superblock_fields(cmd, disk_super);
655 dm_bm_unlock(sblock);
657 return 0;
660 static int __commit_transaction(struct dm_cache_metadata *cmd,
661 flags_mutator mutator)
663 int r;
664 struct cache_disk_superblock *disk_super;
665 struct dm_block *sblock;
668 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
670 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
672 if (separate_dirty_bits(cmd)) {
673 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
674 &cmd->dirty_root);
675 if (r)
676 return r;
679 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
680 &cmd->discard_root);
681 if (r)
682 return r;
684 r = dm_tm_pre_commit(cmd->tm);
685 if (r < 0)
686 return r;
688 r = __save_sm_root(cmd);
689 if (r)
690 return r;
692 r = superblock_lock(cmd, &sblock);
693 if (r)
694 return r;
696 disk_super = dm_block_data(sblock);
698 disk_super->flags = cpu_to_le32(cmd->flags);
699 if (mutator)
700 update_flags(disk_super, mutator);
702 disk_super->mapping_root = cpu_to_le64(cmd->root);
703 if (separate_dirty_bits(cmd))
704 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
705 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
706 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
707 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
708 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
709 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
710 strscpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
711 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
712 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
713 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
714 disk_super->policy_hint_size = cpu_to_le32(cmd->policy_hint_size);
716 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
717 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
718 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
719 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
720 __copy_sm_root(cmd, disk_super);
722 return dm_tm_commit(cmd->tm, sblock);
725 /*----------------------------------------------------------------*/
728 * The mappings are held in a dm-array that has 64-bit values stored in
729 * little-endian format. The index is the cblock, the high 48bits of the
730 * value are the oblock and the low 16 bit the flags.
732 #define FLAGS_MASK ((1 << 16) - 1)
734 static __le64 pack_value(dm_oblock_t block, unsigned int flags)
736 uint64_t value = from_oblock(block);
738 value <<= 16;
739 value = value | (flags & FLAGS_MASK);
740 return cpu_to_le64(value);
743 static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned int *flags)
745 uint64_t value = le64_to_cpu(value_le);
746 uint64_t b = value >> 16;
748 *block = to_oblock(b);
749 *flags = value & FLAGS_MASK;
752 /*----------------------------------------------------------------*/
754 static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
755 sector_t data_block_size,
756 bool may_format_device,
757 size_t policy_hint_size,
758 unsigned int metadata_version)
760 int r;
761 struct dm_cache_metadata *cmd;
763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
764 if (!cmd) {
765 DMERR("could not allocate metadata struct");
766 return ERR_PTR(-ENOMEM);
769 cmd->version = metadata_version;
770 refcount_set(&cmd->ref_count, 1);
771 init_rwsem(&cmd->root_lock);
772 cmd->bdev = bdev;
773 cmd->data_block_size = data_block_size;
774 cmd->cache_blocks = 0;
775 cmd->policy_hint_size = policy_hint_size;
776 cmd->changed = true;
777 cmd->fail_io = false;
779 r = __create_persistent_data_objects(cmd, may_format_device);
780 if (r) {
781 kfree(cmd);
782 return ERR_PTR(r);
785 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
786 if (r < 0) {
787 dm_cache_metadata_close(cmd);
788 return ERR_PTR(r);
791 return cmd;
795 * We keep a little list of ref counted metadata objects to prevent two
796 * different target instances creating separate bufio instances. This is
797 * an issue if a table is reloaded before the suspend.
799 static DEFINE_MUTEX(table_lock);
800 static LIST_HEAD(table);
802 static struct dm_cache_metadata *lookup(struct block_device *bdev)
804 struct dm_cache_metadata *cmd;
806 list_for_each_entry(cmd, &table, list)
807 if (cmd->bdev == bdev) {
808 refcount_inc(&cmd->ref_count);
809 return cmd;
812 return NULL;
815 static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
816 sector_t data_block_size,
817 bool may_format_device,
818 size_t policy_hint_size,
819 unsigned int metadata_version)
821 struct dm_cache_metadata *cmd, *cmd2;
823 mutex_lock(&table_lock);
824 cmd = lookup(bdev);
825 mutex_unlock(&table_lock);
827 if (cmd)
828 return cmd;
830 cmd = metadata_open(bdev, data_block_size, may_format_device,
831 policy_hint_size, metadata_version);
832 if (!IS_ERR(cmd)) {
833 mutex_lock(&table_lock);
834 cmd2 = lookup(bdev);
835 if (cmd2) {
836 mutex_unlock(&table_lock);
837 __destroy_persistent_data_objects(cmd, true);
838 kfree(cmd);
839 return cmd2;
841 list_add(&cmd->list, &table);
842 mutex_unlock(&table_lock);
845 return cmd;
848 static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
850 if (cmd->data_block_size != data_block_size) {
851 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
852 (unsigned long long) data_block_size,
853 (unsigned long long) cmd->data_block_size);
854 return false;
857 return true;
860 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
861 sector_t data_block_size,
862 bool may_format_device,
863 size_t policy_hint_size,
864 unsigned int metadata_version)
866 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
867 policy_hint_size, metadata_version);
869 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
870 dm_cache_metadata_close(cmd);
871 return ERR_PTR(-EINVAL);
874 return cmd;
877 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
879 if (refcount_dec_and_test(&cmd->ref_count)) {
880 mutex_lock(&table_lock);
881 list_del(&cmd->list);
882 mutex_unlock(&table_lock);
884 if (!cmd->fail_io)
885 __destroy_persistent_data_objects(cmd, true);
886 kfree(cmd);
891 * Checks that the given cache block is either unmapped or clean.
893 static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
894 bool *result)
896 int r;
897 __le64 value;
898 dm_oblock_t ob;
899 unsigned int flags;
901 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
902 if (r)
903 return r;
905 unpack_value(value, &ob, &flags);
906 *result = !((flags & M_VALID) && (flags & M_DIRTY));
908 return 0;
911 static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
912 dm_cblock_t begin, dm_cblock_t end,
913 bool *result)
915 int r;
916 *result = true;
918 while (begin != end) {
919 r = block_clean_combined_dirty(cmd, begin, result);
920 if (r) {
921 DMERR("block_clean_combined_dirty failed");
922 return r;
925 if (!*result) {
926 DMERR("cache block %llu is dirty",
927 (unsigned long long) from_cblock(begin));
928 return 0;
931 begin = to_cblock(from_cblock(begin) + 1);
934 return 0;
937 static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
938 dm_cblock_t begin, dm_cblock_t end,
939 bool *result)
941 int r;
942 bool dirty_flag;
943 *result = true;
945 if (from_cblock(cmd->cache_blocks) == 0)
946 /* Nothing to do */
947 return 0;
949 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
950 from_cblock(cmd->cache_blocks), &cmd->dirty_cursor);
951 if (r) {
952 DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
953 return r;
956 r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
957 if (r) {
958 DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
959 dm_bitset_cursor_end(&cmd->dirty_cursor);
960 return r;
963 while (begin != end) {
965 * We assume that unmapped blocks have their dirty bit
966 * cleared.
968 dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
969 if (dirty_flag) {
970 DMERR("%s: cache block %llu is dirty", __func__,
971 (unsigned long long) from_cblock(begin));
972 dm_bitset_cursor_end(&cmd->dirty_cursor);
973 *result = false;
974 return 0;
977 begin = to_cblock(from_cblock(begin) + 1);
978 if (begin == end)
979 break;
981 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
982 if (r) {
983 DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
984 dm_bitset_cursor_end(&cmd->dirty_cursor);
985 return r;
989 dm_bitset_cursor_end(&cmd->dirty_cursor);
991 return 0;
994 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
995 dm_cblock_t begin, dm_cblock_t end,
996 bool *result)
998 if (separate_dirty_bits(cmd))
999 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
1000 else
1001 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
1004 static bool cmd_write_lock(struct dm_cache_metadata *cmd)
1006 down_write(&cmd->root_lock);
1007 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
1008 up_write(&cmd->root_lock);
1009 return false;
1011 return true;
1014 #define WRITE_LOCK(cmd) \
1015 do { \
1016 if (!cmd_write_lock((cmd))) \
1017 return -EINVAL; \
1018 } while (0)
1020 #define WRITE_LOCK_VOID(cmd) \
1021 do { \
1022 if (!cmd_write_lock((cmd))) \
1023 return; \
1024 } while (0)
1026 #define WRITE_UNLOCK(cmd) \
1027 up_write(&(cmd)->root_lock)
1029 static bool cmd_read_lock(struct dm_cache_metadata *cmd)
1031 down_read(&cmd->root_lock);
1032 if (cmd->fail_io) {
1033 up_read(&cmd->root_lock);
1034 return false;
1036 return true;
1039 #define READ_LOCK(cmd) \
1040 do { \
1041 if (!cmd_read_lock((cmd))) \
1042 return -EINVAL; \
1043 } while (0)
1045 #define READ_LOCK_VOID(cmd) \
1046 do { \
1047 if (!cmd_read_lock((cmd))) \
1048 return; \
1049 } while (0)
1051 #define READ_UNLOCK(cmd) \
1052 up_read(&(cmd)->root_lock)
1054 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1056 int r;
1057 bool clean;
1058 __le64 null_mapping = pack_value(0, 0);
1060 WRITE_LOCK(cmd);
1061 __dm_bless_for_disk(&null_mapping);
1063 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1064 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1065 if (r) {
1066 __dm_unbless_for_disk(&null_mapping);
1067 goto out;
1070 if (!clean) {
1071 DMERR("unable to shrink cache due to dirty blocks");
1072 r = -EINVAL;
1073 __dm_unbless_for_disk(&null_mapping);
1074 goto out;
1078 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1079 from_cblock(new_cache_size),
1080 &null_mapping, &cmd->root);
1081 if (r)
1082 goto out;
1084 if (separate_dirty_bits(cmd)) {
1085 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1086 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1087 false, &cmd->dirty_root);
1088 if (r)
1089 goto out;
1092 cmd->cache_blocks = new_cache_size;
1093 cmd->changed = true;
1095 out:
1096 WRITE_UNLOCK(cmd);
1098 return r;
1101 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1102 sector_t discard_block_size,
1103 dm_dblock_t new_nr_entries)
1105 int r;
1107 WRITE_LOCK(cmd);
1108 r = dm_bitset_resize(&cmd->discard_info,
1109 cmd->discard_root,
1110 from_dblock(cmd->discard_nr_blocks),
1111 from_dblock(new_nr_entries),
1112 false, &cmd->discard_root);
1113 if (!r) {
1114 cmd->discard_block_size = discard_block_size;
1115 cmd->discard_nr_blocks = new_nr_entries;
1118 cmd->changed = true;
1119 WRITE_UNLOCK(cmd);
1121 return r;
1124 static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1126 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
1127 from_dblock(b), &cmd->discard_root);
1130 static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1132 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
1133 from_dblock(b), &cmd->discard_root);
1136 static int __discard(struct dm_cache_metadata *cmd,
1137 dm_dblock_t dblock, bool discard)
1139 int r;
1141 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1142 if (r)
1143 return r;
1145 cmd->changed = true;
1146 return 0;
1149 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
1150 dm_dblock_t dblock, bool discard)
1152 int r;
1154 WRITE_LOCK(cmd);
1155 r = __discard(cmd, dblock, discard);
1156 WRITE_UNLOCK(cmd);
1158 return r;
1161 static int __load_discards(struct dm_cache_metadata *cmd,
1162 load_discard_fn fn, void *context)
1164 int r = 0;
1165 uint32_t b;
1166 struct dm_bitset_cursor c;
1168 if (from_dblock(cmd->discard_nr_blocks) == 0)
1169 /* nothing to do */
1170 return 0;
1172 if (cmd->clean_when_opened) {
1173 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1174 if (r)
1175 return r;
1177 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1178 from_dblock(cmd->discard_nr_blocks), &c);
1179 if (r)
1180 return r;
1182 for (b = 0; ; b++) {
1183 r = fn(context, cmd->discard_block_size, to_dblock(b),
1184 dm_bitset_cursor_get_value(&c));
1185 if (r)
1186 break;
1188 if (b >= (from_dblock(cmd->discard_nr_blocks) - 1))
1189 break;
1191 r = dm_bitset_cursor_next(&c);
1192 if (r)
1193 break;
1196 dm_bitset_cursor_end(&c);
1198 } else {
1199 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1200 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
1201 if (r)
1202 return r;
1206 return r;
1209 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1210 load_discard_fn fn, void *context)
1212 int r;
1214 READ_LOCK(cmd);
1215 r = __load_discards(cmd, fn, context);
1216 READ_UNLOCK(cmd);
1218 return r;
1221 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1223 int r;
1224 __le64 value = pack_value(0, 0);
1226 __dm_bless_for_disk(&value);
1227 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1228 &value, &cmd->root);
1229 if (r)
1230 return r;
1232 cmd->changed = true;
1233 return 0;
1236 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1238 int r;
1240 WRITE_LOCK(cmd);
1241 r = __remove(cmd, cblock);
1242 WRITE_UNLOCK(cmd);
1244 return r;
1247 static int __insert(struct dm_cache_metadata *cmd,
1248 dm_cblock_t cblock, dm_oblock_t oblock)
1250 int r;
1251 __le64 value = pack_value(oblock, M_VALID);
1253 __dm_bless_for_disk(&value);
1255 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1256 &value, &cmd->root);
1257 if (r)
1258 return r;
1260 cmd->changed = true;
1261 return 0;
1264 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1265 dm_cblock_t cblock, dm_oblock_t oblock)
1267 int r;
1269 WRITE_LOCK(cmd);
1270 r = __insert(cmd, cblock, oblock);
1271 WRITE_UNLOCK(cmd);
1273 return r;
1276 static bool policy_unchanged(struct dm_cache_metadata *cmd,
1277 struct dm_cache_policy *policy)
1279 const char *policy_name = dm_cache_policy_get_name(policy);
1280 const unsigned int *policy_version = dm_cache_policy_get_version(policy);
1281 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1284 * Ensure policy names match.
1286 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1287 return false;
1290 * Ensure policy major versions match.
1292 if (cmd->policy_version[0] != policy_version[0])
1293 return false;
1296 * Ensure policy hint sizes match.
1298 if (cmd->policy_hint_size != policy_hint_size)
1299 return false;
1301 return true;
1304 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1306 return cmd->hint_root && cmd->policy_hint_size;
1309 static bool hints_array_available(struct dm_cache_metadata *cmd,
1310 struct dm_cache_policy *policy)
1312 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
1313 hints_array_initialized(cmd);
1316 static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1317 uint64_t cb, bool hints_valid,
1318 struct dm_array_cursor *mapping_cursor,
1319 struct dm_array_cursor *hint_cursor,
1320 load_mapping_fn fn, void *context)
1322 int r = 0;
1324 __le64 mapping;
1325 __le32 hint = 0;
1327 __le64 *mapping_value_le;
1328 __le32 *hint_value_le;
1330 dm_oblock_t oblock;
1331 unsigned int flags;
1332 bool dirty = true;
1334 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1335 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1336 unpack_value(mapping, &oblock, &flags);
1338 if (flags & M_VALID) {
1339 if (hints_valid) {
1340 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1341 memcpy(&hint, hint_value_le, sizeof(hint));
1343 if (cmd->clean_when_opened)
1344 dirty = flags & M_DIRTY;
1346 r = fn(context, oblock, to_cblock(cb), dirty,
1347 le32_to_cpu(hint), hints_valid);
1348 if (r) {
1349 DMERR("policy couldn't load cache block %llu",
1350 (unsigned long long) from_cblock(to_cblock(cb)));
1354 return r;
1357 static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1358 uint64_t cb, bool hints_valid,
1359 struct dm_array_cursor *mapping_cursor,
1360 struct dm_array_cursor *hint_cursor,
1361 struct dm_bitset_cursor *dirty_cursor,
1362 load_mapping_fn fn, void *context)
1364 int r = 0;
1366 __le64 mapping;
1367 __le32 hint = 0;
1369 __le64 *mapping_value_le;
1370 __le32 *hint_value_le;
1372 dm_oblock_t oblock;
1373 unsigned int flags;
1374 bool dirty = true;
1376 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1377 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1378 unpack_value(mapping, &oblock, &flags);
1380 if (flags & M_VALID) {
1381 if (hints_valid) {
1382 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1383 memcpy(&hint, hint_value_le, sizeof(hint));
1385 if (cmd->clean_when_opened)
1386 dirty = dm_bitset_cursor_get_value(dirty_cursor);
1388 r = fn(context, oblock, to_cblock(cb), dirty,
1389 le32_to_cpu(hint), hints_valid);
1390 if (r) {
1391 DMERR("policy couldn't load cache block %llu",
1392 (unsigned long long) from_cblock(to_cblock(cb)));
1396 return r;
1399 static int __load_mappings(struct dm_cache_metadata *cmd,
1400 struct dm_cache_policy *policy,
1401 load_mapping_fn fn, void *context)
1403 int r;
1404 uint64_t cb;
1406 bool hints_valid = hints_array_available(cmd, policy);
1408 if (from_cblock(cmd->cache_blocks) == 0)
1409 /* Nothing to do */
1410 return 0;
1412 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1413 if (r)
1414 return r;
1416 if (hints_valid) {
1417 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1418 if (r) {
1419 dm_array_cursor_end(&cmd->mapping_cursor);
1420 return r;
1424 if (separate_dirty_bits(cmd)) {
1425 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1426 from_cblock(cmd->cache_blocks),
1427 &cmd->dirty_cursor);
1428 if (r) {
1429 dm_array_cursor_end(&cmd->hint_cursor);
1430 dm_array_cursor_end(&cmd->mapping_cursor);
1431 return r;
1435 for (cb = 0; ; cb++) {
1436 if (separate_dirty_bits(cmd))
1437 r = __load_mapping_v2(cmd, cb, hints_valid,
1438 &cmd->mapping_cursor,
1439 &cmd->hint_cursor,
1440 &cmd->dirty_cursor,
1441 fn, context);
1442 else
1443 r = __load_mapping_v1(cmd, cb, hints_valid,
1444 &cmd->mapping_cursor, &cmd->hint_cursor,
1445 fn, context);
1446 if (r)
1447 goto out;
1450 * We need to break out before we move the cursors.
1452 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1453 break;
1455 r = dm_array_cursor_next(&cmd->mapping_cursor);
1456 if (r) {
1457 DMERR("dm_array_cursor_next for mapping failed");
1458 goto out;
1461 if (hints_valid) {
1462 r = dm_array_cursor_next(&cmd->hint_cursor);
1463 if (r) {
1464 dm_array_cursor_end(&cmd->hint_cursor);
1465 hints_valid = false;
1469 if (separate_dirty_bits(cmd)) {
1470 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1471 if (r) {
1472 DMERR("dm_bitset_cursor_next for dirty failed");
1473 goto out;
1477 out:
1478 dm_array_cursor_end(&cmd->mapping_cursor);
1479 if (hints_valid)
1480 dm_array_cursor_end(&cmd->hint_cursor);
1482 if (separate_dirty_bits(cmd))
1483 dm_bitset_cursor_end(&cmd->dirty_cursor);
1485 return r;
1488 int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1489 struct dm_cache_policy *policy,
1490 load_mapping_fn fn, void *context)
1492 int r;
1494 READ_LOCK(cmd);
1495 r = __load_mappings(cmd, policy, fn, context);
1496 READ_UNLOCK(cmd);
1498 return r;
1501 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1503 int r;
1505 READ_LOCK(cmd);
1506 r = cmd->changed;
1507 READ_UNLOCK(cmd);
1509 return r;
1512 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1514 int r;
1515 unsigned int flags;
1516 dm_oblock_t oblock;
1517 __le64 value;
1519 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1520 if (r)
1521 return r;
1523 unpack_value(value, &oblock, &flags);
1525 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1526 /* nothing to be done */
1527 return 0;
1529 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
1530 __dm_bless_for_disk(&value);
1532 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1533 &value, &cmd->root);
1534 if (r)
1535 return r;
1537 cmd->changed = true;
1538 return 0;
1542 static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned int nr_bits, unsigned long *bits)
1544 int r;
1545 unsigned int i;
1547 for (i = 0; i < nr_bits; i++) {
1548 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1549 if (r)
1550 return r;
1553 return 0;
1556 static int is_dirty_callback(uint32_t index, bool *value, void *context)
1558 unsigned long *bits = context;
1559 *value = test_bit(index, bits);
1560 return 0;
1563 static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned int nr_bits, unsigned long *bits)
1565 int r = 0;
1567 /* nr_bits is really just a sanity check */
1568 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1569 DMERR("dirty bitset is wrong size");
1570 return -EINVAL;
1573 r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
1574 if (r)
1575 return r;
1577 cmd->changed = true;
1578 return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
1581 int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1582 unsigned int nr_bits,
1583 unsigned long *bits)
1585 int r;
1587 WRITE_LOCK(cmd);
1588 if (separate_dirty_bits(cmd))
1589 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1590 else
1591 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
1592 WRITE_UNLOCK(cmd);
1594 return r;
1597 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1598 struct dm_cache_statistics *stats)
1600 READ_LOCK_VOID(cmd);
1601 *stats = cmd->stats;
1602 READ_UNLOCK(cmd);
1605 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1606 struct dm_cache_statistics *stats)
1608 WRITE_LOCK_VOID(cmd);
1609 cmd->stats = *stats;
1610 WRITE_UNLOCK(cmd);
1613 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1615 int r = -EINVAL;
1616 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1617 clear_clean_shutdown);
1619 WRITE_LOCK(cmd);
1620 if (cmd->fail_io)
1621 goto out;
1623 r = __commit_transaction(cmd, mutator);
1624 if (r)
1625 goto out;
1627 r = __begin_transaction(cmd);
1628 out:
1629 WRITE_UNLOCK(cmd);
1630 return r;
1633 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1634 dm_block_t *result)
1636 int r = -EINVAL;
1638 READ_LOCK(cmd);
1639 if (!cmd->fail_io)
1640 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1641 READ_UNLOCK(cmd);
1643 return r;
1646 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1647 dm_block_t *result)
1649 int r = -EINVAL;
1651 READ_LOCK(cmd);
1652 if (!cmd->fail_io)
1653 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1654 READ_UNLOCK(cmd);
1656 return r;
1659 /*----------------------------------------------------------------*/
1661 static int get_hint(uint32_t index, void *value_le, void *context)
1663 uint32_t value;
1664 struct dm_cache_policy *policy = context;
1666 value = policy_get_hint(policy, to_cblock(index));
1667 *((__le32 *) value_le) = cpu_to_le32(value);
1669 return 0;
1673 * It's quicker to always delete the hint array, and recreate with
1674 * dm_array_new().
1676 static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1678 int r;
1679 size_t hint_size;
1680 const char *policy_name = dm_cache_policy_get_name(policy);
1681 const unsigned int *policy_version = dm_cache_policy_get_version(policy);
1683 if (!policy_name[0] ||
1684 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1685 return -EINVAL;
1687 strscpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1688 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1690 hint_size = dm_cache_policy_get_hint_size(policy);
1691 if (!hint_size)
1692 return 0; /* short-circuit hints initialization */
1693 cmd->policy_hint_size = hint_size;
1695 if (cmd->hint_root) {
1696 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1697 if (r)
1698 return r;
1701 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1702 from_cblock(cmd->cache_blocks),
1703 get_hint, policy);
1706 int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1708 int r;
1710 WRITE_LOCK(cmd);
1711 r = write_hints(cmd, policy);
1712 WRITE_UNLOCK(cmd);
1714 return r;
1717 int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1719 int r;
1721 READ_LOCK(cmd);
1722 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1723 READ_UNLOCK(cmd);
1725 return r;
1728 void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1730 WRITE_LOCK_VOID(cmd);
1731 dm_bm_set_read_only(cmd->bm);
1732 WRITE_UNLOCK(cmd);
1735 void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1737 WRITE_LOCK_VOID(cmd);
1738 dm_bm_set_read_write(cmd->bm);
1739 WRITE_UNLOCK(cmd);
1742 int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1744 int r;
1745 struct dm_block *sblock;
1746 struct cache_disk_superblock *disk_super;
1748 WRITE_LOCK(cmd);
1749 set_bit(NEEDS_CHECK, &cmd->flags);
1751 r = superblock_lock(cmd, &sblock);
1752 if (r) {
1753 DMERR("couldn't read superblock");
1754 goto out;
1757 disk_super = dm_block_data(sblock);
1758 disk_super->flags = cpu_to_le32(cmd->flags);
1760 dm_bm_unlock(sblock);
1762 out:
1763 WRITE_UNLOCK(cmd);
1764 return r;
1767 int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
1769 READ_LOCK(cmd);
1770 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1771 READ_UNLOCK(cmd);
1773 return 0;
1776 int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1778 int r = -EINVAL;
1779 struct dm_block_manager *old_bm = NULL, *new_bm = NULL;
1781 /* fail_io is double-checked with cmd->root_lock held below */
1782 if (unlikely(cmd->fail_io))
1783 return r;
1786 * Replacement block manager (new_bm) is created and old_bm destroyed outside of
1787 * cmd root_lock to avoid ABBA deadlock that would result (due to life-cycle of
1788 * shrinker associated with the block manager's bufio client vs cmd root_lock).
1789 * - must take shrinker_mutex without holding cmd->root_lock
1791 new_bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
1792 CACHE_MAX_CONCURRENT_LOCKS);
1794 WRITE_LOCK(cmd);
1795 if (cmd->fail_io) {
1796 WRITE_UNLOCK(cmd);
1797 goto out;
1800 __destroy_persistent_data_objects(cmd, false);
1801 old_bm = cmd->bm;
1802 if (IS_ERR(new_bm)) {
1803 DMERR("could not create block manager during abort");
1804 cmd->bm = NULL;
1805 r = PTR_ERR(new_bm);
1806 goto out_unlock;
1809 cmd->bm = new_bm;
1810 r = __open_or_format_metadata(cmd, false);
1811 if (r) {
1812 cmd->bm = NULL;
1813 goto out_unlock;
1815 new_bm = NULL;
1816 out_unlock:
1817 if (r)
1818 cmd->fail_io = true;
1819 WRITE_UNLOCK(cmd);
1820 dm_block_manager_destroy(old_bm);
1821 out:
1822 if (new_bm && !IS_ERR(new_bm))
1823 dm_block_manager_destroy(new_bm);
1825 return r;