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