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