1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2003 Sistina Software Limited. 4 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 5 * 6 * This file is released under the GPL. 7 */ 8 9 #include <linux/device-mapper.h> 10 11 #include "dm-rq.h" 12 #include "dm-bio-record.h" 13 #include "dm-path-selector.h" 14 #include "dm-uevent.h" 15 16 #include <linux/blkdev.h> 17 #include <linux/ctype.h> 18 #include <linux/init.h> 19 #include <linux/mempool.h> 20 #include <linux/module.h> 21 #include <linux/pagemap.h> 22 #include <linux/slab.h> 23 #include <linux/time.h> 24 #include <linux/timer.h> 25 #include <linux/workqueue.h> 26 #include <linux/delay.h> 27 #include <scsi/scsi_dh.h> 28 #include <linux/atomic.h> 29 #include <linux/blk-mq.h> 30 31 static struct workqueue_struct *dm_mpath_wq; 32 33 #define DM_MSG_PREFIX "multipath" 34 #define DM_PG_INIT_DELAY_MSECS 2000 35 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned int) -1) 36 #define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0 37 38 static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT; 39 40 /* Path properties */ 41 struct pgpath { 42 struct list_head list; 43 44 struct priority_group *pg; /* Owning PG */ 45 unsigned int fail_count; /* Cumulative failure count */ 46 47 struct dm_path path; 48 struct delayed_work activate_path; 49 50 bool is_active:1; /* Path status */ 51 }; 52 53 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 54 55 /* 56 * Paths are grouped into Priority Groups and numbered from 1 upwards. 57 * Each has a path selector which controls which path gets used. 58 */ 59 struct priority_group { 60 struct list_head list; 61 62 struct multipath *m; /* Owning multipath instance */ 63 struct path_selector ps; 64 65 unsigned int pg_num; /* Reference number */ 66 unsigned int nr_pgpaths; /* Number of paths in PG */ 67 struct list_head pgpaths; 68 69 bool bypassed:1; /* Temporarily bypass this PG? */ 70 }; 71 72 /* Multipath context */ 73 struct multipath { 74 unsigned long flags; /* Multipath state flags */ 75 76 spinlock_t lock; 77 enum dm_queue_mode queue_mode; 78 79 struct pgpath *current_pgpath; 80 struct priority_group *current_pg; 81 struct priority_group *next_pg; /* Switch to this PG if set */ 82 struct priority_group *last_probed_pg; 83 84 atomic_t nr_valid_paths; /* Total number of usable paths */ 85 unsigned int nr_priority_groups; 86 struct list_head priority_groups; 87 88 const char *hw_handler_name; 89 char *hw_handler_params; 90 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 91 wait_queue_head_t probe_wait; /* Wait for probing paths */ 92 unsigned int pg_init_retries; /* Number of times to retry pg_init */ 93 unsigned int pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 94 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 95 atomic_t pg_init_count; /* Number of times pg_init called */ 96 97 struct mutex work_mutex; 98 struct work_struct trigger_event; 99 struct dm_target *ti; 100 101 struct work_struct process_queued_bios; 102 struct bio_list queued_bios; 103 104 struct timer_list nopath_timer; /* Timeout for queue_if_no_path */ 105 bool is_suspending; 106 }; 107 108 /* 109 * Context information attached to each io we process. 110 */ 111 struct dm_mpath_io { 112 struct pgpath *pgpath; 113 size_t nr_bytes; 114 u64 start_time_ns; 115 }; 116 117 typedef int (*action_fn) (struct pgpath *pgpath); 118 119 static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 120 static void trigger_event(struct work_struct *work); 121 static void activate_or_offline_path(struct pgpath *pgpath); 122 static void activate_path_work(struct work_struct *work); 123 static void process_queued_bios(struct work_struct *work); 124 static void queue_if_no_path_timeout_work(struct timer_list *t); 125 126 /* 127 *----------------------------------------------- 128 * Multipath state flags. 129 *----------------------------------------------- 130 */ 131 #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 132 #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 133 #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 134 /* MPATHF_RETAIN_ATTACHED_HW_HANDLER no longer has any effect */ 135 #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 136 #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 137 #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 138 #define MPATHF_DELAY_PG_SWITCH 7 /* Delay switching pg if it still has paths */ 139 #define MPATHF_NEED_PG_SWITCH 8 /* Need to switch pgs after the delay has ended */ 140 141 static bool mpath_double_check_test_bit(int MPATHF_bit, struct multipath *m) 142 { 143 bool r = test_bit(MPATHF_bit, &m->flags); 144 145 if (r) { 146 unsigned long flags; 147 148 spin_lock_irqsave(&m->lock, flags); 149 r = test_bit(MPATHF_bit, &m->flags); 150 spin_unlock_irqrestore(&m->lock, flags); 151 } 152 153 return r; 154 } 155 156 /* 157 *----------------------------------------------- 158 * Allocation routines 159 *----------------------------------------------- 160 */ 161 static struct pgpath *alloc_pgpath(void) 162 { 163 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 164 165 if (!pgpath) 166 return NULL; 167 168 pgpath->is_active = true; 169 170 return pgpath; 171 } 172 173 static void free_pgpath(struct pgpath *pgpath) 174 { 175 kfree(pgpath); 176 } 177 178 static struct priority_group *alloc_priority_group(void) 179 { 180 struct priority_group *pg; 181 182 pg = kzalloc(sizeof(*pg), GFP_KERNEL); 183 184 if (pg) 185 INIT_LIST_HEAD(&pg->pgpaths); 186 187 return pg; 188 } 189 190 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 191 { 192 struct pgpath *pgpath, *tmp; 193 194 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 195 list_del(&pgpath->list); 196 dm_put_device(ti, pgpath->path.dev); 197 free_pgpath(pgpath); 198 } 199 } 200 201 static void free_priority_group(struct priority_group *pg, 202 struct dm_target *ti) 203 { 204 struct path_selector *ps = &pg->ps; 205 206 if (ps->type) { 207 ps->type->destroy(ps); 208 dm_put_path_selector(ps->type); 209 } 210 211 free_pgpaths(&pg->pgpaths, ti); 212 kfree(pg); 213 } 214 215 static struct multipath *alloc_multipath(struct dm_target *ti) 216 { 217 struct multipath *m; 218 219 m = kzalloc(sizeof(*m), GFP_KERNEL); 220 if (m) { 221 INIT_LIST_HEAD(&m->priority_groups); 222 spin_lock_init(&m->lock); 223 atomic_set(&m->nr_valid_paths, 0); 224 INIT_WORK(&m->trigger_event, trigger_event); 225 mutex_init(&m->work_mutex); 226 227 m->queue_mode = DM_TYPE_NONE; 228 229 m->ti = ti; 230 ti->private = m; 231 232 timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0); 233 } 234 235 return m; 236 } 237 238 static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 239 { 240 if (m->queue_mode == DM_TYPE_NONE) 241 m->queue_mode = DM_TYPE_REQUEST_BASED; 242 else if (m->queue_mode == DM_TYPE_BIO_BASED) 243 INIT_WORK(&m->process_queued_bios, process_queued_bios); 244 245 dm_table_set_type(ti->table, m->queue_mode); 246 247 /* 248 * Init fields that are only used when a scsi_dh is attached 249 * - must do this unconditionally (really doesn't hurt non-SCSI uses) 250 */ 251 set_bit(MPATHF_QUEUE_IO, &m->flags); 252 atomic_set(&m->pg_init_in_progress, 0); 253 atomic_set(&m->pg_init_count, 0); 254 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 255 init_waitqueue_head(&m->pg_init_wait); 256 init_waitqueue_head(&m->probe_wait); 257 258 return 0; 259 } 260 261 static void free_multipath(struct multipath *m) 262 { 263 struct priority_group *pg, *tmp; 264 265 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 266 list_del(&pg->list); 267 free_priority_group(pg, m->ti); 268 } 269 270 kfree(m->hw_handler_name); 271 kfree(m->hw_handler_params); 272 mutex_destroy(&m->work_mutex); 273 kfree(m); 274 } 275 276 static struct dm_mpath_io *get_mpio(union map_info *info) 277 { 278 return info->ptr; 279 } 280 281 static size_t multipath_per_bio_data_size(void) 282 { 283 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 284 } 285 286 static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 287 { 288 return dm_per_bio_data(bio, multipath_per_bio_data_size()); 289 } 290 291 static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 292 { 293 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 294 void *bio_details = mpio + 1; 295 return bio_details; 296 } 297 298 static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 299 { 300 struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 301 struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 302 303 mpio->nr_bytes = bio->bi_iter.bi_size; 304 mpio->pgpath = NULL; 305 mpio->start_time_ns = 0; 306 *mpio_p = mpio; 307 308 dm_bio_record(bio_details, bio); 309 } 310 311 /* 312 *----------------------------------------------- 313 * Path selection 314 *----------------------------------------------- 315 */ 316 static int __pg_init_all_paths(struct multipath *m) 317 { 318 struct pgpath *pgpath; 319 unsigned long pg_init_delay = 0; 320 321 lockdep_assert_held(&m->lock); 322 323 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 324 return 0; 325 326 atomic_inc(&m->pg_init_count); 327 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 328 329 /* Check here to reset pg_init_required */ 330 if (!m->current_pg) 331 return 0; 332 333 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 334 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 335 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 336 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 337 /* Skip failed paths */ 338 if (!pgpath->is_active) 339 continue; 340 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 341 pg_init_delay)) 342 atomic_inc(&m->pg_init_in_progress); 343 } 344 return atomic_read(&m->pg_init_in_progress); 345 } 346 347 static int pg_init_all_paths(struct multipath *m) 348 { 349 int ret; 350 unsigned long flags; 351 352 spin_lock_irqsave(&m->lock, flags); 353 ret = __pg_init_all_paths(m); 354 spin_unlock_irqrestore(&m->lock, flags); 355 356 return ret; 357 } 358 359 static void __switch_pg(struct multipath *m, struct priority_group *pg) 360 { 361 lockdep_assert_held(&m->lock); 362 363 m->current_pg = pg; 364 365 /* Must we initialise the PG first, and queue I/O till it's ready? */ 366 if (m->hw_handler_name) { 367 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 368 set_bit(MPATHF_QUEUE_IO, &m->flags); 369 } else { 370 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 371 clear_bit(MPATHF_QUEUE_IO, &m->flags); 372 } 373 374 atomic_set(&m->pg_init_count, 0); 375 } 376 377 static struct pgpath *choose_path_in_pg(struct multipath *m, 378 struct priority_group *pg, 379 size_t nr_bytes) 380 { 381 unsigned long flags; 382 struct dm_path *path; 383 struct pgpath *pgpath; 384 385 path = pg->ps.type->select_path(&pg->ps, nr_bytes); 386 if (!path) 387 return ERR_PTR(-ENXIO); 388 389 pgpath = path_to_pgpath(path); 390 391 if (unlikely(READ_ONCE(m->current_pg) != pg)) { 392 /* Only update current_pgpath if pg changed */ 393 spin_lock_irqsave(&m->lock, flags); 394 m->current_pgpath = pgpath; 395 __switch_pg(m, pg); 396 spin_unlock_irqrestore(&m->lock, flags); 397 } 398 399 return pgpath; 400 } 401 402 static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 403 { 404 unsigned long flags; 405 struct priority_group *pg; 406 struct pgpath *pgpath; 407 unsigned int bypassed = 1; 408 409 if (!atomic_read(&m->nr_valid_paths)) { 410 spin_lock_irqsave(&m->lock, flags); 411 clear_bit(MPATHF_QUEUE_IO, &m->flags); 412 spin_unlock_irqrestore(&m->lock, flags); 413 goto failed; 414 } 415 416 /* Don't change PG until it has no remaining paths */ 417 pg = READ_ONCE(m->current_pg); 418 if (pg) { 419 pgpath = choose_path_in_pg(m, pg, nr_bytes); 420 if (!IS_ERR_OR_NULL(pgpath)) 421 return pgpath; 422 } 423 424 /* Were we instructed to switch PG? */ 425 if (READ_ONCE(m->next_pg)) { 426 spin_lock_irqsave(&m->lock, flags); 427 pg = m->next_pg; 428 if (!pg) { 429 spin_unlock_irqrestore(&m->lock, flags); 430 goto check_all_pgs; 431 } 432 m->next_pg = NULL; 433 spin_unlock_irqrestore(&m->lock, flags); 434 pgpath = choose_path_in_pg(m, pg, nr_bytes); 435 if (!IS_ERR_OR_NULL(pgpath)) 436 return pgpath; 437 } 438 check_all_pgs: 439 /* 440 * Loop through priority groups until we find a valid path. 441 * First time we skip PGs marked 'bypassed'. 442 * Second time we only try the ones we skipped, but set 443 * pg_init_delay_retry so we do not hammer controllers. 444 */ 445 do { 446 list_for_each_entry(pg, &m->priority_groups, list) { 447 if (pg->bypassed == !!bypassed) 448 continue; 449 pgpath = choose_path_in_pg(m, pg, nr_bytes); 450 if (!IS_ERR_OR_NULL(pgpath)) { 451 if (!bypassed) { 452 spin_lock_irqsave(&m->lock, flags); 453 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 454 spin_unlock_irqrestore(&m->lock, flags); 455 } 456 return pgpath; 457 } 458 } 459 } while (bypassed--); 460 461 failed: 462 spin_lock_irqsave(&m->lock, flags); 463 m->current_pgpath = NULL; 464 m->current_pg = NULL; 465 spin_unlock_irqrestore(&m->lock, flags); 466 467 return NULL; 468 } 469 470 /* 471 * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited() 472 * report the function name and line number of the function from which 473 * it has been invoked. 474 */ 475 #define dm_report_EIO(m) \ 476 DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \ 477 dm_table_device_name((m)->ti->table), \ 478 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 479 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 480 dm_noflush_suspending((m)->ti)) 481 482 /* 483 * Check whether bios must be queued in the device-mapper core rather 484 * than here in the target. 485 */ 486 static bool __must_push_back(struct multipath *m) 487 { 488 return dm_noflush_suspending(m->ti); 489 } 490 491 static bool must_push_back_rq(struct multipath *m) 492 { 493 unsigned long flags; 494 bool ret; 495 496 spin_lock_irqsave(&m->lock, flags); 497 ret = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m)); 498 spin_unlock_irqrestore(&m->lock, flags); 499 500 return ret; 501 } 502 503 /* 504 * Map cloned requests (request-based multipath) 505 */ 506 static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 507 union map_info *map_context, 508 struct request **__clone) 509 { 510 struct multipath *m = ti->private; 511 size_t nr_bytes = blk_rq_bytes(rq); 512 struct pgpath *pgpath; 513 struct block_device *bdev; 514 struct dm_mpath_io *mpio = get_mpio(map_context); 515 struct request_queue *q; 516 struct request *clone; 517 518 /* Do we need to select a new pgpath? */ 519 pgpath = READ_ONCE(m->current_pgpath); 520 if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 521 pgpath = choose_pgpath(m, nr_bytes); 522 523 if (!pgpath) { 524 if (must_push_back_rq(m)) 525 return DM_MAPIO_DELAY_REQUEUE; 526 dm_report_EIO(m); /* Failed */ 527 return DM_MAPIO_KILL; 528 } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) || 529 mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) { 530 pg_init_all_paths(m); 531 return DM_MAPIO_DELAY_REQUEUE; 532 } 533 534 mpio->pgpath = pgpath; 535 mpio->nr_bytes = nr_bytes; 536 537 bdev = pgpath->path.dev->bdev; 538 q = bdev_get_queue(bdev); 539 clone = blk_mq_alloc_request(q, rq->cmd_flags | REQ_NOMERGE, 540 BLK_MQ_REQ_NOWAIT); 541 if (IS_ERR(clone)) { 542 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 543 if (blk_queue_dying(q)) { 544 atomic_inc(&m->pg_init_in_progress); 545 activate_or_offline_path(pgpath); 546 return DM_MAPIO_DELAY_REQUEUE; 547 } 548 549 /* 550 * blk-mq's SCHED_RESTART can cover this requeue, so we 551 * needn't deal with it by DELAY_REQUEUE. More importantly, 552 * we have to return DM_MAPIO_REQUEUE so that blk-mq can 553 * get the queue busy feedback (via BLK_STS_RESOURCE), 554 * otherwise I/O merging can suffer. 555 */ 556 return DM_MAPIO_REQUEUE; 557 } 558 clone->bio = clone->biotail = NULL; 559 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 560 *__clone = clone; 561 562 if (pgpath->pg->ps.type->start_io) 563 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 564 &pgpath->path, 565 nr_bytes); 566 return DM_MAPIO_REMAPPED; 567 } 568 569 static void multipath_release_clone(struct request *clone, 570 union map_info *map_context) 571 { 572 if (unlikely(map_context)) { 573 /* 574 * non-NULL map_context means caller is still map 575 * method; must undo multipath_clone_and_map() 576 */ 577 struct dm_mpath_io *mpio = get_mpio(map_context); 578 struct pgpath *pgpath = mpio->pgpath; 579 580 if (pgpath && pgpath->pg->ps.type->end_io) 581 pgpath->pg->ps.type->end_io(&pgpath->pg->ps, 582 &pgpath->path, 583 mpio->nr_bytes, 584 clone->io_start_time_ns); 585 } 586 587 blk_mq_free_request(clone); 588 } 589 590 /* 591 * Map cloned bios (bio-based multipath) 592 */ 593 594 static void __multipath_queue_bio(struct multipath *m, struct bio *bio) 595 { 596 /* Queue for the daemon to resubmit */ 597 bio_list_add(&m->queued_bios, bio); 598 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 599 queue_work(kmultipathd, &m->process_queued_bios); 600 } 601 602 static void multipath_queue_bio(struct multipath *m, struct bio *bio) 603 { 604 unsigned long flags; 605 606 spin_lock_irqsave(&m->lock, flags); 607 __multipath_queue_bio(m, bio); 608 spin_unlock_irqrestore(&m->lock, flags); 609 } 610 611 static struct pgpath *__map_bio(struct multipath *m, struct bio *bio) 612 { 613 struct pgpath *pgpath; 614 615 /* Do we need to select a new pgpath? */ 616 pgpath = READ_ONCE(m->current_pgpath); 617 if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 618 pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 619 620 if (!pgpath) { 621 spin_lock_irq(&m->lock); 622 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 623 __multipath_queue_bio(m, bio); 624 pgpath = ERR_PTR(-EAGAIN); 625 } 626 spin_unlock_irq(&m->lock); 627 628 } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) || 629 mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) { 630 multipath_queue_bio(m, bio); 631 pg_init_all_paths(m); 632 return ERR_PTR(-EAGAIN); 633 } 634 635 return pgpath; 636 } 637 638 static int __multipath_map_bio(struct multipath *m, struct bio *bio, 639 struct dm_mpath_io *mpio) 640 { 641 struct pgpath *pgpath = __map_bio(m, bio); 642 643 if (IS_ERR(pgpath)) 644 return DM_MAPIO_SUBMITTED; 645 646 if (!pgpath) { 647 if (__must_push_back(m)) 648 return DM_MAPIO_REQUEUE; 649 dm_report_EIO(m); 650 return DM_MAPIO_KILL; 651 } 652 653 mpio->pgpath = pgpath; 654 655 if (dm_ps_use_hr_timer(pgpath->pg->ps.type)) 656 mpio->start_time_ns = ktime_get_ns(); 657 658 bio->bi_status = 0; 659 bio_set_dev(bio, pgpath->path.dev->bdev); 660 bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 661 662 if (pgpath->pg->ps.type->start_io) 663 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 664 &pgpath->path, 665 mpio->nr_bytes); 666 return DM_MAPIO_REMAPPED; 667 } 668 669 static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 670 { 671 struct multipath *m = ti->private; 672 struct dm_mpath_io *mpio = NULL; 673 674 multipath_init_per_bio_data(bio, &mpio); 675 return __multipath_map_bio(m, bio, mpio); 676 } 677 678 static void process_queued_io_list(struct multipath *m) 679 { 680 if (m->queue_mode == DM_TYPE_REQUEST_BASED) 681 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 682 else if (m->queue_mode == DM_TYPE_BIO_BASED) 683 queue_work(kmultipathd, &m->process_queued_bios); 684 } 685 686 static void process_queued_bios(struct work_struct *work) 687 { 688 int r; 689 struct bio *bio; 690 struct bio_list bios; 691 struct blk_plug plug; 692 struct multipath *m = 693 container_of(work, struct multipath, process_queued_bios); 694 695 bio_list_init(&bios); 696 697 spin_lock_irq(&m->lock); 698 699 if (bio_list_empty(&m->queued_bios)) { 700 spin_unlock_irq(&m->lock); 701 return; 702 } 703 704 bio_list_merge_init(&bios, &m->queued_bios); 705 706 spin_unlock_irq(&m->lock); 707 708 blk_start_plug(&plug); 709 while ((bio = bio_list_pop(&bios))) { 710 struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 711 712 dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 713 r = __multipath_map_bio(m, bio, mpio); 714 switch (r) { 715 case DM_MAPIO_KILL: 716 bio->bi_status = BLK_STS_IOERR; 717 bio_endio(bio); 718 break; 719 case DM_MAPIO_REQUEUE: 720 bio->bi_status = BLK_STS_DM_REQUEUE; 721 bio_endio(bio); 722 break; 723 case DM_MAPIO_REMAPPED: 724 submit_bio_noacct(bio); 725 break; 726 case DM_MAPIO_SUBMITTED: 727 break; 728 default: 729 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 730 } 731 } 732 blk_finish_plug(&plug); 733 } 734 735 /* 736 * If we run out of usable paths, should we queue I/O or error it? 737 */ 738 static int queue_if_no_path(struct multipath *m, bool f_queue_if_no_path, 739 bool save_old_value, const char *caller) 740 { 741 unsigned long flags; 742 bool queue_if_no_path_bit, saved_queue_if_no_path_bit; 743 const char *dm_dev_name = dm_table_device_name(m->ti->table); 744 745 DMDEBUG("%s: %s caller=%s f_queue_if_no_path=%d save_old_value=%d", 746 dm_dev_name, __func__, caller, f_queue_if_no_path, save_old_value); 747 748 spin_lock_irqsave(&m->lock, flags); 749 750 queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 751 saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 752 753 if (save_old_value) { 754 if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) { 755 DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!", 756 dm_dev_name); 757 } else 758 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit); 759 } else if (!f_queue_if_no_path && saved_queue_if_no_path_bit) { 760 /* due to "fail_if_no_path" message, need to honor it. */ 761 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 762 } 763 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, f_queue_if_no_path); 764 765 DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d", 766 dm_dev_name, __func__, 767 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 768 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags), 769 dm_noflush_suspending(m->ti)); 770 771 spin_unlock_irqrestore(&m->lock, flags); 772 773 if (!f_queue_if_no_path) { 774 dm_table_run_md_queue_async(m->ti->table); 775 process_queued_io_list(m); 776 } 777 778 return 0; 779 } 780 781 /* 782 * If the queue_if_no_path timeout fires, turn off queue_if_no_path and 783 * process any queued I/O. 784 */ 785 static void queue_if_no_path_timeout_work(struct timer_list *t) 786 { 787 struct multipath *m = timer_container_of(m, t, nopath_timer); 788 789 DMWARN("queue_if_no_path timeout on %s, failing queued IO", 790 dm_table_device_name(m->ti->table)); 791 queue_if_no_path(m, false, false, __func__); 792 } 793 794 /* 795 * Enable the queue_if_no_path timeout if necessary. 796 * Called with m->lock held. 797 */ 798 static void enable_nopath_timeout(struct multipath *m) 799 { 800 unsigned long queue_if_no_path_timeout = 801 READ_ONCE(queue_if_no_path_timeout_secs) * HZ; 802 803 lockdep_assert_held(&m->lock); 804 805 if (queue_if_no_path_timeout > 0 && 806 atomic_read(&m->nr_valid_paths) == 0 && 807 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 808 mod_timer(&m->nopath_timer, 809 jiffies + queue_if_no_path_timeout); 810 } 811 } 812 813 static void disable_nopath_timeout(struct multipath *m) 814 { 815 timer_delete_sync(&m->nopath_timer); 816 } 817 818 /* 819 * An event is triggered whenever a path is taken out of use. 820 * Includes path failure and PG bypass. 821 */ 822 static void trigger_event(struct work_struct *work) 823 { 824 struct multipath *m = 825 container_of(work, struct multipath, trigger_event); 826 827 dm_table_event(m->ti->table); 828 } 829 830 /* 831 *--------------------------------------------------------------- 832 * Constructor/argument parsing: 833 * <#multipath feature args> [<arg>]* 834 * <#hw_handler args> [hw_handler [<arg>]*] 835 * <#priority groups> 836 * <initial priority group> 837 * [<selector> <#selector args> [<arg>]* 838 * <#paths> <#per-path selector args> 839 * [<path> [<arg>]* ]+ ]+ 840 *--------------------------------------------------------------- 841 */ 842 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 843 struct dm_target *ti) 844 { 845 int r; 846 struct path_selector_type *pst; 847 unsigned int ps_argc; 848 849 static const struct dm_arg _args[] = { 850 {0, 1024, "invalid number of path selector args"}, 851 }; 852 853 pst = dm_get_path_selector(dm_shift_arg(as)); 854 if (!pst) { 855 ti->error = "unknown path selector type"; 856 return -EINVAL; 857 } 858 859 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 860 if (r) { 861 dm_put_path_selector(pst); 862 return -EINVAL; 863 } 864 865 r = pst->create(&pg->ps, ps_argc, as->argv); 866 if (r) { 867 dm_put_path_selector(pst); 868 ti->error = "path selector constructor failed"; 869 return r; 870 } 871 872 pg->ps.type = pst; 873 dm_consume_args(as, ps_argc); 874 875 return 0; 876 } 877 878 static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, 879 const char **attached_handler_name, char **error) 880 { 881 struct request_queue *q = bdev_get_queue(bdev); 882 int r; 883 884 if (*attached_handler_name) { 885 /* 886 * Clear any hw_handler_params associated with a 887 * handler that isn't already attached. 888 */ 889 if (m->hw_handler_name && strcmp(*attached_handler_name, 890 m->hw_handler_name)) { 891 kfree(m->hw_handler_params); 892 m->hw_handler_params = NULL; 893 } 894 895 /* 896 * Reset hw_handler_name to match the attached handler 897 * 898 * NB. This modifies the table line to show the actual 899 * handler instead of the original table passed in. 900 */ 901 kfree(m->hw_handler_name); 902 m->hw_handler_name = *attached_handler_name; 903 *attached_handler_name = NULL; 904 } 905 906 if (m->hw_handler_name) { 907 r = scsi_dh_attach(q, m->hw_handler_name); 908 if (r < 0) { 909 *error = "error attaching hardware handler"; 910 return r; 911 } 912 913 if (m->hw_handler_params) { 914 r = scsi_dh_set_params(q, m->hw_handler_params); 915 if (r < 0) { 916 *error = "unable to set hardware handler parameters"; 917 return r; 918 } 919 } 920 } 921 922 return 0; 923 } 924 925 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 926 struct dm_target *ti) 927 { 928 int r; 929 struct pgpath *p; 930 struct multipath *m = ti->private; 931 struct request_queue *q; 932 const char *attached_handler_name = NULL; 933 934 /* we need at least a path arg */ 935 if (as->argc < 1) { 936 ti->error = "no device given"; 937 return ERR_PTR(-EINVAL); 938 } 939 940 p = alloc_pgpath(); 941 if (!p) 942 return ERR_PTR(-ENOMEM); 943 944 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 945 &p->path.dev); 946 if (r) { 947 ti->error = "error getting device"; 948 goto bad; 949 } 950 951 q = bdev_get_queue(p->path.dev->bdev); 952 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 953 if (IS_ERR(attached_handler_name)) { 954 if (PTR_ERR(attached_handler_name) == -ENODEV) { 955 if (m->hw_handler_name) { 956 DMERR("hardware handlers are only allowed for SCSI devices"); 957 kfree(m->hw_handler_name); 958 m->hw_handler_name = NULL; 959 } 960 attached_handler_name = NULL; 961 } else { 962 r = PTR_ERR(attached_handler_name); 963 goto bad; 964 } 965 } 966 if (attached_handler_name || m->hw_handler_name) { 967 INIT_DELAYED_WORK(&p->activate_path, activate_path_work); 968 r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error); 969 kfree(attached_handler_name); 970 if (r) { 971 dm_put_device(ti, p->path.dev); 972 goto bad; 973 } 974 } 975 976 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 977 if (r) { 978 dm_put_device(ti, p->path.dev); 979 goto bad; 980 } 981 982 return p; 983 bad: 984 free_pgpath(p); 985 return ERR_PTR(r); 986 } 987 988 static struct priority_group *parse_priority_group(struct dm_arg_set *as, 989 struct multipath *m) 990 { 991 static const struct dm_arg _args[] = { 992 {1, 1024, "invalid number of paths"}, 993 {0, 1024, "invalid number of selector args"} 994 }; 995 996 int r; 997 unsigned int i, nr_selector_args, nr_args; 998 struct priority_group *pg; 999 struct dm_target *ti = m->ti; 1000 1001 if (as->argc < 2) { 1002 as->argc = 0; 1003 ti->error = "not enough priority group arguments"; 1004 return ERR_PTR(-EINVAL); 1005 } 1006 1007 pg = alloc_priority_group(); 1008 if (!pg) { 1009 ti->error = "couldn't allocate priority group"; 1010 return ERR_PTR(-ENOMEM); 1011 } 1012 pg->m = m; 1013 1014 r = parse_path_selector(as, pg, ti); 1015 if (r) 1016 goto bad; 1017 1018 /* 1019 * read the paths 1020 */ 1021 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 1022 if (r) 1023 goto bad; 1024 1025 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 1026 if (r) 1027 goto bad; 1028 1029 nr_args = 1 + nr_selector_args; 1030 for (i = 0; i < pg->nr_pgpaths; i++) { 1031 struct pgpath *pgpath; 1032 struct dm_arg_set path_args; 1033 1034 if (as->argc < nr_args) { 1035 ti->error = "not enough path parameters"; 1036 r = -EINVAL; 1037 goto bad; 1038 } 1039 1040 path_args.argc = nr_args; 1041 path_args.argv = as->argv; 1042 1043 pgpath = parse_path(&path_args, &pg->ps, ti); 1044 if (IS_ERR(pgpath)) { 1045 r = PTR_ERR(pgpath); 1046 goto bad; 1047 } 1048 1049 pgpath->pg = pg; 1050 list_add_tail(&pgpath->list, &pg->pgpaths); 1051 dm_consume_args(as, nr_args); 1052 } 1053 1054 return pg; 1055 1056 bad: 1057 free_priority_group(pg, ti); 1058 return ERR_PTR(r); 1059 } 1060 1061 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 1062 { 1063 unsigned int hw_argc; 1064 int ret; 1065 struct dm_target *ti = m->ti; 1066 1067 static const struct dm_arg _args[] = { 1068 {0, 1024, "invalid number of hardware handler args"}, 1069 }; 1070 1071 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 1072 return -EINVAL; 1073 1074 if (!hw_argc) 1075 return 0; 1076 1077 if (m->queue_mode == DM_TYPE_BIO_BASED) { 1078 dm_consume_args(as, hw_argc); 1079 DMERR("bio-based multipath doesn't allow hardware handler args"); 1080 return 0; 1081 } 1082 1083 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 1084 if (!m->hw_handler_name) 1085 return -EINVAL; 1086 1087 if (hw_argc > 1) { 1088 char *p; 1089 int i, j, len = 4; 1090 1091 for (i = 0; i <= hw_argc - 2; i++) 1092 len += strlen(as->argv[i]) + 1; 1093 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 1094 if (!p) { 1095 ti->error = "memory allocation failed"; 1096 ret = -ENOMEM; 1097 goto fail; 1098 } 1099 j = sprintf(p, "%d", hw_argc - 1); 1100 for (i = 0, p += j + 1; i <= hw_argc - 2; i++, p += j + 1) 1101 j = sprintf(p, "%s", as->argv[i]); 1102 } 1103 dm_consume_args(as, hw_argc - 1); 1104 1105 return 0; 1106 fail: 1107 kfree(m->hw_handler_name); 1108 m->hw_handler_name = NULL; 1109 return ret; 1110 } 1111 1112 static int parse_features(struct dm_arg_set *as, struct multipath *m) 1113 { 1114 int r; 1115 unsigned int argc; 1116 struct dm_target *ti = m->ti; 1117 const char *arg_name; 1118 1119 static const struct dm_arg _args[] = { 1120 {0, 8, "invalid number of feature args"}, 1121 {1, 50, "pg_init_retries must be between 1 and 50"}, 1122 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 1123 }; 1124 1125 r = dm_read_arg_group(_args, as, &argc, &ti->error); 1126 if (r) 1127 return -EINVAL; 1128 1129 if (!argc) 1130 return 0; 1131 1132 do { 1133 arg_name = dm_shift_arg(as); 1134 argc--; 1135 1136 if (!strcasecmp(arg_name, "queue_if_no_path")) { 1137 r = queue_if_no_path(m, true, false, __func__); 1138 continue; 1139 } 1140 1141 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1142 /* no longer has any effect */ 1143 continue; 1144 } 1145 1146 if (!strcasecmp(arg_name, "pg_init_retries") && 1147 (argc >= 1)) { 1148 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1149 argc--; 1150 continue; 1151 } 1152 1153 if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 1154 (argc >= 1)) { 1155 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 1156 argc--; 1157 continue; 1158 } 1159 1160 if (!strcasecmp(arg_name, "queue_mode") && 1161 (argc >= 1)) { 1162 const char *queue_mode_name = dm_shift_arg(as); 1163 1164 if (!strcasecmp(queue_mode_name, "bio")) 1165 m->queue_mode = DM_TYPE_BIO_BASED; 1166 else if (!strcasecmp(queue_mode_name, "rq") || 1167 !strcasecmp(queue_mode_name, "mq")) 1168 m->queue_mode = DM_TYPE_REQUEST_BASED; 1169 else { 1170 ti->error = "Unknown 'queue_mode' requested"; 1171 r = -EINVAL; 1172 } 1173 argc--; 1174 continue; 1175 } 1176 1177 ti->error = "Unrecognised multipath feature request"; 1178 r = -EINVAL; 1179 } while (argc && !r); 1180 1181 return r; 1182 } 1183 1184 static int multipath_ctr(struct dm_target *ti, unsigned int argc, char **argv) 1185 { 1186 /* target arguments */ 1187 static const struct dm_arg _args[] = { 1188 {0, 1024, "invalid number of priority groups"}, 1189 {0, 1024, "invalid initial priority group number"}, 1190 }; 1191 1192 int r; 1193 struct multipath *m; 1194 struct dm_arg_set as; 1195 unsigned int pg_count = 0; 1196 unsigned int next_pg_num; 1197 1198 as.argc = argc; 1199 as.argv = argv; 1200 1201 m = alloc_multipath(ti); 1202 if (!m) { 1203 ti->error = "can't allocate multipath"; 1204 return -EINVAL; 1205 } 1206 1207 r = parse_features(&as, m); 1208 if (r) 1209 goto bad; 1210 1211 r = alloc_multipath_stage2(ti, m); 1212 if (r) 1213 goto bad; 1214 1215 r = parse_hw_handler(&as, m); 1216 if (r) 1217 goto bad; 1218 1219 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 1220 if (r) 1221 goto bad; 1222 1223 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 1224 if (r) 1225 goto bad; 1226 1227 if ((!m->nr_priority_groups && next_pg_num) || 1228 (m->nr_priority_groups && !next_pg_num)) { 1229 ti->error = "invalid initial priority group"; 1230 r = -EINVAL; 1231 goto bad; 1232 } 1233 1234 /* parse the priority groups */ 1235 while (as.argc) { 1236 struct priority_group *pg; 1237 unsigned int nr_valid_paths = atomic_read(&m->nr_valid_paths); 1238 1239 pg = parse_priority_group(&as, m); 1240 if (IS_ERR(pg)) { 1241 r = PTR_ERR(pg); 1242 goto bad; 1243 } 1244 1245 nr_valid_paths += pg->nr_pgpaths; 1246 atomic_set(&m->nr_valid_paths, nr_valid_paths); 1247 1248 list_add_tail(&pg->list, &m->priority_groups); 1249 pg_count++; 1250 pg->pg_num = pg_count; 1251 if (!--next_pg_num) 1252 m->next_pg = pg; 1253 } 1254 1255 if (pg_count != m->nr_priority_groups) { 1256 ti->error = "priority group count mismatch"; 1257 r = -EINVAL; 1258 goto bad; 1259 } 1260 1261 spin_lock_irq(&m->lock); 1262 enable_nopath_timeout(m); 1263 spin_unlock_irq(&m->lock); 1264 1265 ti->num_flush_bios = 1; 1266 ti->num_discard_bios = 1; 1267 ti->num_write_zeroes_bios = 1; 1268 if (m->queue_mode == DM_TYPE_BIO_BASED) 1269 ti->per_io_data_size = multipath_per_bio_data_size(); 1270 else 1271 ti->per_io_data_size = sizeof(struct dm_mpath_io); 1272 1273 return 0; 1274 1275 bad: 1276 free_multipath(m); 1277 return r; 1278 } 1279 1280 static void multipath_wait_for_pg_init_completion(struct multipath *m) 1281 { 1282 DEFINE_WAIT(wait); 1283 1284 while (1) { 1285 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 1286 1287 if (!atomic_read(&m->pg_init_in_progress)) 1288 break; 1289 1290 io_schedule(); 1291 } 1292 finish_wait(&m->pg_init_wait, &wait); 1293 } 1294 1295 static void flush_multipath_work(struct multipath *m) 1296 { 1297 if (m->hw_handler_name) { 1298 if (!atomic_read(&m->pg_init_in_progress)) 1299 goto skip; 1300 1301 spin_lock_irq(&m->lock); 1302 if (atomic_read(&m->pg_init_in_progress) && 1303 !test_and_set_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) { 1304 spin_unlock_irq(&m->lock); 1305 1306 flush_workqueue(kmpath_handlerd); 1307 multipath_wait_for_pg_init_completion(m); 1308 1309 spin_lock_irq(&m->lock); 1310 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1311 } 1312 spin_unlock_irq(&m->lock); 1313 } 1314 skip: 1315 if (m->queue_mode == DM_TYPE_BIO_BASED) 1316 flush_work(&m->process_queued_bios); 1317 flush_work(&m->trigger_event); 1318 } 1319 1320 static void multipath_dtr(struct dm_target *ti) 1321 { 1322 struct multipath *m = ti->private; 1323 1324 disable_nopath_timeout(m); 1325 flush_multipath_work(m); 1326 free_multipath(m); 1327 } 1328 1329 /* 1330 * Take a path out of use. 1331 */ 1332 static int fail_path(struct pgpath *pgpath) 1333 { 1334 unsigned long flags; 1335 struct multipath *m = pgpath->pg->m; 1336 1337 spin_lock_irqsave(&m->lock, flags); 1338 1339 if (!pgpath->is_active) 1340 goto out; 1341 1342 DMWARN("%s: Failing path %s.", 1343 dm_table_device_name(m->ti->table), 1344 pgpath->path.dev->name); 1345 1346 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1347 pgpath->is_active = false; 1348 pgpath->fail_count++; 1349 1350 atomic_dec(&m->nr_valid_paths); 1351 1352 if (pgpath == m->current_pgpath) 1353 m->current_pgpath = NULL; 1354 1355 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 1356 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1357 1358 queue_work(dm_mpath_wq, &m->trigger_event); 1359 1360 enable_nopath_timeout(m); 1361 1362 out: 1363 spin_unlock_irqrestore(&m->lock, flags); 1364 1365 return 0; 1366 } 1367 1368 /* 1369 * Reinstate a previously-failed path 1370 */ 1371 static int reinstate_path(struct pgpath *pgpath) 1372 { 1373 int r = 0, run_queue = 0; 1374 struct multipath *m = pgpath->pg->m; 1375 unsigned int nr_valid_paths; 1376 1377 spin_lock_irq(&m->lock); 1378 1379 if (pgpath->is_active) 1380 goto out; 1381 1382 DMWARN("%s: Reinstating path %s.", 1383 dm_table_device_name(m->ti->table), 1384 pgpath->path.dev->name); 1385 1386 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 1387 if (r) 1388 goto out; 1389 1390 pgpath->is_active = true; 1391 1392 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 1393 if (nr_valid_paths == 1) { 1394 m->current_pgpath = NULL; 1395 run_queue = 1; 1396 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 1397 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 1398 atomic_inc(&m->pg_init_in_progress); 1399 } 1400 1401 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 1402 pgpath->path.dev->name, nr_valid_paths); 1403 1404 schedule_work(&m->trigger_event); 1405 1406 out: 1407 spin_unlock_irq(&m->lock); 1408 if (run_queue) { 1409 dm_table_run_md_queue_async(m->ti->table); 1410 process_queued_io_list(m); 1411 } 1412 1413 if (pgpath->is_active) 1414 disable_nopath_timeout(m); 1415 1416 return r; 1417 } 1418 1419 /* 1420 * Fail or reinstate all paths that match the provided struct dm_dev. 1421 */ 1422 static int action_dev(struct multipath *m, dev_t dev, action_fn action) 1423 { 1424 int r = -EINVAL; 1425 struct pgpath *pgpath; 1426 struct priority_group *pg; 1427 1428 list_for_each_entry(pg, &m->priority_groups, list) { 1429 list_for_each_entry(pgpath, &pg->pgpaths, list) { 1430 if (pgpath->path.dev->bdev->bd_dev == dev) 1431 r = action(pgpath); 1432 } 1433 } 1434 1435 return r; 1436 } 1437 1438 /* 1439 * Temporarily try to avoid having to use the specified PG 1440 */ 1441 static void bypass_pg(struct multipath *m, struct priority_group *pg, 1442 bool bypassed, bool can_be_delayed) 1443 { 1444 unsigned long flags; 1445 1446 spin_lock_irqsave(&m->lock, flags); 1447 1448 pg->bypassed = bypassed; 1449 if (can_be_delayed && test_bit(MPATHF_DELAY_PG_SWITCH, &m->flags)) 1450 set_bit(MPATHF_NEED_PG_SWITCH, &m->flags); 1451 else { 1452 m->current_pgpath = NULL; 1453 m->current_pg = NULL; 1454 } 1455 1456 spin_unlock_irqrestore(&m->lock, flags); 1457 1458 schedule_work(&m->trigger_event); 1459 } 1460 1461 /* 1462 * Switch to using the specified PG from the next I/O that gets mapped 1463 */ 1464 static int switch_pg_num(struct multipath *m, const char *pgstr) 1465 { 1466 struct priority_group *pg; 1467 unsigned int pgnum; 1468 char dummy; 1469 1470 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1471 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 1472 DMWARN("invalid PG number supplied to %s", __func__); 1473 return -EINVAL; 1474 } 1475 1476 spin_lock_irq(&m->lock); 1477 list_for_each_entry(pg, &m->priority_groups, list) { 1478 pg->bypassed = false; 1479 if (--pgnum) 1480 continue; 1481 1482 if (test_bit(MPATHF_DELAY_PG_SWITCH, &m->flags)) 1483 set_bit(MPATHF_NEED_PG_SWITCH, &m->flags); 1484 else { 1485 m->current_pgpath = NULL; 1486 m->current_pg = NULL; 1487 } 1488 m->next_pg = pg; 1489 } 1490 spin_unlock_irq(&m->lock); 1491 1492 schedule_work(&m->trigger_event); 1493 return 0; 1494 } 1495 1496 /* 1497 * Set/clear bypassed status of a PG. 1498 * PGs are numbered upwards from 1 in the order they were declared. 1499 */ 1500 static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 1501 { 1502 struct priority_group *pg; 1503 unsigned int pgnum; 1504 char dummy; 1505 1506 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1507 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 1508 DMWARN("invalid PG number supplied to bypass_pg"); 1509 return -EINVAL; 1510 } 1511 1512 list_for_each_entry(pg, &m->priority_groups, list) { 1513 if (!--pgnum) 1514 break; 1515 } 1516 1517 bypass_pg(m, pg, bypassed, true); 1518 return 0; 1519 } 1520 1521 /* 1522 * Should we retry pg_init immediately? 1523 */ 1524 static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1525 { 1526 unsigned long flags; 1527 bool limit_reached = false; 1528 1529 spin_lock_irqsave(&m->lock, flags); 1530 1531 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 1532 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1533 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1534 else 1535 limit_reached = true; 1536 1537 spin_unlock_irqrestore(&m->lock, flags); 1538 1539 return limit_reached; 1540 } 1541 1542 static void pg_init_done(void *data, int errors) 1543 { 1544 struct pgpath *pgpath = data; 1545 struct priority_group *pg = pgpath->pg; 1546 struct multipath *m = pg->m; 1547 unsigned long flags; 1548 bool delay_retry = false; 1549 1550 /* device or driver problems */ 1551 switch (errors) { 1552 case SCSI_DH_OK: 1553 break; 1554 case SCSI_DH_NOSYS: 1555 if (!m->hw_handler_name) { 1556 errors = 0; 1557 break; 1558 } 1559 DMERR("Could not failover the device: Handler scsi_dh_%s " 1560 "Error %d.", m->hw_handler_name, errors); 1561 /* 1562 * Fail path for now, so we do not ping pong 1563 */ 1564 fail_path(pgpath); 1565 break; 1566 case SCSI_DH_DEV_TEMP_BUSY: 1567 /* 1568 * Probably doing something like FW upgrade on the 1569 * controller so try the other pg. 1570 */ 1571 bypass_pg(m, pg, true, false); 1572 break; 1573 case SCSI_DH_RETRY: 1574 /* Wait before retrying. */ 1575 delay_retry = true; 1576 fallthrough; 1577 case SCSI_DH_IMM_RETRY: 1578 case SCSI_DH_RES_TEMP_UNAVAIL: 1579 if (pg_init_limit_reached(m, pgpath)) 1580 fail_path(pgpath); 1581 errors = 0; 1582 break; 1583 case SCSI_DH_DEV_OFFLINED: 1584 default: 1585 /* 1586 * We probably do not want to fail the path for a device 1587 * error, but this is what the old dm did. In future 1588 * patches we can do more advanced handling. 1589 */ 1590 fail_path(pgpath); 1591 } 1592 1593 spin_lock_irqsave(&m->lock, flags); 1594 if (errors) { 1595 if (pgpath == m->current_pgpath) { 1596 DMERR("Could not failover device. Error %d.", errors); 1597 m->current_pgpath = NULL; 1598 m->current_pg = NULL; 1599 } 1600 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1601 pg->bypassed = false; 1602 1603 if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1604 /* Activations of other paths are still on going */ 1605 goto out; 1606 1607 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1608 if (delay_retry) 1609 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1610 else 1611 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1612 1613 if (__pg_init_all_paths(m)) 1614 goto out; 1615 } 1616 clear_bit(MPATHF_QUEUE_IO, &m->flags); 1617 1618 process_queued_io_list(m); 1619 1620 /* 1621 * Wake up any thread waiting to suspend. 1622 */ 1623 wake_up(&m->pg_init_wait); 1624 1625 out: 1626 spin_unlock_irqrestore(&m->lock, flags); 1627 } 1628 1629 static void activate_or_offline_path(struct pgpath *pgpath) 1630 { 1631 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1632 1633 if (pgpath->is_active && !blk_queue_dying(q)) 1634 scsi_dh_activate(q, pg_init_done, pgpath); 1635 else 1636 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1637 } 1638 1639 static void activate_path_work(struct work_struct *work) 1640 { 1641 struct pgpath *pgpath = 1642 container_of(work, struct pgpath, activate_path.work); 1643 1644 activate_or_offline_path(pgpath); 1645 } 1646 1647 static int multipath_end_io(struct dm_target *ti, struct request *clone, 1648 blk_status_t error, union map_info *map_context) 1649 { 1650 struct dm_mpath_io *mpio = get_mpio(map_context); 1651 struct pgpath *pgpath = mpio->pgpath; 1652 int r = DM_ENDIO_DONE; 1653 1654 /* 1655 * We don't queue any clone request inside the multipath target 1656 * during end I/O handling, since those clone requests don't have 1657 * bio clones. If we queue them inside the multipath target, 1658 * we need to make bio clones, that requires memory allocation. 1659 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1660 * don't have bio clones.) 1661 * Instead of queueing the clone request here, we queue the original 1662 * request into dm core, which will remake a clone request and 1663 * clone bios for it and resubmit it later. 1664 */ 1665 if (error && blk_path_error(error)) { 1666 struct multipath *m = ti->private; 1667 1668 if (error == BLK_STS_RESOURCE) 1669 r = DM_ENDIO_DELAY_REQUEUE; 1670 else 1671 r = DM_ENDIO_REQUEUE; 1672 1673 if (pgpath) 1674 fail_path(pgpath); 1675 1676 if (!atomic_read(&m->nr_valid_paths) && 1677 !must_push_back_rq(m)) { 1678 if (error == BLK_STS_IOERR) 1679 dm_report_EIO(m); 1680 /* complete with the original error */ 1681 r = DM_ENDIO_DONE; 1682 } 1683 } 1684 1685 if (pgpath) { 1686 struct path_selector *ps = &pgpath->pg->ps; 1687 1688 if (ps->type->end_io) 1689 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1690 clone->io_start_time_ns); 1691 } 1692 1693 return r; 1694 } 1695 1696 static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 1697 blk_status_t *error) 1698 { 1699 struct multipath *m = ti->private; 1700 struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 1701 struct pgpath *pgpath = mpio->pgpath; 1702 unsigned long flags; 1703 int r = DM_ENDIO_DONE; 1704 1705 if (!*error || !blk_path_error(*error)) 1706 goto done; 1707 1708 if (pgpath) 1709 fail_path(pgpath); 1710 1711 if (!atomic_read(&m->nr_valid_paths)) { 1712 spin_lock_irqsave(&m->lock, flags); 1713 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1714 if (__must_push_back(m)) { 1715 r = DM_ENDIO_REQUEUE; 1716 } else { 1717 dm_report_EIO(m); 1718 *error = BLK_STS_IOERR; 1719 } 1720 spin_unlock_irqrestore(&m->lock, flags); 1721 goto done; 1722 } 1723 spin_unlock_irqrestore(&m->lock, flags); 1724 } 1725 1726 multipath_queue_bio(m, clone); 1727 r = DM_ENDIO_INCOMPLETE; 1728 done: 1729 if (pgpath) { 1730 struct path_selector *ps = &pgpath->pg->ps; 1731 1732 if (ps->type->end_io) 1733 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1734 (mpio->start_time_ns ?: 1735 dm_start_time_ns_from_clone(clone))); 1736 } 1737 1738 return r; 1739 } 1740 1741 /* 1742 * Suspend with flush can't complete until all the I/O is processed 1743 * so if the last path fails we must error any remaining I/O. 1744 * - Note that if the freeze_bdev fails while suspending, the 1745 * queue_if_no_path state is lost - userspace should reset it. 1746 * Otherwise, during noflush suspend, queue_if_no_path will not change. 1747 */ 1748 static void multipath_presuspend(struct dm_target *ti) 1749 { 1750 struct multipath *m = ti->private; 1751 1752 spin_lock_irq(&m->lock); 1753 m->is_suspending = true; 1754 spin_unlock_irq(&m->lock); 1755 /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */ 1756 if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti)) 1757 queue_if_no_path(m, false, true, __func__); 1758 } 1759 1760 static void multipath_postsuspend(struct dm_target *ti) 1761 { 1762 struct multipath *m = ti->private; 1763 1764 mutex_lock(&m->work_mutex); 1765 flush_multipath_work(m); 1766 mutex_unlock(&m->work_mutex); 1767 } 1768 1769 /* 1770 * Restore the queue_if_no_path setting. 1771 */ 1772 static void multipath_resume(struct dm_target *ti) 1773 { 1774 struct multipath *m = ti->private; 1775 1776 spin_lock_irq(&m->lock); 1777 m->is_suspending = false; 1778 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) { 1779 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1780 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 1781 } 1782 1783 DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d", 1784 dm_table_device_name(m->ti->table), __func__, 1785 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 1786 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 1787 1788 spin_unlock_irq(&m->lock); 1789 } 1790 1791 /* 1792 * Info output has the following format: 1793 * num_multipath_feature_args [multipath_feature_args]* 1794 * num_handler_status_args [handler_status_args]* 1795 * num_groups init_group_number 1796 * [A|D|E num_ps_status_args [ps_status_args]* 1797 * num_paths num_selector_args 1798 * [path_dev A|F fail_count [selector_args]* ]+ ]+ 1799 * 1800 * Table output has the following format (identical to the constructor string): 1801 * num_feature_args [features_args]* 1802 * num_handler_args hw_handler [hw_handler_args]* 1803 * num_groups init_group_number 1804 * [priority selector-name num_ps_args [ps_args]* 1805 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 1806 */ 1807 static void multipath_status(struct dm_target *ti, status_type_t type, 1808 unsigned int status_flags, char *result, unsigned int maxlen) 1809 { 1810 int sz = 0, pg_counter, pgpath_counter; 1811 struct multipath *m = ti->private; 1812 struct priority_group *pg; 1813 struct pgpath *p; 1814 unsigned int pg_num; 1815 char state; 1816 1817 spin_lock_irq(&m->lock); 1818 1819 /* Features */ 1820 if (type == STATUSTYPE_INFO) 1821 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 1822 atomic_read(&m->pg_init_count)); 1823 else { 1824 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 1825 (m->pg_init_retries > 0) * 2 + 1826 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1827 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1828 1829 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1830 DMEMIT("queue_if_no_path "); 1831 if (m->pg_init_retries) 1832 DMEMIT("pg_init_retries %u ", m->pg_init_retries); 1833 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 1834 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1835 if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1836 switch (m->queue_mode) { 1837 case DM_TYPE_BIO_BASED: 1838 DMEMIT("queue_mode bio "); 1839 break; 1840 default: 1841 WARN_ON_ONCE(true); 1842 break; 1843 } 1844 } 1845 } 1846 1847 if (!m->hw_handler_name || type == STATUSTYPE_INFO) 1848 DMEMIT("0 "); 1849 else 1850 DMEMIT("1 %s ", m->hw_handler_name); 1851 1852 DMEMIT("%u ", m->nr_priority_groups); 1853 1854 if (m->current_pg) 1855 pg_num = m->current_pg->pg_num; 1856 else if (m->next_pg) 1857 pg_num = m->next_pg->pg_num; 1858 else 1859 pg_num = (m->nr_priority_groups ? 1 : 0); 1860 1861 DMEMIT("%u ", pg_num); 1862 1863 switch (type) { 1864 case STATUSTYPE_INFO: 1865 list_for_each_entry(pg, &m->priority_groups, list) { 1866 if (pg->bypassed) 1867 state = 'D'; /* Disabled */ 1868 else if (pg == m->current_pg) 1869 state = 'A'; /* Currently Active */ 1870 else 1871 state = 'E'; /* Enabled */ 1872 1873 DMEMIT("%c ", state); 1874 1875 if (pg->ps.type->status) 1876 sz += pg->ps.type->status(&pg->ps, NULL, type, 1877 result + sz, 1878 maxlen - sz); 1879 else 1880 DMEMIT("0 "); 1881 1882 DMEMIT("%u %u ", pg->nr_pgpaths, 1883 pg->ps.type->info_args); 1884 1885 list_for_each_entry(p, &pg->pgpaths, list) { 1886 DMEMIT("%s %s %u ", p->path.dev->name, 1887 p->is_active ? "A" : "F", 1888 p->fail_count); 1889 if (pg->ps.type->status) 1890 sz += pg->ps.type->status(&pg->ps, 1891 &p->path, type, result + sz, 1892 maxlen - sz); 1893 } 1894 } 1895 break; 1896 1897 case STATUSTYPE_TABLE: 1898 list_for_each_entry(pg, &m->priority_groups, list) { 1899 DMEMIT("%s ", pg->ps.type->name); 1900 1901 if (pg->ps.type->status) 1902 sz += pg->ps.type->status(&pg->ps, NULL, type, 1903 result + sz, 1904 maxlen - sz); 1905 else 1906 DMEMIT("0 "); 1907 1908 DMEMIT("%u %u ", pg->nr_pgpaths, 1909 pg->ps.type->table_args); 1910 1911 list_for_each_entry(p, &pg->pgpaths, list) { 1912 DMEMIT("%s ", p->path.dev->name); 1913 if (pg->ps.type->status) 1914 sz += pg->ps.type->status(&pg->ps, 1915 &p->path, type, result + sz, 1916 maxlen - sz); 1917 } 1918 } 1919 break; 1920 1921 case STATUSTYPE_IMA: 1922 sz = 0; /*reset the result pointer*/ 1923 1924 DMEMIT_TARGET_NAME_VERSION(ti->type); 1925 DMEMIT(",nr_priority_groups=%u", m->nr_priority_groups); 1926 1927 pg_counter = 0; 1928 list_for_each_entry(pg, &m->priority_groups, list) { 1929 if (pg->bypassed) 1930 state = 'D'; /* Disabled */ 1931 else if (pg == m->current_pg) 1932 state = 'A'; /* Currently Active */ 1933 else 1934 state = 'E'; /* Enabled */ 1935 DMEMIT(",pg_state_%d=%c", pg_counter, state); 1936 DMEMIT(",nr_pgpaths_%d=%u", pg_counter, pg->nr_pgpaths); 1937 DMEMIT(",path_selector_name_%d=%s", pg_counter, pg->ps.type->name); 1938 1939 pgpath_counter = 0; 1940 list_for_each_entry(p, &pg->pgpaths, list) { 1941 DMEMIT(",path_name_%d_%d=%s,is_active_%d_%d=%c,fail_count_%d_%d=%u", 1942 pg_counter, pgpath_counter, p->path.dev->name, 1943 pg_counter, pgpath_counter, p->is_active ? 'A' : 'F', 1944 pg_counter, pgpath_counter, p->fail_count); 1945 if (pg->ps.type->status) { 1946 DMEMIT(",path_selector_status_%d_%d=", 1947 pg_counter, pgpath_counter); 1948 sz += pg->ps.type->status(&pg->ps, &p->path, 1949 type, result + sz, 1950 maxlen - sz); 1951 } 1952 pgpath_counter++; 1953 } 1954 pg_counter++; 1955 } 1956 DMEMIT(";"); 1957 break; 1958 } 1959 1960 spin_unlock_irq(&m->lock); 1961 } 1962 1963 static int multipath_message(struct dm_target *ti, unsigned int argc, char **argv, 1964 char *result, unsigned int maxlen) 1965 { 1966 int r = -EINVAL; 1967 dev_t dev; 1968 struct multipath *m = ti->private; 1969 action_fn action; 1970 1971 mutex_lock(&m->work_mutex); 1972 1973 if (dm_suspended(ti)) { 1974 r = -EBUSY; 1975 goto out; 1976 } 1977 1978 if (argc == 1) { 1979 if (!strcasecmp(argv[0], "queue_if_no_path")) { 1980 r = queue_if_no_path(m, true, false, __func__); 1981 spin_lock_irq(&m->lock); 1982 enable_nopath_timeout(m); 1983 spin_unlock_irq(&m->lock); 1984 goto out; 1985 } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1986 r = queue_if_no_path(m, false, false, __func__); 1987 disable_nopath_timeout(m); 1988 goto out; 1989 } 1990 } 1991 1992 if (argc != 2) { 1993 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 1994 goto out; 1995 } 1996 1997 if (!strcasecmp(argv[0], "disable_group")) { 1998 r = bypass_pg_num(m, argv[1], true); 1999 goto out; 2000 } else if (!strcasecmp(argv[0], "enable_group")) { 2001 r = bypass_pg_num(m, argv[1], false); 2002 goto out; 2003 } else if (!strcasecmp(argv[0], "switch_group")) { 2004 r = switch_pg_num(m, argv[1]); 2005 goto out; 2006 } else if (!strcasecmp(argv[0], "reinstate_path")) 2007 action = reinstate_path; 2008 else if (!strcasecmp(argv[0], "fail_path")) 2009 action = fail_path; 2010 else { 2011 DMWARN("Unrecognised multipath message received: %s", argv[0]); 2012 goto out; 2013 } 2014 2015 r = dm_devt_from_path(argv[1], &dev); 2016 if (r) { 2017 DMWARN("message: error getting device %s", 2018 argv[1]); 2019 goto out; 2020 } 2021 2022 r = action_dev(m, dev, action); 2023 2024 out: 2025 mutex_unlock(&m->work_mutex); 2026 return r; 2027 } 2028 2029 /* 2030 * Perform a minimal read from the given path to find out whether the 2031 * path still works. If a path error occurs, fail it. 2032 */ 2033 static int probe_path(struct pgpath *pgpath) 2034 { 2035 struct block_device *bdev = pgpath->path.dev->bdev; 2036 unsigned int read_size = bdev_logical_block_size(bdev); 2037 struct page *page; 2038 struct bio *bio; 2039 blk_status_t status; 2040 int r = 0; 2041 2042 if (WARN_ON_ONCE(read_size > PAGE_SIZE)) 2043 return -EINVAL; 2044 2045 page = alloc_page(GFP_KERNEL); 2046 if (!page) 2047 return -ENOMEM; 2048 2049 /* Perform a minimal read: Sector 0, length read_size */ 2050 bio = bio_alloc(bdev, 1, REQ_OP_READ, GFP_KERNEL); 2051 if (!bio) { 2052 r = -ENOMEM; 2053 goto out; 2054 } 2055 2056 bio->bi_iter.bi_sector = 0; 2057 __bio_add_page(bio, page, read_size, 0); 2058 submit_bio_wait(bio); 2059 status = bio->bi_status; 2060 bio_put(bio); 2061 2062 if (status && blk_path_error(status)) 2063 fail_path(pgpath); 2064 2065 out: 2066 __free_page(page); 2067 return r; 2068 } 2069 2070 /* 2071 * Probe all active paths in current_pg to find out whether they still work. 2072 * Fail all paths that do not work. 2073 * 2074 * Return -ENOTCONN if no valid path is left (even outside of current_pg). We 2075 * cannot probe paths in other pgs without switching current_pg, so if valid 2076 * paths are only in different pgs, they may or may not work. Additionally 2077 * we should not probe paths in a pathgroup that is in the process of 2078 * Initializing. Userspace can submit a request and we'll switch and wait 2079 * for the pathgroup to be initialized. If the request fails, it may need to 2080 * probe again. 2081 */ 2082 static int probe_active_paths(struct multipath *m) 2083 { 2084 struct pgpath *pgpath; 2085 struct priority_group *pg = NULL; 2086 int r = 0; 2087 2088 spin_lock_irq(&m->lock); 2089 if (test_bit(MPATHF_DELAY_PG_SWITCH, &m->flags)) { 2090 wait_event_lock_irq(m->probe_wait, 2091 !test_bit(MPATHF_DELAY_PG_SWITCH, &m->flags), 2092 m->lock); 2093 /* 2094 * if we waited because a probe was already in progress, 2095 * and it probed the current active pathgroup, don't 2096 * reprobe. Just return the number of valid paths 2097 */ 2098 if (m->current_pg == m->last_probed_pg) 2099 goto skip_probe; 2100 } 2101 if (!m->current_pg || m->is_suspending || 2102 test_bit(MPATHF_QUEUE_IO, &m->flags)) 2103 goto skip_probe; 2104 set_bit(MPATHF_DELAY_PG_SWITCH, &m->flags); 2105 pg = m->last_probed_pg = m->current_pg; 2106 spin_unlock_irq(&m->lock); 2107 2108 list_for_each_entry(pgpath, &pg->pgpaths, list) { 2109 if (pg != READ_ONCE(m->current_pg) || 2110 READ_ONCE(m->is_suspending)) 2111 goto out; 2112 if (!pgpath->is_active) 2113 continue; 2114 2115 r = probe_path(pgpath); 2116 if (r < 0) 2117 goto out; 2118 } 2119 2120 out: 2121 spin_lock_irq(&m->lock); 2122 clear_bit(MPATHF_DELAY_PG_SWITCH, &m->flags); 2123 if (test_and_clear_bit(MPATHF_NEED_PG_SWITCH, &m->flags)) { 2124 m->current_pgpath = NULL; 2125 m->current_pg = NULL; 2126 } 2127 skip_probe: 2128 if (r == 0 && !atomic_read(&m->nr_valid_paths)) 2129 r = -ENOTCONN; 2130 spin_unlock_irq(&m->lock); 2131 if (pg) 2132 wake_up(&m->probe_wait); 2133 return r; 2134 } 2135 2136 static int multipath_prepare_ioctl(struct dm_target *ti, 2137 struct block_device **bdev, 2138 unsigned int cmd, unsigned long arg, 2139 bool *forward) 2140 { 2141 struct multipath *m = ti->private; 2142 struct pgpath *pgpath; 2143 int r; 2144 2145 if (_IOC_TYPE(cmd) == DM_IOCTL) { 2146 *forward = false; 2147 switch (cmd) { 2148 case DM_MPATH_PROBE_PATHS: 2149 return probe_active_paths(m); 2150 default: 2151 return -ENOTTY; 2152 } 2153 } 2154 2155 pgpath = READ_ONCE(m->current_pgpath); 2156 if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 2157 pgpath = choose_pgpath(m, 0); 2158 2159 if (pgpath) { 2160 if (!mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) { 2161 *bdev = pgpath->path.dev->bdev; 2162 r = 0; 2163 } else { 2164 /* pg_init has not started or completed */ 2165 r = -ENOTCONN; 2166 } 2167 } else { 2168 /* No path is available */ 2169 r = -EIO; 2170 spin_lock_irq(&m->lock); 2171 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 2172 r = -ENOTCONN; 2173 spin_unlock_irq(&m->lock); 2174 } 2175 2176 if (r == -ENOTCONN) { 2177 if (!READ_ONCE(m->current_pg)) { 2178 /* Path status changed, redo selection */ 2179 (void) choose_pgpath(m, 0); 2180 } 2181 spin_lock_irq(&m->lock); 2182 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 2183 (void) __pg_init_all_paths(m); 2184 spin_unlock_irq(&m->lock); 2185 dm_table_run_md_queue_async(m->ti->table); 2186 process_queued_io_list(m); 2187 } 2188 2189 /* 2190 * Only pass ioctls through if the device sizes match exactly. 2191 */ 2192 if (!r && ti->len != bdev_nr_sectors((*bdev))) 2193 return 1; 2194 return r; 2195 } 2196 2197 static int multipath_iterate_devices(struct dm_target *ti, 2198 iterate_devices_callout_fn fn, void *data) 2199 { 2200 struct multipath *m = ti->private; 2201 struct priority_group *pg; 2202 struct pgpath *p; 2203 int ret = 0; 2204 2205 list_for_each_entry(pg, &m->priority_groups, list) { 2206 list_for_each_entry(p, &pg->pgpaths, list) { 2207 ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 2208 if (ret) 2209 goto out; 2210 } 2211 } 2212 2213 out: 2214 return ret; 2215 } 2216 2217 static int pgpath_busy(struct pgpath *pgpath) 2218 { 2219 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 2220 2221 return blk_lld_busy(q); 2222 } 2223 2224 /* 2225 * We return "busy", only when we can map I/Os but underlying devices 2226 * are busy (so even if we map I/Os now, the I/Os will wait on 2227 * the underlying queue). 2228 * In other words, if we want to kill I/Os or queue them inside us 2229 * due to map unavailability, we don't return "busy". Otherwise, 2230 * dm core won't give us the I/Os and we can't do what we want. 2231 */ 2232 static int multipath_busy(struct dm_target *ti) 2233 { 2234 bool busy = false, has_active = false; 2235 struct multipath *m = ti->private; 2236 struct priority_group *pg, *next_pg; 2237 struct pgpath *pgpath; 2238 2239 /* pg_init in progress */ 2240 if (atomic_read(&m->pg_init_in_progress)) 2241 return true; 2242 2243 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 2244 if (!atomic_read(&m->nr_valid_paths)) { 2245 unsigned long flags; 2246 2247 spin_lock_irqsave(&m->lock, flags); 2248 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 2249 spin_unlock_irqrestore(&m->lock, flags); 2250 return (m->queue_mode != DM_TYPE_REQUEST_BASED); 2251 } 2252 spin_unlock_irqrestore(&m->lock, flags); 2253 } 2254 2255 /* Guess which priority_group will be used at next mapping time */ 2256 pg = READ_ONCE(m->current_pg); 2257 next_pg = READ_ONCE(m->next_pg); 2258 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 2259 pg = next_pg; 2260 2261 if (!pg) { 2262 /* 2263 * We don't know which pg will be used at next mapping time. 2264 * We don't call choose_pgpath() here to avoid to trigger 2265 * pg_init just by busy checking. 2266 * So we don't know whether underlying devices we will be using 2267 * at next mapping time are busy or not. Just try mapping. 2268 */ 2269 return busy; 2270 } 2271 2272 /* 2273 * If there is one non-busy active path at least, the path selector 2274 * will be able to select it. So we consider such a pg as not busy. 2275 */ 2276 busy = true; 2277 list_for_each_entry(pgpath, &pg->pgpaths, list) { 2278 if (pgpath->is_active) { 2279 has_active = true; 2280 if (!pgpath_busy(pgpath)) { 2281 busy = false; 2282 break; 2283 } 2284 } 2285 } 2286 2287 if (!has_active) { 2288 /* 2289 * No active path in this pg, so this pg won't be used and 2290 * the current_pg will be changed at next mapping time. 2291 * We need to try mapping to determine it. 2292 */ 2293 busy = false; 2294 } 2295 2296 return busy; 2297 } 2298 2299 /* 2300 *--------------------------------------------------------------- 2301 * Module setup 2302 *--------------------------------------------------------------- 2303 */ 2304 static struct target_type multipath_target = { 2305 .name = "multipath", 2306 .version = {1, 15, 0}, 2307 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE | 2308 DM_TARGET_PASSES_INTEGRITY | DM_TARGET_ATOMIC_WRITES, 2309 .module = THIS_MODULE, 2310 .ctr = multipath_ctr, 2311 .dtr = multipath_dtr, 2312 .clone_and_map_rq = multipath_clone_and_map, 2313 .release_clone_rq = multipath_release_clone, 2314 .rq_end_io = multipath_end_io, 2315 .map = multipath_map_bio, 2316 .end_io = multipath_end_io_bio, 2317 .presuspend = multipath_presuspend, 2318 .postsuspend = multipath_postsuspend, 2319 .resume = multipath_resume, 2320 .status = multipath_status, 2321 .message = multipath_message, 2322 .prepare_ioctl = multipath_prepare_ioctl, 2323 .iterate_devices = multipath_iterate_devices, 2324 .busy = multipath_busy, 2325 }; 2326 2327 static int __init dm_multipath_init(void) 2328 { 2329 int r = -ENOMEM; 2330 2331 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2332 if (!kmultipathd) { 2333 DMERR("failed to create workqueue kmpathd"); 2334 goto bad_alloc_kmultipathd; 2335 } 2336 2337 /* 2338 * A separate workqueue is used to handle the device handlers 2339 * to avoid overloading existing workqueue. Overloading the 2340 * old workqueue would also create a bottleneck in the 2341 * path of the storage hardware device activation. 2342 */ 2343 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 2344 WQ_MEM_RECLAIM); 2345 if (!kmpath_handlerd) { 2346 DMERR("failed to create workqueue kmpath_handlerd"); 2347 goto bad_alloc_kmpath_handlerd; 2348 } 2349 2350 dm_mpath_wq = alloc_workqueue("dm_mpath_wq", 0, 0); 2351 if (!dm_mpath_wq) { 2352 DMERR("failed to create workqueue dm_mpath_wq"); 2353 goto bad_alloc_dm_mpath_wq; 2354 } 2355 2356 r = dm_register_target(&multipath_target); 2357 if (r < 0) 2358 goto bad_register_target; 2359 2360 return 0; 2361 2362 bad_register_target: 2363 destroy_workqueue(dm_mpath_wq); 2364 bad_alloc_dm_mpath_wq: 2365 destroy_workqueue(kmpath_handlerd); 2366 bad_alloc_kmpath_handlerd: 2367 destroy_workqueue(kmultipathd); 2368 bad_alloc_kmultipathd: 2369 return r; 2370 } 2371 2372 static void __exit dm_multipath_exit(void) 2373 { 2374 destroy_workqueue(dm_mpath_wq); 2375 destroy_workqueue(kmpath_handlerd); 2376 destroy_workqueue(kmultipathd); 2377 2378 dm_unregister_target(&multipath_target); 2379 } 2380 2381 module_init(dm_multipath_init); 2382 module_exit(dm_multipath_exit); 2383 2384 module_param_named(queue_if_no_path_timeout_secs, queue_if_no_path_timeout_secs, ulong, 0644); 2385 MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds"); 2386 2387 MODULE_DESCRIPTION(DM_NAME " multipath target"); 2388 MODULE_AUTHOR("Sistina Software <dm-devel@lists.linux.dev>"); 2389 MODULE_LICENSE("GPL"); 2390