Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat, Inc. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm-cache-metadata.h" |
| 8 | |
| 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" |
| 14 | |
| 15 | #include <linux/device-mapper.h> |
Elena Reshetova | 6bdd079 | 2017-10-20 10:37:37 +0300 | [diff] [blame] | 16 | #include <linux/refcount.h> |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 17 | |
| 18 | /*----------------------------------------------------------------*/ |
| 19 | |
| 20 | #define DM_MSG_PREFIX "cache metadata" |
| 21 | |
| 22 | #define CACHE_SUPERBLOCK_MAGIC 06142003 |
| 23 | #define CACHE_SUPERBLOCK_LOCATION 0 |
Joe Thornber | 53d4981 | 2013-10-16 10:59:26 +0100 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * defines a range of metadata versions that this module can handle. |
| 27 | */ |
| 28 | #define MIN_CACHE_VERSION 1 |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 29 | #define MAX_CACHE_VERSION 2 |
Joe Thornber | 53d4981 | 2013-10-16 10:59:26 +0100 | [diff] [blame] | 30 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 31 | /* |
| 32 | * 3 for btree insert + |
| 33 | * 2 for btree lookup used within space map |
| 34 | */ |
| 35 | #define CACHE_MAX_CONCURRENT_LOCKS 5 |
| 36 | #define SPACE_MAP_ROOT_SIZE 128 |
| 37 | |
| 38 | enum superblock_flag_bits { |
| 39 | /* for spotting crashes that would invalidate the dirty bitset */ |
| 40 | CLEAN_SHUTDOWN, |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 41 | /* metadata must be checked using the tools */ |
| 42 | NEEDS_CHECK, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Each mapping from cache block -> origin block carries a set of flags. |
| 47 | */ |
| 48 | enum mapping_bits { |
| 49 | /* |
| 50 | * A valid mapping. Because we're using an array we clear this |
| 51 | * flag for an non existant mapping. |
| 52 | */ |
| 53 | M_VALID = 1, |
| 54 | |
| 55 | /* |
| 56 | * The data on the cache is different from that on the origin. |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 57 | * This flag is only used by metadata format 1. |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 58 | */ |
| 59 | M_DIRTY = 2 |
| 60 | }; |
| 61 | |
| 62 | struct cache_disk_superblock { |
| 63 | __le32 csum; |
| 64 | __le32 flags; |
| 65 | __le64 blocknr; |
| 66 | |
| 67 | __u8 uuid[16]; |
| 68 | __le64 magic; |
| 69 | __le32 version; |
| 70 | |
| 71 | __u8 policy_name[CACHE_POLICY_NAME_SIZE]; |
| 72 | __le32 policy_hint_size; |
| 73 | |
| 74 | __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 75 | __le64 mapping_root; |
| 76 | __le64 hint_root; |
| 77 | |
| 78 | __le64 discard_root; |
| 79 | __le64 discard_block_size; |
| 80 | __le64 discard_nr_blocks; |
| 81 | |
| 82 | __le32 data_block_size; |
| 83 | __le32 metadata_block_size; |
| 84 | __le32 cache_blocks; |
| 85 | |
| 86 | __le32 compat_flags; |
| 87 | __le32 compat_ro_flags; |
| 88 | __le32 incompat_flags; |
| 89 | |
| 90 | __le32 read_hits; |
| 91 | __le32 read_misses; |
| 92 | __le32 write_hits; |
| 93 | __le32 write_misses; |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 94 | |
| 95 | __le32 policy_version[CACHE_POLICY_VERSION_SIZE]; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Metadata format 2 fields. |
| 99 | */ |
| 100 | __le64 dirty_root; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 101 | } __packed; |
| 102 | |
| 103 | struct dm_cache_metadata { |
Elena Reshetova | 6bdd079 | 2017-10-20 10:37:37 +0300 | [diff] [blame] | 104 | refcount_t ref_count; |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 105 | struct list_head list; |
| 106 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 107 | unsigned version; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 108 | struct block_device *bdev; |
| 109 | struct dm_block_manager *bm; |
| 110 | struct dm_space_map *metadata_sm; |
| 111 | struct dm_transaction_manager *tm; |
| 112 | |
| 113 | struct dm_array_info info; |
| 114 | struct dm_array_info hint_info; |
| 115 | struct dm_disk_bitset discard_info; |
| 116 | |
| 117 | struct rw_semaphore root_lock; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 118 | unsigned long flags; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 119 | dm_block_t root; |
| 120 | dm_block_t hint_root; |
| 121 | dm_block_t discard_root; |
| 122 | |
| 123 | sector_t discard_block_size; |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 124 | dm_dblock_t discard_nr_blocks; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 125 | |
| 126 | sector_t data_block_size; |
| 127 | dm_cblock_t cache_blocks; |
| 128 | bool changed:1; |
| 129 | bool clean_when_opened:1; |
| 130 | |
| 131 | char policy_name[CACHE_POLICY_NAME_SIZE]; |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 132 | unsigned policy_version[CACHE_POLICY_VERSION_SIZE]; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 133 | size_t policy_hint_size; |
| 134 | struct dm_cache_statistics stats; |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Reading the space map root can fail, so we read it into this |
| 138 | * buffer before the superblock is locked and updated. |
| 139 | */ |
| 140 | __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Set if a transaction has to be aborted but the attempt to roll |
| 144 | * back to the previous (good) transaction failed. The only |
| 145 | * metadata operation permissible in this state is the closing of |
| 146 | * the device. |
| 147 | */ |
| 148 | bool fail_io:1; |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 149 | |
| 150 | /* |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 151 | * Metadata format 2 fields. |
| 152 | */ |
| 153 | dm_block_t dirty_root; |
| 154 | struct dm_disk_bitset dirty_info; |
| 155 | |
| 156 | /* |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 157 | * These structures are used when loading metadata. They're too |
| 158 | * big to put on the stack. |
| 159 | */ |
| 160 | struct dm_array_cursor mapping_cursor; |
| 161 | struct dm_array_cursor hint_cursor; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 162 | struct dm_bitset_cursor dirty_cursor; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | /*------------------------------------------------------------------- |
| 166 | * superblock validator |
| 167 | *-----------------------------------------------------------------*/ |
| 168 | |
| 169 | #define SUPERBLOCK_CSUM_XOR 9031977 |
| 170 | |
| 171 | static void sb_prepare_for_write(struct dm_block_validator *v, |
| 172 | struct dm_block *b, |
| 173 | size_t sb_block_size) |
| 174 | { |
| 175 | struct cache_disk_superblock *disk_super = dm_block_data(b); |
| 176 | |
| 177 | disk_super->blocknr = cpu_to_le64(dm_block_location(b)); |
| 178 | disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 179 | sb_block_size - sizeof(__le32), |
| 180 | SUPERBLOCK_CSUM_XOR)); |
| 181 | } |
| 182 | |
Joe Thornber | 53d4981 | 2013-10-16 10:59:26 +0100 | [diff] [blame] | 183 | static int check_metadata_version(struct cache_disk_superblock *disk_super) |
| 184 | { |
| 185 | uint32_t metadata_version = le32_to_cpu(disk_super->version); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 186 | |
Joe Thornber | 53d4981 | 2013-10-16 10:59:26 +0100 | [diff] [blame] | 187 | if (metadata_version < MIN_CACHE_VERSION || metadata_version > MAX_CACHE_VERSION) { |
| 188 | DMERR("Cache metadata version %u found, but only versions between %u and %u supported.", |
| 189 | metadata_version, MIN_CACHE_VERSION, MAX_CACHE_VERSION); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 196 | static int sb_check(struct dm_block_validator *v, |
| 197 | struct dm_block *b, |
| 198 | size_t sb_block_size) |
| 199 | { |
| 200 | struct cache_disk_superblock *disk_super = dm_block_data(b); |
| 201 | __le32 csum_le; |
| 202 | |
| 203 | if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) { |
| 204 | DMERR("sb_check failed: blocknr %llu: wanted %llu", |
| 205 | le64_to_cpu(disk_super->blocknr), |
| 206 | (unsigned long long)dm_block_location(b)); |
| 207 | return -ENOTBLK; |
| 208 | } |
| 209 | |
| 210 | if (le64_to_cpu(disk_super->magic) != CACHE_SUPERBLOCK_MAGIC) { |
| 211 | DMERR("sb_check failed: magic %llu: wanted %llu", |
| 212 | le64_to_cpu(disk_super->magic), |
| 213 | (unsigned long long)CACHE_SUPERBLOCK_MAGIC); |
| 214 | return -EILSEQ; |
| 215 | } |
| 216 | |
| 217 | csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 218 | sb_block_size - sizeof(__le32), |
| 219 | SUPERBLOCK_CSUM_XOR)); |
| 220 | if (csum_le != disk_super->csum) { |
| 221 | DMERR("sb_check failed: csum %u: wanted %u", |
| 222 | le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum)); |
| 223 | return -EILSEQ; |
| 224 | } |
| 225 | |
Joe Thornber | 53d4981 | 2013-10-16 10:59:26 +0100 | [diff] [blame] | 226 | return check_metadata_version(disk_super); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static struct dm_block_validator sb_validator = { |
| 230 | .name = "superblock", |
| 231 | .prepare_for_write = sb_prepare_for_write, |
| 232 | .check = sb_check |
| 233 | }; |
| 234 | |
| 235 | /*----------------------------------------------------------------*/ |
| 236 | |
| 237 | static int superblock_read_lock(struct dm_cache_metadata *cmd, |
| 238 | struct dm_block **sblock) |
| 239 | { |
| 240 | return dm_bm_read_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION, |
| 241 | &sb_validator, sblock); |
| 242 | } |
| 243 | |
| 244 | static int superblock_lock_zero(struct dm_cache_metadata *cmd, |
| 245 | struct dm_block **sblock) |
| 246 | { |
| 247 | return dm_bm_write_lock_zero(cmd->bm, CACHE_SUPERBLOCK_LOCATION, |
| 248 | &sb_validator, sblock); |
| 249 | } |
| 250 | |
| 251 | static int superblock_lock(struct dm_cache_metadata *cmd, |
| 252 | struct dm_block **sblock) |
| 253 | { |
| 254 | return dm_bm_write_lock(cmd->bm, CACHE_SUPERBLOCK_LOCATION, |
| 255 | &sb_validator, sblock); |
| 256 | } |
| 257 | |
| 258 | /*----------------------------------------------------------------*/ |
| 259 | |
Joe Thornber | dd8b0c2 | 2013-10-24 14:10:28 -0400 | [diff] [blame] | 260 | static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 261 | { |
| 262 | int r; |
| 263 | unsigned i; |
| 264 | struct dm_block *b; |
| 265 | __le64 *data_le, zero = cpu_to_le64(0); |
| 266 | unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64); |
| 267 | |
| 268 | /* |
| 269 | * We can't use a validator here - it may be all zeroes. |
| 270 | */ |
| 271 | r = dm_bm_read_lock(bm, CACHE_SUPERBLOCK_LOCATION, NULL, &b); |
| 272 | if (r) |
| 273 | return r; |
| 274 | |
| 275 | data_le = dm_block_data(b); |
Joe Thornber | dd8b0c2 | 2013-10-24 14:10:28 -0400 | [diff] [blame] | 276 | *result = true; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 277 | for (i = 0; i < sb_block_size; i++) { |
| 278 | if (data_le[i] != zero) { |
Joe Thornber | dd8b0c2 | 2013-10-24 14:10:28 -0400 | [diff] [blame] | 279 | *result = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 284 | dm_bm_unlock(b); |
| 285 | |
| 286 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void __setup_mapping_info(struct dm_cache_metadata *cmd) |
| 290 | { |
| 291 | struct dm_btree_value_type vt; |
| 292 | |
| 293 | vt.context = NULL; |
| 294 | vt.size = sizeof(__le64); |
| 295 | vt.inc = NULL; |
| 296 | vt.dec = NULL; |
| 297 | vt.equal = NULL; |
| 298 | dm_array_info_init(&cmd->info, cmd->tm, &vt); |
| 299 | |
| 300 | if (cmd->policy_hint_size) { |
| 301 | vt.size = sizeof(__le32); |
| 302 | dm_array_info_init(&cmd->hint_info, cmd->tm, &vt); |
| 303 | } |
| 304 | } |
| 305 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 306 | static int __save_sm_root(struct dm_cache_metadata *cmd) |
| 307 | { |
| 308 | int r; |
| 309 | size_t metadata_len; |
| 310 | |
| 311 | r = dm_sm_root_size(cmd->metadata_sm, &metadata_len); |
| 312 | if (r < 0) |
| 313 | return r; |
| 314 | |
| 315 | return dm_sm_copy_root(cmd->metadata_sm, &cmd->metadata_space_map_root, |
| 316 | metadata_len); |
| 317 | } |
| 318 | |
| 319 | static void __copy_sm_root(struct dm_cache_metadata *cmd, |
| 320 | struct cache_disk_superblock *disk_super) |
| 321 | { |
| 322 | memcpy(&disk_super->metadata_space_map_root, |
| 323 | &cmd->metadata_space_map_root, |
| 324 | sizeof(cmd->metadata_space_map_root)); |
| 325 | } |
| 326 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 327 | static bool separate_dirty_bits(struct dm_cache_metadata *cmd) |
| 328 | { |
| 329 | return cmd->version >= 2; |
| 330 | } |
| 331 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 332 | static int __write_initial_superblock(struct dm_cache_metadata *cmd) |
| 333 | { |
| 334 | int r; |
| 335 | struct dm_block *sblock; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 336 | struct cache_disk_superblock *disk_super; |
| 337 | sector_t bdev_size = i_size_read(cmd->bdev->bd_inode) >> SECTOR_SHIFT; |
| 338 | |
| 339 | /* FIXME: see if we can lose the max sectors limit */ |
| 340 | if (bdev_size > DM_CACHE_METADATA_MAX_SECTORS) |
| 341 | bdev_size = DM_CACHE_METADATA_MAX_SECTORS; |
| 342 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 343 | r = dm_tm_pre_commit(cmd->tm); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 344 | if (r < 0) |
| 345 | return r; |
| 346 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 347 | /* |
| 348 | * dm_sm_copy_root() can fail. So we need to do it before we start |
| 349 | * updating the superblock. |
| 350 | */ |
| 351 | r = __save_sm_root(cmd); |
| 352 | if (r) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 353 | return r; |
| 354 | |
| 355 | r = superblock_lock_zero(cmd, &sblock); |
| 356 | if (r) |
| 357 | return r; |
| 358 | |
| 359 | disk_super = dm_block_data(sblock); |
| 360 | disk_super->flags = 0; |
| 361 | memset(disk_super->uuid, 0, sizeof(disk_super->uuid)); |
| 362 | disk_super->magic = cpu_to_le64(CACHE_SUPERBLOCK_MAGIC); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 363 | disk_super->version = cpu_to_le32(cmd->version); |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 364 | memset(disk_super->policy_name, 0, sizeof(disk_super->policy_name)); |
| 365 | memset(disk_super->policy_version, 0, sizeof(disk_super->policy_version)); |
Mike Snitzer | fd2fa95 | 2018-08-02 16:08:52 -0400 | [diff] [blame] | 366 | disk_super->policy_hint_size = cpu_to_le32(0); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 367 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 368 | __copy_sm_root(cmd, disk_super); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 369 | |
| 370 | disk_super->mapping_root = cpu_to_le64(cmd->root); |
| 371 | disk_super->hint_root = cpu_to_le64(cmd->hint_root); |
| 372 | disk_super->discard_root = cpu_to_le64(cmd->discard_root); |
| 373 | disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 374 | disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks)); |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 375 | disk_super->metadata_block_size = cpu_to_le32(DM_CACHE_METADATA_BLOCK_SIZE); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 376 | disk_super->data_block_size = cpu_to_le32(cmd->data_block_size); |
| 377 | disk_super->cache_blocks = cpu_to_le32(0); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 378 | |
| 379 | disk_super->read_hits = cpu_to_le32(0); |
| 380 | disk_super->read_misses = cpu_to_le32(0); |
| 381 | disk_super->write_hits = cpu_to_le32(0); |
| 382 | disk_super->write_misses = cpu_to_le32(0); |
| 383 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 384 | if (separate_dirty_bits(cmd)) |
| 385 | disk_super->dirty_root = cpu_to_le64(cmd->dirty_root); |
| 386 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 387 | return dm_tm_commit(cmd->tm, sblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static int __format_metadata(struct dm_cache_metadata *cmd) |
| 391 | { |
| 392 | int r; |
| 393 | |
| 394 | r = dm_tm_create_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION, |
| 395 | &cmd->tm, &cmd->metadata_sm); |
| 396 | if (r < 0) { |
| 397 | DMERR("tm_create_with_sm failed"); |
| 398 | return r; |
| 399 | } |
| 400 | |
| 401 | __setup_mapping_info(cmd); |
| 402 | |
| 403 | r = dm_array_empty(&cmd->info, &cmd->root); |
| 404 | if (r < 0) |
| 405 | goto bad; |
| 406 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 407 | if (separate_dirty_bits(cmd)) { |
| 408 | dm_disk_bitset_init(cmd->tm, &cmd->dirty_info); |
| 409 | r = dm_bitset_empty(&cmd->dirty_info, &cmd->dirty_root); |
| 410 | if (r < 0) |
| 411 | goto bad; |
| 412 | } |
| 413 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 414 | dm_disk_bitset_init(cmd->tm, &cmd->discard_info); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 415 | r = dm_bitset_empty(&cmd->discard_info, &cmd->discard_root); |
| 416 | if (r < 0) |
| 417 | goto bad; |
| 418 | |
| 419 | cmd->discard_block_size = 0; |
| 420 | cmd->discard_nr_blocks = 0; |
| 421 | |
| 422 | r = __write_initial_superblock(cmd); |
| 423 | if (r) |
| 424 | goto bad; |
| 425 | |
| 426 | cmd->clean_when_opened = true; |
| 427 | return 0; |
| 428 | |
| 429 | bad: |
| 430 | dm_tm_destroy(cmd->tm); |
| 431 | dm_sm_destroy(cmd->metadata_sm); |
| 432 | |
| 433 | return r; |
| 434 | } |
| 435 | |
| 436 | static int __check_incompat_features(struct cache_disk_superblock *disk_super, |
| 437 | struct dm_cache_metadata *cmd) |
| 438 | { |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 439 | uint32_t incompat_flags, features; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 440 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 441 | incompat_flags = le32_to_cpu(disk_super->incompat_flags); |
| 442 | features = incompat_flags & ~DM_CACHE_FEATURE_INCOMPAT_SUPP; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 443 | if (features) { |
| 444 | DMERR("could not access metadata due to unsupported optional features (%lx).", |
| 445 | (unsigned long)features); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Check for read-only metadata to skip the following RDWR checks. |
| 451 | */ |
| 452 | if (get_disk_ro(cmd->bdev->bd_disk)) |
| 453 | return 0; |
| 454 | |
| 455 | features = le32_to_cpu(disk_super->compat_ro_flags) & ~DM_CACHE_FEATURE_COMPAT_RO_SUPP; |
| 456 | if (features) { |
| 457 | DMERR("could not access metadata RDWR due to unsupported optional features (%lx).", |
| 458 | (unsigned long)features); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int __open_metadata(struct dm_cache_metadata *cmd) |
| 466 | { |
| 467 | int r; |
| 468 | struct dm_block *sblock; |
| 469 | struct cache_disk_superblock *disk_super; |
| 470 | unsigned long sb_flags; |
| 471 | |
| 472 | r = superblock_read_lock(cmd, &sblock); |
| 473 | if (r < 0) { |
| 474 | DMERR("couldn't read lock superblock"); |
| 475 | return r; |
| 476 | } |
| 477 | |
| 478 | disk_super = dm_block_data(sblock); |
| 479 | |
Mike Snitzer | 048e5a0 | 2014-07-14 16:59:39 -0400 | [diff] [blame] | 480 | /* Verify the data block size hasn't changed */ |
| 481 | if (le32_to_cpu(disk_super->data_block_size) != cmd->data_block_size) { |
| 482 | DMERR("changing the data block size (from %u to %llu) is not supported", |
| 483 | le32_to_cpu(disk_super->data_block_size), |
| 484 | (unsigned long long)cmd->data_block_size); |
| 485 | r = -EINVAL; |
| 486 | goto bad; |
| 487 | } |
| 488 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 489 | r = __check_incompat_features(disk_super, cmd); |
| 490 | if (r < 0) |
| 491 | goto bad; |
| 492 | |
| 493 | r = dm_tm_open_with_sm(cmd->bm, CACHE_SUPERBLOCK_LOCATION, |
| 494 | disk_super->metadata_space_map_root, |
| 495 | sizeof(disk_super->metadata_space_map_root), |
| 496 | &cmd->tm, &cmd->metadata_sm); |
| 497 | if (r < 0) { |
| 498 | DMERR("tm_open_with_sm failed"); |
| 499 | goto bad; |
| 500 | } |
| 501 | |
| 502 | __setup_mapping_info(cmd); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 503 | dm_disk_bitset_init(cmd->tm, &cmd->dirty_info); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 504 | dm_disk_bitset_init(cmd->tm, &cmd->discard_info); |
| 505 | sb_flags = le32_to_cpu(disk_super->flags); |
| 506 | cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags); |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 507 | dm_bm_unlock(sblock); |
| 508 | |
| 509 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 510 | |
| 511 | bad: |
| 512 | dm_bm_unlock(sblock); |
| 513 | return r; |
| 514 | } |
| 515 | |
| 516 | static int __open_or_format_metadata(struct dm_cache_metadata *cmd, |
| 517 | bool format_device) |
| 518 | { |
Joe Thornber | dd8b0c2 | 2013-10-24 14:10:28 -0400 | [diff] [blame] | 519 | int r; |
| 520 | bool unformatted = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 521 | |
| 522 | r = __superblock_all_zeroes(cmd->bm, &unformatted); |
| 523 | if (r) |
| 524 | return r; |
| 525 | |
| 526 | if (unformatted) |
| 527 | return format_device ? __format_metadata(cmd) : -EPERM; |
| 528 | |
| 529 | return __open_metadata(cmd); |
| 530 | } |
| 531 | |
| 532 | static int __create_persistent_data_objects(struct dm_cache_metadata *cmd, |
| 533 | bool may_format_device) |
| 534 | { |
| 535 | int r; |
Mike Snitzer | 895b47d | 2014-07-14 15:37:18 -0400 | [diff] [blame] | 536 | cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 537 | CACHE_MAX_CONCURRENT_LOCKS); |
| 538 | if (IS_ERR(cmd->bm)) { |
| 539 | DMERR("could not create block manager"); |
| 540 | return PTR_ERR(cmd->bm); |
| 541 | } |
| 542 | |
| 543 | r = __open_or_format_metadata(cmd, may_format_device); |
| 544 | if (r) |
| 545 | dm_block_manager_destroy(cmd->bm); |
| 546 | |
| 547 | return r; |
| 548 | } |
| 549 | |
| 550 | static void __destroy_persistent_data_objects(struct dm_cache_metadata *cmd) |
| 551 | { |
| 552 | dm_sm_destroy(cmd->metadata_sm); |
| 553 | dm_tm_destroy(cmd->tm); |
| 554 | dm_block_manager_destroy(cmd->bm); |
| 555 | } |
| 556 | |
| 557 | typedef unsigned long (*flags_mutator)(unsigned long); |
| 558 | |
| 559 | static void update_flags(struct cache_disk_superblock *disk_super, |
| 560 | flags_mutator mutator) |
| 561 | { |
| 562 | uint32_t sb_flags = mutator(le32_to_cpu(disk_super->flags)); |
| 563 | disk_super->flags = cpu_to_le32(sb_flags); |
| 564 | } |
| 565 | |
| 566 | static unsigned long set_clean_shutdown(unsigned long flags) |
| 567 | { |
| 568 | set_bit(CLEAN_SHUTDOWN, &flags); |
| 569 | return flags; |
| 570 | } |
| 571 | |
| 572 | static unsigned long clear_clean_shutdown(unsigned long flags) |
| 573 | { |
| 574 | clear_bit(CLEAN_SHUTDOWN, &flags); |
| 575 | return flags; |
| 576 | } |
| 577 | |
| 578 | static void read_superblock_fields(struct dm_cache_metadata *cmd, |
| 579 | struct cache_disk_superblock *disk_super) |
| 580 | { |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 581 | cmd->version = le32_to_cpu(disk_super->version); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 582 | cmd->flags = le32_to_cpu(disk_super->flags); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 583 | cmd->root = le64_to_cpu(disk_super->mapping_root); |
| 584 | cmd->hint_root = le64_to_cpu(disk_super->hint_root); |
| 585 | cmd->discard_root = le64_to_cpu(disk_super->discard_root); |
| 586 | cmd->discard_block_size = le64_to_cpu(disk_super->discard_block_size); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 587 | cmd->discard_nr_blocks = to_dblock(le64_to_cpu(disk_super->discard_nr_blocks)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 588 | cmd->data_block_size = le32_to_cpu(disk_super->data_block_size); |
| 589 | cmd->cache_blocks = to_cblock(le32_to_cpu(disk_super->cache_blocks)); |
| 590 | strncpy(cmd->policy_name, disk_super->policy_name, sizeof(cmd->policy_name)); |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 591 | cmd->policy_version[0] = le32_to_cpu(disk_super->policy_version[0]); |
| 592 | cmd->policy_version[1] = le32_to_cpu(disk_super->policy_version[1]); |
| 593 | cmd->policy_version[2] = le32_to_cpu(disk_super->policy_version[2]); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 594 | cmd->policy_hint_size = le32_to_cpu(disk_super->policy_hint_size); |
| 595 | |
| 596 | cmd->stats.read_hits = le32_to_cpu(disk_super->read_hits); |
| 597 | cmd->stats.read_misses = le32_to_cpu(disk_super->read_misses); |
| 598 | cmd->stats.write_hits = le32_to_cpu(disk_super->write_hits); |
| 599 | cmd->stats.write_misses = le32_to_cpu(disk_super->write_misses); |
| 600 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 601 | if (separate_dirty_bits(cmd)) |
| 602 | cmd->dirty_root = le64_to_cpu(disk_super->dirty_root); |
| 603 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 604 | cmd->changed = false; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * The mutator updates the superblock flags. |
| 609 | */ |
| 610 | static int __begin_transaction_flags(struct dm_cache_metadata *cmd, |
| 611 | flags_mutator mutator) |
| 612 | { |
| 613 | int r; |
| 614 | struct cache_disk_superblock *disk_super; |
| 615 | struct dm_block *sblock; |
| 616 | |
| 617 | r = superblock_lock(cmd, &sblock); |
| 618 | if (r) |
| 619 | return r; |
| 620 | |
| 621 | disk_super = dm_block_data(sblock); |
| 622 | update_flags(disk_super, mutator); |
| 623 | read_superblock_fields(cmd, disk_super); |
Joe Thornber | a9d4539 | 2014-03-27 14:13:20 +0000 | [diff] [blame] | 624 | dm_bm_unlock(sblock); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 625 | |
Joe Thornber | a9d4539 | 2014-03-27 14:13:20 +0000 | [diff] [blame] | 626 | return dm_bm_flush(cmd->bm); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static int __begin_transaction(struct dm_cache_metadata *cmd) |
| 630 | { |
| 631 | int r; |
| 632 | struct cache_disk_superblock *disk_super; |
| 633 | struct dm_block *sblock; |
| 634 | |
| 635 | /* |
| 636 | * We re-read the superblock every time. Shouldn't need to do this |
| 637 | * really. |
| 638 | */ |
| 639 | r = superblock_read_lock(cmd, &sblock); |
| 640 | if (r) |
| 641 | return r; |
| 642 | |
| 643 | disk_super = dm_block_data(sblock); |
| 644 | read_superblock_fields(cmd, disk_super); |
| 645 | dm_bm_unlock(sblock); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int __commit_transaction(struct dm_cache_metadata *cmd, |
| 651 | flags_mutator mutator) |
| 652 | { |
| 653 | int r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 654 | struct cache_disk_superblock *disk_super; |
| 655 | struct dm_block *sblock; |
| 656 | |
| 657 | /* |
| 658 | * We need to know if the cache_disk_superblock exceeds a 512-byte sector. |
| 659 | */ |
| 660 | BUILD_BUG_ON(sizeof(struct cache_disk_superblock) > 512); |
| 661 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 662 | if (separate_dirty_bits(cmd)) { |
| 663 | r = dm_bitset_flush(&cmd->dirty_info, cmd->dirty_root, |
| 664 | &cmd->dirty_root); |
| 665 | if (r) |
| 666 | return r; |
| 667 | } |
| 668 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 669 | r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, |
| 670 | &cmd->discard_root); |
| 671 | if (r) |
| 672 | return r; |
| 673 | |
| 674 | r = dm_tm_pre_commit(cmd->tm); |
| 675 | if (r < 0) |
| 676 | return r; |
| 677 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 678 | r = __save_sm_root(cmd); |
| 679 | if (r) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 680 | return r; |
| 681 | |
| 682 | r = superblock_lock(cmd, &sblock); |
| 683 | if (r) |
| 684 | return r; |
| 685 | |
| 686 | disk_super = dm_block_data(sblock); |
| 687 | |
Joe Thornber | 3201ac4 | 2015-10-22 18:10:55 +0100 | [diff] [blame] | 688 | disk_super->flags = cpu_to_le32(cmd->flags); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 689 | if (mutator) |
| 690 | update_flags(disk_super, mutator); |
| 691 | |
| 692 | disk_super->mapping_root = cpu_to_le64(cmd->root); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 693 | if (separate_dirty_bits(cmd)) |
| 694 | disk_super->dirty_root = cpu_to_le64(cmd->dirty_root); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 695 | disk_super->hint_root = cpu_to_le64(cmd->hint_root); |
| 696 | disk_super->discard_root = cpu_to_le64(cmd->discard_root); |
| 697 | disk_super->discard_block_size = cpu_to_le64(cmd->discard_block_size); |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 698 | disk_super->discard_nr_blocks = cpu_to_le64(from_dblock(cmd->discard_nr_blocks)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 699 | disk_super->cache_blocks = cpu_to_le32(from_cblock(cmd->cache_blocks)); |
| 700 | strncpy(disk_super->policy_name, cmd->policy_name, sizeof(disk_super->policy_name)); |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 701 | disk_super->policy_version[0] = cpu_to_le32(cmd->policy_version[0]); |
| 702 | disk_super->policy_version[1] = cpu_to_le32(cmd->policy_version[1]); |
| 703 | disk_super->policy_version[2] = cpu_to_le32(cmd->policy_version[2]); |
Mike Snitzer | fd2fa95 | 2018-08-02 16:08:52 -0400 | [diff] [blame] | 704 | disk_super->policy_hint_size = cpu_to_le32(cmd->policy_hint_size); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 705 | |
| 706 | disk_super->read_hits = cpu_to_le32(cmd->stats.read_hits); |
| 707 | disk_super->read_misses = cpu_to_le32(cmd->stats.read_misses); |
| 708 | disk_super->write_hits = cpu_to_le32(cmd->stats.write_hits); |
| 709 | disk_super->write_misses = cpu_to_le32(cmd->stats.write_misses); |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 710 | __copy_sm_root(cmd, disk_super); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 711 | |
| 712 | return dm_tm_commit(cmd->tm, sblock); |
| 713 | } |
| 714 | |
| 715 | /*----------------------------------------------------------------*/ |
| 716 | |
| 717 | /* |
| 718 | * The mappings are held in a dm-array that has 64-bit values stored in |
| 719 | * little-endian format. The index is the cblock, the high 48bits of the |
| 720 | * value are the oblock and the low 16 bit the flags. |
| 721 | */ |
| 722 | #define FLAGS_MASK ((1 << 16) - 1) |
| 723 | |
| 724 | static __le64 pack_value(dm_oblock_t block, unsigned flags) |
| 725 | { |
| 726 | uint64_t value = from_oblock(block); |
| 727 | value <<= 16; |
| 728 | value = value | (flags & FLAGS_MASK); |
| 729 | return cpu_to_le64(value); |
| 730 | } |
| 731 | |
| 732 | static void unpack_value(__le64 value_le, dm_oblock_t *block, unsigned *flags) |
| 733 | { |
| 734 | uint64_t value = le64_to_cpu(value_le); |
| 735 | uint64_t b = value >> 16; |
| 736 | *block = to_oblock(b); |
| 737 | *flags = value & FLAGS_MASK; |
| 738 | } |
| 739 | |
| 740 | /*----------------------------------------------------------------*/ |
| 741 | |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 742 | static struct dm_cache_metadata *metadata_open(struct block_device *bdev, |
| 743 | sector_t data_block_size, |
| 744 | bool may_format_device, |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 745 | size_t policy_hint_size, |
| 746 | unsigned metadata_version) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 747 | { |
| 748 | int r; |
| 749 | struct dm_cache_metadata *cmd; |
| 750 | |
| 751 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 752 | if (!cmd) { |
| 753 | DMERR("could not allocate metadata struct"); |
Joe Thornber | 766a788 | 2015-01-28 12:07:46 +0000 | [diff] [blame] | 754 | return ERR_PTR(-ENOMEM); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 757 | cmd->version = metadata_version; |
Elena Reshetova | 6bdd079 | 2017-10-20 10:37:37 +0300 | [diff] [blame] | 758 | refcount_set(&cmd->ref_count, 1); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 759 | init_rwsem(&cmd->root_lock); |
| 760 | cmd->bdev = bdev; |
| 761 | cmd->data_block_size = data_block_size; |
| 762 | cmd->cache_blocks = 0; |
| 763 | cmd->policy_hint_size = policy_hint_size; |
| 764 | cmd->changed = true; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 765 | cmd->fail_io = false; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 766 | |
| 767 | r = __create_persistent_data_objects(cmd, may_format_device); |
| 768 | if (r) { |
| 769 | kfree(cmd); |
| 770 | return ERR_PTR(r); |
| 771 | } |
| 772 | |
| 773 | r = __begin_transaction_flags(cmd, clear_clean_shutdown); |
| 774 | if (r < 0) { |
| 775 | dm_cache_metadata_close(cmd); |
| 776 | return ERR_PTR(r); |
| 777 | } |
| 778 | |
| 779 | return cmd; |
| 780 | } |
| 781 | |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 782 | /* |
| 783 | * We keep a little list of ref counted metadata objects to prevent two |
| 784 | * different target instances creating separate bufio instances. This is |
| 785 | * an issue if a table is reloaded before the suspend. |
| 786 | */ |
| 787 | static DEFINE_MUTEX(table_lock); |
| 788 | static LIST_HEAD(table); |
| 789 | |
| 790 | static struct dm_cache_metadata *lookup(struct block_device *bdev) |
| 791 | { |
| 792 | struct dm_cache_metadata *cmd; |
| 793 | |
| 794 | list_for_each_entry(cmd, &table, list) |
| 795 | if (cmd->bdev == bdev) { |
Elena Reshetova | 6bdd079 | 2017-10-20 10:37:37 +0300 | [diff] [blame] | 796 | refcount_inc(&cmd->ref_count); |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 797 | return cmd; |
| 798 | } |
| 799 | |
| 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | static struct dm_cache_metadata *lookup_or_open(struct block_device *bdev, |
| 804 | sector_t data_block_size, |
| 805 | bool may_format_device, |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 806 | size_t policy_hint_size, |
| 807 | unsigned metadata_version) |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 808 | { |
| 809 | struct dm_cache_metadata *cmd, *cmd2; |
| 810 | |
| 811 | mutex_lock(&table_lock); |
| 812 | cmd = lookup(bdev); |
| 813 | mutex_unlock(&table_lock); |
| 814 | |
| 815 | if (cmd) |
| 816 | return cmd; |
| 817 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 818 | cmd = metadata_open(bdev, data_block_size, may_format_device, |
| 819 | policy_hint_size, metadata_version); |
Joe Thornber | 766a788 | 2015-01-28 12:07:46 +0000 | [diff] [blame] | 820 | if (!IS_ERR(cmd)) { |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 821 | mutex_lock(&table_lock); |
| 822 | cmd2 = lookup(bdev); |
| 823 | if (cmd2) { |
| 824 | mutex_unlock(&table_lock); |
| 825 | __destroy_persistent_data_objects(cmd); |
| 826 | kfree(cmd); |
| 827 | return cmd2; |
| 828 | } |
| 829 | list_add(&cmd->list, &table); |
| 830 | mutex_unlock(&table_lock); |
| 831 | } |
| 832 | |
| 833 | return cmd; |
| 834 | } |
| 835 | |
| 836 | static bool same_params(struct dm_cache_metadata *cmd, sector_t data_block_size) |
| 837 | { |
| 838 | if (cmd->data_block_size != data_block_size) { |
Mike Snitzer | 07d9388 | 2016-10-03 11:56:22 -0400 | [diff] [blame] | 839 | DMERR("data_block_size (%llu) different from that in metadata (%llu)", |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 840 | (unsigned long long) data_block_size, |
| 841 | (unsigned long long) cmd->data_block_size); |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | struct dm_cache_metadata *dm_cache_metadata_open(struct block_device *bdev, |
| 849 | sector_t data_block_size, |
| 850 | bool may_format_device, |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 851 | size_t policy_hint_size, |
| 852 | unsigned metadata_version) |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 853 | { |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 854 | struct dm_cache_metadata *cmd = lookup_or_open(bdev, data_block_size, may_format_device, |
| 855 | policy_hint_size, metadata_version); |
Joe Thornber | 766a788 | 2015-01-28 12:07:46 +0000 | [diff] [blame] | 856 | |
| 857 | if (!IS_ERR(cmd) && !same_params(cmd, data_block_size)) { |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 858 | dm_cache_metadata_close(cmd); |
Joe Thornber | 766a788 | 2015-01-28 12:07:46 +0000 | [diff] [blame] | 859 | return ERR_PTR(-EINVAL); |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | return cmd; |
| 863 | } |
| 864 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 865 | void dm_cache_metadata_close(struct dm_cache_metadata *cmd) |
| 866 | { |
Elena Reshetova | 6bdd079 | 2017-10-20 10:37:37 +0300 | [diff] [blame] | 867 | if (refcount_dec_and_test(&cmd->ref_count)) { |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 868 | mutex_lock(&table_lock); |
| 869 | list_del(&cmd->list); |
| 870 | mutex_unlock(&table_lock); |
| 871 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 872 | if (!cmd->fail_io) |
| 873 | __destroy_persistent_data_objects(cmd); |
Joe Thornber | 9b1cc9f | 2015-01-23 10:00:07 +0000 | [diff] [blame] | 874 | kfree(cmd); |
| 875 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 878 | /* |
| 879 | * Checks that the given cache block is either unmapped or clean. |
| 880 | */ |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 881 | static int block_clean_combined_dirty(struct dm_cache_metadata *cmd, dm_cblock_t b, |
| 882 | bool *result) |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 883 | { |
| 884 | int r; |
| 885 | __le64 value; |
| 886 | dm_oblock_t ob; |
| 887 | unsigned flags; |
| 888 | |
| 889 | r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(b), &value); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 890 | if (r) |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 891 | return r; |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 892 | |
| 893 | unpack_value(value, &ob, &flags); |
| 894 | *result = !((flags & M_VALID) && (flags & M_DIRTY)); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 899 | static int blocks_are_clean_combined_dirty(struct dm_cache_metadata *cmd, |
| 900 | dm_cblock_t begin, dm_cblock_t end, |
| 901 | bool *result) |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 902 | { |
| 903 | int r; |
| 904 | *result = true; |
| 905 | |
| 906 | while (begin != end) { |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 907 | r = block_clean_combined_dirty(cmd, begin, result); |
| 908 | if (r) { |
| 909 | DMERR("block_clean_combined_dirty failed"); |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 910 | return r; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 911 | } |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 912 | |
| 913 | if (!*result) { |
| 914 | DMERR("cache block %llu is dirty", |
| 915 | (unsigned long long) from_cblock(begin)); |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | begin = to_cblock(from_cblock(begin) + 1); |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 925 | static int blocks_are_clean_separate_dirty(struct dm_cache_metadata *cmd, |
| 926 | dm_cblock_t begin, dm_cblock_t end, |
| 927 | bool *result) |
| 928 | { |
| 929 | int r; |
| 930 | bool dirty_flag; |
| 931 | *result = true; |
| 932 | |
Mike Snitzer | 687cf44 | 2018-11-09 11:56:03 -0500 | [diff] [blame] | 933 | if (from_cblock(cmd->cache_blocks) == 0) |
| 934 | /* Nothing to do */ |
| 935 | return 0; |
| 936 | |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 937 | r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root, |
Joe Thornber | 0d963b6 | 2017-03-20 11:54:11 -0400 | [diff] [blame] | 938 | from_cblock(cmd->cache_blocks), &cmd->dirty_cursor); |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 939 | if (r) { |
| 940 | DMERR("%s: dm_bitset_cursor_begin for dirty failed", __func__); |
| 941 | return r; |
| 942 | } |
| 943 | |
| 944 | r = dm_bitset_cursor_skip(&cmd->dirty_cursor, from_cblock(begin)); |
| 945 | if (r) { |
| 946 | DMERR("%s: dm_bitset_cursor_skip for dirty failed", __func__); |
| 947 | dm_bitset_cursor_end(&cmd->dirty_cursor); |
| 948 | return r; |
| 949 | } |
| 950 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 951 | while (begin != end) { |
| 952 | /* |
| 953 | * We assume that unmapped blocks have their dirty bit |
| 954 | * cleared. |
| 955 | */ |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 956 | dirty_flag = dm_bitset_cursor_get_value(&cmd->dirty_cursor); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 957 | if (dirty_flag) { |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 958 | DMERR("%s: cache block %llu is dirty", __func__, |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 959 | (unsigned long long) from_cblock(begin)); |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 960 | dm_bitset_cursor_end(&cmd->dirty_cursor); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 961 | *result = false; |
| 962 | return 0; |
| 963 | } |
| 964 | |
Joe Thornber | 0d963b6 | 2017-03-20 11:54:11 -0400 | [diff] [blame] | 965 | begin = to_cblock(from_cblock(begin) + 1); |
| 966 | if (begin == end) |
| 967 | break; |
| 968 | |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 969 | r = dm_bitset_cursor_next(&cmd->dirty_cursor); |
| 970 | if (r) { |
| 971 | DMERR("%s: dm_bitset_cursor_next for dirty failed", __func__); |
| 972 | dm_bitset_cursor_end(&cmd->dirty_cursor); |
| 973 | return r; |
| 974 | } |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 975 | } |
| 976 | |
Mike Snitzer | 7f1b215 | 2016-10-04 15:00:47 -0400 | [diff] [blame] | 977 | dm_bitset_cursor_end(&cmd->dirty_cursor); |
| 978 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int blocks_are_unmapped_or_clean(struct dm_cache_metadata *cmd, |
| 983 | dm_cblock_t begin, dm_cblock_t end, |
| 984 | bool *result) |
| 985 | { |
| 986 | if (separate_dirty_bits(cmd)) |
| 987 | return blocks_are_clean_separate_dirty(cmd, begin, end, result); |
| 988 | else |
| 989 | return blocks_are_clean_combined_dirty(cmd, begin, end, result); |
| 990 | } |
| 991 | |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 992 | static bool cmd_write_lock(struct dm_cache_metadata *cmd) |
| 993 | { |
| 994 | down_write(&cmd->root_lock); |
| 995 | if (cmd->fail_io || dm_bm_is_read_only(cmd->bm)) { |
| 996 | up_write(&cmd->root_lock); |
| 997 | return false; |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 998 | } |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 999 | return true; |
| 1000 | } |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1001 | |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1002 | #define WRITE_LOCK(cmd) \ |
| 1003 | do { \ |
| 1004 | if (!cmd_write_lock((cmd))) \ |
| 1005 | return -EINVAL; \ |
| 1006 | } while(0) |
| 1007 | |
| 1008 | #define WRITE_LOCK_VOID(cmd) \ |
| 1009 | do { \ |
| 1010 | if (!cmd_write_lock((cmd))) \ |
| 1011 | return; \ |
| 1012 | } while(0) |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1013 | |
| 1014 | #define WRITE_UNLOCK(cmd) \ |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1015 | up_write(&(cmd)->root_lock) |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1016 | |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1017 | static bool cmd_read_lock(struct dm_cache_metadata *cmd) |
| 1018 | { |
Ahmed Samy | 6545b60 | 2016-04-17 05:37:09 +0000 | [diff] [blame] | 1019 | down_read(&cmd->root_lock); |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1020 | if (cmd->fail_io) { |
Ahmed Samy | 6545b60 | 2016-04-17 05:37:09 +0000 | [diff] [blame] | 1021 | up_read(&cmd->root_lock); |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1022 | return false; |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1023 | } |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1024 | return true; |
| 1025 | } |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1026 | |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1027 | #define READ_LOCK(cmd) \ |
| 1028 | do { \ |
| 1029 | if (!cmd_read_lock((cmd))) \ |
| 1030 | return -EINVAL; \ |
| 1031 | } while(0) |
| 1032 | |
| 1033 | #define READ_LOCK_VOID(cmd) \ |
| 1034 | do { \ |
| 1035 | if (!cmd_read_lock((cmd))) \ |
| 1036 | return; \ |
| 1037 | } while(0) |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1038 | |
| 1039 | #define READ_UNLOCK(cmd) \ |
Mike Snitzer | 9567366 | 2016-04-12 12:14:46 -0400 | [diff] [blame] | 1040 | up_read(&(cmd)->root_lock) |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1041 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1042 | int dm_cache_resize(struct dm_cache_metadata *cmd, dm_cblock_t new_cache_size) |
| 1043 | { |
| 1044 | int r; |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 1045 | bool clean; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1046 | __le64 null_mapping = pack_value(0, 0); |
| 1047 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1048 | WRITE_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1049 | __dm_bless_for_disk(&null_mapping); |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 1050 | |
| 1051 | if (from_cblock(new_cache_size) < from_cblock(cmd->cache_blocks)) { |
| 1052 | r = blocks_are_unmapped_or_clean(cmd, new_cache_size, cmd->cache_blocks, &clean); |
| 1053 | if (r) { |
| 1054 | __dm_unbless_for_disk(&null_mapping); |
| 1055 | goto out; |
| 1056 | } |
| 1057 | |
| 1058 | if (!clean) { |
| 1059 | DMERR("unable to shrink cache due to dirty blocks"); |
| 1060 | r = -EINVAL; |
| 1061 | __dm_unbless_for_disk(&null_mapping); |
| 1062 | goto out; |
| 1063 | } |
| 1064 | } |
| 1065 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1066 | r = dm_array_resize(&cmd->info, cmd->root, from_cblock(cmd->cache_blocks), |
| 1067 | from_cblock(new_cache_size), |
| 1068 | &null_mapping, &cmd->root); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1069 | if (r) |
| 1070 | goto out; |
| 1071 | |
| 1072 | if (separate_dirty_bits(cmd)) { |
| 1073 | r = dm_bitset_resize(&cmd->dirty_info, cmd->dirty_root, |
| 1074 | from_cblock(cmd->cache_blocks), from_cblock(new_cache_size), |
| 1075 | false, &cmd->dirty_root); |
| 1076 | if (r) |
| 1077 | goto out; |
| 1078 | } |
| 1079 | |
| 1080 | cmd->cache_blocks = new_cache_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1081 | cmd->changed = true; |
Joe Thornber | f494a9c | 2013-10-31 13:55:49 -0400 | [diff] [blame] | 1082 | |
| 1083 | out: |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1084 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1085 | |
| 1086 | return r; |
| 1087 | } |
| 1088 | |
| 1089 | int dm_cache_discard_bitset_resize(struct dm_cache_metadata *cmd, |
| 1090 | sector_t discard_block_size, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1091 | dm_dblock_t new_nr_entries) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1092 | { |
| 1093 | int r; |
| 1094 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1095 | WRITE_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1096 | r = dm_bitset_resize(&cmd->discard_info, |
| 1097 | cmd->discard_root, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1098 | from_dblock(cmd->discard_nr_blocks), |
| 1099 | from_dblock(new_nr_entries), |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1100 | false, &cmd->discard_root); |
| 1101 | if (!r) { |
| 1102 | cmd->discard_block_size = discard_block_size; |
| 1103 | cmd->discard_nr_blocks = new_nr_entries; |
| 1104 | } |
| 1105 | |
| 1106 | cmd->changed = true; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1107 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1108 | |
| 1109 | return r; |
| 1110 | } |
| 1111 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1112 | static int __set_discard(struct dm_cache_metadata *cmd, dm_dblock_t b) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1113 | { |
| 1114 | return dm_bitset_set_bit(&cmd->discard_info, cmd->discard_root, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1115 | from_dblock(b), &cmd->discard_root); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1118 | static int __clear_discard(struct dm_cache_metadata *cmd, dm_dblock_t b) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1119 | { |
| 1120 | return dm_bitset_clear_bit(&cmd->discard_info, cmd->discard_root, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1121 | from_dblock(b), &cmd->discard_root); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1124 | static int __discard(struct dm_cache_metadata *cmd, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1125 | dm_dblock_t dblock, bool discard) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1126 | { |
| 1127 | int r; |
| 1128 | |
| 1129 | r = (discard ? __set_discard : __clear_discard)(cmd, dblock); |
| 1130 | if (r) |
| 1131 | return r; |
| 1132 | |
| 1133 | cmd->changed = true; |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | int dm_cache_set_discard(struct dm_cache_metadata *cmd, |
Joe Thornber | 1bad9bc | 2014-11-07 14:47:07 +0000 | [diff] [blame] | 1138 | dm_dblock_t dblock, bool discard) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1139 | { |
| 1140 | int r; |
| 1141 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1142 | WRITE_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1143 | r = __discard(cmd, dblock, discard); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1144 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1145 | |
| 1146 | return r; |
| 1147 | } |
| 1148 | |
| 1149 | static int __load_discards(struct dm_cache_metadata *cmd, |
| 1150 | load_discard_fn fn, void *context) |
| 1151 | { |
| 1152 | int r = 0; |
Joe Thornber | ae4a46a | 2016-10-03 14:16:20 -0400 | [diff] [blame] | 1153 | uint32_t b; |
| 1154 | struct dm_bitset_cursor c; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1155 | |
Joe Thornber | ae4a46a | 2016-10-03 14:16:20 -0400 | [diff] [blame] | 1156 | if (from_dblock(cmd->discard_nr_blocks) == 0) |
| 1157 | /* nothing to do */ |
| 1158 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1159 | |
Joe Thornber | ae4a46a | 2016-10-03 14:16:20 -0400 | [diff] [blame] | 1160 | if (cmd->clean_when_opened) { |
| 1161 | r = dm_bitset_flush(&cmd->discard_info, cmd->discard_root, &cmd->discard_root); |
| 1162 | if (r) |
| 1163 | return r; |
| 1164 | |
| 1165 | r = dm_bitset_cursor_begin(&cmd->discard_info, cmd->discard_root, |
| 1166 | from_dblock(cmd->discard_nr_blocks), &c); |
| 1167 | if (r) |
| 1168 | return r; |
| 1169 | |
| 1170 | for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) { |
| 1171 | r = fn(context, cmd->discard_block_size, to_dblock(b), |
| 1172 | dm_bitset_cursor_get_value(&c)); |
| 1173 | if (r) |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | dm_bitset_cursor_end(&c); |
| 1178 | |
| 1179 | } else { |
| 1180 | for (b = 0; b < from_dblock(cmd->discard_nr_blocks); b++) { |
| 1181 | r = fn(context, cmd->discard_block_size, to_dblock(b), false); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1182 | if (r) |
| 1183 | return r; |
Joe Thornber | ae4a46a | 2016-10-03 14:16:20 -0400 | [diff] [blame] | 1184 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | return r; |
| 1188 | } |
| 1189 | |
| 1190 | int dm_cache_load_discards(struct dm_cache_metadata *cmd, |
| 1191 | load_discard_fn fn, void *context) |
| 1192 | { |
| 1193 | int r; |
| 1194 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1195 | READ_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1196 | r = __load_discards(cmd, fn, context); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1197 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1198 | |
| 1199 | return r; |
| 1200 | } |
| 1201 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1202 | int dm_cache_size(struct dm_cache_metadata *cmd, dm_cblock_t *result) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1203 | { |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1204 | READ_LOCK(cmd); |
| 1205 | *result = cmd->cache_blocks; |
| 1206 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1207 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1208 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | static int __remove(struct dm_cache_metadata *cmd, dm_cblock_t cblock) |
| 1212 | { |
| 1213 | int r; |
| 1214 | __le64 value = pack_value(0, 0); |
| 1215 | |
| 1216 | __dm_bless_for_disk(&value); |
| 1217 | r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock), |
| 1218 | &value, &cmd->root); |
| 1219 | if (r) |
| 1220 | return r; |
| 1221 | |
| 1222 | cmd->changed = true; |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | int dm_cache_remove_mapping(struct dm_cache_metadata *cmd, dm_cblock_t cblock) |
| 1227 | { |
| 1228 | int r; |
| 1229 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1230 | WRITE_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1231 | r = __remove(cmd, cblock); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1232 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1233 | |
| 1234 | return r; |
| 1235 | } |
| 1236 | |
| 1237 | static int __insert(struct dm_cache_metadata *cmd, |
| 1238 | dm_cblock_t cblock, dm_oblock_t oblock) |
| 1239 | { |
| 1240 | int r; |
| 1241 | __le64 value = pack_value(oblock, M_VALID); |
| 1242 | __dm_bless_for_disk(&value); |
| 1243 | |
| 1244 | r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock), |
| 1245 | &value, &cmd->root); |
| 1246 | if (r) |
| 1247 | return r; |
| 1248 | |
| 1249 | cmd->changed = true; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | int dm_cache_insert_mapping(struct dm_cache_metadata *cmd, |
| 1254 | dm_cblock_t cblock, dm_oblock_t oblock) |
| 1255 | { |
| 1256 | int r; |
| 1257 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1258 | WRITE_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1259 | r = __insert(cmd, cblock, oblock); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1260 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1261 | |
| 1262 | return r; |
| 1263 | } |
| 1264 | |
| 1265 | struct thunk { |
| 1266 | load_mapping_fn fn; |
| 1267 | void *context; |
| 1268 | |
| 1269 | struct dm_cache_metadata *cmd; |
| 1270 | bool respect_dirty_flags; |
| 1271 | bool hints_valid; |
| 1272 | }; |
| 1273 | |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1274 | static bool policy_unchanged(struct dm_cache_metadata *cmd, |
| 1275 | struct dm_cache_policy *policy) |
| 1276 | { |
| 1277 | const char *policy_name = dm_cache_policy_get_name(policy); |
| 1278 | const unsigned *policy_version = dm_cache_policy_get_version(policy); |
| 1279 | size_t policy_hint_size = dm_cache_policy_get_hint_size(policy); |
| 1280 | |
| 1281 | /* |
| 1282 | * Ensure policy names match. |
| 1283 | */ |
| 1284 | if (strncmp(cmd->policy_name, policy_name, sizeof(cmd->policy_name))) |
| 1285 | return false; |
| 1286 | |
| 1287 | /* |
| 1288 | * Ensure policy major versions match. |
| 1289 | */ |
| 1290 | if (cmd->policy_version[0] != policy_version[0]) |
| 1291 | return false; |
| 1292 | |
| 1293 | /* |
| 1294 | * Ensure policy hint sizes match. |
| 1295 | */ |
| 1296 | if (cmd->policy_hint_size != policy_hint_size) |
| 1297 | return false; |
| 1298 | |
| 1299 | return true; |
| 1300 | } |
| 1301 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1302 | static bool hints_array_initialized(struct dm_cache_metadata *cmd) |
| 1303 | { |
| 1304 | return cmd->hint_root && cmd->policy_hint_size; |
| 1305 | } |
| 1306 | |
| 1307 | static bool hints_array_available(struct dm_cache_metadata *cmd, |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1308 | struct dm_cache_policy *policy) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1309 | { |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1310 | return cmd->clean_when_opened && policy_unchanged(cmd, policy) && |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1311 | hints_array_initialized(cmd); |
| 1312 | } |
| 1313 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1314 | static int __load_mapping_v1(struct dm_cache_metadata *cmd, |
| 1315 | uint64_t cb, bool hints_valid, |
| 1316 | struct dm_array_cursor *mapping_cursor, |
| 1317 | struct dm_array_cursor *hint_cursor, |
| 1318 | load_mapping_fn fn, void *context) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1319 | { |
| 1320 | int r = 0; |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1321 | |
| 1322 | __le64 mapping; |
| 1323 | __le32 hint = 0; |
| 1324 | |
| 1325 | __le64 *mapping_value_le; |
| 1326 | __le32 *hint_value_le; |
| 1327 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1328 | dm_oblock_t oblock; |
| 1329 | unsigned flags; |
Ilya Dryomov | 5b1fe7b | 2018-08-09 12:38:28 +0200 | [diff] [blame] | 1330 | bool dirty = true; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1331 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1332 | dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le); |
| 1333 | memcpy(&mapping, mapping_value_le, sizeof(mapping)); |
| 1334 | unpack_value(mapping, &oblock, &flags); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (flags & M_VALID) { |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1337 | if (hints_valid) { |
| 1338 | dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le); |
| 1339 | memcpy(&hint, hint_value_le, sizeof(hint)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1340 | } |
Ilya Dryomov | 5b1fe7b | 2018-08-09 12:38:28 +0200 | [diff] [blame] | 1341 | if (cmd->clean_when_opened) |
| 1342 | dirty = flags & M_DIRTY; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1343 | |
Ilya Dryomov | 5b1fe7b | 2018-08-09 12:38:28 +0200 | [diff] [blame] | 1344 | r = fn(context, oblock, to_cblock(cb), dirty, |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1345 | le32_to_cpu(hint), hints_valid); |
Mike Snitzer | 4855105 | 2016-10-04 15:22:17 -0400 | [diff] [blame] | 1346 | if (r) { |
| 1347 | DMERR("policy couldn't load cache block %llu", |
| 1348 | (unsigned long long) from_cblock(to_cblock(cb))); |
| 1349 | } |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return r; |
| 1353 | } |
| 1354 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1355 | static int __load_mapping_v2(struct dm_cache_metadata *cmd, |
| 1356 | uint64_t cb, bool hints_valid, |
| 1357 | struct dm_array_cursor *mapping_cursor, |
| 1358 | struct dm_array_cursor *hint_cursor, |
| 1359 | struct dm_bitset_cursor *dirty_cursor, |
| 1360 | load_mapping_fn fn, void *context) |
| 1361 | { |
| 1362 | int r = 0; |
| 1363 | |
| 1364 | __le64 mapping; |
| 1365 | __le32 hint = 0; |
| 1366 | |
| 1367 | __le64 *mapping_value_le; |
| 1368 | __le32 *hint_value_le; |
| 1369 | |
| 1370 | dm_oblock_t oblock; |
| 1371 | unsigned flags; |
Ilya Dryomov | 5b1fe7b | 2018-08-09 12:38:28 +0200 | [diff] [blame] | 1372 | bool dirty = true; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1373 | |
| 1374 | dm_array_cursor_get_value(mapping_cursor, (void **) &mapping_value_le); |
| 1375 | memcpy(&mapping, mapping_value_le, sizeof(mapping)); |
| 1376 | unpack_value(mapping, &oblock, &flags); |
| 1377 | |
| 1378 | if (flags & M_VALID) { |
| 1379 | if (hints_valid) { |
| 1380 | dm_array_cursor_get_value(hint_cursor, (void **) &hint_value_le); |
| 1381 | memcpy(&hint, hint_value_le, sizeof(hint)); |
| 1382 | } |
Ilya Dryomov | 5b1fe7b | 2018-08-09 12:38:28 +0200 | [diff] [blame] | 1383 | if (cmd->clean_when_opened) |
| 1384 | dirty = dm_bitset_cursor_get_value(dirty_cursor); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1385 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1386 | r = fn(context, oblock, to_cblock(cb), dirty, |
| 1387 | le32_to_cpu(hint), hints_valid); |
Mike Snitzer | 4855105 | 2016-10-04 15:22:17 -0400 | [diff] [blame] | 1388 | if (r) { |
| 1389 | DMERR("policy couldn't load cache block %llu", |
| 1390 | (unsigned long long) from_cblock(to_cblock(cb))); |
| 1391 | } |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | return r; |
| 1395 | } |
| 1396 | |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1397 | static int __load_mappings(struct dm_cache_metadata *cmd, |
| 1398 | struct dm_cache_policy *policy, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1399 | load_mapping_fn fn, void *context) |
| 1400 | { |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1401 | int r; |
| 1402 | uint64_t cb; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1403 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1404 | bool hints_valid = hints_array_available(cmd, policy); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1405 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1406 | if (from_cblock(cmd->cache_blocks) == 0) |
| 1407 | /* Nothing to do */ |
| 1408 | return 0; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1409 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1410 | r = dm_array_cursor_begin(&cmd->info, cmd->root, &cmd->mapping_cursor); |
| 1411 | if (r) |
| 1412 | return r; |
| 1413 | |
| 1414 | if (hints_valid) { |
| 1415 | r = dm_array_cursor_begin(&cmd->hint_info, cmd->hint_root, &cmd->hint_cursor); |
| 1416 | if (r) { |
| 1417 | dm_array_cursor_end(&cmd->mapping_cursor); |
| 1418 | return r; |
| 1419 | } |
| 1420 | } |
| 1421 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1422 | if (separate_dirty_bits(cmd)) { |
| 1423 | r = dm_bitset_cursor_begin(&cmd->dirty_info, cmd->dirty_root, |
| 1424 | from_cblock(cmd->cache_blocks), |
| 1425 | &cmd->dirty_cursor); |
| 1426 | if (r) { |
| 1427 | dm_array_cursor_end(&cmd->hint_cursor); |
| 1428 | dm_array_cursor_end(&cmd->mapping_cursor); |
| 1429 | return r; |
| 1430 | } |
| 1431 | } |
| 1432 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1433 | for (cb = 0; ; cb++) { |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1434 | if (separate_dirty_bits(cmd)) |
| 1435 | r = __load_mapping_v2(cmd, cb, hints_valid, |
| 1436 | &cmd->mapping_cursor, |
| 1437 | &cmd->hint_cursor, |
| 1438 | &cmd->dirty_cursor, |
| 1439 | fn, context); |
| 1440 | else |
| 1441 | r = __load_mapping_v1(cmd, cb, hints_valid, |
| 1442 | &cmd->mapping_cursor, &cmd->hint_cursor, |
| 1443 | fn, context); |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1444 | if (r) |
| 1445 | goto out; |
| 1446 | |
| 1447 | /* |
| 1448 | * We need to break out before we move the cursors. |
| 1449 | */ |
| 1450 | if (cb >= (from_cblock(cmd->cache_blocks) - 1)) |
| 1451 | break; |
| 1452 | |
| 1453 | r = dm_array_cursor_next(&cmd->mapping_cursor); |
| 1454 | if (r) { |
| 1455 | DMERR("dm_array_cursor_next for mapping failed"); |
| 1456 | goto out; |
| 1457 | } |
| 1458 | |
| 1459 | if (hints_valid) { |
| 1460 | r = dm_array_cursor_next(&cmd->hint_cursor); |
| 1461 | if (r) { |
Joe Thornber | 4561ffc | 2018-09-24 16:19:30 -0400 | [diff] [blame] | 1462 | dm_array_cursor_end(&cmd->hint_cursor); |
| 1463 | hints_valid = false; |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1464 | } |
| 1465 | } |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1466 | |
| 1467 | if (separate_dirty_bits(cmd)) { |
| 1468 | r = dm_bitset_cursor_next(&cmd->dirty_cursor); |
| 1469 | if (r) { |
| 1470 | DMERR("dm_bitset_cursor_next for dirty failed"); |
| 1471 | goto out; |
| 1472 | } |
| 1473 | } |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1474 | } |
| 1475 | out: |
| 1476 | dm_array_cursor_end(&cmd->mapping_cursor); |
| 1477 | if (hints_valid) |
| 1478 | dm_array_cursor_end(&cmd->hint_cursor); |
| 1479 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1480 | if (separate_dirty_bits(cmd)) |
| 1481 | dm_bitset_cursor_end(&cmd->dirty_cursor); |
| 1482 | |
Joe Thornber | f177940 | 2016-09-20 06:16:00 -0400 | [diff] [blame] | 1483 | return r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1486 | int dm_cache_load_mappings(struct dm_cache_metadata *cmd, |
| 1487 | struct dm_cache_policy *policy, |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1488 | load_mapping_fn fn, void *context) |
| 1489 | { |
| 1490 | int r; |
| 1491 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1492 | READ_LOCK(cmd); |
Mike Snitzer | ea2dd8c | 2013-03-20 17:21:28 +0000 | [diff] [blame] | 1493 | r = __load_mappings(cmd, policy, fn, context); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1494 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1495 | |
| 1496 | return r; |
| 1497 | } |
| 1498 | |
| 1499 | static int __dump_mapping(void *context, uint64_t cblock, void *leaf) |
| 1500 | { |
| 1501 | int r = 0; |
| 1502 | __le64 value; |
| 1503 | dm_oblock_t oblock; |
| 1504 | unsigned flags; |
| 1505 | |
| 1506 | memcpy(&value, leaf, sizeof(value)); |
| 1507 | unpack_value(value, &oblock, &flags); |
| 1508 | |
| 1509 | return r; |
| 1510 | } |
| 1511 | |
| 1512 | static int __dump_mappings(struct dm_cache_metadata *cmd) |
| 1513 | { |
| 1514 | return dm_array_walk(&cmd->info, cmd->root, __dump_mapping, NULL); |
| 1515 | } |
| 1516 | |
| 1517 | void dm_cache_dump(struct dm_cache_metadata *cmd) |
| 1518 | { |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1519 | READ_LOCK_VOID(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1520 | __dump_mappings(cmd); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1521 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | int dm_cache_changed_this_transaction(struct dm_cache_metadata *cmd) |
| 1525 | { |
| 1526 | int r; |
| 1527 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1528 | READ_LOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1529 | r = cmd->changed; |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1530 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1531 | |
| 1532 | return r; |
| 1533 | } |
| 1534 | |
| 1535 | static int __dirty(struct dm_cache_metadata *cmd, dm_cblock_t cblock, bool dirty) |
| 1536 | { |
| 1537 | int r; |
| 1538 | unsigned flags; |
| 1539 | dm_oblock_t oblock; |
| 1540 | __le64 value; |
| 1541 | |
| 1542 | r = dm_array_get_value(&cmd->info, cmd->root, from_cblock(cblock), &value); |
| 1543 | if (r) |
| 1544 | return r; |
| 1545 | |
| 1546 | unpack_value(value, &oblock, &flags); |
| 1547 | |
| 1548 | if (((flags & M_DIRTY) && dirty) || (!(flags & M_DIRTY) && !dirty)) |
| 1549 | /* nothing to be done */ |
| 1550 | return 0; |
| 1551 | |
Joe Thornber | 79ed9ca | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1552 | value = pack_value(oblock, (flags & ~M_DIRTY) | (dirty ? M_DIRTY : 0)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1553 | __dm_bless_for_disk(&value); |
| 1554 | |
| 1555 | r = dm_array_set_value(&cmd->info, cmd->root, from_cblock(cblock), |
| 1556 | &value, &cmd->root); |
| 1557 | if (r) |
| 1558 | return r; |
| 1559 | |
| 1560 | cmd->changed = true; |
| 1561 | return 0; |
| 1562 | |
| 1563 | } |
| 1564 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1565 | static int __set_dirty_bits_v1(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits) |
| 1566 | { |
| 1567 | int r; |
| 1568 | unsigned i; |
| 1569 | for (i = 0; i < nr_bits; i++) { |
| 1570 | r = __dirty(cmd, to_cblock(i), test_bit(i, bits)); |
| 1571 | if (r) |
| 1572 | return r; |
| 1573 | } |
| 1574 | |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Joe Thornber | 683bb1a | 2016-09-22 10:45:21 -0400 | [diff] [blame] | 1578 | static int is_dirty_callback(uint32_t index, bool *value, void *context) |
| 1579 | { |
| 1580 | unsigned long *bits = context; |
| 1581 | *value = test_bit(index, bits); |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1585 | static int __set_dirty_bits_v2(struct dm_cache_metadata *cmd, unsigned nr_bits, unsigned long *bits) |
| 1586 | { |
| 1587 | int r = 0; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1588 | |
| 1589 | /* nr_bits is really just a sanity check */ |
| 1590 | if (nr_bits != from_cblock(cmd->cache_blocks)) { |
| 1591 | DMERR("dirty bitset is wrong size"); |
| 1592 | return -EINVAL; |
| 1593 | } |
| 1594 | |
Joe Thornber | 683bb1a | 2016-09-22 10:45:21 -0400 | [diff] [blame] | 1595 | r = dm_bitset_del(&cmd->dirty_info, cmd->dirty_root); |
| 1596 | if (r) |
| 1597 | return r; |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1598 | |
| 1599 | cmd->changed = true; |
Joe Thornber | 683bb1a | 2016-09-22 10:45:21 -0400 | [diff] [blame] | 1600 | return dm_bitset_new(&cmd->dirty_info, &cmd->dirty_root, nr_bits, is_dirty_callback, bits); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | int dm_cache_set_dirty_bits(struct dm_cache_metadata *cmd, |
| 1604 | unsigned nr_bits, |
| 1605 | unsigned long *bits) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1606 | { |
| 1607 | int r; |
| 1608 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1609 | WRITE_LOCK(cmd); |
Joe Thornber | 629d0a8 | 2016-09-22 06:15:21 -0400 | [diff] [blame] | 1610 | if (separate_dirty_bits(cmd)) |
| 1611 | r = __set_dirty_bits_v2(cmd, nr_bits, bits); |
| 1612 | else |
| 1613 | r = __set_dirty_bits_v1(cmd, nr_bits, bits); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1614 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1615 | |
| 1616 | return r; |
| 1617 | } |
| 1618 | |
| 1619 | void dm_cache_metadata_get_stats(struct dm_cache_metadata *cmd, |
| 1620 | struct dm_cache_statistics *stats) |
| 1621 | { |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1622 | READ_LOCK_VOID(cmd); |
Joe Thornber | 8c5008f | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1623 | *stats = cmd->stats; |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1624 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | void dm_cache_metadata_set_stats(struct dm_cache_metadata *cmd, |
| 1628 | struct dm_cache_statistics *stats) |
| 1629 | { |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1630 | WRITE_LOCK_VOID(cmd); |
Joe Thornber | 8c5008f | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1631 | cmd->stats = *stats; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1632 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | int dm_cache_commit(struct dm_cache_metadata *cmd, bool clean_shutdown) |
| 1636 | { |
Mike Snitzer | 10add84e | 2017-05-05 14:40:13 -0400 | [diff] [blame] | 1637 | int r = -EINVAL; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1638 | flags_mutator mutator = (clean_shutdown ? set_clean_shutdown : |
| 1639 | clear_clean_shutdown); |
| 1640 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1641 | WRITE_LOCK(cmd); |
Mike Snitzer | 10add84e | 2017-05-05 14:40:13 -0400 | [diff] [blame] | 1642 | if (cmd->fail_io) |
| 1643 | goto out; |
| 1644 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1645 | r = __commit_transaction(cmd, mutator); |
| 1646 | if (r) |
| 1647 | goto out; |
| 1648 | |
| 1649 | r = __begin_transaction(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1650 | out: |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1651 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1652 | return r; |
| 1653 | } |
| 1654 | |
| 1655 | int dm_cache_get_free_metadata_block_count(struct dm_cache_metadata *cmd, |
| 1656 | dm_block_t *result) |
| 1657 | { |
| 1658 | int r = -EINVAL; |
| 1659 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1660 | READ_LOCK(cmd); |
Mike Snitzer | 10add84e | 2017-05-05 14:40:13 -0400 | [diff] [blame] | 1661 | if (!cmd->fail_io) |
| 1662 | r = dm_sm_get_nr_free(cmd->metadata_sm, result); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1663 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1664 | |
| 1665 | return r; |
| 1666 | } |
| 1667 | |
| 1668 | int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd, |
| 1669 | dm_block_t *result) |
| 1670 | { |
| 1671 | int r = -EINVAL; |
| 1672 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1673 | READ_LOCK(cmd); |
Mike Snitzer | 10add84e | 2017-05-05 14:40:13 -0400 | [diff] [blame] | 1674 | if (!cmd->fail_io) |
| 1675 | r = dm_sm_get_nr_blocks(cmd->metadata_sm, result); |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1676 | READ_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1677 | |
| 1678 | return r; |
| 1679 | } |
| 1680 | |
| 1681 | /*----------------------------------------------------------------*/ |
| 1682 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 1683 | static int get_hint(uint32_t index, void *value_le, void *context) |
| 1684 | { |
| 1685 | uint32_t value; |
| 1686 | struct dm_cache_policy *policy = context; |
| 1687 | |
| 1688 | value = policy_get_hint(policy, to_cblock(index)); |
| 1689 | *((__le32 *) value_le) = cpu_to_le32(value); |
| 1690 | |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * It's quicker to always delete the hint array, and recreate with |
| 1696 | * dm_array_new(). |
| 1697 | */ |
| 1698 | static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy) |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1699 | { |
| 1700 | int r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1701 | size_t hint_size; |
| 1702 | const char *policy_name = dm_cache_policy_get_name(policy); |
Mike Snitzer | 4e7f506 | 2013-03-20 17:21:27 +0000 | [diff] [blame] | 1703 | const unsigned *policy_version = dm_cache_policy_get_version(policy); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (!policy_name[0] || |
| 1706 | (strlen(policy_name) > sizeof(cmd->policy_name) - 1)) |
| 1707 | return -EINVAL; |
| 1708 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 1709 | strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name)); |
| 1710 | memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version)); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1711 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 1712 | hint_size = dm_cache_policy_get_hint_size(policy); |
| 1713 | if (!hint_size) |
| 1714 | return 0; /* short-circuit hints initialization */ |
| 1715 | cmd->policy_hint_size = hint_size; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1716 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 1717 | if (cmd->hint_root) { |
| 1718 | r = dm_array_del(&cmd->hint_info, cmd->hint_root); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1719 | if (r) |
| 1720 | return r; |
| 1721 | } |
| 1722 | |
Joe Thornber | 4e781b4 | 2016-09-15 09:23:46 -0400 | [diff] [blame] | 1723 | return dm_array_new(&cmd->hint_info, &cmd->hint_root, |
| 1724 | from_cblock(cmd->cache_blocks), |
| 1725 | get_hint, policy); |
Joe Thornber | 0596661 | 2014-04-03 16:16:44 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy) |
| 1729 | { |
| 1730 | int r; |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1731 | |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1732 | WRITE_LOCK(cmd); |
Joe Thornber | 0596661 | 2014-04-03 16:16:44 +0100 | [diff] [blame] | 1733 | r = write_hints(cmd, policy); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1734 | WRITE_UNLOCK(cmd); |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 1735 | |
| 1736 | return r; |
| 1737 | } |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1738 | |
| 1739 | int dm_cache_metadata_all_clean(struct dm_cache_metadata *cmd, bool *result) |
| 1740 | { |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1741 | int r; |
| 1742 | |
| 1743 | READ_LOCK(cmd); |
| 1744 | r = blocks_are_unmapped_or_clean(cmd, 0, cmd->cache_blocks, result); |
| 1745 | READ_UNLOCK(cmd); |
| 1746 | |
| 1747 | return r; |
Joe Thornber | 2ee57d5 | 2013-10-24 14:10:29 -0400 | [diff] [blame] | 1748 | } |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1749 | |
| 1750 | void dm_cache_metadata_set_read_only(struct dm_cache_metadata *cmd) |
| 1751 | { |
| 1752 | WRITE_LOCK_VOID(cmd); |
| 1753 | dm_bm_set_read_only(cmd->bm); |
| 1754 | WRITE_UNLOCK(cmd); |
| 1755 | } |
| 1756 | |
| 1757 | void dm_cache_metadata_set_read_write(struct dm_cache_metadata *cmd) |
| 1758 | { |
| 1759 | WRITE_LOCK_VOID(cmd); |
| 1760 | dm_bm_set_read_write(cmd->bm); |
| 1761 | WRITE_UNLOCK(cmd); |
| 1762 | } |
| 1763 | |
| 1764 | int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd) |
| 1765 | { |
| 1766 | int r; |
| 1767 | struct dm_block *sblock; |
| 1768 | struct cache_disk_superblock *disk_super; |
| 1769 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1770 | WRITE_LOCK(cmd); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1771 | set_bit(NEEDS_CHECK, &cmd->flags); |
| 1772 | |
| 1773 | r = superblock_lock(cmd, &sblock); |
| 1774 | if (r) { |
| 1775 | DMERR("couldn't read superblock"); |
| 1776 | goto out; |
| 1777 | } |
| 1778 | |
| 1779 | disk_super = dm_block_data(sblock); |
| 1780 | disk_super->flags = cpu_to_le32(cmd->flags); |
| 1781 | |
| 1782 | dm_bm_unlock(sblock); |
| 1783 | |
| 1784 | out: |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1785 | WRITE_UNLOCK(cmd); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1786 | return r; |
| 1787 | } |
| 1788 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1789 | int dm_cache_metadata_needs_check(struct dm_cache_metadata *cmd, bool *result) |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1790 | { |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1791 | READ_LOCK(cmd); |
| 1792 | *result = !!test_bit(NEEDS_CHECK, &cmd->flags); |
| 1793 | READ_UNLOCK(cmd); |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1794 | |
Joe Thornber | d14fcf3 | 2016-03-10 16:20:58 +0000 | [diff] [blame] | 1795 | return 0; |
Joe Thornber | 028ae9f | 2015-04-22 16:42:35 -0400 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | int dm_cache_metadata_abort(struct dm_cache_metadata *cmd) |
| 1799 | { |
| 1800 | int r; |
| 1801 | |
| 1802 | WRITE_LOCK(cmd); |
| 1803 | __destroy_persistent_data_objects(cmd); |
| 1804 | r = __create_persistent_data_objects(cmd, false); |
| 1805 | if (r) |
| 1806 | cmd->fail_io = true; |
| 1807 | WRITE_UNLOCK(cmd); |
| 1808 | |
| 1809 | return r; |
| 1810 | } |