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