1/* 2 * Copyright (C) 2011-2012 Red Hat UK. 3 * 4 * This file is released under the GPL. 5 */ 6 7#include "dm-thin-metadata.h" 8#include "dm-bio-prison.h" 9#include "dm.h" 10 11#include <linux/device-mapper.h> 12#include <linux/dm-io.h> 13#include <linux/dm-kcopyd.h> 14#include <linux/list.h> 15#include <linux/rculist.h> 16#include <linux/init.h> 17#include <linux/module.h> 18#include <linux/slab.h> 19#include <linux/rbtree.h> 20 21#define DM_MSG_PREFIX "thin" 22 23/* 24 * Tunable constants 25 */ 26#define ENDIO_HOOK_POOL_SIZE 1024 27#define MAPPING_POOL_SIZE 1024 28#define PRISON_CELLS 1024 29#define COMMIT_PERIOD HZ 30#define NO_SPACE_TIMEOUT_SECS 60 31 32static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS; 33 34DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle, 35 "A percentage of time allocated for copy on write"); 36 37/* 38 * The block size of the device holding pool data must be 39 * between 64KB and 1GB. 40 */ 41#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT) 42#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) 43 44/* 45 * Device id is restricted to 24 bits. 46 */ 47#define MAX_DEV_ID ((1 << 24) - 1) 48 49/* 50 * How do we handle breaking sharing of data blocks? 51 * ================================================= 52 * 53 * We use a standard copy-on-write btree to store the mappings for the 54 * devices (note I'm talking about copy-on-write of the metadata here, not 55 * the data). When you take an internal snapshot you clone the root node 56 * of the origin btree. After this there is no concept of an origin or a 57 * snapshot. They are just two device trees that happen to point to the 58 * same data blocks. 59 * 60 * When we get a write in we decide if it's to a shared data block using 61 * some timestamp magic. If it is, we have to break sharing. 62 * 63 * Let's say we write to a shared block in what was the origin. The 64 * steps are: 65 * 66 * i) plug io further to this physical block. (see bio_prison code). 67 * 68 * ii) quiesce any read io to that shared data block. Obviously 69 * including all devices that share this block. (see dm_deferred_set code) 70 * 71 * iii) copy the data block to a newly allocate block. This step can be 72 * missed out if the io covers the block. (schedule_copy). 73 * 74 * iv) insert the new mapping into the origin's btree 75 * (process_prepared_mapping). This act of inserting breaks some 76 * sharing of btree nodes between the two devices. Breaking sharing only 77 * effects the btree of that specific device. Btrees for the other 78 * devices that share the block never change. The btree for the origin 79 * device as it was after the last commit is untouched, ie. we're using 80 * persistent data structures in the functional programming sense. 81 * 82 * v) unplug io to this physical block, including the io that triggered 83 * the breaking of sharing. 84 * 85 * Steps (ii) and (iii) occur in parallel. 86 * 87 * The metadata _doesn't_ need to be committed before the io continues. We 88 * get away with this because the io is always written to a _new_ block. 89 * If there's a crash, then: 90 * 91 * - The origin mapping will point to the old origin block (the shared 92 * one). This will contain the data as it was before the io that triggered 93 * the breaking of sharing came in. 94 * 95 * - The snap mapping still points to the old block. As it would after 96 * the commit. 97 * 98 * The downside of this scheme is the timestamp magic isn't perfect, and 99 * will continue to think that data block in the snapshot device is shared 100 * even after the write to the origin has broken sharing. I suspect data 101 * blocks will typically be shared by many different devices, so we're 102 * breaking sharing n + 1 times, rather than n, where n is the number of 103 * devices that reference this data block. At the moment I think the 104 * benefits far, far outweigh the disadvantages. 105 */ 106 107/*----------------------------------------------------------------*/ 108 109/* 110 * Key building. 111 */ 112static void build_data_key(struct dm_thin_device *td, 113 dm_block_t b, struct dm_cell_key *key) 114{ 115 key->virtual = 0; 116 key->dev = dm_thin_dev_id(td); 117 key->block = b; 118} 119 120static void build_virtual_key(struct dm_thin_device *td, dm_block_t b, 121 struct dm_cell_key *key) 122{ 123 key->virtual = 1; 124 key->dev = dm_thin_dev_id(td); 125 key->block = b; 126} 127 128/*----------------------------------------------------------------*/ 129 130/* 131 * A pool device ties together a metadata device and a data device. It 132 * also provides the interface for creating and destroying internal 133 * devices. 134 */ 135struct dm_thin_new_mapping; 136 137/* 138 * The pool runs in 4 modes. Ordered in degraded order for comparisons. 139 */ 140enum pool_mode { 141 PM_WRITE, /* metadata may be changed */ 142 PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */ 143 PM_READ_ONLY, /* metadata may not be changed */ 144 PM_FAIL, /* all I/O fails */ 145}; 146 147struct pool_features { 148 enum pool_mode mode; 149 150 bool zero_new_blocks:1; 151 bool discard_enabled:1; 152 bool discard_passdown:1; 153 bool error_if_no_space:1; 154}; 155 156struct thin_c; 157typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio); 158typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m); 159 160struct pool { 161 struct list_head list; 162 struct dm_target *ti; /* Only set if a pool target is bound */ 163 164 struct mapped_device *pool_md; 165 struct block_device *md_dev; 166 struct dm_pool_metadata *pmd; 167 168 dm_block_t low_water_blocks; 169 uint32_t sectors_per_block; 170 int sectors_per_block_shift; 171 172 struct pool_features pf; 173 bool low_water_triggered:1; /* A dm event has been sent */ 174 175 struct dm_bio_prison *prison; 176 struct dm_kcopyd_client *copier; 177 178 struct workqueue_struct *wq; 179 struct work_struct worker; 180 struct delayed_work waker; 181 struct delayed_work no_space_timeout; 182 183 unsigned long last_commit_jiffies; 184 unsigned ref_count; 185 186 spinlock_t lock; 187 struct bio_list deferred_flush_bios; 188 struct list_head prepared_mappings; 189 struct list_head prepared_discards; 190 struct list_head active_thins; 191 192 struct dm_deferred_set *shared_read_ds; 193 struct dm_deferred_set *all_io_ds; 194 195 struct dm_thin_new_mapping *next_mapping; 196 mempool_t *mapping_pool; 197 198 process_bio_fn process_bio; 199 process_bio_fn process_discard; 200 201 process_mapping_fn process_prepared_mapping; 202 process_mapping_fn process_prepared_discard; 203}; 204 205static enum pool_mode get_pool_mode(struct pool *pool); 206static void metadata_operation_failed(struct pool *pool, const char *op, int r); 207 208/* 209 * Target context for a pool. 210 */ 211struct pool_c { 212 struct dm_target *ti; 213 struct pool *pool; 214 struct dm_dev *data_dev; 215 struct dm_dev *metadata_dev; 216 struct dm_target_callbacks callbacks; 217 218 dm_block_t low_water_blocks; 219 struct pool_features requested_pf; /* Features requested during table load */ 220 struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */ 221}; 222 223/* 224 * Target context for a thin. 225 */ 226struct thin_c { 227 struct list_head list; 228 struct dm_dev *pool_dev; 229 struct dm_dev *origin_dev; 230 sector_t origin_size; 231 dm_thin_id dev_id; 232 233 struct pool *pool; 234 struct dm_thin_device *td; 235 bool requeue_mode:1; 236 spinlock_t lock; 237 struct bio_list deferred_bio_list; 238 struct bio_list retry_on_resume_list; 239 struct rb_root sort_bio_list; /* sorted list of deferred bios */ 240 241 /* 242 * Ensures the thin is not destroyed until the worker has finished 243 * iterating the active_thins list. 244 */ 245 atomic_t refcount; 246 struct completion can_destroy; 247}; 248 249/*----------------------------------------------------------------*/ 250 251/* 252 * wake_worker() is used when new work is queued and when pool_resume is 253 * ready to continue deferred IO processing. 254 */ 255static void wake_worker(struct pool *pool) 256{ 257 queue_work(pool->wq, &pool->worker); 258} 259 260/*----------------------------------------------------------------*/ 261 262static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio, 263 struct dm_bio_prison_cell **cell_result) 264{ 265 int r; 266 struct dm_bio_prison_cell *cell_prealloc; 267 268 /* 269 * Allocate a cell from the prison's mempool. 270 * This might block but it can't fail. 271 */ 272 cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO); 273 274 r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result); 275 if (r) 276 /* 277 * We reused an old cell; we can get rid of 278 * the new one. 279 */ 280 dm_bio_prison_free_cell(pool->prison, cell_prealloc); 281 282 return r; 283} 284 285static void cell_release(struct pool *pool, 286 struct dm_bio_prison_cell *cell, 287 struct bio_list *bios) 288{ 289 dm_cell_release(pool->prison, cell, bios); 290 dm_bio_prison_free_cell(pool->prison, cell); 291} 292 293static void cell_release_no_holder(struct pool *pool, 294 struct dm_bio_prison_cell *cell, 295 struct bio_list *bios) 296{ 297 dm_cell_release_no_holder(pool->prison, cell, bios); 298 dm_bio_prison_free_cell(pool->prison, cell); 299} 300 301static void cell_defer_no_holder_no_free(struct thin_c *tc, 302 struct dm_bio_prison_cell *cell) 303{ 304 struct pool *pool = tc->pool; 305 unsigned long flags; 306 307 spin_lock_irqsave(&tc->lock, flags); 308 dm_cell_release_no_holder(pool->prison, cell, &tc->deferred_bio_list); 309 spin_unlock_irqrestore(&tc->lock, flags); 310 311 wake_worker(pool); 312} 313 314static void cell_error_with_code(struct pool *pool, 315 struct dm_bio_prison_cell *cell, int error_code) 316{ 317 dm_cell_error(pool->prison, cell, error_code); 318 dm_bio_prison_free_cell(pool->prison, cell); 319} 320 321static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell) 322{ 323 cell_error_with_code(pool, cell, -EIO); 324} 325 326/*----------------------------------------------------------------*/ 327 328/* 329 * A global list of pools that uses a struct mapped_device as a key. 330 */ 331static struct dm_thin_pool_table { 332 struct mutex mutex; 333 struct list_head pools; 334} dm_thin_pool_table; 335 336static void pool_table_init(void) 337{ 338 mutex_init(&dm_thin_pool_table.mutex); 339 INIT_LIST_HEAD(&dm_thin_pool_table.pools); 340} 341 342static void __pool_table_insert(struct pool *pool) 343{ 344 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 345 list_add(&pool->list, &dm_thin_pool_table.pools); 346} 347 348static void __pool_table_remove(struct pool *pool) 349{ 350 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 351 list_del(&pool->list); 352} 353 354static struct pool *__pool_table_lookup(struct mapped_device *md) 355{ 356 struct pool *pool = NULL, *tmp; 357 358 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 359 360 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) { 361 if (tmp->pool_md == md) { 362 pool = tmp; 363 break; 364 } 365 } 366 367 return pool; 368} 369 370static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev) 371{ 372 struct pool *pool = NULL, *tmp; 373 374 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 375 376 list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) { 377 if (tmp->md_dev == md_dev) { 378 pool = tmp; 379 break; 380 } 381 } 382 383 return pool; 384} 385 386/*----------------------------------------------------------------*/ 387 388struct dm_thin_endio_hook { 389 struct thin_c *tc; 390 struct dm_deferred_entry *shared_read_entry; 391 struct dm_deferred_entry *all_io_entry; 392 struct dm_thin_new_mapping *overwrite_mapping; 393 struct rb_node rb_node; 394}; 395 396static void requeue_bio_list(struct thin_c *tc, struct bio_list *master) 397{ 398 struct bio *bio; 399 struct bio_list bios; 400 unsigned long flags; 401 402 bio_list_init(&bios); 403 404 spin_lock_irqsave(&tc->lock, flags); 405 bio_list_merge(&bios, master); 406 bio_list_init(master); 407 spin_unlock_irqrestore(&tc->lock, flags); 408 409 while ((bio = bio_list_pop(&bios))) 410 bio_endio(bio, DM_ENDIO_REQUEUE); 411} 412 413static void requeue_io(struct thin_c *tc) 414{ 415 requeue_bio_list(tc, &tc->deferred_bio_list); 416 requeue_bio_list(tc, &tc->retry_on_resume_list); 417} 418 419static void error_thin_retry_list(struct thin_c *tc) 420{ 421 struct bio *bio; 422 unsigned long flags; 423 struct bio_list bios; 424 425 bio_list_init(&bios); 426 427 spin_lock_irqsave(&tc->lock, flags); 428 bio_list_merge(&bios, &tc->retry_on_resume_list); 429 bio_list_init(&tc->retry_on_resume_list); 430 spin_unlock_irqrestore(&tc->lock, flags); 431 432 while ((bio = bio_list_pop(&bios))) 433 bio_io_error(bio); 434} 435 436static void error_retry_list(struct pool *pool) 437{ 438 struct thin_c *tc; 439 440 rcu_read_lock(); 441 list_for_each_entry_rcu(tc, &pool->active_thins, list) 442 error_thin_retry_list(tc); 443 rcu_read_unlock(); 444} 445 446/* 447 * This section of code contains the logic for processing a thin device's IO. 448 * Much of the code depends on pool object resources (lists, workqueues, etc) 449 * but most is exclusively called from the thin target rather than the thin-pool 450 * target. 451 */ 452 453static bool block_size_is_power_of_two(struct pool *pool) 454{ 455 return pool->sectors_per_block_shift >= 0; 456} 457 458static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio) 459{ 460 struct pool *pool = tc->pool; 461 sector_t block_nr = bio->bi_iter.bi_sector; 462 463 if (block_size_is_power_of_two(pool)) 464 block_nr >>= pool->sectors_per_block_shift; 465 else 466 (void) sector_div(block_nr, pool->sectors_per_block); 467 468 return block_nr; 469} 470 471static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block) 472{ 473 struct pool *pool = tc->pool; 474 sector_t bi_sector = bio->bi_iter.bi_sector; 475 476 bio->bi_bdev = tc->pool_dev->bdev; 477 if (block_size_is_power_of_two(pool)) 478 bio->bi_iter.bi_sector = 479 (block << pool->sectors_per_block_shift) | 480 (bi_sector & (pool->sectors_per_block - 1)); 481 else 482 bio->bi_iter.bi_sector = (block * pool->sectors_per_block) + 483 sector_div(bi_sector, pool->sectors_per_block); 484} 485 486static void remap_to_origin(struct thin_c *tc, struct bio *bio) 487{ 488 bio->bi_bdev = tc->origin_dev->bdev; 489} 490 491static int bio_triggers_commit(struct thin_c *tc, struct bio *bio) 492{ 493 return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && 494 dm_thin_changed_this_transaction(tc->td); 495} 496 497static void inc_all_io_entry(struct pool *pool, struct bio *bio) 498{ 499 struct dm_thin_endio_hook *h; 500 501 if (bio->bi_rw & REQ_DISCARD) 502 return; 503 504 h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 505 h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds); 506} 507 508static void issue(struct thin_c *tc, struct bio *bio) 509{ 510 struct pool *pool = tc->pool; 511 unsigned long flags; 512 513 if (!bio_triggers_commit(tc, bio)) { 514 generic_make_request(bio); 515 return; 516 } 517 518 /* 519 * Complete bio with an error if earlier I/O caused changes to 520 * the metadata that can't be committed e.g, due to I/O errors 521 * on the metadata device. 522 */ 523 if (dm_thin_aborted_changes(tc->td)) { 524 bio_io_error(bio); 525 return; 526 } 527 528 /* 529 * Batch together any bios that trigger commits and then issue a 530 * single commit for them in process_deferred_bios(). 531 */ 532 spin_lock_irqsave(&pool->lock, flags); 533 bio_list_add(&pool->deferred_flush_bios, bio); 534 spin_unlock_irqrestore(&pool->lock, flags); 535} 536 537static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio) 538{ 539 remap_to_origin(tc, bio); 540 issue(tc, bio); 541} 542 543static void remap_and_issue(struct thin_c *tc, struct bio *bio, 544 dm_block_t block) 545{ 546 remap(tc, bio, block); 547 issue(tc, bio); 548} 549 550/*----------------------------------------------------------------*/ 551 552/* 553 * Bio endio functions. 554 */ 555struct dm_thin_new_mapping { 556 struct list_head list; 557 558 bool pass_discard:1; 559 bool definitely_not_shared:1; 560 561 /* 562 * Track quiescing, copying and zeroing preparation actions. When this 563 * counter hits zero the block is prepared and can be inserted into the 564 * btree. 565 */ 566 atomic_t prepare_actions; 567 568 int err; 569 struct thin_c *tc; 570 dm_block_t virt_block; 571 dm_block_t data_block; 572 struct dm_bio_prison_cell *cell, *cell2; 573 574 /* 575 * If the bio covers the whole area of a block then we can avoid 576 * zeroing or copying. Instead this bio is hooked. The bio will 577 * still be in the cell, so care has to be taken to avoid issuing 578 * the bio twice. 579 */ 580 struct bio *bio; 581 bio_end_io_t *saved_bi_end_io; 582}; 583 584static void __complete_mapping_preparation(struct dm_thin_new_mapping *m) 585{ 586 struct pool *pool = m->tc->pool; 587 588 if (atomic_dec_and_test(&m->prepare_actions)) { 589 list_add_tail(&m->list, &pool->prepared_mappings); 590 wake_worker(pool); 591 } 592} 593 594static void complete_mapping_preparation(struct dm_thin_new_mapping *m) 595{ 596 unsigned long flags; 597 struct pool *pool = m->tc->pool; 598 599 spin_lock_irqsave(&pool->lock, flags); 600 __complete_mapping_preparation(m); 601 spin_unlock_irqrestore(&pool->lock, flags); 602} 603 604static void copy_complete(int read_err, unsigned long write_err, void *context) 605{ 606 struct dm_thin_new_mapping *m = context; 607 608 m->err = read_err || write_err ? -EIO : 0; 609 complete_mapping_preparation(m); 610} 611 612static void overwrite_endio(struct bio *bio, int err) 613{ 614 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 615 struct dm_thin_new_mapping *m = h->overwrite_mapping; 616 617 m->err = err; 618 complete_mapping_preparation(m); 619} 620 621/*----------------------------------------------------------------*/ 622 623/* 624 * Workqueue. 625 */ 626 627/* 628 * Prepared mapping jobs. 629 */ 630 631/* 632 * This sends the bios in the cell back to the deferred_bios list. 633 */ 634static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell) 635{ 636 struct pool *pool = tc->pool; 637 unsigned long flags; 638 639 spin_lock_irqsave(&tc->lock, flags); 640 cell_release(pool, cell, &tc->deferred_bio_list); 641 spin_unlock_irqrestore(&tc->lock, flags); 642 643 wake_worker(pool); 644} 645 646/* 647 * Same as cell_defer above, except it omits the original holder of the cell. 648 */ 649static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell) 650{ 651 struct pool *pool = tc->pool; 652 unsigned long flags; 653 654 spin_lock_irqsave(&tc->lock, flags); 655 cell_release_no_holder(pool, cell, &tc->deferred_bio_list); 656 spin_unlock_irqrestore(&tc->lock, flags); 657 658 wake_worker(pool); 659} 660 661static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m) 662{ 663 if (m->bio) { 664 m->bio->bi_end_io = m->saved_bi_end_io; 665 atomic_inc(&m->bio->bi_remaining); 666 } 667 cell_error(m->tc->pool, m->cell); 668 list_del(&m->list); 669 mempool_free(m, m->tc->pool->mapping_pool); 670} 671 672static void process_prepared_mapping(struct dm_thin_new_mapping *m) 673{ 674 struct thin_c *tc = m->tc; 675 struct pool *pool = tc->pool; 676 struct bio *bio; 677 int r; 678 679 bio = m->bio; 680 if (bio) { 681 bio->bi_end_io = m->saved_bi_end_io; 682 atomic_inc(&bio->bi_remaining); 683 } 684 685 if (m->err) { 686 cell_error(pool, m->cell); 687 goto out; 688 } 689 690 /* 691 * Commit the prepared block into the mapping btree. 692 * Any I/O for this block arriving after this point will get 693 * remapped to it directly. 694 */ 695 r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block); 696 if (r) { 697 metadata_operation_failed(pool, "dm_thin_insert_block", r); 698 cell_error(pool, m->cell); 699 goto out; 700 } 701 702 /* 703 * Release any bios held while the block was being provisioned. 704 * If we are processing a write bio that completely covers the block, 705 * we already processed it so can ignore it now when processing 706 * the bios in the cell. 707 */ 708 if (bio) { 709 cell_defer_no_holder(tc, m->cell); 710 bio_endio(bio, 0); 711 } else 712 cell_defer(tc, m->cell); 713 714out: 715 list_del(&m->list); 716 mempool_free(m, pool->mapping_pool); 717} 718 719static void process_prepared_discard_fail(struct dm_thin_new_mapping *m) 720{ 721 struct thin_c *tc = m->tc; 722 723 bio_io_error(m->bio); 724 cell_defer_no_holder(tc, m->cell); 725 cell_defer_no_holder(tc, m->cell2); 726 mempool_free(m, tc->pool->mapping_pool); 727} 728 729static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m) 730{ 731 struct thin_c *tc = m->tc; 732 733 inc_all_io_entry(tc->pool, m->bio); 734 cell_defer_no_holder(tc, m->cell); 735 cell_defer_no_holder(tc, m->cell2); 736 737 if (m->pass_discard) 738 if (m->definitely_not_shared) 739 remap_and_issue(tc, m->bio, m->data_block); 740 else { 741 bool used = false; 742 if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used) 743 bio_endio(m->bio, 0); 744 else 745 remap_and_issue(tc, m->bio, m->data_block); 746 } 747 else 748 bio_endio(m->bio, 0); 749 750 mempool_free(m, tc->pool->mapping_pool); 751} 752 753static void process_prepared_discard(struct dm_thin_new_mapping *m) 754{ 755 int r; 756 struct thin_c *tc = m->tc; 757 758 r = dm_thin_remove_block(tc->td, m->virt_block); 759 if (r) 760 DMERR_LIMIT("dm_thin_remove_block() failed"); 761 762 process_prepared_discard_passdown(m); 763} 764 765static void process_prepared(struct pool *pool, struct list_head *head, 766 process_mapping_fn *fn) 767{ 768 unsigned long flags; 769 struct list_head maps; 770 struct dm_thin_new_mapping *m, *tmp; 771 772 INIT_LIST_HEAD(&maps); 773 spin_lock_irqsave(&pool->lock, flags); 774 list_splice_init(head, &maps); 775 spin_unlock_irqrestore(&pool->lock, flags); 776 777 list_for_each_entry_safe(m, tmp, &maps, list) 778 (*fn)(m); 779} 780 781/* 782 * Deferred bio jobs. 783 */ 784static int io_overlaps_block(struct pool *pool, struct bio *bio) 785{ 786 return bio->bi_iter.bi_size == 787 (pool->sectors_per_block << SECTOR_SHIFT); 788} 789 790static int io_overwrites_block(struct pool *pool, struct bio *bio) 791{ 792 return (bio_data_dir(bio) == WRITE) && 793 io_overlaps_block(pool, bio); 794} 795 796static void save_and_set_endio(struct bio *bio, bio_end_io_t **save, 797 bio_end_io_t *fn) 798{ 799 *save = bio->bi_end_io; 800 bio->bi_end_io = fn; 801} 802 803static int ensure_next_mapping(struct pool *pool) 804{ 805 if (pool->next_mapping) 806 return 0; 807 808 pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC); 809 810 return pool->next_mapping ? 0 : -ENOMEM; 811} 812 813static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool) 814{ 815 struct dm_thin_new_mapping *m = pool->next_mapping; 816 817 BUG_ON(!pool->next_mapping); 818 819 memset(m, 0, sizeof(struct dm_thin_new_mapping)); 820 INIT_LIST_HEAD(&m->list); 821 m->bio = NULL; 822 823 pool->next_mapping = NULL; 824 825 return m; 826} 827 828static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m, 829 sector_t begin, sector_t end) 830{ 831 int r; 832 struct dm_io_region to; 833 834 to.bdev = tc->pool_dev->bdev; 835 to.sector = begin; 836 to.count = end - begin; 837 838 r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m); 839 if (r < 0) { 840 DMERR_LIMIT("dm_kcopyd_zero() failed"); 841 copy_complete(1, 1, m); 842 } 843} 844 845/* 846 * A partial copy also needs to zero the uncopied region. 847 */ 848static void schedule_copy(struct thin_c *tc, dm_block_t virt_block, 849 struct dm_dev *origin, dm_block_t data_origin, 850 dm_block_t data_dest, 851 struct dm_bio_prison_cell *cell, struct bio *bio, 852 sector_t len) 853{ 854 int r; 855 struct pool *pool = tc->pool; 856 struct dm_thin_new_mapping *m = get_next_mapping(pool); 857 858 m->tc = tc; 859 m->virt_block = virt_block; 860 m->data_block = data_dest; 861 m->cell = cell; 862 863 /* 864 * quiesce action + copy action + an extra reference held for the 865 * duration of this function (we may need to inc later for a 866 * partial zero). 867 */ 868 atomic_set(&m->prepare_actions, 3); 869 870 if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list)) 871 complete_mapping_preparation(m); /* already quiesced */ 872 873 /* 874 * IO to pool_dev remaps to the pool target's data_dev. 875 * 876 * If the whole block of data is being overwritten, we can issue the 877 * bio immediately. Otherwise we use kcopyd to clone the data first. 878 */ 879 if (io_overwrites_block(pool, bio)) { 880 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 881 882 h->overwrite_mapping = m; 883 m->bio = bio; 884 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio); 885 inc_all_io_entry(pool, bio); 886 remap_and_issue(tc, bio, data_dest); 887 } else { 888 struct dm_io_region from, to; 889 890 from.bdev = origin->bdev; 891 from.sector = data_origin * pool->sectors_per_block; 892 from.count = len; 893 894 to.bdev = tc->pool_dev->bdev; 895 to.sector = data_dest * pool->sectors_per_block; 896 to.count = len; 897 898 r = dm_kcopyd_copy(pool->copier, &from, 1, &to, 899 0, copy_complete, m); 900 if (r < 0) { 901 DMERR_LIMIT("dm_kcopyd_copy() failed"); 902 copy_complete(1, 1, m); 903 904 /* 905 * We allow the zero to be issued, to simplify the 906 * error path. Otherwise we'd need to start 907 * worrying about decrementing the prepare_actions 908 * counter. 909 */ 910 } 911 912 /* 913 * Do we need to zero a tail region? 914 */ 915 if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) { 916 atomic_inc(&m->prepare_actions); 917 ll_zero(tc, m, 918 data_dest * pool->sectors_per_block + len, 919 (data_dest + 1) * pool->sectors_per_block); 920 } 921 } 922 923 complete_mapping_preparation(m); /* drop our ref */ 924} 925 926static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block, 927 dm_block_t data_origin, dm_block_t data_dest, 928 struct dm_bio_prison_cell *cell, struct bio *bio) 929{ 930 schedule_copy(tc, virt_block, tc->pool_dev, 931 data_origin, data_dest, cell, bio, 932 tc->pool->sectors_per_block); 933} 934 935static void schedule_zero(struct thin_c *tc, dm_block_t virt_block, 936 dm_block_t data_block, struct dm_bio_prison_cell *cell, 937 struct bio *bio) 938{ 939 struct pool *pool = tc->pool; 940 struct dm_thin_new_mapping *m = get_next_mapping(pool); 941 942 atomic_set(&m->prepare_actions, 1); /* no need to quiesce */ 943 m->tc = tc; 944 m->virt_block = virt_block; 945 m->data_block = data_block; 946 m->cell = cell; 947 948 /* 949 * If the whole block of data is being overwritten or we are not 950 * zeroing pre-existing data, we can issue the bio immediately. 951 * Otherwise we use kcopyd to zero the data first. 952 */ 953 if (!pool->pf.zero_new_blocks) 954 process_prepared_mapping(m); 955 956 else if (io_overwrites_block(pool, bio)) { 957 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 958 959 h->overwrite_mapping = m; 960 m->bio = bio; 961 save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio); 962 inc_all_io_entry(pool, bio); 963 remap_and_issue(tc, bio, data_block); 964 965 } else 966 ll_zero(tc, m, 967 data_block * pool->sectors_per_block, 968 (data_block + 1) * pool->sectors_per_block); 969} 970 971static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block, 972 dm_block_t data_dest, 973 struct dm_bio_prison_cell *cell, struct bio *bio) 974{ 975 struct pool *pool = tc->pool; 976 sector_t virt_block_begin = virt_block * pool->sectors_per_block; 977 sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block; 978 979 if (virt_block_end <= tc->origin_size) 980 schedule_copy(tc, virt_block, tc->origin_dev, 981 virt_block, data_dest, cell, bio, 982 pool->sectors_per_block); 983 984 else if (virt_block_begin < tc->origin_size) 985 schedule_copy(tc, virt_block, tc->origin_dev, 986 virt_block, data_dest, cell, bio, 987 tc->origin_size - virt_block_begin); 988 989 else 990 schedule_zero(tc, virt_block, data_dest, cell, bio); 991} 992 993/* 994 * A non-zero return indicates read_only or fail_io mode. 995 * Many callers don't care about the return value. 996 */ 997static int commit(struct pool *pool) 998{ 999 int r; 1000 1001 if (get_pool_mode(pool) >= PM_READ_ONLY) 1002 return -EINVAL; 1003 1004 r = dm_pool_commit_metadata(pool->pmd); 1005 if (r) 1006 metadata_operation_failed(pool, "dm_pool_commit_metadata", r); 1007 1008 return r; 1009} 1010 1011static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks) 1012{ 1013 unsigned long flags; 1014 1015 if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) { 1016 DMWARN("%s: reached low water mark for data device: sending event.", 1017 dm_device_name(pool->pool_md)); 1018 spin_lock_irqsave(&pool->lock, flags); 1019 pool->low_water_triggered = true; 1020 spin_unlock_irqrestore(&pool->lock, flags); 1021 dm_table_event(pool->ti->table); 1022 } 1023} 1024 1025static void set_pool_mode(struct pool *pool, enum pool_mode new_mode); 1026 1027static int alloc_data_block(struct thin_c *tc, dm_block_t *result) 1028{ 1029 int r; 1030 dm_block_t free_blocks; 1031 struct pool *pool = tc->pool; 1032 1033 if (WARN_ON(get_pool_mode(pool) != PM_WRITE)) 1034 return -EINVAL; 1035 1036 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); 1037 if (r) { 1038 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); 1039 return r; 1040 } 1041 1042 check_low_water_mark(pool, free_blocks); 1043 1044 if (!free_blocks) { 1045 /* 1046 * Try to commit to see if that will free up some 1047 * more space. 1048 */ 1049 r = commit(pool); 1050 if (r) 1051 return r; 1052 1053 r = dm_pool_get_free_block_count(pool->pmd, &free_blocks); 1054 if (r) { 1055 metadata_operation_failed(pool, "dm_pool_get_free_block_count", r); 1056 return r; 1057 } 1058 1059 if (!free_blocks) { 1060 set_pool_mode(pool, PM_OUT_OF_DATA_SPACE); 1061 return -ENOSPC; 1062 } 1063 } 1064 1065 r = dm_pool_alloc_data_block(pool->pmd, result); 1066 if (r) { 1067 metadata_operation_failed(pool, "dm_pool_alloc_data_block", r); 1068 return r; 1069 } 1070 1071 return 0; 1072} 1073 1074/* 1075 * If we have run out of space, queue bios until the device is 1076 * resumed, presumably after having been reloaded with more space. 1077 */ 1078static void retry_on_resume(struct bio *bio) 1079{ 1080 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 1081 struct thin_c *tc = h->tc; 1082 unsigned long flags; 1083 1084 spin_lock_irqsave(&tc->lock, flags); 1085 bio_list_add(&tc->retry_on_resume_list, bio); 1086 spin_unlock_irqrestore(&tc->lock, flags); 1087} 1088 1089static int should_error_unserviceable_bio(struct pool *pool) 1090{ 1091 enum pool_mode m = get_pool_mode(pool); 1092 1093 switch (m) { 1094 case PM_WRITE: 1095 /* Shouldn't get here */ 1096 DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode"); 1097 return -EIO; 1098 1099 case PM_OUT_OF_DATA_SPACE: 1100 return pool->pf.error_if_no_space ? -ENOSPC : 0; 1101 1102 case PM_READ_ONLY: 1103 case PM_FAIL: 1104 return -EIO; 1105 default: 1106 /* Shouldn't get here */ 1107 DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode"); 1108 return -EIO; 1109 } 1110} 1111 1112static void handle_unserviceable_bio(struct pool *pool, struct bio *bio) 1113{ 1114 int error = should_error_unserviceable_bio(pool); 1115 1116 if (error) 1117 bio_endio(bio, error); 1118 else 1119 retry_on_resume(bio); 1120} 1121 1122static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell) 1123{ 1124 struct bio *bio; 1125 struct bio_list bios; 1126 int error; 1127 1128 error = should_error_unserviceable_bio(pool); 1129 if (error) { 1130 cell_error_with_code(pool, cell, error); 1131 return; 1132 } 1133 1134 bio_list_init(&bios); 1135 cell_release(pool, cell, &bios); 1136 1137 error = should_error_unserviceable_bio(pool); 1138 if (error) 1139 while ((bio = bio_list_pop(&bios))) 1140 bio_endio(bio, error); 1141 else 1142 while ((bio = bio_list_pop(&bios))) 1143 retry_on_resume(bio); 1144} 1145 1146static void process_discard(struct thin_c *tc, struct bio *bio) 1147{ 1148 int r; 1149 unsigned long flags; 1150 struct pool *pool = tc->pool; 1151 struct dm_bio_prison_cell *cell, *cell2; 1152 struct dm_cell_key key, key2; 1153 dm_block_t block = get_bio_block(tc, bio); 1154 struct dm_thin_lookup_result lookup_result; 1155 struct dm_thin_new_mapping *m; 1156 1157 build_virtual_key(tc->td, block, &key); 1158 if (bio_detain(tc->pool, &key, bio, &cell)) 1159 return; 1160 1161 r = dm_thin_find_block(tc->td, block, 1, &lookup_result); 1162 switch (r) { 1163 case 0: 1164 /* 1165 * Check nobody is fiddling with this pool block. This can 1166 * happen if someone's in the process of breaking sharing 1167 * on this block. 1168 */ 1169 build_data_key(tc->td, lookup_result.block, &key2); 1170 if (bio_detain(tc->pool, &key2, bio, &cell2)) { 1171 cell_defer_no_holder(tc, cell); 1172 break; 1173 } 1174 1175 if (io_overlaps_block(pool, bio)) { 1176 /* 1177 * IO may still be going to the destination block. We must 1178 * quiesce before we can do the removal. 1179 */ 1180 m = get_next_mapping(pool); 1181 m->tc = tc; 1182 m->pass_discard = pool->pf.discard_passdown; 1183 m->definitely_not_shared = !lookup_result.shared; 1184 m->virt_block = block; 1185 m->data_block = lookup_result.block; 1186 m->cell = cell; 1187 m->cell2 = cell2; 1188 m->bio = bio; 1189 1190 if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) { 1191 spin_lock_irqsave(&pool->lock, flags); 1192 list_add_tail(&m->list, &pool->prepared_discards); 1193 spin_unlock_irqrestore(&pool->lock, flags); 1194 wake_worker(pool); 1195 } 1196 } else { 1197 inc_all_io_entry(pool, bio); 1198 cell_defer_no_holder(tc, cell); 1199 cell_defer_no_holder(tc, cell2); 1200 1201 /* 1202 * The DM core makes sure that the discard doesn't span 1203 * a block boundary. So we submit the discard of a 1204 * partial block appropriately. 1205 */ 1206 if ((!lookup_result.shared) && pool->pf.discard_passdown) 1207 remap_and_issue(tc, bio, lookup_result.block); 1208 else 1209 bio_endio(bio, 0); 1210 } 1211 break; 1212 1213 case -ENODATA: 1214 /* 1215 * It isn't provisioned, just forget it. 1216 */ 1217 cell_defer_no_holder(tc, cell); 1218 bio_endio(bio, 0); 1219 break; 1220 1221 default: 1222 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", 1223 __func__, r); 1224 cell_defer_no_holder(tc, cell); 1225 bio_io_error(bio); 1226 break; 1227 } 1228} 1229 1230static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block, 1231 struct dm_cell_key *key, 1232 struct dm_thin_lookup_result *lookup_result, 1233 struct dm_bio_prison_cell *cell) 1234{ 1235 int r; 1236 dm_block_t data_block; 1237 struct pool *pool = tc->pool; 1238 1239 r = alloc_data_block(tc, &data_block); 1240 switch (r) { 1241 case 0: 1242 schedule_internal_copy(tc, block, lookup_result->block, 1243 data_block, cell, bio); 1244 break; 1245 1246 case -ENOSPC: 1247 retry_bios_on_resume(pool, cell); 1248 break; 1249 1250 default: 1251 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", 1252 __func__, r); 1253 cell_error(pool, cell); 1254 break; 1255 } 1256} 1257 1258static void process_shared_bio(struct thin_c *tc, struct bio *bio, 1259 dm_block_t block, 1260 struct dm_thin_lookup_result *lookup_result) 1261{ 1262 struct dm_bio_prison_cell *cell; 1263 struct pool *pool = tc->pool; 1264 struct dm_cell_key key; 1265 1266 /* 1267 * If cell is already occupied, then sharing is already in the process 1268 * of being broken so we have nothing further to do here. 1269 */ 1270 build_data_key(tc->td, lookup_result->block, &key); 1271 if (bio_detain(pool, &key, bio, &cell)) 1272 return; 1273 1274 if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) 1275 break_sharing(tc, bio, block, &key, lookup_result, cell); 1276 else { 1277 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 1278 1279 h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds); 1280 inc_all_io_entry(pool, bio); 1281 cell_defer_no_holder(tc, cell); 1282 1283 remap_and_issue(tc, bio, lookup_result->block); 1284 } 1285} 1286 1287static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block, 1288 struct dm_bio_prison_cell *cell) 1289{ 1290 int r; 1291 dm_block_t data_block; 1292 struct pool *pool = tc->pool; 1293 1294 /* 1295 * Remap empty bios (flushes) immediately, without provisioning. 1296 */ 1297 if (!bio->bi_iter.bi_size) { 1298 inc_all_io_entry(pool, bio); 1299 cell_defer_no_holder(tc, cell); 1300 1301 remap_and_issue(tc, bio, 0); 1302 return; 1303 } 1304 1305 /* 1306 * Fill read bios with zeroes and complete them immediately. 1307 */ 1308 if (bio_data_dir(bio) == READ) { 1309 zero_fill_bio(bio); 1310 cell_defer_no_holder(tc, cell); 1311 bio_endio(bio, 0); 1312 return; 1313 } 1314 1315 r = alloc_data_block(tc, &data_block); 1316 switch (r) { 1317 case 0: 1318 if (tc->origin_dev) 1319 schedule_external_copy(tc, block, data_block, cell, bio); 1320 else 1321 schedule_zero(tc, block, data_block, cell, bio); 1322 break; 1323 1324 case -ENOSPC: 1325 retry_bios_on_resume(pool, cell); 1326 break; 1327 1328 default: 1329 DMERR_LIMIT("%s: alloc_data_block() failed: error = %d", 1330 __func__, r); 1331 cell_error(pool, cell); 1332 break; 1333 } 1334} 1335 1336static void process_bio(struct thin_c *tc, struct bio *bio) 1337{ 1338 int r; 1339 struct pool *pool = tc->pool; 1340 dm_block_t block = get_bio_block(tc, bio); 1341 struct dm_bio_prison_cell *cell; 1342 struct dm_cell_key key; 1343 struct dm_thin_lookup_result lookup_result; 1344 1345 /* 1346 * If cell is already occupied, then the block is already 1347 * being provisioned so we have nothing further to do here. 1348 */ 1349 build_virtual_key(tc->td, block, &key); 1350 if (bio_detain(pool, &key, bio, &cell)) 1351 return; 1352 1353 r = dm_thin_find_block(tc->td, block, 1, &lookup_result); 1354 switch (r) { 1355 case 0: 1356 if (lookup_result.shared) { 1357 process_shared_bio(tc, bio, block, &lookup_result); 1358 cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */ 1359 } else { 1360 inc_all_io_entry(pool, bio); 1361 cell_defer_no_holder(tc, cell); 1362 1363 remap_and_issue(tc, bio, lookup_result.block); 1364 } 1365 break; 1366 1367 case -ENODATA: 1368 if (bio_data_dir(bio) == READ && tc->origin_dev) { 1369 inc_all_io_entry(pool, bio); 1370 cell_defer_no_holder(tc, cell); 1371 1372 if (bio_end_sector(bio) <= tc->origin_size) 1373 remap_to_origin_and_issue(tc, bio); 1374 1375 else if (bio->bi_iter.bi_sector < tc->origin_size) { 1376 zero_fill_bio(bio); 1377 bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT; 1378 remap_to_origin_and_issue(tc, bio); 1379 1380 } else { 1381 zero_fill_bio(bio); 1382 bio_endio(bio, 0); 1383 } 1384 } else 1385 provision_block(tc, bio, block, cell); 1386 break; 1387 1388 default: 1389 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", 1390 __func__, r); 1391 cell_defer_no_holder(tc, cell); 1392 bio_io_error(bio); 1393 break; 1394 } 1395} 1396 1397static void process_bio_read_only(struct thin_c *tc, struct bio *bio) 1398{ 1399 int r; 1400 int rw = bio_data_dir(bio); 1401 dm_block_t block = get_bio_block(tc, bio); 1402 struct dm_thin_lookup_result lookup_result; 1403 1404 r = dm_thin_find_block(tc->td, block, 1, &lookup_result); 1405 switch (r) { 1406 case 0: 1407 if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) 1408 handle_unserviceable_bio(tc->pool, bio); 1409 else { 1410 inc_all_io_entry(tc->pool, bio); 1411 remap_and_issue(tc, bio, lookup_result.block); 1412 } 1413 break; 1414 1415 case -ENODATA: 1416 if (rw != READ) { 1417 handle_unserviceable_bio(tc->pool, bio); 1418 break; 1419 } 1420 1421 if (tc->origin_dev) { 1422 inc_all_io_entry(tc->pool, bio); 1423 remap_to_origin_and_issue(tc, bio); 1424 break; 1425 } 1426 1427 zero_fill_bio(bio); 1428 bio_endio(bio, 0); 1429 break; 1430 1431 default: 1432 DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d", 1433 __func__, r); 1434 bio_io_error(bio); 1435 break; 1436 } 1437} 1438 1439static void process_bio_success(struct thin_c *tc, struct bio *bio) 1440{ 1441 bio_endio(bio, 0); 1442} 1443 1444static void process_bio_fail(struct thin_c *tc, struct bio *bio) 1445{ 1446 bio_io_error(bio); 1447} 1448 1449/* 1450 * FIXME: should we also commit due to size of transaction, measured in 1451 * metadata blocks? 1452 */ 1453static int need_commit_due_to_time(struct pool *pool) 1454{ 1455 return jiffies < pool->last_commit_jiffies || 1456 jiffies > pool->last_commit_jiffies + COMMIT_PERIOD; 1457} 1458 1459#define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node) 1460#define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook)) 1461 1462static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio) 1463{ 1464 struct rb_node **rbp, *parent; 1465 struct dm_thin_endio_hook *pbd; 1466 sector_t bi_sector = bio->bi_iter.bi_sector; 1467 1468 rbp = &tc->sort_bio_list.rb_node; 1469 parent = NULL; 1470 while (*rbp) { 1471 parent = *rbp; 1472 pbd = thin_pbd(parent); 1473 1474 if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector) 1475 rbp = &(*rbp)->rb_left; 1476 else 1477 rbp = &(*rbp)->rb_right; 1478 } 1479 1480 pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 1481 rb_link_node(&pbd->rb_node, parent, rbp); 1482 rb_insert_color(&pbd->rb_node, &tc->sort_bio_list); 1483} 1484 1485static void __extract_sorted_bios(struct thin_c *tc) 1486{ 1487 struct rb_node *node; 1488 struct dm_thin_endio_hook *pbd; 1489 struct bio *bio; 1490 1491 for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) { 1492 pbd = thin_pbd(node); 1493 bio = thin_bio(pbd); 1494 1495 bio_list_add(&tc->deferred_bio_list, bio); 1496 rb_erase(&pbd->rb_node, &tc->sort_bio_list); 1497 } 1498 1499 WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list)); 1500} 1501 1502static void __sort_thin_deferred_bios(struct thin_c *tc) 1503{ 1504 struct bio *bio; 1505 struct bio_list bios; 1506 1507 bio_list_init(&bios); 1508 bio_list_merge(&bios, &tc->deferred_bio_list); 1509 bio_list_init(&tc->deferred_bio_list); 1510 1511 /* Sort deferred_bio_list using rb-tree */ 1512 while ((bio = bio_list_pop(&bios))) 1513 __thin_bio_rb_add(tc, bio); 1514 1515 /* 1516 * Transfer the sorted bios in sort_bio_list back to 1517 * deferred_bio_list to allow lockless submission of 1518 * all bios. 1519 */ 1520 __extract_sorted_bios(tc); 1521} 1522 1523static void process_thin_deferred_bios(struct thin_c *tc) 1524{ 1525 struct pool *pool = tc->pool; 1526 unsigned long flags; 1527 struct bio *bio; 1528 struct bio_list bios; 1529 struct blk_plug plug; 1530 1531 if (tc->requeue_mode) { 1532 requeue_bio_list(tc, &tc->deferred_bio_list); 1533 return; 1534 } 1535 1536 bio_list_init(&bios); 1537 1538 spin_lock_irqsave(&tc->lock, flags); 1539 1540 if (bio_list_empty(&tc->deferred_bio_list)) { 1541 spin_unlock_irqrestore(&tc->lock, flags); 1542 return; 1543 } 1544 1545 __sort_thin_deferred_bios(tc); 1546 1547 bio_list_merge(&bios, &tc->deferred_bio_list); 1548 bio_list_init(&tc->deferred_bio_list); 1549 1550 spin_unlock_irqrestore(&tc->lock, flags); 1551 1552 blk_start_plug(&plug); 1553 while ((bio = bio_list_pop(&bios))) { 1554 /* 1555 * If we've got no free new_mapping structs, and processing 1556 * this bio might require one, we pause until there are some 1557 * prepared mappings to process. 1558 */ 1559 if (ensure_next_mapping(pool)) { 1560 spin_lock_irqsave(&tc->lock, flags); 1561 bio_list_add(&tc->deferred_bio_list, bio); 1562 bio_list_merge(&tc->deferred_bio_list, &bios); 1563 spin_unlock_irqrestore(&tc->lock, flags); 1564 break; 1565 } 1566 1567 if (bio->bi_rw & REQ_DISCARD) 1568 pool->process_discard(tc, bio); 1569 else 1570 pool->process_bio(tc, bio); 1571 } 1572 blk_finish_plug(&plug); 1573} 1574 1575static void thin_get(struct thin_c *tc); 1576static void thin_put(struct thin_c *tc); 1577 1578/* 1579 * We can't hold rcu_read_lock() around code that can block. So we 1580 * find a thin with the rcu lock held; bump a refcount; then drop 1581 * the lock. 1582 */ 1583static struct thin_c *get_first_thin(struct pool *pool) 1584{ 1585 struct thin_c *tc = NULL; 1586 1587 rcu_read_lock(); 1588 if (!list_empty(&pool->active_thins)) { 1589 tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list); 1590 thin_get(tc); 1591 } 1592 rcu_read_unlock(); 1593 1594 return tc; 1595} 1596 1597static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc) 1598{ 1599 struct thin_c *old_tc = tc; 1600 1601 rcu_read_lock(); 1602 list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) { 1603 thin_get(tc); 1604 thin_put(old_tc); 1605 rcu_read_unlock(); 1606 return tc; 1607 } 1608 thin_put(old_tc); 1609 rcu_read_unlock(); 1610 1611 return NULL; 1612} 1613 1614static void process_deferred_bios(struct pool *pool) 1615{ 1616 unsigned long flags; 1617 struct bio *bio; 1618 struct bio_list bios; 1619 struct thin_c *tc; 1620 1621 tc = get_first_thin(pool); 1622 while (tc) { 1623 process_thin_deferred_bios(tc); 1624 tc = get_next_thin(pool, tc); 1625 } 1626 1627 /* 1628 * If there are any deferred flush bios, we must commit 1629 * the metadata before issuing them. 1630 */ 1631 bio_list_init(&bios); 1632 spin_lock_irqsave(&pool->lock, flags); 1633 bio_list_merge(&bios, &pool->deferred_flush_bios); 1634 bio_list_init(&pool->deferred_flush_bios); 1635 spin_unlock_irqrestore(&pool->lock, flags); 1636 1637 if (bio_list_empty(&bios) && 1638 !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool))) 1639 return; 1640 1641 if (commit(pool)) { 1642 while ((bio = bio_list_pop(&bios))) 1643 bio_io_error(bio); 1644 return; 1645 } 1646 pool->last_commit_jiffies = jiffies; 1647 1648 while ((bio = bio_list_pop(&bios))) 1649 generic_make_request(bio); 1650} 1651 1652static void do_worker(struct work_struct *ws) 1653{ 1654 struct pool *pool = container_of(ws, struct pool, worker); 1655 1656 process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping); 1657 process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard); 1658 process_deferred_bios(pool); 1659} 1660 1661/* 1662 * We want to commit periodically so that not too much 1663 * unwritten data builds up. 1664 */ 1665static void do_waker(struct work_struct *ws) 1666{ 1667 struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker); 1668 wake_worker(pool); 1669 queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD); 1670} 1671 1672/* 1673 * We're holding onto IO to allow userland time to react. After the 1674 * timeout either the pool will have been resized (and thus back in 1675 * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO. 1676 */ 1677static void do_no_space_timeout(struct work_struct *ws) 1678{ 1679 struct pool *pool = container_of(to_delayed_work(ws), struct pool, 1680 no_space_timeout); 1681 1682 if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) 1683 set_pool_mode(pool, PM_READ_ONLY); 1684} 1685 1686/*----------------------------------------------------------------*/ 1687 1688struct pool_work { 1689 struct work_struct worker; 1690 struct completion complete; 1691}; 1692 1693static struct pool_work *to_pool_work(struct work_struct *ws) 1694{ 1695 return container_of(ws, struct pool_work, worker); 1696} 1697 1698static void pool_work_complete(struct pool_work *pw) 1699{ 1700 complete(&pw->complete); 1701} 1702 1703static void pool_work_wait(struct pool_work *pw, struct pool *pool, 1704 void (*fn)(struct work_struct *)) 1705{ 1706 INIT_WORK_ONSTACK(&pw->worker, fn); 1707 init_completion(&pw->complete); 1708 queue_work(pool->wq, &pw->worker); 1709 wait_for_completion(&pw->complete); 1710} 1711 1712/*----------------------------------------------------------------*/ 1713 1714struct noflush_work { 1715 struct pool_work pw; 1716 struct thin_c *tc; 1717}; 1718 1719static struct noflush_work *to_noflush(struct work_struct *ws) 1720{ 1721 return container_of(to_pool_work(ws), struct noflush_work, pw); 1722} 1723 1724static void do_noflush_start(struct work_struct *ws) 1725{ 1726 struct noflush_work *w = to_noflush(ws); 1727 w->tc->requeue_mode = true; 1728 requeue_io(w->tc); 1729 pool_work_complete(&w->pw); 1730} 1731 1732static void do_noflush_stop(struct work_struct *ws) 1733{ 1734 struct noflush_work *w = to_noflush(ws); 1735 w->tc->requeue_mode = false; 1736 pool_work_complete(&w->pw); 1737} 1738 1739static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *)) 1740{ 1741 struct noflush_work w; 1742 1743 w.tc = tc; 1744 pool_work_wait(&w.pw, tc->pool, fn); 1745} 1746 1747/*----------------------------------------------------------------*/ 1748 1749static enum pool_mode get_pool_mode(struct pool *pool) 1750{ 1751 return pool->pf.mode; 1752} 1753 1754static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode) 1755{ 1756 dm_table_event(pool->ti->table); 1757 DMINFO("%s: switching pool to %s mode", 1758 dm_device_name(pool->pool_md), new_mode); 1759} 1760 1761static void set_pool_mode(struct pool *pool, enum pool_mode new_mode) 1762{ 1763 struct pool_c *pt = pool->ti->private; 1764 bool needs_check = dm_pool_metadata_needs_check(pool->pmd); 1765 enum pool_mode old_mode = get_pool_mode(pool); 1766 unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ; 1767 1768 /* 1769 * Never allow the pool to transition to PM_WRITE mode if user 1770 * intervention is required to verify metadata and data consistency. 1771 */ 1772 if (new_mode == PM_WRITE && needs_check) { 1773 DMERR("%s: unable to switch pool to write mode until repaired.", 1774 dm_device_name(pool->pool_md)); 1775 if (old_mode != new_mode) 1776 new_mode = old_mode; 1777 else 1778 new_mode = PM_READ_ONLY; 1779 } 1780 /* 1781 * If we were in PM_FAIL mode, rollback of metadata failed. We're 1782 * not going to recover without a thin_repair. So we never let the 1783 * pool move out of the old mode. 1784 */ 1785 if (old_mode == PM_FAIL) 1786 new_mode = old_mode; 1787 1788 switch (new_mode) { 1789 case PM_FAIL: 1790 if (old_mode != new_mode) 1791 notify_of_pool_mode_change(pool, "failure"); 1792 dm_pool_metadata_read_only(pool->pmd); 1793 pool->process_bio = process_bio_fail; 1794 pool->process_discard = process_bio_fail; 1795 pool->process_prepared_mapping = process_prepared_mapping_fail; 1796 pool->process_prepared_discard = process_prepared_discard_fail; 1797 1798 error_retry_list(pool); 1799 break; 1800 1801 case PM_READ_ONLY: 1802 if (old_mode != new_mode) 1803 notify_of_pool_mode_change(pool, "read-only"); 1804 dm_pool_metadata_read_only(pool->pmd); 1805 pool->process_bio = process_bio_read_only; 1806 pool->process_discard = process_bio_success; 1807 pool->process_prepared_mapping = process_prepared_mapping_fail; 1808 pool->process_prepared_discard = process_prepared_discard_passdown; 1809 1810 error_retry_list(pool); 1811 break; 1812 1813 case PM_OUT_OF_DATA_SPACE: 1814 /* 1815 * Ideally we'd never hit this state; the low water mark 1816 * would trigger userland to extend the pool before we 1817 * completely run out of data space. However, many small 1818 * IOs to unprovisioned space can consume data space at an 1819 * alarming rate. Adjust your low water mark if you're 1820 * frequently seeing this mode. 1821 */ 1822 if (old_mode != new_mode) 1823 notify_of_pool_mode_change(pool, "out-of-data-space"); 1824 pool->process_bio = process_bio_read_only; 1825 pool->process_discard = process_discard; 1826 pool->process_prepared_mapping = process_prepared_mapping; 1827 pool->process_prepared_discard = process_prepared_discard_passdown; 1828 1829 if (!pool->pf.error_if_no_space && no_space_timeout) 1830 queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout); 1831 break; 1832 1833 case PM_WRITE: 1834 if (old_mode != new_mode) 1835 notify_of_pool_mode_change(pool, "write"); 1836 dm_pool_metadata_read_write(pool->pmd); 1837 pool->process_bio = process_bio; 1838 pool->process_discard = process_discard; 1839 pool->process_prepared_mapping = process_prepared_mapping; 1840 pool->process_prepared_discard = process_prepared_discard; 1841 break; 1842 } 1843 1844 pool->pf.mode = new_mode; 1845 /* 1846 * The pool mode may have changed, sync it so bind_control_target() 1847 * doesn't cause an unexpected mode transition on resume. 1848 */ 1849 pt->adjusted_pf.mode = new_mode; 1850} 1851 1852static void abort_transaction(struct pool *pool) 1853{ 1854 const char *dev_name = dm_device_name(pool->pool_md); 1855 1856 DMERR_LIMIT("%s: aborting current metadata transaction", dev_name); 1857 if (dm_pool_abort_metadata(pool->pmd)) { 1858 DMERR("%s: failed to abort metadata transaction", dev_name); 1859 set_pool_mode(pool, PM_FAIL); 1860 } 1861 1862 if (dm_pool_metadata_set_needs_check(pool->pmd)) { 1863 DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name); 1864 set_pool_mode(pool, PM_FAIL); 1865 } 1866} 1867 1868static void metadata_operation_failed(struct pool *pool, const char *op, int r) 1869{ 1870 DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d", 1871 dm_device_name(pool->pool_md), op, r); 1872 1873 abort_transaction(pool); 1874 set_pool_mode(pool, PM_READ_ONLY); 1875} 1876 1877/*----------------------------------------------------------------*/ 1878 1879/* 1880 * Mapping functions. 1881 */ 1882 1883/* 1884 * Called only while mapping a thin bio to hand it over to the workqueue. 1885 */ 1886static void thin_defer_bio(struct thin_c *tc, struct bio *bio) 1887{ 1888 unsigned long flags; 1889 struct pool *pool = tc->pool; 1890 1891 spin_lock_irqsave(&tc->lock, flags); 1892 bio_list_add(&tc->deferred_bio_list, bio); 1893 spin_unlock_irqrestore(&tc->lock, flags); 1894 1895 wake_worker(pool); 1896} 1897 1898static void thin_hook_bio(struct thin_c *tc, struct bio *bio) 1899{ 1900 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 1901 1902 h->tc = tc; 1903 h->shared_read_entry = NULL; 1904 h->all_io_entry = NULL; 1905 h->overwrite_mapping = NULL; 1906} 1907 1908/* 1909 * Non-blocking function called from the thin target's map function. 1910 */ 1911static int thin_bio_map(struct dm_target *ti, struct bio *bio) 1912{ 1913 int r; 1914 struct thin_c *tc = ti->private; 1915 dm_block_t block = get_bio_block(tc, bio); 1916 struct dm_thin_device *td = tc->td; 1917 struct dm_thin_lookup_result result; 1918 struct dm_bio_prison_cell cell1, cell2; 1919 struct dm_bio_prison_cell *cell_result; 1920 struct dm_cell_key key; 1921 1922 thin_hook_bio(tc, bio); 1923 1924 if (tc->requeue_mode) { 1925 bio_endio(bio, DM_ENDIO_REQUEUE); 1926 return DM_MAPIO_SUBMITTED; 1927 } 1928 1929 if (get_pool_mode(tc->pool) == PM_FAIL) { 1930 bio_io_error(bio); 1931 return DM_MAPIO_SUBMITTED; 1932 } 1933 1934 if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) { 1935 thin_defer_bio(tc, bio); 1936 return DM_MAPIO_SUBMITTED; 1937 } 1938 1939 /* 1940 * We must hold the virtual cell before doing the lookup, otherwise 1941 * there's a race with discard. 1942 */ 1943 build_virtual_key(tc->td, block, &key); 1944 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result)) 1945 return DM_MAPIO_SUBMITTED; 1946 1947 r = dm_thin_find_block(td, block, 0, &result); 1948 1949 /* 1950 * Note that we defer readahead too. 1951 */ 1952 switch (r) { 1953 case 0: 1954 if (unlikely(result.shared)) { 1955 /* 1956 * We have a race condition here between the 1957 * result.shared value returned by the lookup and 1958 * snapshot creation, which may cause new 1959 * sharing. 1960 * 1961 * To avoid this always quiesce the origin before 1962 * taking the snap. You want to do this anyway to 1963 * ensure a consistent application view 1964 * (i.e. lockfs). 1965 * 1966 * More distant ancestors are irrelevant. The 1967 * shared flag will be set in their case. 1968 */ 1969 thin_defer_bio(tc, bio); 1970 cell_defer_no_holder_no_free(tc, &cell1); 1971 return DM_MAPIO_SUBMITTED; 1972 } 1973 1974 build_data_key(tc->td, result.block, &key); 1975 if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) { 1976 cell_defer_no_holder_no_free(tc, &cell1); 1977 return DM_MAPIO_SUBMITTED; 1978 } 1979 1980 inc_all_io_entry(tc->pool, bio); 1981 cell_defer_no_holder_no_free(tc, &cell2); 1982 cell_defer_no_holder_no_free(tc, &cell1); 1983 1984 remap(tc, bio, result.block); 1985 return DM_MAPIO_REMAPPED; 1986 1987 case -ENODATA: 1988 if (get_pool_mode(tc->pool) == PM_READ_ONLY) { 1989 /* 1990 * This block isn't provisioned, and we have no way 1991 * of doing so. 1992 */ 1993 handle_unserviceable_bio(tc->pool, bio); 1994 cell_defer_no_holder_no_free(tc, &cell1); 1995 return DM_MAPIO_SUBMITTED; 1996 } 1997 /* fall through */ 1998 1999 case -EWOULDBLOCK: 2000 /* 2001 * In future, the failed dm_thin_find_block above could 2002 * provide the hint to load the metadata into cache. 2003 */ 2004 thin_defer_bio(tc, bio); 2005 cell_defer_no_holder_no_free(tc, &cell1); 2006 return DM_MAPIO_SUBMITTED; 2007 2008 default: 2009 /* 2010 * Must always call bio_io_error on failure. 2011 * dm_thin_find_block can fail with -EINVAL if the 2012 * pool is switched to fail-io mode. 2013 */ 2014 bio_io_error(bio); 2015 cell_defer_no_holder_no_free(tc, &cell1); 2016 return DM_MAPIO_SUBMITTED; 2017 } 2018} 2019 2020static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits) 2021{ 2022 struct pool_c *pt = container_of(cb, struct pool_c, callbacks); 2023 struct request_queue *q; 2024 2025 if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE) 2026 return 1; 2027 2028 q = bdev_get_queue(pt->data_dev->bdev); 2029 return bdi_congested(&q->backing_dev_info, bdi_bits); 2030} 2031 2032static void requeue_bios(struct pool *pool) 2033{ 2034 unsigned long flags; 2035 struct thin_c *tc; 2036 2037 rcu_read_lock(); 2038 list_for_each_entry_rcu(tc, &pool->active_thins, list) { 2039 spin_lock_irqsave(&tc->lock, flags); 2040 bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list); 2041 bio_list_init(&tc->retry_on_resume_list); 2042 spin_unlock_irqrestore(&tc->lock, flags); 2043 } 2044 rcu_read_unlock(); 2045} 2046 2047/*---------------------------------------------------------------- 2048 * Binding of control targets to a pool object 2049 *--------------------------------------------------------------*/ 2050static bool data_dev_supports_discard(struct pool_c *pt) 2051{ 2052 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); 2053 2054 return q && blk_queue_discard(q); 2055} 2056 2057static bool is_factor(sector_t block_size, uint32_t n) 2058{ 2059 return !sector_div(block_size, n); 2060} 2061 2062/* 2063 * If discard_passdown was enabled verify that the data device 2064 * supports discards. Disable discard_passdown if not. 2065 */ 2066static void disable_passdown_if_not_supported(struct pool_c *pt) 2067{ 2068 struct pool *pool = pt->pool; 2069 struct block_device *data_bdev = pt->data_dev->bdev; 2070 struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits; 2071 sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT; 2072 const char *reason = NULL; 2073 char buf[BDEVNAME_SIZE]; 2074 2075 if (!pt->adjusted_pf.discard_passdown) 2076 return; 2077 2078 if (!data_dev_supports_discard(pt)) 2079 reason = "discard unsupported"; 2080 2081 else if (data_limits->max_discard_sectors < pool->sectors_per_block) 2082 reason = "max discard sectors smaller than a block"; 2083 2084 else if (data_limits->discard_granularity > block_size) 2085 reason = "discard granularity larger than a block"; 2086 2087 else if (!is_factor(block_size, data_limits->discard_granularity)) 2088 reason = "discard granularity not a factor of block size"; 2089 2090 if (reason) { 2091 DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason); 2092 pt->adjusted_pf.discard_passdown = false; 2093 } 2094} 2095 2096static int bind_control_target(struct pool *pool, struct dm_target *ti) 2097{ 2098 struct pool_c *pt = ti->private; 2099 2100 /* 2101 * We want to make sure that a pool in PM_FAIL mode is never upgraded. 2102 */ 2103 enum pool_mode old_mode = get_pool_mode(pool); 2104 enum pool_mode new_mode = pt->adjusted_pf.mode; 2105 2106 /* 2107 * Don't change the pool's mode until set_pool_mode() below. 2108 * Otherwise the pool's process_* function pointers may 2109 * not match the desired pool mode. 2110 */ 2111 pt->adjusted_pf.mode = old_mode; 2112 2113 pool->ti = ti; 2114 pool->pf = pt->adjusted_pf; 2115 pool->low_water_blocks = pt->low_water_blocks; 2116 2117 set_pool_mode(pool, new_mode); 2118 2119 return 0; 2120} 2121 2122static void unbind_control_target(struct pool *pool, struct dm_target *ti) 2123{ 2124 if (pool->ti == ti) 2125 pool->ti = NULL; 2126} 2127 2128/*---------------------------------------------------------------- 2129 * Pool creation 2130 *--------------------------------------------------------------*/ 2131/* Initialize pool features. */ 2132static void pool_features_init(struct pool_features *pf) 2133{ 2134 pf->mode = PM_WRITE; 2135 pf->zero_new_blocks = true; 2136 pf->discard_enabled = true; 2137 pf->discard_passdown = true; 2138 pf->error_if_no_space = false; 2139} 2140 2141static void __pool_destroy(struct pool *pool) 2142{ 2143 __pool_table_remove(pool); 2144 2145 if (dm_pool_metadata_close(pool->pmd) < 0) 2146 DMWARN("%s: dm_pool_metadata_close() failed.", __func__); 2147 2148 dm_bio_prison_destroy(pool->prison); 2149 dm_kcopyd_client_destroy(pool->copier); 2150 2151 if (pool->wq) 2152 destroy_workqueue(pool->wq); 2153 2154 if (pool->next_mapping) 2155 mempool_free(pool->next_mapping, pool->mapping_pool); 2156 mempool_destroy(pool->mapping_pool); 2157 dm_deferred_set_destroy(pool->shared_read_ds); 2158 dm_deferred_set_destroy(pool->all_io_ds); 2159 kfree(pool); 2160} 2161 2162static struct kmem_cache *_new_mapping_cache; 2163 2164static struct pool *pool_create(struct mapped_device *pool_md, 2165 struct block_device *metadata_dev, 2166 unsigned long block_size, 2167 int read_only, char **error) 2168{ 2169 int r; 2170 void *err_p; 2171 struct pool *pool; 2172 struct dm_pool_metadata *pmd; 2173 bool format_device = read_only ? false : true; 2174 2175 pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device); 2176 if (IS_ERR(pmd)) { 2177 *error = "Error creating metadata object"; 2178 return (struct pool *)pmd; 2179 } 2180 2181 pool = kmalloc(sizeof(*pool), GFP_KERNEL); 2182 if (!pool) { 2183 *error = "Error allocating memory for pool"; 2184 err_p = ERR_PTR(-ENOMEM); 2185 goto bad_pool; 2186 } 2187 2188 pool->pmd = pmd; 2189 pool->sectors_per_block = block_size; 2190 if (block_size & (block_size - 1)) 2191 pool->sectors_per_block_shift = -1; 2192 else 2193 pool->sectors_per_block_shift = __ffs(block_size); 2194 pool->low_water_blocks = 0; 2195 pool_features_init(&pool->pf); 2196 pool->prison = dm_bio_prison_create(PRISON_CELLS); 2197 if (!pool->prison) { 2198 *error = "Error creating pool's bio prison"; 2199 err_p = ERR_PTR(-ENOMEM); 2200 goto bad_prison; 2201 } 2202 2203 pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle); 2204 if (IS_ERR(pool->copier)) { 2205 r = PTR_ERR(pool->copier); 2206 *error = "Error creating pool's kcopyd client"; 2207 err_p = ERR_PTR(r); 2208 goto bad_kcopyd_client; 2209 } 2210 2211 /* 2212 * Create singlethreaded workqueue that will service all devices 2213 * that use this metadata. 2214 */ 2215 pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM); 2216 if (!pool->wq) { 2217 *error = "Error creating pool's workqueue"; 2218 err_p = ERR_PTR(-ENOMEM); 2219 goto bad_wq; 2220 } 2221 2222 INIT_WORK(&pool->worker, do_worker); 2223 INIT_DELAYED_WORK(&pool->waker, do_waker); 2224 INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout); 2225 spin_lock_init(&pool->lock); 2226 bio_list_init(&pool->deferred_flush_bios); 2227 INIT_LIST_HEAD(&pool->prepared_mappings); 2228 INIT_LIST_HEAD(&pool->prepared_discards); 2229 INIT_LIST_HEAD(&pool->active_thins); 2230 pool->low_water_triggered = false; 2231 2232 pool->shared_read_ds = dm_deferred_set_create(); 2233 if (!pool->shared_read_ds) { 2234 *error = "Error creating pool's shared read deferred set"; 2235 err_p = ERR_PTR(-ENOMEM); 2236 goto bad_shared_read_ds; 2237 } 2238 2239 pool->all_io_ds = dm_deferred_set_create(); 2240 if (!pool->all_io_ds) { 2241 *error = "Error creating pool's all io deferred set"; 2242 err_p = ERR_PTR(-ENOMEM); 2243 goto bad_all_io_ds; 2244 } 2245 2246 pool->next_mapping = NULL; 2247 pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE, 2248 _new_mapping_cache); 2249 if (!pool->mapping_pool) { 2250 *error = "Error creating pool's mapping mempool"; 2251 err_p = ERR_PTR(-ENOMEM); 2252 goto bad_mapping_pool; 2253 } 2254 2255 pool->ref_count = 1; 2256 pool->last_commit_jiffies = jiffies; 2257 pool->pool_md = pool_md; 2258 pool->md_dev = metadata_dev; 2259 __pool_table_insert(pool); 2260 2261 return pool; 2262 2263bad_mapping_pool: 2264 dm_deferred_set_destroy(pool->all_io_ds); 2265bad_all_io_ds: 2266 dm_deferred_set_destroy(pool->shared_read_ds); 2267bad_shared_read_ds: 2268 destroy_workqueue(pool->wq); 2269bad_wq: 2270 dm_kcopyd_client_destroy(pool->copier); 2271bad_kcopyd_client: 2272 dm_bio_prison_destroy(pool->prison); 2273bad_prison: 2274 kfree(pool); 2275bad_pool: 2276 if (dm_pool_metadata_close(pmd)) 2277 DMWARN("%s: dm_pool_metadata_close() failed.", __func__); 2278 2279 return err_p; 2280} 2281 2282static void __pool_inc(struct pool *pool) 2283{ 2284 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 2285 pool->ref_count++; 2286} 2287 2288static void __pool_dec(struct pool *pool) 2289{ 2290 BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex)); 2291 BUG_ON(!pool->ref_count); 2292 if (!--pool->ref_count) 2293 __pool_destroy(pool); 2294} 2295 2296static struct pool *__pool_find(struct mapped_device *pool_md, 2297 struct block_device *metadata_dev, 2298 unsigned long block_size, int read_only, 2299 char **error, int *created) 2300{ 2301 struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev); 2302 2303 if (pool) { 2304 if (pool->pool_md != pool_md) { 2305 *error = "metadata device already in use by a pool"; 2306 return ERR_PTR(-EBUSY); 2307 } 2308 __pool_inc(pool); 2309 2310 } else { 2311 pool = __pool_table_lookup(pool_md); 2312 if (pool) { 2313 if (pool->md_dev != metadata_dev) { 2314 *error = "different pool cannot replace a pool"; 2315 return ERR_PTR(-EINVAL); 2316 } 2317 __pool_inc(pool); 2318 2319 } else { 2320 pool = pool_create(pool_md, metadata_dev, block_size, read_only, error); 2321 *created = 1; 2322 } 2323 } 2324 2325 return pool; 2326} 2327 2328/*---------------------------------------------------------------- 2329 * Pool target methods 2330 *--------------------------------------------------------------*/ 2331static void pool_dtr(struct dm_target *ti) 2332{ 2333 struct pool_c *pt = ti->private; 2334 2335 mutex_lock(&dm_thin_pool_table.mutex); 2336 2337 unbind_control_target(pt->pool, ti); 2338 __pool_dec(pt->pool); 2339 dm_put_device(ti, pt->metadata_dev); 2340 dm_put_device(ti, pt->data_dev); 2341 kfree(pt); 2342 2343 mutex_unlock(&dm_thin_pool_table.mutex); 2344} 2345 2346static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf, 2347 struct dm_target *ti) 2348{ 2349 int r; 2350 unsigned argc; 2351 const char *arg_name; 2352 2353 static struct dm_arg _args[] = { 2354 {0, 4, "Invalid number of pool feature arguments"}, 2355 }; 2356 2357 /* 2358 * No feature arguments supplied. 2359 */ 2360 if (!as->argc) 2361 return 0; 2362 2363 r = dm_read_arg_group(_args, as, &argc, &ti->error); 2364 if (r) 2365 return -EINVAL; 2366 2367 while (argc && !r) { 2368 arg_name = dm_shift_arg(as); 2369 argc--; 2370 2371 if (!strcasecmp(arg_name, "skip_block_zeroing")) 2372 pf->zero_new_blocks = false; 2373 2374 else if (!strcasecmp(arg_name, "ignore_discard")) 2375 pf->discard_enabled = false; 2376 2377 else if (!strcasecmp(arg_name, "no_discard_passdown")) 2378 pf->discard_passdown = false; 2379 2380 else if (!strcasecmp(arg_name, "read_only")) 2381 pf->mode = PM_READ_ONLY; 2382 2383 else if (!strcasecmp(arg_name, "error_if_no_space")) 2384 pf->error_if_no_space = true; 2385 2386 else { 2387 ti->error = "Unrecognised pool feature requested"; 2388 r = -EINVAL; 2389 break; 2390 } 2391 } 2392 2393 return r; 2394} 2395 2396static void metadata_low_callback(void *context) 2397{ 2398 struct pool *pool = context; 2399 2400 DMWARN("%s: reached low water mark for metadata device: sending event.", 2401 dm_device_name(pool->pool_md)); 2402 2403 dm_table_event(pool->ti->table); 2404} 2405 2406static sector_t get_dev_size(struct block_device *bdev) 2407{ 2408 return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT; 2409} 2410 2411static void warn_if_metadata_device_too_big(struct block_device *bdev) 2412{ 2413 sector_t metadata_dev_size = get_dev_size(bdev); 2414 char buffer[BDEVNAME_SIZE]; 2415 2416 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING) 2417 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.", 2418 bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS); 2419} 2420 2421static sector_t get_metadata_dev_size(struct block_device *bdev) 2422{ 2423 sector_t metadata_dev_size = get_dev_size(bdev); 2424 2425 if (metadata_dev_size > THIN_METADATA_MAX_SECTORS) 2426 metadata_dev_size = THIN_METADATA_MAX_SECTORS; 2427 2428 return metadata_dev_size; 2429} 2430 2431static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev) 2432{ 2433 sector_t metadata_dev_size = get_metadata_dev_size(bdev); 2434 2435 sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE); 2436 2437 return metadata_dev_size; 2438} 2439 2440/* 2441 * When a metadata threshold is crossed a dm event is triggered, and 2442 * userland should respond by growing the metadata device. We could let 2443 * userland set the threshold, like we do with the data threshold, but I'm 2444 * not sure they know enough to do this well. 2445 */ 2446static dm_block_t calc_metadata_threshold(struct pool_c *pt) 2447{ 2448 /* 2449 * 4M is ample for all ops with the possible exception of thin 2450 * device deletion which is harmless if it fails (just retry the 2451 * delete after you've grown the device). 2452 */ 2453 dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4; 2454 return min((dm_block_t)1024ULL /* 4M */, quarter); 2455} 2456 2457/* 2458 * thin-pool <metadata dev> <data dev> 2459 * <data block size (sectors)> 2460 * <low water mark (blocks)> 2461 * [<#feature args> [<arg>]*] 2462 * 2463 * Optional feature arguments are: 2464 * skip_block_zeroing: skips the zeroing of newly-provisioned blocks. 2465 * ignore_discard: disable discard 2466 * no_discard_passdown: don't pass discards down to the data device 2467 * read_only: Don't allow any changes to be made to the pool metadata. 2468 * error_if_no_space: error IOs, instead of queueing, if no space. 2469 */ 2470static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv) 2471{ 2472 int r, pool_created = 0; 2473 struct pool_c *pt; 2474 struct pool *pool; 2475 struct pool_features pf; 2476 struct dm_arg_set as; 2477 struct dm_dev *data_dev; 2478 unsigned long block_size; 2479 dm_block_t low_water_blocks; 2480 struct dm_dev *metadata_dev; 2481 fmode_t metadata_mode; 2482 2483 /* 2484 * FIXME Remove validation from scope of lock. 2485 */ 2486 mutex_lock(&dm_thin_pool_table.mutex); 2487 2488 if (argc < 4) { 2489 ti->error = "Invalid argument count"; 2490 r = -EINVAL; 2491 goto out_unlock; 2492 } 2493 2494 as.argc = argc; 2495 as.argv = argv; 2496 2497 /* 2498 * Set default pool features. 2499 */ 2500 pool_features_init(&pf); 2501 2502 dm_consume_args(&as, 4); 2503 r = parse_pool_features(&as, &pf, ti); 2504 if (r) 2505 goto out_unlock; 2506 2507 metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE); 2508 r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev); 2509 if (r) { 2510 ti->error = "Error opening metadata block device"; 2511 goto out_unlock; 2512 } 2513 warn_if_metadata_device_too_big(metadata_dev->bdev); 2514 2515 r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev); 2516 if (r) { 2517 ti->error = "Error getting data device"; 2518 goto out_metadata; 2519 } 2520 2521 if (kstrtoul(argv[2], 10, &block_size) || !block_size || 2522 block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS || 2523 block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS || 2524 block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) { 2525 ti->error = "Invalid block size"; 2526 r = -EINVAL; 2527 goto out; 2528 } 2529 2530 if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) { 2531 ti->error = "Invalid low water mark"; 2532 r = -EINVAL; 2533 goto out; 2534 } 2535 2536 pt = kzalloc(sizeof(*pt), GFP_KERNEL); 2537 if (!pt) { 2538 r = -ENOMEM; 2539 goto out; 2540 } 2541 2542 pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, 2543 block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created); 2544 if (IS_ERR(pool)) { 2545 r = PTR_ERR(pool); 2546 goto out_free_pt; 2547 } 2548 2549 /* 2550 * 'pool_created' reflects whether this is the first table load. 2551 * Top level discard support is not allowed to be changed after 2552 * initial load. This would require a pool reload to trigger thin 2553 * device changes. 2554 */ 2555 if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) { 2556 ti->error = "Discard support cannot be disabled once enabled"; 2557 r = -EINVAL; 2558 goto out_flags_changed; 2559 } 2560 2561 pt->pool = pool; 2562 pt->ti = ti; 2563 pt->metadata_dev = metadata_dev; 2564 pt->data_dev = data_dev; 2565 pt->low_water_blocks = low_water_blocks; 2566 pt->adjusted_pf = pt->requested_pf = pf; 2567 ti->num_flush_bios = 1; 2568 2569 /* 2570 * Only need to enable discards if the pool should pass 2571 * them down to the data device. The thin device's discard 2572 * processing will cause mappings to be removed from the btree. 2573 */ 2574 ti->discard_zeroes_data_unsupported = true; 2575 if (pf.discard_enabled && pf.discard_passdown) { 2576 ti->num_discard_bios = 1; 2577 2578 /* 2579 * Setting 'discards_supported' circumvents the normal 2580 * stacking of discard limits (this keeps the pool and 2581 * thin devices' discard limits consistent). 2582 */ 2583 ti->discards_supported = true; 2584 } 2585 ti->private = pt; 2586 2587 r = dm_pool_register_metadata_threshold(pt->pool->pmd, 2588 calc_metadata_threshold(pt), 2589 metadata_low_callback, 2590 pool); 2591 if (r) 2592 goto out_free_pt; 2593 2594 pt->callbacks.congested_fn = pool_is_congested; 2595 dm_table_add_target_callbacks(ti->table, &pt->callbacks); 2596 2597 mutex_unlock(&dm_thin_pool_table.mutex); 2598 2599 return 0; 2600 2601out_flags_changed: 2602 __pool_dec(pool); 2603out_free_pt: 2604 kfree(pt); 2605out: 2606 dm_put_device(ti, data_dev); 2607out_metadata: 2608 dm_put_device(ti, metadata_dev); 2609out_unlock: 2610 mutex_unlock(&dm_thin_pool_table.mutex); 2611 2612 return r; 2613} 2614 2615static int pool_map(struct dm_target *ti, struct bio *bio) 2616{ 2617 int r; 2618 struct pool_c *pt = ti->private; 2619 struct pool *pool = pt->pool; 2620 unsigned long flags; 2621 2622 /* 2623 * As this is a singleton target, ti->begin is always zero. 2624 */ 2625 spin_lock_irqsave(&pool->lock, flags); 2626 bio->bi_bdev = pt->data_dev->bdev; 2627 r = DM_MAPIO_REMAPPED; 2628 spin_unlock_irqrestore(&pool->lock, flags); 2629 2630 return r; 2631} 2632 2633static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit) 2634{ 2635 int r; 2636 struct pool_c *pt = ti->private; 2637 struct pool *pool = pt->pool; 2638 sector_t data_size = ti->len; 2639 dm_block_t sb_data_size; 2640 2641 *need_commit = false; 2642 2643 (void) sector_div(data_size, pool->sectors_per_block); 2644 2645 r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size); 2646 if (r) { 2647 DMERR("%s: failed to retrieve data device size", 2648 dm_device_name(pool->pool_md)); 2649 return r; 2650 } 2651 2652 if (data_size < sb_data_size) { 2653 DMERR("%s: pool target (%llu blocks) too small: expected %llu", 2654 dm_device_name(pool->pool_md), 2655 (unsigned long long)data_size, sb_data_size); 2656 return -EINVAL; 2657 2658 } else if (data_size > sb_data_size) { 2659 if (dm_pool_metadata_needs_check(pool->pmd)) { 2660 DMERR("%s: unable to grow the data device until repaired.", 2661 dm_device_name(pool->pool_md)); 2662 return 0; 2663 } 2664 2665 if (sb_data_size) 2666 DMINFO("%s: growing the data device from %llu to %llu blocks", 2667 dm_device_name(pool->pool_md), 2668 sb_data_size, (unsigned long long)data_size); 2669 r = dm_pool_resize_data_dev(pool->pmd, data_size); 2670 if (r) { 2671 metadata_operation_failed(pool, "dm_pool_resize_data_dev", r); 2672 return r; 2673 } 2674 2675 *need_commit = true; 2676 } 2677 2678 return 0; 2679} 2680 2681static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit) 2682{ 2683 int r; 2684 struct pool_c *pt = ti->private; 2685 struct pool *pool = pt->pool; 2686 dm_block_t metadata_dev_size, sb_metadata_dev_size; 2687 2688 *need_commit = false; 2689 2690 metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev); 2691 2692 r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size); 2693 if (r) { 2694 DMERR("%s: failed to retrieve metadata device size", 2695 dm_device_name(pool->pool_md)); 2696 return r; 2697 } 2698 2699 if (metadata_dev_size < sb_metadata_dev_size) { 2700 DMERR("%s: metadata device (%llu blocks) too small: expected %llu", 2701 dm_device_name(pool->pool_md), 2702 metadata_dev_size, sb_metadata_dev_size); 2703 return -EINVAL; 2704 2705 } else if (metadata_dev_size > sb_metadata_dev_size) { 2706 if (dm_pool_metadata_needs_check(pool->pmd)) { 2707 DMERR("%s: unable to grow the metadata device until repaired.", 2708 dm_device_name(pool->pool_md)); 2709 return 0; 2710 } 2711 2712 warn_if_metadata_device_too_big(pool->md_dev); 2713 DMINFO("%s: growing the metadata device from %llu to %llu blocks", 2714 dm_device_name(pool->pool_md), 2715 sb_metadata_dev_size, metadata_dev_size); 2716 r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size); 2717 if (r) { 2718 metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r); 2719 return r; 2720 } 2721 2722 *need_commit = true; 2723 } 2724 2725 return 0; 2726} 2727 2728/* 2729 * Retrieves the number of blocks of the data device from 2730 * the superblock and compares it to the actual device size, 2731 * thus resizing the data device in case it has grown. 2732 * 2733 * This both copes with opening preallocated data devices in the ctr 2734 * being followed by a resume 2735 * -and- 2736 * calling the resume method individually after userspace has 2737 * grown the data device in reaction to a table event. 2738 */ 2739static int pool_preresume(struct dm_target *ti) 2740{ 2741 int r; 2742 bool need_commit1, need_commit2; 2743 struct pool_c *pt = ti->private; 2744 struct pool *pool = pt->pool; 2745 2746 /* 2747 * Take control of the pool object. 2748 */ 2749 r = bind_control_target(pool, ti); 2750 if (r) 2751 return r; 2752 2753 r = maybe_resize_data_dev(ti, &need_commit1); 2754 if (r) 2755 return r; 2756 2757 r = maybe_resize_metadata_dev(ti, &need_commit2); 2758 if (r) 2759 return r; 2760 2761 if (need_commit1 || need_commit2) 2762 (void) commit(pool); 2763 2764 return 0; 2765} 2766 2767static void pool_resume(struct dm_target *ti) 2768{ 2769 struct pool_c *pt = ti->private; 2770 struct pool *pool = pt->pool; 2771 unsigned long flags; 2772 2773 spin_lock_irqsave(&pool->lock, flags); 2774 pool->low_water_triggered = false; 2775 spin_unlock_irqrestore(&pool->lock, flags); 2776 requeue_bios(pool); 2777 2778 do_waker(&pool->waker.work); 2779} 2780 2781static void pool_postsuspend(struct dm_target *ti) 2782{ 2783 struct pool_c *pt = ti->private; 2784 struct pool *pool = pt->pool; 2785 2786 cancel_delayed_work(&pool->waker); 2787 cancel_delayed_work(&pool->no_space_timeout); 2788 flush_workqueue(pool->wq); 2789 (void) commit(pool); 2790} 2791 2792static int check_arg_count(unsigned argc, unsigned args_required) 2793{ 2794 if (argc != args_required) { 2795 DMWARN("Message received with %u arguments instead of %u.", 2796 argc, args_required); 2797 return -EINVAL; 2798 } 2799 2800 return 0; 2801} 2802 2803static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning) 2804{ 2805 if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) && 2806 *dev_id <= MAX_DEV_ID) 2807 return 0; 2808 2809 if (warning) 2810 DMWARN("Message received with invalid device id: %s", arg); 2811 2812 return -EINVAL; 2813} 2814 2815static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool) 2816{ 2817 dm_thin_id dev_id; 2818 int r; 2819 2820 r = check_arg_count(argc, 2); 2821 if (r) 2822 return r; 2823 2824 r = read_dev_id(argv[1], &dev_id, 1); 2825 if (r) 2826 return r; 2827 2828 r = dm_pool_create_thin(pool->pmd, dev_id); 2829 if (r) { 2830 DMWARN("Creation of new thinly-provisioned device with id %s failed.", 2831 argv[1]); 2832 return r; 2833 } 2834 2835 return 0; 2836} 2837 2838static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool) 2839{ 2840 dm_thin_id dev_id; 2841 dm_thin_id origin_dev_id; 2842 int r; 2843 2844 r = check_arg_count(argc, 3); 2845 if (r) 2846 return r; 2847 2848 r = read_dev_id(argv[1], &dev_id, 1); 2849 if (r) 2850 return r; 2851 2852 r = read_dev_id(argv[2], &origin_dev_id, 1); 2853 if (r) 2854 return r; 2855 2856 r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id); 2857 if (r) { 2858 DMWARN("Creation of new snapshot %s of device %s failed.", 2859 argv[1], argv[2]); 2860 return r; 2861 } 2862 2863 return 0; 2864} 2865 2866static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool) 2867{ 2868 dm_thin_id dev_id; 2869 int r; 2870 2871 r = check_arg_count(argc, 2); 2872 if (r) 2873 return r; 2874 2875 r = read_dev_id(argv[1], &dev_id, 1); 2876 if (r) 2877 return r; 2878 2879 r = dm_pool_delete_thin_device(pool->pmd, dev_id); 2880 if (r) 2881 DMWARN("Deletion of thin device %s failed.", argv[1]); 2882 2883 return r; 2884} 2885 2886static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool) 2887{ 2888 dm_thin_id old_id, new_id; 2889 int r; 2890 2891 r = check_arg_count(argc, 3); 2892 if (r) 2893 return r; 2894 2895 if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) { 2896 DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]); 2897 return -EINVAL; 2898 } 2899 2900 if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) { 2901 DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]); 2902 return -EINVAL; 2903 } 2904 2905 r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id); 2906 if (r) { 2907 DMWARN("Failed to change transaction id from %s to %s.", 2908 argv[1], argv[2]); 2909 return r; 2910 } 2911 2912 return 0; 2913} 2914 2915static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) 2916{ 2917 int r; 2918 2919 r = check_arg_count(argc, 1); 2920 if (r) 2921 return r; 2922 2923 (void) commit(pool); 2924 2925 r = dm_pool_reserve_metadata_snap(pool->pmd); 2926 if (r) 2927 DMWARN("reserve_metadata_snap message failed."); 2928 2929 return r; 2930} 2931 2932static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool) 2933{ 2934 int r; 2935 2936 r = check_arg_count(argc, 1); 2937 if (r) 2938 return r; 2939 2940 r = dm_pool_release_metadata_snap(pool->pmd); 2941 if (r) 2942 DMWARN("release_metadata_snap message failed."); 2943 2944 return r; 2945} 2946 2947/* 2948 * Messages supported: 2949 * create_thin <dev_id> 2950 * create_snap <dev_id> <origin_id> 2951 * delete <dev_id> 2952 * trim <dev_id> <new_size_in_sectors> 2953 * set_transaction_id <current_trans_id> <new_trans_id> 2954 * reserve_metadata_snap 2955 * release_metadata_snap 2956 */ 2957static int pool_message(struct dm_target *ti, unsigned argc, char **argv) 2958{ 2959 int r = -EINVAL; 2960 struct pool_c *pt = ti->private; 2961 struct pool *pool = pt->pool; 2962 2963 if (!strcasecmp(argv[0], "create_thin")) 2964 r = process_create_thin_mesg(argc, argv, pool); 2965 2966 else if (!strcasecmp(argv[0], "create_snap")) 2967 r = process_create_snap_mesg(argc, argv, pool); 2968 2969 else if (!strcasecmp(argv[0], "delete")) 2970 r = process_delete_mesg(argc, argv, pool); 2971 2972 else if (!strcasecmp(argv[0], "set_transaction_id")) 2973 r = process_set_transaction_id_mesg(argc, argv, pool); 2974 2975 else if (!strcasecmp(argv[0], "reserve_metadata_snap")) 2976 r = process_reserve_metadata_snap_mesg(argc, argv, pool); 2977 2978 else if (!strcasecmp(argv[0], "release_metadata_snap")) 2979 r = process_release_metadata_snap_mesg(argc, argv, pool); 2980 2981 else 2982 DMWARN("Unrecognised thin pool target message received: %s", argv[0]); 2983 2984 if (!r) 2985 (void) commit(pool); 2986 2987 return r; 2988} 2989 2990static void emit_flags(struct pool_features *pf, char *result, 2991 unsigned sz, unsigned maxlen) 2992{ 2993 unsigned count = !pf->zero_new_blocks + !pf->discard_enabled + 2994 !pf->discard_passdown + (pf->mode == PM_READ_ONLY) + 2995 pf->error_if_no_space; 2996 DMEMIT("%u ", count); 2997 2998 if (!pf->zero_new_blocks) 2999 DMEMIT("skip_block_zeroing "); 3000 3001 if (!pf->discard_enabled) 3002 DMEMIT("ignore_discard "); 3003 3004 if (!pf->discard_passdown) 3005 DMEMIT("no_discard_passdown "); 3006 3007 if (pf->mode == PM_READ_ONLY) 3008 DMEMIT("read_only "); 3009 3010 if (pf->error_if_no_space) 3011 DMEMIT("error_if_no_space "); 3012} 3013 3014/* 3015 * Status line is: 3016 * <transaction id> <used metadata sectors>/<total metadata sectors> 3017 * <used data sectors>/<total data sectors> <held metadata root> 3018 */ 3019static void pool_status(struct dm_target *ti, status_type_t type, 3020 unsigned status_flags, char *result, unsigned maxlen) 3021{ 3022 int r; 3023 unsigned sz = 0; 3024 uint64_t transaction_id; 3025 dm_block_t nr_free_blocks_data; 3026 dm_block_t nr_free_blocks_metadata; 3027 dm_block_t nr_blocks_data; 3028 dm_block_t nr_blocks_metadata; 3029 dm_block_t held_root; 3030 char buf[BDEVNAME_SIZE]; 3031 char buf2[BDEVNAME_SIZE]; 3032 struct pool_c *pt = ti->private; 3033 struct pool *pool = pt->pool; 3034 3035 switch (type) { 3036 case STATUSTYPE_INFO: 3037 if (get_pool_mode(pool) == PM_FAIL) { 3038 DMEMIT("Fail"); 3039 break; 3040 } 3041 3042 /* Commit to ensure statistics aren't out-of-date */ 3043 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) 3044 (void) commit(pool); 3045 3046 r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id); 3047 if (r) { 3048 DMERR("%s: dm_pool_get_metadata_transaction_id returned %d", 3049 dm_device_name(pool->pool_md), r); 3050 goto err; 3051 } 3052 3053 r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata); 3054 if (r) { 3055 DMERR("%s: dm_pool_get_free_metadata_block_count returned %d", 3056 dm_device_name(pool->pool_md), r); 3057 goto err; 3058 } 3059 3060 r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata); 3061 if (r) { 3062 DMERR("%s: dm_pool_get_metadata_dev_size returned %d", 3063 dm_device_name(pool->pool_md), r); 3064 goto err; 3065 } 3066 3067 r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data); 3068 if (r) { 3069 DMERR("%s: dm_pool_get_free_block_count returned %d", 3070 dm_device_name(pool->pool_md), r); 3071 goto err; 3072 } 3073 3074 r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data); 3075 if (r) { 3076 DMERR("%s: dm_pool_get_data_dev_size returned %d", 3077 dm_device_name(pool->pool_md), r); 3078 goto err; 3079 } 3080 3081 r = dm_pool_get_metadata_snap(pool->pmd, &held_root); 3082 if (r) { 3083 DMERR("%s: dm_pool_get_metadata_snap returned %d", 3084 dm_device_name(pool->pool_md), r); 3085 goto err; 3086 } 3087 3088 DMEMIT("%llu %llu/%llu %llu/%llu ", 3089 (unsigned long long)transaction_id, 3090 (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata), 3091 (unsigned long long)nr_blocks_metadata, 3092 (unsigned long long)(nr_blocks_data - nr_free_blocks_data), 3093 (unsigned long long)nr_blocks_data); 3094 3095 if (held_root) 3096 DMEMIT("%llu ", held_root); 3097 else 3098 DMEMIT("- "); 3099 3100 if (pool->pf.mode == PM_OUT_OF_DATA_SPACE) 3101 DMEMIT("out_of_data_space "); 3102 else if (pool->pf.mode == PM_READ_ONLY) 3103 DMEMIT("ro "); 3104 else 3105 DMEMIT("rw "); 3106 3107 if (!pool->pf.discard_enabled) 3108 DMEMIT("ignore_discard "); 3109 else if (pool->pf.discard_passdown) 3110 DMEMIT("discard_passdown "); 3111 else 3112 DMEMIT("no_discard_passdown "); 3113 3114 if (pool->pf.error_if_no_space) 3115 DMEMIT("error_if_no_space "); 3116 else 3117 DMEMIT("queue_if_no_space "); 3118 3119 break; 3120 3121 case STATUSTYPE_TABLE: 3122 DMEMIT("%s %s %lu %llu ", 3123 format_dev_t(buf, pt->metadata_dev->bdev->bd_dev), 3124 format_dev_t(buf2, pt->data_dev->bdev->bd_dev), 3125 (unsigned long)pool->sectors_per_block, 3126 (unsigned long long)pt->low_water_blocks); 3127 emit_flags(&pt->requested_pf, result, sz, maxlen); 3128 break; 3129 } 3130 return; 3131 3132err: 3133 DMEMIT("Error"); 3134} 3135 3136static int pool_iterate_devices(struct dm_target *ti, 3137 iterate_devices_callout_fn fn, void *data) 3138{ 3139 struct pool_c *pt = ti->private; 3140 3141 return fn(ti, pt->data_dev, 0, ti->len, data); 3142} 3143 3144static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm, 3145 struct bio_vec *biovec, int max_size) 3146{ 3147 struct pool_c *pt = ti->private; 3148 struct request_queue *q = bdev_get_queue(pt->data_dev->bdev); 3149 3150 if (!q->merge_bvec_fn) 3151 return max_size; 3152 3153 bvm->bi_bdev = pt->data_dev->bdev; 3154 3155 return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); 3156} 3157 3158static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits) 3159{ 3160 struct pool *pool = pt->pool; 3161 struct queue_limits *data_limits; 3162 3163 limits->max_discard_sectors = pool->sectors_per_block; 3164 3165 /* 3166 * discard_granularity is just a hint, and not enforced. 3167 */ 3168 if (pt->adjusted_pf.discard_passdown) { 3169 data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits; 3170 limits->discard_granularity = max(data_limits->discard_granularity, 3171 pool->sectors_per_block << SECTOR_SHIFT); 3172 } else 3173 limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT; 3174} 3175 3176static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits) 3177{ 3178 struct pool_c *pt = ti->private; 3179 struct pool *pool = pt->pool; 3180 uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT; 3181 3182 /* 3183 * If the system-determined stacked limits are compatible with the 3184 * pool's blocksize (io_opt is a factor) do not override them. 3185 */ 3186 if (io_opt_sectors < pool->sectors_per_block || 3187 do_div(io_opt_sectors, pool->sectors_per_block)) { 3188 blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT); 3189 blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT); 3190 } 3191 3192 /* 3193 * pt->adjusted_pf is a staging area for the actual features to use. 3194 * They get transferred to the live pool in bind_control_target() 3195 * called from pool_preresume(). 3196 */ 3197 if (!pt->adjusted_pf.discard_enabled) { 3198 /* 3199 * Must explicitly disallow stacking discard limits otherwise the 3200 * block layer will stack them if pool's data device has support. 3201 * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the 3202 * user to see that, so make sure to set all discard limits to 0. 3203 */ 3204 limits->discard_granularity = 0; 3205 return; 3206 } 3207 3208 disable_passdown_if_not_supported(pt); 3209 3210 set_discard_limits(pt, limits); 3211} 3212 3213static struct target_type pool_target = { 3214 .name = "thin-pool", 3215 .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE | 3216 DM_TARGET_IMMUTABLE, 3217 .version = {1, 13, 0}, 3218 .module = THIS_MODULE, 3219 .ctr = pool_ctr, 3220 .dtr = pool_dtr, 3221 .map = pool_map, 3222 .postsuspend = pool_postsuspend, 3223 .preresume = pool_preresume, 3224 .resume = pool_resume, 3225 .message = pool_message, 3226 .status = pool_status, 3227 .merge = pool_merge, 3228 .iterate_devices = pool_iterate_devices, 3229 .io_hints = pool_io_hints, 3230}; 3231 3232/*---------------------------------------------------------------- 3233 * Thin target methods 3234 *--------------------------------------------------------------*/ 3235static void thin_get(struct thin_c *tc) 3236{ 3237 atomic_inc(&tc->refcount); 3238} 3239 3240static void thin_put(struct thin_c *tc) 3241{ 3242 if (atomic_dec_and_test(&tc->refcount)) 3243 complete(&tc->can_destroy); 3244} 3245 3246static void thin_dtr(struct dm_target *ti) 3247{ 3248 struct thin_c *tc = ti->private; 3249 unsigned long flags; 3250 3251 thin_put(tc); 3252 wait_for_completion(&tc->can_destroy); 3253 3254 spin_lock_irqsave(&tc->pool->lock, flags); 3255 list_del_rcu(&tc->list); 3256 spin_unlock_irqrestore(&tc->pool->lock, flags); 3257 synchronize_rcu(); 3258 3259 mutex_lock(&dm_thin_pool_table.mutex); 3260 3261 __pool_dec(tc->pool); 3262 dm_pool_close_thin_device(tc->td); 3263 dm_put_device(ti, tc->pool_dev); 3264 if (tc->origin_dev) 3265 dm_put_device(ti, tc->origin_dev); 3266 kfree(tc); 3267 3268 mutex_unlock(&dm_thin_pool_table.mutex); 3269} 3270 3271/* 3272 * Thin target parameters: 3273 * 3274 * <pool_dev> <dev_id> [origin_dev] 3275 * 3276 * pool_dev: the path to the pool (eg, /dev/mapper/my_pool) 3277 * dev_id: the internal device identifier 3278 * origin_dev: a device external to the pool that should act as the origin 3279 * 3280 * If the pool device has discards disabled, they get disabled for the thin 3281 * device as well. 3282 */ 3283static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv) 3284{ 3285 int r; 3286 struct thin_c *tc; 3287 struct dm_dev *pool_dev, *origin_dev; 3288 struct mapped_device *pool_md; 3289 unsigned long flags; 3290 3291 mutex_lock(&dm_thin_pool_table.mutex); 3292 3293 if (argc != 2 && argc != 3) { 3294 ti->error = "Invalid argument count"; 3295 r = -EINVAL; 3296 goto out_unlock; 3297 } 3298 3299 tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL); 3300 if (!tc) { 3301 ti->error = "Out of memory"; 3302 r = -ENOMEM; 3303 goto out_unlock; 3304 } 3305 spin_lock_init(&tc->lock); 3306 bio_list_init(&tc->deferred_bio_list); 3307 bio_list_init(&tc->retry_on_resume_list); 3308 tc->sort_bio_list = RB_ROOT; 3309 3310 if (argc == 3) { 3311 r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev); 3312 if (r) { 3313 ti->error = "Error opening origin device"; 3314 goto bad_origin_dev; 3315 } 3316 tc->origin_dev = origin_dev; 3317 } 3318 3319 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev); 3320 if (r) { 3321 ti->error = "Error opening pool device"; 3322 goto bad_pool_dev; 3323 } 3324 tc->pool_dev = pool_dev; 3325 3326 if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) { 3327 ti->error = "Invalid device id"; 3328 r = -EINVAL; 3329 goto bad_common; 3330 } 3331 3332 pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev); 3333 if (!pool_md) { 3334 ti->error = "Couldn't get pool mapped device"; 3335 r = -EINVAL; 3336 goto bad_common; 3337 } 3338 3339 tc->pool = __pool_table_lookup(pool_md); 3340 if (!tc->pool) { 3341 ti->error = "Couldn't find pool object"; 3342 r = -EINVAL; 3343 goto bad_pool_lookup; 3344 } 3345 __pool_inc(tc->pool); 3346 3347 if (get_pool_mode(tc->pool) == PM_FAIL) { 3348 ti->error = "Couldn't open thin device, Pool is in fail mode"; 3349 r = -EINVAL; 3350 goto bad_thin_open; 3351 } 3352 3353 r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td); 3354 if (r) { 3355 ti->error = "Couldn't open thin internal device"; 3356 goto bad_thin_open; 3357 } 3358 3359 r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block); 3360 if (r) 3361 goto bad_target_max_io_len; 3362 3363 ti->num_flush_bios = 1; 3364 ti->flush_supported = true; 3365 ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook); 3366 3367 /* In case the pool supports discards, pass them on. */ 3368 ti->discard_zeroes_data_unsupported = true; 3369 if (tc->pool->pf.discard_enabled) { 3370 ti->discards_supported = true; 3371 ti->num_discard_bios = 1; 3372 /* Discard bios must be split on a block boundary */ 3373 ti->split_discard_bios = true; 3374 } 3375 3376 dm_put(pool_md); 3377 3378 mutex_unlock(&dm_thin_pool_table.mutex); 3379 3380 atomic_set(&tc->refcount, 1); 3381 init_completion(&tc->can_destroy); 3382 3383 spin_lock_irqsave(&tc->pool->lock, flags); 3384 list_add_tail_rcu(&tc->list, &tc->pool->active_thins); 3385 spin_unlock_irqrestore(&tc->pool->lock, flags); 3386 /* 3387 * This synchronize_rcu() call is needed here otherwise we risk a 3388 * wake_worker() call finding no bios to process (because the newly 3389 * added tc isn't yet visible). So this reduces latency since we 3390 * aren't then dependent on the periodic commit to wake_worker(). 3391 */ 3392 synchronize_rcu(); 3393 3394 return 0; 3395 3396bad_target_max_io_len: 3397 dm_pool_close_thin_device(tc->td); 3398bad_thin_open: 3399 __pool_dec(tc->pool); 3400bad_pool_lookup: 3401 dm_put(pool_md); 3402bad_common: 3403 dm_put_device(ti, tc->pool_dev); 3404bad_pool_dev: 3405 if (tc->origin_dev) 3406 dm_put_device(ti, tc->origin_dev); 3407bad_origin_dev: 3408 kfree(tc); 3409out_unlock: 3410 mutex_unlock(&dm_thin_pool_table.mutex); 3411 3412 return r; 3413} 3414 3415static int thin_map(struct dm_target *ti, struct bio *bio) 3416{ 3417 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); 3418 3419 return thin_bio_map(ti, bio); 3420} 3421 3422static int thin_endio(struct dm_target *ti, struct bio *bio, int err) 3423{ 3424 unsigned long flags; 3425 struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook)); 3426 struct list_head work; 3427 struct dm_thin_new_mapping *m, *tmp; 3428 struct pool *pool = h->tc->pool; 3429 3430 if (h->shared_read_entry) { 3431 INIT_LIST_HEAD(&work); 3432 dm_deferred_entry_dec(h->shared_read_entry, &work); 3433 3434 spin_lock_irqsave(&pool->lock, flags); 3435 list_for_each_entry_safe(m, tmp, &work, list) { 3436 list_del(&m->list); 3437 __complete_mapping_preparation(m); 3438 } 3439 spin_unlock_irqrestore(&pool->lock, flags); 3440 } 3441 3442 if (h->all_io_entry) { 3443 INIT_LIST_HEAD(&work); 3444 dm_deferred_entry_dec(h->all_io_entry, &work); 3445 if (!list_empty(&work)) { 3446 spin_lock_irqsave(&pool->lock, flags); 3447 list_for_each_entry_safe(m, tmp, &work, list) 3448 list_add_tail(&m->list, &pool->prepared_discards); 3449 spin_unlock_irqrestore(&pool->lock, flags); 3450 wake_worker(pool); 3451 } 3452 } 3453 3454 return 0; 3455} 3456 3457static void thin_presuspend(struct dm_target *ti) 3458{ 3459 struct thin_c *tc = ti->private; 3460 3461 if (dm_noflush_suspending(ti)) 3462 noflush_work(tc, do_noflush_start); 3463} 3464 3465static void thin_postsuspend(struct dm_target *ti) 3466{ 3467 struct thin_c *tc = ti->private; 3468 3469 /* 3470 * The dm_noflush_suspending flag has been cleared by now, so 3471 * unfortunately we must always run this. 3472 */ 3473 noflush_work(tc, do_noflush_stop); 3474} 3475 3476static int thin_preresume(struct dm_target *ti) 3477{ 3478 struct thin_c *tc = ti->private; 3479 3480 if (tc->origin_dev) 3481 tc->origin_size = get_dev_size(tc->origin_dev->bdev); 3482 3483 return 0; 3484} 3485 3486/* 3487 * <nr mapped sectors> <highest mapped sector> 3488 */ 3489static void thin_status(struct dm_target *ti, status_type_t type, 3490 unsigned status_flags, char *result, unsigned maxlen) 3491{ 3492 int r; 3493 ssize_t sz = 0; 3494 dm_block_t mapped, highest; 3495 char buf[BDEVNAME_SIZE]; 3496 struct thin_c *tc = ti->private; 3497 3498 if (get_pool_mode(tc->pool) == PM_FAIL) { 3499 DMEMIT("Fail"); 3500 return; 3501 } 3502 3503 if (!tc->td) 3504 DMEMIT("-"); 3505 else { 3506 switch (type) { 3507 case STATUSTYPE_INFO: 3508 r = dm_thin_get_mapped_count(tc->td, &mapped); 3509 if (r) { 3510 DMERR("dm_thin_get_mapped_count returned %d", r); 3511 goto err; 3512 } 3513 3514 r = dm_thin_get_highest_mapped_block(tc->td, &highest); 3515 if (r < 0) { 3516 DMERR("dm_thin_get_highest_mapped_block returned %d", r); 3517 goto err; 3518 } 3519 3520 DMEMIT("%llu ", mapped * tc->pool->sectors_per_block); 3521 if (r) 3522 DMEMIT("%llu", ((highest + 1) * 3523 tc->pool->sectors_per_block) - 1); 3524 else 3525 DMEMIT("-"); 3526 break; 3527 3528 case STATUSTYPE_TABLE: 3529 DMEMIT("%s %lu", 3530 format_dev_t(buf, tc->pool_dev->bdev->bd_dev), 3531 (unsigned long) tc->dev_id); 3532 if (tc->origin_dev) 3533 DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev)); 3534 break; 3535 } 3536 } 3537 3538 return; 3539 3540err: 3541 DMEMIT("Error"); 3542} 3543 3544static int thin_iterate_devices(struct dm_target *ti, 3545 iterate_devices_callout_fn fn, void *data) 3546{ 3547 sector_t blocks; 3548 struct thin_c *tc = ti->private; 3549 struct pool *pool = tc->pool; 3550 3551 /* 3552 * We can't call dm_pool_get_data_dev_size() since that blocks. So 3553 * we follow a more convoluted path through to the pool's target. 3554 */ 3555 if (!pool->ti) 3556 return 0; /* nothing is bound */ 3557 3558 blocks = pool->ti->len; 3559 (void) sector_div(blocks, pool->sectors_per_block); 3560 if (blocks) 3561 return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data); 3562 3563 return 0; 3564} 3565 3566static struct target_type thin_target = { 3567 .name = "thin", 3568 .version = {1, 13, 0}, 3569 .module = THIS_MODULE, 3570 .ctr = thin_ctr, 3571 .dtr = thin_dtr, 3572 .map = thin_map, 3573 .end_io = thin_endio, 3574 .preresume = thin_preresume, 3575 .presuspend = thin_presuspend, 3576 .postsuspend = thin_postsuspend, 3577 .status = thin_status, 3578 .iterate_devices = thin_iterate_devices, 3579}; 3580 3581/*----------------------------------------------------------------*/ 3582 3583static int __init dm_thin_init(void) 3584{ 3585 int r; 3586 3587 pool_table_init(); 3588 3589 r = dm_register_target(&thin_target); 3590 if (r) 3591 return r; 3592 3593 r = dm_register_target(&pool_target); 3594 if (r) 3595 goto bad_pool_target; 3596 3597 r = -ENOMEM; 3598 3599 _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0); 3600 if (!_new_mapping_cache) 3601 goto bad_new_mapping_cache; 3602 3603 return 0; 3604 3605bad_new_mapping_cache: 3606 dm_unregister_target(&pool_target); 3607bad_pool_target: 3608 dm_unregister_target(&thin_target); 3609 3610 return r; 3611} 3612 3613static void dm_thin_exit(void) 3614{ 3615 dm_unregister_target(&thin_target); 3616 dm_unregister_target(&pool_target); 3617 3618 kmem_cache_destroy(_new_mapping_cache); 3619} 3620 3621module_init(dm_thin_init); 3622module_exit(dm_thin_exit); 3623 3624module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR); 3625MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds"); 3626 3627MODULE_DESCRIPTION(DM_NAME " thin provisioning target"); 3628MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 3629MODULE_LICENSE("GPL"); 3630