Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Red Hat, Inc. |
| 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 |
| 79 | #define THIN_VERSION 1 |
| 80 | #define THIN_METADATA_CACHE_SIZE 64 |
| 81 | #define SECTOR_TO_BLOCK_SHIFT 3 |
| 82 | |
| 83 | /* This should be plenty */ |
| 84 | #define SPACE_MAP_ROOT_SIZE 128 |
| 85 | |
| 86 | /* |
| 87 | * Little endian on-disk superblock and device details. |
| 88 | */ |
| 89 | struct thin_disk_superblock { |
| 90 | __le32 csum; /* Checksum of superblock except for this field. */ |
| 91 | __le32 flags; |
| 92 | __le64 blocknr; /* This block number, dm_block_t. */ |
| 93 | |
| 94 | __u8 uuid[16]; |
| 95 | __le64 magic; |
| 96 | __le32 version; |
| 97 | __le32 time; |
| 98 | |
| 99 | __le64 trans_id; |
| 100 | |
| 101 | /* |
| 102 | * Root held by userspace transactions. |
| 103 | */ |
| 104 | __le64 held_root; |
| 105 | |
| 106 | __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 107 | __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE]; |
| 108 | |
| 109 | /* |
| 110 | * 2-level btree mapping (dev_id, (dev block, time)) -> data block |
| 111 | */ |
| 112 | __le64 data_mapping_root; |
| 113 | |
| 114 | /* |
| 115 | * Device detail root mapping dev_id -> device_details |
| 116 | */ |
| 117 | __le64 device_details_root; |
| 118 | |
| 119 | __le32 data_block_size; /* In 512-byte sectors. */ |
| 120 | |
| 121 | __le32 metadata_block_size; /* In 512-byte sectors. */ |
| 122 | __le64 metadata_nr_blocks; |
| 123 | |
| 124 | __le32 compat_flags; |
| 125 | __le32 compat_ro_flags; |
| 126 | __le32 incompat_flags; |
| 127 | } __packed; |
| 128 | |
| 129 | struct disk_device_details { |
| 130 | __le64 mapped_blocks; |
| 131 | __le64 transaction_id; /* When created. */ |
| 132 | __le32 creation_time; |
| 133 | __le32 snapshotted_time; |
| 134 | } __packed; |
| 135 | |
| 136 | struct dm_pool_metadata { |
| 137 | struct hlist_node hash; |
| 138 | |
| 139 | struct block_device *bdev; |
| 140 | struct dm_block_manager *bm; |
| 141 | struct dm_space_map *metadata_sm; |
| 142 | struct dm_space_map *data_sm; |
| 143 | struct dm_transaction_manager *tm; |
| 144 | struct dm_transaction_manager *nb_tm; |
| 145 | |
| 146 | /* |
| 147 | * Two-level btree. |
| 148 | * First level holds thin_dev_t. |
| 149 | * Second level holds mappings. |
| 150 | */ |
| 151 | struct dm_btree_info info; |
| 152 | |
| 153 | /* |
| 154 | * Non-blocking version of the above. |
| 155 | */ |
| 156 | struct dm_btree_info nb_info; |
| 157 | |
| 158 | /* |
| 159 | * Just the top level for deleting whole devices. |
| 160 | */ |
| 161 | struct dm_btree_info tl_info; |
| 162 | |
| 163 | /* |
| 164 | * Just the bottom level for creating new devices. |
| 165 | */ |
| 166 | struct dm_btree_info bl_info; |
| 167 | |
| 168 | /* |
| 169 | * Describes the device details btree. |
| 170 | */ |
| 171 | struct dm_btree_info details_info; |
| 172 | |
| 173 | struct rw_semaphore root_lock; |
| 174 | uint32_t time; |
| 175 | int need_commit; |
| 176 | dm_block_t root; |
| 177 | dm_block_t details_root; |
| 178 | struct list_head thin_devices; |
| 179 | uint64_t trans_id; |
| 180 | unsigned long flags; |
| 181 | sector_t data_block_size; |
| 182 | }; |
| 183 | |
| 184 | struct dm_thin_device { |
| 185 | struct list_head list; |
| 186 | struct dm_pool_metadata *pmd; |
| 187 | dm_thin_id id; |
| 188 | |
| 189 | int open_count; |
| 190 | int changed; |
| 191 | uint64_t mapped_blocks; |
| 192 | uint64_t transaction_id; |
| 193 | uint32_t creation_time; |
| 194 | uint32_t snapshotted_time; |
| 195 | }; |
| 196 | |
| 197 | /*---------------------------------------------------------------- |
| 198 | * superblock validator |
| 199 | *--------------------------------------------------------------*/ |
| 200 | |
| 201 | #define SUPERBLOCK_CSUM_XOR 160774 |
| 202 | |
| 203 | static void sb_prepare_for_write(struct dm_block_validator *v, |
| 204 | struct dm_block *b, |
| 205 | size_t block_size) |
| 206 | { |
| 207 | struct thin_disk_superblock *disk_super = dm_block_data(b); |
| 208 | |
| 209 | disk_super->blocknr = cpu_to_le64(dm_block_location(b)); |
| 210 | disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 211 | block_size - sizeof(__le32), |
| 212 | SUPERBLOCK_CSUM_XOR)); |
| 213 | } |
| 214 | |
| 215 | static int sb_check(struct dm_block_validator *v, |
| 216 | struct dm_block *b, |
| 217 | size_t block_size) |
| 218 | { |
| 219 | struct thin_disk_superblock *disk_super = dm_block_data(b); |
| 220 | __le32 csum_le; |
| 221 | |
| 222 | if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) { |
| 223 | DMERR("sb_check failed: blocknr %llu: " |
| 224 | "wanted %llu", le64_to_cpu(disk_super->blocknr), |
| 225 | (unsigned long long)dm_block_location(b)); |
| 226 | return -ENOTBLK; |
| 227 | } |
| 228 | |
| 229 | if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) { |
| 230 | DMERR("sb_check failed: magic %llu: " |
| 231 | "wanted %llu", le64_to_cpu(disk_super->magic), |
| 232 | (unsigned long long)THIN_SUPERBLOCK_MAGIC); |
| 233 | return -EILSEQ; |
| 234 | } |
| 235 | |
| 236 | csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags, |
| 237 | block_size - sizeof(__le32), |
| 238 | SUPERBLOCK_CSUM_XOR)); |
| 239 | if (csum_le != disk_super->csum) { |
| 240 | DMERR("sb_check failed: csum %u: wanted %u", |
| 241 | le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum)); |
| 242 | return -EILSEQ; |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static struct dm_block_validator sb_validator = { |
| 249 | .name = "superblock", |
| 250 | .prepare_for_write = sb_prepare_for_write, |
| 251 | .check = sb_check |
| 252 | }; |
| 253 | |
| 254 | /*---------------------------------------------------------------- |
| 255 | * Methods for the btree value types |
| 256 | *--------------------------------------------------------------*/ |
| 257 | |
| 258 | static uint64_t pack_block_time(dm_block_t b, uint32_t t) |
| 259 | { |
| 260 | return (b << 24) | t; |
| 261 | } |
| 262 | |
| 263 | static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t) |
| 264 | { |
| 265 | *b = v >> 24; |
| 266 | *t = v & ((1 << 24) - 1); |
| 267 | } |
| 268 | |
| 269 | static void data_block_inc(void *context, void *value_le) |
| 270 | { |
| 271 | struct dm_space_map *sm = context; |
| 272 | __le64 v_le; |
| 273 | uint64_t b; |
| 274 | uint32_t t; |
| 275 | |
| 276 | memcpy(&v_le, value_le, sizeof(v_le)); |
| 277 | unpack_block_time(le64_to_cpu(v_le), &b, &t); |
| 278 | dm_sm_inc_block(sm, b); |
| 279 | } |
| 280 | |
| 281 | static void data_block_dec(void *context, void *value_le) |
| 282 | { |
| 283 | struct dm_space_map *sm = context; |
| 284 | __le64 v_le; |
| 285 | uint64_t b; |
| 286 | uint32_t t; |
| 287 | |
| 288 | memcpy(&v_le, value_le, sizeof(v_le)); |
| 289 | unpack_block_time(le64_to_cpu(v_le), &b, &t); |
| 290 | dm_sm_dec_block(sm, b); |
| 291 | } |
| 292 | |
| 293 | static int data_block_equal(void *context, void *value1_le, void *value2_le) |
| 294 | { |
| 295 | __le64 v1_le, v2_le; |
| 296 | uint64_t b1, b2; |
| 297 | uint32_t t; |
| 298 | |
| 299 | memcpy(&v1_le, value1_le, sizeof(v1_le)); |
| 300 | memcpy(&v2_le, value2_le, sizeof(v2_le)); |
| 301 | unpack_block_time(le64_to_cpu(v1_le), &b1, &t); |
| 302 | unpack_block_time(le64_to_cpu(v2_le), &b2, &t); |
| 303 | |
| 304 | return b1 == b2; |
| 305 | } |
| 306 | |
| 307 | static void subtree_inc(void *context, void *value) |
| 308 | { |
| 309 | struct dm_btree_info *info = context; |
| 310 | __le64 root_le; |
| 311 | uint64_t root; |
| 312 | |
| 313 | memcpy(&root_le, value, sizeof(root_le)); |
| 314 | root = le64_to_cpu(root_le); |
| 315 | dm_tm_inc(info->tm, root); |
| 316 | } |
| 317 | |
| 318 | static void subtree_dec(void *context, void *value) |
| 319 | { |
| 320 | struct dm_btree_info *info = context; |
| 321 | __le64 root_le; |
| 322 | uint64_t root; |
| 323 | |
| 324 | memcpy(&root_le, value, sizeof(root_le)); |
| 325 | root = le64_to_cpu(root_le); |
| 326 | if (dm_btree_del(info, root)) |
| 327 | DMERR("btree delete failed\n"); |
| 328 | } |
| 329 | |
| 330 | static int subtree_equal(void *context, void *value1_le, void *value2_le) |
| 331 | { |
| 332 | __le64 v1_le, v2_le; |
| 333 | memcpy(&v1_le, value1_le, sizeof(v1_le)); |
| 334 | memcpy(&v2_le, value2_le, sizeof(v2_le)); |
| 335 | |
| 336 | return v1_le == v2_le; |
| 337 | } |
| 338 | |
| 339 | /*----------------------------------------------------------------*/ |
| 340 | |
| 341 | static int superblock_all_zeroes(struct dm_block_manager *bm, int *result) |
| 342 | { |
| 343 | int r; |
| 344 | unsigned i; |
| 345 | struct dm_block *b; |
| 346 | __le64 *data_le, zero = cpu_to_le64(0); |
| 347 | unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64); |
| 348 | |
| 349 | /* |
| 350 | * We can't use a validator here - it may be all zeroes. |
| 351 | */ |
| 352 | r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b); |
| 353 | if (r) |
| 354 | return r; |
| 355 | |
| 356 | data_le = dm_block_data(b); |
| 357 | *result = 1; |
| 358 | for (i = 0; i < block_size; i++) { |
| 359 | if (data_le[i] != zero) { |
| 360 | *result = 0; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return dm_bm_unlock(b); |
| 366 | } |
| 367 | |
| 368 | static int init_pmd(struct dm_pool_metadata *pmd, |
| 369 | struct dm_block_manager *bm, |
| 370 | dm_block_t nr_blocks, int create) |
| 371 | { |
| 372 | int r; |
| 373 | struct dm_space_map *sm, *data_sm; |
| 374 | struct dm_transaction_manager *tm; |
| 375 | struct dm_block *sblock; |
| 376 | |
| 377 | if (create) { |
| 378 | r = dm_tm_create_with_sm(bm, THIN_SUPERBLOCK_LOCATION, |
| 379 | &sb_validator, &tm, &sm, &sblock); |
| 380 | if (r < 0) { |
| 381 | DMERR("tm_create_with_sm failed"); |
| 382 | return r; |
| 383 | } |
| 384 | |
| 385 | data_sm = dm_sm_disk_create(tm, nr_blocks); |
| 386 | if (IS_ERR(data_sm)) { |
| 387 | DMERR("sm_disk_create failed"); |
Joe Thornber | 4469a5f | 2012-03-07 19:09:43 +0000 | [diff] [blame] | 388 | dm_tm_unlock(tm, sblock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 389 | r = PTR_ERR(data_sm); |
| 390 | goto bad; |
| 391 | } |
| 392 | } else { |
| 393 | struct thin_disk_superblock *disk_super = NULL; |
| 394 | size_t space_map_root_offset = |
| 395 | offsetof(struct thin_disk_superblock, metadata_space_map_root); |
| 396 | |
| 397 | r = dm_tm_open_with_sm(bm, THIN_SUPERBLOCK_LOCATION, |
| 398 | &sb_validator, space_map_root_offset, |
| 399 | SPACE_MAP_ROOT_SIZE, &tm, &sm, &sblock); |
| 400 | if (r < 0) { |
| 401 | DMERR("tm_open_with_sm failed"); |
| 402 | return r; |
| 403 | } |
| 404 | |
| 405 | disk_super = dm_block_data(sblock); |
| 406 | data_sm = dm_sm_disk_open(tm, disk_super->data_space_map_root, |
| 407 | sizeof(disk_super->data_space_map_root)); |
| 408 | if (IS_ERR(data_sm)) { |
| 409 | DMERR("sm_disk_open failed"); |
| 410 | r = PTR_ERR(data_sm); |
| 411 | goto bad; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | |
| 416 | r = dm_tm_unlock(tm, sblock); |
| 417 | if (r < 0) { |
| 418 | DMERR("couldn't unlock superblock"); |
| 419 | goto bad_data_sm; |
| 420 | } |
| 421 | |
| 422 | pmd->bm = bm; |
| 423 | pmd->metadata_sm = sm; |
| 424 | pmd->data_sm = data_sm; |
| 425 | pmd->tm = tm; |
| 426 | pmd->nb_tm = dm_tm_create_non_blocking_clone(tm); |
| 427 | if (!pmd->nb_tm) { |
| 428 | DMERR("could not create clone tm"); |
| 429 | r = -ENOMEM; |
| 430 | goto bad_data_sm; |
| 431 | } |
| 432 | |
| 433 | pmd->info.tm = tm; |
| 434 | pmd->info.levels = 2; |
| 435 | pmd->info.value_type.context = pmd->data_sm; |
| 436 | pmd->info.value_type.size = sizeof(__le64); |
| 437 | pmd->info.value_type.inc = data_block_inc; |
| 438 | pmd->info.value_type.dec = data_block_dec; |
| 439 | pmd->info.value_type.equal = data_block_equal; |
| 440 | |
| 441 | memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info)); |
| 442 | pmd->nb_info.tm = pmd->nb_tm; |
| 443 | |
| 444 | pmd->tl_info.tm = tm; |
| 445 | pmd->tl_info.levels = 1; |
| 446 | pmd->tl_info.value_type.context = &pmd->info; |
| 447 | pmd->tl_info.value_type.size = sizeof(__le64); |
| 448 | pmd->tl_info.value_type.inc = subtree_inc; |
| 449 | pmd->tl_info.value_type.dec = subtree_dec; |
| 450 | pmd->tl_info.value_type.equal = subtree_equal; |
| 451 | |
| 452 | pmd->bl_info.tm = tm; |
| 453 | pmd->bl_info.levels = 1; |
| 454 | pmd->bl_info.value_type.context = pmd->data_sm; |
| 455 | pmd->bl_info.value_type.size = sizeof(__le64); |
| 456 | pmd->bl_info.value_type.inc = data_block_inc; |
| 457 | pmd->bl_info.value_type.dec = data_block_dec; |
| 458 | pmd->bl_info.value_type.equal = data_block_equal; |
| 459 | |
| 460 | pmd->details_info.tm = tm; |
| 461 | pmd->details_info.levels = 1; |
| 462 | pmd->details_info.value_type.context = NULL; |
| 463 | pmd->details_info.value_type.size = sizeof(struct disk_device_details); |
| 464 | pmd->details_info.value_type.inc = NULL; |
| 465 | pmd->details_info.value_type.dec = NULL; |
| 466 | pmd->details_info.value_type.equal = NULL; |
| 467 | |
| 468 | pmd->root = 0; |
| 469 | |
| 470 | init_rwsem(&pmd->root_lock); |
| 471 | pmd->time = 0; |
| 472 | pmd->need_commit = 0; |
| 473 | pmd->details_root = 0; |
| 474 | pmd->trans_id = 0; |
| 475 | pmd->flags = 0; |
| 476 | INIT_LIST_HEAD(&pmd->thin_devices); |
| 477 | |
| 478 | return 0; |
| 479 | |
| 480 | bad_data_sm: |
| 481 | dm_sm_destroy(data_sm); |
| 482 | bad: |
| 483 | dm_tm_destroy(tm); |
| 484 | dm_sm_destroy(sm); |
| 485 | |
| 486 | return r; |
| 487 | } |
| 488 | |
| 489 | static int __begin_transaction(struct dm_pool_metadata *pmd) |
| 490 | { |
| 491 | int r; |
| 492 | u32 features; |
| 493 | struct thin_disk_superblock *disk_super; |
| 494 | struct dm_block *sblock; |
| 495 | |
| 496 | /* |
| 497 | * __maybe_commit_transaction() resets these |
| 498 | */ |
| 499 | WARN_ON(pmd->need_commit); |
| 500 | |
| 501 | /* |
| 502 | * We re-read the superblock every time. Shouldn't need to do this |
| 503 | * really. |
| 504 | */ |
| 505 | r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 506 | &sb_validator, &sblock); |
| 507 | if (r) |
| 508 | return r; |
| 509 | |
| 510 | disk_super = dm_block_data(sblock); |
| 511 | pmd->time = le32_to_cpu(disk_super->time); |
| 512 | pmd->root = le64_to_cpu(disk_super->data_mapping_root); |
| 513 | pmd->details_root = le64_to_cpu(disk_super->device_details_root); |
| 514 | pmd->trans_id = le64_to_cpu(disk_super->trans_id); |
| 515 | pmd->flags = le32_to_cpu(disk_super->flags); |
| 516 | pmd->data_block_size = le32_to_cpu(disk_super->data_block_size); |
| 517 | |
| 518 | features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP; |
| 519 | if (features) { |
| 520 | DMERR("could not access metadata due to " |
| 521 | "unsupported optional features (%lx).", |
| 522 | (unsigned long)features); |
| 523 | r = -EINVAL; |
| 524 | goto out; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Check for read-only metadata to skip the following RDWR checks. |
| 529 | */ |
| 530 | if (get_disk_ro(pmd->bdev->bd_disk)) |
| 531 | goto out; |
| 532 | |
| 533 | features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP; |
| 534 | if (features) { |
| 535 | DMERR("could not access metadata RDWR due to " |
| 536 | "unsupported optional features (%lx).", |
| 537 | (unsigned long)features); |
| 538 | r = -EINVAL; |
| 539 | } |
| 540 | |
| 541 | out: |
| 542 | dm_bm_unlock(sblock); |
| 543 | return r; |
| 544 | } |
| 545 | |
| 546 | static int __write_changed_details(struct dm_pool_metadata *pmd) |
| 547 | { |
| 548 | int r; |
| 549 | struct dm_thin_device *td, *tmp; |
| 550 | struct disk_device_details details; |
| 551 | uint64_t key; |
| 552 | |
| 553 | list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) { |
| 554 | if (!td->changed) |
| 555 | continue; |
| 556 | |
| 557 | key = td->id; |
| 558 | |
| 559 | details.mapped_blocks = cpu_to_le64(td->mapped_blocks); |
| 560 | details.transaction_id = cpu_to_le64(td->transaction_id); |
| 561 | details.creation_time = cpu_to_le32(td->creation_time); |
| 562 | details.snapshotted_time = cpu_to_le32(td->snapshotted_time); |
| 563 | __dm_bless_for_disk(&details); |
| 564 | |
| 565 | r = dm_btree_insert(&pmd->details_info, pmd->details_root, |
| 566 | &key, &details, &pmd->details_root); |
| 567 | if (r) |
| 568 | return r; |
| 569 | |
| 570 | if (td->open_count) |
| 571 | td->changed = 0; |
| 572 | else { |
| 573 | list_del(&td->list); |
| 574 | kfree(td); |
| 575 | } |
| 576 | |
| 577 | pmd->need_commit = 1; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int __commit_transaction(struct dm_pool_metadata *pmd) |
| 584 | { |
| 585 | /* |
| 586 | * FIXME: Associated pool should be made read-only on failure. |
| 587 | */ |
| 588 | int r; |
| 589 | size_t metadata_len, data_len; |
| 590 | struct thin_disk_superblock *disk_super; |
| 591 | struct dm_block *sblock; |
| 592 | |
| 593 | /* |
| 594 | * We need to know if the thin_disk_superblock exceeds a 512-byte sector. |
| 595 | */ |
| 596 | BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512); |
| 597 | |
| 598 | r = __write_changed_details(pmd); |
| 599 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 600 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 601 | |
| 602 | if (!pmd->need_commit) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 603 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 604 | |
| 605 | r = dm_sm_commit(pmd->data_sm); |
| 606 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 607 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 608 | |
| 609 | r = dm_tm_pre_commit(pmd->tm); |
| 610 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 611 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 612 | |
| 613 | r = dm_sm_root_size(pmd->metadata_sm, &metadata_len); |
| 614 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 615 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 616 | |
Joe Thornber | fef838c | 2012-03-28 18:41:25 +0100 | [diff] [blame] | 617 | r = dm_sm_root_size(pmd->data_sm, &data_len); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 618 | if (r < 0) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 619 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 620 | |
| 621 | r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 622 | &sb_validator, &sblock); |
| 623 | if (r) |
Joe Thornber | d973ac1 | 2012-07-27 15:07:58 +0100 | [diff] [blame^] | 624 | return r; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 625 | |
| 626 | disk_super = dm_block_data(sblock); |
| 627 | disk_super->time = cpu_to_le32(pmd->time); |
| 628 | disk_super->data_mapping_root = cpu_to_le64(pmd->root); |
| 629 | disk_super->device_details_root = cpu_to_le64(pmd->details_root); |
| 630 | disk_super->trans_id = cpu_to_le64(pmd->trans_id); |
| 631 | disk_super->flags = cpu_to_le32(pmd->flags); |
| 632 | |
| 633 | r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root, |
| 634 | metadata_len); |
| 635 | if (r < 0) |
| 636 | goto out_locked; |
| 637 | |
| 638 | r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root, |
| 639 | data_len); |
| 640 | if (r < 0) |
| 641 | goto out_locked; |
| 642 | |
| 643 | r = dm_tm_commit(pmd->tm, sblock); |
| 644 | if (!r) |
| 645 | pmd->need_commit = 0; |
| 646 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 647 | return r; |
| 648 | |
| 649 | out_locked: |
| 650 | dm_bm_unlock(sblock); |
| 651 | return r; |
| 652 | } |
| 653 | |
| 654 | struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev, |
| 655 | sector_t data_block_size) |
| 656 | { |
| 657 | int r; |
| 658 | struct thin_disk_superblock *disk_super; |
| 659 | struct dm_pool_metadata *pmd; |
| 660 | sector_t bdev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT; |
| 661 | struct dm_block_manager *bm; |
| 662 | int create; |
| 663 | struct dm_block *sblock; |
| 664 | |
| 665 | pmd = kmalloc(sizeof(*pmd), GFP_KERNEL); |
| 666 | if (!pmd) { |
| 667 | DMERR("could not allocate metadata struct"); |
| 668 | return ERR_PTR(-ENOMEM); |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Max hex locks: |
| 673 | * 3 for btree insert + |
| 674 | * 2 for btree lookup used within space map |
| 675 | */ |
| 676 | bm = dm_block_manager_create(bdev, THIN_METADATA_BLOCK_SIZE, |
| 677 | THIN_METADATA_CACHE_SIZE, 5); |
| 678 | if (!bm) { |
| 679 | DMERR("could not create block manager"); |
| 680 | kfree(pmd); |
| 681 | return ERR_PTR(-ENOMEM); |
| 682 | } |
| 683 | |
| 684 | r = superblock_all_zeroes(bm, &create); |
| 685 | if (r) { |
| 686 | dm_block_manager_destroy(bm); |
| 687 | kfree(pmd); |
| 688 | return ERR_PTR(r); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | r = init_pmd(pmd, bm, 0, create); |
| 693 | if (r) { |
| 694 | dm_block_manager_destroy(bm); |
| 695 | kfree(pmd); |
| 696 | return ERR_PTR(r); |
| 697 | } |
| 698 | pmd->bdev = bdev; |
| 699 | |
| 700 | if (!create) { |
| 701 | r = __begin_transaction(pmd); |
| 702 | if (r < 0) |
| 703 | goto bad; |
| 704 | return pmd; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Create. |
| 709 | */ |
| 710 | r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 711 | &sb_validator, &sblock); |
| 712 | if (r) |
| 713 | goto bad; |
| 714 | |
Mike Snitzer | c4a69ec | 2012-03-28 18:41:28 +0100 | [diff] [blame] | 715 | if (bdev_size > THIN_METADATA_MAX_SECTORS) |
| 716 | bdev_size = THIN_METADATA_MAX_SECTORS; |
| 717 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 718 | disk_super = dm_block_data(sblock); |
| 719 | disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC); |
| 720 | disk_super->version = cpu_to_le32(THIN_VERSION); |
| 721 | disk_super->time = 0; |
| 722 | disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT); |
| 723 | disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT); |
| 724 | disk_super->data_block_size = cpu_to_le32(data_block_size); |
| 725 | |
| 726 | r = dm_bm_unlock(sblock); |
| 727 | if (r < 0) |
| 728 | goto bad; |
| 729 | |
| 730 | r = dm_btree_empty(&pmd->info, &pmd->root); |
| 731 | if (r < 0) |
| 732 | goto bad; |
| 733 | |
| 734 | r = dm_btree_empty(&pmd->details_info, &pmd->details_root); |
| 735 | if (r < 0) { |
| 736 | DMERR("couldn't create devices root"); |
| 737 | goto bad; |
| 738 | } |
| 739 | |
| 740 | pmd->flags = 0; |
| 741 | pmd->need_commit = 1; |
| 742 | r = dm_pool_commit_metadata(pmd); |
| 743 | if (r < 0) { |
| 744 | DMERR("%s: dm_pool_commit_metadata() failed, error = %d", |
| 745 | __func__, r); |
| 746 | goto bad; |
| 747 | } |
| 748 | |
| 749 | return pmd; |
| 750 | |
| 751 | bad: |
| 752 | if (dm_pool_metadata_close(pmd) < 0) |
| 753 | DMWARN("%s: dm_pool_metadata_close() failed.", __func__); |
| 754 | return ERR_PTR(r); |
| 755 | } |
| 756 | |
| 757 | int dm_pool_metadata_close(struct dm_pool_metadata *pmd) |
| 758 | { |
| 759 | int r; |
| 760 | unsigned open_devices = 0; |
| 761 | struct dm_thin_device *td, *tmp; |
| 762 | |
| 763 | down_read(&pmd->root_lock); |
| 764 | list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) { |
| 765 | if (td->open_count) |
| 766 | open_devices++; |
| 767 | else { |
| 768 | list_del(&td->list); |
| 769 | kfree(td); |
| 770 | } |
| 771 | } |
| 772 | up_read(&pmd->root_lock); |
| 773 | |
| 774 | if (open_devices) { |
| 775 | DMERR("attempt to close pmd when %u device(s) are still open", |
| 776 | open_devices); |
| 777 | return -EBUSY; |
| 778 | } |
| 779 | |
| 780 | r = __commit_transaction(pmd); |
| 781 | if (r < 0) |
| 782 | DMWARN("%s: __commit_transaction() failed, error = %d", |
| 783 | __func__, r); |
| 784 | |
| 785 | dm_tm_destroy(pmd->tm); |
| 786 | dm_tm_destroy(pmd->nb_tm); |
| 787 | dm_block_manager_destroy(pmd->bm); |
| 788 | dm_sm_destroy(pmd->metadata_sm); |
| 789 | dm_sm_destroy(pmd->data_sm); |
| 790 | kfree(pmd); |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 795 | /* |
| 796 | * __open_device: Returns @td corresponding to device with id @dev, |
| 797 | * creating it if @create is set and incrementing @td->open_count. |
| 798 | * On failure, @td is undefined. |
| 799 | */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 800 | static int __open_device(struct dm_pool_metadata *pmd, |
| 801 | dm_thin_id dev, int create, |
| 802 | struct dm_thin_device **td) |
| 803 | { |
| 804 | int r, changed = 0; |
| 805 | struct dm_thin_device *td2; |
| 806 | uint64_t key = dev; |
| 807 | struct disk_device_details details_le; |
| 808 | |
| 809 | /* |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 810 | * If the device is already open, return it. |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 811 | */ |
| 812 | list_for_each_entry(td2, &pmd->thin_devices, list) |
| 813 | if (td2->id == dev) { |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 814 | /* |
| 815 | * May not create an already-open device. |
| 816 | */ |
| 817 | if (create) |
| 818 | return -EEXIST; |
| 819 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 820 | td2->open_count++; |
| 821 | *td = td2; |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * Check the device exists. |
| 827 | */ |
| 828 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 829 | &key, &details_le); |
| 830 | if (r) { |
| 831 | if (r != -ENODATA || !create) |
| 832 | return r; |
| 833 | |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 834 | /* |
| 835 | * Create new device. |
| 836 | */ |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 837 | changed = 1; |
| 838 | details_le.mapped_blocks = 0; |
| 839 | details_le.transaction_id = cpu_to_le64(pmd->trans_id); |
| 840 | details_le.creation_time = cpu_to_le32(pmd->time); |
| 841 | details_le.snapshotted_time = cpu_to_le32(pmd->time); |
| 842 | } |
| 843 | |
| 844 | *td = kmalloc(sizeof(**td), GFP_NOIO); |
| 845 | if (!*td) |
| 846 | return -ENOMEM; |
| 847 | |
| 848 | (*td)->pmd = pmd; |
| 849 | (*td)->id = dev; |
| 850 | (*td)->open_count = 1; |
| 851 | (*td)->changed = changed; |
| 852 | (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks); |
| 853 | (*td)->transaction_id = le64_to_cpu(details_le.transaction_id); |
| 854 | (*td)->creation_time = le32_to_cpu(details_le.creation_time); |
| 855 | (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time); |
| 856 | |
| 857 | list_add(&(*td)->list, &pmd->thin_devices); |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static void __close_device(struct dm_thin_device *td) |
| 863 | { |
| 864 | --td->open_count; |
| 865 | } |
| 866 | |
| 867 | static int __create_thin(struct dm_pool_metadata *pmd, |
| 868 | dm_thin_id dev) |
| 869 | { |
| 870 | int r; |
| 871 | dm_block_t dev_root; |
| 872 | uint64_t key = dev; |
| 873 | struct disk_device_details details_le; |
| 874 | struct dm_thin_device *td; |
| 875 | __le64 value; |
| 876 | |
| 877 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 878 | &key, &details_le); |
| 879 | if (!r) |
| 880 | return -EEXIST; |
| 881 | |
| 882 | /* |
| 883 | * Create an empty btree for the mappings. |
| 884 | */ |
| 885 | r = dm_btree_empty(&pmd->bl_info, &dev_root); |
| 886 | if (r) |
| 887 | return r; |
| 888 | |
| 889 | /* |
| 890 | * Insert it into the main mapping tree. |
| 891 | */ |
| 892 | value = cpu_to_le64(dev_root); |
| 893 | __dm_bless_for_disk(&value); |
| 894 | r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root); |
| 895 | if (r) { |
| 896 | dm_btree_del(&pmd->bl_info, dev_root); |
| 897 | return r; |
| 898 | } |
| 899 | |
| 900 | r = __open_device(pmd, dev, 1, &td); |
| 901 | if (r) { |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 902 | dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 903 | dm_btree_del(&pmd->bl_info, dev_root); |
| 904 | return r; |
| 905 | } |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 906 | __close_device(td); |
| 907 | |
| 908 | return r; |
| 909 | } |
| 910 | |
| 911 | int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev) |
| 912 | { |
| 913 | int r; |
| 914 | |
| 915 | down_write(&pmd->root_lock); |
| 916 | r = __create_thin(pmd, dev); |
| 917 | up_write(&pmd->root_lock); |
| 918 | |
| 919 | return r; |
| 920 | } |
| 921 | |
| 922 | static int __set_snapshot_details(struct dm_pool_metadata *pmd, |
| 923 | struct dm_thin_device *snap, |
| 924 | dm_thin_id origin, uint32_t time) |
| 925 | { |
| 926 | int r; |
| 927 | struct dm_thin_device *td; |
| 928 | |
| 929 | r = __open_device(pmd, origin, 0, &td); |
| 930 | if (r) |
| 931 | return r; |
| 932 | |
| 933 | td->changed = 1; |
| 934 | td->snapshotted_time = time; |
| 935 | |
| 936 | snap->mapped_blocks = td->mapped_blocks; |
| 937 | snap->snapshotted_time = time; |
| 938 | __close_device(td); |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static int __create_snap(struct dm_pool_metadata *pmd, |
| 944 | dm_thin_id dev, dm_thin_id origin) |
| 945 | { |
| 946 | int r; |
| 947 | dm_block_t origin_root; |
| 948 | uint64_t key = origin, dev_key = dev; |
| 949 | struct dm_thin_device *td; |
| 950 | struct disk_device_details details_le; |
| 951 | __le64 value; |
| 952 | |
| 953 | /* check this device is unused */ |
| 954 | r = dm_btree_lookup(&pmd->details_info, pmd->details_root, |
| 955 | &dev_key, &details_le); |
| 956 | if (!r) |
| 957 | return -EEXIST; |
| 958 | |
| 959 | /* find the mapping tree for the origin */ |
| 960 | r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value); |
| 961 | if (r) |
| 962 | return r; |
| 963 | origin_root = le64_to_cpu(value); |
| 964 | |
| 965 | /* clone the origin, an inc will do */ |
| 966 | dm_tm_inc(pmd->tm, origin_root); |
| 967 | |
| 968 | /* insert into the main mapping tree */ |
| 969 | value = cpu_to_le64(origin_root); |
| 970 | __dm_bless_for_disk(&value); |
| 971 | key = dev; |
| 972 | r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root); |
| 973 | if (r) { |
| 974 | dm_tm_dec(pmd->tm, origin_root); |
| 975 | return r; |
| 976 | } |
| 977 | |
| 978 | pmd->time++; |
| 979 | |
| 980 | r = __open_device(pmd, dev, 1, &td); |
| 981 | if (r) |
| 982 | goto bad; |
| 983 | |
| 984 | r = __set_snapshot_details(pmd, td, origin, pmd->time); |
Mike Snitzer | 1f3db25 | 2012-03-07 19:09:41 +0000 | [diff] [blame] | 985 | __close_device(td); |
| 986 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 987 | if (r) |
| 988 | goto bad; |
| 989 | |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 990 | return 0; |
| 991 | |
| 992 | bad: |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 993 | dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 994 | dm_btree_remove(&pmd->details_info, pmd->details_root, |
| 995 | &key, &pmd->details_root); |
| 996 | return r; |
| 997 | } |
| 998 | |
| 999 | int dm_pool_create_snap(struct dm_pool_metadata *pmd, |
| 1000 | dm_thin_id dev, |
| 1001 | dm_thin_id origin) |
| 1002 | { |
| 1003 | int r; |
| 1004 | |
| 1005 | down_write(&pmd->root_lock); |
| 1006 | r = __create_snap(pmd, dev, origin); |
| 1007 | up_write(&pmd->root_lock); |
| 1008 | |
| 1009 | return r; |
| 1010 | } |
| 1011 | |
| 1012 | static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev) |
| 1013 | { |
| 1014 | int r; |
| 1015 | uint64_t key = dev; |
| 1016 | struct dm_thin_device *td; |
| 1017 | |
| 1018 | /* TODO: failure should mark the transaction invalid */ |
| 1019 | r = __open_device(pmd, dev, 0, &td); |
| 1020 | if (r) |
| 1021 | return r; |
| 1022 | |
| 1023 | if (td->open_count > 1) { |
| 1024 | __close_device(td); |
| 1025 | return -EBUSY; |
| 1026 | } |
| 1027 | |
| 1028 | list_del(&td->list); |
| 1029 | kfree(td); |
| 1030 | r = dm_btree_remove(&pmd->details_info, pmd->details_root, |
| 1031 | &key, &pmd->details_root); |
| 1032 | if (r) |
| 1033 | return r; |
| 1034 | |
| 1035 | r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); |
| 1036 | if (r) |
| 1037 | return r; |
| 1038 | |
| 1039 | pmd->need_commit = 1; |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd, |
| 1045 | dm_thin_id dev) |
| 1046 | { |
| 1047 | int r; |
| 1048 | |
| 1049 | down_write(&pmd->root_lock); |
| 1050 | r = __delete_device(pmd, dev); |
| 1051 | up_write(&pmd->root_lock); |
| 1052 | |
| 1053 | return r; |
| 1054 | } |
| 1055 | |
| 1056 | int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd, |
| 1057 | uint64_t current_id, |
| 1058 | uint64_t new_id) |
| 1059 | { |
| 1060 | down_write(&pmd->root_lock); |
| 1061 | if (pmd->trans_id != current_id) { |
| 1062 | up_write(&pmd->root_lock); |
| 1063 | DMERR("mismatched transaction id"); |
| 1064 | return -EINVAL; |
| 1065 | } |
| 1066 | |
| 1067 | pmd->trans_id = new_id; |
| 1068 | pmd->need_commit = 1; |
| 1069 | up_write(&pmd->root_lock); |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd, |
| 1075 | uint64_t *result) |
| 1076 | { |
| 1077 | down_read(&pmd->root_lock); |
| 1078 | *result = pmd->trans_id; |
| 1079 | up_read(&pmd->root_lock); |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1084 | static int __reserve_metadata_snap(struct dm_pool_metadata *pmd) |
| 1085 | { |
| 1086 | int r, inc; |
| 1087 | struct thin_disk_superblock *disk_super; |
| 1088 | struct dm_block *copy, *sblock; |
| 1089 | dm_block_t held_root; |
| 1090 | |
| 1091 | /* |
| 1092 | * Copy the superblock. |
| 1093 | */ |
| 1094 | dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION); |
| 1095 | r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION, |
| 1096 | &sb_validator, ©, &inc); |
| 1097 | if (r) |
| 1098 | return r; |
| 1099 | |
| 1100 | BUG_ON(!inc); |
| 1101 | |
| 1102 | held_root = dm_block_location(copy); |
| 1103 | disk_super = dm_block_data(copy); |
| 1104 | |
| 1105 | if (le64_to_cpu(disk_super->held_root)) { |
| 1106 | DMWARN("Pool metadata snapshot already exists: release this before taking another."); |
| 1107 | |
| 1108 | dm_tm_dec(pmd->tm, held_root); |
| 1109 | dm_tm_unlock(pmd->tm, copy); |
| 1110 | pmd->need_commit = 1; |
| 1111 | |
| 1112 | return -EBUSY; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Wipe the spacemap since we're not publishing this. |
| 1117 | */ |
| 1118 | memset(&disk_super->data_space_map_root, 0, |
| 1119 | sizeof(disk_super->data_space_map_root)); |
| 1120 | memset(&disk_super->metadata_space_map_root, 0, |
| 1121 | sizeof(disk_super->metadata_space_map_root)); |
| 1122 | |
| 1123 | /* |
| 1124 | * Increment the data structures that need to be preserved. |
| 1125 | */ |
| 1126 | dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root)); |
| 1127 | dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root)); |
| 1128 | dm_tm_unlock(pmd->tm, copy); |
| 1129 | |
| 1130 | /* |
| 1131 | * Write the held root into the superblock. |
| 1132 | */ |
| 1133 | r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 1134 | &sb_validator, &sblock); |
| 1135 | if (r) { |
| 1136 | dm_tm_dec(pmd->tm, held_root); |
| 1137 | pmd->need_commit = 1; |
| 1138 | return r; |
| 1139 | } |
| 1140 | |
| 1141 | disk_super = dm_block_data(sblock); |
| 1142 | disk_super->held_root = cpu_to_le64(held_root); |
| 1143 | dm_bm_unlock(sblock); |
| 1144 | |
| 1145 | pmd->need_commit = 1; |
| 1146 | |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd) |
| 1151 | { |
| 1152 | int r; |
| 1153 | |
| 1154 | down_write(&pmd->root_lock); |
| 1155 | r = __reserve_metadata_snap(pmd); |
| 1156 | up_write(&pmd->root_lock); |
| 1157 | |
| 1158 | return r; |
| 1159 | } |
| 1160 | |
| 1161 | static int __release_metadata_snap(struct dm_pool_metadata *pmd) |
| 1162 | { |
| 1163 | int r; |
| 1164 | struct thin_disk_superblock *disk_super; |
| 1165 | struct dm_block *sblock, *copy; |
| 1166 | dm_block_t held_root; |
| 1167 | |
| 1168 | r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 1169 | &sb_validator, &sblock); |
| 1170 | if (r) |
| 1171 | return r; |
| 1172 | |
| 1173 | disk_super = dm_block_data(sblock); |
| 1174 | held_root = le64_to_cpu(disk_super->held_root); |
| 1175 | disk_super->held_root = cpu_to_le64(0); |
| 1176 | pmd->need_commit = 1; |
| 1177 | |
| 1178 | dm_bm_unlock(sblock); |
| 1179 | |
| 1180 | if (!held_root) { |
| 1181 | DMWARN("No pool metadata snapshot found: nothing to release."); |
| 1182 | return -EINVAL; |
| 1183 | } |
| 1184 | |
| 1185 | r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, ©); |
| 1186 | if (r) |
| 1187 | return r; |
| 1188 | |
| 1189 | disk_super = dm_block_data(copy); |
| 1190 | dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root)); |
| 1191 | dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root)); |
| 1192 | dm_sm_dec_block(pmd->metadata_sm, held_root); |
| 1193 | |
| 1194 | return dm_tm_unlock(pmd->tm, copy); |
| 1195 | } |
| 1196 | |
| 1197 | int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd) |
| 1198 | { |
| 1199 | int r; |
| 1200 | |
| 1201 | down_write(&pmd->root_lock); |
| 1202 | r = __release_metadata_snap(pmd); |
| 1203 | up_write(&pmd->root_lock); |
| 1204 | |
| 1205 | return r; |
| 1206 | } |
| 1207 | |
| 1208 | static int __get_metadata_snap(struct dm_pool_metadata *pmd, |
| 1209 | dm_block_t *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1210 | { |
| 1211 | int r; |
| 1212 | struct thin_disk_superblock *disk_super; |
| 1213 | struct dm_block *sblock; |
| 1214 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1215 | r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION, |
| 1216 | &sb_validator, &sblock); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1217 | if (r) |
| 1218 | return r; |
| 1219 | |
| 1220 | disk_super = dm_block_data(sblock); |
| 1221 | *result = le64_to_cpu(disk_super->held_root); |
| 1222 | |
| 1223 | return dm_bm_unlock(sblock); |
| 1224 | } |
| 1225 | |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1226 | int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd, |
| 1227 | dm_block_t *result) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1228 | { |
| 1229 | int r; |
| 1230 | |
| 1231 | down_read(&pmd->root_lock); |
Joe Thornber | cc8394d | 2012-06-03 00:30:01 +0100 | [diff] [blame] | 1232 | r = __get_metadata_snap(pmd, result); |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1233 | up_read(&pmd->root_lock); |
| 1234 | |
| 1235 | return r; |
| 1236 | } |
| 1237 | |
| 1238 | int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev, |
| 1239 | struct dm_thin_device **td) |
| 1240 | { |
| 1241 | int r; |
| 1242 | |
| 1243 | down_write(&pmd->root_lock); |
| 1244 | r = __open_device(pmd, dev, 0, td); |
| 1245 | up_write(&pmd->root_lock); |
| 1246 | |
| 1247 | return r; |
| 1248 | } |
| 1249 | |
| 1250 | int dm_pool_close_thin_device(struct dm_thin_device *td) |
| 1251 | { |
| 1252 | down_write(&td->pmd->root_lock); |
| 1253 | __close_device(td); |
| 1254 | up_write(&td->pmd->root_lock); |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | dm_thin_id dm_thin_dev_id(struct dm_thin_device *td) |
| 1260 | { |
| 1261 | return td->id; |
| 1262 | } |
| 1263 | |
Mike Snitzer | 17b7d63 | 2012-07-27 15:07:57 +0100 | [diff] [blame] | 1264 | static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time) |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1265 | { |
| 1266 | return td->snapshotted_time > time; |
| 1267 | } |
| 1268 | |
| 1269 | int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block, |
| 1270 | int can_block, struct dm_thin_lookup_result *result) |
| 1271 | { |
| 1272 | int r; |
| 1273 | uint64_t block_time = 0; |
| 1274 | __le64 value; |
| 1275 | struct dm_pool_metadata *pmd = td->pmd; |
| 1276 | dm_block_t keys[2] = { td->id, block }; |
| 1277 | |
| 1278 | if (can_block) { |
| 1279 | down_read(&pmd->root_lock); |
| 1280 | r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value); |
| 1281 | if (!r) |
| 1282 | block_time = le64_to_cpu(value); |
| 1283 | up_read(&pmd->root_lock); |
| 1284 | |
| 1285 | } else if (down_read_trylock(&pmd->root_lock)) { |
| 1286 | r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value); |
| 1287 | if (!r) |
| 1288 | block_time = le64_to_cpu(value); |
| 1289 | up_read(&pmd->root_lock); |
| 1290 | |
| 1291 | } else |
| 1292 | return -EWOULDBLOCK; |
| 1293 | |
| 1294 | if (!r) { |
| 1295 | dm_block_t exception_block; |
| 1296 | uint32_t exception_time; |
| 1297 | unpack_block_time(block_time, &exception_block, |
| 1298 | &exception_time); |
| 1299 | result->block = exception_block; |
| 1300 | result->shared = __snapshotted_since(td, exception_time); |
| 1301 | } |
| 1302 | |
| 1303 | return r; |
| 1304 | } |
| 1305 | |
| 1306 | static int __insert(struct dm_thin_device *td, dm_block_t block, |
| 1307 | dm_block_t data_block) |
| 1308 | { |
| 1309 | int r, inserted; |
| 1310 | __le64 value; |
| 1311 | struct dm_pool_metadata *pmd = td->pmd; |
| 1312 | dm_block_t keys[2] = { td->id, block }; |
| 1313 | |
| 1314 | pmd->need_commit = 1; |
| 1315 | value = cpu_to_le64(pack_block_time(data_block, pmd->time)); |
| 1316 | __dm_bless_for_disk(&value); |
| 1317 | |
| 1318 | r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value, |
| 1319 | &pmd->root, &inserted); |
| 1320 | if (r) |
| 1321 | return r; |
| 1322 | |
| 1323 | if (inserted) { |
| 1324 | td->mapped_blocks++; |
| 1325 | td->changed = 1; |
| 1326 | } |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block, |
| 1332 | dm_block_t data_block) |
| 1333 | { |
| 1334 | int r; |
| 1335 | |
| 1336 | down_write(&td->pmd->root_lock); |
| 1337 | r = __insert(td, block, data_block); |
| 1338 | up_write(&td->pmd->root_lock); |
| 1339 | |
| 1340 | return r; |
| 1341 | } |
| 1342 | |
| 1343 | static int __remove(struct dm_thin_device *td, dm_block_t block) |
| 1344 | { |
| 1345 | int r; |
| 1346 | struct dm_pool_metadata *pmd = td->pmd; |
| 1347 | dm_block_t keys[2] = { td->id, block }; |
| 1348 | |
| 1349 | r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root); |
| 1350 | if (r) |
| 1351 | return r; |
| 1352 | |
Joe Thornber | af63bcb | 2012-03-07 19:09:44 +0000 | [diff] [blame] | 1353 | td->mapped_blocks--; |
| 1354 | td->changed = 1; |
Joe Thornber | 991d9fa | 2011-10-31 20:21:18 +0000 | [diff] [blame] | 1355 | pmd->need_commit = 1; |
| 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block) |
| 1361 | { |
| 1362 | int r; |
| 1363 | |
| 1364 | down_write(&td->pmd->root_lock); |
| 1365 | r = __remove(td, block); |
| 1366 | up_write(&td->pmd->root_lock); |
| 1367 | |
| 1368 | return r; |
| 1369 | } |
| 1370 | |
| 1371 | int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1372 | { |
| 1373 | int r; |
| 1374 | |
| 1375 | down_write(&pmd->root_lock); |
| 1376 | |
| 1377 | r = dm_sm_new_block(pmd->data_sm, result); |
| 1378 | pmd->need_commit = 1; |
| 1379 | |
| 1380 | up_write(&pmd->root_lock); |
| 1381 | |
| 1382 | return r; |
| 1383 | } |
| 1384 | |
| 1385 | int dm_pool_commit_metadata(struct dm_pool_metadata *pmd) |
| 1386 | { |
| 1387 | int r; |
| 1388 | |
| 1389 | down_write(&pmd->root_lock); |
| 1390 | |
| 1391 | r = __commit_transaction(pmd); |
| 1392 | if (r <= 0) |
| 1393 | goto out; |
| 1394 | |
| 1395 | /* |
| 1396 | * Open the next transaction. |
| 1397 | */ |
| 1398 | r = __begin_transaction(pmd); |
| 1399 | out: |
| 1400 | up_write(&pmd->root_lock); |
| 1401 | return r; |
| 1402 | } |
| 1403 | |
| 1404 | int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1405 | { |
| 1406 | int r; |
| 1407 | |
| 1408 | down_read(&pmd->root_lock); |
| 1409 | r = dm_sm_get_nr_free(pmd->data_sm, result); |
| 1410 | up_read(&pmd->root_lock); |
| 1411 | |
| 1412 | return r; |
| 1413 | } |
| 1414 | |
| 1415 | int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd, |
| 1416 | dm_block_t *result) |
| 1417 | { |
| 1418 | int r; |
| 1419 | |
| 1420 | down_read(&pmd->root_lock); |
| 1421 | r = dm_sm_get_nr_free(pmd->metadata_sm, result); |
| 1422 | up_read(&pmd->root_lock); |
| 1423 | |
| 1424 | return r; |
| 1425 | } |
| 1426 | |
| 1427 | int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd, |
| 1428 | dm_block_t *result) |
| 1429 | { |
| 1430 | int r; |
| 1431 | |
| 1432 | down_read(&pmd->root_lock); |
| 1433 | r = dm_sm_get_nr_blocks(pmd->metadata_sm, result); |
| 1434 | up_read(&pmd->root_lock); |
| 1435 | |
| 1436 | return r; |
| 1437 | } |
| 1438 | |
| 1439 | int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result) |
| 1440 | { |
| 1441 | down_read(&pmd->root_lock); |
| 1442 | *result = pmd->data_block_size; |
| 1443 | up_read(&pmd->root_lock); |
| 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result) |
| 1449 | { |
| 1450 | int r; |
| 1451 | |
| 1452 | down_read(&pmd->root_lock); |
| 1453 | r = dm_sm_get_nr_blocks(pmd->data_sm, result); |
| 1454 | up_read(&pmd->root_lock); |
| 1455 | |
| 1456 | return r; |
| 1457 | } |
| 1458 | |
| 1459 | int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result) |
| 1460 | { |
| 1461 | struct dm_pool_metadata *pmd = td->pmd; |
| 1462 | |
| 1463 | down_read(&pmd->root_lock); |
| 1464 | *result = td->mapped_blocks; |
| 1465 | up_read(&pmd->root_lock); |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | static int __highest_block(struct dm_thin_device *td, dm_block_t *result) |
| 1471 | { |
| 1472 | int r; |
| 1473 | __le64 value_le; |
| 1474 | dm_block_t thin_root; |
| 1475 | struct dm_pool_metadata *pmd = td->pmd; |
| 1476 | |
| 1477 | r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le); |
| 1478 | if (r) |
| 1479 | return r; |
| 1480 | |
| 1481 | thin_root = le64_to_cpu(value_le); |
| 1482 | |
| 1483 | return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result); |
| 1484 | } |
| 1485 | |
| 1486 | int dm_thin_get_highest_mapped_block(struct dm_thin_device *td, |
| 1487 | dm_block_t *result) |
| 1488 | { |
| 1489 | int r; |
| 1490 | struct dm_pool_metadata *pmd = td->pmd; |
| 1491 | |
| 1492 | down_read(&pmd->root_lock); |
| 1493 | r = __highest_block(td, result); |
| 1494 | up_read(&pmd->root_lock); |
| 1495 | |
| 1496 | return r; |
| 1497 | } |
| 1498 | |
| 1499 | static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count) |
| 1500 | { |
| 1501 | int r; |
| 1502 | dm_block_t old_count; |
| 1503 | |
| 1504 | r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count); |
| 1505 | if (r) |
| 1506 | return r; |
| 1507 | |
| 1508 | if (new_count == old_count) |
| 1509 | return 0; |
| 1510 | |
| 1511 | if (new_count < old_count) { |
| 1512 | DMERR("cannot reduce size of data device"); |
| 1513 | return -EINVAL; |
| 1514 | } |
| 1515 | |
| 1516 | r = dm_sm_extend(pmd->data_sm, new_count - old_count); |
| 1517 | if (!r) |
| 1518 | pmd->need_commit = 1; |
| 1519 | |
| 1520 | return r; |
| 1521 | } |
| 1522 | |
| 1523 | int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count) |
| 1524 | { |
| 1525 | int r; |
| 1526 | |
| 1527 | down_write(&pmd->root_lock); |
| 1528 | r = __resize_data_dev(pmd, new_count); |
| 1529 | up_write(&pmd->root_lock); |
| 1530 | |
| 1531 | return r; |
| 1532 | } |