Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1 | /* |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 Red Hat, Inc. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm-thin-metadata.h" |
| 8 | #include "persistent-data/dm-btree.h" |
| 9 | #include "persistent-data/dm-space-map.h" |
| 10 | #include "persistent-data/dm-space-map-disk.h" |
| 11 | #include "persistent-data/dm-transaction-manager.h" |
| 12 | |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/device-mapper.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | |
| 17 | /*-------------------------------------------------------------------------- |
| 18 | * As far as the metadata goes, there is: |
| 19 | * |
| 20 | * - A superblock in block zero, taking up fewer than 512 bytes for |
| 21 | * atomic writes. |
| 22 | * |
| 23 | * - A space map managing the metadata blocks. |
| 24 | * |
| 25 | * - A space map managing the data blocks. |
| 26 | * |
| 27 | * - A btree mapping our internal thin dev ids onto struct disk_device_details. |
| 28 | * |
| 29 | * - A hierarchical btree, with 2 levels which effectively maps (thin |
| 30 | * dev id, virtual block) -> block_time. Block time is a 64-bit |
| 31 | * field holding the time in the low 24 bits, and block in the top 48 |
| 32 | * bits. |
| 33 | * |
| 34 | * BTrees consist solely of btree_nodes, that fill a block. Some are |
| 35 | * internal nodes, as such their values are a __le64 pointing to other |
| 36 | * nodes. Leaf nodes can store data of any reasonable size (ie. much |
| 37 | * smaller than the block size). The nodes consist of the header, |
| 38 | * followed by an array of keys, followed by an array of values. We have |
| 39 | * to binary search on the keys so they're all held together to help the |
| 40 | * cpu cache. |
| 41 | * |
| 42 | * Space maps have 2 btrees: |
| 43 | * |
| 44 | * - One maps a uint64_t onto a struct index_entry. Which points to a |
| 45 | * bitmap block, and has some details about how many free entries there |
| 46 | * are etc. |
| 47 | * |
| 48 | * - The bitmap blocks have a header (for the checksum). Then the rest |
| 49 | * of the block is pairs of bits. With the meaning being: |
| 50 | * |
| 51 | * 0 - ref count is 0 |
| 52 | * 1 - ref count is 1 |
| 53 | * 2 - ref count is 2 |
| 54 | * 3 - ref count is higher than 2 |
| 55 | * |
| 56 | * - If the count is higher than 2 then the ref count is entered in a |
| 57 | * second btree that directly maps the block_address to a uint32_t ref |
| 58 | * count. |
| 59 | * |
| 60 | * The space map metadata variant doesn't have a bitmaps btree. Instead |
| 61 | * it has one single blocks worth of index_entries. This avoids |
| 62 | * recursive issues with the bitmap btree needing to allocate space in |
| 63 | * order to insert. With a small data block size such as 64k the |
| 64 | * metadata support data devices that are hundreds of terrabytes. |
| 65 | * |
| 66 | * The space maps allocate space linearly from front to back. Space that |
| 67 | * is freed in a transaction is never recycled within that transaction. |
| 68 | * To try and avoid fragmenting _free_ space the allocator always goes |
| 69 | * back and fills in gaps. |
| 70 | * |
| 71 | * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks |
| 72 | * from the block manager. |
| 73 | *--------------------------------------------------------------------------*/ |
| 74 | |
| 75 | #define DM_MSG_PREFIX "thin metadata" |
| 76 | |
| 77 | #define THIN_SUPERBLOCK_MAGIC 27022010 |
| 78 | #define THIN_SUPERBLOCK_LOCATION 0 |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 79 | #define THIN_VERSION 2 |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 80 | #define THIN_METADATA_CACHE_SIZE 64 |
| 81 | #define SECTOR_TO_BLOCK_SHIFT 3 |
| 82 | |
Joe Thornber | 8c97117 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 83 | /* |
| 84 | * 3 for btree insert + |
| 85 | * 2 for btree lookup used within space map |
| 86 | */ |
| 87 | #define THIN_MAX_CONCURRENT_LOCKS 5 |
| 88 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 89 | /* This should be plenty */ |
| 90 | #define SPACE_MAP_ROOT_SIZE 128 |
| 91 | |
| 92 | /* |
| 93 | * Little endian on-disk superblock and device details. |
| 94 | */ |
| 95 | struct thin_disk_superblock { |
| 96 | __le32 csum; /* Checksum of superblock except for this field. */ |
| 97 | __le32 flags; |
| 98 | __le64 blocknr; /* This block number, dm_block_t. */ |
| 99 | |
| 100 | __u8 uuid[16]; |
| 101 | __le64 magic; |
| 102 | __le32 version; |
| 103 | __le32 time; |
| 104 | |
| 105 | __le64 trans_id; |
| 106 | |
| 107 | /* |
| 108 | * Root held by userspace transactions. |
| 109 | */ |
| 110 | __le64 held_root; |
| 111 | |
| 112 | __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 113 | __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 114 | |
| 115 | /* |
| 116 | * 2-level btree mapping (dev_id, (dev block, time)) -> data block |
| 117 | */ |
| 118 | __le64 data_mapping_root; |
| 119 | |
| 120 | /* |
| 121 | * Device detail root mapping dev_id -> device_details |
| 122 | */ |
| 123 | __le64 device_details_root; |
| 124 | |
| 125 | __le32 data_block_size; /* In 512-byte sectors. */ |
| 126 | |
| 127 | __le32 metadata_block_size; /* In 512-byte sectors. */ |
| 128 | __le64 metadata_nr_blocks; |
| 129 | |
| 130 | __le32 compat_flags; |
| 131 | __le32 compat_ro_flags; |
| 132 | __le32 incompat_flags; |
| 133 | } __packed; |
| 134 | |
| 135 | struct disk_device_details { |
| 136 | __le64 mapped_blocks; |
| 137 | __le64 transaction_id; /* When created. */ |
| 138 | __le32 creation_time; |
| 139 | __le32 snapshotted_time; |
| 140 | } __packed; |
| 141 | |
| 142 | struct dm_pool_metadata { |
| 143 | struct hlist_node hash; |
| 144 | |
| 145 | struct block_device *bdev; |
| 146 | struct dm_block_manager *bm; |
| 147 | struct dm_space_map *metadata_sm; |
| 148 | struct dm_space_map *data_sm; |
| 149 | struct dm_transaction_manager *tm; |
| 150 | struct dm_transaction_manager *nb_tm; |
| 151 | |
| 152 | /* |
| 153 | * Two-level btree. |
| 154 | * First level holds thin_dev_t. |
| 155 | * Second level holds mappings. |
| 156 | */ |
| 157 | struct dm_btree_info info; |
| 158 | |
| 159 | /* |
| 160 | * Non-blocking version of the above. |
| 161 | */ |
| 162 | struct dm_btree_info nb_info; |
| 163 | |
| 164 | /* |
| 165 | * Just the top level for deleting whole devices. |
| 166 | */ |
| 167 | struct dm_btree_info tl_info; |
| 168 | |
| 169 | /* |
| 170 | * Just the bottom level for creating new devices. |
| 171 | */ |
| 172 | struct dm_btree_info bl_info; |
| 173 | |
| 174 | /* |
| 175 | * Describes the device details btree. |
| 176 | */ |
| 177 | struct dm_btree_info details_info; |
| 178 | |
| 179 | struct rw_semaphore root_lock; |
| 180 | uint32_t time; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 181 | dm_block_t root; |
| 182 | dm_block_t details_root; |
| 183 | struct list_head thin_devices; |
| 184 | uint64_t trans_id; |
| 185 | unsigned long flags; |
| 186 | sector_t data_block_size; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Set if a transaction has to be aborted but the attempt to roll back |
| 190 | * to the previous (good) transaction failed. The only pool metadata |
| 191 | * operation possible in this state is the closing of the device. |
| 192 | */ |
| 193 | bool fail_io:1; |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Reading the space map roots can fail, so we read it into these |
| 197 | * buffers before the superblock is locked and updated. |
| 198 | */ |
| 199 | __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 200 | __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | struct dm_thin_device { |
| 204 | struct list_head list; |
| 205 | struct dm_pool_metadata *pmd; |
| 206 | dm_thin_id id; |
| 207 | |
| 208 | int open_count; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 209 | bool changed:1; |
| 210 | bool aborted_with_changes:1; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 211 | uint64_t mapped_blocks; |
| 212 | uint64_t transaction_id; |
| 213 | uint32_t creation_time; |
| 214 | uint32_t snapshotted_time; |
| 215 | }; |
| 216 | |
| 217 | /*---------------------------------------------------------------- |
| 218 | * superblock validator |
| 219 | *--------------------------------------------------------------*/ |
| 220 | |
| 221 | #define SUPERBLOCK_CSUM_XOR 160774 |
| 222 | |
| 223 | static void sb_prepare_for_write(struct dm_block_validator *v, |
| 224 | struct dm_block *b, |
| 225 | size_t block_size) |
| 226 | { |
| 227 | struct thin_disk_superblock *disk_super = dm_block_data(b); |
| 228 | |
| 229 | disk_super->blocknr = cpu_to_le64(dm_block_location(b)); |
| 230 | disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 231 | block_size - sizeof(__le32), |
| 232 | SUPERBLOCK_CSUM_XOR)); |
| 233 | } |
| 234 | |
| 235 | static int sb_check(struct dm_block_validator *v, |
| 236 | struct dm_block *b, |
| 237 | size_t block_size) |
| 238 | { |
| 239 | struct thin_disk_superblock *disk_super = dm_block_data(b); |
| 240 | __le32 csum_le; |
| 241 | |
| 242 | if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) { |
| 243 | DMERR("sb_check failed: blocknr %llu: " |
| 244 | "wanted %llu", le64_to_cpu(disk_super->blocknr), |
| 245 | (unsigned long long)dm_block_location(b)); |
| 246 | return -ENOTBLK; |
| 247 | } |
| 248 | |
| 249 | if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) { |
| 250 | DMERR("sb_check failed: magic %llu: " |
| 251 | "wanted %llu", le64_to_cpu(disk_super->magic), |
| 252 | (unsigned long long)THIN_SUPERBLOCK_MAGIC); |
| 253 | return -EILSEQ; |
| 254 | } |
| 255 | |
| 256 | csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 257 | block_size - sizeof(__le32), |
| 258 | SUPERBLOCK_CSUM_XOR)); |
| 259 | if (csum_le != disk_super->csum) { |
| 260 | DMERR("sb_check failed: csum %u: wanted %u", |
| 261 | le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum)); |
| 262 | return -EILSEQ; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static struct dm_block_validator sb_validator = { |
| 269 | .name = "superblock", |
| 270 | .prepare_for_write = sb_prepare_for_write, |
| 271 | .check = sb_check |
| 272 | }; |
| 273 | |
| 274 | /*---------------------------------------------------------------- |
| 275 | * Methods for the btree value types |
| 276 | *--------------------------------------------------------------*/ |
| 277 | |
| 278 | static uint64_t pack_block_time(dm_block_t b, uint32_t t) |
| 279 | { |
| 280 | return (b << 24) | t; |
| 281 | } |
| 282 | |
| 283 | static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t) |
| 284 | { |
| 285 | *b = v >> 24; |
| 286 | *t = v & ((1 << 24) - 1); |
| 287 | } |
| 288 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 289 | static void data_block_inc(void *context, const void *value_le) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 290 | { |
| 291 | struct dm_space_map *sm = context; |
| 292 | __le64 v_le; |
| 293 | uint64_t b; |
| 294 | uint32_t t; |
| 295 | |
| 296 | memcpy(&v_le, value_le, sizeof(v_le)); |
| 297 | unpack_block_time(le64_to_cpu(v_le), &b, &t); |
| 298 | dm_sm_inc_block(sm, b); |
| 299 | } |
| 300 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 301 | static void data_block_dec(void *context, const void *value_le) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 302 | { |
| 303 | struct dm_space_map *sm = context; |
| 304 | __le64 v_le; |
| 305 | uint64_t b; |
| 306 | uint32_t t; |
| 307 | |
| 308 | memcpy(&v_le, value_le, sizeof(v_le)); |
| 309 | unpack_block_time(le64_to_cpu(v_le), &b, &t); |
| 310 | dm_sm_dec_block(sm, b); |
| 311 | } |
| 312 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 313 | static int data_block_equal(void *context, const void *value1_le, const void *value2_le) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 314 | { |
| 315 | __le64 v1_le, v2_le; |
| 316 | uint64_t b1, b2; |
| 317 | uint32_t t; |
| 318 | |
| 319 | memcpy(&v1_le, value1_le, sizeof(v1_le)); |
| 320 | memcpy(&v2_le, value2_le, sizeof(v2_le)); |
| 321 | unpack_block_time(le64_to_cpu(v1_le), &b1, &t); |
| 322 | unpack_block_time(le64_to_cpu(v2_le), &b2, &t); |
| 323 | |
| 324 | return b1 == b2; |
| 325 | } |
| 326 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 327 | static void subtree_inc(void *context, const void *value) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 328 | { |
| 329 | struct dm_btree_info *info = context; |
| 330 | __le64 root_le; |
| 331 | uint64_t root; |
| 332 | |
| 333 | memcpy(&root_le, value, sizeof(root_le)); |
| 334 | root = le64_to_cpu(root_le); |
| 335 | dm_tm_inc(info->tm, root); |
| 336 | } |
| 337 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 338 | static void subtree_dec(void *context, const void *value) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 339 | { |
| 340 | struct dm_btree_info *info = context; |
| 341 | __le64 root_le; |
| 342 | uint64_t root; |
| 343 | |
| 344 | memcpy(&root_le, value, sizeof(root_le)); |
| 345 | root = le64_to_cpu(root_le); |
| 346 | if (dm_btree_del(info, root)) |
| 347 | DMERR("btree delete failed\n"); |
| 348 | } |
| 349 | |
Mike Snitzer | 018cede | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 350 | static int subtree_equal(void *context, const void *value1_le, const void *value2_le) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 351 | { |
| 352 | __le64 v1_le, v2_le; |
| 353 | memcpy(&v1_le, value1_le, sizeof(v1_le)); |
| 354 | memcpy(&v2_le, value2_le, sizeof(v2_le)); |
| 355 | |
| 356 | return v1_le == v2_le; |
| 357 | } |
| 358 | |
| 359 | /*----------------------------------------------------------------*/ |
| 360 | |
Joe Thornber | 2597119 | 2012-07-27 15:08:09 +0100 | [diff] [blame] | 361 | static int superblock_lock_zero(struct dm_pool_metadata *pmd, |
| 362 | struct dm_block **sblock) |
| 363 | { |
| 364 | return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 365 | &sb_validator, sblock); |
| 366 | } |
| 367 | |
| 368 | static int superblock_lock(struct dm_pool_metadata *pmd, |
| 369 | struct dm_block **sblock) |
| 370 | { |
| 371 | return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 372 | &sb_validator, sblock); |
| 373 | } |
| 374 | |
Joe Thornber | 332627d | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 375 | static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 376 | { |
| 377 | int r; |
| 378 | unsigned i; |
| 379 | struct dm_block *b; |
| 380 | __le64 *data_le, zero = cpu_to_le64(0); |
| 381 | unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64); |
| 382 | |
| 383 | /* |
| 384 | * We can't use a validator here - it may be all zeroes. |
| 385 | */ |
| 386 | r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b); |
| 387 | if (r) |
| 388 | return r; |
| 389 | |
| 390 | data_le = dm_block_data(b); |
| 391 | *result = 1; |
| 392 | for (i = 0; i < block_size; i++) { |
| 393 | if (data_le[i] != zero) { |
| 394 | *result = 0; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 399 | dm_bm_unlock(b); |
| 400 | |
| 401 | return 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Joe Thornber | 41675ae | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 404 | static void __setup_btree_details(struct dm_pool_metadata *pmd) |
| 405 | { |
| 406 | pmd->info.tm = pmd->tm; |
| 407 | pmd->info.levels = 2; |
| 408 | pmd->info.value_type.context = pmd->data_sm; |
| 409 | pmd->info.value_type.size = sizeof(__le64); |
| 410 | pmd->info.value_type.inc = data_block_inc; |
| 411 | pmd->info.value_type.dec = data_block_dec; |
| 412 | pmd->info.value_type.equal = data_block_equal; |
| 413 | |
| 414 | memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info)); |
| 415 | pmd->nb_info.tm = pmd->nb_tm; |
| 416 | |
| 417 | pmd->tl_info.tm = pmd->tm; |
| 418 | pmd->tl_info.levels = 1; |
Joe Thornber | e3cbf94 | 2012-12-21 20:23:32 +0000 | [diff] [blame] | 419 | pmd->tl_info.value_type.context = &pmd->bl_info; |
Joe Thornber | 41675ae | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 420 | pmd->tl_info.value_type.size = sizeof(__le64); |
| 421 | pmd->tl_info.value_type.inc = subtree_inc; |
| 422 | pmd->tl_info.value_type.dec = subtree_dec; |
| 423 | pmd->tl_info.value_type.equal = subtree_equal; |
| 424 | |
| 425 | pmd->bl_info.tm = pmd->tm; |
| 426 | pmd->bl_info.levels = 1; |
| 427 | pmd->bl_info.value_type.context = pmd->data_sm; |
| 428 | pmd->bl_info.value_type.size = sizeof(__le64); |
| 429 | pmd->bl_info.value_type.inc = data_block_inc; |
| 430 | pmd->bl_info.value_type.dec = data_block_dec; |
| 431 | pmd->bl_info.value_type.equal = data_block_equal; |
| 432 | |
| 433 | pmd->details_info.tm = pmd->tm; |
| 434 | pmd->details_info.levels = 1; |
| 435 | pmd->details_info.value_type.context = NULL; |
| 436 | pmd->details_info.value_type.size = sizeof(struct disk_device_details); |
| 437 | pmd->details_info.value_type.inc = NULL; |
| 438 | pmd->details_info.value_type.dec = NULL; |
| 439 | pmd->details_info.value_type.equal = NULL; |
| 440 | } |
| 441 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 442 | static int save_sm_roots(struct dm_pool_metadata *pmd) |
| 443 | { |
| 444 | int r; |
| 445 | size_t len; |
| 446 | |
| 447 | r = dm_sm_root_size(pmd->metadata_sm, &len); |
| 448 | if (r < 0) |
| 449 | return r; |
| 450 | |
| 451 | r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len); |
| 452 | if (r < 0) |
| 453 | return r; |
| 454 | |
| 455 | r = dm_sm_root_size(pmd->data_sm, &len); |
| 456 | if (r < 0) |
| 457 | return r; |
| 458 | |
| 459 | return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len); |
| 460 | } |
| 461 | |
| 462 | static void copy_sm_roots(struct dm_pool_metadata *pmd, |
| 463 | struct thin_disk_superblock *disk) |
| 464 | { |
| 465 | memcpy(&disk->metadata_space_map_root, |
| 466 | &pmd->metadata_space_map_root, |
| 467 | sizeof(pmd->metadata_space_map_root)); |
| 468 | |
| 469 | memcpy(&disk->data_space_map_root, |
| 470 | &pmd->data_space_map_root, |
| 471 | sizeof(pmd->data_space_map_root)); |
| 472 | } |
| 473 | |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 474 | static int __write_initial_superblock(struct dm_pool_metadata *pmd) |
| 475 | { |
| 476 | int r; |
| 477 | struct dm_block *sblock; |
| 478 | struct thin_disk_superblock *disk_super; |
| 479 | sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT; |
| 480 | |
| 481 | if (bdev_size > THIN_METADATA_MAX_SECTORS) |
| 482 | bdev_size = THIN_METADATA_MAX_SECTORS; |
| 483 | |
Joe Thornber | 10d2a9f | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 484 | r = dm_sm_commit(pmd->data_sm); |
| 485 | if (r < 0) |
| 486 | return r; |
| 487 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 488 | r = save_sm_roots(pmd); |
| 489 | if (r < 0) |
| 490 | return r; |
| 491 | |
Joe Thornber | 10d2a9f | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 492 | r = dm_tm_pre_commit(pmd->tm); |
| 493 | if (r < 0) |
| 494 | return r; |
| 495 | |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 496 | r = superblock_lock_zero(pmd, &sblock); |
| 497 | if (r) |
| 498 | return r; |
| 499 | |
| 500 | disk_super = dm_block_data(sblock); |
Joe Thornber | 10d2a9f | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 501 | disk_super->flags = 0; |
Joe Thornber | 583ceee | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 502 | memset(disk_super->uuid, 0, sizeof(disk_super->uuid)); |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 503 | disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC); |
| 504 | disk_super->version = cpu_to_le32(THIN_VERSION); |
| 505 | disk_super->time = 0; |
Joe Thornber | 10d2a9f | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 506 | disk_super->trans_id = 0; |
| 507 | disk_super->held_root = 0; |
| 508 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 509 | copy_sm_roots(pmd, disk_super); |
Joe Thornber | 10d2a9f | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 510 | |
| 511 | disk_super->data_mapping_root = cpu_to_le64(pmd->root); |
| 512 | disk_super->device_details_root = cpu_to_le64(pmd->details_root); |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 513 | disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE); |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 514 | disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT); |
| 515 | disk_super->data_block_size = cpu_to_le32(pmd->data_block_size); |
| 516 | |
Joe Thornber | 270938b | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 517 | return dm_tm_commit(pmd->tm, sblock); |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Joe Thornber | a97e5e6 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 520 | static int __format_metadata(struct dm_pool_metadata *pmd) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 521 | { |
| 522 | int r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 523 | |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 524 | r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 525 | &pmd->tm, &pmd->metadata_sm); |
| 526 | if (r < 0) { |
| 527 | DMERR("tm_create_with_sm failed"); |
| 528 | return r; |
| 529 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 530 | |
Joe Thornber | a97e5e6 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 531 | pmd->data_sm = dm_sm_disk_create(pmd->tm, 0); |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 532 | if (IS_ERR(pmd->data_sm)) { |
| 533 | DMERR("sm_disk_create failed"); |
| 534 | r = PTR_ERR(pmd->data_sm); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 535 | goto bad_cleanup_tm; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Joe Thornber | d633281 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 538 | pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 539 | if (!pmd->nb_tm) { |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 540 | DMERR("could not create non-blocking clone tm"); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 541 | r = -ENOMEM; |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 542 | goto bad_cleanup_data_sm; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Joe Thornber | 41675ae | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 545 | __setup_btree_details(pmd); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 546 | |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 547 | r = dm_btree_empty(&pmd->info, &pmd->root); |
| 548 | if (r < 0) |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 549 | goto bad_cleanup_nb_tm; |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 550 | |
| 551 | r = dm_btree_empty(&pmd->details_info, &pmd->details_root); |
| 552 | if (r < 0) { |
| 553 | DMERR("couldn't create devices root"); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 554 | goto bad_cleanup_nb_tm; |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | r = __write_initial_superblock(pmd); |
| 558 | if (r) |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 559 | goto bad_cleanup_nb_tm; |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 560 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 561 | return 0; |
| 562 | |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 563 | bad_cleanup_nb_tm: |
| 564 | dm_tm_destroy(pmd->nb_tm); |
| 565 | bad_cleanup_data_sm: |
Joe Thornber | d633281 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 566 | dm_sm_destroy(pmd->data_sm); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 567 | bad_cleanup_tm: |
Joe Thornber | d633281 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 568 | dm_tm_destroy(pmd->tm); |
| 569 | dm_sm_destroy(pmd->metadata_sm); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 570 | |
| 571 | return r; |
| 572 | } |
| 573 | |
Mike Snitzer | d73ec52 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 574 | static int __check_incompat_features(struct thin_disk_superblock *disk_super, |
| 575 | struct dm_pool_metadata *pmd) |
| 576 | { |
| 577 | uint32_t features; |
| 578 | |
| 579 | features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP; |
| 580 | if (features) { |
| 581 | DMERR("could not access metadata due to unsupported optional features (%lx).", |
| 582 | (unsigned long)features); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Check for read-only metadata to skip the following RDWR checks. |
| 588 | */ |
| 589 | if (get_disk_ro(pmd->bdev->bd_disk)) |
| 590 | return 0; |
| 591 | |
| 592 | features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP; |
| 593 | if (features) { |
| 594 | DMERR("could not access metadata RDWR due to unsupported optional features (%lx).", |
| 595 | (unsigned long)features); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 602 | static int __open_metadata(struct dm_pool_metadata *pmd) |
| 603 | { |
| 604 | int r; |
| 605 | struct dm_block *sblock; |
| 606 | struct thin_disk_superblock *disk_super; |
| 607 | |
| 608 | r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 609 | &sb_validator, &sblock); |
| 610 | if (r < 0) { |
| 611 | DMERR("couldn't read superblock"); |
| 612 | return r; |
| 613 | } |
| 614 | |
| 615 | disk_super = dm_block_data(sblock); |
Mike Snitzer | d73ec52 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 616 | |
Mike Snitzer | 9aec862 | 2014-07-14 16:35:54 -0400 | [diff] [blame] | 617 | /* Verify the data block size hasn't changed */ |
| 618 | if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) { |
| 619 | DMERR("changing the data block size (from %u to %llu) is not supported", |
| 620 | le32_to_cpu(disk_super->data_block_size), |
| 621 | (unsigned long long)pmd->data_block_size); |
| 622 | r = -EINVAL; |
| 623 | goto bad_unlock_sblock; |
| 624 | } |
| 625 | |
Mike Snitzer | d73ec52 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 626 | r = __check_incompat_features(disk_super, pmd); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 627 | if (r < 0) |
| 628 | goto bad_unlock_sblock; |
Mike Snitzer | d73ec52 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 629 | |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 630 | r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 631 | disk_super->metadata_space_map_root, |
| 632 | sizeof(disk_super->metadata_space_map_root), |
| 633 | &pmd->tm, &pmd->metadata_sm); |
| 634 | if (r < 0) { |
| 635 | DMERR("tm_open_with_sm failed"); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 636 | goto bad_unlock_sblock; |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root, |
| 640 | sizeof(disk_super->data_space_map_root)); |
| 641 | if (IS_ERR(pmd->data_sm)) { |
| 642 | DMERR("sm_disk_open failed"); |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 643 | r = PTR_ERR(pmd->data_sm); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 644 | goto bad_cleanup_tm; |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 647 | pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm); |
| 648 | if (!pmd->nb_tm) { |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 649 | DMERR("could not create non-blocking clone tm"); |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 650 | r = -ENOMEM; |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 651 | goto bad_cleanup_data_sm; |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | __setup_btree_details(pmd); |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 655 | dm_bm_unlock(sblock); |
| 656 | |
| 657 | return 0; |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 658 | |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 659 | bad_cleanup_data_sm: |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 660 | dm_sm_destroy(pmd->data_sm); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 661 | bad_cleanup_tm: |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 662 | dm_tm_destroy(pmd->tm); |
| 663 | dm_sm_destroy(pmd->metadata_sm); |
Joe Thornber | 0fa5b17 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 664 | bad_unlock_sblock: |
| 665 | dm_bm_unlock(sblock); |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 666 | |
| 667 | return r; |
| 668 | } |
| 669 | |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 670 | static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device) |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 671 | { |
Joe Thornber | 8801e06 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 672 | int r, unformatted; |
Joe Thornber | 237074c | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 673 | |
Joe Thornber | 8801e06 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 674 | r = __superblock_all_zeroes(pmd->bm, &unformatted); |
Joe Thornber | 237074c | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 675 | if (r) |
| 676 | return r; |
| 677 | |
Joe Thornber | 8801e06 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 678 | if (unformatted) |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 679 | return format_device ? __format_metadata(pmd) : -EPERM; |
| 680 | |
| 681 | return __open_metadata(pmd); |
Joe Thornber | e4d2205 | 2012-07-27 15:08:12 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 684 | static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device) |
Joe Thornber | 332627d | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 685 | { |
| 686 | int r; |
| 687 | |
Mike Snitzer | 7d48935 | 2014-02-12 23:58:15 -0500 | [diff] [blame] | 688 | pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT, |
Joe Thornber | 332627d | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 689 | THIN_METADATA_CACHE_SIZE, |
| 690 | THIN_MAX_CONCURRENT_LOCKS); |
| 691 | if (IS_ERR(pmd->bm)) { |
| 692 | DMERR("could not create block manager"); |
| 693 | return PTR_ERR(pmd->bm); |
| 694 | } |
| 695 | |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 696 | r = __open_or_format_metadata(pmd, format_device); |
Joe Thornber | 332627d | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 697 | if (r) |
| 698 | dm_block_manager_destroy(pmd->bm); |
| 699 | |
| 700 | return r; |
| 701 | } |
| 702 | |
Joe Thornber | f9dd935 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 703 | static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd) |
| 704 | { |
| 705 | dm_sm_destroy(pmd->data_sm); |
| 706 | dm_sm_destroy(pmd->metadata_sm); |
| 707 | dm_tm_destroy(pmd->nb_tm); |
| 708 | dm_tm_destroy(pmd->tm); |
| 709 | dm_block_manager_destroy(pmd->bm); |
| 710 | } |
| 711 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 712 | static int __begin_transaction(struct dm_pool_metadata *pmd) |
| 713 | { |
| 714 | int r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 715 | struct thin_disk_superblock *disk_super; |
| 716 | struct dm_block *sblock; |
| 717 | |
| 718 | /* |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 719 | * We re-read the superblock every time. Shouldn't need to do this |
| 720 | * really. |
| 721 | */ |
| 722 | r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 723 | &sb_validator, &sblock); |
| 724 | if (r) |
| 725 | return r; |
| 726 | |
| 727 | disk_super = dm_block_data(sblock); |
| 728 | pmd->time = le32_to_cpu(disk_super->time); |
| 729 | pmd->root = le64_to_cpu(disk_super->data_mapping_root); |
| 730 | pmd->details_root = le64_to_cpu(disk_super->device_details_root); |
| 731 | pmd->trans_id = le64_to_cpu(disk_super->trans_id); |
| 732 | pmd->flags = le32_to_cpu(disk_super->flags); |
| 733 | pmd->data_block_size = le32_to_cpu(disk_super->data_block_size); |
| 734 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 735 | dm_bm_unlock(sblock); |
Mike Snitzer | d73ec52 | 2012-07-27 15:08:13 +0100 | [diff] [blame] | 736 | return 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static int __write_changed_details(struct dm_pool_metadata *pmd) |
| 740 | { |
| 741 | int r; |
| 742 | struct dm_thin_device *td, *tmp; |
| 743 | struct disk_device_details details; |
| 744 | uint64_t key; |
| 745 | |
| 746 | list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) { |
| 747 | if (!td->changed) |
| 748 | continue; |
| 749 | |
| 750 | key = td->id; |
| 751 | |
| 752 | details.mapped_blocks = cpu_to_le64(td->mapped_blocks); |
| 753 | details.transaction_id = cpu_to_le64(td->transaction_id); |
| 754 | details.creation_time = cpu_to_le32(td->creation_time); |
| 755 | details.snapshotted_time = cpu_to_le32(td->snapshotted_time); |
| 756 | __dm_bless_for_disk(&details); |
| 757 | |
| 758 | r = dm_btree_insert(&pmd->details_info, pmd->details_root, |
| 759 | &key, &details, &pmd->details_root); |
| 760 | if (r) |
| 761 | return r; |
| 762 | |
| 763 | if (td->open_count) |
| 764 | td->changed = 0; |
| 765 | else { |
| 766 | list_del(&td->list); |
| 767 | kfree(td); |
| 768 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int __commit_transaction(struct dm_pool_metadata *pmd) |
| 775 | { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 776 | int r; |
| 777 | size_t metadata_len, data_len; |
| 778 | struct thin_disk_superblock *disk_super; |
| 779 | struct dm_block *sblock; |
| 780 | |
| 781 | /* |
| 782 | * We need to know if the thin_disk_superblock exceeds a 512-byte sector. |
| 783 | */ |
| 784 | BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512); |
| 785 | |
| 786 | r = __write_changed_details(pmd); |
| 787 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 788 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 789 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 790 | r = dm_sm_commit(pmd->data_sm); |
| 791 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 792 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 793 | |
| 794 | r = dm_tm_pre_commit(pmd->tm); |
| 795 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 796 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 797 | |
| 798 | r = dm_sm_root_size(pmd->metadata_sm, &metadata_len); |
| 799 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 800 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 801 | |
Joe Thornber | fef838c | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 802 | r = dm_sm_root_size(pmd->data_sm, &data_len); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 803 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 804 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 805 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 806 | r = save_sm_roots(pmd); |
| 807 | if (r < 0) |
| 808 | return r; |
| 809 | |
Joe Thornber | 2597119 | 2012-07-27 15:08:09 +0100 | [diff] [blame] | 810 | r = superblock_lock(pmd, &sblock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 811 | if (r) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame] | 812 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 813 | |
| 814 | disk_super = dm_block_data(sblock); |
| 815 | disk_super->time = cpu_to_le32(pmd->time); |
| 816 | disk_super->data_mapping_root = cpu_to_le64(pmd->root); |
| 817 | disk_super->device_details_root = cpu_to_le64(pmd->details_root); |
| 818 | disk_super->trans_id = cpu_to_le64(pmd->trans_id); |
| 819 | disk_super->flags = cpu_to_le32(pmd->flags); |
| 820 | |
Joe Thornber | 5a32083 | 2014-03-27 14:13:23 +0000 | [diff] [blame] | 821 | copy_sm_roots(pmd, disk_super); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 822 | |
Joe Thornber | eb04cf6 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 823 | return dm_tm_commit(pmd->tm, sblock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev, |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 827 | sector_t data_block_size, |
| 828 | bool format_device) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 829 | { |
| 830 | int r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 831 | struct dm_pool_metadata *pmd; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 832 | |
| 833 | pmd = kmalloc(sizeof(*pmd), GFP_KERNEL); |
| 834 | if (!pmd) { |
| 835 | DMERR("could not allocate metadata struct"); |
| 836 | return ERR_PTR(-ENOMEM); |
| 837 | } |
| 838 | |
Joe Thornber | 6a0ebd3 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 839 | init_rwsem(&pmd->root_lock); |
| 840 | pmd->time = 0; |
| 841 | INIT_LIST_HEAD(&pmd->thin_devices); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 842 | pmd->fail_io = false; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 843 | pmd->bdev = bdev; |
Joe Thornber | 9cb6653 | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 844 | pmd->data_block_size = data_block_size; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 845 | |
Joe Thornber | 66b1edc | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 846 | r = __create_persistent_data_objects(pmd, format_device); |
Joe Thornber | 332627d | 2012-07-27 15:08:10 +0100 | [diff] [blame] | 847 | if (r) { |
| 848 | kfree(pmd); |
| 849 | return ERR_PTR(r); |
| 850 | } |
| 851 | |
Joe Thornber | 270938b | 2012-07-27 15:08:11 +0100 | [diff] [blame] | 852 | r = __begin_transaction(pmd); |
| 853 | if (r < 0) { |
| 854 | if (dm_pool_metadata_close(pmd) < 0) |
| 855 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); |
| 856 | return ERR_PTR(r); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | return pmd; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | int dm_pool_metadata_close(struct dm_pool_metadata *pmd) |
| 863 | { |
| 864 | int r; |
| 865 | unsigned open_devices = 0; |
| 866 | struct dm_thin_device *td, *tmp; |
| 867 | |
| 868 | down_read(&pmd->root_lock); |
| 869 | list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) { |
| 870 | if (td->open_count) |
| 871 | open_devices++; |
| 872 | else { |
| 873 | list_del(&td->list); |
| 874 | kfree(td); |
| 875 | } |
| 876 | } |
| 877 | up_read(&pmd->root_lock); |
| 878 | |
| 879 | if (open_devices) { |
| 880 | DMERR("attempt to close pmd when %u device(s) are still open", |
| 881 | open_devices); |
| 882 | return -EBUSY; |
| 883 | } |
| 884 | |
Mike Snitzer | 49f154c | 2015-04-23 15:06:27 -0400 | [diff] [blame] | 885 | if (!dm_bm_is_read_only(pmd->bm) && !pmd->fail_io) { |
Joe Thornber | 12ba58a | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 886 | r = __commit_transaction(pmd); |
| 887 | if (r < 0) |
| 888 | DMWARN("%s: __commit_transaction() failed, error = %d", |
| 889 | __func__, r); |
| 890 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 891 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 892 | if (!pmd->fail_io) |
| 893 | __destroy_persistent_data_objects(pmd); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 894 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 895 | kfree(pmd); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 899 | /* |
| 900 | * __open_device: Returns @td corresponding to device with id @dev, |
| 901 | * creating it if @create is set and incrementing @td->open_count. |
| 902 | * On failure, @td is undefined. |
| 903 | */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 904 | static int __open_device(struct dm_pool_metadata *pmd, |
| 905 | dm_thin_id dev, int create, |
| 906 | struct dm_thin_device **td) |
| 907 | { |
| 908 | int r, changed = 0; |
| 909 | struct dm_thin_device *td2; |
| 910 | uint64_t key = dev; |
| 911 | struct disk_device_details details_le; |
| 912 | |
| 913 | /* |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 914 | * If the device is already open, return it. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 915 | */ |
| 916 | list_for_each_entry(td2, &pmd->thin_devices, list) |
| 917 | if (td2->id == dev) { |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 918 | /* |
| 919 | * May not create an already-open device. |
| 920 | */ |
| 921 | if (create) |
| 922 | return -EEXIST; |
| 923 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 924 | td2->open_count++; |
| 925 | *td = td2; |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * Check the device exists. |
| 931 | */ |
| 932 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 933 | &key, &details_le); |
| 934 | if (r) { |
| 935 | if (r != -ENODATA || !create) |
| 936 | return r; |
| 937 | |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 938 | /* |
| 939 | * Create new device. |
| 940 | */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 941 | changed = 1; |
| 942 | details_le.mapped_blocks = 0; |
| 943 | details_le.transaction_id = cpu_to_le64(pmd->trans_id); |
| 944 | details_le.creation_time = cpu_to_le32(pmd->time); |
| 945 | details_le.snapshotted_time = cpu_to_le32(pmd->time); |
| 946 | } |
| 947 | |
| 948 | *td = kmalloc(sizeof(**td), GFP_NOIO); |
| 949 | if (!*td) |
| 950 | return -ENOMEM; |
| 951 | |
| 952 | (*td)->pmd = pmd; |
| 953 | (*td)->id = dev; |
| 954 | (*td)->open_count = 1; |
| 955 | (*td)->changed = changed; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 956 | (*td)->aborted_with_changes = false; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 957 | (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks); |
| 958 | (*td)->transaction_id = le64_to_cpu(details_le.transaction_id); |
| 959 | (*td)->creation_time = le32_to_cpu(details_le.creation_time); |
| 960 | (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time); |
| 961 | |
| 962 | list_add(&(*td)->list, &pmd->thin_devices); |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static void __close_device(struct dm_thin_device *td) |
| 968 | { |
| 969 | --td->open_count; |
| 970 | } |
| 971 | |
| 972 | static int __create_thin(struct dm_pool_metadata *pmd, |
| 973 | dm_thin_id dev) |
| 974 | { |
| 975 | int r; |
| 976 | dm_block_t dev_root; |
| 977 | uint64_t key = dev; |
| 978 | struct disk_device_details details_le; |
| 979 | struct dm_thin_device *td; |
| 980 | __le64 value; |
| 981 | |
| 982 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 983 | &key, &details_le); |
| 984 | if (!r) |
| 985 | return -EEXIST; |
| 986 | |
| 987 | /* |
| 988 | * Create an empty btree for the mappings. |
| 989 | */ |
| 990 | r = dm_btree_empty(&pmd->bl_info, &dev_root); |
| 991 | if (r) |
| 992 | return r; |
| 993 | |
| 994 | /* |
| 995 | * Insert it into the main mapping tree. |
| 996 | */ |
| 997 | value = cpu_to_le64(dev_root); |
| 998 | __dm_bless_for_disk(&value); |
| 999 | r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root); |
| 1000 | if (r) { |
| 1001 | dm_btree_del(&pmd->bl_info, dev_root); |
| 1002 | return r; |
| 1003 | } |
| 1004 | |
| 1005 | r = __open_device(pmd, dev, 1, &td); |
| 1006 | if (r) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1007 | dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 1008 | dm_btree_del(&pmd->bl_info, dev_root); |
| 1009 | return r; |
| 1010 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1011 | __close_device(td); |
| 1012 | |
| 1013 | return r; |
| 1014 | } |
| 1015 | |
| 1016 | int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev) |
| 1017 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1018 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1019 | |
| 1020 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1021 | if (!pmd->fail_io) |
| 1022 | r = __create_thin(pmd, dev); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1023 | up_write(&pmd->root_lock); |
| 1024 | |
| 1025 | return r; |
| 1026 | } |
| 1027 | |
| 1028 | static int __set_snapshot_details(struct dm_pool_metadata *pmd, |
| 1029 | struct dm_thin_device *snap, |
| 1030 | dm_thin_id origin, uint32_t time) |
| 1031 | { |
| 1032 | int r; |
| 1033 | struct dm_thin_device *td; |
| 1034 | |
| 1035 | r = __open_device(pmd, origin, 0, &td); |
| 1036 | if (r) |
| 1037 | return r; |
| 1038 | |
| 1039 | td->changed = 1; |
| 1040 | td->snapshotted_time = time; |
| 1041 | |
| 1042 | snap->mapped_blocks = td->mapped_blocks; |
| 1043 | snap->snapshotted_time = time; |
| 1044 | __close_device(td); |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static int __create_snap(struct dm_pool_metadata *pmd, |
| 1050 | dm_thin_id dev, dm_thin_id origin) |
| 1051 | { |
| 1052 | int r; |
| 1053 | dm_block_t origin_root; |
| 1054 | uint64_t key = origin, dev_key = dev; |
| 1055 | struct dm_thin_device *td; |
| 1056 | struct disk_device_details details_le; |
| 1057 | __le64 value; |
| 1058 | |
| 1059 | /* check this device is unused */ |
| 1060 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 1061 | &dev_key, &details_le); |
| 1062 | if (!r) |
| 1063 | return -EEXIST; |
| 1064 | |
| 1065 | /* find the mapping tree for the origin */ |
| 1066 | r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value); |
| 1067 | if (r) |
| 1068 | return r; |
| 1069 | origin_root = le64_to_cpu(value); |
| 1070 | |
| 1071 | /* clone the origin, an inc will do */ |
| 1072 | dm_tm_inc(pmd->tm, origin_root); |
| 1073 | |
| 1074 | /* insert into the main mapping tree */ |
| 1075 | value = cpu_to_le64(origin_root); |
| 1076 | __dm_bless_for_disk(&value); |
| 1077 | key = dev; |
| 1078 | r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root); |
| 1079 | if (r) { |
| 1080 | dm_tm_dec(pmd->tm, origin_root); |
| 1081 | return r; |
| 1082 | } |
| 1083 | |
| 1084 | pmd->time++; |
| 1085 | |
| 1086 | r = __open_device(pmd, dev, 1, &td); |
| 1087 | if (r) |
| 1088 | goto bad; |
| 1089 | |
| 1090 | r = __set_snapshot_details(pmd, td, origin, pmd->time); |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 1091 | __close_device(td); |
| 1092 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1093 | if (r) |
| 1094 | goto bad; |
| 1095 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1096 | return 0; |
| 1097 | |
| 1098 | bad: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1099 | dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 1100 | dm_btree_remove(&pmd->details_info, pmd->details_root, |
| 1101 | &key, &pmd->details_root); |
| 1102 | return r; |
| 1103 | } |
| 1104 | |
| 1105 | int dm_pool_create_snap(struct dm_pool_metadata *pmd, |
| 1106 | dm_thin_id dev, |
| 1107 | dm_thin_id origin) |
| 1108 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1109 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1110 | |
| 1111 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1112 | if (!pmd->fail_io) |
| 1113 | r = __create_snap(pmd, dev, origin); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1114 | up_write(&pmd->root_lock); |
| 1115 | |
| 1116 | return r; |
| 1117 | } |
| 1118 | |
| 1119 | static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev) |
| 1120 | { |
| 1121 | int r; |
| 1122 | uint64_t key = dev; |
| 1123 | struct dm_thin_device *td; |
| 1124 | |
| 1125 | /* TODO: failure should mark the transaction invalid */ |
| 1126 | r = __open_device(pmd, dev, 0, &td); |
| 1127 | if (r) |
| 1128 | return r; |
| 1129 | |
| 1130 | if (td->open_count > 1) { |
| 1131 | __close_device(td); |
| 1132 | return -EBUSY; |
| 1133 | } |
| 1134 | |
| 1135 | list_del(&td->list); |
| 1136 | kfree(td); |
| 1137 | r = dm_btree_remove(&pmd->details_info, pmd->details_root, |
| 1138 | &key, &pmd->details_root); |
| 1139 | if (r) |
| 1140 | return r; |
| 1141 | |
| 1142 | r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 1143 | if (r) |
| 1144 | return r; |
| 1145 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd, |
| 1150 | dm_thin_id dev) |
| 1151 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1152 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1153 | |
| 1154 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1155 | if (!pmd->fail_io) |
| 1156 | r = __delete_device(pmd, dev); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1157 | up_write(&pmd->root_lock); |
| 1158 | |
| 1159 | return r; |
| 1160 | } |
| 1161 | |
| 1162 | int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd, |
| 1163 | uint64_t current_id, |
| 1164 | uint64_t new_id) |
| 1165 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1166 | int r = -EINVAL; |
| 1167 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1168 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1169 | |
| 1170 | if (pmd->fail_io) |
| 1171 | goto out; |
| 1172 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1173 | if (pmd->trans_id != current_id) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1174 | DMERR("mismatched transaction id"); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1175 | goto out; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | pmd->trans_id = new_id; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1179 | r = 0; |
| 1180 | |
| 1181 | out: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1182 | up_write(&pmd->root_lock); |
| 1183 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1184 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd, |
| 1188 | uint64_t *result) |
| 1189 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1190 | int r = -EINVAL; |
| 1191 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1192 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1193 | if (!pmd->fail_io) { |
| 1194 | *result = pmd->trans_id; |
| 1195 | r = 0; |
| 1196 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1197 | up_read(&pmd->root_lock); |
| 1198 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1199 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1202 | static int __reserve_metadata_snap(struct dm_pool_metadata *pmd) |
| 1203 | { |
| 1204 | int r, inc; |
| 1205 | struct thin_disk_superblock *disk_super; |
| 1206 | struct dm_block *copy, *sblock; |
| 1207 | dm_block_t held_root; |
| 1208 | |
| 1209 | /* |
Joe Thornber | 49e99fc | 2015-12-09 16:23:24 +0000 | [diff] [blame] | 1210 | * We commit to ensure the btree roots which we increment in a |
| 1211 | * moment are up to date. |
| 1212 | */ |
| 1213 | __commit_transaction(pmd); |
| 1214 | |
| 1215 | /* |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1216 | * Copy the superblock. |
| 1217 | */ |
| 1218 | dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION); |
| 1219 | r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION, |
| 1220 | &sb_validator, ©, &inc); |
| 1221 | if (r) |
| 1222 | return r; |
| 1223 | |
| 1224 | BUG_ON(!inc); |
| 1225 | |
| 1226 | held_root = dm_block_location(copy); |
| 1227 | disk_super = dm_block_data(copy); |
| 1228 | |
| 1229 | if (le64_to_cpu(disk_super->held_root)) { |
| 1230 | DMWARN("Pool metadata snapshot already exists: release this before taking another."); |
| 1231 | |
| 1232 | dm_tm_dec(pmd->tm, held_root); |
| 1233 | dm_tm_unlock(pmd->tm, copy); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1234 | return -EBUSY; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Wipe the spacemap since we're not publishing this. |
| 1239 | */ |
| 1240 | memset(&disk_super->data_space_map_root, 0, |
| 1241 | sizeof(disk_super->data_space_map_root)); |
| 1242 | memset(&disk_super->metadata_space_map_root, 0, |
| 1243 | sizeof(disk_super->metadata_space_map_root)); |
| 1244 | |
| 1245 | /* |
| 1246 | * Increment the data structures that need to be preserved. |
| 1247 | */ |
| 1248 | dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root)); |
| 1249 | dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root)); |
| 1250 | dm_tm_unlock(pmd->tm, copy); |
| 1251 | |
| 1252 | /* |
| 1253 | * Write the held root into the superblock. |
| 1254 | */ |
Joe Thornber | 2597119 | 2012-07-27 15:08:09 +0100 | [diff] [blame] | 1255 | r = superblock_lock(pmd, &sblock); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1256 | if (r) { |
| 1257 | dm_tm_dec(pmd->tm, held_root); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1258 | return r; |
| 1259 | } |
| 1260 | |
| 1261 | disk_super = dm_block_data(sblock); |
| 1262 | disk_super->held_root = cpu_to_le64(held_root); |
| 1263 | dm_bm_unlock(sblock); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd) |
| 1268 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1269 | int r = -EINVAL; |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1270 | |
| 1271 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1272 | if (!pmd->fail_io) |
| 1273 | r = __reserve_metadata_snap(pmd); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1274 | up_write(&pmd->root_lock); |
| 1275 | |
| 1276 | return r; |
| 1277 | } |
| 1278 | |
| 1279 | static int __release_metadata_snap(struct dm_pool_metadata *pmd) |
| 1280 | { |
| 1281 | int r; |
| 1282 | struct thin_disk_superblock *disk_super; |
| 1283 | struct dm_block *sblock, *copy; |
| 1284 | dm_block_t held_root; |
| 1285 | |
Joe Thornber | 2597119 | 2012-07-27 15:08:09 +0100 | [diff] [blame] | 1286 | r = superblock_lock(pmd, &sblock); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1287 | if (r) |
| 1288 | return r; |
| 1289 | |
| 1290 | disk_super = dm_block_data(sblock); |
| 1291 | held_root = le64_to_cpu(disk_super->held_root); |
| 1292 | disk_super->held_root = cpu_to_le64(0); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1293 | |
| 1294 | dm_bm_unlock(sblock); |
| 1295 | |
| 1296 | if (!held_root) { |
| 1297 | DMWARN("No pool metadata snapshot found: nothing to release."); |
| 1298 | return -EINVAL; |
| 1299 | } |
| 1300 | |
| 1301 | r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, ©); |
| 1302 | if (r) |
| 1303 | return r; |
| 1304 | |
| 1305 | disk_super = dm_block_data(copy); |
Joe Thornber | 7f518ad | 2015-08-12 15:10:21 +0100 | [diff] [blame] | 1306 | dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root)); |
| 1307 | dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root)); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1308 | dm_sm_dec_block(pmd->metadata_sm, held_root); |
| 1309 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 1310 | dm_tm_unlock(pmd->tm, copy); |
| 1311 | |
| 1312 | return 0; |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd) |
| 1316 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1317 | int r = -EINVAL; |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1318 | |
| 1319 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1320 | if (!pmd->fail_io) |
| 1321 | r = __release_metadata_snap(pmd); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1322 | up_write(&pmd->root_lock); |
| 1323 | |
| 1324 | return r; |
| 1325 | } |
| 1326 | |
| 1327 | static int __get_metadata_snap(struct dm_pool_metadata *pmd, |
| 1328 | dm_block_t *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1329 | { |
| 1330 | int r; |
| 1331 | struct thin_disk_superblock *disk_super; |
| 1332 | struct dm_block *sblock; |
| 1333 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1334 | r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 1335 | &sb_validator, &sblock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1336 | if (r) |
| 1337 | return r; |
| 1338 | |
| 1339 | disk_super = dm_block_data(sblock); |
| 1340 | *result = le64_to_cpu(disk_super->held_root); |
| 1341 | |
Mikulas Patocka | 4c7da06 | 2015-10-22 16:46:59 -0400 | [diff] [blame] | 1342 | dm_bm_unlock(sblock); |
| 1343 | |
| 1344 | return 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1347 | int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd, |
| 1348 | dm_block_t *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1349 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1350 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1351 | |
| 1352 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1353 | if (!pmd->fail_io) |
| 1354 | r = __get_metadata_snap(pmd, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1355 | up_read(&pmd->root_lock); |
| 1356 | |
| 1357 | return r; |
| 1358 | } |
| 1359 | |
| 1360 | int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev, |
| 1361 | struct dm_thin_device **td) |
| 1362 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1363 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1364 | |
| 1365 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1366 | if (!pmd->fail_io) |
| 1367 | r = __open_device(pmd, dev, 0, td); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1368 | up_write(&pmd->root_lock); |
| 1369 | |
| 1370 | return r; |
| 1371 | } |
| 1372 | |
| 1373 | int dm_pool_close_thin_device(struct dm_thin_device *td) |
| 1374 | { |
| 1375 | down_write(&td->pmd->root_lock); |
| 1376 | __close_device(td); |
| 1377 | up_write(&td->pmd->root_lock); |
| 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | dm_thin_id dm_thin_dev_id(struct dm_thin_device *td) |
| 1383 | { |
| 1384 | return td->id; |
| 1385 | } |
| 1386 | |
Joe Thornber | 19fa1a6 | 2013-12-17 12:09:40 -0500 | [diff] [blame] | 1387 | /* |
| 1388 | * Check whether @time (of block creation) is older than @td's last snapshot. |
| 1389 | * If so then the associated block is shared with the last snapshot device. |
| 1390 | * Any block on a device created *after* the device last got snapshotted is |
| 1391 | * necessarily not shared. |
| 1392 | */ |
Mike Snitzer | 17b7d63 | 2012-07-27 15:07:57 +0100 | [diff] [blame] | 1393 | static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1394 | { |
| 1395 | return td->snapshotted_time > time; |
| 1396 | } |
| 1397 | |
| 1398 | int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block, |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1399 | int can_issue_io, struct dm_thin_lookup_result *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1400 | { |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1401 | int r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1402 | __le64 value; |
| 1403 | struct dm_pool_metadata *pmd = td->pmd; |
| 1404 | dm_block_t keys[2] = { td->id, block }; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1405 | struct dm_btree_info *info; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1406 | |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1407 | down_read(&pmd->root_lock); |
Joe Thornber | b1f11af | 2015-06-11 17:11:48 +0100 | [diff] [blame] | 1408 | if (pmd->fail_io) { |
| 1409 | up_read(&pmd->root_lock); |
| 1410 | return -EINVAL; |
| 1411 | } |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1412 | |
| 1413 | if (can_issue_io) { |
| 1414 | info = &pmd->info; |
| 1415 | } else |
| 1416 | info = &pmd->nb_info; |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1417 | |
| 1418 | r = dm_btree_lookup(info, pmd->root, keys, &value); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1419 | if (!r) { |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1420 | uint64_t block_time = 0; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1421 | dm_block_t exception_block; |
| 1422 | uint32_t exception_time; |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1423 | |
| 1424 | block_time = le64_to_cpu(value); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1425 | unpack_block_time(block_time, &exception_block, |
| 1426 | &exception_time); |
| 1427 | result->block = exception_block; |
| 1428 | result->shared = __snapshotted_since(td, exception_time); |
| 1429 | } |
| 1430 | |
Joe Thornber | e5cfc69 | 2014-10-06 15:24:55 +0100 | [diff] [blame] | 1431 | up_read(&pmd->root_lock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1432 | return r; |
| 1433 | } |
| 1434 | |
Joe Thornber | a5d895a | 2015-04-16 12:47:21 +0100 | [diff] [blame] | 1435 | /* FIXME: write a more efficient one in btree */ |
| 1436 | int dm_thin_find_mapped_range(struct dm_thin_device *td, |
| 1437 | dm_block_t begin, dm_block_t end, |
| 1438 | dm_block_t *thin_begin, dm_block_t *thin_end, |
| 1439 | dm_block_t *pool_begin, bool *maybe_shared) |
| 1440 | { |
| 1441 | int r; |
| 1442 | dm_block_t pool_end; |
| 1443 | struct dm_thin_lookup_result lookup; |
| 1444 | |
| 1445 | if (end < begin) |
| 1446 | return -ENODATA; |
| 1447 | |
| 1448 | /* |
| 1449 | * Find first mapped block. |
| 1450 | */ |
| 1451 | while (begin < end) { |
| 1452 | r = dm_thin_find_block(td, begin, true, &lookup); |
| 1453 | if (r) { |
| 1454 | if (r != -ENODATA) |
| 1455 | return r; |
| 1456 | } else |
| 1457 | break; |
| 1458 | |
| 1459 | begin++; |
| 1460 | } |
| 1461 | |
| 1462 | if (begin == end) |
| 1463 | return -ENODATA; |
| 1464 | |
| 1465 | *thin_begin = begin; |
| 1466 | *pool_begin = lookup.block; |
| 1467 | *maybe_shared = lookup.shared; |
| 1468 | |
| 1469 | begin++; |
| 1470 | pool_end = *pool_begin + 1; |
| 1471 | while (begin != end) { |
| 1472 | r = dm_thin_find_block(td, begin, true, &lookup); |
| 1473 | if (r) { |
| 1474 | if (r == -ENODATA) |
| 1475 | break; |
| 1476 | else |
| 1477 | return r; |
| 1478 | } |
| 1479 | |
| 1480 | if ((lookup.block != pool_end) || |
| 1481 | (lookup.shared != *maybe_shared)) |
| 1482 | break; |
| 1483 | |
| 1484 | pool_end++; |
| 1485 | begin++; |
| 1486 | } |
| 1487 | |
| 1488 | *thin_end = begin; |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1492 | static int __insert(struct dm_thin_device *td, dm_block_t block, |
| 1493 | dm_block_t data_block) |
| 1494 | { |
| 1495 | int r, inserted; |
| 1496 | __le64 value; |
| 1497 | struct dm_pool_metadata *pmd = td->pmd; |
| 1498 | dm_block_t keys[2] = { td->id, block }; |
| 1499 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1500 | value = cpu_to_le64(pack_block_time(data_block, pmd->time)); |
| 1501 | __dm_bless_for_disk(&value); |
| 1502 | |
| 1503 | r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value, |
| 1504 | &pmd->root, &inserted); |
| 1505 | if (r) |
| 1506 | return r; |
| 1507 | |
Joe Thornber | 40db5a5 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 1508 | td->changed = 1; |
| 1509 | if (inserted) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1510 | td->mapped_blocks++; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block, |
| 1516 | dm_block_t data_block) |
| 1517 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1518 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1519 | |
| 1520 | down_write(&td->pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1521 | if (!td->pmd->fail_io) |
| 1522 | r = __insert(td, block, data_block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1523 | up_write(&td->pmd->root_lock); |
| 1524 | |
| 1525 | return r; |
| 1526 | } |
| 1527 | |
| 1528 | static int __remove(struct dm_thin_device *td, dm_block_t block) |
| 1529 | { |
| 1530 | int r; |
| 1531 | struct dm_pool_metadata *pmd = td->pmd; |
| 1532 | dm_block_t keys[2] = { td->id, block }; |
| 1533 | |
| 1534 | r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root); |
| 1535 | if (r) |
| 1536 | return r; |
| 1537 | |
Joe Thornber | af63bcb | 2012-03-07 19:09:44 +0000 | [diff] [blame] | 1538 | td->mapped_blocks--; |
| 1539 | td->changed = 1; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
Joe Thornber | 6550f07 | 2015-04-13 09:45:25 +0100 | [diff] [blame] | 1544 | static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end) |
| 1545 | { |
| 1546 | int r; |
Joe Thornber | 993ceab | 2015-12-02 12:24:39 +0000 | [diff] [blame] | 1547 | unsigned count, total_count = 0; |
Joe Thornber | 6550f07 | 2015-04-13 09:45:25 +0100 | [diff] [blame] | 1548 | struct dm_pool_metadata *pmd = td->pmd; |
| 1549 | dm_block_t keys[1] = { td->id }; |
| 1550 | __le64 value; |
| 1551 | dm_block_t mapping_root; |
| 1552 | |
| 1553 | /* |
| 1554 | * Find the mapping tree |
| 1555 | */ |
| 1556 | r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value); |
| 1557 | if (r) |
| 1558 | return r; |
| 1559 | |
| 1560 | /* |
| 1561 | * Remove from the mapping tree, taking care to inc the |
| 1562 | * ref count so it doesn't get deleted. |
| 1563 | */ |
| 1564 | mapping_root = le64_to_cpu(value); |
| 1565 | dm_tm_inc(pmd->tm, mapping_root); |
| 1566 | r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root); |
| 1567 | if (r) |
| 1568 | return r; |
| 1569 | |
Joe Thornber | 993ceab | 2015-12-02 12:24:39 +0000 | [diff] [blame] | 1570 | /* |
| 1571 | * Remove leaves stops at the first unmapped entry, so we have to |
| 1572 | * loop round finding mapped ranges. |
| 1573 | */ |
| 1574 | while (begin < end) { |
| 1575 | r = dm_btree_lookup_next(&pmd->bl_info, mapping_root, &begin, &begin, &value); |
| 1576 | if (r == -ENODATA) |
| 1577 | break; |
Joe Thornber | 6550f07 | 2015-04-13 09:45:25 +0100 | [diff] [blame] | 1578 | |
Joe Thornber | 993ceab | 2015-12-02 12:24:39 +0000 | [diff] [blame] | 1579 | if (r) |
| 1580 | return r; |
| 1581 | |
| 1582 | if (begin >= end) |
| 1583 | break; |
| 1584 | |
| 1585 | r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count); |
| 1586 | if (r) |
| 1587 | return r; |
| 1588 | |
| 1589 | total_count += count; |
| 1590 | } |
| 1591 | |
| 1592 | td->mapped_blocks -= total_count; |
Joe Thornber | 6550f07 | 2015-04-13 09:45:25 +0100 | [diff] [blame] | 1593 | td->changed = 1; |
| 1594 | |
| 1595 | /* |
| 1596 | * Reinsert the mapping tree. |
| 1597 | */ |
| 1598 | value = cpu_to_le64(mapping_root); |
| 1599 | __dm_bless_for_disk(&value); |
| 1600 | return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root); |
| 1601 | } |
| 1602 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1603 | int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block) |
| 1604 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1605 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1606 | |
| 1607 | down_write(&td->pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1608 | if (!td->pmd->fail_io) |
| 1609 | r = __remove(td, block); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1610 | up_write(&td->pmd->root_lock); |
| 1611 | |
| 1612 | return r; |
| 1613 | } |
| 1614 | |
Joe Thornber | 6550f07 | 2015-04-13 09:45:25 +0100 | [diff] [blame] | 1615 | int dm_thin_remove_range(struct dm_thin_device *td, |
| 1616 | dm_block_t begin, dm_block_t end) |
| 1617 | { |
| 1618 | int r = -EINVAL; |
| 1619 | |
| 1620 | down_write(&td->pmd->root_lock); |
| 1621 | if (!td->pmd->fail_io) |
| 1622 | r = __remove_range(td, begin, end); |
| 1623 | up_write(&td->pmd->root_lock); |
| 1624 | |
| 1625 | return r; |
| 1626 | } |
| 1627 | |
Joe Thornber | 19fa1a6 | 2013-12-17 12:09:40 -0500 | [diff] [blame] | 1628 | int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result) |
| 1629 | { |
| 1630 | int r; |
| 1631 | uint32_t ref_count; |
| 1632 | |
| 1633 | down_read(&pmd->root_lock); |
| 1634 | r = dm_sm_get_count(pmd->data_sm, b, &ref_count); |
| 1635 | if (!r) |
| 1636 | *result = (ref_count != 0); |
| 1637 | up_read(&pmd->root_lock); |
| 1638 | |
| 1639 | return r; |
| 1640 | } |
| 1641 | |
Joe Thornber | 40db5a5 | 2012-07-27 15:08:14 +0100 | [diff] [blame] | 1642 | bool dm_thin_changed_this_transaction(struct dm_thin_device *td) |
| 1643 | { |
| 1644 | int r; |
| 1645 | |
| 1646 | down_read(&td->pmd->root_lock); |
| 1647 | r = td->changed; |
| 1648 | up_read(&td->pmd->root_lock); |
| 1649 | |
| 1650 | return r; |
| 1651 | } |
| 1652 | |
Mike Snitzer | 4d1662a | 2014-02-06 06:08:56 -0500 | [diff] [blame] | 1653 | bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd) |
| 1654 | { |
| 1655 | bool r = false; |
| 1656 | struct dm_thin_device *td, *tmp; |
| 1657 | |
| 1658 | down_read(&pmd->root_lock); |
| 1659 | list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) { |
| 1660 | if (td->changed) { |
| 1661 | r = td->changed; |
| 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | up_read(&pmd->root_lock); |
| 1666 | |
| 1667 | return r; |
| 1668 | } |
| 1669 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1670 | bool dm_thin_aborted_changes(struct dm_thin_device *td) |
| 1671 | { |
| 1672 | bool r; |
| 1673 | |
| 1674 | down_read(&td->pmd->root_lock); |
| 1675 | r = td->aborted_with_changes; |
| 1676 | up_read(&td->pmd->root_lock); |
| 1677 | |
| 1678 | return r; |
| 1679 | } |
| 1680 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1681 | int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1682 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1683 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1684 | |
| 1685 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1686 | if (!pmd->fail_io) |
| 1687 | r = dm_sm_new_block(pmd->data_sm, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1688 | up_write(&pmd->root_lock); |
| 1689 | |
| 1690 | return r; |
| 1691 | } |
| 1692 | |
| 1693 | int dm_pool_commit_metadata(struct dm_pool_metadata *pmd) |
| 1694 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1695 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1696 | |
| 1697 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1698 | if (pmd->fail_io) |
| 1699 | goto out; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1700 | |
| 1701 | r = __commit_transaction(pmd); |
| 1702 | if (r <= 0) |
| 1703 | goto out; |
| 1704 | |
| 1705 | /* |
| 1706 | * Open the next transaction. |
| 1707 | */ |
| 1708 | r = __begin_transaction(pmd); |
| 1709 | out: |
| 1710 | up_write(&pmd->root_lock); |
| 1711 | return r; |
| 1712 | } |
| 1713 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1714 | static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd) |
| 1715 | { |
| 1716 | struct dm_thin_device *td; |
| 1717 | |
| 1718 | list_for_each_entry(td, &pmd->thin_devices, list) |
| 1719 | td->aborted_with_changes = td->changed; |
| 1720 | } |
| 1721 | |
| 1722 | int dm_pool_abort_metadata(struct dm_pool_metadata *pmd) |
| 1723 | { |
| 1724 | int r = -EINVAL; |
| 1725 | |
| 1726 | down_write(&pmd->root_lock); |
| 1727 | if (pmd->fail_io) |
| 1728 | goto out; |
| 1729 | |
| 1730 | __set_abort_with_changes_flags(pmd); |
| 1731 | __destroy_persistent_data_objects(pmd); |
| 1732 | r = __create_persistent_data_objects(pmd, false); |
| 1733 | if (r) |
| 1734 | pmd->fail_io = true; |
| 1735 | |
| 1736 | out: |
| 1737 | up_write(&pmd->root_lock); |
| 1738 | |
| 1739 | return r; |
| 1740 | } |
| 1741 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1742 | int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1743 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1744 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1745 | |
| 1746 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1747 | if (!pmd->fail_io) |
| 1748 | r = dm_sm_get_nr_free(pmd->data_sm, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1749 | up_read(&pmd->root_lock); |
| 1750 | |
| 1751 | return r; |
| 1752 | } |
| 1753 | |
| 1754 | int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd, |
| 1755 | dm_block_t *result) |
| 1756 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1757 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1758 | |
| 1759 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1760 | if (!pmd->fail_io) |
| 1761 | r = dm_sm_get_nr_free(pmd->metadata_sm, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1762 | up_read(&pmd->root_lock); |
| 1763 | |
| 1764 | return r; |
| 1765 | } |
| 1766 | |
| 1767 | int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd, |
| 1768 | dm_block_t *result) |
| 1769 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1770 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1771 | |
| 1772 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1773 | if (!pmd->fail_io) |
| 1774 | r = dm_sm_get_nr_blocks(pmd->metadata_sm, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1775 | up_read(&pmd->root_lock); |
| 1776 | |
| 1777 | return r; |
| 1778 | } |
| 1779 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1780 | int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1781 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1782 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1783 | |
| 1784 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1785 | if (!pmd->fail_io) |
| 1786 | r = dm_sm_get_nr_blocks(pmd->data_sm, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1787 | up_read(&pmd->root_lock); |
| 1788 | |
| 1789 | return r; |
| 1790 | } |
| 1791 | |
| 1792 | int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result) |
| 1793 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1794 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1795 | struct dm_pool_metadata *pmd = td->pmd; |
| 1796 | |
| 1797 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1798 | if (!pmd->fail_io) { |
| 1799 | *result = td->mapped_blocks; |
| 1800 | r = 0; |
| 1801 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1802 | up_read(&pmd->root_lock); |
| 1803 | |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1804 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | static int __highest_block(struct dm_thin_device *td, dm_block_t *result) |
| 1808 | { |
| 1809 | int r; |
| 1810 | __le64 value_le; |
| 1811 | dm_block_t thin_root; |
| 1812 | struct dm_pool_metadata *pmd = td->pmd; |
| 1813 | |
| 1814 | r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le); |
| 1815 | if (r) |
| 1816 | return r; |
| 1817 | |
| 1818 | thin_root = le64_to_cpu(value_le); |
| 1819 | |
| 1820 | return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result); |
| 1821 | } |
| 1822 | |
| 1823 | int dm_thin_get_highest_mapped_block(struct dm_thin_device *td, |
| 1824 | dm_block_t *result) |
| 1825 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1826 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1827 | struct dm_pool_metadata *pmd = td->pmd; |
| 1828 | |
| 1829 | down_read(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1830 | if (!pmd->fail_io) |
| 1831 | r = __highest_block(td, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1832 | up_read(&pmd->root_lock); |
| 1833 | |
| 1834 | return r; |
| 1835 | } |
| 1836 | |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1837 | static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1838 | { |
| 1839 | int r; |
| 1840 | dm_block_t old_count; |
| 1841 | |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1842 | r = dm_sm_get_nr_blocks(sm, &old_count); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1843 | if (r) |
| 1844 | return r; |
| 1845 | |
| 1846 | if (new_count == old_count) |
| 1847 | return 0; |
| 1848 | |
| 1849 | if (new_count < old_count) { |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1850 | DMERR("cannot reduce size of space map"); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1851 | return -EINVAL; |
| 1852 | } |
| 1853 | |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1854 | return dm_sm_extend(sm, new_count - old_count); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count) |
| 1858 | { |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1859 | int r = -EINVAL; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1860 | |
| 1861 | down_write(&pmd->root_lock); |
Joe Thornber | da105ed | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1862 | if (!pmd->fail_io) |
Joe Thornber | b17446d | 2013-05-10 14:37:18 +0100 | [diff] [blame] | 1863 | r = __resize_space_map(pmd->data_sm, new_count); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1864 | up_write(&pmd->root_lock); |
| 1865 | |
| 1866 | return r; |
| 1867 | } |
Joe Thornber | 12ba58a | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1868 | |
Joe Thornber | 24347e9 | 2013-05-10 14:37:19 +0100 | [diff] [blame] | 1869 | int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count) |
| 1870 | { |
| 1871 | int r = -EINVAL; |
| 1872 | |
| 1873 | down_write(&pmd->root_lock); |
| 1874 | if (!pmd->fail_io) |
| 1875 | r = __resize_space_map(pmd->metadata_sm, new_count); |
| 1876 | up_write(&pmd->root_lock); |
| 1877 | |
| 1878 | return r; |
| 1879 | } |
| 1880 | |
Joe Thornber | 12ba58a | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1881 | void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd) |
| 1882 | { |
| 1883 | down_write(&pmd->root_lock); |
Joe Thornber | 12ba58a | 2012-07-27 15:08:15 +0100 | [diff] [blame] | 1884 | dm_bm_set_read_only(pmd->bm); |
| 1885 | up_write(&pmd->root_lock); |
| 1886 | } |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1887 | |
Joe Thornber | 9b7aaa6 | 2013-12-04 16:58:19 -0500 | [diff] [blame] | 1888 | void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd) |
| 1889 | { |
| 1890 | down_write(&pmd->root_lock); |
Joe Thornber | 9b7aaa6 | 2013-12-04 16:58:19 -0500 | [diff] [blame] | 1891 | dm_bm_set_read_write(pmd->bm); |
| 1892 | up_write(&pmd->root_lock); |
| 1893 | } |
| 1894 | |
Joe Thornber | ac8c3f3 | 2013-05-10 14:37:21 +0100 | [diff] [blame] | 1895 | int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd, |
| 1896 | dm_block_t threshold, |
| 1897 | dm_sm_threshold_fn fn, |
| 1898 | void *context) |
| 1899 | { |
| 1900 | int r; |
| 1901 | |
| 1902 | down_write(&pmd->root_lock); |
| 1903 | r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context); |
| 1904 | up_write(&pmd->root_lock); |
| 1905 | |
| 1906 | return r; |
| 1907 | } |
Mike Snitzer | 07f2b6e | 2014-02-14 11:58:41 -0500 | [diff] [blame] | 1908 | |
| 1909 | int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd) |
| 1910 | { |
| 1911 | int r; |
| 1912 | struct dm_block *sblock; |
| 1913 | struct thin_disk_superblock *disk_super; |
| 1914 | |
| 1915 | down_write(&pmd->root_lock); |
| 1916 | pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG; |
| 1917 | |
| 1918 | r = superblock_lock(pmd, &sblock); |
| 1919 | if (r) { |
| 1920 | DMERR("couldn't read superblock"); |
| 1921 | goto out; |
| 1922 | } |
| 1923 | |
| 1924 | disk_super = dm_block_data(sblock); |
| 1925 | disk_super->flags = cpu_to_le32(pmd->flags); |
| 1926 | |
| 1927 | dm_bm_unlock(sblock); |
| 1928 | out: |
| 1929 | up_write(&pmd->root_lock); |
| 1930 | return r; |
| 1931 | } |
| 1932 | |
| 1933 | bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd) |
| 1934 | { |
| 1935 | bool needs_check; |
| 1936 | |
| 1937 | down_read(&pmd->root_lock); |
| 1938 | needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG; |
| 1939 | up_read(&pmd->root_lock); |
| 1940 | |
| 1941 | return needs_check; |
| 1942 | } |
Joe Thornber | 8a01a6a | 2014-10-06 15:28:30 +0100 | [diff] [blame] | 1943 | |
| 1944 | void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd) |
| 1945 | { |
| 1946 | dm_tm_issue_prefetches(pmd->tm); |
| 1947 | } |