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-core.h" 9 #include "dm-rq.h" 10 #include "dm-uevent.h" 11 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/mutex.h> 15 #include <linux/sched/mm.h> 16 #include <linux/sched/signal.h> 17 #include <linux/blkpg.h> 18 #include <linux/bio.h> 19 #include <linux/mempool.h> 20 #include <linux/dax.h> 21 #include <linux/slab.h> 22 #include <linux/idr.h> 23 #include <linux/uio.h> 24 #include <linux/hdreg.h> 25 #include <linux/delay.h> 26 #include <linux/wait.h> 27 #include <linux/pr.h> 28 #include <linux/refcount.h> 29 #include <linux/part_stat.h> 30 #include <linux/blk-crypto.h> 31 32 #define DM_MSG_PREFIX "core" 33 34 /* 35 * Cookies are numeric values sent with CHANGE and REMOVE 36 * uevents while resuming, removing or renaming the device. 37 */ 38 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 39 #define DM_COOKIE_LENGTH 24 40 41 static const char *_name = DM_NAME; 42 43 static unsigned int major = 0; 44 static unsigned int _major = 0; 45 46 static DEFINE_IDR(_minor_idr); 47 48 static DEFINE_SPINLOCK(_minor_lock); 49 50 static void do_deferred_remove(struct work_struct *w); 51 52 static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 53 54 static struct workqueue_struct *deferred_remove_workqueue; 55 56 atomic_t dm_global_event_nr = ATOMIC_INIT(0); 57 DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 58 59 void dm_issue_global_event(void) 60 { 61 atomic_inc(&dm_global_event_nr); 62 wake_up(&dm_global_eventq); 63 } 64 65 /* 66 * One of these is allocated (on-stack) per original bio. 67 */ 68 struct clone_info { 69 struct dm_table *map; 70 struct bio *bio; 71 struct dm_io *io; 72 sector_t sector; 73 unsigned sector_count; 74 }; 75 76 /* 77 * One of these is allocated per clone bio. 78 */ 79 #define DM_TIO_MAGIC 7282014 80 struct dm_target_io { 81 unsigned magic; 82 struct dm_io *io; 83 struct dm_target *ti; 84 unsigned target_bio_nr; 85 unsigned *len_ptr; 86 bool inside_dm_io; 87 struct bio clone; 88 }; 89 90 /* 91 * One of these is allocated per original bio. 92 * It contains the first clone used for that original. 93 */ 94 #define DM_IO_MAGIC 5191977 95 struct dm_io { 96 unsigned magic; 97 struct mapped_device *md; 98 blk_status_t status; 99 atomic_t io_count; 100 struct bio *orig_bio; 101 unsigned long start_time; 102 spinlock_t endio_lock; 103 struct dm_stats_aux stats_aux; 104 /* last member of dm_target_io is 'struct bio' */ 105 struct dm_target_io tio; 106 }; 107 108 void *dm_per_bio_data(struct bio *bio, size_t data_size) 109 { 110 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 111 if (!tio->inside_dm_io) 112 return (char *)bio - offsetof(struct dm_target_io, clone) - data_size; 113 return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size; 114 } 115 EXPORT_SYMBOL_GPL(dm_per_bio_data); 116 117 struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 118 { 119 struct dm_io *io = (struct dm_io *)((char *)data + data_size); 120 if (io->magic == DM_IO_MAGIC) 121 return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone)); 122 BUG_ON(io->magic != DM_TIO_MAGIC); 123 return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone)); 124 } 125 EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 126 127 unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 128 { 129 return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 130 } 131 EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 132 133 #define MINOR_ALLOCED ((void *)-1) 134 135 /* 136 * Bits for the md->flags field. 137 */ 138 #define DMF_BLOCK_IO_FOR_SUSPEND 0 139 #define DMF_SUSPENDED 1 140 #define DMF_FROZEN 2 141 #define DMF_FREEING 3 142 #define DMF_DELETING 4 143 #define DMF_NOFLUSH_SUSPENDING 5 144 #define DMF_DEFERRED_REMOVE 6 145 #define DMF_SUSPENDED_INTERNALLY 7 146 #define DMF_POST_SUSPENDING 8 147 148 #define DM_NUMA_NODE NUMA_NO_NODE 149 static int dm_numa_node = DM_NUMA_NODE; 150 151 /* 152 * For mempools pre-allocation at the table loading time. 153 */ 154 struct dm_md_mempools { 155 struct bio_set bs; 156 struct bio_set io_bs; 157 }; 158 159 struct table_device { 160 struct list_head list; 161 refcount_t count; 162 struct dm_dev dm_dev; 163 }; 164 165 /* 166 * Bio-based DM's mempools' reserved IOs set by the user. 167 */ 168 #define RESERVED_BIO_BASED_IOS 16 169 static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 170 171 static int __dm_get_module_param_int(int *module_param, int min, int max) 172 { 173 int param = READ_ONCE(*module_param); 174 int modified_param = 0; 175 bool modified = true; 176 177 if (param < min) 178 modified_param = min; 179 else if (param > max) 180 modified_param = max; 181 else 182 modified = false; 183 184 if (modified) { 185 (void)cmpxchg(module_param, param, modified_param); 186 param = modified_param; 187 } 188 189 return param; 190 } 191 192 unsigned __dm_get_module_param(unsigned *module_param, 193 unsigned def, unsigned max) 194 { 195 unsigned param = READ_ONCE(*module_param); 196 unsigned modified_param = 0; 197 198 if (!param) 199 modified_param = def; 200 else if (param > max) 201 modified_param = max; 202 203 if (modified_param) { 204 (void)cmpxchg(module_param, param, modified_param); 205 param = modified_param; 206 } 207 208 return param; 209 } 210 211 unsigned dm_get_reserved_bio_based_ios(void) 212 { 213 return __dm_get_module_param(&reserved_bio_based_ios, 214 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 215 } 216 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 217 218 static unsigned dm_get_numa_node(void) 219 { 220 return __dm_get_module_param_int(&dm_numa_node, 221 DM_NUMA_NODE, num_online_nodes() - 1); 222 } 223 224 static int __init local_init(void) 225 { 226 int r; 227 228 r = dm_uevent_init(); 229 if (r) 230 return r; 231 232 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 233 if (!deferred_remove_workqueue) { 234 r = -ENOMEM; 235 goto out_uevent_exit; 236 } 237 238 _major = major; 239 r = register_blkdev(_major, _name); 240 if (r < 0) 241 goto out_free_workqueue; 242 243 if (!_major) 244 _major = r; 245 246 return 0; 247 248 out_free_workqueue: 249 destroy_workqueue(deferred_remove_workqueue); 250 out_uevent_exit: 251 dm_uevent_exit(); 252 253 return r; 254 } 255 256 static void local_exit(void) 257 { 258 flush_scheduled_work(); 259 destroy_workqueue(deferred_remove_workqueue); 260 261 unregister_blkdev(_major, _name); 262 dm_uevent_exit(); 263 264 _major = 0; 265 266 DMINFO("cleaned up"); 267 } 268 269 static int (*_inits[])(void) __initdata = { 270 local_init, 271 dm_target_init, 272 dm_linear_init, 273 dm_stripe_init, 274 dm_io_init, 275 dm_kcopyd_init, 276 dm_interface_init, 277 dm_statistics_init, 278 }; 279 280 static void (*_exits[])(void) = { 281 local_exit, 282 dm_target_exit, 283 dm_linear_exit, 284 dm_stripe_exit, 285 dm_io_exit, 286 dm_kcopyd_exit, 287 dm_interface_exit, 288 dm_statistics_exit, 289 }; 290 291 static int __init dm_init(void) 292 { 293 const int count = ARRAY_SIZE(_inits); 294 295 int r, i; 296 297 for (i = 0; i < count; i++) { 298 r = _inits[i](); 299 if (r) 300 goto bad; 301 } 302 303 return 0; 304 305 bad: 306 while (i--) 307 _exits[i](); 308 309 return r; 310 } 311 312 static void __exit dm_exit(void) 313 { 314 int i = ARRAY_SIZE(_exits); 315 316 while (i--) 317 _exits[i](); 318 319 /* 320 * Should be empty by this point. 321 */ 322 idr_destroy(&_minor_idr); 323 } 324 325 /* 326 * Block device functions 327 */ 328 int dm_deleting_md(struct mapped_device *md) 329 { 330 return test_bit(DMF_DELETING, &md->flags); 331 } 332 333 static int dm_blk_open(struct block_device *bdev, fmode_t mode) 334 { 335 struct mapped_device *md; 336 337 spin_lock(&_minor_lock); 338 339 md = bdev->bd_disk->private_data; 340 if (!md) 341 goto out; 342 343 if (test_bit(DMF_FREEING, &md->flags) || 344 dm_deleting_md(md)) { 345 md = NULL; 346 goto out; 347 } 348 349 dm_get(md); 350 atomic_inc(&md->open_count); 351 out: 352 spin_unlock(&_minor_lock); 353 354 return md ? 0 : -ENXIO; 355 } 356 357 static void dm_blk_close(struct gendisk *disk, fmode_t mode) 358 { 359 struct mapped_device *md; 360 361 spin_lock(&_minor_lock); 362 363 md = disk->private_data; 364 if (WARN_ON(!md)) 365 goto out; 366 367 if (atomic_dec_and_test(&md->open_count) && 368 (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 369 queue_work(deferred_remove_workqueue, &deferred_remove_work); 370 371 dm_put(md); 372 out: 373 spin_unlock(&_minor_lock); 374 } 375 376 int dm_open_count(struct mapped_device *md) 377 { 378 return atomic_read(&md->open_count); 379 } 380 381 /* 382 * Guarantees nothing is using the device before it's deleted. 383 */ 384 int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 385 { 386 int r = 0; 387 388 spin_lock(&_minor_lock); 389 390 if (dm_open_count(md)) { 391 r = -EBUSY; 392 if (mark_deferred) 393 set_bit(DMF_DEFERRED_REMOVE, &md->flags); 394 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 395 r = -EEXIST; 396 else 397 set_bit(DMF_DELETING, &md->flags); 398 399 spin_unlock(&_minor_lock); 400 401 return r; 402 } 403 404 int dm_cancel_deferred_remove(struct mapped_device *md) 405 { 406 int r = 0; 407 408 spin_lock(&_minor_lock); 409 410 if (test_bit(DMF_DELETING, &md->flags)) 411 r = -EBUSY; 412 else 413 clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 414 415 spin_unlock(&_minor_lock); 416 417 return r; 418 } 419 420 static void do_deferred_remove(struct work_struct *w) 421 { 422 dm_deferred_remove(); 423 } 424 425 sector_t dm_get_size(struct mapped_device *md) 426 { 427 return get_capacity(md->disk); 428 } 429 430 struct request_queue *dm_get_md_queue(struct mapped_device *md) 431 { 432 return md->queue; 433 } 434 435 struct dm_stats *dm_get_stats(struct mapped_device *md) 436 { 437 return &md->stats; 438 } 439 440 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 441 { 442 struct mapped_device *md = bdev->bd_disk->private_data; 443 444 return dm_get_geometry(md, geo); 445 } 446 447 #ifdef CONFIG_BLK_DEV_ZONED 448 int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data) 449 { 450 struct dm_report_zones_args *args = data; 451 sector_t sector_diff = args->tgt->begin - args->start; 452 453 /* 454 * Ignore zones beyond the target range. 455 */ 456 if (zone->start >= args->start + args->tgt->len) 457 return 0; 458 459 /* 460 * Remap the start sector and write pointer position of the zone 461 * to match its position in the target range. 462 */ 463 zone->start += sector_diff; 464 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { 465 if (zone->cond == BLK_ZONE_COND_FULL) 466 zone->wp = zone->start + zone->len; 467 else if (zone->cond == BLK_ZONE_COND_EMPTY) 468 zone->wp = zone->start; 469 else 470 zone->wp += sector_diff; 471 } 472 473 args->next_sector = zone->start + zone->len; 474 return args->orig_cb(zone, args->zone_idx++, args->orig_data); 475 } 476 EXPORT_SYMBOL_GPL(dm_report_zones_cb); 477 478 static int dm_blk_report_zones(struct gendisk *disk, sector_t sector, 479 unsigned int nr_zones, report_zones_cb cb, void *data) 480 { 481 struct mapped_device *md = disk->private_data; 482 struct dm_table *map; 483 int srcu_idx, ret; 484 struct dm_report_zones_args args = { 485 .next_sector = sector, 486 .orig_data = data, 487 .orig_cb = cb, 488 }; 489 490 if (dm_suspended_md(md)) 491 return -EAGAIN; 492 493 map = dm_get_live_table(md, &srcu_idx); 494 if (!map) 495 return -EIO; 496 497 do { 498 struct dm_target *tgt; 499 500 tgt = dm_table_find_target(map, args.next_sector); 501 if (WARN_ON_ONCE(!tgt->type->report_zones)) { 502 ret = -EIO; 503 goto out; 504 } 505 506 args.tgt = tgt; 507 ret = tgt->type->report_zones(tgt, &args, nr_zones); 508 if (ret < 0) 509 goto out; 510 } while (args.zone_idx < nr_zones && 511 args.next_sector < get_capacity(disk)); 512 513 ret = args.zone_idx; 514 out: 515 dm_put_live_table(md, srcu_idx); 516 return ret; 517 } 518 #else 519 #define dm_blk_report_zones NULL 520 #endif /* CONFIG_BLK_DEV_ZONED */ 521 522 static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 523 struct block_device **bdev) 524 __acquires(md->io_barrier) 525 { 526 struct dm_target *tgt; 527 struct dm_table *map; 528 int r; 529 530 retry: 531 r = -ENOTTY; 532 map = dm_get_live_table(md, srcu_idx); 533 if (!map || !dm_table_get_size(map)) 534 return r; 535 536 /* We only support devices that have a single target */ 537 if (dm_table_get_num_targets(map) != 1) 538 return r; 539 540 tgt = dm_table_get_target(map, 0); 541 if (!tgt->type->prepare_ioctl) 542 return r; 543 544 if (dm_suspended_md(md)) 545 return -EAGAIN; 546 547 r = tgt->type->prepare_ioctl(tgt, bdev); 548 if (r == -ENOTCONN && !fatal_signal_pending(current)) { 549 dm_put_live_table(md, *srcu_idx); 550 msleep(10); 551 goto retry; 552 } 553 554 return r; 555 } 556 557 static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 558 __releases(md->io_barrier) 559 { 560 dm_put_live_table(md, srcu_idx); 561 } 562 563 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 564 unsigned int cmd, unsigned long arg) 565 { 566 struct mapped_device *md = bdev->bd_disk->private_data; 567 int r, srcu_idx; 568 569 r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 570 if (r < 0) 571 goto out; 572 573 if (r > 0) { 574 /* 575 * Target determined this ioctl is being issued against a 576 * subset of the parent bdev; require extra privileges. 577 */ 578 if (!capable(CAP_SYS_RAWIO)) { 579 DMWARN_LIMIT( 580 "%s: sending ioctl %x to DM device without required privilege.", 581 current->comm, cmd); 582 r = -ENOIOCTLCMD; 583 goto out; 584 } 585 } 586 587 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 588 out: 589 dm_unprepare_ioctl(md, srcu_idx); 590 return r; 591 } 592 593 static void start_io_acct(struct dm_io *io); 594 595 static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 596 { 597 struct dm_io *io; 598 struct dm_target_io *tio; 599 struct bio *clone; 600 601 clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs); 602 if (!clone) 603 return NULL; 604 605 tio = container_of(clone, struct dm_target_io, clone); 606 tio->inside_dm_io = true; 607 tio->io = NULL; 608 609 io = container_of(tio, struct dm_io, tio); 610 io->magic = DM_IO_MAGIC; 611 io->status = 0; 612 atomic_set(&io->io_count, 1); 613 io->orig_bio = bio; 614 io->md = md; 615 spin_lock_init(&io->endio_lock); 616 617 start_io_acct(io); 618 619 return io; 620 } 621 622 static void free_io(struct mapped_device *md, struct dm_io *io) 623 { 624 bio_put(&io->tio.clone); 625 } 626 627 static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, 628 unsigned target_bio_nr, gfp_t gfp_mask) 629 { 630 struct dm_target_io *tio; 631 632 if (!ci->io->tio.io) { 633 /* the dm_target_io embedded in ci->io is available */ 634 tio = &ci->io->tio; 635 } else { 636 struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs); 637 if (!clone) 638 return NULL; 639 640 tio = container_of(clone, struct dm_target_io, clone); 641 tio->inside_dm_io = false; 642 } 643 644 tio->magic = DM_TIO_MAGIC; 645 tio->io = ci->io; 646 tio->ti = ti; 647 tio->target_bio_nr = target_bio_nr; 648 649 return tio; 650 } 651 652 static void free_tio(struct dm_target_io *tio) 653 { 654 if (tio->inside_dm_io) 655 return; 656 bio_put(&tio->clone); 657 } 658 659 u64 dm_start_time_ns_from_clone(struct bio *bio) 660 { 661 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 662 struct dm_io *io = tio->io; 663 664 return jiffies_to_nsecs(io->start_time); 665 } 666 EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 667 668 static void start_io_acct(struct dm_io *io) 669 { 670 struct mapped_device *md = io->md; 671 struct bio *bio = io->orig_bio; 672 673 io->start_time = bio_start_io_acct(bio); 674 if (unlikely(dm_stats_used(&md->stats))) 675 dm_stats_account_io(&md->stats, bio_data_dir(bio), 676 bio->bi_iter.bi_sector, bio_sectors(bio), 677 false, 0, &io->stats_aux); 678 } 679 680 static void end_io_acct(struct dm_io *io) 681 { 682 struct mapped_device *md = io->md; 683 struct bio *bio = io->orig_bio; 684 unsigned long duration = jiffies - io->start_time; 685 686 bio_end_io_acct(bio, io->start_time); 687 688 if (unlikely(dm_stats_used(&md->stats))) 689 dm_stats_account_io(&md->stats, bio_data_dir(bio), 690 bio->bi_iter.bi_sector, bio_sectors(bio), 691 true, duration, &io->stats_aux); 692 693 /* nudge anyone waiting on suspend queue */ 694 if (unlikely(wq_has_sleeper(&md->wait))) 695 wake_up(&md->wait); 696 } 697 698 /* 699 * Add the bio to the list of deferred io. 700 */ 701 static void queue_io(struct mapped_device *md, struct bio *bio) 702 { 703 unsigned long flags; 704 705 spin_lock_irqsave(&md->deferred_lock, flags); 706 bio_list_add(&md->deferred, bio); 707 spin_unlock_irqrestore(&md->deferred_lock, flags); 708 queue_work(md->wq, &md->work); 709 } 710 711 /* 712 * Everyone (including functions in this file), should use this 713 * function to access the md->map field, and make sure they call 714 * dm_put_live_table() when finished. 715 */ 716 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 717 { 718 *srcu_idx = srcu_read_lock(&md->io_barrier); 719 720 return srcu_dereference(md->map, &md->io_barrier); 721 } 722 723 void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 724 { 725 srcu_read_unlock(&md->io_barrier, srcu_idx); 726 } 727 728 void dm_sync_table(struct mapped_device *md) 729 { 730 synchronize_srcu(&md->io_barrier); 731 synchronize_rcu_expedited(); 732 } 733 734 /* 735 * A fast alternative to dm_get_live_table/dm_put_live_table. 736 * The caller must not block between these two functions. 737 */ 738 static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 739 { 740 rcu_read_lock(); 741 return rcu_dereference(md->map); 742 } 743 744 static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 745 { 746 rcu_read_unlock(); 747 } 748 749 static char *_dm_claim_ptr = "I belong to device-mapper"; 750 751 /* 752 * Open a table device so we can use it as a map destination. 753 */ 754 static int open_table_device(struct table_device *td, dev_t dev, 755 struct mapped_device *md) 756 { 757 struct block_device *bdev; 758 759 int r; 760 761 BUG_ON(td->dm_dev.bdev); 762 763 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 764 if (IS_ERR(bdev)) 765 return PTR_ERR(bdev); 766 767 r = bd_link_disk_holder(bdev, dm_disk(md)); 768 if (r) { 769 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 770 return r; 771 } 772 773 td->dm_dev.bdev = bdev; 774 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 775 return 0; 776 } 777 778 /* 779 * Close a table device that we've been using. 780 */ 781 static void close_table_device(struct table_device *td, struct mapped_device *md) 782 { 783 if (!td->dm_dev.bdev) 784 return; 785 786 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 787 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 788 put_dax(td->dm_dev.dax_dev); 789 td->dm_dev.bdev = NULL; 790 td->dm_dev.dax_dev = NULL; 791 } 792 793 static struct table_device *find_table_device(struct list_head *l, dev_t dev, 794 fmode_t mode) 795 { 796 struct table_device *td; 797 798 list_for_each_entry(td, l, list) 799 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 800 return td; 801 802 return NULL; 803 } 804 805 int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 806 struct dm_dev **result) 807 { 808 int r; 809 struct table_device *td; 810 811 mutex_lock(&md->table_devices_lock); 812 td = find_table_device(&md->table_devices, dev, mode); 813 if (!td) { 814 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 815 if (!td) { 816 mutex_unlock(&md->table_devices_lock); 817 return -ENOMEM; 818 } 819 820 td->dm_dev.mode = mode; 821 td->dm_dev.bdev = NULL; 822 823 if ((r = open_table_device(td, dev, md))) { 824 mutex_unlock(&md->table_devices_lock); 825 kfree(td); 826 return r; 827 } 828 829 format_dev_t(td->dm_dev.name, dev); 830 831 refcount_set(&td->count, 1); 832 list_add(&td->list, &md->table_devices); 833 } else { 834 refcount_inc(&td->count); 835 } 836 mutex_unlock(&md->table_devices_lock); 837 838 *result = &td->dm_dev; 839 return 0; 840 } 841 EXPORT_SYMBOL_GPL(dm_get_table_device); 842 843 void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 844 { 845 struct table_device *td = container_of(d, struct table_device, dm_dev); 846 847 mutex_lock(&md->table_devices_lock); 848 if (refcount_dec_and_test(&td->count)) { 849 close_table_device(td, md); 850 list_del(&td->list); 851 kfree(td); 852 } 853 mutex_unlock(&md->table_devices_lock); 854 } 855 EXPORT_SYMBOL(dm_put_table_device); 856 857 static void free_table_devices(struct list_head *devices) 858 { 859 struct list_head *tmp, *next; 860 861 list_for_each_safe(tmp, next, devices) { 862 struct table_device *td = list_entry(tmp, struct table_device, list); 863 864 DMWARN("dm_destroy: %s still exists with %d references", 865 td->dm_dev.name, refcount_read(&td->count)); 866 kfree(td); 867 } 868 } 869 870 /* 871 * Get the geometry associated with a dm device 872 */ 873 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 874 { 875 *geo = md->geometry; 876 877 return 0; 878 } 879 880 /* 881 * Set the geometry of a device. 882 */ 883 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 884 { 885 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 886 887 if (geo->start > sz) { 888 DMWARN("Start sector is beyond the geometry limits."); 889 return -EINVAL; 890 } 891 892 md->geometry = *geo; 893 894 return 0; 895 } 896 897 static int __noflush_suspending(struct mapped_device *md) 898 { 899 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 900 } 901 902 /* 903 * Decrements the number of outstanding ios that a bio has been 904 * cloned into, completing the original io if necc. 905 */ 906 static void dec_pending(struct dm_io *io, blk_status_t error) 907 { 908 unsigned long flags; 909 blk_status_t io_error; 910 struct bio *bio; 911 struct mapped_device *md = io->md; 912 913 /* Push-back supersedes any I/O errors */ 914 if (unlikely(error)) { 915 spin_lock_irqsave(&io->endio_lock, flags); 916 if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) 917 io->status = error; 918 spin_unlock_irqrestore(&io->endio_lock, flags); 919 } 920 921 if (atomic_dec_and_test(&io->io_count)) { 922 if (io->status == BLK_STS_DM_REQUEUE) { 923 /* 924 * Target requested pushing back the I/O. 925 */ 926 spin_lock_irqsave(&md->deferred_lock, flags); 927 if (__noflush_suspending(md)) 928 /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 929 bio_list_add_head(&md->deferred, io->orig_bio); 930 else 931 /* noflush suspend was interrupted. */ 932 io->status = BLK_STS_IOERR; 933 spin_unlock_irqrestore(&md->deferred_lock, flags); 934 } 935 936 io_error = io->status; 937 bio = io->orig_bio; 938 end_io_acct(io); 939 free_io(md, io); 940 941 if (io_error == BLK_STS_DM_REQUEUE) 942 return; 943 944 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { 945 /* 946 * Preflush done for flush with data, reissue 947 * without REQ_PREFLUSH. 948 */ 949 bio->bi_opf &= ~REQ_PREFLUSH; 950 queue_io(md, bio); 951 } else { 952 /* done with normal IO or empty flush */ 953 if (io_error) 954 bio->bi_status = io_error; 955 bio_endio(bio); 956 } 957 } 958 } 959 960 void disable_discard(struct mapped_device *md) 961 { 962 struct queue_limits *limits = dm_get_queue_limits(md); 963 964 /* device doesn't really support DISCARD, disable it */ 965 limits->max_discard_sectors = 0; 966 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue); 967 } 968 969 void disable_write_same(struct mapped_device *md) 970 { 971 struct queue_limits *limits = dm_get_queue_limits(md); 972 973 /* device doesn't really support WRITE SAME, disable it */ 974 limits->max_write_same_sectors = 0; 975 } 976 977 void disable_write_zeroes(struct mapped_device *md) 978 { 979 struct queue_limits *limits = dm_get_queue_limits(md); 980 981 /* device doesn't really support WRITE ZEROES, disable it */ 982 limits->max_write_zeroes_sectors = 0; 983 } 984 985 static void clone_endio(struct bio *bio) 986 { 987 blk_status_t error = bio->bi_status; 988 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 989 struct dm_io *io = tio->io; 990 struct mapped_device *md = tio->io->md; 991 dm_endio_fn endio = tio->ti->type->end_io; 992 struct bio *orig_bio = io->orig_bio; 993 994 if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) { 995 if (bio_op(bio) == REQ_OP_DISCARD && 996 !bio->bi_disk->queue->limits.max_discard_sectors) 997 disable_discard(md); 998 else if (bio_op(bio) == REQ_OP_WRITE_SAME && 999 !bio->bi_disk->queue->limits.max_write_same_sectors) 1000 disable_write_same(md); 1001 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 1002 !bio->bi_disk->queue->limits.max_write_zeroes_sectors) 1003 disable_write_zeroes(md); 1004 } 1005 1006 /* 1007 * For zone-append bios get offset in zone of the written 1008 * sector and add that to the original bio sector pos. 1009 */ 1010 if (bio_op(orig_bio) == REQ_OP_ZONE_APPEND) { 1011 sector_t written_sector = bio->bi_iter.bi_sector; 1012 struct request_queue *q = orig_bio->bi_disk->queue; 1013 u64 mask = (u64)blk_queue_zone_sectors(q) - 1; 1014 1015 orig_bio->bi_iter.bi_sector += written_sector & mask; 1016 } 1017 1018 if (endio) { 1019 int r = endio(tio->ti, bio, &error); 1020 switch (r) { 1021 case DM_ENDIO_REQUEUE: 1022 error = BLK_STS_DM_REQUEUE; 1023 /*FALLTHRU*/ 1024 case DM_ENDIO_DONE: 1025 break; 1026 case DM_ENDIO_INCOMPLETE: 1027 /* The target will handle the io */ 1028 return; 1029 default: 1030 DMWARN("unimplemented target endio return value: %d", r); 1031 BUG(); 1032 } 1033 } 1034 1035 free_tio(tio); 1036 dec_pending(io, error); 1037 } 1038 1039 /* 1040 * Return maximum size of I/O possible at the supplied sector up to the current 1041 * target boundary. 1042 */ 1043 static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 1044 { 1045 sector_t target_offset = dm_target_offset(ti, sector); 1046 1047 return ti->len - target_offset; 1048 } 1049 1050 static sector_t max_io_len(sector_t sector, struct dm_target *ti) 1051 { 1052 sector_t len = max_io_len_target_boundary(sector, ti); 1053 sector_t offset, max_len; 1054 1055 /* 1056 * Does the target need to split even further? 1057 */ 1058 if (ti->max_io_len) { 1059 offset = dm_target_offset(ti, sector); 1060 if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 1061 max_len = sector_div(offset, ti->max_io_len); 1062 else 1063 max_len = offset & (ti->max_io_len - 1); 1064 max_len = ti->max_io_len - max_len; 1065 1066 if (len > max_len) 1067 len = max_len; 1068 } 1069 1070 return len; 1071 } 1072 1073 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1074 { 1075 if (len > UINT_MAX) { 1076 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1077 (unsigned long long)len, UINT_MAX); 1078 ti->error = "Maximum size of target IO is too large"; 1079 return -EINVAL; 1080 } 1081 1082 ti->max_io_len = (uint32_t) len; 1083 1084 return 0; 1085 } 1086 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1087 1088 static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1089 sector_t sector, int *srcu_idx) 1090 __acquires(md->io_barrier) 1091 { 1092 struct dm_table *map; 1093 struct dm_target *ti; 1094 1095 map = dm_get_live_table(md, srcu_idx); 1096 if (!map) 1097 return NULL; 1098 1099 ti = dm_table_find_target(map, sector); 1100 if (!ti) 1101 return NULL; 1102 1103 return ti; 1104 } 1105 1106 static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1107 long nr_pages, void **kaddr, pfn_t *pfn) 1108 { 1109 struct mapped_device *md = dax_get_private(dax_dev); 1110 sector_t sector = pgoff * PAGE_SECTORS; 1111 struct dm_target *ti; 1112 long len, ret = -EIO; 1113 int srcu_idx; 1114 1115 ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1116 1117 if (!ti) 1118 goto out; 1119 if (!ti->type->direct_access) 1120 goto out; 1121 len = max_io_len(sector, ti) / PAGE_SECTORS; 1122 if (len < 1) 1123 goto out; 1124 nr_pages = min(len, nr_pages); 1125 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1126 1127 out: 1128 dm_put_live_table(md, srcu_idx); 1129 1130 return ret; 1131 } 1132 1133 static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev, 1134 int blocksize, sector_t start, sector_t len) 1135 { 1136 struct mapped_device *md = dax_get_private(dax_dev); 1137 struct dm_table *map; 1138 int srcu_idx; 1139 bool ret; 1140 1141 map = dm_get_live_table(md, &srcu_idx); 1142 if (!map) 1143 return false; 1144 1145 ret = dm_table_supports_dax(map, device_supports_dax, &blocksize); 1146 1147 dm_put_live_table(md, srcu_idx); 1148 1149 return ret; 1150 } 1151 1152 static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, 1153 void *addr, size_t bytes, struct iov_iter *i) 1154 { 1155 struct mapped_device *md = dax_get_private(dax_dev); 1156 sector_t sector = pgoff * PAGE_SECTORS; 1157 struct dm_target *ti; 1158 long ret = 0; 1159 int srcu_idx; 1160 1161 ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1162 1163 if (!ti) 1164 goto out; 1165 if (!ti->type->dax_copy_from_iter) { 1166 ret = copy_from_iter(addr, bytes, i); 1167 goto out; 1168 } 1169 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i); 1170 out: 1171 dm_put_live_table(md, srcu_idx); 1172 1173 return ret; 1174 } 1175 1176 static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, 1177 void *addr, size_t bytes, struct iov_iter *i) 1178 { 1179 struct mapped_device *md = dax_get_private(dax_dev); 1180 sector_t sector = pgoff * PAGE_SECTORS; 1181 struct dm_target *ti; 1182 long ret = 0; 1183 int srcu_idx; 1184 1185 ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1186 1187 if (!ti) 1188 goto out; 1189 if (!ti->type->dax_copy_to_iter) { 1190 ret = copy_to_iter(addr, bytes, i); 1191 goto out; 1192 } 1193 ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i); 1194 out: 1195 dm_put_live_table(md, srcu_idx); 1196 1197 return ret; 1198 } 1199 1200 static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1201 size_t nr_pages) 1202 { 1203 struct mapped_device *md = dax_get_private(dax_dev); 1204 sector_t sector = pgoff * PAGE_SECTORS; 1205 struct dm_target *ti; 1206 int ret = -EIO; 1207 int srcu_idx; 1208 1209 ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1210 1211 if (!ti) 1212 goto out; 1213 if (WARN_ON(!ti->type->dax_zero_page_range)) { 1214 /* 1215 * ->zero_page_range() is mandatory dax operation. If we are 1216 * here, something is wrong. 1217 */ 1218 dm_put_live_table(md, srcu_idx); 1219 goto out; 1220 } 1221 ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1222 1223 out: 1224 dm_put_live_table(md, srcu_idx); 1225 1226 return ret; 1227 } 1228 1229 /* 1230 * A target may call dm_accept_partial_bio only from the map routine. It is 1231 * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET, 1232 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH. 1233 * 1234 * dm_accept_partial_bio informs the dm that the target only wants to process 1235 * additional n_sectors sectors of the bio and the rest of the data should be 1236 * sent in a next bio. 1237 * 1238 * A diagram that explains the arithmetics: 1239 * +--------------------+---------------+-------+ 1240 * | 1 | 2 | 3 | 1241 * +--------------------+---------------+-------+ 1242 * 1243 * <-------------- *tio->len_ptr ---------------> 1244 * <------- bi_size -------> 1245 * <-- n_sectors --> 1246 * 1247 * Region 1 was already iterated over with bio_advance or similar function. 1248 * (it may be empty if the target doesn't use bio_advance) 1249 * Region 2 is the remaining bio size that the target wants to process. 1250 * (it may be empty if region 1 is non-empty, although there is no reason 1251 * to make it empty) 1252 * The target requires that region 3 is to be sent in the next bio. 1253 * 1254 * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 1255 * the partially processed part (the sum of regions 1+2) must be the same for all 1256 * copies of the bio. 1257 */ 1258 void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 1259 { 1260 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 1261 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 1262 BUG_ON(bio->bi_opf & REQ_PREFLUSH); 1263 BUG_ON(bi_size > *tio->len_ptr); 1264 BUG_ON(n_sectors > bi_size); 1265 *tio->len_ptr -= bi_size - n_sectors; 1266 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 1267 } 1268 EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 1269 1270 static blk_qc_t __map_bio(struct dm_target_io *tio) 1271 { 1272 int r; 1273 sector_t sector; 1274 struct bio *clone = &tio->clone; 1275 struct dm_io *io = tio->io; 1276 struct dm_target *ti = tio->ti; 1277 blk_qc_t ret = BLK_QC_T_NONE; 1278 1279 clone->bi_end_io = clone_endio; 1280 1281 /* 1282 * Map the clone. If r == 0 we don't need to do 1283 * anything, the target has assumed ownership of 1284 * this io. 1285 */ 1286 atomic_inc(&io->io_count); 1287 sector = clone->bi_iter.bi_sector; 1288 1289 r = ti->type->map(ti, clone); 1290 switch (r) { 1291 case DM_MAPIO_SUBMITTED: 1292 break; 1293 case DM_MAPIO_REMAPPED: 1294 /* the bio has been remapped so dispatch it */ 1295 trace_block_bio_remap(clone->bi_disk->queue, clone, 1296 bio_dev(io->orig_bio), sector); 1297 ret = submit_bio_noacct(clone); 1298 break; 1299 case DM_MAPIO_KILL: 1300 free_tio(tio); 1301 dec_pending(io, BLK_STS_IOERR); 1302 break; 1303 case DM_MAPIO_REQUEUE: 1304 free_tio(tio); 1305 dec_pending(io, BLK_STS_DM_REQUEUE); 1306 break; 1307 default: 1308 DMWARN("unimplemented target map return value: %d", r); 1309 BUG(); 1310 } 1311 1312 return ret; 1313 } 1314 1315 static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1316 { 1317 bio->bi_iter.bi_sector = sector; 1318 bio->bi_iter.bi_size = to_bytes(len); 1319 } 1320 1321 /* 1322 * Creates a bio that consists of range of complete bvecs. 1323 */ 1324 static int clone_bio(struct dm_target_io *tio, struct bio *bio, 1325 sector_t sector, unsigned len) 1326 { 1327 struct bio *clone = &tio->clone; 1328 1329 __bio_clone_fast(clone, bio); 1330 1331 bio_crypt_clone(clone, bio, GFP_NOIO); 1332 1333 if (bio_integrity(bio)) { 1334 int r; 1335 1336 if (unlikely(!dm_target_has_integrity(tio->ti->type) && 1337 !dm_target_passes_integrity(tio->ti->type))) { 1338 DMWARN("%s: the target %s doesn't support integrity data.", 1339 dm_device_name(tio->io->md), 1340 tio->ti->type->name); 1341 return -EIO; 1342 } 1343 1344 r = bio_integrity_clone(clone, bio, GFP_NOIO); 1345 if (r < 0) 1346 return r; 1347 } 1348 1349 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 1350 clone->bi_iter.bi_size = to_bytes(len); 1351 1352 if (bio_integrity(bio)) 1353 bio_integrity_trim(clone); 1354 1355 return 0; 1356 } 1357 1358 static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 1359 struct dm_target *ti, unsigned num_bios) 1360 { 1361 struct dm_target_io *tio; 1362 int try; 1363 1364 if (!num_bios) 1365 return; 1366 1367 if (num_bios == 1) { 1368 tio = alloc_tio(ci, ti, 0, GFP_NOIO); 1369 bio_list_add(blist, &tio->clone); 1370 return; 1371 } 1372 1373 for (try = 0; try < 2; try++) { 1374 int bio_nr; 1375 struct bio *bio; 1376 1377 if (try) 1378 mutex_lock(&ci->io->md->table_devices_lock); 1379 for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 1380 tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT); 1381 if (!tio) 1382 break; 1383 1384 bio_list_add(blist, &tio->clone); 1385 } 1386 if (try) 1387 mutex_unlock(&ci->io->md->table_devices_lock); 1388 if (bio_nr == num_bios) 1389 return; 1390 1391 while ((bio = bio_list_pop(blist))) { 1392 tio = container_of(bio, struct dm_target_io, clone); 1393 free_tio(tio); 1394 } 1395 } 1396 } 1397 1398 static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci, 1399 struct dm_target_io *tio, unsigned *len) 1400 { 1401 struct bio *clone = &tio->clone; 1402 1403 tio->len_ptr = len; 1404 1405 __bio_clone_fast(clone, ci->bio); 1406 if (len) 1407 bio_setup_sector(clone, ci->sector, *len); 1408 1409 return __map_bio(tio); 1410 } 1411 1412 static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 1413 unsigned num_bios, unsigned *len) 1414 { 1415 struct bio_list blist = BIO_EMPTY_LIST; 1416 struct bio *bio; 1417 struct dm_target_io *tio; 1418 1419 alloc_multiple_bios(&blist, ci, ti, num_bios); 1420 1421 while ((bio = bio_list_pop(&blist))) { 1422 tio = container_of(bio, struct dm_target_io, clone); 1423 (void) __clone_and_map_simple_bio(ci, tio, len); 1424 } 1425 } 1426 1427 static int __send_empty_flush(struct clone_info *ci) 1428 { 1429 unsigned target_nr = 0; 1430 struct dm_target *ti; 1431 1432 /* 1433 * Empty flush uses a statically initialized bio, as the base for 1434 * cloning. However, blkg association requires that a bdev is 1435 * associated with a gendisk, which doesn't happen until the bdev is 1436 * opened. So, blkg association is done at issue time of the flush 1437 * rather than when the device is created in alloc_dev(). 1438 */ 1439 bio_set_dev(ci->bio, ci->io->md->bdev); 1440 1441 BUG_ON(bio_has_data(ci->bio)); 1442 while ((ti = dm_table_get_target(ci->map, target_nr++))) 1443 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1444 return 0; 1445 } 1446 1447 static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 1448 sector_t sector, unsigned *len) 1449 { 1450 struct bio *bio = ci->bio; 1451 struct dm_target_io *tio; 1452 int r; 1453 1454 tio = alloc_tio(ci, ti, 0, GFP_NOIO); 1455 tio->len_ptr = len; 1456 r = clone_bio(tio, bio, sector, *len); 1457 if (r < 0) { 1458 free_tio(tio); 1459 return r; 1460 } 1461 (void) __map_bio(tio); 1462 1463 return 0; 1464 } 1465 1466 typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); 1467 1468 static unsigned get_num_discard_bios(struct dm_target *ti) 1469 { 1470 return ti->num_discard_bios; 1471 } 1472 1473 static unsigned get_num_secure_erase_bios(struct dm_target *ti) 1474 { 1475 return ti->num_secure_erase_bios; 1476 } 1477 1478 static unsigned get_num_write_same_bios(struct dm_target *ti) 1479 { 1480 return ti->num_write_same_bios; 1481 } 1482 1483 static unsigned get_num_write_zeroes_bios(struct dm_target *ti) 1484 { 1485 return ti->num_write_zeroes_bios; 1486 } 1487 1488 static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 1489 unsigned num_bios) 1490 { 1491 unsigned len; 1492 1493 /* 1494 * Even though the device advertised support for this type of 1495 * request, that does not mean every target supports it, and 1496 * reconfiguration might also have changed that since the 1497 * check was performed. 1498 */ 1499 if (!num_bios) 1500 return -EOPNOTSUPP; 1501 1502 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 1503 1504 __send_duplicate_bios(ci, ti, num_bios, &len); 1505 1506 ci->sector += len; 1507 ci->sector_count -= len; 1508 1509 return 0; 1510 } 1511 1512 static int __send_discard(struct clone_info *ci, struct dm_target *ti) 1513 { 1514 return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti)); 1515 } 1516 1517 static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti) 1518 { 1519 return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti)); 1520 } 1521 1522 static int __send_write_same(struct clone_info *ci, struct dm_target *ti) 1523 { 1524 return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti)); 1525 } 1526 1527 static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti) 1528 { 1529 return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti)); 1530 } 1531 1532 static bool is_abnormal_io(struct bio *bio) 1533 { 1534 bool r = false; 1535 1536 switch (bio_op(bio)) { 1537 case REQ_OP_DISCARD: 1538 case REQ_OP_SECURE_ERASE: 1539 case REQ_OP_WRITE_SAME: 1540 case REQ_OP_WRITE_ZEROES: 1541 r = true; 1542 break; 1543 } 1544 1545 return r; 1546 } 1547 1548 static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 1549 int *result) 1550 { 1551 struct bio *bio = ci->bio; 1552 1553 if (bio_op(bio) == REQ_OP_DISCARD) 1554 *result = __send_discard(ci, ti); 1555 else if (bio_op(bio) == REQ_OP_SECURE_ERASE) 1556 *result = __send_secure_erase(ci, ti); 1557 else if (bio_op(bio) == REQ_OP_WRITE_SAME) 1558 *result = __send_write_same(ci, ti); 1559 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES) 1560 *result = __send_write_zeroes(ci, ti); 1561 else 1562 return false; 1563 1564 return true; 1565 } 1566 1567 /* 1568 * Select the correct strategy for processing a non-flush bio. 1569 */ 1570 static int __split_and_process_non_flush(struct clone_info *ci) 1571 { 1572 struct dm_target *ti; 1573 unsigned len; 1574 int r; 1575 1576 ti = dm_table_find_target(ci->map, ci->sector); 1577 if (!ti) 1578 return -EIO; 1579 1580 if (__process_abnormal_io(ci, ti, &r)) 1581 return r; 1582 1583 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); 1584 1585 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1586 if (r < 0) 1587 return r; 1588 1589 ci->sector += len; 1590 ci->sector_count -= len; 1591 1592 return 0; 1593 } 1594 1595 static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1596 struct dm_table *map, struct bio *bio) 1597 { 1598 ci->map = map; 1599 ci->io = alloc_io(md, bio); 1600 ci->sector = bio->bi_iter.bi_sector; 1601 } 1602 1603 #define __dm_part_stat_sub(part, field, subnd) \ 1604 (part_stat_get(part, field) -= (subnd)) 1605 1606 /* 1607 * Entry point to split a bio into clones and submit them to the targets. 1608 */ 1609 static blk_qc_t __split_and_process_bio(struct mapped_device *md, 1610 struct dm_table *map, struct bio *bio) 1611 { 1612 struct clone_info ci; 1613 blk_qc_t ret = BLK_QC_T_NONE; 1614 int error = 0; 1615 1616 init_clone_info(&ci, md, map, bio); 1617 1618 if (bio->bi_opf & REQ_PREFLUSH) { 1619 struct bio flush_bio; 1620 1621 /* 1622 * Use an on-stack bio for this, it's safe since we don't 1623 * need to reference it after submit. It's just used as 1624 * the basis for the clone(s). 1625 */ 1626 bio_init(&flush_bio, NULL, 0); 1627 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1628 ci.bio = &flush_bio; 1629 ci.sector_count = 0; 1630 error = __send_empty_flush(&ci); 1631 bio_uninit(ci.bio); 1632 /* dec_pending submits any data associated with flush */ 1633 } else if (op_is_zone_mgmt(bio_op(bio))) { 1634 ci.bio = bio; 1635 ci.sector_count = 0; 1636 error = __split_and_process_non_flush(&ci); 1637 } else { 1638 ci.bio = bio; 1639 ci.sector_count = bio_sectors(bio); 1640 while (ci.sector_count && !error) { 1641 error = __split_and_process_non_flush(&ci); 1642 if (current->bio_list && ci.sector_count && !error) { 1643 /* 1644 * Remainder must be passed to submit_bio_noacct() 1645 * so that it gets handled *after* bios already submitted 1646 * have been completely processed. 1647 * We take a clone of the original to store in 1648 * ci.io->orig_bio to be used by end_io_acct() and 1649 * for dec_pending to use for completion handling. 1650 */ 1651 struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1652 GFP_NOIO, &md->queue->bio_split); 1653 ci.io->orig_bio = b; 1654 1655 /* 1656 * Adjust IO stats for each split, otherwise upon queue 1657 * reentry there will be redundant IO accounting. 1658 * NOTE: this is a stop-gap fix, a proper fix involves 1659 * significant refactoring of DM core's bio splitting 1660 * (by eliminating DM's splitting and just using bio_split) 1661 */ 1662 part_stat_lock(); 1663 __dm_part_stat_sub(&dm_disk(md)->part0, 1664 sectors[op_stat_group(bio_op(bio))], ci.sector_count); 1665 part_stat_unlock(); 1666 1667 bio_chain(b, bio); 1668 trace_block_split(md->queue, b, bio->bi_iter.bi_sector); 1669 ret = submit_bio_noacct(bio); 1670 break; 1671 } 1672 } 1673 } 1674 1675 /* drop the extra reference count */ 1676 dec_pending(ci.io, errno_to_blk_status(error)); 1677 return ret; 1678 } 1679 1680 /* 1681 * Optimized variant of __split_and_process_bio that leverages the 1682 * fact that targets that use it do _not_ have a need to split bios. 1683 */ 1684 static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map, 1685 struct bio *bio, struct dm_target *ti) 1686 { 1687 struct clone_info ci; 1688 blk_qc_t ret = BLK_QC_T_NONE; 1689 int error = 0; 1690 1691 init_clone_info(&ci, md, map, bio); 1692 1693 if (bio->bi_opf & REQ_PREFLUSH) { 1694 struct bio flush_bio; 1695 1696 /* 1697 * Use an on-stack bio for this, it's safe since we don't 1698 * need to reference it after submit. It's just used as 1699 * the basis for the clone(s). 1700 */ 1701 bio_init(&flush_bio, NULL, 0); 1702 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1703 ci.bio = &flush_bio; 1704 ci.sector_count = 0; 1705 error = __send_empty_flush(&ci); 1706 bio_uninit(ci.bio); 1707 /* dec_pending submits any data associated with flush */ 1708 } else { 1709 struct dm_target_io *tio; 1710 1711 ci.bio = bio; 1712 ci.sector_count = bio_sectors(bio); 1713 if (__process_abnormal_io(&ci, ti, &error)) 1714 goto out; 1715 1716 tio = alloc_tio(&ci, ti, 0, GFP_NOIO); 1717 ret = __clone_and_map_simple_bio(&ci, tio, NULL); 1718 } 1719 out: 1720 /* drop the extra reference count */ 1721 dec_pending(ci.io, errno_to_blk_status(error)); 1722 return ret; 1723 } 1724 1725 static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio) 1726 { 1727 unsigned len, sector_count; 1728 1729 sector_count = bio_sectors(*bio); 1730 len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count); 1731 1732 if (sector_count > len) { 1733 struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split); 1734 1735 bio_chain(split, *bio); 1736 trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector); 1737 submit_bio_noacct(*bio); 1738 *bio = split; 1739 } 1740 } 1741 1742 static blk_qc_t dm_process_bio(struct mapped_device *md, 1743 struct dm_table *map, struct bio *bio) 1744 { 1745 blk_qc_t ret = BLK_QC_T_NONE; 1746 struct dm_target *ti = md->immutable_target; 1747 1748 if (unlikely(!map)) { 1749 bio_io_error(bio); 1750 return ret; 1751 } 1752 1753 if (!ti) { 1754 ti = dm_table_find_target(map, bio->bi_iter.bi_sector); 1755 if (unlikely(!ti)) { 1756 bio_io_error(bio); 1757 return ret; 1758 } 1759 } 1760 1761 /* 1762 * If in ->queue_bio we need to use blk_queue_split(), otherwise 1763 * queue_limits for abnormal requests (e.g. discard, writesame, etc) 1764 * won't be imposed. 1765 */ 1766 if (current->bio_list) { 1767 if (is_abnormal_io(bio)) 1768 blk_queue_split(&bio); 1769 else 1770 dm_queue_split(md, ti, &bio); 1771 } 1772 1773 if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED) 1774 return __process_bio(md, map, bio, ti); 1775 else 1776 return __split_and_process_bio(md, map, bio); 1777 } 1778 1779 static blk_qc_t dm_submit_bio(struct bio *bio) 1780 { 1781 struct mapped_device *md = bio->bi_disk->private_data; 1782 blk_qc_t ret = BLK_QC_T_NONE; 1783 int srcu_idx; 1784 struct dm_table *map; 1785 1786 if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) { 1787 /* 1788 * We are called with a live reference on q_usage_counter, but 1789 * that one will be released as soon as we return. Grab an 1790 * extra one as blk_mq_submit_bio expects to be able to consume 1791 * a reference (which lives until the request is freed in case a 1792 * request is allocated). 1793 */ 1794 percpu_ref_get(&bio->bi_disk->queue->q_usage_counter); 1795 return blk_mq_submit_bio(bio); 1796 } 1797 1798 map = dm_get_live_table(md, &srcu_idx); 1799 1800 /* if we're suspended, we have to queue this io for later */ 1801 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 1802 dm_put_live_table(md, srcu_idx); 1803 1804 if (!(bio->bi_opf & REQ_RAHEAD)) 1805 queue_io(md, bio); 1806 else 1807 bio_io_error(bio); 1808 return ret; 1809 } 1810 1811 ret = dm_process_bio(md, map, bio); 1812 1813 dm_put_live_table(md, srcu_idx); 1814 return ret; 1815 } 1816 1817 /*----------------------------------------------------------------- 1818 * An IDR is used to keep track of allocated minor numbers. 1819 *---------------------------------------------------------------*/ 1820 static void free_minor(int minor) 1821 { 1822 spin_lock(&_minor_lock); 1823 idr_remove(&_minor_idr, minor); 1824 spin_unlock(&_minor_lock); 1825 } 1826 1827 /* 1828 * See if the device with a specific minor # is free. 1829 */ 1830 static int specific_minor(int minor) 1831 { 1832 int r; 1833 1834 if (minor >= (1 << MINORBITS)) 1835 return -EINVAL; 1836 1837 idr_preload(GFP_KERNEL); 1838 spin_lock(&_minor_lock); 1839 1840 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 1841 1842 spin_unlock(&_minor_lock); 1843 idr_preload_end(); 1844 if (r < 0) 1845 return r == -ENOSPC ? -EBUSY : r; 1846 return 0; 1847 } 1848 1849 static int next_free_minor(int *minor) 1850 { 1851 int r; 1852 1853 idr_preload(GFP_KERNEL); 1854 spin_lock(&_minor_lock); 1855 1856 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 1857 1858 spin_unlock(&_minor_lock); 1859 idr_preload_end(); 1860 if (r < 0) 1861 return r; 1862 *minor = r; 1863 return 0; 1864 } 1865 1866 static const struct block_device_operations dm_blk_dops; 1867 static const struct dax_operations dm_dax_ops; 1868 1869 static void dm_wq_work(struct work_struct *work); 1870 1871 static void cleanup_mapped_device(struct mapped_device *md) 1872 { 1873 if (md->wq) 1874 destroy_workqueue(md->wq); 1875 bioset_exit(&md->bs); 1876 bioset_exit(&md->io_bs); 1877 1878 if (md->dax_dev) { 1879 kill_dax(md->dax_dev); 1880 put_dax(md->dax_dev); 1881 md->dax_dev = NULL; 1882 } 1883 1884 if (md->disk) { 1885 spin_lock(&_minor_lock); 1886 md->disk->private_data = NULL; 1887 spin_unlock(&_minor_lock); 1888 del_gendisk(md->disk); 1889 put_disk(md->disk); 1890 } 1891 1892 if (md->queue) 1893 blk_cleanup_queue(md->queue); 1894 1895 cleanup_srcu_struct(&md->io_barrier); 1896 1897 if (md->bdev) { 1898 bdput(md->bdev); 1899 md->bdev = NULL; 1900 } 1901 1902 mutex_destroy(&md->suspend_lock); 1903 mutex_destroy(&md->type_lock); 1904 mutex_destroy(&md->table_devices_lock); 1905 1906 dm_mq_cleanup_mapped_device(md); 1907 } 1908 1909 /* 1910 * Allocate and initialise a blank device with a given minor. 1911 */ 1912 static struct mapped_device *alloc_dev(int minor) 1913 { 1914 int r, numa_node_id = dm_get_numa_node(); 1915 struct mapped_device *md; 1916 void *old_md; 1917 1918 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 1919 if (!md) { 1920 DMWARN("unable to allocate device, out of memory."); 1921 return NULL; 1922 } 1923 1924 if (!try_module_get(THIS_MODULE)) 1925 goto bad_module_get; 1926 1927 /* get a minor number for the dev */ 1928 if (minor == DM_ANY_MINOR) 1929 r = next_free_minor(&minor); 1930 else 1931 r = specific_minor(minor); 1932 if (r < 0) 1933 goto bad_minor; 1934 1935 r = init_srcu_struct(&md->io_barrier); 1936 if (r < 0) 1937 goto bad_io_barrier; 1938 1939 md->numa_node_id = numa_node_id; 1940 md->init_tio_pdu = false; 1941 md->type = DM_TYPE_NONE; 1942 mutex_init(&md->suspend_lock); 1943 mutex_init(&md->type_lock); 1944 mutex_init(&md->table_devices_lock); 1945 spin_lock_init(&md->deferred_lock); 1946 atomic_set(&md->holders, 1); 1947 atomic_set(&md->open_count, 0); 1948 atomic_set(&md->event_nr, 0); 1949 atomic_set(&md->uevent_seq, 0); 1950 INIT_LIST_HEAD(&md->uevent_list); 1951 INIT_LIST_HEAD(&md->table_devices); 1952 spin_lock_init(&md->uevent_lock); 1953 1954 /* 1955 * default to bio-based until DM table is loaded and md->type 1956 * established. If request-based table is loaded: blk-mq will 1957 * override accordingly. 1958 */ 1959 md->queue = blk_alloc_queue(numa_node_id); 1960 if (!md->queue) 1961 goto bad; 1962 1963 md->disk = alloc_disk_node(1, md->numa_node_id); 1964 if (!md->disk) 1965 goto bad; 1966 1967 init_waitqueue_head(&md->wait); 1968 INIT_WORK(&md->work, dm_wq_work); 1969 init_waitqueue_head(&md->eventq); 1970 init_completion(&md->kobj_holder.completion); 1971 1972 md->disk->major = _major; 1973 md->disk->first_minor = minor; 1974 md->disk->fops = &dm_blk_dops; 1975 md->disk->queue = md->queue; 1976 md->disk->private_data = md; 1977 sprintf(md->disk->disk_name, "dm-%d", minor); 1978 1979 if (IS_ENABLED(CONFIG_DAX_DRIVER)) { 1980 md->dax_dev = alloc_dax(md, md->disk->disk_name, 1981 &dm_dax_ops, 0); 1982 if (IS_ERR(md->dax_dev)) 1983 goto bad; 1984 } 1985 1986 add_disk_no_queue_reg(md->disk); 1987 format_dev_t(md->name, MKDEV(_major, minor)); 1988 1989 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 1990 if (!md->wq) 1991 goto bad; 1992 1993 md->bdev = bdget_disk(md->disk, 0); 1994 if (!md->bdev) 1995 goto bad; 1996 1997 dm_stats_init(&md->stats); 1998 1999 /* Populate the mapping, nobody knows we exist yet */ 2000 spin_lock(&_minor_lock); 2001 old_md = idr_replace(&_minor_idr, md, minor); 2002 spin_unlock(&_minor_lock); 2003 2004 BUG_ON(old_md != MINOR_ALLOCED); 2005 2006 return md; 2007 2008 bad: 2009 cleanup_mapped_device(md); 2010 bad_io_barrier: 2011 free_minor(minor); 2012 bad_minor: 2013 module_put(THIS_MODULE); 2014 bad_module_get: 2015 kvfree(md); 2016 return NULL; 2017 } 2018 2019 static void unlock_fs(struct mapped_device *md); 2020 2021 static void free_dev(struct mapped_device *md) 2022 { 2023 int minor = MINOR(disk_devt(md->disk)); 2024 2025 unlock_fs(md); 2026 2027 cleanup_mapped_device(md); 2028 2029 free_table_devices(&md->table_devices); 2030 dm_stats_cleanup(&md->stats); 2031 free_minor(minor); 2032 2033 module_put(THIS_MODULE); 2034 kvfree(md); 2035 } 2036 2037 static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 2038 { 2039 struct dm_md_mempools *p = dm_table_get_md_mempools(t); 2040 int ret = 0; 2041 2042 if (dm_table_bio_based(t)) { 2043 /* 2044 * The md may already have mempools that need changing. 2045 * If so, reload bioset because front_pad may have changed 2046 * because a different table was loaded. 2047 */ 2048 bioset_exit(&md->bs); 2049 bioset_exit(&md->io_bs); 2050 2051 } else if (bioset_initialized(&md->bs)) { 2052 /* 2053 * There's no need to reload with request-based dm 2054 * because the size of front_pad doesn't change. 2055 * Note for future: If you are to reload bioset, 2056 * prep-ed requests in the queue may refer 2057 * to bio from the old bioset, so you must walk 2058 * through the queue to unprep. 2059 */ 2060 goto out; 2061 } 2062 2063 BUG_ON(!p || 2064 bioset_initialized(&md->bs) || 2065 bioset_initialized(&md->io_bs)); 2066 2067 ret = bioset_init_from_src(&md->bs, &p->bs); 2068 if (ret) 2069 goto out; 2070 ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 2071 if (ret) 2072 bioset_exit(&md->bs); 2073 out: 2074 /* mempool bind completed, no longer need any mempools in the table */ 2075 dm_table_free_md_mempools(t); 2076 return ret; 2077 } 2078 2079 /* 2080 * Bind a table to the device. 2081 */ 2082 static void event_callback(void *context) 2083 { 2084 unsigned long flags; 2085 LIST_HEAD(uevents); 2086 struct mapped_device *md = (struct mapped_device *) context; 2087 2088 spin_lock_irqsave(&md->uevent_lock, flags); 2089 list_splice_init(&md->uevent_list, &uevents); 2090 spin_unlock_irqrestore(&md->uevent_lock, flags); 2091 2092 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 2093 2094 atomic_inc(&md->event_nr); 2095 wake_up(&md->eventq); 2096 dm_issue_global_event(); 2097 } 2098 2099 /* 2100 * Protected by md->suspend_lock obtained by dm_swap_table(). 2101 */ 2102 static void __set_size(struct mapped_device *md, sector_t size) 2103 { 2104 lockdep_assert_held(&md->suspend_lock); 2105 2106 set_capacity(md->disk, size); 2107 2108 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 2109 } 2110 2111 /* 2112 * Returns old map, which caller must destroy. 2113 */ 2114 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2115 struct queue_limits *limits) 2116 { 2117 struct dm_table *old_map; 2118 struct request_queue *q = md->queue; 2119 bool request_based = dm_table_request_based(t); 2120 sector_t size; 2121 int ret; 2122 2123 lockdep_assert_held(&md->suspend_lock); 2124 2125 size = dm_table_get_size(t); 2126 2127 /* 2128 * Wipe any geometry if the size of the table changed. 2129 */ 2130 if (size != dm_get_size(md)) 2131 memset(&md->geometry, 0, sizeof(md->geometry)); 2132 2133 __set_size(md, size); 2134 2135 dm_table_event_callback(t, event_callback, md); 2136 2137 /* 2138 * The queue hasn't been stopped yet, if the old table type wasn't 2139 * for request-based during suspension. So stop it to prevent 2140 * I/O mapping before resume. 2141 * This must be done before setting the queue restrictions, 2142 * because request-based dm may be run just after the setting. 2143 */ 2144 if (request_based) 2145 dm_stop_queue(q); 2146 2147 if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) { 2148 /* 2149 * Leverage the fact that request-based DM targets and 2150 * NVMe bio based targets are immutable singletons 2151 * - used to optimize both dm_request_fn and dm_mq_queue_rq; 2152 * and __process_bio. 2153 */ 2154 md->immutable_target = dm_table_get_immutable_target(t); 2155 } 2156 2157 ret = __bind_mempools(md, t); 2158 if (ret) { 2159 old_map = ERR_PTR(ret); 2160 goto out; 2161 } 2162 2163 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2164 rcu_assign_pointer(md->map, (void *)t); 2165 md->immutable_target_type = dm_table_get_immutable_target_type(t); 2166 2167 dm_table_set_restrictions(t, q, limits); 2168 if (old_map) 2169 dm_sync_table(md); 2170 2171 out: 2172 return old_map; 2173 } 2174 2175 /* 2176 * Returns unbound table for the caller to free. 2177 */ 2178 static struct dm_table *__unbind(struct mapped_device *md) 2179 { 2180 struct dm_table *map = rcu_dereference_protected(md->map, 1); 2181 2182 if (!map) 2183 return NULL; 2184 2185 dm_table_event_callback(map, NULL, NULL); 2186 RCU_INIT_POINTER(md->map, NULL); 2187 dm_sync_table(md); 2188 2189 return map; 2190 } 2191 2192 /* 2193 * Constructor for a new device. 2194 */ 2195 int dm_create(int minor, struct mapped_device **result) 2196 { 2197 int r; 2198 struct mapped_device *md; 2199 2200 md = alloc_dev(minor); 2201 if (!md) 2202 return -ENXIO; 2203 2204 r = dm_sysfs_init(md); 2205 if (r) { 2206 free_dev(md); 2207 return r; 2208 } 2209 2210 *result = md; 2211 return 0; 2212 } 2213 2214 /* 2215 * Functions to manage md->type. 2216 * All are required to hold md->type_lock. 2217 */ 2218 void dm_lock_md_type(struct mapped_device *md) 2219 { 2220 mutex_lock(&md->type_lock); 2221 } 2222 2223 void dm_unlock_md_type(struct mapped_device *md) 2224 { 2225 mutex_unlock(&md->type_lock); 2226 } 2227 2228 void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2229 { 2230 BUG_ON(!mutex_is_locked(&md->type_lock)); 2231 md->type = type; 2232 } 2233 2234 enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2235 { 2236 return md->type; 2237 } 2238 2239 struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 2240 { 2241 return md->immutable_target_type; 2242 } 2243 2244 /* 2245 * The queue_limits are only valid as long as you have a reference 2246 * count on 'md'. 2247 */ 2248 struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2249 { 2250 BUG_ON(!atomic_read(&md->holders)); 2251 return &md->queue->limits; 2252 } 2253 EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2254 2255 /* 2256 * Setup the DM device's queue based on md's type 2257 */ 2258 int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 2259 { 2260 int r; 2261 struct queue_limits limits; 2262 enum dm_queue_mode type = dm_get_md_type(md); 2263 2264 switch (type) { 2265 case DM_TYPE_REQUEST_BASED: 2266 r = dm_mq_init_request_queue(md, t); 2267 if (r) { 2268 DMERR("Cannot initialize queue for request-based dm-mq mapped device"); 2269 return r; 2270 } 2271 break; 2272 case DM_TYPE_BIO_BASED: 2273 case DM_TYPE_DAX_BIO_BASED: 2274 case DM_TYPE_NVME_BIO_BASED: 2275 break; 2276 case DM_TYPE_NONE: 2277 WARN_ON_ONCE(true); 2278 break; 2279 } 2280 2281 r = dm_calculate_queue_limits(t, &limits); 2282 if (r) { 2283 DMERR("Cannot calculate initial queue limits"); 2284 return r; 2285 } 2286 dm_table_set_restrictions(t, md->queue, &limits); 2287 blk_register_queue(md->disk); 2288 2289 return 0; 2290 } 2291 2292 struct mapped_device *dm_get_md(dev_t dev) 2293 { 2294 struct mapped_device *md; 2295 unsigned minor = MINOR(dev); 2296 2297 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 2298 return NULL; 2299 2300 spin_lock(&_minor_lock); 2301 2302 md = idr_find(&_minor_idr, minor); 2303 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 2304 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2305 md = NULL; 2306 goto out; 2307 } 2308 dm_get(md); 2309 out: 2310 spin_unlock(&_minor_lock); 2311 2312 return md; 2313 } 2314 EXPORT_SYMBOL_GPL(dm_get_md); 2315 2316 void *dm_get_mdptr(struct mapped_device *md) 2317 { 2318 return md->interface_ptr; 2319 } 2320 2321 void dm_set_mdptr(struct mapped_device *md, void *ptr) 2322 { 2323 md->interface_ptr = ptr; 2324 } 2325 2326 void dm_get(struct mapped_device *md) 2327 { 2328 atomic_inc(&md->holders); 2329 BUG_ON(test_bit(DMF_FREEING, &md->flags)); 2330 } 2331 2332 int dm_hold(struct mapped_device *md) 2333 { 2334 spin_lock(&_minor_lock); 2335 if (test_bit(DMF_FREEING, &md->flags)) { 2336 spin_unlock(&_minor_lock); 2337 return -EBUSY; 2338 } 2339 dm_get(md); 2340 spin_unlock(&_minor_lock); 2341 return 0; 2342 } 2343 EXPORT_SYMBOL_GPL(dm_hold); 2344 2345 const char *dm_device_name(struct mapped_device *md) 2346 { 2347 return md->name; 2348 } 2349 EXPORT_SYMBOL_GPL(dm_device_name); 2350 2351 static void __dm_destroy(struct mapped_device *md, bool wait) 2352 { 2353 struct dm_table *map; 2354 int srcu_idx; 2355 2356 might_sleep(); 2357 2358 spin_lock(&_minor_lock); 2359 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2360 set_bit(DMF_FREEING, &md->flags); 2361 spin_unlock(&_minor_lock); 2362 2363 blk_set_queue_dying(md->queue); 2364 2365 /* 2366 * Take suspend_lock so that presuspend and postsuspend methods 2367 * do not race with internal suspend. 2368 */ 2369 mutex_lock(&md->suspend_lock); 2370 map = dm_get_live_table(md, &srcu_idx); 2371 if (!dm_suspended_md(md)) { 2372 dm_table_presuspend_targets(map); 2373 set_bit(DMF_SUSPENDED, &md->flags); 2374 set_bit(DMF_POST_SUSPENDING, &md->flags); 2375 dm_table_postsuspend_targets(map); 2376 } 2377 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 2378 dm_put_live_table(md, srcu_idx); 2379 mutex_unlock(&md->suspend_lock); 2380 2381 /* 2382 * Rare, but there may be I/O requests still going to complete, 2383 * for example. Wait for all references to disappear. 2384 * No one should increment the reference count of the mapped_device, 2385 * after the mapped_device state becomes DMF_FREEING. 2386 */ 2387 if (wait) 2388 while (atomic_read(&md->holders)) 2389 msleep(1); 2390 else if (atomic_read(&md->holders)) 2391 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 2392 dm_device_name(md), atomic_read(&md->holders)); 2393 2394 dm_sysfs_exit(md); 2395 dm_table_destroy(__unbind(md)); 2396 free_dev(md); 2397 } 2398 2399 void dm_destroy(struct mapped_device *md) 2400 { 2401 __dm_destroy(md, true); 2402 } 2403 2404 void dm_destroy_immediate(struct mapped_device *md) 2405 { 2406 __dm_destroy(md, false); 2407 } 2408 2409 void dm_put(struct mapped_device *md) 2410 { 2411 atomic_dec(&md->holders); 2412 } 2413 EXPORT_SYMBOL_GPL(dm_put); 2414 2415 static bool md_in_flight_bios(struct mapped_device *md) 2416 { 2417 int cpu; 2418 struct hd_struct *part = &dm_disk(md)->part0; 2419 long sum = 0; 2420 2421 for_each_possible_cpu(cpu) { 2422 sum += part_stat_local_read_cpu(part, in_flight[0], cpu); 2423 sum += part_stat_local_read_cpu(part, in_flight[1], cpu); 2424 } 2425 2426 return sum != 0; 2427 } 2428 2429 static int dm_wait_for_bios_completion(struct mapped_device *md, long task_state) 2430 { 2431 int r = 0; 2432 DEFINE_WAIT(wait); 2433 2434 while (true) { 2435 prepare_to_wait(&md->wait, &wait, task_state); 2436 2437 if (!md_in_flight_bios(md)) 2438 break; 2439 2440 if (signal_pending_state(task_state, current)) { 2441 r = -EINTR; 2442 break; 2443 } 2444 2445 io_schedule(); 2446 } 2447 finish_wait(&md->wait, &wait); 2448 2449 return r; 2450 } 2451 2452 static int dm_wait_for_completion(struct mapped_device *md, long task_state) 2453 { 2454 int r = 0; 2455 2456 if (!queue_is_mq(md->queue)) 2457 return dm_wait_for_bios_completion(md, task_state); 2458 2459 while (true) { 2460 if (!blk_mq_queue_inflight(md->queue)) 2461 break; 2462 2463 if (signal_pending_state(task_state, current)) { 2464 r = -EINTR; 2465 break; 2466 } 2467 2468 msleep(5); 2469 } 2470 2471 return r; 2472 } 2473 2474 /* 2475 * Process the deferred bios 2476 */ 2477 static void dm_wq_work(struct work_struct *work) 2478 { 2479 struct mapped_device *md = container_of(work, struct mapped_device, 2480 work); 2481 struct bio *c; 2482 int srcu_idx; 2483 struct dm_table *map; 2484 2485 map = dm_get_live_table(md, &srcu_idx); 2486 2487 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2488 spin_lock_irq(&md->deferred_lock); 2489 c = bio_list_pop(&md->deferred); 2490 spin_unlock_irq(&md->deferred_lock); 2491 2492 if (!c) 2493 break; 2494 2495 if (dm_request_based(md)) 2496 (void) submit_bio_noacct(c); 2497 else 2498 (void) dm_process_bio(md, map, c); 2499 } 2500 2501 dm_put_live_table(md, srcu_idx); 2502 } 2503 2504 static void dm_queue_flush(struct mapped_device *md) 2505 { 2506 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2507 smp_mb__after_atomic(); 2508 queue_work(md->wq, &md->work); 2509 } 2510 2511 /* 2512 * Swap in a new table, returning the old one for the caller to destroy. 2513 */ 2514 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 2515 { 2516 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2517 struct queue_limits limits; 2518 int r; 2519 2520 mutex_lock(&md->suspend_lock); 2521 2522 /* device must be suspended */ 2523 if (!dm_suspended_md(md)) 2524 goto out; 2525 2526 /* 2527 * If the new table has no data devices, retain the existing limits. 2528 * This helps multipath with queue_if_no_path if all paths disappear, 2529 * then new I/O is queued based on these limits, and then some paths 2530 * reappear. 2531 */ 2532 if (dm_table_has_no_data_devices(table)) { 2533 live_map = dm_get_live_table_fast(md); 2534 if (live_map) 2535 limits = md->queue->limits; 2536 dm_put_live_table_fast(md); 2537 } 2538 2539 if (!live_map) { 2540 r = dm_calculate_queue_limits(table, &limits); 2541 if (r) { 2542 map = ERR_PTR(r); 2543 goto out; 2544 } 2545 } 2546 2547 map = __bind(md, table, &limits); 2548 dm_issue_global_event(); 2549 2550 out: 2551 mutex_unlock(&md->suspend_lock); 2552 return map; 2553 } 2554 2555 /* 2556 * Functions to lock and unlock any filesystem running on the 2557 * device. 2558 */ 2559 static int lock_fs(struct mapped_device *md) 2560 { 2561 int r; 2562 2563 WARN_ON(md->frozen_sb); 2564 2565 md->frozen_sb = freeze_bdev(md->bdev); 2566 if (IS_ERR(md->frozen_sb)) { 2567 r = PTR_ERR(md->frozen_sb); 2568 md->frozen_sb = NULL; 2569 return r; 2570 } 2571 2572 set_bit(DMF_FROZEN, &md->flags); 2573 2574 return 0; 2575 } 2576 2577 static void unlock_fs(struct mapped_device *md) 2578 { 2579 if (!test_bit(DMF_FROZEN, &md->flags)) 2580 return; 2581 2582 thaw_bdev(md->bdev, md->frozen_sb); 2583 md->frozen_sb = NULL; 2584 clear_bit(DMF_FROZEN, &md->flags); 2585 } 2586 2587 /* 2588 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2589 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2590 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2591 * 2592 * If __dm_suspend returns 0, the device is completely quiescent 2593 * now. There is no request-processing activity. All new requests 2594 * are being added to md->deferred list. 2595 */ 2596 static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 2597 unsigned suspend_flags, long task_state, 2598 int dmf_suspended_flag) 2599 { 2600 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2601 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2602 int r; 2603 2604 lockdep_assert_held(&md->suspend_lock); 2605 2606 /* 2607 * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 2608 * This flag is cleared before dm_suspend returns. 2609 */ 2610 if (noflush) 2611 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 2612 else 2613 DMDEBUG("%s: suspending with flush", dm_device_name(md)); 2614 2615 /* 2616 * This gets reverted if there's an error later and the targets 2617 * provide the .presuspend_undo hook. 2618 */ 2619 dm_table_presuspend_targets(map); 2620 2621 /* 2622 * Flush I/O to the device. 2623 * Any I/O submitted after lock_fs() may not be flushed. 2624 * noflush takes precedence over do_lockfs. 2625 * (lock_fs() flushes I/Os and waits for them to complete.) 2626 */ 2627 if (!noflush && do_lockfs) { 2628 r = lock_fs(md); 2629 if (r) { 2630 dm_table_presuspend_undo_targets(map); 2631 return r; 2632 } 2633 } 2634 2635 /* 2636 * Here we must make sure that no processes are submitting requests 2637 * to target drivers i.e. no one may be executing 2638 * __split_and_process_bio. This is called from dm_request and 2639 * dm_wq_work. 2640 * 2641 * To get all processes out of __split_and_process_bio in dm_request, 2642 * we take the write lock. To prevent any process from reentering 2643 * __split_and_process_bio from dm_request and quiesce the thread 2644 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 2645 * flush_workqueue(md->wq). 2646 */ 2647 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2648 if (map) 2649 synchronize_srcu(&md->io_barrier); 2650 2651 /* 2652 * Stop md->queue before flushing md->wq in case request-based 2653 * dm defers requests to md->wq from md->queue. 2654 */ 2655 if (dm_request_based(md)) 2656 dm_stop_queue(md->queue); 2657 2658 flush_workqueue(md->wq); 2659 2660 /* 2661 * At this point no more requests are entering target request routines. 2662 * We call dm_wait_for_completion to wait for all existing requests 2663 * to finish. 2664 */ 2665 r = dm_wait_for_completion(md, task_state); 2666 if (!r) 2667 set_bit(dmf_suspended_flag, &md->flags); 2668 2669 if (noflush) 2670 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 2671 if (map) 2672 synchronize_srcu(&md->io_barrier); 2673 2674 /* were we interrupted ? */ 2675 if (r < 0) { 2676 dm_queue_flush(md); 2677 2678 if (dm_request_based(md)) 2679 dm_start_queue(md->queue); 2680 2681 unlock_fs(md); 2682 dm_table_presuspend_undo_targets(map); 2683 /* pushback list is already flushed, so skip flush */ 2684 } 2685 2686 return r; 2687 } 2688 2689 /* 2690 * We need to be able to change a mapping table under a mounted 2691 * filesystem. For example we might want to move some data in 2692 * the background. Before the table can be swapped with 2693 * dm_bind_table, dm_suspend must be called to flush any in 2694 * flight bios and ensure that any further io gets deferred. 2695 */ 2696 /* 2697 * Suspend mechanism in request-based dm. 2698 * 2699 * 1. Flush all I/Os by lock_fs() if needed. 2700 * 2. Stop dispatching any I/O by stopping the request_queue. 2701 * 3. Wait for all in-flight I/Os to be completed or requeued. 2702 * 2703 * To abort suspend, start the request_queue. 2704 */ 2705 int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2706 { 2707 struct dm_table *map = NULL; 2708 int r = 0; 2709 2710 retry: 2711 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2712 2713 if (dm_suspended_md(md)) { 2714 r = -EINVAL; 2715 goto out_unlock; 2716 } 2717 2718 if (dm_suspended_internally_md(md)) { 2719 /* already internally suspended, wait for internal resume */ 2720 mutex_unlock(&md->suspend_lock); 2721 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2722 if (r) 2723 return r; 2724 goto retry; 2725 } 2726 2727 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2728 2729 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2730 if (r) 2731 goto out_unlock; 2732 2733 set_bit(DMF_POST_SUSPENDING, &md->flags); 2734 dm_table_postsuspend_targets(map); 2735 clear_bit(DMF_POST_SUSPENDING, &md->flags); 2736 2737 out_unlock: 2738 mutex_unlock(&md->suspend_lock); 2739 return r; 2740 } 2741 2742 static int __dm_resume(struct mapped_device *md, struct dm_table *map) 2743 { 2744 if (map) { 2745 int r = dm_table_resume_targets(map); 2746 if (r) 2747 return r; 2748 } 2749 2750 dm_queue_flush(md); 2751 2752 /* 2753 * Flushing deferred I/Os must be done after targets are resumed 2754 * so that mapping of targets can work correctly. 2755 * Request-based dm is queueing the deferred I/Os in its request_queue. 2756 */ 2757 if (dm_request_based(md)) 2758 dm_start_queue(md->queue); 2759 2760 unlock_fs(md); 2761 2762 return 0; 2763 } 2764 2765 int dm_resume(struct mapped_device *md) 2766 { 2767 int r; 2768 struct dm_table *map = NULL; 2769 2770 retry: 2771 r = -EINVAL; 2772 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2773 2774 if (!dm_suspended_md(md)) 2775 goto out; 2776 2777 if (dm_suspended_internally_md(md)) { 2778 /* already internally suspended, wait for internal resume */ 2779 mutex_unlock(&md->suspend_lock); 2780 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2781 if (r) 2782 return r; 2783 goto retry; 2784 } 2785 2786 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2787 if (!map || !dm_table_get_size(map)) 2788 goto out; 2789 2790 r = __dm_resume(md, map); 2791 if (r) 2792 goto out; 2793 2794 clear_bit(DMF_SUSPENDED, &md->flags); 2795 out: 2796 mutex_unlock(&md->suspend_lock); 2797 2798 return r; 2799 } 2800 2801 /* 2802 * Internal suspend/resume works like userspace-driven suspend. It waits 2803 * until all bios finish and prevents issuing new bios to the target drivers. 2804 * It may be used only from the kernel. 2805 */ 2806 2807 static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2808 { 2809 struct dm_table *map = NULL; 2810 2811 lockdep_assert_held(&md->suspend_lock); 2812 2813 if (md->internal_suspend_count++) 2814 return; /* nested internal suspend */ 2815 2816 if (dm_suspended_md(md)) { 2817 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2818 return; /* nest suspend */ 2819 } 2820 2821 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2822 2823 /* 2824 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2825 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2826 * would require changing .presuspend to return an error -- avoid this 2827 * until there is a need for more elaborate variants of internal suspend. 2828 */ 2829 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2830 DMF_SUSPENDED_INTERNALLY); 2831 2832 set_bit(DMF_POST_SUSPENDING, &md->flags); 2833 dm_table_postsuspend_targets(map); 2834 clear_bit(DMF_POST_SUSPENDING, &md->flags); 2835 } 2836 2837 static void __dm_internal_resume(struct mapped_device *md) 2838 { 2839 BUG_ON(!md->internal_suspend_count); 2840 2841 if (--md->internal_suspend_count) 2842 return; /* resume from nested internal suspend */ 2843 2844 if (dm_suspended_md(md)) 2845 goto done; /* resume from nested suspend */ 2846 2847 /* 2848 * NOTE: existing callers don't need to call dm_table_resume_targets 2849 * (which may fail -- so best to avoid it for now by passing NULL map) 2850 */ 2851 (void) __dm_resume(md, NULL); 2852 2853 done: 2854 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2855 smp_mb__after_atomic(); 2856 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2857 } 2858 2859 void dm_internal_suspend_noflush(struct mapped_device *md) 2860 { 2861 mutex_lock(&md->suspend_lock); 2862 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2863 mutex_unlock(&md->suspend_lock); 2864 } 2865 EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2866 2867 void dm_internal_resume(struct mapped_device *md) 2868 { 2869 mutex_lock(&md->suspend_lock); 2870 __dm_internal_resume(md); 2871 mutex_unlock(&md->suspend_lock); 2872 } 2873 EXPORT_SYMBOL_GPL(dm_internal_resume); 2874 2875 /* 2876 * Fast variants of internal suspend/resume hold md->suspend_lock, 2877 * which prevents interaction with userspace-driven suspend. 2878 */ 2879 2880 void dm_internal_suspend_fast(struct mapped_device *md) 2881 { 2882 mutex_lock(&md->suspend_lock); 2883 if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2884 return; 2885 2886 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2887 synchronize_srcu(&md->io_barrier); 2888 flush_workqueue(md->wq); 2889 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2890 } 2891 EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2892 2893 void dm_internal_resume_fast(struct mapped_device *md) 2894 { 2895 if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2896 goto done; 2897 2898 dm_queue_flush(md); 2899 2900 done: 2901 mutex_unlock(&md->suspend_lock); 2902 } 2903 EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2904 2905 /*----------------------------------------------------------------- 2906 * Event notification. 2907 *---------------------------------------------------------------*/ 2908 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 2909 unsigned cookie) 2910 { 2911 int r; 2912 unsigned noio_flag; 2913 char udev_cookie[DM_COOKIE_LENGTH]; 2914 char *envp[] = { udev_cookie, NULL }; 2915 2916 noio_flag = memalloc_noio_save(); 2917 2918 if (!cookie) 2919 r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 2920 else { 2921 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 2922 DM_COOKIE_ENV_VAR_NAME, cookie); 2923 r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 2924 action, envp); 2925 } 2926 2927 memalloc_noio_restore(noio_flag); 2928 2929 return r; 2930 } 2931 2932 uint32_t dm_next_uevent_seq(struct mapped_device *md) 2933 { 2934 return atomic_add_return(1, &md->uevent_seq); 2935 } 2936 2937 uint32_t dm_get_event_nr(struct mapped_device *md) 2938 { 2939 return atomic_read(&md->event_nr); 2940 } 2941 2942 int dm_wait_event(struct mapped_device *md, int event_nr) 2943 { 2944 return wait_event_interruptible(md->eventq, 2945 (event_nr != atomic_read(&md->event_nr))); 2946 } 2947 2948 void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 2949 { 2950 unsigned long flags; 2951 2952 spin_lock_irqsave(&md->uevent_lock, flags); 2953 list_add(elist, &md->uevent_list); 2954 spin_unlock_irqrestore(&md->uevent_lock, flags); 2955 } 2956 2957 /* 2958 * The gendisk is only valid as long as you have a reference 2959 * count on 'md'. 2960 */ 2961 struct gendisk *dm_disk(struct mapped_device *md) 2962 { 2963 return md->disk; 2964 } 2965 EXPORT_SYMBOL_GPL(dm_disk); 2966 2967 struct kobject *dm_kobject(struct mapped_device *md) 2968 { 2969 return &md->kobj_holder.kobj; 2970 } 2971 2972 struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2973 { 2974 struct mapped_device *md; 2975 2976 md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2977 2978 spin_lock(&_minor_lock); 2979 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2980 md = NULL; 2981 goto out; 2982 } 2983 dm_get(md); 2984 out: 2985 spin_unlock(&_minor_lock); 2986 2987 return md; 2988 } 2989 2990 int dm_suspended_md(struct mapped_device *md) 2991 { 2992 return test_bit(DMF_SUSPENDED, &md->flags); 2993 } 2994 2995 static int dm_post_suspending_md(struct mapped_device *md) 2996 { 2997 return test_bit(DMF_POST_SUSPENDING, &md->flags); 2998 } 2999 3000 int dm_suspended_internally_md(struct mapped_device *md) 3001 { 3002 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3003 } 3004 3005 int dm_test_deferred_remove_flag(struct mapped_device *md) 3006 { 3007 return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 3008 } 3009 3010 int dm_suspended(struct dm_target *ti) 3011 { 3012 return dm_suspended_md(dm_table_get_md(ti->table)); 3013 } 3014 EXPORT_SYMBOL_GPL(dm_suspended); 3015 3016 int dm_post_suspending(struct dm_target *ti) 3017 { 3018 return dm_post_suspending_md(dm_table_get_md(ti->table)); 3019 } 3020 EXPORT_SYMBOL_GPL(dm_post_suspending); 3021 3022 int dm_noflush_suspending(struct dm_target *ti) 3023 { 3024 return __noflush_suspending(dm_table_get_md(ti->table)); 3025 } 3026 EXPORT_SYMBOL_GPL(dm_noflush_suspending); 3027 3028 struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 3029 unsigned integrity, unsigned per_io_data_size, 3030 unsigned min_pool_size) 3031 { 3032 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 3033 unsigned int pool_size = 0; 3034 unsigned int front_pad, io_front_pad; 3035 int ret; 3036 3037 if (!pools) 3038 return NULL; 3039 3040 switch (type) { 3041 case DM_TYPE_BIO_BASED: 3042 case DM_TYPE_DAX_BIO_BASED: 3043 case DM_TYPE_NVME_BIO_BASED: 3044 pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 3045 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); 3046 io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio); 3047 ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 3048 if (ret) 3049 goto out; 3050 if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 3051 goto out; 3052 break; 3053 case DM_TYPE_REQUEST_BASED: 3054 pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 3055 front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 3056 /* per_io_data_size is used for blk-mq pdu at queue allocation */ 3057 break; 3058 default: 3059 BUG(); 3060 } 3061 3062 ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 3063 if (ret) 3064 goto out; 3065 3066 if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 3067 goto out; 3068 3069 return pools; 3070 3071 out: 3072 dm_free_md_mempools(pools); 3073 3074 return NULL; 3075 } 3076 3077 void dm_free_md_mempools(struct dm_md_mempools *pools) 3078 { 3079 if (!pools) 3080 return; 3081 3082 bioset_exit(&pools->bs); 3083 bioset_exit(&pools->io_bs); 3084 3085 kfree(pools); 3086 } 3087 3088 struct dm_pr { 3089 u64 old_key; 3090 u64 new_key; 3091 u32 flags; 3092 bool fail_early; 3093 }; 3094 3095 static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 3096 void *data) 3097 { 3098 struct mapped_device *md = bdev->bd_disk->private_data; 3099 struct dm_table *table; 3100 struct dm_target *ti; 3101 int ret = -ENOTTY, srcu_idx; 3102 3103 table = dm_get_live_table(md, &srcu_idx); 3104 if (!table || !dm_table_get_size(table)) 3105 goto out; 3106 3107 /* We only support devices that have a single target */ 3108 if (dm_table_get_num_targets(table) != 1) 3109 goto out; 3110 ti = dm_table_get_target(table, 0); 3111 3112 ret = -EINVAL; 3113 if (!ti->type->iterate_devices) 3114 goto out; 3115 3116 ret = ti->type->iterate_devices(ti, fn, data); 3117 out: 3118 dm_put_live_table(md, srcu_idx); 3119 return ret; 3120 } 3121 3122 /* 3123 * For register / unregister we need to manually call out to every path. 3124 */ 3125 static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 3126 sector_t start, sector_t len, void *data) 3127 { 3128 struct dm_pr *pr = data; 3129 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 3130 3131 if (!ops || !ops->pr_register) 3132 return -EOPNOTSUPP; 3133 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 3134 } 3135 3136 static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 3137 u32 flags) 3138 { 3139 struct dm_pr pr = { 3140 .old_key = old_key, 3141 .new_key = new_key, 3142 .flags = flags, 3143 .fail_early = true, 3144 }; 3145 int ret; 3146 3147 ret = dm_call_pr(bdev, __dm_pr_register, &pr); 3148 if (ret && new_key) { 3149 /* unregister all paths if we failed to register any path */ 3150 pr.old_key = new_key; 3151 pr.new_key = 0; 3152 pr.flags = 0; 3153 pr.fail_early = false; 3154 dm_call_pr(bdev, __dm_pr_register, &pr); 3155 } 3156 3157 return ret; 3158 } 3159 3160 static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 3161 u32 flags) 3162 { 3163 struct mapped_device *md = bdev->bd_disk->private_data; 3164 const struct pr_ops *ops; 3165 int r, srcu_idx; 3166 3167 r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 3168 if (r < 0) 3169 goto out; 3170 3171 ops = bdev->bd_disk->fops->pr_ops; 3172 if (ops && ops->pr_reserve) 3173 r = ops->pr_reserve(bdev, key, type, flags); 3174 else 3175 r = -EOPNOTSUPP; 3176 out: 3177 dm_unprepare_ioctl(md, srcu_idx); 3178 return r; 3179 } 3180 3181 static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 3182 { 3183 struct mapped_device *md = bdev->bd_disk->private_data; 3184 const struct pr_ops *ops; 3185 int r, srcu_idx; 3186 3187 r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 3188 if (r < 0) 3189 goto out; 3190 3191 ops = bdev->bd_disk->fops->pr_ops; 3192 if (ops && ops->pr_release) 3193 r = ops->pr_release(bdev, key, type); 3194 else 3195 r = -EOPNOTSUPP; 3196 out: 3197 dm_unprepare_ioctl(md, srcu_idx); 3198 return r; 3199 } 3200 3201 static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 3202 enum pr_type type, bool abort) 3203 { 3204 struct mapped_device *md = bdev->bd_disk->private_data; 3205 const struct pr_ops *ops; 3206 int r, srcu_idx; 3207 3208 r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 3209 if (r < 0) 3210 goto out; 3211 3212 ops = bdev->bd_disk->fops->pr_ops; 3213 if (ops && ops->pr_preempt) 3214 r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 3215 else 3216 r = -EOPNOTSUPP; 3217 out: 3218 dm_unprepare_ioctl(md, srcu_idx); 3219 return r; 3220 } 3221 3222 static int dm_pr_clear(struct block_device *bdev, u64 key) 3223 { 3224 struct mapped_device *md = bdev->bd_disk->private_data; 3225 const struct pr_ops *ops; 3226 int r, srcu_idx; 3227 3228 r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 3229 if (r < 0) 3230 goto out; 3231 3232 ops = bdev->bd_disk->fops->pr_ops; 3233 if (ops && ops->pr_clear) 3234 r = ops->pr_clear(bdev, key); 3235 else 3236 r = -EOPNOTSUPP; 3237 out: 3238 dm_unprepare_ioctl(md, srcu_idx); 3239 return r; 3240 } 3241 3242 static const struct pr_ops dm_pr_ops = { 3243 .pr_register = dm_pr_register, 3244 .pr_reserve = dm_pr_reserve, 3245 .pr_release = dm_pr_release, 3246 .pr_preempt = dm_pr_preempt, 3247 .pr_clear = dm_pr_clear, 3248 }; 3249 3250 static const struct block_device_operations dm_blk_dops = { 3251 .submit_bio = dm_submit_bio, 3252 .open = dm_blk_open, 3253 .release = dm_blk_close, 3254 .ioctl = dm_blk_ioctl, 3255 .getgeo = dm_blk_getgeo, 3256 .report_zones = dm_blk_report_zones, 3257 .pr_ops = &dm_pr_ops, 3258 .owner = THIS_MODULE 3259 }; 3260 3261 static const struct dax_operations dm_dax_ops = { 3262 .direct_access = dm_dax_direct_access, 3263 .dax_supported = dm_dax_supported, 3264 .copy_from_iter = dm_dax_copy_from_iter, 3265 .copy_to_iter = dm_dax_copy_to_iter, 3266 .zero_page_range = dm_dax_zero_page_range, 3267 }; 3268 3269 /* 3270 * module hooks 3271 */ 3272 module_init(dm_init); 3273 module_exit(dm_exit); 3274 3275 module_param(major, uint, 0); 3276 MODULE_PARM_DESC(major, "The major number of the device mapper"); 3277 3278 module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3279 MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3280 3281 module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3282 MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3283 3284 MODULE_DESCRIPTION(DM_NAME " driver"); 3285 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 3286 MODULE_LICENSE("GPL"); 3287