x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / md / dm-cache-metadata.c
blobb564c0610259a7e897be161fdd43a565778d88c0
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
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 */
35 CLEAN_SHUTDOWN,
39 * Each mapping from cache block -> origin block carries a set of flags.
41 enum mapping_bits {
43 * A valid mapping. Because we're using an array we clear this
44 * flag for an non existant mapping.
46 M_VALID = 1,
49 * The data on the cache is different from that on the origin.
51 M_DIRTY = 2
54 struct cache_disk_superblock {
55 __le32 csum;
56 __le32 flags;
57 __le64 blocknr;
59 __u8 uuid[16];
60 __le64 magic;
61 __le32 version;
63 __u8 policy_name[CACHE_POLICY_NAME_SIZE];
64 __le32 policy_hint_size;
66 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
67 __le64 mapping_root;
68 __le64 hint_root;
70 __le64 discard_root;
71 __le64 discard_block_size;
72 __le64 discard_nr_blocks;
74 __le32 data_block_size;
75 __le32 metadata_block_size;
76 __le32 cache_blocks;
78 __le32 compat_flags;
79 __le32 compat_ro_flags;
80 __le32 incompat_flags;
82 __le32 read_hits;
83 __le32 read_misses;
84 __le32 write_hits;
85 __le32 write_misses;
87 __le32 policy_version[CACHE_POLICY_VERSION_SIZE];
88 } __packed;
90 struct dm_cache_metadata {
91 struct block_device *bdev;
92 struct dm_block_manager *bm;
93 struct dm_space_map *metadata_sm;
94 struct dm_transaction_manager *tm;
96 struct dm_array_info info;
97 struct dm_array_info hint_info;
98 struct dm_disk_bitset discard_info;
100 struct rw_semaphore root_lock;
101 dm_block_t root;
102 dm_block_t hint_root;
103 dm_block_t discard_root;
105 sector_t discard_block_size;
106 dm_dblock_t discard_nr_blocks;
108 sector_t data_block_size;
109 dm_cblock_t cache_blocks;
110 bool changed:1;
111 bool clean_when_opened:1;
113 char policy_name[CACHE_POLICY_NAME_SIZE];
114 unsigned policy_version[CACHE_POLICY_VERSION_SIZE];
115 size_t policy_hint_size;
116 struct dm_cache_statistics stats;
119 * Reading the space map root can fail, so we read it into this
120 * buffer before the superblock is locked and updated.
122 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
125 /*-------------------------------------------------------------------
126 * superblock validator
127 *-----------------------------------------------------------------*/
129 #define SUPERBLOCK_CSUM_XOR 9031977
131 static void sb_prepare_for_write(struct dm_block_validator *v,
132 struct dm_block *b,
133 size_t sb_block_size)
135 struct cache_disk_superblock *disk_super = dm_block_data(b);
137 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
138 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
139 sb_block_size - sizeof(__le32),
140 SUPERBLOCK_CSUM_XOR));
143 static int sb_check(struct dm_block_validator *v,
144 struct dm_block *b,
145 size_t sb_block_size)
147 struct cache_disk_superblock *disk_super = dm_block_data(b);
148 __le32 csum_le;
150 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
151 DMERR("sb_check failed: blocknr %llu: wanted %llu",
152 le64_to_cpu(disk_super->blocknr),
153 (unsigned long long)dm_block_location(b));
154 return -ENOTBLK;
157 if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) {
158 DMERR("sb_check failed: magic %llu: wanted %llu",
159 le64_to_cpu(disk_super->magic),
160 (unsigned long long)CACHE_SUPERBLOCK_MAGIC);
161 return -EILSEQ;
164 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
165 sb_block_size - sizeof(__le32),
166 SUPERBLOCK_CSUM_XOR));
167 if (csum_le != disk_super->csum) {
168 DMERR("sb_check failed: csum %u: wanted %u",
169 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
170 return -EILSEQ;
173 return 0;
176 static struct dm_block_validator sb_validator = {
177 .name = "superblock",
178 .prepare_for_write = sb_prepare_for_write,
179 .check = sb_check
182 /*----------------------------------------------------------------*/
184 static int superblock_read_lock(struct dm_cache_metadata *cmd,
185 struct dm_block **sblock)
187 return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
188 &sb_validator, sblock);
191 static int superblock_lock_zero(struct dm_cache_metadata *cmd,
192 struct dm_block **sblock)
194 return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
195 &sb_validator, sblock);
198 static int superblock_lock(struct dm_cache_metadata *cmd,
199 struct dm_block **sblock)
201 return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
202 &sb_validator, sblock);
205 /*----------------------------------------------------------------*/
207 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
209 int r;
210 unsigned i;
211 struct dm_block *b;
212 __le64 *data_le, zero = cpu_to_le64(0);
213 unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
216 * We can't use a validator here - it may be all zeroes.
218 r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b);
219 if (r)
220 return r;
222 data_le = dm_block_data(b);
223 *result = 1;
224 for (i = 0; i < sb_block_size; i++) {
225 if (data_le[i] != zero) {
226 *result = 0;
227 break;
231 return dm_bm_unlock(b);
234 static void __setup_mapping_info(struct dm_cache_metadata *cmd)
236 struct dm_btree_value_type vt;
238 vt.context = NULL;
239 vt.size = sizeof(__le64);
240 vt.inc = NULL;
241 vt.dec = NULL;
242 vt.equal = NULL;
243 dm_array_info_init(&cmd->info, cmd->tm, &vt);
245 if (cmd->policy_hint_size) {
246 vt.size = sizeof(__le32);
247 dm_array_info_init(&cmd->hint_info, cmd->tm, &vt);
251 static int __save_sm_root(struct dm_cache_metadata *cmd)
253 int r;
254 size_t metadata_len;
256 r = dm_sm_root_size(cmd->metadata_sm, &metadata_len);
257 if (r < 0)
258 return r;
260 return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root,
261 metadata_len);
264 static void __copy_sm_root(struct dm_cache_metadata *cmd,
265 struct cache_disk_superblock *disk_super)
267 memcpy(&disk_super->metadata_space_map_root,
268 &cmd->metadata_space_map_root,
269 sizeof(cmd->metadata_space_map_root));
272 static int __write_initial_superblock(struct dm_cache_metadata *cmd)
274 int r;
275 struct dm_block *sblock;
276 struct cache_disk_superblock *disk_super;
277 sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT;
279 /* FIXME: see if we can lose the max sectors limit */
280 if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS)
281 bdev_size = DM_CACHE_METADATA_MAX_SECTORS;
283 r = dm_tm_pre_commit(cmd->tm);
284 if (r < 0)
285 return r;
288 * dm_sm_copy_root() can fail. So we need to do it before we start
289 * updating the superblock.
291 r = __save_sm_root(cmd);
292 if (r)
293 return r;
295 r = superblock_lock_zero(cmd, &sblock);
296 if (r)
297 return r;
299 disk_super = dm_block_data(sblock);
300 disk_super->flags = 0;
301 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
302 disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC);
303 disk_super->version = cpu_to_le32(CACHE_VERSION);
304 memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name));
305 memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version));
306 disk_super->policy_hint_size = 0;
308 __copy_sm_root(cmd, disk_super);
310 disk_super->mapping_root = cpu_to_le64(cmd->root);
311 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
312 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
313 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
314 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
315 disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
316 disk_super->data_block_size = cpu_to_le32(cmd->data_block_size);
317 disk_super->cache_blocks = cpu_to_le32(0);
319 disk_super->read_hits = cpu_to_le32(0);
320 disk_super->read_misses = cpu_to_le32(0);
321 disk_super->write_hits = cpu_to_le32(0);
322 disk_super->write_misses = cpu_to_le32(0);
324 return dm_tm_commit(cmd->tm, sblock);
327 static int __format_metadata(struct dm_cache_metadata *cmd)
329 int r;
331 r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
332 &cmd->tm, &cmd->metadata_sm);
333 if (r < 0) {
334 DMERR("tm_create_with_sm failed");
335 return r;
338 __setup_mapping_info(cmd);
340 r = dm_array_empty(&cmd->info, &cmd->root);
341 if (r < 0)
342 goto bad;
344 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
346 r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root);
347 if (r < 0)
348 goto bad;
350 cmd->discard_block_size = 0;
351 cmd->discard_nr_blocks = 0;
353 r = __write_initial_superblock(cmd);
354 if (r)
355 goto bad;
357 cmd->clean_when_opened = true;
358 return 0;
360 bad:
361 dm_tm_destroy(cmd->tm);
362 dm_sm_destroy(cmd->metadata_sm);
364 return r;
367 static int __check_incompat_features(struct cache_disk_superblock *disk_super,
368 struct dm_cache_metadata *cmd)
370 uint32_t features;
372 features = le32_to_cpu(disk_super->incompat_flags) & ~DM_CACHE_FEATURE_INCOMPAT_SUPP;
373 if (features) {
374 DMERR("could not access metadata due to unsupported optional features (%lx).",
375 (unsigned long)features);
376 return -EINVAL;
380 * Check for read-only metadata to skip the following RDWR checks.
382 if (get_disk_ro(cmd->bdev->bd_disk))
383 return 0;
385 features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP;
386 if (features) {
387 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
388 (unsigned long)features);
389 return -EINVAL;
392 return 0;
395 static int __open_metadata(struct dm_cache_metadata *cmd)
397 int r;
398 struct dm_block *sblock;
399 struct cache_disk_superblock *disk_super;
400 unsigned long sb_flags;
402 r = superblock_read_lock(cmd, &sblock);
403 if (r < 0) {
404 DMERR("couldn't read lock superblock");
405 return r;
408 disk_super = dm_block_data(sblock);
410 /* Verify the data block size hasn't changed */
411 if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) {
412 DMERR("changing the data block size (from %u to %llu) is not supported",
413 le32_to_cpu(disk_super->data_block_size),
414 (unsigned long long)cmd->data_block_size);
415 r = -EINVAL;
416 goto bad;
419 r = __check_incompat_features(disk_super, cmd);
420 if (r < 0)
421 goto bad;
423 r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION,
424 disk_super->metadata_space_map_root,
425 sizeof(disk_super->metadata_space_map_root),
426 &cmd->tm, &cmd->metadata_sm);
427 if (r < 0) {
428 DMERR("tm_open_with_sm failed");
429 goto bad;
432 __setup_mapping_info(cmd);
433 dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
434 sb_flags = le32_to_cpu(disk_super->flags);
435 cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
436 return dm_bm_unlock(sblock);
438 bad:
439 dm_bm_unlock(sblock);
440 return r;
443 static int __open_or_format_metadata(struct dm_cache_metadata *cmd,
444 bool format_device)
446 int r, unformatted;
448 r = __superblock_all_zeroes(cmd->bm, &unformatted);
449 if (r)
450 return r;
452 if (unformatted)
453 return format_device ? __format_metadata(cmd) : -EPERM;
455 return __open_metadata(cmd);
458 static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
459 bool may_format_device)
461 int r;
462 cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE,
463 CACHE_METADATA_CACHE_SIZE,
464 CACHE_MAX_CONCURRENT_LOCKS);
465 if (IS_ERR(cmd->bm)) {
466 DMERR("could not create block manager");
467 return PTR_ERR(cmd->bm);
470 r = __open_or_format_metadata(cmd, may_format_device);
471 if (r)
472 dm_block_manager_destroy(cmd->bm);
474 return r;
477 static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd)
479 dm_sm_destroy(cmd->metadata_sm);
480 dm_tm_destroy(cmd->tm);
481 dm_block_manager_destroy(cmd->bm);
484 typedef unsigned long (*flags_mutator)(unsigned long);
486 static void update_flags(struct cache_disk_superblock *disk_super,
487 flags_mutator mutator)
489 uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags));
490 disk_super->flags = cpu_to_le32(sb_flags);
493 static unsigned long set_clean_shutdown(unsigned long flags)
495 set_bit(CLEAN_SHUTDOWN, &flags);
496 return flags;
499 static unsigned long clear_clean_shutdown(unsigned long flags)
501 clear_bit(CLEAN_SHUTDOWN, &flags);
502 return flags;
505 static void read_superblock_fields(struct dm_cache_metadata *cmd,
506 struct cache_disk_superblock *disk_super)
508 cmd->root = le64_to_cpu(disk_super->mapping_root);
509 cmd->hint_root = le64_to_cpu(disk_super->hint_root);
510 cmd->discard_root = le64_to_cpu(disk_super->discard_root);
511 cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size);
512 cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks));
513 cmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
514 cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks));
515 strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name));
516 cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]);
517 cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]);
518 cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]);
519 cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size);
521 cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits);
522 cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses);
523 cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits);
524 cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses);
526 cmd->changed = false;
530 * The mutator updates the superblock flags.
532 static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
533 flags_mutator mutator)
535 int r;
536 struct cache_disk_superblock *disk_super;
537 struct dm_block *sblock;
539 r = superblock_lock(cmd, &sblock);
540 if (r)
541 return r;
543 disk_super = dm_block_data(sblock);
544 update_flags(disk_super, mutator);
545 read_superblock_fields(cmd, disk_super);
546 dm_bm_unlock(sblock);
548 return dm_bm_flush(cmd->bm);
551 static int __begin_transaction(struct dm_cache_metadata *cmd)
553 int r;
554 struct cache_disk_superblock *disk_super;
555 struct dm_block *sblock;
558 * We re-read the superblock every time. Shouldn't need to do this
559 * really.
561 r = superblock_read_lock(cmd, &sblock);
562 if (r)
563 return r;
565 disk_super = dm_block_data(sblock);
566 read_superblock_fields(cmd, disk_super);
567 dm_bm_unlock(sblock);
569 return 0;
572 static int __commit_transaction(struct dm_cache_metadata *cmd,
573 flags_mutator mutator)
575 int r;
576 struct cache_disk_superblock *disk_super;
577 struct dm_block *sblock;
580 * We need to know if the cache_disk_superblock exceeds a 512-byte sector.
582 BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512);
584 r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root,
585 &cmd->discard_root);
586 if (r)
587 return r;
589 r = dm_tm_pre_commit(cmd->tm);
590 if (r < 0)
591 return r;
593 r = __save_sm_root(cmd);
594 if (r)
595 return r;
597 r = superblock_lock(cmd, &sblock);
598 if (r)
599 return r;
601 disk_super = dm_block_data(sblock);
603 if (mutator)
604 update_flags(disk_super, mutator);
606 disk_super->mapping_root = cpu_to_le64(cmd->root);
607 disk_super->hint_root = cpu_to_le64(cmd->hint_root);
608 disk_super->discard_root = cpu_to_le64(cmd->discard_root);
609 disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size);
610 disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks));
611 disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks));
612 strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name));
613 disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]);
614 disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]);
615 disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]);
617 disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits);
618 disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses);
619 disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits);
620 disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses);
621 __copy_sm_root(cmd, disk_super);
623 return dm_tm_commit(cmd->tm, sblock);
626 /*----------------------------------------------------------------*/
629 * The mappings are held in a dm-array that has 64-bit values stored in
630 * little-endian format. The index is the cblock, the high 48bits of the
631 * value are the oblock and the low 16 bit the flags.
633 #define FLAGS_MASK ((1 << 16) - 1)
635 static __le64 pack_value(dm_oblock_t block, unsigned flags)
637 uint64_t value = from_oblock(block);
638 value <<= 16;
639 value = value | (flags & FLAGS_MASK);
640 return cpu_to_le64(value);
643 static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags)
645 uint64_t value = le64_to_cpu(value_le);
646 uint64_t b = value >> 16;
647 *block = to_oblock(b);
648 *flags = value & FLAGS_MASK;
651 /*----------------------------------------------------------------*/
653 struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev,
654 sector_t data_block_size,
655 bool may_format_device,
656 size_t policy_hint_size)
658 int r;
659 struct dm_cache_metadata *cmd;
661 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
662 if (!cmd) {
663 DMERR("could not allocate metadata struct");
664 return NULL;
667 init_rwsem(&cmd->root_lock);
668 cmd->bdev = bdev;
669 cmd->data_block_size = data_block_size;
670 cmd->cache_blocks = 0;
671 cmd->policy_hint_size = policy_hint_size;
672 cmd->changed = true;
674 r = __create_persistent_data_objects(cmd, may_format_device);
675 if (r) {
676 kfree(cmd);
677 return ERR_PTR(r);
680 r = __begin_transaction_flags(cmd, clear_clean_shutdown);
681 if (r < 0) {
682 dm_cache_metadata_close(cmd);
683 return ERR_PTR(r);
686 return cmd;
689 void dm_cache_metadata_close(struct dm_cache_metadata *cmd)
691 __destroy_persistent_data_objects(cmd);
692 kfree(cmd);
695 int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size)
697 int r;
698 __le64 null_mapping = pack_value(0, 0);
700 down_write(&cmd->root_lock);
701 __dm_bless_for_disk(&null_mapping);
702 r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks),
703 from_cblock(new_cache_size),
704 &null_mapping, &cmd->root);
705 if (!r)
706 cmd->cache_blocks = new_cache_size;
707 cmd->changed = true;
708 up_write(&cmd->root_lock);
710 return r;
713 int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd,
714 sector_t discard_block_size,
715 dm_dblock_t new_nr_entries)
717 int r;
719 down_write(&cmd->root_lock);
720 r = dm_bitset_resize(&cmd->discard_info,
721 cmd->discard_root,
722 from_dblock(cmd->discard_nr_blocks),
723 from_dblock(new_nr_entries),
724 false, &cmd->discard_root);
725 if (!r) {
726 cmd->discard_block_size = discard_block_size;
727 cmd->discard_nr_blocks = new_nr_entries;
730 cmd->changed = true;
731 up_write(&cmd->root_lock);
733 return r;
736 static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
738 return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root,
739 from_dblock(b), &cmd->discard_root);
742 static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b)
744 return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root,
745 from_dblock(b), &cmd->discard_root);
748 static int __is_discarded(struct dm_cache_metadata *cmd, dm_dblock_t b,
749 bool *is_discarded)
751 return dm_bitset_test_bit(&cmd->discard_info, cmd->discard_root,
752 from_dblock(b), &cmd->discard_root,
753 is_discarded);
756 static int __discard(struct dm_cache_metadata *cmd,
757 dm_dblock_t dblock, bool discard)
759 int r;
761 r = (discard ? __set_discard : __clear_discard)(cmd, dblock);
762 if (r)
763 return r;
765 cmd->changed = true;
766 return 0;
769 int dm_cache_set_discard(struct dm_cache_metadata *cmd,
770 dm_dblock_t dblock, bool discard)
772 int r;
774 down_write(&cmd->root_lock);
775 r = __discard(cmd, dblock, discard);
776 up_write(&cmd->root_lock);
778 return r;
781 static int __load_discards(struct dm_cache_metadata *cmd,
782 load_discard_fn fn, void *context)
784 int r = 0;
785 dm_block_t b;
786 bool discard;
788 for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) {
789 dm_dblock_t dblock = to_dblock(b);
791 if (cmd->clean_when_opened) {
792 r = __is_discarded(cmd, dblock, &discard);
793 if (r)
794 return r;
795 } else
796 discard = false;
798 r = fn(context, cmd->discard_block_size, dblock, discard);
799 if (r)
800 break;
803 return r;
806 int dm_cache_load_discards(struct dm_cache_metadata *cmd,
807 load_discard_fn fn, void *context)
809 int r;
811 down_read(&cmd->root_lock);
812 r = __load_discards(cmd, fn, context);
813 up_read(&cmd->root_lock);
815 return r;
818 dm_cblock_t dm_cache_size(struct dm_cache_metadata *cmd)
820 dm_cblock_t r;
822 down_read(&cmd->root_lock);
823 r = cmd->cache_blocks;
824 up_read(&cmd->root_lock);
826 return r;
829 static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
831 int r;
832 __le64 value = pack_value(0, 0);
834 __dm_bless_for_disk(&value);
835 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
836 &value, &cmd->root);
837 if (r)
838 return r;
840 cmd->changed = true;
841 return 0;
844 int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock)
846 int r;
848 down_write(&cmd->root_lock);
849 r = __remove(cmd, cblock);
850 up_write(&cmd->root_lock);
852 return r;
855 static int __insert(struct dm_cache_metadata *cmd,
856 dm_cblock_t cblock, dm_oblock_t oblock)
858 int r;
859 __le64 value = pack_value(oblock, M_VALID);
860 __dm_bless_for_disk(&value);
862 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
863 &value, &cmd->root);
864 if (r)
865 return r;
867 cmd->changed = true;
868 return 0;
871 int dm_cache_insert_mapping(struct dm_cache_metadata *cmd,
872 dm_cblock_t cblock, dm_oblock_t oblock)
874 int r;
876 down_write(&cmd->root_lock);
877 r = __insert(cmd, cblock, oblock);
878 up_write(&cmd->root_lock);
880 return r;
883 struct thunk {
884 load_mapping_fn fn;
885 void *context;
887 struct dm_cache_metadata *cmd;
888 bool respect_dirty_flags;
889 bool hints_valid;
892 static bool policy_unchanged(struct dm_cache_metadata *cmd,
893 struct dm_cache_policy *policy)
895 const char *policy_name = dm_cache_policy_get_name(policy);
896 const unsigned *policy_version = dm_cache_policy_get_version(policy);
897 size_t policy_hint_size = dm_cache_policy_get_hint_size(policy);
900 * Ensure policy names match.
902 if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name)))
903 return false;
906 * Ensure policy major versions match.
908 if (cmd->policy_version[0] != policy_version[0])
909 return false;
912 * Ensure policy hint sizes match.
914 if (cmd->policy_hint_size != policy_hint_size)
915 return false;
917 return true;
920 static bool hints_array_initialized(struct dm_cache_metadata *cmd)
922 return cmd->hint_root && cmd->policy_hint_size;
925 static bool hints_array_available(struct dm_cache_metadata *cmd,
926 struct dm_cache_policy *policy)
928 return cmd->clean_when_opened && policy_unchanged(cmd, policy) &&
929 hints_array_initialized(cmd);
932 static int __load_mapping(void *context, uint64_t cblock, void *leaf)
934 int r = 0;
935 bool dirty;
936 __le64 value;
937 __le32 hint_value = 0;
938 dm_oblock_t oblock;
939 unsigned flags;
940 struct thunk *thunk = context;
941 struct dm_cache_metadata *cmd = thunk->cmd;
943 memcpy(&value, leaf, sizeof(value));
944 unpack_value(value, &oblock, &flags);
946 if (flags & M_VALID) {
947 if (thunk->hints_valid) {
948 r = dm_array_get_value(&cmd->hint_info, cmd->hint_root,
949 cblock, &hint_value);
950 if (r && r != -ENODATA)
951 return r;
954 dirty = thunk->respect_dirty_flags ? (flags & M_DIRTY) : true;
955 r = thunk->fn(thunk->context, oblock, to_cblock(cblock),
956 dirty, le32_to_cpu(hint_value), thunk->hints_valid);
959 return r;
962 static int __load_mappings(struct dm_cache_metadata *cmd,
963 struct dm_cache_policy *policy,
964 load_mapping_fn fn, void *context)
966 struct thunk thunk;
968 thunk.fn = fn;
969 thunk.context = context;
971 thunk.cmd = cmd;
972 thunk.respect_dirty_flags = cmd->clean_when_opened;
973 thunk.hints_valid = hints_array_available(cmd, policy);
975 return dm_array_walk(&cmd->info, cmd->root, __load_mapping, &thunk);
978 int dm_cache_load_mappings(struct dm_cache_metadata *cmd,
979 struct dm_cache_policy *policy,
980 load_mapping_fn fn, void *context)
982 int r;
984 down_read(&cmd->root_lock);
985 r = __load_mappings(cmd, policy, fn, context);
986 up_read(&cmd->root_lock);
988 return r;
991 static int __dump_mapping(void *context, uint64_t cblock, void *leaf)
993 int r = 0;
994 __le64 value;
995 dm_oblock_t oblock;
996 unsigned flags;
998 memcpy(&value, leaf, sizeof(value));
999 unpack_value(value, &oblock, &flags);
1001 return r;
1004 static int __dump_mappings(struct dm_cache_metadata *cmd)
1006 return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL);
1009 void dm_cache_dump(struct dm_cache_metadata *cmd)
1011 down_read(&cmd->root_lock);
1012 __dump_mappings(cmd);
1013 up_read(&cmd->root_lock);
1016 int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd)
1018 int r;
1020 down_read(&cmd->root_lock);
1021 r = cmd->changed;
1022 up_read(&cmd->root_lock);
1024 return r;
1027 static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty)
1029 int r;
1030 unsigned flags;
1031 dm_oblock_t oblock;
1032 __le64 value;
1034 r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value);
1035 if (r)
1036 return r;
1038 unpack_value(value, &oblock, &flags);
1040 if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty))
1041 /* nothing to be done */
1042 return 0;
1044 value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0));
1045 __dm_bless_for_disk(&value);
1047 r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock),
1048 &value, &cmd->root);
1049 if (r)
1050 return r;
1052 cmd->changed = true;
1053 return 0;
1057 int dm_cache_set_dirty(struct dm_cache_metadata *cmd,
1058 dm_cblock_t cblock, bool dirty)
1060 int r;
1062 down_write(&cmd->root_lock);
1063 r = __dirty(cmd, cblock, dirty);
1064 up_write(&cmd->root_lock);
1066 return r;
1069 void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd,
1070 struct dm_cache_statistics *stats)
1072 down_read(&cmd->root_lock);
1073 *stats = cmd->stats;
1074 up_read(&cmd->root_lock);
1077 void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd,
1078 struct dm_cache_statistics *stats)
1080 down_write(&cmd->root_lock);
1081 cmd->stats = *stats;
1082 up_write(&cmd->root_lock);
1085 int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown)
1087 int r;
1088 flags_mutator mutator = (clean_shutdown ? set_clean_shutdown :
1089 clear_clean_shutdown);
1091 down_write(&cmd->root_lock);
1092 r = __commit_transaction(cmd, mutator);
1093 if (r)
1094 goto out;
1096 r = __begin_transaction(cmd);
1098 out:
1099 up_write(&cmd->root_lock);
1100 return r;
1103 int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd,
1104 dm_block_t *result)
1106 int r = -EINVAL;
1108 down_read(&cmd->root_lock);
1109 r = dm_sm_get_nr_free(cmd->metadata_sm, result);
1110 up_read(&cmd->root_lock);
1112 return r;
1115 int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
1116 dm_block_t *result)
1118 int r = -EINVAL;
1120 down_read(&cmd->root_lock);
1121 r = dm_sm_get_nr_blocks(cmd->metadata_sm, result);
1122 up_read(&cmd->root_lock);
1124 return r;
1127 /*----------------------------------------------------------------*/
1129 static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1131 int r;
1132 __le32 value;
1133 size_t hint_size;
1134 const char *policy_name = dm_cache_policy_get_name(policy);
1135 const unsigned *policy_version = dm_cache_policy_get_version(policy);
1137 if (!policy_name[0] ||
1138 (strlen(policy_name) > sizeof(cmd->policy_name) - 1))
1139 return -EINVAL;
1141 if (!policy_unchanged(cmd, policy)) {
1142 strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1143 memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1145 hint_size = dm_cache_policy_get_hint_size(policy);
1146 if (!hint_size)
1147 return 0; /* short-circuit hints initialization */
1148 cmd->policy_hint_size = hint_size;
1150 if (cmd->hint_root) {
1151 r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1152 if (r)
1153 return r;
1156 r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
1157 if (r)
1158 return r;
1160 value = cpu_to_le32(0);
1161 __dm_bless_for_disk(&value);
1162 r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
1163 from_cblock(cmd->cache_blocks),
1164 &value, &cmd->hint_root);
1165 if (r)
1166 return r;
1169 return 0;
1172 int dm_cache_begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1174 int r;
1176 down_write(&cmd->root_lock);
1177 r = begin_hints(cmd, policy);
1178 up_write(&cmd->root_lock);
1180 return r;
1183 static int save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1184 uint32_t hint)
1186 int r;
1187 __le32 value = cpu_to_le32(hint);
1188 __dm_bless_for_disk(&value);
1190 r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
1191 from_cblock(cblock), &value, &cmd->hint_root);
1192 cmd->changed = true;
1194 return r;
1197 int dm_cache_save_hint(struct dm_cache_metadata *cmd, dm_cblock_t cblock,
1198 uint32_t hint)
1200 int r;
1202 if (!hints_array_initialized(cmd))
1203 return 0;
1205 down_write(&cmd->root_lock);
1206 r = save_hint(cmd, cblock, hint);
1207 up_write(&cmd->root_lock);
1209 return r;