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