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