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