1 /* 2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 4 * 5 * This file is released under the GPL. 6 */ 7 8 #include "dm.h" 9 #include "dm-uevent.h" 10 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/moduleparam.h> 15 #include <linux/blkpg.h> 16 #include <linux/bio.h> 17 #include <linux/buffer_head.h> 18 #include <linux/smp_lock.h> 19 #include <linux/mempool.h> 20 #include <linux/slab.h> 21 #include <linux/idr.h> 22 #include <linux/hdreg.h> 23 #include <linux/delay.h> 24 25 #include <trace/events/block.h> 26 27 #define DM_MSG_PREFIX "core" 28 29 /* 30 * Cookies are numeric values sent with CHANGE and REMOVE 31 * uevents while resuming, removing or renaming the device. 32 */ 33 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 34 #define DM_COOKIE_LENGTH 24 35 36 static const char *_name = DM_NAME; 37 38 static unsigned int major = 0; 39 static unsigned int _major = 0; 40 41 static DEFINE_SPINLOCK(_minor_lock); 42 /* 43 * For bio-based dm. 44 * One of these is allocated per bio. 45 */ 46 struct dm_io { 47 struct mapped_device *md; 48 int error; 49 atomic_t io_count; 50 struct bio *bio; 51 unsigned long start_time; 52 spinlock_t endio_lock; 53 }; 54 55 /* 56 * For bio-based dm. 57 * One of these is allocated per target within a bio. Hopefully 58 * this will be simplified out one day. 59 */ 60 struct dm_target_io { 61 struct dm_io *io; 62 struct dm_target *ti; 63 union map_info info; 64 }; 65 66 /* 67 * For request-based dm. 68 * One of these is allocated per request. 69 */ 70 struct dm_rq_target_io { 71 struct mapped_device *md; 72 struct dm_target *ti; 73 struct request *orig, clone; 74 int error; 75 union map_info info; 76 }; 77 78 /* 79 * For request-based dm. 80 * One of these is allocated per bio. 81 */ 82 struct dm_rq_clone_bio_info { 83 struct bio *orig; 84 struct dm_rq_target_io *tio; 85 }; 86 87 union map_info *dm_get_mapinfo(struct bio *bio) 88 { 89 if (bio && bio->bi_private) 90 return &((struct dm_target_io *)bio->bi_private)->info; 91 return NULL; 92 } 93 94 union map_info *dm_get_rq_mapinfo(struct request *rq) 95 { 96 if (rq && rq->end_io_data) 97 return &((struct dm_rq_target_io *)rq->end_io_data)->info; 98 return NULL; 99 } 100 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 101 102 #define MINOR_ALLOCED ((void *)-1) 103 104 /* 105 * Bits for the md->flags field. 106 */ 107 #define DMF_BLOCK_IO_FOR_SUSPEND 0 108 #define DMF_SUSPENDED 1 109 #define DMF_FROZEN 2 110 #define DMF_FREEING 3 111 #define DMF_DELETING 4 112 #define DMF_NOFLUSH_SUSPENDING 5 113 #define DMF_QUEUE_IO_TO_THREAD 6 114 115 /* 116 * Work processed by per-device workqueue. 117 */ 118 struct mapped_device { 119 struct rw_semaphore io_lock; 120 struct mutex suspend_lock; 121 rwlock_t map_lock; 122 atomic_t holders; 123 atomic_t open_count; 124 125 unsigned long flags; 126 127 struct request_queue *queue; 128 struct gendisk *disk; 129 char name[16]; 130 131 void *interface_ptr; 132 133 /* 134 * A list of ios that arrived while we were suspended. 135 */ 136 atomic_t pending[2]; 137 wait_queue_head_t wait; 138 struct work_struct work; 139 struct bio_list deferred; 140 spinlock_t deferred_lock; 141 142 /* 143 * An error from the barrier request currently being processed. 144 */ 145 int barrier_error; 146 147 /* 148 * Protect barrier_error from concurrent endio processing 149 * in request-based dm. 150 */ 151 spinlock_t barrier_error_lock; 152 153 /* 154 * Processing queue (flush/barriers) 155 */ 156 struct workqueue_struct *wq; 157 struct work_struct barrier_work; 158 159 /* A pointer to the currently processing pre/post flush request */ 160 struct request *flush_request; 161 162 /* 163 * The current mapping. 164 */ 165 struct dm_table *map; 166 167 /* 168 * io objects are allocated from here. 169 */ 170 mempool_t *io_pool; 171 mempool_t *tio_pool; 172 173 struct bio_set *bs; 174 175 /* 176 * Event handling. 177 */ 178 atomic_t event_nr; 179 wait_queue_head_t eventq; 180 atomic_t uevent_seq; 181 struct list_head uevent_list; 182 spinlock_t uevent_lock; /* Protect access to uevent_list */ 183 184 /* 185 * freeze/thaw support require holding onto a super block 186 */ 187 struct super_block *frozen_sb; 188 struct block_device *bdev; 189 190 /* forced geometry settings */ 191 struct hd_geometry geometry; 192 193 /* For saving the address of __make_request for request based dm */ 194 make_request_fn *saved_make_request_fn; 195 196 /* sysfs handle */ 197 struct kobject kobj; 198 199 /* zero-length barrier that will be cloned and submitted to targets */ 200 struct bio barrier_bio; 201 }; 202 203 /* 204 * For mempools pre-allocation at the table loading time. 205 */ 206 struct dm_md_mempools { 207 mempool_t *io_pool; 208 mempool_t *tio_pool; 209 struct bio_set *bs; 210 }; 211 212 #define MIN_IOS 256 213 static struct kmem_cache *_io_cache; 214 static struct kmem_cache *_tio_cache; 215 static struct kmem_cache *_rq_tio_cache; 216 static struct kmem_cache *_rq_bio_info_cache; 217 218 static int __init local_init(void) 219 { 220 int r = -ENOMEM; 221 222 /* allocate a slab for the dm_ios */ 223 _io_cache = KMEM_CACHE(dm_io, 0); 224 if (!_io_cache) 225 return r; 226 227 /* allocate a slab for the target ios */ 228 _tio_cache = KMEM_CACHE(dm_target_io, 0); 229 if (!_tio_cache) 230 goto out_free_io_cache; 231 232 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 233 if (!_rq_tio_cache) 234 goto out_free_tio_cache; 235 236 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 237 if (!_rq_bio_info_cache) 238 goto out_free_rq_tio_cache; 239 240 r = dm_uevent_init(); 241 if (r) 242 goto out_free_rq_bio_info_cache; 243 244 _major = major; 245 r = register_blkdev(_major, _name); 246 if (r < 0) 247 goto out_uevent_exit; 248 249 if (!_major) 250 _major = r; 251 252 return 0; 253 254 out_uevent_exit: 255 dm_uevent_exit(); 256 out_free_rq_bio_info_cache: 257 kmem_cache_destroy(_rq_bio_info_cache); 258 out_free_rq_tio_cache: 259 kmem_cache_destroy(_rq_tio_cache); 260 out_free_tio_cache: 261 kmem_cache_destroy(_tio_cache); 262 out_free_io_cache: 263 kmem_cache_destroy(_io_cache); 264 265 return r; 266 } 267 268 static void local_exit(void) 269 { 270 kmem_cache_destroy(_rq_bio_info_cache); 271 kmem_cache_destroy(_rq_tio_cache); 272 kmem_cache_destroy(_tio_cache); 273 kmem_cache_destroy(_io_cache); 274 unregister_blkdev(_major, _name); 275 dm_uevent_exit(); 276 277 _major = 0; 278 279 DMINFO("cleaned up"); 280 } 281 282 static int (*_inits[])(void) __initdata = { 283 local_init, 284 dm_target_init, 285 dm_linear_init, 286 dm_stripe_init, 287 dm_io_init, 288 dm_kcopyd_init, 289 dm_interface_init, 290 }; 291 292 static void (*_exits[])(void) = { 293 local_exit, 294 dm_target_exit, 295 dm_linear_exit, 296 dm_stripe_exit, 297 dm_io_exit, 298 dm_kcopyd_exit, 299 dm_interface_exit, 300 }; 301 302 static int __init dm_init(void) 303 { 304 const int count = ARRAY_SIZE(_inits); 305 306 int r, i; 307 308 for (i = 0; i < count; i++) { 309 r = _inits[i](); 310 if (r) 311 goto bad; 312 } 313 314 return 0; 315 316 bad: 317 while (i--) 318 _exits[i](); 319 320 return r; 321 } 322 323 static void __exit dm_exit(void) 324 { 325 int i = ARRAY_SIZE(_exits); 326 327 while (i--) 328 _exits[i](); 329 } 330 331 /* 332 * Block device functions 333 */ 334 int dm_deleting_md(struct mapped_device *md) 335 { 336 return test_bit(DMF_DELETING, &md->flags); 337 } 338 339 static int dm_blk_open(struct block_device *bdev, fmode_t mode) 340 { 341 struct mapped_device *md; 342 343 lock_kernel(); 344 spin_lock(&_minor_lock); 345 346 md = bdev->bd_disk->private_data; 347 if (!md) 348 goto out; 349 350 if (test_bit(DMF_FREEING, &md->flags) || 351 dm_deleting_md(md)) { 352 md = NULL; 353 goto out; 354 } 355 356 dm_get(md); 357 atomic_inc(&md->open_count); 358 359 out: 360 spin_unlock(&_minor_lock); 361 unlock_kernel(); 362 363 return md ? 0 : -ENXIO; 364 } 365 366 static int dm_blk_close(struct gendisk *disk, fmode_t mode) 367 { 368 struct mapped_device *md = disk->private_data; 369 370 lock_kernel(); 371 atomic_dec(&md->open_count); 372 dm_put(md); 373 unlock_kernel(); 374 375 return 0; 376 } 377 378 int dm_open_count(struct mapped_device *md) 379 { 380 return atomic_read(&md->open_count); 381 } 382 383 /* 384 * Guarantees nothing is using the device before it's deleted. 385 */ 386 int dm_lock_for_deletion(struct mapped_device *md) 387 { 388 int r = 0; 389 390 spin_lock(&_minor_lock); 391 392 if (dm_open_count(md)) 393 r = -EBUSY; 394 else 395 set_bit(DMF_DELETING, &md->flags); 396 397 spin_unlock(&_minor_lock); 398 399 return r; 400 } 401 402 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 403 { 404 struct mapped_device *md = bdev->bd_disk->private_data; 405 406 return dm_get_geometry(md, geo); 407 } 408 409 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 410 unsigned int cmd, unsigned long arg) 411 { 412 struct mapped_device *md = bdev->bd_disk->private_data; 413 struct dm_table *map = dm_get_live_table(md); 414 struct dm_target *tgt; 415 int r = -ENOTTY; 416 417 if (!map || !dm_table_get_size(map)) 418 goto out; 419 420 /* We only support devices that have a single target */ 421 if (dm_table_get_num_targets(map) != 1) 422 goto out; 423 424 tgt = dm_table_get_target(map, 0); 425 426 if (dm_suspended_md(md)) { 427 r = -EAGAIN; 428 goto out; 429 } 430 431 if (tgt->type->ioctl) 432 r = tgt->type->ioctl(tgt, cmd, arg); 433 434 out: 435 dm_table_put(map); 436 437 return r; 438 } 439 440 static struct dm_io *alloc_io(struct mapped_device *md) 441 { 442 return mempool_alloc(md->io_pool, GFP_NOIO); 443 } 444 445 static void free_io(struct mapped_device *md, struct dm_io *io) 446 { 447 mempool_free(io, md->io_pool); 448 } 449 450 static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 451 { 452 mempool_free(tio, md->tio_pool); 453 } 454 455 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 456 gfp_t gfp_mask) 457 { 458 return mempool_alloc(md->tio_pool, gfp_mask); 459 } 460 461 static void free_rq_tio(struct dm_rq_target_io *tio) 462 { 463 mempool_free(tio, tio->md->tio_pool); 464 } 465 466 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md) 467 { 468 return mempool_alloc(md->io_pool, GFP_ATOMIC); 469 } 470 471 static void free_bio_info(struct dm_rq_clone_bio_info *info) 472 { 473 mempool_free(info, info->tio->md->io_pool); 474 } 475 476 static int md_in_flight(struct mapped_device *md) 477 { 478 return atomic_read(&md->pending[READ]) + 479 atomic_read(&md->pending[WRITE]); 480 } 481 482 static void start_io_acct(struct dm_io *io) 483 { 484 struct mapped_device *md = io->md; 485 int cpu; 486 int rw = bio_data_dir(io->bio); 487 488 io->start_time = jiffies; 489 490 cpu = part_stat_lock(); 491 part_round_stats(cpu, &dm_disk(md)->part0); 492 part_stat_unlock(); 493 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]); 494 } 495 496 static void end_io_acct(struct dm_io *io) 497 { 498 struct mapped_device *md = io->md; 499 struct bio *bio = io->bio; 500 unsigned long duration = jiffies - io->start_time; 501 int pending, cpu; 502 int rw = bio_data_dir(bio); 503 504 cpu = part_stat_lock(); 505 part_round_stats(cpu, &dm_disk(md)->part0); 506 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 507 part_stat_unlock(); 508 509 /* 510 * After this is decremented the bio must not be touched if it is 511 * a barrier. 512 */ 513 dm_disk(md)->part0.in_flight[rw] = pending = 514 atomic_dec_return(&md->pending[rw]); 515 pending += atomic_read(&md->pending[rw^0x1]); 516 517 /* nudge anyone waiting on suspend queue */ 518 if (!pending) 519 wake_up(&md->wait); 520 } 521 522 /* 523 * Add the bio to the list of deferred io. 524 */ 525 static void queue_io(struct mapped_device *md, struct bio *bio) 526 { 527 down_write(&md->io_lock); 528 529 spin_lock_irq(&md->deferred_lock); 530 bio_list_add(&md->deferred, bio); 531 spin_unlock_irq(&md->deferred_lock); 532 533 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) 534 queue_work(md->wq, &md->work); 535 536 up_write(&md->io_lock); 537 } 538 539 /* 540 * Everyone (including functions in this file), should use this 541 * function to access the md->map field, and make sure they call 542 * dm_table_put() when finished. 543 */ 544 struct dm_table *dm_get_live_table(struct mapped_device *md) 545 { 546 struct dm_table *t; 547 unsigned long flags; 548 549 read_lock_irqsave(&md->map_lock, flags); 550 t = md->map; 551 if (t) 552 dm_table_get(t); 553 read_unlock_irqrestore(&md->map_lock, flags); 554 555 return t; 556 } 557 558 /* 559 * Get the geometry associated with a dm device 560 */ 561 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 562 { 563 *geo = md->geometry; 564 565 return 0; 566 } 567 568 /* 569 * Set the geometry of a device. 570 */ 571 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 572 { 573 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 574 575 if (geo->start > sz) { 576 DMWARN("Start sector is beyond the geometry limits."); 577 return -EINVAL; 578 } 579 580 md->geometry = *geo; 581 582 return 0; 583 } 584 585 /*----------------------------------------------------------------- 586 * CRUD START: 587 * A more elegant soln is in the works that uses the queue 588 * merge fn, unfortunately there are a couple of changes to 589 * the block layer that I want to make for this. So in the 590 * interests of getting something for people to use I give 591 * you this clearly demarcated crap. 592 *---------------------------------------------------------------*/ 593 594 static int __noflush_suspending(struct mapped_device *md) 595 { 596 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 597 } 598 599 /* 600 * Decrements the number of outstanding ios that a bio has been 601 * cloned into, completing the original io if necc. 602 */ 603 static void dec_pending(struct dm_io *io, int error) 604 { 605 unsigned long flags; 606 int io_error; 607 struct bio *bio; 608 struct mapped_device *md = io->md; 609 610 /* Push-back supersedes any I/O errors */ 611 if (unlikely(error)) { 612 spin_lock_irqsave(&io->endio_lock, flags); 613 if (!(io->error > 0 && __noflush_suspending(md))) 614 io->error = error; 615 spin_unlock_irqrestore(&io->endio_lock, flags); 616 } 617 618 if (atomic_dec_and_test(&io->io_count)) { 619 if (io->error == DM_ENDIO_REQUEUE) { 620 /* 621 * Target requested pushing back the I/O. 622 */ 623 spin_lock_irqsave(&md->deferred_lock, flags); 624 if (__noflush_suspending(md)) { 625 if (!(io->bio->bi_rw & REQ_HARDBARRIER)) 626 bio_list_add_head(&md->deferred, 627 io->bio); 628 } else 629 /* noflush suspend was interrupted. */ 630 io->error = -EIO; 631 spin_unlock_irqrestore(&md->deferred_lock, flags); 632 } 633 634 io_error = io->error; 635 bio = io->bio; 636 637 if (bio->bi_rw & REQ_HARDBARRIER) { 638 /* 639 * There can be just one barrier request so we use 640 * a per-device variable for error reporting. 641 * Note that you can't touch the bio after end_io_acct 642 * 643 * We ignore -EOPNOTSUPP for empty flush reported by 644 * underlying devices. We assume that if the device 645 * doesn't support empty barriers, it doesn't need 646 * cache flushing commands. 647 */ 648 if (!md->barrier_error && 649 !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP)) 650 md->barrier_error = io_error; 651 end_io_acct(io); 652 free_io(md, io); 653 } else { 654 end_io_acct(io); 655 free_io(md, io); 656 657 if (io_error != DM_ENDIO_REQUEUE) { 658 trace_block_bio_complete(md->queue, bio); 659 660 bio_endio(bio, io_error); 661 } 662 } 663 } 664 } 665 666 static void clone_endio(struct bio *bio, int error) 667 { 668 int r = 0; 669 struct dm_target_io *tio = bio->bi_private; 670 struct dm_io *io = tio->io; 671 struct mapped_device *md = tio->io->md; 672 dm_endio_fn endio = tio->ti->type->end_io; 673 674 if (!bio_flagged(bio, BIO_UPTODATE) && !error) 675 error = -EIO; 676 677 if (endio) { 678 r = endio(tio->ti, bio, error, &tio->info); 679 if (r < 0 || r == DM_ENDIO_REQUEUE) 680 /* 681 * error and requeue request are handled 682 * in dec_pending(). 683 */ 684 error = r; 685 else if (r == DM_ENDIO_INCOMPLETE) 686 /* The target will handle the io */ 687 return; 688 else if (r) { 689 DMWARN("unimplemented target endio return value: %d", r); 690 BUG(); 691 } 692 } 693 694 /* 695 * Store md for cleanup instead of tio which is about to get freed. 696 */ 697 bio->bi_private = md->bs; 698 699 free_tio(md, tio); 700 bio_put(bio); 701 dec_pending(io, error); 702 } 703 704 /* 705 * Partial completion handling for request-based dm 706 */ 707 static void end_clone_bio(struct bio *clone, int error) 708 { 709 struct dm_rq_clone_bio_info *info = clone->bi_private; 710 struct dm_rq_target_io *tio = info->tio; 711 struct bio *bio = info->orig; 712 unsigned int nr_bytes = info->orig->bi_size; 713 714 bio_put(clone); 715 716 if (tio->error) 717 /* 718 * An error has already been detected on the request. 719 * Once error occurred, just let clone->end_io() handle 720 * the remainder. 721 */ 722 return; 723 else if (error) { 724 /* 725 * Don't notice the error to the upper layer yet. 726 * The error handling decision is made by the target driver, 727 * when the request is completed. 728 */ 729 tio->error = error; 730 return; 731 } 732 733 /* 734 * I/O for the bio successfully completed. 735 * Notice the data completion to the upper layer. 736 */ 737 738 /* 739 * bios are processed from the head of the list. 740 * So the completing bio should always be rq->bio. 741 * If it's not, something wrong is happening. 742 */ 743 if (tio->orig->bio != bio) 744 DMERR("bio completion is going in the middle of the request"); 745 746 /* 747 * Update the original request. 748 * Do not use blk_end_request() here, because it may complete 749 * the original request before the clone, and break the ordering. 750 */ 751 blk_update_request(tio->orig, 0, nr_bytes); 752 } 753 754 static void store_barrier_error(struct mapped_device *md, int error) 755 { 756 unsigned long flags; 757 758 spin_lock_irqsave(&md->barrier_error_lock, flags); 759 /* 760 * Basically, the first error is taken, but: 761 * -EOPNOTSUPP supersedes any I/O error. 762 * Requeue request supersedes any I/O error but -EOPNOTSUPP. 763 */ 764 if (!md->barrier_error || error == -EOPNOTSUPP || 765 (md->barrier_error != -EOPNOTSUPP && 766 error == DM_ENDIO_REQUEUE)) 767 md->barrier_error = error; 768 spin_unlock_irqrestore(&md->barrier_error_lock, flags); 769 } 770 771 /* 772 * Don't touch any member of the md after calling this function because 773 * the md may be freed in dm_put() at the end of this function. 774 * Or do dm_get() before calling this function and dm_put() later. 775 */ 776 static void rq_completed(struct mapped_device *md, int rw, int run_queue) 777 { 778 atomic_dec(&md->pending[rw]); 779 780 /* nudge anyone waiting on suspend queue */ 781 if (!md_in_flight(md)) 782 wake_up(&md->wait); 783 784 if (run_queue) 785 blk_run_queue(md->queue); 786 787 /* 788 * dm_put() must be at the end of this function. See the comment above 789 */ 790 dm_put(md); 791 } 792 793 static void free_rq_clone(struct request *clone) 794 { 795 struct dm_rq_target_io *tio = clone->end_io_data; 796 797 blk_rq_unprep_clone(clone); 798 free_rq_tio(tio); 799 } 800 801 /* 802 * Complete the clone and the original request. 803 * Must be called without queue lock. 804 */ 805 static void dm_end_request(struct request *clone, int error) 806 { 807 int rw = rq_data_dir(clone); 808 int run_queue = 1; 809 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER; 810 struct dm_rq_target_io *tio = clone->end_io_data; 811 struct mapped_device *md = tio->md; 812 struct request *rq = tio->orig; 813 814 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) { 815 rq->errors = clone->errors; 816 rq->resid_len = clone->resid_len; 817 818 if (rq->sense) 819 /* 820 * We are using the sense buffer of the original 821 * request. 822 * So setting the length of the sense data is enough. 823 */ 824 rq->sense_len = clone->sense_len; 825 } 826 827 free_rq_clone(clone); 828 829 if (unlikely(is_barrier)) { 830 if (unlikely(error)) 831 store_barrier_error(md, error); 832 run_queue = 0; 833 } else 834 blk_end_request_all(rq, error); 835 836 rq_completed(md, rw, run_queue); 837 } 838 839 static void dm_unprep_request(struct request *rq) 840 { 841 struct request *clone = rq->special; 842 843 rq->special = NULL; 844 rq->cmd_flags &= ~REQ_DONTPREP; 845 846 free_rq_clone(clone); 847 } 848 849 /* 850 * Requeue the original request of a clone. 851 */ 852 void dm_requeue_unmapped_request(struct request *clone) 853 { 854 int rw = rq_data_dir(clone); 855 struct dm_rq_target_io *tio = clone->end_io_data; 856 struct mapped_device *md = tio->md; 857 struct request *rq = tio->orig; 858 struct request_queue *q = rq->q; 859 unsigned long flags; 860 861 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) { 862 /* 863 * Barrier clones share an original request. 864 * Leave it to dm_end_request(), which handles this special 865 * case. 866 */ 867 dm_end_request(clone, DM_ENDIO_REQUEUE); 868 return; 869 } 870 871 dm_unprep_request(rq); 872 873 spin_lock_irqsave(q->queue_lock, flags); 874 if (elv_queue_empty(q)) 875 blk_plug_device(q); 876 blk_requeue_request(q, rq); 877 spin_unlock_irqrestore(q->queue_lock, flags); 878 879 rq_completed(md, rw, 0); 880 } 881 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); 882 883 static void __stop_queue(struct request_queue *q) 884 { 885 blk_stop_queue(q); 886 } 887 888 static void stop_queue(struct request_queue *q) 889 { 890 unsigned long flags; 891 892 spin_lock_irqsave(q->queue_lock, flags); 893 __stop_queue(q); 894 spin_unlock_irqrestore(q->queue_lock, flags); 895 } 896 897 static void __start_queue(struct request_queue *q) 898 { 899 if (blk_queue_stopped(q)) 900 blk_start_queue(q); 901 } 902 903 static void start_queue(struct request_queue *q) 904 { 905 unsigned long flags; 906 907 spin_lock_irqsave(q->queue_lock, flags); 908 __start_queue(q); 909 spin_unlock_irqrestore(q->queue_lock, flags); 910 } 911 912 static void dm_done(struct request *clone, int error, bool mapped) 913 { 914 int r = error; 915 struct dm_rq_target_io *tio = clone->end_io_data; 916 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io; 917 918 if (mapped && rq_end_io) 919 r = rq_end_io(tio->ti, clone, error, &tio->info); 920 921 if (r <= 0) 922 /* The target wants to complete the I/O */ 923 dm_end_request(clone, r); 924 else if (r == DM_ENDIO_INCOMPLETE) 925 /* The target will handle the I/O */ 926 return; 927 else if (r == DM_ENDIO_REQUEUE) 928 /* The target wants to requeue the I/O */ 929 dm_requeue_unmapped_request(clone); 930 else { 931 DMWARN("unimplemented target endio return value: %d", r); 932 BUG(); 933 } 934 } 935 936 /* 937 * Request completion handler for request-based dm 938 */ 939 static void dm_softirq_done(struct request *rq) 940 { 941 bool mapped = true; 942 struct request *clone = rq->completion_data; 943 struct dm_rq_target_io *tio = clone->end_io_data; 944 945 if (rq->cmd_flags & REQ_FAILED) 946 mapped = false; 947 948 dm_done(clone, tio->error, mapped); 949 } 950 951 /* 952 * Complete the clone and the original request with the error status 953 * through softirq context. 954 */ 955 static void dm_complete_request(struct request *clone, int error) 956 { 957 struct dm_rq_target_io *tio = clone->end_io_data; 958 struct request *rq = tio->orig; 959 960 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) { 961 /* 962 * Barrier clones share an original request. So can't use 963 * softirq_done with the original. 964 * Pass the clone to dm_done() directly in this special case. 965 * It is safe (even if clone->q->queue_lock is held here) 966 * because there is no I/O dispatching during the completion 967 * of barrier clone. 968 */ 969 dm_done(clone, error, true); 970 return; 971 } 972 973 tio->error = error; 974 rq->completion_data = clone; 975 blk_complete_request(rq); 976 } 977 978 /* 979 * Complete the not-mapped clone and the original request with the error status 980 * through softirq context. 981 * Target's rq_end_io() function isn't called. 982 * This may be used when the target's map_rq() function fails. 983 */ 984 void dm_kill_unmapped_request(struct request *clone, int error) 985 { 986 struct dm_rq_target_io *tio = clone->end_io_data; 987 struct request *rq = tio->orig; 988 989 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) { 990 /* 991 * Barrier clones share an original request. 992 * Leave it to dm_end_request(), which handles this special 993 * case. 994 */ 995 BUG_ON(error > 0); 996 dm_end_request(clone, error); 997 return; 998 } 999 1000 rq->cmd_flags |= REQ_FAILED; 1001 dm_complete_request(clone, error); 1002 } 1003 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); 1004 1005 /* 1006 * Called with the queue lock held 1007 */ 1008 static void end_clone_request(struct request *clone, int error) 1009 { 1010 /* 1011 * For just cleaning up the information of the queue in which 1012 * the clone was dispatched. 1013 * The clone is *NOT* freed actually here because it is alloced from 1014 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. 1015 */ 1016 __blk_put_request(clone->q, clone); 1017 1018 /* 1019 * Actual request completion is done in a softirq context which doesn't 1020 * hold the queue lock. Otherwise, deadlock could occur because: 1021 * - another request may be submitted by the upper level driver 1022 * of the stacking during the completion 1023 * - the submission which requires queue lock may be done 1024 * against this queue 1025 */ 1026 dm_complete_request(clone, error); 1027 } 1028 1029 static sector_t max_io_len(struct mapped_device *md, 1030 sector_t sector, struct dm_target *ti) 1031 { 1032 sector_t offset = sector - ti->begin; 1033 sector_t len = ti->len - offset; 1034 1035 /* 1036 * Does the target need to split even further ? 1037 */ 1038 if (ti->split_io) { 1039 sector_t boundary; 1040 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 1041 - offset; 1042 if (len > boundary) 1043 len = boundary; 1044 } 1045 1046 return len; 1047 } 1048 1049 static void __map_bio(struct dm_target *ti, struct bio *clone, 1050 struct dm_target_io *tio) 1051 { 1052 int r; 1053 sector_t sector; 1054 struct mapped_device *md; 1055 1056 clone->bi_end_io = clone_endio; 1057 clone->bi_private = tio; 1058 1059 /* 1060 * Map the clone. If r == 0 we don't need to do 1061 * anything, the target has assumed ownership of 1062 * this io. 1063 */ 1064 atomic_inc(&tio->io->io_count); 1065 sector = clone->bi_sector; 1066 r = ti->type->map(ti, clone, &tio->info); 1067 if (r == DM_MAPIO_REMAPPED) { 1068 /* the bio has been remapped so dispatch it */ 1069 1070 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, 1071 tio->io->bio->bi_bdev->bd_dev, sector); 1072 1073 generic_make_request(clone); 1074 } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 1075 /* error the io and bail out, or requeue it if needed */ 1076 md = tio->io->md; 1077 dec_pending(tio->io, r); 1078 /* 1079 * Store bio_set for cleanup. 1080 */ 1081 clone->bi_private = md->bs; 1082 bio_put(clone); 1083 free_tio(md, tio); 1084 } else if (r) { 1085 DMWARN("unimplemented target map return value: %d", r); 1086 BUG(); 1087 } 1088 } 1089 1090 struct clone_info { 1091 struct mapped_device *md; 1092 struct dm_table *map; 1093 struct bio *bio; 1094 struct dm_io *io; 1095 sector_t sector; 1096 sector_t sector_count; 1097 unsigned short idx; 1098 }; 1099 1100 static void dm_bio_destructor(struct bio *bio) 1101 { 1102 struct bio_set *bs = bio->bi_private; 1103 1104 bio_free(bio, bs); 1105 } 1106 1107 /* 1108 * Creates a little bio that is just does part of a bvec. 1109 */ 1110 static struct bio *split_bvec(struct bio *bio, sector_t sector, 1111 unsigned short idx, unsigned int offset, 1112 unsigned int len, struct bio_set *bs) 1113 { 1114 struct bio *clone; 1115 struct bio_vec *bv = bio->bi_io_vec + idx; 1116 1117 clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 1118 clone->bi_destructor = dm_bio_destructor; 1119 *clone->bi_io_vec = *bv; 1120 1121 clone->bi_sector = sector; 1122 clone->bi_bdev = bio->bi_bdev; 1123 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER; 1124 clone->bi_vcnt = 1; 1125 clone->bi_size = to_bytes(len); 1126 clone->bi_io_vec->bv_offset = offset; 1127 clone->bi_io_vec->bv_len = clone->bi_size; 1128 clone->bi_flags |= 1 << BIO_CLONED; 1129 1130 if (bio_integrity(bio)) { 1131 bio_integrity_clone(clone, bio, GFP_NOIO, bs); 1132 bio_integrity_trim(clone, 1133 bio_sector_offset(bio, idx, offset), len); 1134 } 1135 1136 return clone; 1137 } 1138 1139 /* 1140 * Creates a bio that consists of range of complete bvecs. 1141 */ 1142 static struct bio *clone_bio(struct bio *bio, sector_t sector, 1143 unsigned short idx, unsigned short bv_count, 1144 unsigned int len, struct bio_set *bs) 1145 { 1146 struct bio *clone; 1147 1148 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 1149 __bio_clone(clone, bio); 1150 clone->bi_rw &= ~REQ_HARDBARRIER; 1151 clone->bi_destructor = dm_bio_destructor; 1152 clone->bi_sector = sector; 1153 clone->bi_idx = idx; 1154 clone->bi_vcnt = idx + bv_count; 1155 clone->bi_size = to_bytes(len); 1156 clone->bi_flags &= ~(1 << BIO_SEG_VALID); 1157 1158 if (bio_integrity(bio)) { 1159 bio_integrity_clone(clone, bio, GFP_NOIO, bs); 1160 1161 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) 1162 bio_integrity_trim(clone, 1163 bio_sector_offset(bio, idx, 0), len); 1164 } 1165 1166 return clone; 1167 } 1168 1169 static struct dm_target_io *alloc_tio(struct clone_info *ci, 1170 struct dm_target *ti) 1171 { 1172 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO); 1173 1174 tio->io = ci->io; 1175 tio->ti = ti; 1176 memset(&tio->info, 0, sizeof(tio->info)); 1177 1178 return tio; 1179 } 1180 1181 static void __flush_target(struct clone_info *ci, struct dm_target *ti, 1182 unsigned flush_nr) 1183 { 1184 struct dm_target_io *tio = alloc_tio(ci, ti); 1185 struct bio *clone; 1186 1187 tio->info.flush_request = flush_nr; 1188 1189 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); 1190 __bio_clone(clone, ci->bio); 1191 clone->bi_destructor = dm_bio_destructor; 1192 1193 __map_bio(ti, clone, tio); 1194 } 1195 1196 static int __clone_and_map_empty_barrier(struct clone_info *ci) 1197 { 1198 unsigned target_nr = 0, flush_nr; 1199 struct dm_target *ti; 1200 1201 while ((ti = dm_table_get_target(ci->map, target_nr++))) 1202 for (flush_nr = 0; flush_nr < ti->num_flush_requests; 1203 flush_nr++) 1204 __flush_target(ci, ti, flush_nr); 1205 1206 ci->sector_count = 0; 1207 1208 return 0; 1209 } 1210 1211 static int __clone_and_map(struct clone_info *ci) 1212 { 1213 struct bio *clone, *bio = ci->bio; 1214 struct dm_target *ti; 1215 sector_t len = 0, max; 1216 struct dm_target_io *tio; 1217 1218 if (unlikely(bio_empty_barrier(bio))) 1219 return __clone_and_map_empty_barrier(ci); 1220 1221 ti = dm_table_find_target(ci->map, ci->sector); 1222 if (!dm_target_is_valid(ti)) 1223 return -EIO; 1224 1225 max = max_io_len(ci->md, ci->sector, ti); 1226 1227 /* 1228 * Allocate a target io object. 1229 */ 1230 tio = alloc_tio(ci, ti); 1231 1232 if (ci->sector_count <= max) { 1233 /* 1234 * Optimise for the simple case where we can do all of 1235 * the remaining io with a single clone. 1236 */ 1237 clone = clone_bio(bio, ci->sector, ci->idx, 1238 bio->bi_vcnt - ci->idx, ci->sector_count, 1239 ci->md->bs); 1240 __map_bio(ti, clone, tio); 1241 ci->sector_count = 0; 1242 1243 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 1244 /* 1245 * There are some bvecs that don't span targets. 1246 * Do as many of these as possible. 1247 */ 1248 int i; 1249 sector_t remaining = max; 1250 sector_t bv_len; 1251 1252 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 1253 bv_len = to_sector(bio->bi_io_vec[i].bv_len); 1254 1255 if (bv_len > remaining) 1256 break; 1257 1258 remaining -= bv_len; 1259 len += bv_len; 1260 } 1261 1262 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 1263 ci->md->bs); 1264 __map_bio(ti, clone, tio); 1265 1266 ci->sector += len; 1267 ci->sector_count -= len; 1268 ci->idx = i; 1269 1270 } else { 1271 /* 1272 * Handle a bvec that must be split between two or more targets. 1273 */ 1274 struct bio_vec *bv = bio->bi_io_vec + ci->idx; 1275 sector_t remaining = to_sector(bv->bv_len); 1276 unsigned int offset = 0; 1277 1278 do { 1279 if (offset) { 1280 ti = dm_table_find_target(ci->map, ci->sector); 1281 if (!dm_target_is_valid(ti)) 1282 return -EIO; 1283 1284 max = max_io_len(ci->md, ci->sector, ti); 1285 1286 tio = alloc_tio(ci, ti); 1287 } 1288 1289 len = min(remaining, max); 1290 1291 clone = split_bvec(bio, ci->sector, ci->idx, 1292 bv->bv_offset + offset, len, 1293 ci->md->bs); 1294 1295 __map_bio(ti, clone, tio); 1296 1297 ci->sector += len; 1298 ci->sector_count -= len; 1299 offset += to_bytes(len); 1300 } while (remaining -= len); 1301 1302 ci->idx++; 1303 } 1304 1305 return 0; 1306 } 1307 1308 /* 1309 * Split the bio into several clones and submit it to targets. 1310 */ 1311 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) 1312 { 1313 struct clone_info ci; 1314 int error = 0; 1315 1316 ci.map = dm_get_live_table(md); 1317 if (unlikely(!ci.map)) { 1318 if (!(bio->bi_rw & REQ_HARDBARRIER)) 1319 bio_io_error(bio); 1320 else 1321 if (!md->barrier_error) 1322 md->barrier_error = -EIO; 1323 return; 1324 } 1325 1326 ci.md = md; 1327 ci.bio = bio; 1328 ci.io = alloc_io(md); 1329 ci.io->error = 0; 1330 atomic_set(&ci.io->io_count, 1); 1331 ci.io->bio = bio; 1332 ci.io->md = md; 1333 spin_lock_init(&ci.io->endio_lock); 1334 ci.sector = bio->bi_sector; 1335 ci.sector_count = bio_sectors(bio); 1336 if (unlikely(bio_empty_barrier(bio))) 1337 ci.sector_count = 1; 1338 ci.idx = bio->bi_idx; 1339 1340 start_io_acct(ci.io); 1341 while (ci.sector_count && !error) 1342 error = __clone_and_map(&ci); 1343 1344 /* drop the extra reference count */ 1345 dec_pending(ci.io, error); 1346 dm_table_put(ci.map); 1347 } 1348 /*----------------------------------------------------------------- 1349 * CRUD END 1350 *---------------------------------------------------------------*/ 1351 1352 static int dm_merge_bvec(struct request_queue *q, 1353 struct bvec_merge_data *bvm, 1354 struct bio_vec *biovec) 1355 { 1356 struct mapped_device *md = q->queuedata; 1357 struct dm_table *map = dm_get_live_table(md); 1358 struct dm_target *ti; 1359 sector_t max_sectors; 1360 int max_size = 0; 1361 1362 if (unlikely(!map)) 1363 goto out; 1364 1365 ti = dm_table_find_target(map, bvm->bi_sector); 1366 if (!dm_target_is_valid(ti)) 1367 goto out_table; 1368 1369 /* 1370 * Find maximum amount of I/O that won't need splitting 1371 */ 1372 max_sectors = min(max_io_len(md, bvm->bi_sector, ti), 1373 (sector_t) BIO_MAX_SECTORS); 1374 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1375 if (max_size < 0) 1376 max_size = 0; 1377 1378 /* 1379 * merge_bvec_fn() returns number of bytes 1380 * it can accept at this offset 1381 * max is precomputed maximal io size 1382 */ 1383 if (max_size && ti->type->merge) 1384 max_size = ti->type->merge(ti, bvm, biovec, max_size); 1385 /* 1386 * If the target doesn't support merge method and some of the devices 1387 * provided their merge_bvec method (we know this by looking at 1388 * queue_max_hw_sectors), then we can't allow bios with multiple vector 1389 * entries. So always set max_size to 0, and the code below allows 1390 * just one page. 1391 */ 1392 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 1393 1394 max_size = 0; 1395 1396 out_table: 1397 dm_table_put(map); 1398 1399 out: 1400 /* 1401 * Always allow an entire first page 1402 */ 1403 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1404 max_size = biovec->bv_len; 1405 1406 return max_size; 1407 } 1408 1409 /* 1410 * The request function that just remaps the bio built up by 1411 * dm_merge_bvec. 1412 */ 1413 static int _dm_request(struct request_queue *q, struct bio *bio) 1414 { 1415 int rw = bio_data_dir(bio); 1416 struct mapped_device *md = q->queuedata; 1417 int cpu; 1418 1419 down_read(&md->io_lock); 1420 1421 cpu = part_stat_lock(); 1422 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 1423 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 1424 part_stat_unlock(); 1425 1426 /* 1427 * If we're suspended or the thread is processing barriers 1428 * we have to queue this io for later. 1429 */ 1430 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) || 1431 unlikely(bio->bi_rw & REQ_HARDBARRIER)) { 1432 up_read(&md->io_lock); 1433 1434 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) && 1435 bio_rw(bio) == READA) { 1436 bio_io_error(bio); 1437 return 0; 1438 } 1439 1440 queue_io(md, bio); 1441 1442 return 0; 1443 } 1444 1445 __split_and_process_bio(md, bio); 1446 up_read(&md->io_lock); 1447 return 0; 1448 } 1449 1450 static int dm_make_request(struct request_queue *q, struct bio *bio) 1451 { 1452 struct mapped_device *md = q->queuedata; 1453 1454 return md->saved_make_request_fn(q, bio); /* call __make_request() */ 1455 } 1456 1457 static int dm_request_based(struct mapped_device *md) 1458 { 1459 return blk_queue_stackable(md->queue); 1460 } 1461 1462 static int dm_request(struct request_queue *q, struct bio *bio) 1463 { 1464 struct mapped_device *md = q->queuedata; 1465 1466 if (dm_request_based(md)) 1467 return dm_make_request(q, bio); 1468 1469 return _dm_request(q, bio); 1470 } 1471 1472 static bool dm_rq_is_flush_request(struct request *rq) 1473 { 1474 if (rq->cmd_flags & REQ_FLUSH) 1475 return true; 1476 else 1477 return false; 1478 } 1479 1480 void dm_dispatch_request(struct request *rq) 1481 { 1482 int r; 1483 1484 if (blk_queue_io_stat(rq->q)) 1485 rq->cmd_flags |= REQ_IO_STAT; 1486 1487 rq->start_time = jiffies; 1488 r = blk_insert_cloned_request(rq->q, rq); 1489 if (r) 1490 dm_complete_request(rq, r); 1491 } 1492 EXPORT_SYMBOL_GPL(dm_dispatch_request); 1493 1494 static void dm_rq_bio_destructor(struct bio *bio) 1495 { 1496 struct dm_rq_clone_bio_info *info = bio->bi_private; 1497 struct mapped_device *md = info->tio->md; 1498 1499 free_bio_info(info); 1500 bio_free(bio, md->bs); 1501 } 1502 1503 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1504 void *data) 1505 { 1506 struct dm_rq_target_io *tio = data; 1507 struct mapped_device *md = tio->md; 1508 struct dm_rq_clone_bio_info *info = alloc_bio_info(md); 1509 1510 if (!info) 1511 return -ENOMEM; 1512 1513 info->orig = bio_orig; 1514 info->tio = tio; 1515 bio->bi_end_io = end_clone_bio; 1516 bio->bi_private = info; 1517 bio->bi_destructor = dm_rq_bio_destructor; 1518 1519 return 0; 1520 } 1521 1522 static int setup_clone(struct request *clone, struct request *rq, 1523 struct dm_rq_target_io *tio) 1524 { 1525 int r; 1526 1527 if (dm_rq_is_flush_request(rq)) { 1528 blk_rq_init(NULL, clone); 1529 clone->cmd_type = REQ_TYPE_FS; 1530 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE); 1531 } else { 1532 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, 1533 dm_rq_bio_constructor, tio); 1534 if (r) 1535 return r; 1536 1537 clone->cmd = rq->cmd; 1538 clone->cmd_len = rq->cmd_len; 1539 clone->sense = rq->sense; 1540 clone->buffer = rq->buffer; 1541 } 1542 1543 clone->end_io = end_clone_request; 1544 clone->end_io_data = tio; 1545 1546 return 0; 1547 } 1548 1549 static struct request *clone_rq(struct request *rq, struct mapped_device *md, 1550 gfp_t gfp_mask) 1551 { 1552 struct request *clone; 1553 struct dm_rq_target_io *tio; 1554 1555 tio = alloc_rq_tio(md, gfp_mask); 1556 if (!tio) 1557 return NULL; 1558 1559 tio->md = md; 1560 tio->ti = NULL; 1561 tio->orig = rq; 1562 tio->error = 0; 1563 memset(&tio->info, 0, sizeof(tio->info)); 1564 1565 clone = &tio->clone; 1566 if (setup_clone(clone, rq, tio)) { 1567 /* -ENOMEM */ 1568 free_rq_tio(tio); 1569 return NULL; 1570 } 1571 1572 return clone; 1573 } 1574 1575 /* 1576 * Called with the queue lock held. 1577 */ 1578 static int dm_prep_fn(struct request_queue *q, struct request *rq) 1579 { 1580 struct mapped_device *md = q->queuedata; 1581 struct request *clone; 1582 1583 if (unlikely(dm_rq_is_flush_request(rq))) 1584 return BLKPREP_OK; 1585 1586 if (unlikely(rq->special)) { 1587 DMWARN("Already has something in rq->special."); 1588 return BLKPREP_KILL; 1589 } 1590 1591 clone = clone_rq(rq, md, GFP_ATOMIC); 1592 if (!clone) 1593 return BLKPREP_DEFER; 1594 1595 rq->special = clone; 1596 rq->cmd_flags |= REQ_DONTPREP; 1597 1598 return BLKPREP_OK; 1599 } 1600 1601 /* 1602 * Returns: 1603 * 0 : the request has been processed (not requeued) 1604 * !0 : the request has been requeued 1605 */ 1606 static int map_request(struct dm_target *ti, struct request *clone, 1607 struct mapped_device *md) 1608 { 1609 int r, requeued = 0; 1610 struct dm_rq_target_io *tio = clone->end_io_data; 1611 1612 /* 1613 * Hold the md reference here for the in-flight I/O. 1614 * We can't rely on the reference count by device opener, 1615 * because the device may be closed during the request completion 1616 * when all bios are completed. 1617 * See the comment in rq_completed() too. 1618 */ 1619 dm_get(md); 1620 1621 tio->ti = ti; 1622 r = ti->type->map_rq(ti, clone, &tio->info); 1623 switch (r) { 1624 case DM_MAPIO_SUBMITTED: 1625 /* The target has taken the I/O to submit by itself later */ 1626 break; 1627 case DM_MAPIO_REMAPPED: 1628 /* The target has remapped the I/O so dispatch it */ 1629 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), 1630 blk_rq_pos(tio->orig)); 1631 dm_dispatch_request(clone); 1632 break; 1633 case DM_MAPIO_REQUEUE: 1634 /* The target wants to requeue the I/O */ 1635 dm_requeue_unmapped_request(clone); 1636 requeued = 1; 1637 break; 1638 default: 1639 if (r > 0) { 1640 DMWARN("unimplemented target map return value: %d", r); 1641 BUG(); 1642 } 1643 1644 /* The target wants to complete the I/O */ 1645 dm_kill_unmapped_request(clone, r); 1646 break; 1647 } 1648 1649 return requeued; 1650 } 1651 1652 /* 1653 * q->request_fn for request-based dm. 1654 * Called with the queue lock held. 1655 */ 1656 static void dm_request_fn(struct request_queue *q) 1657 { 1658 struct mapped_device *md = q->queuedata; 1659 struct dm_table *map = dm_get_live_table(md); 1660 struct dm_target *ti; 1661 struct request *rq, *clone; 1662 1663 /* 1664 * For suspend, check blk_queue_stopped() and increment 1665 * ->pending within a single queue_lock not to increment the 1666 * number of in-flight I/Os after the queue is stopped in 1667 * dm_suspend(). 1668 */ 1669 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) { 1670 rq = blk_peek_request(q); 1671 if (!rq) 1672 goto plug_and_out; 1673 1674 if (unlikely(dm_rq_is_flush_request(rq))) { 1675 BUG_ON(md->flush_request); 1676 md->flush_request = rq; 1677 blk_start_request(rq); 1678 queue_work(md->wq, &md->barrier_work); 1679 goto out; 1680 } 1681 1682 ti = dm_table_find_target(map, blk_rq_pos(rq)); 1683 if (ti->type->busy && ti->type->busy(ti)) 1684 goto plug_and_out; 1685 1686 blk_start_request(rq); 1687 clone = rq->special; 1688 atomic_inc(&md->pending[rq_data_dir(clone)]); 1689 1690 spin_unlock(q->queue_lock); 1691 if (map_request(ti, clone, md)) 1692 goto requeued; 1693 1694 spin_lock_irq(q->queue_lock); 1695 } 1696 1697 goto out; 1698 1699 requeued: 1700 spin_lock_irq(q->queue_lock); 1701 1702 plug_and_out: 1703 if (!elv_queue_empty(q)) 1704 /* Some requests still remain, retry later */ 1705 blk_plug_device(q); 1706 1707 out: 1708 dm_table_put(map); 1709 1710 return; 1711 } 1712 1713 int dm_underlying_device_busy(struct request_queue *q) 1714 { 1715 return blk_lld_busy(q); 1716 } 1717 EXPORT_SYMBOL_GPL(dm_underlying_device_busy); 1718 1719 static int dm_lld_busy(struct request_queue *q) 1720 { 1721 int r; 1722 struct mapped_device *md = q->queuedata; 1723 struct dm_table *map = dm_get_live_table(md); 1724 1725 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) 1726 r = 1; 1727 else 1728 r = dm_table_any_busy_target(map); 1729 1730 dm_table_put(map); 1731 1732 return r; 1733 } 1734 1735 static void dm_unplug_all(struct request_queue *q) 1736 { 1737 struct mapped_device *md = q->queuedata; 1738 struct dm_table *map = dm_get_live_table(md); 1739 1740 if (map) { 1741 if (dm_request_based(md)) 1742 generic_unplug_device(q); 1743 1744 dm_table_unplug_all(map); 1745 dm_table_put(map); 1746 } 1747 } 1748 1749 static int dm_any_congested(void *congested_data, int bdi_bits) 1750 { 1751 int r = bdi_bits; 1752 struct mapped_device *md = congested_data; 1753 struct dm_table *map; 1754 1755 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 1756 map = dm_get_live_table(md); 1757 if (map) { 1758 /* 1759 * Request-based dm cares about only own queue for 1760 * the query about congestion status of request_queue 1761 */ 1762 if (dm_request_based(md)) 1763 r = md->queue->backing_dev_info.state & 1764 bdi_bits; 1765 else 1766 r = dm_table_any_congested(map, bdi_bits); 1767 1768 dm_table_put(map); 1769 } 1770 } 1771 1772 return r; 1773 } 1774 1775 /*----------------------------------------------------------------- 1776 * An IDR is used to keep track of allocated minor numbers. 1777 *---------------------------------------------------------------*/ 1778 static DEFINE_IDR(_minor_idr); 1779 1780 static void free_minor(int minor) 1781 { 1782 spin_lock(&_minor_lock); 1783 idr_remove(&_minor_idr, minor); 1784 spin_unlock(&_minor_lock); 1785 } 1786 1787 /* 1788 * See if the device with a specific minor # is free. 1789 */ 1790 static int specific_minor(int minor) 1791 { 1792 int r, m; 1793 1794 if (minor >= (1 << MINORBITS)) 1795 return -EINVAL; 1796 1797 r = idr_pre_get(&_minor_idr, GFP_KERNEL); 1798 if (!r) 1799 return -ENOMEM; 1800 1801 spin_lock(&_minor_lock); 1802 1803 if (idr_find(&_minor_idr, minor)) { 1804 r = -EBUSY; 1805 goto out; 1806 } 1807 1808 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 1809 if (r) 1810 goto out; 1811 1812 if (m != minor) { 1813 idr_remove(&_minor_idr, m); 1814 r = -EBUSY; 1815 goto out; 1816 } 1817 1818 out: 1819 spin_unlock(&_minor_lock); 1820 return r; 1821 } 1822 1823 static int next_free_minor(int *minor) 1824 { 1825 int r, m; 1826 1827 r = idr_pre_get(&_minor_idr, GFP_KERNEL); 1828 if (!r) 1829 return -ENOMEM; 1830 1831 spin_lock(&_minor_lock); 1832 1833 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1834 if (r) 1835 goto out; 1836 1837 if (m >= (1 << MINORBITS)) { 1838 idr_remove(&_minor_idr, m); 1839 r = -ENOSPC; 1840 goto out; 1841 } 1842 1843 *minor = m; 1844 1845 out: 1846 spin_unlock(&_minor_lock); 1847 return r; 1848 } 1849 1850 static const struct block_device_operations dm_blk_dops; 1851 1852 static void dm_wq_work(struct work_struct *work); 1853 static void dm_rq_barrier_work(struct work_struct *work); 1854 1855 /* 1856 * Allocate and initialise a blank device with a given minor. 1857 */ 1858 static struct mapped_device *alloc_dev(int minor) 1859 { 1860 int r; 1861 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1862 void *old_md; 1863 1864 if (!md) { 1865 DMWARN("unable to allocate device, out of memory."); 1866 return NULL; 1867 } 1868 1869 if (!try_module_get(THIS_MODULE)) 1870 goto bad_module_get; 1871 1872 /* get a minor number for the dev */ 1873 if (minor == DM_ANY_MINOR) 1874 r = next_free_minor(&minor); 1875 else 1876 r = specific_minor(minor); 1877 if (r < 0) 1878 goto bad_minor; 1879 1880 init_rwsem(&md->io_lock); 1881 mutex_init(&md->suspend_lock); 1882 spin_lock_init(&md->deferred_lock); 1883 spin_lock_init(&md->barrier_error_lock); 1884 rwlock_init(&md->map_lock); 1885 atomic_set(&md->holders, 1); 1886 atomic_set(&md->open_count, 0); 1887 atomic_set(&md->event_nr, 0); 1888 atomic_set(&md->uevent_seq, 0); 1889 INIT_LIST_HEAD(&md->uevent_list); 1890 spin_lock_init(&md->uevent_lock); 1891 1892 md->queue = blk_init_queue(dm_request_fn, NULL); 1893 if (!md->queue) 1894 goto bad_queue; 1895 1896 /* 1897 * Request-based dm devices cannot be stacked on top of bio-based dm 1898 * devices. The type of this dm device has not been decided yet, 1899 * although we initialized the queue using blk_init_queue(). 1900 * The type is decided at the first table loading time. 1901 * To prevent problematic device stacking, clear the queue flag 1902 * for request stacking support until then. 1903 * 1904 * This queue is new, so no concurrency on the queue_flags. 1905 */ 1906 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 1907 md->saved_make_request_fn = md->queue->make_request_fn; 1908 md->queue->queuedata = md; 1909 md->queue->backing_dev_info.congested_fn = dm_any_congested; 1910 md->queue->backing_dev_info.congested_data = md; 1911 blk_queue_make_request(md->queue, dm_request); 1912 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 1913 md->queue->unplug_fn = dm_unplug_all; 1914 blk_queue_merge_bvec(md->queue, dm_merge_bvec); 1915 blk_queue_softirq_done(md->queue, dm_softirq_done); 1916 blk_queue_prep_rq(md->queue, dm_prep_fn); 1917 blk_queue_lld_busy(md->queue, dm_lld_busy); 1918 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH); 1919 1920 md->disk = alloc_disk(1); 1921 if (!md->disk) 1922 goto bad_disk; 1923 1924 atomic_set(&md->pending[0], 0); 1925 atomic_set(&md->pending[1], 0); 1926 init_waitqueue_head(&md->wait); 1927 INIT_WORK(&md->work, dm_wq_work); 1928 INIT_WORK(&md->barrier_work, dm_rq_barrier_work); 1929 init_waitqueue_head(&md->eventq); 1930 1931 md->disk->major = _major; 1932 md->disk->first_minor = minor; 1933 md->disk->fops = &dm_blk_dops; 1934 md->disk->queue = md->queue; 1935 md->disk->private_data = md; 1936 sprintf(md->disk->disk_name, "dm-%d", minor); 1937 add_disk(md->disk); 1938 format_dev_t(md->name, MKDEV(_major, minor)); 1939 1940 md->wq = create_singlethread_workqueue("kdmflush"); 1941 if (!md->wq) 1942 goto bad_thread; 1943 1944 md->bdev = bdget_disk(md->disk, 0); 1945 if (!md->bdev) 1946 goto bad_bdev; 1947 1948 /* Populate the mapping, nobody knows we exist yet */ 1949 spin_lock(&_minor_lock); 1950 old_md = idr_replace(&_minor_idr, md, minor); 1951 spin_unlock(&_minor_lock); 1952 1953 BUG_ON(old_md != MINOR_ALLOCED); 1954 1955 return md; 1956 1957 bad_bdev: 1958 destroy_workqueue(md->wq); 1959 bad_thread: 1960 del_gendisk(md->disk); 1961 put_disk(md->disk); 1962 bad_disk: 1963 blk_cleanup_queue(md->queue); 1964 bad_queue: 1965 free_minor(minor); 1966 bad_minor: 1967 module_put(THIS_MODULE); 1968 bad_module_get: 1969 kfree(md); 1970 return NULL; 1971 } 1972 1973 static void unlock_fs(struct mapped_device *md); 1974 1975 static void free_dev(struct mapped_device *md) 1976 { 1977 int minor = MINOR(disk_devt(md->disk)); 1978 1979 unlock_fs(md); 1980 bdput(md->bdev); 1981 destroy_workqueue(md->wq); 1982 if (md->tio_pool) 1983 mempool_destroy(md->tio_pool); 1984 if (md->io_pool) 1985 mempool_destroy(md->io_pool); 1986 if (md->bs) 1987 bioset_free(md->bs); 1988 blk_integrity_unregister(md->disk); 1989 del_gendisk(md->disk); 1990 free_minor(minor); 1991 1992 spin_lock(&_minor_lock); 1993 md->disk->private_data = NULL; 1994 spin_unlock(&_minor_lock); 1995 1996 put_disk(md->disk); 1997 blk_cleanup_queue(md->queue); 1998 module_put(THIS_MODULE); 1999 kfree(md); 2000 } 2001 2002 static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 2003 { 2004 struct dm_md_mempools *p; 2005 2006 if (md->io_pool && md->tio_pool && md->bs) 2007 /* the md already has necessary mempools */ 2008 goto out; 2009 2010 p = dm_table_get_md_mempools(t); 2011 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); 2012 2013 md->io_pool = p->io_pool; 2014 p->io_pool = NULL; 2015 md->tio_pool = p->tio_pool; 2016 p->tio_pool = NULL; 2017 md->bs = p->bs; 2018 p->bs = NULL; 2019 2020 out: 2021 /* mempool bind completed, now no need any mempools in the table */ 2022 dm_table_free_md_mempools(t); 2023 } 2024 2025 /* 2026 * Bind a table to the device. 2027 */ 2028 static void event_callback(void *context) 2029 { 2030 unsigned long flags; 2031 LIST_HEAD(uevents); 2032 struct mapped_device *md = (struct mapped_device *) context; 2033 2034 spin_lock_irqsave(&md->uevent_lock, flags); 2035 list_splice_init(&md->uevent_list, &uevents); 2036 spin_unlock_irqrestore(&md->uevent_lock, flags); 2037 2038 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 2039 2040 atomic_inc(&md->event_nr); 2041 wake_up(&md->eventq); 2042 } 2043 2044 static void __set_size(struct mapped_device *md, sector_t size) 2045 { 2046 set_capacity(md->disk, size); 2047 2048 mutex_lock(&md->bdev->bd_inode->i_mutex); 2049 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 2050 mutex_unlock(&md->bdev->bd_inode->i_mutex); 2051 } 2052 2053 /* 2054 * Returns old map, which caller must destroy. 2055 */ 2056 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2057 struct queue_limits *limits) 2058 { 2059 struct dm_table *old_map; 2060 struct request_queue *q = md->queue; 2061 sector_t size; 2062 unsigned long flags; 2063 2064 size = dm_table_get_size(t); 2065 2066 /* 2067 * Wipe any geometry if the size of the table changed. 2068 */ 2069 if (size != get_capacity(md->disk)) 2070 memset(&md->geometry, 0, sizeof(md->geometry)); 2071 2072 __set_size(md, size); 2073 2074 dm_table_event_callback(t, event_callback, md); 2075 2076 /* 2077 * The queue hasn't been stopped yet, if the old table type wasn't 2078 * for request-based during suspension. So stop it to prevent 2079 * I/O mapping before resume. 2080 * This must be done before setting the queue restrictions, 2081 * because request-based dm may be run just after the setting. 2082 */ 2083 if (dm_table_request_based(t) && !blk_queue_stopped(q)) 2084 stop_queue(q); 2085 2086 __bind_mempools(md, t); 2087 2088 write_lock_irqsave(&md->map_lock, flags); 2089 old_map = md->map; 2090 md->map = t; 2091 dm_table_set_restrictions(t, q, limits); 2092 write_unlock_irqrestore(&md->map_lock, flags); 2093 2094 return old_map; 2095 } 2096 2097 /* 2098 * Returns unbound table for the caller to free. 2099 */ 2100 static struct dm_table *__unbind(struct mapped_device *md) 2101 { 2102 struct dm_table *map = md->map; 2103 unsigned long flags; 2104 2105 if (!map) 2106 return NULL; 2107 2108 dm_table_event_callback(map, NULL, NULL); 2109 write_lock_irqsave(&md->map_lock, flags); 2110 md->map = NULL; 2111 write_unlock_irqrestore(&md->map_lock, flags); 2112 2113 return map; 2114 } 2115 2116 /* 2117 * Constructor for a new device. 2118 */ 2119 int dm_create(int minor, struct mapped_device **result) 2120 { 2121 struct mapped_device *md; 2122 2123 md = alloc_dev(minor); 2124 if (!md) 2125 return -ENXIO; 2126 2127 dm_sysfs_init(md); 2128 2129 *result = md; 2130 return 0; 2131 } 2132 2133 static struct mapped_device *dm_find_md(dev_t dev) 2134 { 2135 struct mapped_device *md; 2136 unsigned minor = MINOR(dev); 2137 2138 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 2139 return NULL; 2140 2141 spin_lock(&_minor_lock); 2142 2143 md = idr_find(&_minor_idr, minor); 2144 if (md && (md == MINOR_ALLOCED || 2145 (MINOR(disk_devt(dm_disk(md))) != minor) || 2146 dm_deleting_md(md) || 2147 test_bit(DMF_FREEING, &md->flags))) { 2148 md = NULL; 2149 goto out; 2150 } 2151 2152 out: 2153 spin_unlock(&_minor_lock); 2154 2155 return md; 2156 } 2157 2158 struct mapped_device *dm_get_md(dev_t dev) 2159 { 2160 struct mapped_device *md = dm_find_md(dev); 2161 2162 if (md) 2163 dm_get(md); 2164 2165 return md; 2166 } 2167 2168 void *dm_get_mdptr(struct mapped_device *md) 2169 { 2170 return md->interface_ptr; 2171 } 2172 2173 void dm_set_mdptr(struct mapped_device *md, void *ptr) 2174 { 2175 md->interface_ptr = ptr; 2176 } 2177 2178 void dm_get(struct mapped_device *md) 2179 { 2180 atomic_inc(&md->holders); 2181 BUG_ON(test_bit(DMF_FREEING, &md->flags)); 2182 } 2183 2184 const char *dm_device_name(struct mapped_device *md) 2185 { 2186 return md->name; 2187 } 2188 EXPORT_SYMBOL_GPL(dm_device_name); 2189 2190 static void __dm_destroy(struct mapped_device *md, bool wait) 2191 { 2192 struct dm_table *map; 2193 2194 might_sleep(); 2195 2196 spin_lock(&_minor_lock); 2197 map = dm_get_live_table(md); 2198 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2199 set_bit(DMF_FREEING, &md->flags); 2200 spin_unlock(&_minor_lock); 2201 2202 if (!dm_suspended_md(md)) { 2203 dm_table_presuspend_targets(map); 2204 dm_table_postsuspend_targets(map); 2205 } 2206 2207 /* 2208 * Rare, but there may be I/O requests still going to complete, 2209 * for example. Wait for all references to disappear. 2210 * No one should increment the reference count of the mapped_device, 2211 * after the mapped_device state becomes DMF_FREEING. 2212 */ 2213 if (wait) 2214 while (atomic_read(&md->holders)) 2215 msleep(1); 2216 else if (atomic_read(&md->holders)) 2217 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 2218 dm_device_name(md), atomic_read(&md->holders)); 2219 2220 dm_sysfs_exit(md); 2221 dm_table_put(map); 2222 dm_table_destroy(__unbind(md)); 2223 free_dev(md); 2224 } 2225 2226 void dm_destroy(struct mapped_device *md) 2227 { 2228 __dm_destroy(md, true); 2229 } 2230 2231 void dm_destroy_immediate(struct mapped_device *md) 2232 { 2233 __dm_destroy(md, false); 2234 } 2235 2236 void dm_put(struct mapped_device *md) 2237 { 2238 atomic_dec(&md->holders); 2239 } 2240 EXPORT_SYMBOL_GPL(dm_put); 2241 2242 static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 2243 { 2244 int r = 0; 2245 DECLARE_WAITQUEUE(wait, current); 2246 2247 dm_unplug_all(md->queue); 2248 2249 add_wait_queue(&md->wait, &wait); 2250 2251 while (1) { 2252 set_current_state(interruptible); 2253 2254 smp_mb(); 2255 if (!md_in_flight(md)) 2256 break; 2257 2258 if (interruptible == TASK_INTERRUPTIBLE && 2259 signal_pending(current)) { 2260 r = -EINTR; 2261 break; 2262 } 2263 2264 io_schedule(); 2265 } 2266 set_current_state(TASK_RUNNING); 2267 2268 remove_wait_queue(&md->wait, &wait); 2269 2270 return r; 2271 } 2272 2273 static void dm_flush(struct mapped_device *md) 2274 { 2275 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2276 2277 bio_init(&md->barrier_bio); 2278 md->barrier_bio.bi_bdev = md->bdev; 2279 md->barrier_bio.bi_rw = WRITE_BARRIER; 2280 __split_and_process_bio(md, &md->barrier_bio); 2281 2282 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2283 } 2284 2285 static void process_barrier(struct mapped_device *md, struct bio *bio) 2286 { 2287 md->barrier_error = 0; 2288 2289 dm_flush(md); 2290 2291 if (!bio_empty_barrier(bio)) { 2292 __split_and_process_bio(md, bio); 2293 /* 2294 * If the request isn't supported, don't waste time with 2295 * the second flush. 2296 */ 2297 if (md->barrier_error != -EOPNOTSUPP) 2298 dm_flush(md); 2299 } 2300 2301 if (md->barrier_error != DM_ENDIO_REQUEUE) 2302 bio_endio(bio, md->barrier_error); 2303 else { 2304 spin_lock_irq(&md->deferred_lock); 2305 bio_list_add_head(&md->deferred, bio); 2306 spin_unlock_irq(&md->deferred_lock); 2307 } 2308 } 2309 2310 /* 2311 * Process the deferred bios 2312 */ 2313 static void dm_wq_work(struct work_struct *work) 2314 { 2315 struct mapped_device *md = container_of(work, struct mapped_device, 2316 work); 2317 struct bio *c; 2318 2319 down_write(&md->io_lock); 2320 2321 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2322 spin_lock_irq(&md->deferred_lock); 2323 c = bio_list_pop(&md->deferred); 2324 spin_unlock_irq(&md->deferred_lock); 2325 2326 if (!c) { 2327 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 2328 break; 2329 } 2330 2331 up_write(&md->io_lock); 2332 2333 if (dm_request_based(md)) 2334 generic_make_request(c); 2335 else { 2336 if (c->bi_rw & REQ_HARDBARRIER) 2337 process_barrier(md, c); 2338 else 2339 __split_and_process_bio(md, c); 2340 } 2341 2342 down_write(&md->io_lock); 2343 } 2344 2345 up_write(&md->io_lock); 2346 } 2347 2348 static void dm_queue_flush(struct mapped_device *md) 2349 { 2350 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2351 smp_mb__after_clear_bit(); 2352 queue_work(md->wq, &md->work); 2353 } 2354 2355 static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr) 2356 { 2357 struct dm_rq_target_io *tio = clone->end_io_data; 2358 2359 tio->info.flush_request = flush_nr; 2360 } 2361 2362 /* Issue barrier requests to targets and wait for their completion. */ 2363 static int dm_rq_barrier(struct mapped_device *md) 2364 { 2365 int i, j; 2366 struct dm_table *map = dm_get_live_table(md); 2367 unsigned num_targets = dm_table_get_num_targets(map); 2368 struct dm_target *ti; 2369 struct request *clone; 2370 2371 md->barrier_error = 0; 2372 2373 for (i = 0; i < num_targets; i++) { 2374 ti = dm_table_get_target(map, i); 2375 for (j = 0; j < ti->num_flush_requests; j++) { 2376 clone = clone_rq(md->flush_request, md, GFP_NOIO); 2377 dm_rq_set_flush_nr(clone, j); 2378 atomic_inc(&md->pending[rq_data_dir(clone)]); 2379 map_request(ti, clone, md); 2380 } 2381 } 2382 2383 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2384 dm_table_put(map); 2385 2386 return md->barrier_error; 2387 } 2388 2389 static void dm_rq_barrier_work(struct work_struct *work) 2390 { 2391 int error; 2392 struct mapped_device *md = container_of(work, struct mapped_device, 2393 barrier_work); 2394 struct request_queue *q = md->queue; 2395 struct request *rq; 2396 unsigned long flags; 2397 2398 /* 2399 * Hold the md reference here and leave it at the last part so that 2400 * the md can't be deleted by device opener when the barrier request 2401 * completes. 2402 */ 2403 dm_get(md); 2404 2405 error = dm_rq_barrier(md); 2406 2407 rq = md->flush_request; 2408 md->flush_request = NULL; 2409 2410 if (error == DM_ENDIO_REQUEUE) { 2411 spin_lock_irqsave(q->queue_lock, flags); 2412 blk_requeue_request(q, rq); 2413 spin_unlock_irqrestore(q->queue_lock, flags); 2414 } else 2415 blk_end_request_all(rq, error); 2416 2417 blk_run_queue(q); 2418 2419 dm_put(md); 2420 } 2421 2422 /* 2423 * Swap in a new table, returning the old one for the caller to destroy. 2424 */ 2425 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 2426 { 2427 struct dm_table *map = ERR_PTR(-EINVAL); 2428 struct queue_limits limits; 2429 int r; 2430 2431 mutex_lock(&md->suspend_lock); 2432 2433 /* device must be suspended */ 2434 if (!dm_suspended_md(md)) 2435 goto out; 2436 2437 r = dm_calculate_queue_limits(table, &limits); 2438 if (r) { 2439 map = ERR_PTR(r); 2440 goto out; 2441 } 2442 2443 /* cannot change the device type, once a table is bound */ 2444 if (md->map && 2445 (dm_table_get_type(md->map) != dm_table_get_type(table))) { 2446 DMWARN("can't change the device type after a table is bound"); 2447 goto out; 2448 } 2449 2450 map = __bind(md, table, &limits); 2451 2452 out: 2453 mutex_unlock(&md->suspend_lock); 2454 return map; 2455 } 2456 2457 /* 2458 * Functions to lock and unlock any filesystem running on the 2459 * device. 2460 */ 2461 static int lock_fs(struct mapped_device *md) 2462 { 2463 int r; 2464 2465 WARN_ON(md->frozen_sb); 2466 2467 md->frozen_sb = freeze_bdev(md->bdev); 2468 if (IS_ERR(md->frozen_sb)) { 2469 r = PTR_ERR(md->frozen_sb); 2470 md->frozen_sb = NULL; 2471 return r; 2472 } 2473 2474 set_bit(DMF_FROZEN, &md->flags); 2475 2476 return 0; 2477 } 2478 2479 static void unlock_fs(struct mapped_device *md) 2480 { 2481 if (!test_bit(DMF_FROZEN, &md->flags)) 2482 return; 2483 2484 thaw_bdev(md->bdev, md->frozen_sb); 2485 md->frozen_sb = NULL; 2486 clear_bit(DMF_FROZEN, &md->flags); 2487 } 2488 2489 /* 2490 * We need to be able to change a mapping table under a mounted 2491 * filesystem. For example we might want to move some data in 2492 * the background. Before the table can be swapped with 2493 * dm_bind_table, dm_suspend must be called to flush any in 2494 * flight bios and ensure that any further io gets deferred. 2495 */ 2496 /* 2497 * Suspend mechanism in request-based dm. 2498 * 2499 * 1. Flush all I/Os by lock_fs() if needed. 2500 * 2. Stop dispatching any I/O by stopping the request_queue. 2501 * 3. Wait for all in-flight I/Os to be completed or requeued. 2502 * 2503 * To abort suspend, start the request_queue. 2504 */ 2505 int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2506 { 2507 struct dm_table *map = NULL; 2508 int r = 0; 2509 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 2510 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 2511 2512 mutex_lock(&md->suspend_lock); 2513 2514 if (dm_suspended_md(md)) { 2515 r = -EINVAL; 2516 goto out_unlock; 2517 } 2518 2519 map = dm_get_live_table(md); 2520 2521 /* 2522 * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 2523 * This flag is cleared before dm_suspend returns. 2524 */ 2525 if (noflush) 2526 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 2527 2528 /* This does not get reverted if there's an error later. */ 2529 dm_table_presuspend_targets(map); 2530 2531 /* 2532 * Flush I/O to the device. 2533 * Any I/O submitted after lock_fs() may not be flushed. 2534 * noflush takes precedence over do_lockfs. 2535 * (lock_fs() flushes I/Os and waits for them to complete.) 2536 */ 2537 if (!noflush && do_lockfs) { 2538 r = lock_fs(md); 2539 if (r) 2540 goto out; 2541 } 2542 2543 /* 2544 * Here we must make sure that no processes are submitting requests 2545 * to target drivers i.e. no one may be executing 2546 * __split_and_process_bio. This is called from dm_request and 2547 * dm_wq_work. 2548 * 2549 * To get all processes out of __split_and_process_bio in dm_request, 2550 * we take the write lock. To prevent any process from reentering 2551 * __split_and_process_bio from dm_request, we set 2552 * DMF_QUEUE_IO_TO_THREAD. 2553 * 2554 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND 2555 * and call flush_workqueue(md->wq). flush_workqueue will wait until 2556 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any 2557 * further calls to __split_and_process_bio from dm_wq_work. 2558 */ 2559 down_write(&md->io_lock); 2560 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2561 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 2562 up_write(&md->io_lock); 2563 2564 /* 2565 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which 2566 * can be kicked until md->queue is stopped. So stop md->queue before 2567 * flushing md->wq. 2568 */ 2569 if (dm_request_based(md)) 2570 stop_queue(md->queue); 2571 2572 flush_workqueue(md->wq); 2573 2574 /* 2575 * At this point no more requests are entering target request routines. 2576 * We call dm_wait_for_completion to wait for all existing requests 2577 * to finish. 2578 */ 2579 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); 2580 2581 down_write(&md->io_lock); 2582 if (noflush) 2583 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 2584 up_write(&md->io_lock); 2585 2586 /* were we interrupted ? */ 2587 if (r < 0) { 2588 dm_queue_flush(md); 2589 2590 if (dm_request_based(md)) 2591 start_queue(md->queue); 2592 2593 unlock_fs(md); 2594 goto out; /* pushback list is already flushed, so skip flush */ 2595 } 2596 2597 /* 2598 * If dm_wait_for_completion returned 0, the device is completely 2599 * quiescent now. There is no request-processing activity. All new 2600 * requests are being added to md->deferred list. 2601 */ 2602 2603 set_bit(DMF_SUSPENDED, &md->flags); 2604 2605 dm_table_postsuspend_targets(map); 2606 2607 out: 2608 dm_table_put(map); 2609 2610 out_unlock: 2611 mutex_unlock(&md->suspend_lock); 2612 return r; 2613 } 2614 2615 int dm_resume(struct mapped_device *md) 2616 { 2617 int r = -EINVAL; 2618 struct dm_table *map = NULL; 2619 2620 mutex_lock(&md->suspend_lock); 2621 if (!dm_suspended_md(md)) 2622 goto out; 2623 2624 map = dm_get_live_table(md); 2625 if (!map || !dm_table_get_size(map)) 2626 goto out; 2627 2628 r = dm_table_resume_targets(map); 2629 if (r) 2630 goto out; 2631 2632 dm_queue_flush(md); 2633 2634 /* 2635 * Flushing deferred I/Os must be done after targets are resumed 2636 * so that mapping of targets can work correctly. 2637 * Request-based dm is queueing the deferred I/Os in its request_queue. 2638 */ 2639 if (dm_request_based(md)) 2640 start_queue(md->queue); 2641 2642 unlock_fs(md); 2643 2644 clear_bit(DMF_SUSPENDED, &md->flags); 2645 2646 dm_table_unplug_all(map); 2647 r = 0; 2648 out: 2649 dm_table_put(map); 2650 mutex_unlock(&md->suspend_lock); 2651 2652 return r; 2653 } 2654 2655 /*----------------------------------------------------------------- 2656 * Event notification. 2657 *---------------------------------------------------------------*/ 2658 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 2659 unsigned cookie) 2660 { 2661 char udev_cookie[DM_COOKIE_LENGTH]; 2662 char *envp[] = { udev_cookie, NULL }; 2663 2664 if (!cookie) 2665 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 2666 else { 2667 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 2668 DM_COOKIE_ENV_VAR_NAME, cookie); 2669 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 2670 action, envp); 2671 } 2672 } 2673 2674 uint32_t dm_next_uevent_seq(struct mapped_device *md) 2675 { 2676 return atomic_add_return(1, &md->uevent_seq); 2677 } 2678 2679 uint32_t dm_get_event_nr(struct mapped_device *md) 2680 { 2681 return atomic_read(&md->event_nr); 2682 } 2683 2684 int dm_wait_event(struct mapped_device *md, int event_nr) 2685 { 2686 return wait_event_interruptible(md->eventq, 2687 (event_nr != atomic_read(&md->event_nr))); 2688 } 2689 2690 void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 2691 { 2692 unsigned long flags; 2693 2694 spin_lock_irqsave(&md->uevent_lock, flags); 2695 list_add(elist, &md->uevent_list); 2696 spin_unlock_irqrestore(&md->uevent_lock, flags); 2697 } 2698 2699 /* 2700 * The gendisk is only valid as long as you have a reference 2701 * count on 'md'. 2702 */ 2703 struct gendisk *dm_disk(struct mapped_device *md) 2704 { 2705 return md->disk; 2706 } 2707 2708 struct kobject *dm_kobject(struct mapped_device *md) 2709 { 2710 return &md->kobj; 2711 } 2712 2713 /* 2714 * struct mapped_device should not be exported outside of dm.c 2715 * so use this check to verify that kobj is part of md structure 2716 */ 2717 struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2718 { 2719 struct mapped_device *md; 2720 2721 md = container_of(kobj, struct mapped_device, kobj); 2722 if (&md->kobj != kobj) 2723 return NULL; 2724 2725 if (test_bit(DMF_FREEING, &md->flags) || 2726 dm_deleting_md(md)) 2727 return NULL; 2728 2729 dm_get(md); 2730 return md; 2731 } 2732 2733 int dm_suspended_md(struct mapped_device *md) 2734 { 2735 return test_bit(DMF_SUSPENDED, &md->flags); 2736 } 2737 2738 int dm_suspended(struct dm_target *ti) 2739 { 2740 return dm_suspended_md(dm_table_get_md(ti->table)); 2741 } 2742 EXPORT_SYMBOL_GPL(dm_suspended); 2743 2744 int dm_noflush_suspending(struct dm_target *ti) 2745 { 2746 return __noflush_suspending(dm_table_get_md(ti->table)); 2747 } 2748 EXPORT_SYMBOL_GPL(dm_noflush_suspending); 2749 2750 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type) 2751 { 2752 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); 2753 2754 if (!pools) 2755 return NULL; 2756 2757 pools->io_pool = (type == DM_TYPE_BIO_BASED) ? 2758 mempool_create_slab_pool(MIN_IOS, _io_cache) : 2759 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); 2760 if (!pools->io_pool) 2761 goto free_pools_and_out; 2762 2763 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ? 2764 mempool_create_slab_pool(MIN_IOS, _tio_cache) : 2765 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); 2766 if (!pools->tio_pool) 2767 goto free_io_pool_and_out; 2768 2769 pools->bs = (type == DM_TYPE_BIO_BASED) ? 2770 bioset_create(16, 0) : bioset_create(MIN_IOS, 0); 2771 if (!pools->bs) 2772 goto free_tio_pool_and_out; 2773 2774 return pools; 2775 2776 free_tio_pool_and_out: 2777 mempool_destroy(pools->tio_pool); 2778 2779 free_io_pool_and_out: 2780 mempool_destroy(pools->io_pool); 2781 2782 free_pools_and_out: 2783 kfree(pools); 2784 2785 return NULL; 2786 } 2787 2788 void dm_free_md_mempools(struct dm_md_mempools *pools) 2789 { 2790 if (!pools) 2791 return; 2792 2793 if (pools->io_pool) 2794 mempool_destroy(pools->io_pool); 2795 2796 if (pools->tio_pool) 2797 mempool_destroy(pools->tio_pool); 2798 2799 if (pools->bs) 2800 bioset_free(pools->bs); 2801 2802 kfree(pools); 2803 } 2804 2805 static const struct block_device_operations dm_blk_dops = { 2806 .open = dm_blk_open, 2807 .release = dm_blk_close, 2808 .ioctl = dm_blk_ioctl, 2809 .getgeo = dm_blk_getgeo, 2810 .owner = THIS_MODULE 2811 }; 2812 2813 EXPORT_SYMBOL(dm_get_mapinfo); 2814 2815 /* 2816 * module hooks 2817 */ 2818 module_init(dm_init); 2819 module_exit(dm_exit); 2820 2821 module_param(major, uint, 0); 2822 MODULE_PARM_DESC(major, "The major number of the device mapper"); 2823 MODULE_DESCRIPTION(DM_NAME " driver"); 2824 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 2825 MODULE_LICENSE("GPL"); 2826