doc: Update rcu_assign_pointer() definition in whatisRCU.txt
[linux/fpc-iii.git] / drivers / md / dm-cache-metadata.c
blobe4c2c1a1e9933282fa2b971999270199c8d9c224
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm-cache-metadata.h"
9 #include "persistent-data/dm-array.h"
10 #include "persistent-data/dm-bitset.h"
11 #include "persistent-data/dm-space-map.h"
12 #include "persistent-data/dm-space-map-disk.h"
13 #include "persistent-data/dm-transaction-manager.h"
15 #include <linux/device-mapper.h>
17 /*----------------------------------------------------------------*/
19 #define DM_MSG_PREFIX "cache metadata"
21 #define CACHE_SUPERBLOCK_MAGIC 06142003
22 #define CACHE_SUPERBLOCK_LOCATION 0
25 * defines a range of metadata versions that this module can handle.
27 #define MIN_CACHE_VERSION 1
28 #define MAX_CACHE_VERSION 2
30 #define CACHE_METADATA_CACHE_SIZE 64
33 * 3 for btree insert +
34 * 2 for btree lookup used within space map
36 #define CACHE_MAX_CONCURRENT_LOCKS 5
37 #define SPACE_MAP_ROOT_SIZE 128
39 enum superblock_flag_bits {
40 /* for spotting crashes that would invalidate the dirty bitset */
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 atomic_t ref_count;
106 struct list_head list;
108 unsigned 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 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;
166 /*-------------------------------------------------------------------
167 * superblock validator
168 *-----------------------------------------------------------------*/
170 #define SUPERBLOCK_CSUM_XOR 9031977
172 static void sb_prepare_for_write(struct dm_block_validator *v,
173 struct dm_block *b,
174 size_t sb_block_size)
176 struct cache_disk_superblock *disk_super = dm_block_data(b);
178 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
179 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
180 sb_block_size - sizeof(__le32),
181 SUPERBLOCK_CSUM_XOR));
184 static int check_metadata_version(struct cache_disk_superblock *disk_super)
186 uint32_t metadata_version = le32_to_cpu(disk_super->version);
188 if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) {
189 DMERR("Cache metadata version %u found, but only versions between %u and %u supported.",
190 metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION);
191 return -EINVAL;
194 return 0;
197 static int sb_check(struct dm_block_validator *v,
198 struct dm_block *b,
199 size_t sb_block_size)
201 struct cache_disk_superblock *disk_super = dm_block_data(b);
202 __le32 csum_le;
204 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
205 DMERR("sb_check failed: blocknr %llu: wanted %llu",
206 le64_to_cpu(disk_super->blocknr),
207 (unsigned long long)dm_block_location(b));
208 return -ENOTBLK;
211 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
212 DMERR("sb_check failed: magic %llu: wanted %llu",
213 le64_to_cpu(disk_super->magic),
214 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
215 return -EILSEQ;
218 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
219 sb_block_size - sizeof(__le32),
220 SUPERBLOCK_CSUM_XOR));
221 if (csum_le != disk_super->csum) {
222 DMERR("sb_check failed: csum %u: wanted %u",
223 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
224 return -EILSEQ;
227 return check_metadata_version(disk_super);
230 static struct dm_block_validator sb_validator = {
231 .name = "superblock",
232 .prepare_for_write = sb_prepare_for_write,
233 .check = sb_check
236 /*----------------------------------------------------------------*/
238 static int superblock_read_lock(struct dm_cache_metadata *cmd,
239 struct dm_block **sblock)
241 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
242 &sb_validator, sblock);
245 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
246 struct dm_block **sblock)
248 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
249 &sb_validator, sblock);
252 static int superblock_lock(struct dm_cache_metadata *cmd,
253 struct dm_block **sblock)
255 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
256 &sb_validator, sblock);
259 /*----------------------------------------------------------------*/
261 static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
263 int r;
264 unsigned i;
265 struct dm_block *b;
266 __le64 *data_le, zero = cpu_to_le64(0);
267 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
270 * We can't use a validator here - it may be all zeroes.
272 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
273 if (r)
274 return r;
276 data_le = dm_block_data(b);
277 *result = true;
278 for (i = 0; i < sb_block_size; i++) {
279 if (data_le[i] != zero) {
280 *result = false;
281 break;
285 dm_bm_unlock(b);
287 return 0;
290 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
292 struct dm_btree_value_type vt;
294 vt.context = NULL;
295 vt.size = sizeof(__le64);
296 vt.inc = NULL;
297 vt.dec = NULL;
298 vt.equal = NULL;
299 dm_array_info_init(&cmd->info, cmd->tm, &vt);
301 if (cmd->policy_hint_size) {
302 vt.size = sizeof(__le32);
303 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
307 static int __save_sm_root(struct dm_cache_metadata *cmd)
309 int r;
310 size_t metadata_len;
312 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
313 if (r < 0)
314 return r;
316 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
317 metadata_len);
320 static void __copy_sm_root(struct dm_cache_metadata *cmd,
321 struct cache_disk_superblock *disk_super)
323 memcpy(&disk_super->metadata_space_map_root,
324 &cmd->metadata_space_map_root,
325 sizeof(cmd->metadata_space_map_root));
328 static bool separate_dirty_bits(struct dm_cache_metadata *cmd)
330 return cmd->version >= 2;
333 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
335 int r;
336 struct dm_block *sblock;
337 struct cache_disk_superblock *disk_super;
338 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
340 /* FIXME: see if we can lose the max sectors limit */
341 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
342 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
344 r = dm_tm_pre_commit(cmd->tm);
345 if (r < 0)
346 return r;
349 * dm_sm_copy_root() can fail. So we need to do it before we start
350 * updating the superblock.
352 r = __save_sm_root(cmd);
353 if (r)
354 return r;
356 r = superblock_lock_zero(cmd, &sblock);
357 if (r)
358 return r;
360 disk_super = dm_block_data(sblock);
361 disk_super->flags = 0;
362 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
363 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
364 disk_super->version = cpu_to_le32(cmd->version);
365 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
366 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
367 disk_super->policy_hint_size = 0;
369 __copy_sm_root(cmd, disk_super);
371 disk_super->mapping_root = cpu_to_le64(cmd->root);
372 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
373 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
374 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
375 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
376 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE);
377 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
378 disk_super->cache_blocks = cpu_to_le32(0);
380 disk_super->read_hits = cpu_to_le32(0);
381 disk_super->read_misses = cpu_to_le32(0);
382 disk_super->write_hits = cpu_to_le32(0);
383 disk_super->write_misses = cpu_to_le32(0);
385 if (separate_dirty_bits(cmd))
386 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
388 return dm_tm_commit(cmd->tm, sblock);
391 static int __format_metadata(struct dm_cache_metadata *cmd)
393 int r;
395 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
396 &cmd->tm, &cmd->metadata_sm);
397 if (r < 0) {
398 DMERR("tm_create_with_sm failed");
399 return r;
402 __setup_mapping_info(cmd);
404 r = dm_array_empty(&cmd->info, &cmd->root);
405 if (r < 0)
406 goto bad;
408 if (separate_dirty_bits(cmd)) {
409 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
410 r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root);
411 if (r < 0)
412 goto bad;
415 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
416 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
417 if (r < 0)
418 goto bad;
420 cmd->discard_block_size = 0;
421 cmd->discard_nr_blocks = 0;
423 r = __write_initial_superblock(cmd);
424 if (r)
425 goto bad;
427 cmd->clean_when_opened = true;
428 return 0;
430 bad:
431 dm_tm_destroy(cmd->tm);
432 dm_sm_destroy(cmd->metadata_sm);
434 return r;
437 static int __check_incompat_features(struct cache_disk_superblock *disk_super,
438 struct dm_cache_metadata *cmd)
440 uint32_t incompat_flags, features;
442 incompat_flags = le32_to_cpu(disk_super->incompat_flags);
443 features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
444 if (features) {
445 DMERR("could not access metadata due to unsupported optional features (%lx).",
446 (unsigned long)features);
447 return -EINVAL;
451 * Check for read-only metadata to skip the following RDWR checks.
453 if (get_disk_ro(cmd->bdev->bd_disk))
454 return 0;
456 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
457 if (features) {
458 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
459 (unsigned long)features);
460 return -EINVAL;
463 return 0;
466 static int __open_metadata(struct dm_cache_metadata *cmd)
468 int r;
469 struct dm_block *sblock;
470 struct cache_disk_superblock *disk_super;
471 unsigned long sb_flags;
473 r = superblock_read_lock(cmd, &sblock);
474 if (r < 0) {
475 DMERR("couldn't read lock superblock");
476 return r;
479 disk_super = dm_block_data(sblock);
481 /* Verify the data block size hasn't changed */
482 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
483 DMERR("changing the data block size (from %u to %llu) is not supported",
484 le32_to_cpu(disk_super->data_block_size),
485 (unsigned long long)cmd->data_block_size);
486 r = -EINVAL;
487 goto bad;
490 r = __check_incompat_features(disk_super, cmd);
491 if (r < 0)
492 goto bad;
494 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
495 disk_super->metadata_space_map_root,
496 sizeof(disk_super->metadata_space_map_root),
497 &cmd->tm, &cmd->metadata_sm);
498 if (r < 0) {
499 DMERR("tm_open_with_sm failed");
500 goto bad;
503 __setup_mapping_info(cmd);
504 dm_disk_bitset_init(cmd->tm, &cmd->dirty_info);
505 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
506 sb_flags = le32_to_cpu(disk_super->flags);
507 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
508 dm_bm_unlock(sblock);
510 return 0;
512 bad:
513 dm_bm_unlock(sblock);
514 return r;
517 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
518 bool format_device)
520 int r;
521 bool unformatted = false;
523 r = __superblock_all_zeroes(cmd->bm, &unformatted);
524 if (r)
525 return r;
527 if (unformatted)
528 return format_device ? __format_metadata(cmd) : -EPERM;
530 return __open_metadata(cmd);
533 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
534 bool may_format_device)
536 int r;
537 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
538 CACHE_METADATA_CACHE_SIZE,
539 CACHE_MAX_CONCURRENT_LOCKS);
540 if (IS_ERR(cmd->bm)) {
541 DMERR("could not create block manager");
542 return PTR_ERR(cmd->bm);
545 r = __open_or_format_metadata(cmd, may_format_device);
546 if (r)
547 dm_block_manager_destroy(cmd->bm);
549 return r;
552 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
554 dm_sm_destroy(cmd->metadata_sm);
555 dm_tm_destroy(cmd->tm);
556 dm_block_manager_destroy(cmd->bm);
559 typedef unsigned long (*flags_mutator)(unsigned long);
561 static void update_flags(struct cache_disk_superblock *disk_super,
562 flags_mutator mutator)
564 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
565 disk_super->flags = cpu_to_le32(sb_flags);
568 static unsigned long set_clean_shutdown(unsigned long flags)
570 set_bit(CLEAN_SHUTDOWN, &flags);
571 return flags;
574 static unsigned long clear_clean_shutdown(unsigned long flags)
576 clear_bit(CLEAN_SHUTDOWN, &flags);
577 return flags;
580 static void read_superblock_fields(struct dm_cache_metadata *cmd,
581 struct cache_disk_superblock *disk_super)
583 cmd->version = le32_to_cpu(disk_super->version);
584 cmd->flags = le32_to_cpu(disk_super->flags);
585 cmd->root = le64_to_cpu(disk_super->mapping_root);
586 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
587 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
588 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
589 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
590 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
591 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
592 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
593 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
594 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
595 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
596 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
598 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
599 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
600 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
601 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
603 if (separate_dirty_bits(cmd))
604 cmd->dirty_root = le64_to_cpu(disk_super->dirty_root);
606 cmd->changed = false;
610 * The mutator updates the superblock flags.
612 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
613 flags_mutator mutator)
615 int r;
616 struct cache_disk_superblock *disk_super;
617 struct dm_block *sblock;
619 r = superblock_lock(cmd, &sblock);
620 if (r)
621 return r;
623 disk_super = dm_block_data(sblock);
624 update_flags(disk_super, mutator);
625 read_superblock_fields(cmd, disk_super);
626 dm_bm_unlock(sblock);
628 return dm_bm_flush(cmd->bm);
631 static int __begin_transaction(struct dm_cache_metadata *cmd)
633 int r;
634 struct cache_disk_superblock *disk_super;
635 struct dm_block *sblock;
638 * We re-read the superblock every time. Shouldn't need to do this
639 * really.
641 r = superblock_read_lock(cmd, &sblock);
642 if (r)
643 return r;
645 disk_super = dm_block_data(sblock);
646 read_superblock_fields(cmd, disk_super);
647 dm_bm_unlock(sblock);
649 return 0;
652 static int __commit_transaction(struct dm_cache_metadata *cmd,
653 flags_mutator mutator)
655 int r;
656 struct cache_disk_superblock *disk_super;
657 struct dm_block *sblock;
660 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
662 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
664 if (separate_dirty_bits(cmd)) {
665 r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root,
666 &cmd->dirty_root);
667 if (r)
668 return r;
671 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
672 &cmd->discard_root);
673 if (r)
674 return r;
676 r = dm_tm_pre_commit(cmd->tm);
677 if (r < 0)
678 return r;
680 r = __save_sm_root(cmd);
681 if (r)
682 return r;
684 r = superblock_lock(cmd, &sblock);
685 if (r)
686 return r;
688 disk_super = dm_block_data(sblock);
690 disk_super->flags = cpu_to_le32(cmd->flags);
691 if (mutator)
692 update_flags(disk_super, mutator);
694 disk_super->mapping_root = cpu_to_le64(cmd->root);
695 if (separate_dirty_bits(cmd))
696 disk_super->dirty_root = cpu_to_le64(cmd->dirty_root);
697 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
698 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
699 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
700 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
701 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
702 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
703 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
704 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
705 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
707 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
708 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
709 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
710 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
711 __copy_sm_root(cmd, disk_super);
713 return dm_tm_commit(cmd->tm, sblock);
716 /*----------------------------------------------------------------*/
719 * The mappings are held in a dm-array that has 64-bit values stored in
720 * little-endian format. The index is the cblock, the high 48bits of the
721 * value are the oblock and the low 16 bit the flags.
723 #define FLAGS_MASK ((1 << 16) - 1)
725 static __le64 pack_value(dm_oblock_t block, unsigned flags)
727 uint64_t value = from_oblock(block);
728 value <<= 16;
729 value = value | (flags & FLAGS_MASK);
730 return cpu_to_le64(value);
733 static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
735 uint64_t value = le64_to_cpu(value_le);
736 uint64_t b = value >> 16;
737 *block = to_oblock(b);
738 *flags = value & FLAGS_MASK;
741 /*----------------------------------------------------------------*/
743 static struct dm_cache_metadata *metadata_open(struct block_device *bdev,
744 sector_t data_block_size,
745 bool may_format_device,
746 size_t policy_hint_size,
747 unsigned metadata_version)
749 int r;
750 struct dm_cache_metadata *cmd;
752 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
753 if (!cmd) {
754 DMERR("could not allocate metadata struct");
755 return ERR_PTR(-ENOMEM);
758 cmd->version = metadata_version;
759 atomic_set(&cmd->ref_count, 1);
760 init_rwsem(&cmd->root_lock);
761 cmd->bdev = bdev;
762 cmd->data_block_size = data_block_size;
763 cmd->cache_blocks = 0;
764 cmd->policy_hint_size = policy_hint_size;
765 cmd->changed = true;
766 cmd->fail_io = false;
768 r = __create_persistent_data_objects(cmd, may_format_device);
769 if (r) {
770 kfree(cmd);
771 return ERR_PTR(r);
774 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
775 if (r < 0) {
776 dm_cache_metadata_close(cmd);
777 return ERR_PTR(r);
780 return cmd;
784 * We keep a little list of ref counted metadata objects to prevent two
785 * different target instances creating separate bufio instances. This is
786 * an issue if a table is reloaded before the suspend.
788 static DEFINE_MUTEX(table_lock);
789 static LIST_HEAD(table);
791 static struct dm_cache_metadata *lookup(struct block_device *bdev)
793 struct dm_cache_metadata *cmd;
795 list_for_each_entry(cmd, &table, list)
796 if (cmd->bdev == bdev) {
797 atomic_inc(&cmd->ref_count);
798 return cmd;
801 return NULL;
804 static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev,
805 sector_t data_block_size,
806 bool may_format_device,
807 size_t policy_hint_size,
808 unsigned metadata_version)
810 struct dm_cache_metadata *cmd, *cmd2;
812 mutex_lock(&table_lock);
813 cmd = lookup(bdev);
814 mutex_unlock(&table_lock);
816 if (cmd)
817 return cmd;
819 cmd = metadata_open(bdev, data_block_size, may_format_device,
820 policy_hint_size, metadata_version);
821 if (!IS_ERR(cmd)) {
822 mutex_lock(&table_lock);
823 cmd2 = lookup(bdev);
824 if (cmd2) {
825 mutex_unlock(&table_lock);
826 __destroy_persistent_data_objects(cmd);
827 kfree(cmd);
828 return cmd2;
830 list_add(&cmd->list, &table);
831 mutex_unlock(&table_lock);
834 return cmd;
837 static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size)
839 if (cmd->data_block_size != data_block_size) {
840 DMERR("data_block_size (%llu) different from that in metadata (%llu)",
841 (unsigned long long) data_block_size,
842 (unsigned long long) cmd->data_block_size);
843 return false;
846 return true;
849 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
850 sector_t data_block_size,
851 bool may_format_device,
852 size_t policy_hint_size,
853 unsigned metadata_version)
855 struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device,
856 policy_hint_size, metadata_version);
858 if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) {
859 dm_cache_metadata_close(cmd);
860 return ERR_PTR(-EINVAL);
863 return cmd;
866 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
868 if (atomic_dec_and_test(&cmd->ref_count)) {
869 mutex_lock(&table_lock);
870 list_del(&cmd->list);
871 mutex_unlock(&table_lock);
873 if (!cmd->fail_io)
874 __destroy_persistent_data_objects(cmd);
875 kfree(cmd);
880 * Checks that the given cache block is either unmapped or clean.
882 static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b,
883 bool *result)
885 int r;
886 __le64 value;
887 dm_oblock_t ob;
888 unsigned flags;
890 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value);
891 if (r)
892 return r;
894 unpack_value(value, &ob, &flags);
895 *result = !((flags & M_VALID) && (flags & M_DIRTY));
897 return 0;
900 static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd,
901 dm_cblock_t begin, dm_cblock_t end,
902 bool *result)
904 int r;
905 *result = true;
907 while (begin != end) {
908 r = block_clean_combined_dirty(cmd, begin, result);
909 if (r) {
910 DMERR("block_clean_combined_dirty failed");
911 return r;
914 if (!*result) {
915 DMERR("cache block %llu is dirty",
916 (unsigned long long) from_cblock(begin));
917 return 0;
920 begin = to_cblock(from_cblock(begin) + 1);
923 return 0;
926 static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd,
927 dm_cblock_t begin, dm_cblock_t end,
928 bool *result)
930 int r;
931 bool dirty_flag;
932 *result = true;
934 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
935 from_cblock(begin), &cmd->dirty_cursor);
936 if (r) {
937 DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__);
938 return r;
941 r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin));
942 if (r) {
943 DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__);
944 dm_bitset_cursor_end(&cmd->dirty_cursor);
945 return r;
948 while (begin != end) {
950 * We assume that unmapped blocks have their dirty bit
951 * cleared.
953 dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor);
954 if (dirty_flag) {
955 DMERR("%s: cache block %llu is dirty", __func__,
956 (unsigned long long) from_cblock(begin));
957 dm_bitset_cursor_end(&cmd->dirty_cursor);
958 *result = false;
959 return 0;
962 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
963 if (r) {
964 DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__);
965 dm_bitset_cursor_end(&cmd->dirty_cursor);
966 return r;
969 begin = to_cblock(from_cblock(begin) + 1);
972 dm_bitset_cursor_end(&cmd->dirty_cursor);
974 return 0;
977 static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd,
978 dm_cblock_t begin, dm_cblock_t end,
979 bool *result)
981 if (separate_dirty_bits(cmd))
982 return blocks_are_clean_separate_dirty(cmd, begin, end, result);
983 else
984 return blocks_are_clean_combined_dirty(cmd, begin, end, result);
987 static bool cmd_write_lock(struct dm_cache_metadata *cmd)
989 down_write(&cmd->root_lock);
990 if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) {
991 up_write(&cmd->root_lock);
992 return false;
994 return true;
997 #define WRITE_LOCK(cmd) \
998 do { \
999 if (!cmd_write_lock((cmd))) \
1000 return -EINVAL; \
1001 } while(0)
1003 #define WRITE_LOCK_VOID(cmd) \
1004 do { \
1005 if (!cmd_write_lock((cmd))) \
1006 return; \
1007 } while(0)
1009 #define WRITE_UNLOCK(cmd) \
1010 up_write(&(cmd)->root_lock)
1012 static bool cmd_read_lock(struct dm_cache_metadata *cmd)
1014 down_read(&cmd->root_lock);
1015 if (cmd->fail_io) {
1016 up_read(&cmd->root_lock);
1017 return false;
1019 return true;
1022 #define READ_LOCK(cmd) \
1023 do { \
1024 if (!cmd_read_lock((cmd))) \
1025 return -EINVAL; \
1026 } while(0)
1028 #define READ_LOCK_VOID(cmd) \
1029 do { \
1030 if (!cmd_read_lock((cmd))) \
1031 return; \
1032 } while(0)
1034 #define READ_UNLOCK(cmd) \
1035 up_read(&(cmd)->root_lock)
1037 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
1039 int r;
1040 bool clean;
1041 __le64 null_mapping = pack_value(0, 0);
1043 WRITE_LOCK(cmd);
1044 __dm_bless_for_disk(&null_mapping);
1046 if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) {
1047 r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean);
1048 if (r) {
1049 __dm_unbless_for_disk(&null_mapping);
1050 goto out;
1053 if (!clean) {
1054 DMERR("unable to shrink cache due to dirty blocks");
1055 r = -EINVAL;
1056 __dm_unbless_for_disk(&null_mapping);
1057 goto out;
1061 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
1062 from_cblock(new_cache_size),
1063 &null_mapping, &cmd->root);
1064 if (r)
1065 goto out;
1067 if (separate_dirty_bits(cmd)) {
1068 r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root,
1069 from_cblock(cmd->cache_blocks), from_cblock(new_cache_size),
1070 false, &cmd->dirty_root);
1071 if (r)
1072 goto out;
1075 cmd->cache_blocks = new_cache_size;
1076 cmd->changed = true;
1078 out:
1079 WRITE_UNLOCK(cmd);
1081 return r;
1084 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
1085 sector_t discard_block_size,
1086 dm_dblock_t new_nr_entries)
1088 int r;
1090 WRITE_LOCK(cmd);
1091 r = dm_bitset_resize(&cmd->discard_info,
1092 cmd->discard_root,
1093 from_dblock(cmd->discard_nr_blocks),
1094 from_dblock(new_nr_entries),
1095 false, &cmd->discard_root);
1096 if (!r) {
1097 cmd->discard_block_size = discard_block_size;
1098 cmd->discard_nr_blocks = new_nr_entries;
1101 cmd->changed = true;
1102 WRITE_UNLOCK(cmd);
1104 return r;
1107 static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1109 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
1110 from_dblock(b), &cmd->discard_root);
1113 static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
1115 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
1116 from_dblock(b), &cmd->discard_root);
1119 static int __discard(struct dm_cache_metadata *cmd,
1120 dm_dblock_t dblock, bool discard)
1122 int r;
1124 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
1125 if (r)
1126 return r;
1128 cmd->changed = true;
1129 return 0;
1132 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
1133 dm_dblock_t dblock, bool discard)
1135 int r;
1137 WRITE_LOCK(cmd);
1138 r = __discard(cmd, dblock, discard);
1139 WRITE_UNLOCK(cmd);
1141 return r;
1144 static int __load_discards(struct dm_cache_metadata *cmd,
1145 load_discard_fn fn, void *context)
1147 int r = 0;
1148 uint32_t b;
1149 struct dm_bitset_cursor c;
1151 if (from_dblock(cmd->discard_nr_blocks) == 0)
1152 /* nothing to do */
1153 return 0;
1155 if (cmd->clean_when_opened) {
1156 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root);
1157 if (r)
1158 return r;
1160 r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root,
1161 from_dblock(cmd->discard_nr_blocks), &c);
1162 if (r)
1163 return r;
1165 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1166 r = fn(context, cmd->discard_block_size, to_dblock(b),
1167 dm_bitset_cursor_get_value(&c));
1168 if (r)
1169 break;
1172 dm_bitset_cursor_end(&c);
1174 } else {
1175 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
1176 r = fn(context, cmd->discard_block_size, to_dblock(b), false);
1177 if (r)
1178 return r;
1182 return r;
1185 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
1186 load_discard_fn fn, void *context)
1188 int r;
1190 READ_LOCK(cmd);
1191 r = __load_discards(cmd, fn, context);
1192 READ_UNLOCK(cmd);
1194 return r;
1197 int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result)
1199 READ_LOCK(cmd);
1200 *result = cmd->cache_blocks;
1201 READ_UNLOCK(cmd);
1203 return 0;
1206 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1208 int r;
1209 __le64 value = pack_value(0, 0);
1211 __dm_bless_for_disk(&value);
1212 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1213 &value, &cmd->root);
1214 if (r)
1215 return r;
1217 cmd->changed = true;
1218 return 0;
1221 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
1223 int r;
1225 WRITE_LOCK(cmd);
1226 r = __remove(cmd, cblock);
1227 WRITE_UNLOCK(cmd);
1229 return r;
1232 static int __insert(struct dm_cache_metadata *cmd,
1233 dm_cblock_t cblock, dm_oblock_t oblock)
1235 int r;
1236 __le64 value = pack_value(oblock, M_VALID);
1237 __dm_bless_for_disk(&value);
1239 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1240 &value, &cmd->root);
1241 if (r)
1242 return r;
1244 cmd->changed = true;
1245 return 0;
1248 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
1249 dm_cblock_t cblock, dm_oblock_t oblock)
1251 int r;
1253 WRITE_LOCK(cmd);
1254 r = __insert(cmd, cblock, oblock);
1255 WRITE_UNLOCK(cmd);
1257 return r;
1260 struct thunk {
1261 load_mapping_fn fn;
1262 void *context;
1264 struct dm_cache_metadata *cmd;
1265 bool respect_dirty_flags;
1266 bool hints_valid;
1269 static bool policy_unchanged(struct dm_cache_metadata *cmd,
1270 struct dm_cache_policy *policy)
1272 const char *policy_name = dm_cache_policy_get_name(policy);
1273 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1274 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
1277 * Ensure policy names match.
1279 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
1280 return false;
1283 * Ensure policy major versions match.
1285 if (cmd->policy_version[0] != policy_version[0])
1286 return false;
1289 * Ensure policy hint sizes match.
1291 if (cmd->policy_hint_size != policy_hint_size)
1292 return false;
1294 return true;
1297 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
1299 return cmd->hint_root && cmd->policy_hint_size;
1302 static bool hints_array_available(struct dm_cache_metadata *cmd,
1303 struct dm_cache_policy *policy)
1305 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
1306 hints_array_initialized(cmd);
1309 static int __load_mapping_v1(struct dm_cache_metadata *cmd,
1310 uint64_t cb, bool hints_valid,
1311 struct dm_array_cursor *mapping_cursor,
1312 struct dm_array_cursor *hint_cursor,
1313 load_mapping_fn fn, void *context)
1315 int r = 0;
1317 __le64 mapping;
1318 __le32 hint = 0;
1320 __le64 *mapping_value_le;
1321 __le32 *hint_value_le;
1323 dm_oblock_t oblock;
1324 unsigned flags;
1326 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1327 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1328 unpack_value(mapping, &oblock, &flags);
1330 if (flags & M_VALID) {
1331 if (hints_valid) {
1332 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1333 memcpy(&hint, hint_value_le, sizeof(hint));
1336 r = fn(context, oblock, to_cblock(cb), flags & M_DIRTY,
1337 le32_to_cpu(hint), hints_valid);
1338 if (r) {
1339 DMERR("policy couldn't load cache block %llu",
1340 (unsigned long long) from_cblock(to_cblock(cb)));
1344 return r;
1347 static int __load_mapping_v2(struct dm_cache_metadata *cmd,
1348 uint64_t cb, bool hints_valid,
1349 struct dm_array_cursor *mapping_cursor,
1350 struct dm_array_cursor *hint_cursor,
1351 struct dm_bitset_cursor *dirty_cursor,
1352 load_mapping_fn fn, void *context)
1354 int r = 0;
1356 __le64 mapping;
1357 __le32 hint = 0;
1359 __le64 *mapping_value_le;
1360 __le32 *hint_value_le;
1362 dm_oblock_t oblock;
1363 unsigned flags;
1364 bool dirty;
1366 dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le);
1367 memcpy(&mapping, mapping_value_le, sizeof(mapping));
1368 unpack_value(mapping, &oblock, &flags);
1370 if (flags & M_VALID) {
1371 if (hints_valid) {
1372 dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le);
1373 memcpy(&hint, hint_value_le, sizeof(hint));
1376 dirty = dm_bitset_cursor_get_value(dirty_cursor);
1377 r = fn(context, oblock, to_cblock(cb), dirty,
1378 le32_to_cpu(hint), hints_valid);
1379 if (r) {
1380 DMERR("policy couldn't load cache block %llu",
1381 (unsigned long long) from_cblock(to_cblock(cb)));
1385 return r;
1388 static int __load_mappings(struct dm_cache_metadata *cmd,
1389 struct dm_cache_policy *policy,
1390 load_mapping_fn fn, void *context)
1392 int r;
1393 uint64_t cb;
1395 bool hints_valid = hints_array_available(cmd, policy);
1397 if (from_cblock(cmd->cache_blocks) == 0)
1398 /* Nothing to do */
1399 return 0;
1401 r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor);
1402 if (r)
1403 return r;
1405 if (hints_valid) {
1406 r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor);
1407 if (r) {
1408 dm_array_cursor_end(&cmd->mapping_cursor);
1409 return r;
1413 if (separate_dirty_bits(cmd)) {
1414 r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root,
1415 from_cblock(cmd->cache_blocks),
1416 &cmd->dirty_cursor);
1417 if (r) {
1418 dm_array_cursor_end(&cmd->hint_cursor);
1419 dm_array_cursor_end(&cmd->mapping_cursor);
1420 return r;
1424 for (cb = 0; ; cb++) {
1425 if (separate_dirty_bits(cmd))
1426 r = __load_mapping_v2(cmd, cb, hints_valid,
1427 &cmd->mapping_cursor,
1428 &cmd->hint_cursor,
1429 &cmd->dirty_cursor,
1430 fn, context);
1431 else
1432 r = __load_mapping_v1(cmd, cb, hints_valid,
1433 &cmd->mapping_cursor, &cmd->hint_cursor,
1434 fn, context);
1435 if (r)
1436 goto out;
1439 * We need to break out before we move the cursors.
1441 if (cb >= (from_cblock(cmd->cache_blocks) - 1))
1442 break;
1444 r = dm_array_cursor_next(&cmd->mapping_cursor);
1445 if (r) {
1446 DMERR("dm_array_cursor_next for mapping failed");
1447 goto out;
1450 if (hints_valid) {
1451 r = dm_array_cursor_next(&cmd->hint_cursor);
1452 if (r) {
1453 DMERR("dm_array_cursor_next for hint failed");
1454 goto out;
1458 if (separate_dirty_bits(cmd)) {
1459 r = dm_bitset_cursor_next(&cmd->dirty_cursor);
1460 if (r) {
1461 DMERR("dm_bitset_cursor_next for dirty failed");
1462 goto out;
1466 out:
1467 dm_array_cursor_end(&cmd->mapping_cursor);
1468 if (hints_valid)
1469 dm_array_cursor_end(&cmd->hint_cursor);
1471 if (separate_dirty_bits(cmd))
1472 dm_bitset_cursor_end(&cmd->dirty_cursor);
1474 return r;
1477 int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
1478 struct dm_cache_policy *policy,
1479 load_mapping_fn fn, void *context)
1481 int r;
1483 READ_LOCK(cmd);
1484 r = __load_mappings(cmd, policy, fn, context);
1485 READ_UNLOCK(cmd);
1487 return r;
1490 static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
1492 int r = 0;
1493 __le64 value;
1494 dm_oblock_t oblock;
1495 unsigned flags;
1497 memcpy(&value, leaf, sizeof(value));
1498 unpack_value(value, &oblock, &flags);
1500 return r;
1503 static int __dump_mappings(struct dm_cache_metadata *cmd)
1505 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1508 void dm_cache_dump(struct dm_cache_metadata *cmd)
1510 READ_LOCK_VOID(cmd);
1511 __dump_mappings(cmd);
1512 READ_UNLOCK(cmd);
1515 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1517 int r;
1519 READ_LOCK(cmd);
1520 r = cmd->changed;
1521 READ_UNLOCK(cmd);
1523 return r;
1526 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1528 int r;
1529 unsigned flags;
1530 dm_oblock_t oblock;
1531 __le64 value;
1533 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1534 if (r)
1535 return r;
1537 unpack_value(value, &oblock, &flags);
1539 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1540 /* nothing to be done */
1541 return 0;
1543 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
1544 __dm_bless_for_disk(&value);
1546 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1547 &value, &cmd->root);
1548 if (r)
1549 return r;
1551 cmd->changed = true;
1552 return 0;
1556 static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1558 int r;
1559 unsigned i;
1560 for (i = 0; i < nr_bits; i++) {
1561 r = __dirty(cmd, to_cblock(i), test_bit(i, bits));
1562 if (r)
1563 return r;
1566 return 0;
1569 static int is_dirty_callback(uint32_t index, bool *value, void *context)
1571 unsigned long *bits = context;
1572 *value = test_bit(index, bits);
1573 return 0;
1576 static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits)
1578 int r = 0;
1580 /* nr_bits is really just a sanity check */
1581 if (nr_bits != from_cblock(cmd->cache_blocks)) {
1582 DMERR("dirty bitset is wrong size");
1583 return -EINVAL;
1586 r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root);
1587 if (r)
1588 return r;
1590 cmd->changed = true;
1591 return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits);
1594 int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd,
1595 unsigned nr_bits,
1596 unsigned long *bits)
1598 int r;
1600 WRITE_LOCK(cmd);
1601 if (separate_dirty_bits(cmd))
1602 r = __set_dirty_bits_v2(cmd, nr_bits, bits);
1603 else
1604 r = __set_dirty_bits_v1(cmd, nr_bits, bits);
1605 WRITE_UNLOCK(cmd);
1607 return r;
1610 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1611 struct dm_cache_statistics *stats)
1613 READ_LOCK_VOID(cmd);
1614 *stats = cmd->stats;
1615 READ_UNLOCK(cmd);
1618 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1619 struct dm_cache_statistics *stats)
1621 WRITE_LOCK_VOID(cmd);
1622 cmd->stats = *stats;
1623 WRITE_UNLOCK(cmd);
1626 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1628 int r;
1629 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1630 clear_clean_shutdown);
1632 WRITE_LOCK(cmd);
1633 r = __commit_transaction(cmd, mutator);
1634 if (r)
1635 goto out;
1637 r = __begin_transaction(cmd);
1639 out:
1640 WRITE_UNLOCK(cmd);
1641 return r;
1644 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1645 dm_block_t *result)
1647 int r = -EINVAL;
1649 READ_LOCK(cmd);
1650 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1651 READ_UNLOCK(cmd);
1653 return r;
1656 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1657 dm_block_t *result)
1659 int r = -EINVAL;
1661 READ_LOCK(cmd);
1662 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1663 READ_UNLOCK(cmd);
1665 return r;
1668 /*----------------------------------------------------------------*/
1670 static int get_hint(uint32_t index, void *value_le, void *context)
1672 uint32_t value;
1673 struct dm_cache_policy *policy = context;
1675 value = policy_get_hint(policy, to_cblock(index));
1676 *((__le32 *) value_le) = cpu_to_le32(value);
1678 return 0;
1682 * It's quicker to always delete the hint array, and recreate with
1683 * dm_array_new().
1685 static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1687 int r;
1688 size_t hint_size;
1689 const char *policy_name = dm_cache_policy_get_name(policy);
1690 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1692 if (!policy_name[0] ||
1693 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1694 return -EINVAL;
1696 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1697 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1699 hint_size = dm_cache_policy_get_hint_size(policy);
1700 if (!hint_size)
1701 return 0; /* short-circuit hints initialization */
1702 cmd->policy_hint_size = hint_size;
1704 if (cmd->hint_root) {
1705 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1706 if (r)
1707 return r;
1710 return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1711 from_cblock(cmd->cache_blocks),
1712 get_hint, policy);
1715 int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1717 int r;
1719 WRITE_LOCK(cmd);
1720 r = write_hints(cmd, policy);
1721 WRITE_UNLOCK(cmd);
1723 return r;
1726 int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result)
1728 int r;
1730 READ_LOCK(cmd);
1731 r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result);
1732 READ_UNLOCK(cmd);
1734 return r;
1737 void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd)
1739 WRITE_LOCK_VOID(cmd);
1740 dm_bm_set_read_only(cmd->bm);
1741 WRITE_UNLOCK(cmd);
1744 void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd)
1746 WRITE_LOCK_VOID(cmd);
1747 dm_bm_set_read_write(cmd->bm);
1748 WRITE_UNLOCK(cmd);
1751 int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
1753 int r;
1754 struct dm_block *sblock;
1755 struct cache_disk_superblock *disk_super;
1757 WRITE_LOCK(cmd);
1758 set_bit(NEEDS_CHECK, &cmd->flags);
1760 r = superblock_lock(cmd, &sblock);
1761 if (r) {
1762 DMERR("couldn't read superblock");
1763 goto out;
1766 disk_super = dm_block_data(sblock);
1767 disk_super->flags = cpu_to_le32(cmd->flags);
1769 dm_bm_unlock(sblock);
1771 out:
1772 WRITE_UNLOCK(cmd);
1773 return r;
1776 int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result)
1778 READ_LOCK(cmd);
1779 *result = !!test_bit(NEEDS_CHECK, &cmd->flags);
1780 READ_UNLOCK(cmd);
1782 return 0;
1785 int dm_cache_metadata_abort(struct dm_cache_metadata *cmd)
1787 int r;
1789 WRITE_LOCK(cmd);
1790 __destroy_persistent_data_objects(cmd);
1791 r = __create_persistent_data_objects(cmd, false);
1792 if (r)
1793 cmd->fail_io = true;
1794 WRITE_UNLOCK(cmd);
1796 return r;