2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
11 #include "dm-bio-record.h"
12 #include "dm-path-selector.h"
13 #include "dm-uevent.h"
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/workqueue.h>
24 #include <linux/delay.h>
25 #include <scsi/scsi_dh.h>
26 #include <linux/atomic.h>
27 #include <linux/blk-mq.h>
29 #define DM_MSG_PREFIX "multipath"
30 #define DM_PG_INIT_DELAY_MSECS 2000
31 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
35 struct list_head list;
37 struct priority_group *pg; /* Owning PG */
38 unsigned fail_count; /* Cumulative failure count */
41 struct delayed_work activate_path;
43 bool is_active:1; /* Path status */
46 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
49 * Paths are grouped into Priority Groups and numbered from 1 upwards.
50 * Each has a path selector which controls which path gets used.
52 struct priority_group {
53 struct list_head list;
55 struct multipath *m; /* Owning multipath instance */
56 struct path_selector ps;
58 unsigned pg_num; /* Reference number */
59 unsigned nr_pgpaths; /* Number of paths in PG */
60 struct list_head pgpaths;
62 bool bypassed:1; /* Temporarily bypass this PG? */
65 /* Multipath context */
67 unsigned long flags; /* Multipath state flags */
70 enum dm_queue_mode queue_mode;
72 struct pgpath *current_pgpath;
73 struct priority_group *current_pg;
74 struct priority_group *next_pg; /* Switch to this PG if set */
76 atomic_t nr_valid_paths; /* Total number of usable paths */
77 unsigned nr_priority_groups;
78 struct list_head priority_groups;
80 const char *hw_handler_name;
81 char *hw_handler_params;
82 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
83 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
85 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
86 atomic_t pg_init_count; /* Number of times pg_init called */
88 struct mutex work_mutex;
89 struct work_struct trigger_event;
92 struct work_struct process_queued_bios;
93 struct bio_list queued_bios;
97 * Context information attached to each io we process.
100 struct pgpath *pgpath;
104 typedef int (*action_fn) (struct pgpath *pgpath);
106 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
107 static void trigger_event(struct work_struct *work);
108 static void activate_or_offline_path(struct pgpath *pgpath);
109 static void activate_path_work(struct work_struct *work);
110 static void process_queued_bios(struct work_struct *work);
112 /*-----------------------------------------------
113 * Multipath state flags.
114 *-----------------------------------------------*/
116 #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
117 #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
118 #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
119 #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
120 #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
121 #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
122 #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
124 /*-----------------------------------------------
125 * Allocation routines
126 *-----------------------------------------------*/
128 static struct pgpath *alloc_pgpath(void)
130 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
135 pgpath->is_active = true;
140 static void free_pgpath(struct pgpath *pgpath)
145 static struct priority_group *alloc_priority_group(void)
147 struct priority_group *pg;
149 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
152 INIT_LIST_HEAD(&pg->pgpaths);
157 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
159 struct pgpath *pgpath, *tmp;
161 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
162 list_del(&pgpath->list);
163 dm_put_device(ti, pgpath->path.dev);
168 static void free_priority_group(struct priority_group *pg,
169 struct dm_target *ti)
171 struct path_selector *ps = &pg->ps;
174 ps->type->destroy(ps);
175 dm_put_path_selector(ps->type);
178 free_pgpaths(&pg->pgpaths, ti);
182 static struct multipath *alloc_multipath(struct dm_target *ti)
186 m = kzalloc(sizeof(*m), GFP_KERNEL);
188 INIT_LIST_HEAD(&m->priority_groups);
189 spin_lock_init(&m->lock);
190 atomic_set(&m->nr_valid_paths, 0);
191 INIT_WORK(&m->trigger_event, trigger_event);
192 mutex_init(&m->work_mutex);
194 m->queue_mode = DM_TYPE_NONE;
203 static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
205 if (m->queue_mode == DM_TYPE_NONE) {
207 * Default to request-based.
209 if (dm_use_blk_mq(dm_table_get_md(ti->table)))
210 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
212 m->queue_mode = DM_TYPE_REQUEST_BASED;
214 } else if (m->queue_mode == DM_TYPE_BIO_BASED ||
215 m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
216 INIT_WORK(&m->process_queued_bios, process_queued_bios);
218 if (m->queue_mode == DM_TYPE_BIO_BASED) {
220 * bio-based doesn't support any direct scsi_dh management;
221 * it just discovers if a scsi_dh is attached.
223 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
227 if (m->queue_mode != DM_TYPE_NVME_BIO_BASED) {
228 set_bit(MPATHF_QUEUE_IO, &m->flags);
229 atomic_set(&m->pg_init_in_progress, 0);
230 atomic_set(&m->pg_init_count, 0);
231 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
232 init_waitqueue_head(&m->pg_init_wait);
235 dm_table_set_type(ti->table, m->queue_mode);
240 static void free_multipath(struct multipath *m)
242 struct priority_group *pg, *tmp;
244 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
246 free_priority_group(pg, m->ti);
249 kfree(m->hw_handler_name);
250 kfree(m->hw_handler_params);
254 static struct dm_mpath_io *get_mpio(union map_info *info)
259 static size_t multipath_per_bio_data_size(void)
261 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
264 static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
266 return dm_per_bio_data(bio, multipath_per_bio_data_size());
269 static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
271 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
272 void *bio_details = mpio + 1;
276 static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
278 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
279 struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
281 mpio->nr_bytes = bio->bi_iter.bi_size;
285 dm_bio_record(bio_details, bio);
288 /*-----------------------------------------------
290 *-----------------------------------------------*/
292 static int __pg_init_all_paths(struct multipath *m)
294 struct pgpath *pgpath;
295 unsigned long pg_init_delay = 0;
297 lockdep_assert_held(&m->lock);
299 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
302 atomic_inc(&m->pg_init_count);
303 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
305 /* Check here to reset pg_init_required */
309 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
310 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
311 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
312 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
313 /* Skip failed paths */
314 if (!pgpath->is_active)
316 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
318 atomic_inc(&m->pg_init_in_progress);
320 return atomic_read(&m->pg_init_in_progress);
323 static int pg_init_all_paths(struct multipath *m)
328 spin_lock_irqsave(&m->lock, flags);
329 ret = __pg_init_all_paths(m);
330 spin_unlock_irqrestore(&m->lock, flags);
335 static void __switch_pg(struct multipath *m, struct priority_group *pg)
339 if (m->queue_mode == DM_TYPE_NVME_BIO_BASED)
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);
347 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
348 clear_bit(MPATHF_QUEUE_IO, &m->flags);
351 atomic_set(&m->pg_init_count, 0);
354 static struct pgpath *choose_path_in_pg(struct multipath *m,
355 struct priority_group *pg,
359 struct dm_path *path;
360 struct pgpath *pgpath;
362 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
364 return ERR_PTR(-ENXIO);
366 pgpath = path_to_pgpath(path);
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;
373 spin_unlock_irqrestore(&m->lock, flags);
379 static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
382 struct priority_group *pg;
383 struct pgpath *pgpath;
384 unsigned bypassed = 1;
386 if (!atomic_read(&m->nr_valid_paths)) {
387 if (m->queue_mode != DM_TYPE_NVME_BIO_BASED)
388 clear_bit(MPATHF_QUEUE_IO, &m->flags);
392 /* Were we instructed to switch PG? */
393 if (READ_ONCE(m->next_pg)) {
394 spin_lock_irqsave(&m->lock, flags);
397 spin_unlock_irqrestore(&m->lock, flags);
398 goto check_current_pg;
401 spin_unlock_irqrestore(&m->lock, flags);
402 pgpath = choose_path_in_pg(m, pg, nr_bytes);
403 if (!IS_ERR_OR_NULL(pgpath))
407 /* Don't change PG until it has no remaining paths */
409 pg = READ_ONCE(m->current_pg);
411 pgpath = choose_path_in_pg(m, pg, nr_bytes);
412 if (!IS_ERR_OR_NULL(pgpath))
417 * Loop through priority groups until we find a valid path.
418 * First time we skip PGs marked 'bypassed'.
419 * Second time we only try the ones we skipped, but set
420 * pg_init_delay_retry so we do not hammer controllers.
423 list_for_each_entry(pg, &m->priority_groups, list) {
424 if (pg->bypassed == !!bypassed)
426 pgpath = choose_path_in_pg(m, pg, nr_bytes);
427 if (!IS_ERR_OR_NULL(pgpath)) {
429 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
433 } while (bypassed--);
436 spin_lock_irqsave(&m->lock, flags);
437 m->current_pgpath = NULL;
438 m->current_pg = NULL;
439 spin_unlock_irqrestore(&m->lock, flags);
445 * dm_report_EIO() is a macro instead of a function to make pr_debug()
446 * report the function name and line number of the function from which
447 * it has been invoked.
449 #define dm_report_EIO(m) \
451 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
453 pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
454 dm_device_name(md), \
455 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
456 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
457 dm_noflush_suspending((m)->ti)); \
461 * Check whether bios must be queued in the device-mapper core rather
462 * than here in the target.
464 * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
465 * the same value then we are not between multipath_presuspend()
466 * and multipath_resume() calls and we have no need to check
467 * for the DMF_NOFLUSH_SUSPENDING flag.
469 static bool __must_push_back(struct multipath *m, unsigned long flags)
471 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
472 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
473 dm_noflush_suspending(m->ti));
477 * Following functions use READ_ONCE to get atomic access to
478 * all m->flags to avoid taking spinlock
480 static bool must_push_back_rq(struct multipath *m)
482 unsigned long flags = READ_ONCE(m->flags);
483 return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
486 static bool must_push_back_bio(struct multipath *m)
488 unsigned long flags = READ_ONCE(m->flags);
489 return __must_push_back(m, flags);
493 * Map cloned requests (request-based multipath)
495 static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
496 union map_info *map_context,
497 struct request **__clone)
499 struct multipath *m = ti->private;
500 size_t nr_bytes = blk_rq_bytes(rq);
501 struct pgpath *pgpath;
502 struct block_device *bdev;
503 struct dm_mpath_io *mpio = get_mpio(map_context);
504 struct request_queue *q;
505 struct request *clone;
507 /* Do we need to select a new pgpath? */
508 pgpath = READ_ONCE(m->current_pgpath);
509 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
510 pgpath = choose_pgpath(m, nr_bytes);
513 if (must_push_back_rq(m))
514 return DM_MAPIO_DELAY_REQUEUE;
515 dm_report_EIO(m); /* Failed */
516 return DM_MAPIO_KILL;
517 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
518 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
519 if (pg_init_all_paths(m))
520 return DM_MAPIO_DELAY_REQUEUE;
521 return DM_MAPIO_REQUEUE;
524 mpio->pgpath = pgpath;
525 mpio->nr_bytes = nr_bytes;
527 bdev = pgpath->path.dev->bdev;
528 q = bdev_get_queue(bdev);
529 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
531 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
532 if (blk_queue_dying(q)) {
533 atomic_inc(&m->pg_init_in_progress);
534 activate_or_offline_path(pgpath);
536 return DM_MAPIO_DELAY_REQUEUE;
538 clone->bio = clone->biotail = NULL;
539 clone->rq_disk = bdev->bd_disk;
540 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
543 if (pgpath->pg->ps.type->start_io)
544 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
547 return DM_MAPIO_REMAPPED;
550 static void multipath_release_clone(struct request *clone)
552 blk_put_request(clone);
556 * Map cloned bios (bio-based multipath)
558 static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
560 struct pgpath *pgpath;
564 /* Do we need to select a new pgpath? */
565 pgpath = READ_ONCE(m->current_pgpath);
566 /* MPATHF_QUEUE_IO will never be set for NVMe */
567 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
568 if (!pgpath || !queue_io)
569 pgpath = choose_pgpath(m, mpio->nr_bytes);
571 if ((!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
572 (pgpath && queue_io)) {
573 /* Queue for the daemon to resubmit */
574 spin_lock_irqsave(&m->lock, flags);
575 bio_list_add(&m->queued_bios, bio);
576 spin_unlock_irqrestore(&m->lock, flags);
578 if (m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
579 queue_work(kmultipathd, &m->process_queued_bios);
581 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
582 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
583 pg_init_all_paths(m);
585 queue_work(kmultipathd, &m->process_queued_bios);
588 return DM_MAPIO_SUBMITTED;
592 if (must_push_back_bio(m))
593 return DM_MAPIO_REQUEUE;
595 return DM_MAPIO_KILL;
598 mpio->pgpath = pgpath;
601 bio_set_dev(bio, pgpath->path.dev->bdev);
602 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
604 if (pgpath->pg->ps.type->start_io)
605 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
608 return DM_MAPIO_REMAPPED;
611 static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
613 struct multipath *m = ti->private;
614 struct dm_mpath_io *mpio = NULL;
616 multipath_init_per_bio_data(bio, &mpio);
617 return __multipath_map_bio(m, bio, mpio);
620 static void process_queued_io_list(struct multipath *m)
622 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
623 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
624 else if (m->queue_mode == DM_TYPE_BIO_BASED ||
625 m->queue_mode == DM_TYPE_NVME_BIO_BASED)
626 queue_work(kmultipathd, &m->process_queued_bios);
629 static void process_queued_bios(struct work_struct *work)
634 struct bio_list bios;
635 struct blk_plug plug;
636 struct multipath *m =
637 container_of(work, struct multipath, process_queued_bios);
639 bio_list_init(&bios);
641 spin_lock_irqsave(&m->lock, flags);
643 if (bio_list_empty(&m->queued_bios)) {
644 spin_unlock_irqrestore(&m->lock, flags);
648 bio_list_merge(&bios, &m->queued_bios);
649 bio_list_init(&m->queued_bios);
651 spin_unlock_irqrestore(&m->lock, flags);
653 blk_start_plug(&plug);
654 while ((bio = bio_list_pop(&bios))) {
655 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
656 dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
657 r = __multipath_map_bio(m, bio, mpio);
660 bio->bi_status = BLK_STS_IOERR;
663 case DM_MAPIO_REQUEUE:
664 bio->bi_status = BLK_STS_DM_REQUEUE;
667 case DM_MAPIO_REMAPPED:
668 generic_make_request(bio);
673 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
676 blk_finish_plug(&plug);
680 * If we run out of usable paths, should we queue I/O or error it?
682 static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
687 spin_lock_irqsave(&m->lock, flags);
688 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
689 (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
690 (!save_old_value && queue_if_no_path));
691 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
692 spin_unlock_irqrestore(&m->lock, flags);
694 if (!queue_if_no_path) {
695 dm_table_run_md_queue_async(m->ti->table);
696 process_queued_io_list(m);
703 * An event is triggered whenever a path is taken out of use.
704 * Includes path failure and PG bypass.
706 static void trigger_event(struct work_struct *work)
708 struct multipath *m =
709 container_of(work, struct multipath, trigger_event);
711 dm_table_event(m->ti->table);
714 /*-----------------------------------------------------------------
715 * Constructor/argument parsing:
716 * <#multipath feature args> [<arg>]*
717 * <#hw_handler args> [hw_handler [<arg>]*]
719 * <initial priority group>
720 * [<selector> <#selector args> [<arg>]*
721 * <#paths> <#per-path selector args>
722 * [<path> [<arg>]* ]+ ]+
723 *---------------------------------------------------------------*/
724 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
725 struct dm_target *ti)
728 struct path_selector_type *pst;
731 static const struct dm_arg _args[] = {
732 {0, 1024, "invalid number of path selector args"},
735 pst = dm_get_path_selector(dm_shift_arg(as));
737 ti->error = "unknown path selector type";
741 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
743 dm_put_path_selector(pst);
747 r = pst->create(&pg->ps, ps_argc, as->argv);
749 dm_put_path_selector(pst);
750 ti->error = "path selector constructor failed";
755 dm_consume_args(as, ps_argc);
760 static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, char **error)
762 struct request_queue *q = bdev_get_queue(bdev);
763 const char *attached_handler_name;
766 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
768 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
769 if (attached_handler_name) {
771 * Clear any hw_handler_params associated with a
772 * handler that isn't already attached.
774 if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
775 kfree(m->hw_handler_params);
776 m->hw_handler_params = NULL;
780 * Reset hw_handler_name to match the attached handler
782 * NB. This modifies the table line to show the actual
783 * handler instead of the original table passed in.
785 kfree(m->hw_handler_name);
786 m->hw_handler_name = attached_handler_name;
790 if (m->hw_handler_name) {
791 r = scsi_dh_attach(q, m->hw_handler_name);
793 char b[BDEVNAME_SIZE];
795 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
800 *error = "error attaching hardware handler";
804 if (m->hw_handler_params) {
805 r = scsi_dh_set_params(q, m->hw_handler_params);
807 *error = "unable to set hardware handler parameters";
816 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
817 struct dm_target *ti)
821 struct multipath *m = ti->private;
823 /* we need at least a path arg */
825 ti->error = "no device given";
826 return ERR_PTR(-EINVAL);
831 return ERR_PTR(-ENOMEM);
833 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
836 ti->error = "error getting device";
840 if (m->queue_mode != DM_TYPE_NVME_BIO_BASED) {
841 INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
842 r = setup_scsi_dh(p->path.dev->bdev, m, &ti->error);
844 dm_put_device(ti, p->path.dev);
849 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
851 dm_put_device(ti, p->path.dev);
861 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
864 static const struct dm_arg _args[] = {
865 {1, 1024, "invalid number of paths"},
866 {0, 1024, "invalid number of selector args"}
870 unsigned i, nr_selector_args, nr_args;
871 struct priority_group *pg;
872 struct dm_target *ti = m->ti;
876 ti->error = "not enough priority group arguments";
877 return ERR_PTR(-EINVAL);
880 pg = alloc_priority_group();
882 ti->error = "couldn't allocate priority group";
883 return ERR_PTR(-ENOMEM);
887 r = parse_path_selector(as, pg, ti);
894 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
898 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
902 nr_args = 1 + nr_selector_args;
903 for (i = 0; i < pg->nr_pgpaths; i++) {
904 struct pgpath *pgpath;
905 struct dm_arg_set path_args;
907 if (as->argc < nr_args) {
908 ti->error = "not enough path parameters";
913 path_args.argc = nr_args;
914 path_args.argv = as->argv;
916 pgpath = parse_path(&path_args, &pg->ps, ti);
917 if (IS_ERR(pgpath)) {
923 list_add_tail(&pgpath->list, &pg->pgpaths);
924 dm_consume_args(as, nr_args);
930 free_priority_group(pg, ti);
934 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
938 struct dm_target *ti = m->ti;
940 static const struct dm_arg _args[] = {
941 {0, 1024, "invalid number of hardware handler args"},
944 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
950 if (m->queue_mode == DM_TYPE_BIO_BASED ||
951 m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
952 dm_consume_args(as, hw_argc);
953 DMERR("bio-based multipath doesn't allow hardware handler args");
957 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
958 if (!m->hw_handler_name)
965 for (i = 0; i <= hw_argc - 2; i++)
966 len += strlen(as->argv[i]) + 1;
967 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
969 ti->error = "memory allocation failed";
973 j = sprintf(p, "%d", hw_argc - 1);
974 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
975 j = sprintf(p, "%s", as->argv[i]);
977 dm_consume_args(as, hw_argc - 1);
981 kfree(m->hw_handler_name);
982 m->hw_handler_name = NULL;
986 static int parse_features(struct dm_arg_set *as, struct multipath *m)
990 struct dm_target *ti = m->ti;
991 const char *arg_name;
993 static const struct dm_arg _args[] = {
994 {0, 8, "invalid number of feature args"},
995 {1, 50, "pg_init_retries must be between 1 and 50"},
996 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
999 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1007 arg_name = dm_shift_arg(as);
1010 if (!strcasecmp(arg_name, "queue_if_no_path")) {
1011 r = queue_if_no_path(m, true, false);
1015 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
1016 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
1020 if (!strcasecmp(arg_name, "pg_init_retries") &&
1022 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
1027 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
1029 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
1034 if (!strcasecmp(arg_name, "queue_mode") &&
1036 const char *queue_mode_name = dm_shift_arg(as);
1038 if (!strcasecmp(queue_mode_name, "bio"))
1039 m->queue_mode = DM_TYPE_BIO_BASED;
1040 else if (!strcasecmp(queue_mode_name, "nvme"))
1041 m->queue_mode = DM_TYPE_NVME_BIO_BASED;
1042 else if (!strcasecmp(queue_mode_name, "rq"))
1043 m->queue_mode = DM_TYPE_REQUEST_BASED;
1044 else if (!strcasecmp(queue_mode_name, "mq"))
1045 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1047 ti->error = "Unknown 'queue_mode' requested";
1054 ti->error = "Unrecognised multipath feature request";
1056 } while (argc && !r);
1061 static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
1063 /* target arguments */
1064 static const struct dm_arg _args[] = {
1065 {0, 1024, "invalid number of priority groups"},
1066 {0, 1024, "invalid initial priority group number"},
1070 struct multipath *m;
1071 struct dm_arg_set as;
1072 unsigned pg_count = 0;
1073 unsigned next_pg_num;
1078 m = alloc_multipath(ti);
1080 ti->error = "can't allocate multipath";
1084 r = parse_features(&as, m);
1088 r = alloc_multipath_stage2(ti, m);
1092 r = parse_hw_handler(&as, m);
1096 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
1100 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
1104 if ((!m->nr_priority_groups && next_pg_num) ||
1105 (m->nr_priority_groups && !next_pg_num)) {
1106 ti->error = "invalid initial priority group";
1111 /* parse the priority groups */
1113 struct priority_group *pg;
1114 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
1116 pg = parse_priority_group(&as, m);
1122 nr_valid_paths += pg->nr_pgpaths;
1123 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1125 list_add_tail(&pg->list, &m->priority_groups);
1127 pg->pg_num = pg_count;
1132 if (pg_count != m->nr_priority_groups) {
1133 ti->error = "priority group count mismatch";
1138 ti->num_flush_bios = 1;
1139 ti->num_discard_bios = 1;
1140 ti->num_write_same_bios = 1;
1141 ti->num_write_zeroes_bios = 1;
1142 if (m->queue_mode == DM_TYPE_BIO_BASED || m->queue_mode == DM_TYPE_NVME_BIO_BASED)
1143 ti->per_io_data_size = multipath_per_bio_data_size();
1145 ti->per_io_data_size = sizeof(struct dm_mpath_io);
1154 static void multipath_wait_for_pg_init_completion(struct multipath *m)
1159 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
1161 if (!atomic_read(&m->pg_init_in_progress))
1166 finish_wait(&m->pg_init_wait, &wait);
1169 static void flush_multipath_work(struct multipath *m)
1171 if (m->hw_handler_name) {
1172 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1173 smp_mb__after_atomic();
1175 flush_workqueue(kmpath_handlerd);
1176 multipath_wait_for_pg_init_completion(m);
1178 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1179 smp_mb__after_atomic();
1182 flush_workqueue(kmultipathd);
1183 flush_work(&m->trigger_event);
1186 static void multipath_dtr(struct dm_target *ti)
1188 struct multipath *m = ti->private;
1190 flush_multipath_work(m);
1195 * Take a path out of use.
1197 static int fail_path(struct pgpath *pgpath)
1199 unsigned long flags;
1200 struct multipath *m = pgpath->pg->m;
1202 spin_lock_irqsave(&m->lock, flags);
1204 if (!pgpath->is_active)
1207 DMWARN("Failing path %s.", pgpath->path.dev->name);
1209 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1210 pgpath->is_active = false;
1211 pgpath->fail_count++;
1213 atomic_dec(&m->nr_valid_paths);
1215 if (pgpath == m->current_pgpath)
1216 m->current_pgpath = NULL;
1218 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1219 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1221 schedule_work(&m->trigger_event);
1224 spin_unlock_irqrestore(&m->lock, flags);
1230 * Reinstate a previously-failed path
1232 static int reinstate_path(struct pgpath *pgpath)
1234 int r = 0, run_queue = 0;
1235 unsigned long flags;
1236 struct multipath *m = pgpath->pg->m;
1237 unsigned nr_valid_paths;
1239 spin_lock_irqsave(&m->lock, flags);
1241 if (pgpath->is_active)
1244 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
1246 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1250 pgpath->is_active = true;
1252 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1253 if (nr_valid_paths == 1) {
1254 m->current_pgpath = NULL;
1256 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1257 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1258 atomic_inc(&m->pg_init_in_progress);
1261 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1262 pgpath->path.dev->name, nr_valid_paths);
1264 schedule_work(&m->trigger_event);
1267 spin_unlock_irqrestore(&m->lock, flags);
1269 dm_table_run_md_queue_async(m->ti->table);
1270 process_queued_io_list(m);
1277 * Fail or reinstate all paths that match the provided struct dm_dev.
1279 static int action_dev(struct multipath *m, struct dm_dev *dev,
1283 struct pgpath *pgpath;
1284 struct priority_group *pg;
1286 list_for_each_entry(pg, &m->priority_groups, list) {
1287 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1288 if (pgpath->path.dev == dev)
1297 * Temporarily try to avoid having to use the specified PG
1299 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1302 unsigned long flags;
1304 spin_lock_irqsave(&m->lock, flags);
1306 pg->bypassed = bypassed;
1307 m->current_pgpath = NULL;
1308 m->current_pg = NULL;
1310 spin_unlock_irqrestore(&m->lock, flags);
1312 schedule_work(&m->trigger_event);
1316 * Switch to using the specified PG from the next I/O that gets mapped
1318 static int switch_pg_num(struct multipath *m, const char *pgstr)
1320 struct priority_group *pg;
1322 unsigned long flags;
1325 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1326 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
1327 DMWARN("invalid PG number supplied to switch_pg_num");
1331 spin_lock_irqsave(&m->lock, flags);
1332 list_for_each_entry(pg, &m->priority_groups, list) {
1333 pg->bypassed = false;
1337 m->current_pgpath = NULL;
1338 m->current_pg = NULL;
1341 spin_unlock_irqrestore(&m->lock, flags);
1343 schedule_work(&m->trigger_event);
1348 * Set/clear bypassed status of a PG.
1349 * PGs are numbered upwards from 1 in the order they were declared.
1351 static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
1353 struct priority_group *pg;
1357 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1358 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
1359 DMWARN("invalid PG number supplied to bypass_pg");
1363 list_for_each_entry(pg, &m->priority_groups, list) {
1368 bypass_pg(m, pg, bypassed);
1373 * Should we retry pg_init immediately?
1375 static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1377 unsigned long flags;
1378 bool limit_reached = false;
1380 spin_lock_irqsave(&m->lock, flags);
1382 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1383 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1384 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1386 limit_reached = true;
1388 spin_unlock_irqrestore(&m->lock, flags);
1390 return limit_reached;
1393 static void pg_init_done(void *data, int errors)
1395 struct pgpath *pgpath = data;
1396 struct priority_group *pg = pgpath->pg;
1397 struct multipath *m = pg->m;
1398 unsigned long flags;
1399 bool delay_retry = false;
1401 /* device or driver problems */
1406 if (!m->hw_handler_name) {
1410 DMERR("Could not failover the device: Handler scsi_dh_%s "
1411 "Error %d.", m->hw_handler_name, errors);
1413 * Fail path for now, so we do not ping pong
1417 case SCSI_DH_DEV_TEMP_BUSY:
1419 * Probably doing something like FW upgrade on the
1420 * controller so try the other pg.
1422 bypass_pg(m, pg, true);
1425 /* Wait before retrying. */
1428 case SCSI_DH_IMM_RETRY:
1429 case SCSI_DH_RES_TEMP_UNAVAIL:
1430 if (pg_init_limit_reached(m, pgpath))
1434 case SCSI_DH_DEV_OFFLINED:
1437 * We probably do not want to fail the path for a device
1438 * error, but this is what the old dm did. In future
1439 * patches we can do more advanced handling.
1444 spin_lock_irqsave(&m->lock, flags);
1446 if (pgpath == m->current_pgpath) {
1447 DMERR("Could not failover device. Error %d.", errors);
1448 m->current_pgpath = NULL;
1449 m->current_pg = NULL;
1451 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1452 pg->bypassed = false;
1454 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1455 /* Activations of other paths are still on going */
1458 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1460 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1462 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1464 if (__pg_init_all_paths(m))
1467 clear_bit(MPATHF_QUEUE_IO, &m->flags);
1469 process_queued_io_list(m);
1472 * Wake up any thread waiting to suspend.
1474 wake_up(&m->pg_init_wait);
1477 spin_unlock_irqrestore(&m->lock, flags);
1480 static void activate_or_offline_path(struct pgpath *pgpath)
1482 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1484 if (pgpath->is_active && !blk_queue_dying(q))
1485 scsi_dh_activate(q, pg_init_done, pgpath);
1487 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1490 static void activate_path_work(struct work_struct *work)
1492 struct pgpath *pgpath =
1493 container_of(work, struct pgpath, activate_path.work);
1495 activate_or_offline_path(pgpath);
1498 static int noretry_error(blk_status_t error)
1501 case BLK_STS_NOTSUPP:
1503 case BLK_STS_TARGET:
1505 case BLK_STS_MEDIUM:
1509 /* Anything else could be a path failure, so should be retried */
1513 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1514 blk_status_t error, union map_info *map_context)
1516 struct dm_mpath_io *mpio = get_mpio(map_context);
1517 struct pgpath *pgpath = mpio->pgpath;
1518 int r = DM_ENDIO_DONE;
1521 * We don't queue any clone request inside the multipath target
1522 * during end I/O handling, since those clone requests don't have
1523 * bio clones. If we queue them inside the multipath target,
1524 * we need to make bio clones, that requires memory allocation.
1525 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1526 * don't have bio clones.)
1527 * Instead of queueing the clone request here, we queue the original
1528 * request into dm core, which will remake a clone request and
1529 * clone bios for it and resubmit it later.
1531 if (error && !noretry_error(error)) {
1532 struct multipath *m = ti->private;
1534 r = DM_ENDIO_REQUEUE;
1539 if (atomic_read(&m->nr_valid_paths) == 0 &&
1540 !must_push_back_rq(m)) {
1541 if (error == BLK_STS_IOERR)
1543 /* complete with the original error */
1549 struct path_selector *ps = &pgpath->pg->ps;
1551 if (ps->type->end_io)
1552 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1558 static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
1559 blk_status_t *error)
1561 struct multipath *m = ti->private;
1562 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1563 struct pgpath *pgpath = mpio->pgpath;
1564 unsigned long flags;
1565 int r = DM_ENDIO_DONE;
1567 if (!*error || noretry_error(*error))
1573 if (atomic_read(&m->nr_valid_paths) == 0 &&
1574 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1575 if (must_push_back_bio(m)) {
1576 r = DM_ENDIO_REQUEUE;
1579 *error = BLK_STS_IOERR;
1584 spin_lock_irqsave(&m->lock, flags);
1585 bio_list_add(&m->queued_bios, clone);
1586 spin_unlock_irqrestore(&m->lock, flags);
1587 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1588 queue_work(kmultipathd, &m->process_queued_bios);
1590 r = DM_ENDIO_INCOMPLETE;
1593 struct path_selector *ps = &pgpath->pg->ps;
1595 if (ps->type->end_io)
1596 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1603 * Suspend can't complete until all the I/O is processed so if
1604 * the last path fails we must error any remaining I/O.
1605 * Note that if the freeze_bdev fails while suspending, the
1606 * queue_if_no_path state is lost - userspace should reset it.
1608 static void multipath_presuspend(struct dm_target *ti)
1610 struct multipath *m = ti->private;
1612 queue_if_no_path(m, false, true);
1615 static void multipath_postsuspend(struct dm_target *ti)
1617 struct multipath *m = ti->private;
1619 mutex_lock(&m->work_mutex);
1620 flush_multipath_work(m);
1621 mutex_unlock(&m->work_mutex);
1625 * Restore the queue_if_no_path setting.
1627 static void multipath_resume(struct dm_target *ti)
1629 struct multipath *m = ti->private;
1630 unsigned long flags;
1632 spin_lock_irqsave(&m->lock, flags);
1633 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1634 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
1635 spin_unlock_irqrestore(&m->lock, flags);
1639 * Info output has the following format:
1640 * num_multipath_feature_args [multipath_feature_args]*
1641 * num_handler_status_args [handler_status_args]*
1642 * num_groups init_group_number
1643 * [A|D|E num_ps_status_args [ps_status_args]*
1644 * num_paths num_selector_args
1645 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1647 * Table output has the following format (identical to the constructor string):
1648 * num_feature_args [features_args]*
1649 * num_handler_args hw_handler [hw_handler_args]*
1650 * num_groups init_group_number
1651 * [priority selector-name num_ps_args [ps_args]*
1652 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1654 static void multipath_status(struct dm_target *ti, status_type_t type,
1655 unsigned status_flags, char *result, unsigned maxlen)
1658 unsigned long flags;
1659 struct multipath *m = ti->private;
1660 struct priority_group *pg;
1665 spin_lock_irqsave(&m->lock, flags);
1668 if (type == STATUSTYPE_INFO)
1669 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1670 atomic_read(&m->pg_init_count));
1672 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
1673 (m->pg_init_retries > 0) * 2 +
1674 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1675 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1676 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1678 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1679 DMEMIT("queue_if_no_path ");
1680 if (m->pg_init_retries)
1681 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1682 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1683 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1684 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1685 DMEMIT("retain_attached_hw_handler ");
1686 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1687 switch(m->queue_mode) {
1688 case DM_TYPE_BIO_BASED:
1689 DMEMIT("queue_mode bio ");
1691 case DM_TYPE_NVME_BIO_BASED:
1692 DMEMIT("queue_mode nvme ");
1694 case DM_TYPE_MQ_REQUEST_BASED:
1695 DMEMIT("queue_mode mq ");
1704 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1707 DMEMIT("1 %s ", m->hw_handler_name);
1709 DMEMIT("%u ", m->nr_priority_groups);
1712 pg_num = m->next_pg->pg_num;
1713 else if (m->current_pg)
1714 pg_num = m->current_pg->pg_num;
1716 pg_num = (m->nr_priority_groups ? 1 : 0);
1718 DMEMIT("%u ", pg_num);
1721 case STATUSTYPE_INFO:
1722 list_for_each_entry(pg, &m->priority_groups, list) {
1724 state = 'D'; /* Disabled */
1725 else if (pg == m->current_pg)
1726 state = 'A'; /* Currently Active */
1728 state = 'E'; /* Enabled */
1730 DMEMIT("%c ", state);
1732 if (pg->ps.type->status)
1733 sz += pg->ps.type->status(&pg->ps, NULL, type,
1739 DMEMIT("%u %u ", pg->nr_pgpaths,
1740 pg->ps.type->info_args);
1742 list_for_each_entry(p, &pg->pgpaths, list) {
1743 DMEMIT("%s %s %u ", p->path.dev->name,
1744 p->is_active ? "A" : "F",
1746 if (pg->ps.type->status)
1747 sz += pg->ps.type->status(&pg->ps,
1748 &p->path, type, result + sz,
1754 case STATUSTYPE_TABLE:
1755 list_for_each_entry(pg, &m->priority_groups, list) {
1756 DMEMIT("%s ", pg->ps.type->name);
1758 if (pg->ps.type->status)
1759 sz += pg->ps.type->status(&pg->ps, NULL, type,
1765 DMEMIT("%u %u ", pg->nr_pgpaths,
1766 pg->ps.type->table_args);
1768 list_for_each_entry(p, &pg->pgpaths, list) {
1769 DMEMIT("%s ", p->path.dev->name);
1770 if (pg->ps.type->status)
1771 sz += pg->ps.type->status(&pg->ps,
1772 &p->path, type, result + sz,
1779 spin_unlock_irqrestore(&m->lock, flags);
1782 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1786 struct multipath *m = ti->private;
1789 mutex_lock(&m->work_mutex);
1791 if (dm_suspended(ti)) {
1797 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1798 r = queue_if_no_path(m, true, false);
1800 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1801 r = queue_if_no_path(m, false, false);
1807 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
1811 if (!strcasecmp(argv[0], "disable_group")) {
1812 r = bypass_pg_num(m, argv[1], true);
1814 } else if (!strcasecmp(argv[0], "enable_group")) {
1815 r = bypass_pg_num(m, argv[1], false);
1817 } else if (!strcasecmp(argv[0], "switch_group")) {
1818 r = switch_pg_num(m, argv[1]);
1820 } else if (!strcasecmp(argv[0], "reinstate_path"))
1821 action = reinstate_path;
1822 else if (!strcasecmp(argv[0], "fail_path"))
1825 DMWARN("Unrecognised multipath message received: %s", argv[0]);
1829 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1831 DMWARN("message: error getting device %s",
1836 r = action_dev(m, dev, action);
1838 dm_put_device(ti, dev);
1841 mutex_unlock(&m->work_mutex);
1845 static int multipath_prepare_ioctl(struct dm_target *ti,
1846 struct block_device **bdev, fmode_t *mode)
1848 struct multipath *m = ti->private;
1849 struct pgpath *current_pgpath;
1852 current_pgpath = READ_ONCE(m->current_pgpath);
1853 if (!current_pgpath)
1854 current_pgpath = choose_pgpath(m, 0);
1856 if (current_pgpath) {
1857 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
1858 *bdev = current_pgpath->path.dev->bdev;
1859 *mode = current_pgpath->path.dev->mode;
1862 /* pg_init has not started or completed */
1866 /* No path is available */
1867 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1873 if (r == -ENOTCONN) {
1874 if (!READ_ONCE(m->current_pg)) {
1875 /* Path status changed, redo selection */
1876 (void) choose_pgpath(m, 0);
1878 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1879 pg_init_all_paths(m);
1880 dm_table_run_md_queue_async(m->ti->table);
1881 process_queued_io_list(m);
1885 * Only pass ioctls through if the device sizes match exactly.
1887 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1892 static int multipath_iterate_devices(struct dm_target *ti,
1893 iterate_devices_callout_fn fn, void *data)
1895 struct multipath *m = ti->private;
1896 struct priority_group *pg;
1900 list_for_each_entry(pg, &m->priority_groups, list) {
1901 list_for_each_entry(p, &pg->pgpaths, list) {
1902 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1912 static int pgpath_busy(struct pgpath *pgpath)
1914 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1916 return blk_lld_busy(q);
1920 * We return "busy", only when we can map I/Os but underlying devices
1921 * are busy (so even if we map I/Os now, the I/Os will wait on
1922 * the underlying queue).
1923 * In other words, if we want to kill I/Os or queue them inside us
1924 * due to map unavailability, we don't return "busy". Otherwise,
1925 * dm core won't give us the I/Os and we can't do what we want.
1927 static int multipath_busy(struct dm_target *ti)
1929 bool busy = false, has_active = false;
1930 struct multipath *m = ti->private;
1931 struct priority_group *pg, *next_pg;
1932 struct pgpath *pgpath;
1934 /* pg_init in progress */
1935 if (atomic_read(&m->pg_init_in_progress))
1938 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1939 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1940 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1942 /* Guess which priority_group will be used at next mapping time */
1943 pg = READ_ONCE(m->current_pg);
1944 next_pg = READ_ONCE(m->next_pg);
1945 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
1950 * We don't know which pg will be used at next mapping time.
1951 * We don't call choose_pgpath() here to avoid to trigger
1952 * pg_init just by busy checking.
1953 * So we don't know whether underlying devices we will be using
1954 * at next mapping time are busy or not. Just try mapping.
1960 * If there is one non-busy active path at least, the path selector
1961 * will be able to select it. So we consider such a pg as not busy.
1964 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1965 if (pgpath->is_active) {
1967 if (!pgpath_busy(pgpath)) {
1976 * No active path in this pg, so this pg won't be used and
1977 * the current_pg will be changed at next mapping time.
1978 * We need to try mapping to determine it.
1986 /*-----------------------------------------------------------------
1988 *---------------------------------------------------------------*/
1989 static struct target_type multipath_target = {
1990 .name = "multipath",
1991 .version = {1, 12, 0},
1992 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
1993 .module = THIS_MODULE,
1994 .ctr = multipath_ctr,
1995 .dtr = multipath_dtr,
1996 .clone_and_map_rq = multipath_clone_and_map,
1997 .release_clone_rq = multipath_release_clone,
1998 .rq_end_io = multipath_end_io,
1999 .map = multipath_map_bio,
2000 .end_io = multipath_end_io_bio,
2001 .presuspend = multipath_presuspend,
2002 .postsuspend = multipath_postsuspend,
2003 .resume = multipath_resume,
2004 .status = multipath_status,
2005 .message = multipath_message,
2006 .prepare_ioctl = multipath_prepare_ioctl,
2007 .iterate_devices = multipath_iterate_devices,
2008 .busy = multipath_busy,
2011 static int __init dm_multipath_init(void)
2015 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2017 DMERR("failed to create workqueue kmpathd");
2019 goto bad_alloc_kmultipathd;
2023 * A separate workqueue is used to handle the device handlers
2024 * to avoid overloading existing workqueue. Overloading the
2025 * old workqueue would also create a bottleneck in the
2026 * path of the storage hardware device activation.
2028 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2030 if (!kmpath_handlerd) {
2031 DMERR("failed to create workqueue kmpath_handlerd");
2033 goto bad_alloc_kmpath_handlerd;
2036 r = dm_register_target(&multipath_target);
2038 DMERR("request-based register failed %d", r);
2040 goto bad_register_target;
2045 bad_register_target:
2046 destroy_workqueue(kmpath_handlerd);
2047 bad_alloc_kmpath_handlerd:
2048 destroy_workqueue(kmultipathd);
2049 bad_alloc_kmultipathd:
2053 static void __exit dm_multipath_exit(void)
2055 destroy_workqueue(kmpath_handlerd);
2056 destroy_workqueue(kmultipathd);
2058 dm_unregister_target(&multipath_target);
2061 module_init(dm_multipath_init);
2062 module_exit(dm_multipath_exit);
2064 MODULE_DESCRIPTION(DM_NAME " multipath target");
2065 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2066 MODULE_LICENSE("GPL");