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 struct list_head list;
70 const char *hw_handler_name;
71 char *hw_handler_params;
75 unsigned nr_priority_groups;
76 struct list_head priority_groups;
78 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
80 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
84 unsigned long flags; /* Multipath state flags */
86 unsigned pg_init_retries; /* Number of times to retry pg_init */
87 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
89 atomic_t nr_valid_paths; /* Total number of usable paths */
90 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
91 atomic_t pg_init_count; /* Number of times pg_init called */
94 * We must use a mempool of dm_mpath_io structs so that we
95 * can resubmit bios on error.
99 struct mutex work_mutex;
100 struct work_struct trigger_event;
102 struct work_struct process_queued_bios;
103 struct bio_list queued_bios;
107 * Context information attached to each io we process.
110 struct pgpath *pgpath;
114 * FIXME: make request-based code _not_ include this member.
116 struct dm_bio_details bio_details;
119 typedef int (*action_fn) (struct pgpath *pgpath);
121 static struct kmem_cache *_mpio_cache;
123 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
124 static void trigger_event(struct work_struct *work);
125 static void activate_path(struct work_struct *work);
126 static void process_queued_bios(struct work_struct *work);
128 /*-----------------------------------------------
129 * Multipath state flags.
130 *-----------------------------------------------*/
132 #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
133 #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
134 #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
135 #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
136 #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
137 #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
138 #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
139 #define MPATHF_BIO_BASED 7 /* Device is bio-based? */
141 /*-----------------------------------------------
142 * Allocation routines
143 *-----------------------------------------------*/
145 static struct pgpath *alloc_pgpath(void)
147 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
150 pgpath->is_active = true;
151 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
157 static void free_pgpath(struct pgpath *pgpath)
162 static struct priority_group *alloc_priority_group(void)
164 struct priority_group *pg;
166 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
169 INIT_LIST_HEAD(&pg->pgpaths);
174 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
176 struct pgpath *pgpath, *tmp;
178 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
179 list_del(&pgpath->list);
180 dm_put_device(ti, pgpath->path.dev);
185 static void free_priority_group(struct priority_group *pg,
186 struct dm_target *ti)
188 struct path_selector *ps = &pg->ps;
191 ps->type->destroy(ps);
192 dm_put_path_selector(ps->type);
195 free_pgpaths(&pg->pgpaths, ti);
199 static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq,
204 m = kzalloc(sizeof(*m), GFP_KERNEL);
206 INIT_LIST_HEAD(&m->priority_groups);
207 spin_lock_init(&m->lock);
208 set_bit(MPATHF_QUEUE_IO, &m->flags);
209 atomic_set(&m->nr_valid_paths, 0);
210 atomic_set(&m->pg_init_in_progress, 0);
211 atomic_set(&m->pg_init_count, 0);
212 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
213 INIT_WORK(&m->trigger_event, trigger_event);
214 init_waitqueue_head(&m->pg_init_wait);
215 mutex_init(&m->work_mutex);
218 if (!use_blk_mq && !bio_based) {
219 unsigned min_ios = dm_get_reserved_rq_based_ios();
221 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
229 INIT_WORK(&m->process_queued_bios, process_queued_bios);
230 set_bit(MPATHF_BIO_BASED, &m->flags);
232 * bio-based doesn't support any direct scsi_dh management;
233 * it just discovers if a scsi_dh is attached.
235 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
245 static void free_multipath(struct multipath *m)
247 struct priority_group *pg, *tmp;
249 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
251 free_priority_group(pg, m->ti);
254 kfree(m->hw_handler_name);
255 kfree(m->hw_handler_params);
256 mempool_destroy(m->mpio_pool);
260 static struct dm_mpath_io *get_mpio(union map_info *info)
265 static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
267 struct dm_mpath_io *mpio;
270 /* Use blk-mq pdu memory requested via per_io_data_size */
271 mpio = get_mpio(info);
272 memset(mpio, 0, sizeof(*mpio));
276 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
280 memset(mpio, 0, sizeof(*mpio));
286 static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
288 /* Only needed for non blk-mq (.request_fn) multipath */
290 struct dm_mpath_io *mpio = info->ptr;
293 mempool_free(mpio, m->mpio_pool);
297 static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
299 return dm_per_bio_data(bio, sizeof(struct dm_mpath_io));
302 static struct dm_mpath_io *set_mpio_bio(struct multipath *m, struct bio *bio)
304 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
306 memset(mpio, 0, sizeof(*mpio));
307 dm_bio_record(&mpio->bio_details, bio);
312 /*-----------------------------------------------
314 *-----------------------------------------------*/
316 static int __pg_init_all_paths(struct multipath *m)
318 struct pgpath *pgpath;
319 unsigned long pg_init_delay = 0;
321 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
324 atomic_inc(&m->pg_init_count);
325 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
327 /* Check here to reset pg_init_required */
331 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
332 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
333 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
334 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
335 /* Skip failed paths */
336 if (!pgpath->is_active)
338 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
340 atomic_inc(&m->pg_init_in_progress);
342 return atomic_read(&m->pg_init_in_progress);
345 static int pg_init_all_paths(struct multipath *m)
350 spin_lock_irqsave(&m->lock, flags);
351 r = __pg_init_all_paths(m);
352 spin_unlock_irqrestore(&m->lock, flags);
357 static void __switch_pg(struct multipath *m, struct priority_group *pg)
361 /* Must we initialise the PG first, and queue I/O till it's ready? */
362 if (m->hw_handler_name) {
363 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
364 set_bit(MPATHF_QUEUE_IO, &m->flags);
366 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
367 clear_bit(MPATHF_QUEUE_IO, &m->flags);
370 atomic_set(&m->pg_init_count, 0);
373 static struct pgpath *choose_path_in_pg(struct multipath *m,
374 struct priority_group *pg,
378 struct dm_path *path;
379 struct pgpath *pgpath;
381 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
383 return ERR_PTR(-ENXIO);
385 pgpath = path_to_pgpath(path);
387 if (unlikely(lockless_dereference(m->current_pg) != pg)) {
388 /* Only update current_pgpath if pg changed */
389 spin_lock_irqsave(&m->lock, flags);
390 m->current_pgpath = pgpath;
392 spin_unlock_irqrestore(&m->lock, flags);
398 static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
401 struct priority_group *pg;
402 struct pgpath *pgpath;
403 bool bypassed = true;
405 if (!atomic_read(&m->nr_valid_paths)) {
406 clear_bit(MPATHF_QUEUE_IO, &m->flags);
410 /* Were we instructed to switch PG? */
411 if (lockless_dereference(m->next_pg)) {
412 spin_lock_irqsave(&m->lock, flags);
415 spin_unlock_irqrestore(&m->lock, flags);
416 goto check_current_pg;
419 spin_unlock_irqrestore(&m->lock, flags);
420 pgpath = choose_path_in_pg(m, pg, nr_bytes);
421 if (!IS_ERR_OR_NULL(pgpath))
425 /* Don't change PG until it has no remaining paths */
427 pg = lockless_dereference(m->current_pg);
429 pgpath = choose_path_in_pg(m, pg, nr_bytes);
430 if (!IS_ERR_OR_NULL(pgpath))
435 * Loop through priority groups until we find a valid path.
436 * First time we skip PGs marked 'bypassed'.
437 * Second time we only try the ones we skipped, but set
438 * pg_init_delay_retry so we do not hammer controllers.
441 list_for_each_entry(pg, &m->priority_groups, list) {
442 if (pg->bypassed == bypassed)
444 pgpath = choose_path_in_pg(m, pg, nr_bytes);
445 if (!IS_ERR_OR_NULL(pgpath)) {
447 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
451 } while (bypassed--);
454 spin_lock_irqsave(&m->lock, flags);
455 m->current_pgpath = NULL;
456 m->current_pg = NULL;
457 spin_unlock_irqrestore(&m->lock, flags);
463 * Check whether bios must be queued in the device-mapper core rather
464 * than here in the target.
466 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
467 * same value then we are not between multipath_presuspend()
468 * and multipath_resume() calls and we have no need to check
469 * for the DMF_NOFLUSH_SUSPENDING flag.
471 static bool __must_push_back(struct multipath *m)
473 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
474 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
475 dm_noflush_suspending(m->ti));
478 static bool must_push_back_rq(struct multipath *m)
480 return (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
481 __must_push_back(m));
484 static bool must_push_back_bio(struct multipath *m)
486 return __must_push_back(m);
490 * Map cloned requests (request-based multipath)
492 static int __multipath_map(struct dm_target *ti, struct request *clone,
493 union map_info *map_context,
494 struct request *rq, struct request **__clone)
496 struct multipath *m = ti->private;
497 int r = DM_MAPIO_REQUEUE;
498 size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
499 struct pgpath *pgpath;
500 struct block_device *bdev;
501 struct dm_mpath_io *mpio;
503 /* Do we need to select a new pgpath? */
504 pgpath = lockless_dereference(m->current_pgpath);
505 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
506 pgpath = choose_pgpath(m, nr_bytes);
509 if (!must_push_back_rq(m))
510 r = -EIO; /* Failed */
512 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
513 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
514 pg_init_all_paths(m);
518 mpio = set_mpio(m, map_context);
520 /* ENOMEM, requeue */
523 mpio->pgpath = pgpath;
524 mpio->nr_bytes = nr_bytes;
526 bdev = pgpath->path.dev->bdev;
530 * Old request-based interface: allocated clone is passed in.
531 * Used by: .request_fn stacked on .request_fn path(s).
533 clone->q = bdev_get_queue(bdev);
534 clone->rq_disk = bdev->bd_disk;
535 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
538 * blk-mq request-based interface; used by both:
539 * .request_fn stacked on blk-mq path(s) and
540 * blk-mq stacked on blk-mq path(s).
542 *__clone = blk_mq_alloc_request(bdev_get_queue(bdev),
543 rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
544 if (IS_ERR(*__clone)) {
545 /* ENOMEM, requeue */
546 clear_request_fn_mpio(m, map_context);
549 (*__clone)->bio = (*__clone)->biotail = NULL;
550 (*__clone)->rq_disk = bdev->bd_disk;
551 (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
554 if (pgpath->pg->ps.type->start_io)
555 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
558 return DM_MAPIO_REMAPPED;
561 static int multipath_map(struct dm_target *ti, struct request *clone,
562 union map_info *map_context)
564 return __multipath_map(ti, clone, map_context, NULL, NULL);
567 static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
568 union map_info *map_context,
569 struct request **clone)
571 return __multipath_map(ti, NULL, map_context, rq, clone);
574 static void multipath_release_clone(struct request *clone)
576 blk_mq_free_request(clone);
580 * Map cloned bios (bio-based multipath)
582 static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
584 size_t nr_bytes = bio->bi_iter.bi_size;
585 struct pgpath *pgpath;
589 /* Do we need to select a new pgpath? */
590 pgpath = lockless_dereference(m->current_pgpath);
591 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
592 if (!pgpath || !queue_io)
593 pgpath = choose_pgpath(m, nr_bytes);
595 if ((pgpath && queue_io) ||
596 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
597 /* Queue for the daemon to resubmit */
598 spin_lock_irqsave(&m->lock, flags);
599 bio_list_add(&m->queued_bios, bio);
600 spin_unlock_irqrestore(&m->lock, flags);
601 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
602 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
603 pg_init_all_paths(m);
605 queue_work(kmultipathd, &m->process_queued_bios);
606 return DM_MAPIO_SUBMITTED;
610 if (!must_push_back_bio(m))
612 return DM_MAPIO_REQUEUE;
615 mpio->pgpath = pgpath;
616 mpio->nr_bytes = nr_bytes;
619 bio->bi_bdev = pgpath->path.dev->bdev;
620 bio->bi_rw |= REQ_FAILFAST_TRANSPORT;
622 if (pgpath->pg->ps.type->start_io)
623 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
626 return DM_MAPIO_REMAPPED;
629 static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
631 struct multipath *m = ti->private;
632 struct dm_mpath_io *mpio = set_mpio_bio(m, bio);
634 return __multipath_map_bio(m, bio, mpio);
637 static void process_queued_bios_list(struct multipath *m)
639 if (test_bit(MPATHF_BIO_BASED, &m->flags))
640 queue_work(kmultipathd, &m->process_queued_bios);
643 static void process_queued_bios(struct work_struct *work)
648 struct bio_list bios;
649 struct blk_plug plug;
650 struct multipath *m =
651 container_of(work, struct multipath, process_queued_bios);
653 bio_list_init(&bios);
655 spin_lock_irqsave(&m->lock, flags);
657 if (bio_list_empty(&m->queued_bios)) {
658 spin_unlock_irqrestore(&m->lock, flags);
662 bio_list_merge(&bios, &m->queued_bios);
663 bio_list_init(&m->queued_bios);
665 spin_unlock_irqrestore(&m->lock, flags);
667 blk_start_plug(&plug);
668 while ((bio = bio_list_pop(&bios))) {
669 r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
670 if (r < 0 || r == DM_MAPIO_REQUEUE) {
673 } else if (r == DM_MAPIO_REMAPPED)
674 generic_make_request(bio);
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);
689 if (save_old_value) {
690 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
691 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
693 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
695 if (queue_if_no_path)
696 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
698 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
700 if (queue_if_no_path)
701 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
703 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
705 spin_unlock_irqrestore(&m->lock, flags);
707 if (!queue_if_no_path) {
708 dm_table_run_md_queue_async(m->ti->table);
709 process_queued_bios_list(m);
716 * An event is triggered whenever a path is taken out of use.
717 * Includes path failure and PG bypass.
719 static void trigger_event(struct work_struct *work)
721 struct multipath *m =
722 container_of(work, struct multipath, trigger_event);
724 dm_table_event(m->ti->table);
727 /*-----------------------------------------------------------------
728 * Constructor/argument parsing:
729 * <#multipath feature args> [<arg>]*
730 * <#hw_handler args> [hw_handler [<arg>]*]
732 * <initial priority group>
733 * [<selector> <#selector args> [<arg>]*
734 * <#paths> <#per-path selector args>
735 * [<path> [<arg>]* ]+ ]+
736 *---------------------------------------------------------------*/
737 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
738 struct dm_target *ti)
741 struct path_selector_type *pst;
744 static struct dm_arg _args[] = {
745 {0, 1024, "invalid number of path selector args"},
748 pst = dm_get_path_selector(dm_shift_arg(as));
750 ti->error = "unknown path selector type";
754 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
756 dm_put_path_selector(pst);
760 r = pst->create(&pg->ps, ps_argc, as->argv);
762 dm_put_path_selector(pst);
763 ti->error = "path selector constructor failed";
768 dm_consume_args(as, ps_argc);
773 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
774 struct dm_target *ti)
778 struct multipath *m = ti->private;
779 struct request_queue *q = NULL;
780 const char *attached_handler_name;
782 /* we need at least a path arg */
784 ti->error = "no device given";
785 return ERR_PTR(-EINVAL);
790 return ERR_PTR(-ENOMEM);
792 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
795 ti->error = "error getting device";
799 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
800 q = bdev_get_queue(p->path.dev->bdev);
802 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
804 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
805 if (attached_handler_name) {
807 * Reset hw_handler_name to match the attached handler
808 * and clear any hw_handler_params associated with the
811 * NB. This modifies the table line to show the actual
812 * handler instead of the original table passed in.
814 kfree(m->hw_handler_name);
815 m->hw_handler_name = attached_handler_name;
817 kfree(m->hw_handler_params);
818 m->hw_handler_params = NULL;
822 if (m->hw_handler_name) {
823 r = scsi_dh_attach(q, m->hw_handler_name);
825 char b[BDEVNAME_SIZE];
827 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
828 bdevname(p->path.dev->bdev, b));
832 ti->error = "error attaching hardware handler";
833 dm_put_device(ti, p->path.dev);
837 if (m->hw_handler_params) {
838 r = scsi_dh_set_params(q, m->hw_handler_params);
840 ti->error = "unable to set hardware "
841 "handler parameters";
842 dm_put_device(ti, p->path.dev);
848 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
850 dm_put_device(ti, p->path.dev);
861 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
864 static 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 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 (test_bit(MPATHF_BIO_BASED, &m->flags)) {
951 dm_consume_args(as, hw_argc);
952 DMERR("bio-based multipath doesn't allow hardware handler args");
956 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
962 for (i = 0; i <= hw_argc - 2; i++)
963 len += strlen(as->argv[i]) + 1;
964 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
966 ti->error = "memory allocation failed";
970 j = sprintf(p, "%d", hw_argc - 1);
971 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
972 j = sprintf(p, "%s", as->argv[i]);
974 dm_consume_args(as, hw_argc - 1);
978 kfree(m->hw_handler_name);
979 m->hw_handler_name = NULL;
983 static int parse_features(struct dm_arg_set *as, struct multipath *m)
987 struct dm_target *ti = m->ti;
988 const char *arg_name;
990 static struct dm_arg _args[] = {
991 {0, 6, "invalid number of feature args"},
992 {1, 50, "pg_init_retries must be between 1 and 50"},
993 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
996 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1004 arg_name = dm_shift_arg(as);
1007 if (!strcasecmp(arg_name, "queue_if_no_path")) {
1008 r = queue_if_no_path(m, true, false);
1012 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
1013 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
1017 if (!strcasecmp(arg_name, "pg_init_retries") &&
1019 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
1024 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
1026 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
1031 ti->error = "Unrecognised multipath feature request";
1033 } while (argc && !r);
1038 static int __multipath_ctr(struct dm_target *ti, unsigned int argc,
1039 char **argv, bool bio_based)
1041 /* target arguments */
1042 static struct dm_arg _args[] = {
1043 {0, 1024, "invalid number of priority groups"},
1044 {0, 1024, "invalid initial priority group number"},
1048 struct multipath *m;
1049 struct dm_arg_set as;
1050 unsigned pg_count = 0;
1051 unsigned next_pg_num;
1052 bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table));
1057 m = alloc_multipath(ti, use_blk_mq, bio_based);
1059 ti->error = "can't allocate multipath";
1063 r = parse_features(&as, m);
1067 r = parse_hw_handler(&as, m);
1071 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
1075 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
1079 if ((!m->nr_priority_groups && next_pg_num) ||
1080 (m->nr_priority_groups && !next_pg_num)) {
1081 ti->error = "invalid initial priority group";
1086 /* parse the priority groups */
1088 struct priority_group *pg;
1089 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
1091 pg = parse_priority_group(&as, m);
1097 nr_valid_paths += pg->nr_pgpaths;
1098 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1100 list_add_tail(&pg->list, &m->priority_groups);
1102 pg->pg_num = pg_count;
1107 if (pg_count != m->nr_priority_groups) {
1108 ti->error = "priority group count mismatch";
1113 ti->num_flush_bios = 1;
1114 ti->num_discard_bios = 1;
1115 ti->num_write_same_bios = 1;
1116 if (use_blk_mq || bio_based)
1117 ti->per_io_data_size = sizeof(struct dm_mpath_io);
1126 static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
1128 return __multipath_ctr(ti, argc, argv, false);
1131 static int multipath_bio_ctr(struct dm_target *ti, unsigned argc, char **argv)
1133 return __multipath_ctr(ti, argc, argv, true);
1136 static void multipath_wait_for_pg_init_completion(struct multipath *m)
1138 DECLARE_WAITQUEUE(wait, current);
1140 add_wait_queue(&m->pg_init_wait, &wait);
1143 set_current_state(TASK_UNINTERRUPTIBLE);
1145 if (!atomic_read(&m->pg_init_in_progress))
1150 set_current_state(TASK_RUNNING);
1152 remove_wait_queue(&m->pg_init_wait, &wait);
1155 static void flush_multipath_work(struct multipath *m)
1157 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1158 smp_mb__after_atomic();
1160 flush_workqueue(kmpath_handlerd);
1161 multipath_wait_for_pg_init_completion(m);
1162 flush_workqueue(kmultipathd);
1163 flush_work(&m->trigger_event);
1165 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1166 smp_mb__after_atomic();
1169 static void multipath_dtr(struct dm_target *ti)
1171 struct multipath *m = ti->private;
1173 flush_multipath_work(m);
1178 * Take a path out of use.
1180 static int fail_path(struct pgpath *pgpath)
1182 unsigned long flags;
1183 struct multipath *m = pgpath->pg->m;
1185 spin_lock_irqsave(&m->lock, flags);
1187 if (!pgpath->is_active)
1190 DMWARN("Failing path %s.", pgpath->path.dev->name);
1192 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1193 pgpath->is_active = false;
1194 pgpath->fail_count++;
1196 atomic_dec(&m->nr_valid_paths);
1198 if (pgpath == m->current_pgpath)
1199 m->current_pgpath = NULL;
1201 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1202 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1204 schedule_work(&m->trigger_event);
1207 spin_unlock_irqrestore(&m->lock, flags);
1213 * Reinstate a previously-failed path
1215 static int reinstate_path(struct pgpath *pgpath)
1217 int r = 0, run_queue = 0;
1218 unsigned long flags;
1219 struct multipath *m = pgpath->pg->m;
1220 unsigned nr_valid_paths;
1222 spin_lock_irqsave(&m->lock, flags);
1224 if (pgpath->is_active)
1227 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
1229 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1233 pgpath->is_active = true;
1235 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1236 if (nr_valid_paths == 1) {
1237 m->current_pgpath = NULL;
1239 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1240 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1241 atomic_inc(&m->pg_init_in_progress);
1244 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1245 pgpath->path.dev->name, nr_valid_paths);
1247 schedule_work(&m->trigger_event);
1250 spin_unlock_irqrestore(&m->lock, flags);
1252 dm_table_run_md_queue_async(m->ti->table);
1253 process_queued_bios_list(m);
1260 * Fail or reinstate all paths that match the provided struct dm_dev.
1262 static int action_dev(struct multipath *m, struct dm_dev *dev,
1266 struct pgpath *pgpath;
1267 struct priority_group *pg;
1269 list_for_each_entry(pg, &m->priority_groups, list) {
1270 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1271 if (pgpath->path.dev == dev)
1280 * Temporarily try to avoid having to use the specified PG
1282 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1285 unsigned long flags;
1287 spin_lock_irqsave(&m->lock, flags);
1289 pg->bypassed = bypassed;
1290 m->current_pgpath = NULL;
1291 m->current_pg = NULL;
1293 spin_unlock_irqrestore(&m->lock, flags);
1295 schedule_work(&m->trigger_event);
1299 * Switch to using the specified PG from the next I/O that gets mapped
1301 static int switch_pg_num(struct multipath *m, const char *pgstr)
1303 struct priority_group *pg;
1305 unsigned long flags;
1308 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1309 (pgnum > m->nr_priority_groups)) {
1310 DMWARN("invalid PG number supplied to switch_pg_num");
1314 spin_lock_irqsave(&m->lock, flags);
1315 list_for_each_entry(pg, &m->priority_groups, list) {
1316 pg->bypassed = false;
1320 m->current_pgpath = NULL;
1321 m->current_pg = NULL;
1324 spin_unlock_irqrestore(&m->lock, flags);
1326 schedule_work(&m->trigger_event);
1331 * Set/clear bypassed status of a PG.
1332 * PGs are numbered upwards from 1 in the order they were declared.
1334 static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
1336 struct priority_group *pg;
1340 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1341 (pgnum > m->nr_priority_groups)) {
1342 DMWARN("invalid PG number supplied to bypass_pg");
1346 list_for_each_entry(pg, &m->priority_groups, list) {
1351 bypass_pg(m, pg, bypassed);
1356 * Should we retry pg_init immediately?
1358 static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1360 unsigned long flags;
1361 bool limit_reached = false;
1363 spin_lock_irqsave(&m->lock, flags);
1365 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1366 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1367 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1369 limit_reached = true;
1371 spin_unlock_irqrestore(&m->lock, flags);
1373 return limit_reached;
1376 static void pg_init_done(void *data, int errors)
1378 struct pgpath *pgpath = data;
1379 struct priority_group *pg = pgpath->pg;
1380 struct multipath *m = pg->m;
1381 unsigned long flags;
1382 bool delay_retry = false;
1384 /* device or driver problems */
1389 if (!m->hw_handler_name) {
1393 DMERR("Could not failover the device: Handler scsi_dh_%s "
1394 "Error %d.", m->hw_handler_name, errors);
1396 * Fail path for now, so we do not ping pong
1400 case SCSI_DH_DEV_TEMP_BUSY:
1402 * Probably doing something like FW upgrade on the
1403 * controller so try the other pg.
1405 bypass_pg(m, pg, true);
1408 /* Wait before retrying. */
1410 case SCSI_DH_IMM_RETRY:
1411 case SCSI_DH_RES_TEMP_UNAVAIL:
1412 if (pg_init_limit_reached(m, pgpath))
1416 case SCSI_DH_DEV_OFFLINED:
1419 * We probably do not want to fail the path for a device
1420 * error, but this is what the old dm did. In future
1421 * patches we can do more advanced handling.
1426 spin_lock_irqsave(&m->lock, flags);
1428 if (pgpath == m->current_pgpath) {
1429 DMERR("Could not failover device. Error %d.", errors);
1430 m->current_pgpath = NULL;
1431 m->current_pg = NULL;
1433 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1434 pg->bypassed = false;
1436 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1437 /* Activations of other paths are still on going */
1440 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1442 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1444 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1446 if (__pg_init_all_paths(m))
1449 clear_bit(MPATHF_QUEUE_IO, &m->flags);
1451 process_queued_bios_list(m);
1454 * Wake up any thread waiting to suspend.
1456 wake_up(&m->pg_init_wait);
1459 spin_unlock_irqrestore(&m->lock, flags);
1462 static void activate_path(struct work_struct *work)
1464 struct pgpath *pgpath =
1465 container_of(work, struct pgpath, activate_path.work);
1467 if (pgpath->is_active)
1468 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1469 pg_init_done, pgpath);
1471 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1474 static int noretry_error(int error)
1485 /* Anything else could be a path failure, so should be retried */
1492 static int do_end_io(struct multipath *m, struct request *clone,
1493 int error, struct dm_mpath_io *mpio)
1496 * We don't queue any clone request inside the multipath target
1497 * during end I/O handling, since those clone requests don't have
1498 * bio clones. If we queue them inside the multipath target,
1499 * we need to make bio clones, that requires memory allocation.
1500 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1501 * don't have bio clones.)
1502 * Instead of queueing the clone request here, we queue the original
1503 * request into dm core, which will remake a clone request and
1504 * clone bios for it and resubmit it later.
1506 int r = DM_ENDIO_REQUEUE;
1508 if (!error && !clone->errors)
1509 return 0; /* I/O complete */
1511 if (noretry_error(error))
1515 fail_path(mpio->pgpath);
1517 if (!atomic_read(&m->nr_valid_paths)) {
1518 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1519 if (!must_push_back_rq(m))
1522 if (error == -EBADE)
1530 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1531 int error, union map_info *map_context)
1533 struct multipath *m = ti->private;
1534 struct dm_mpath_io *mpio = get_mpio(map_context);
1535 struct pgpath *pgpath;
1536 struct path_selector *ps;
1541 r = do_end_io(m, clone, error, mpio);
1542 pgpath = mpio->pgpath;
1544 ps = &pgpath->pg->ps;
1545 if (ps->type->end_io)
1546 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1548 clear_request_fn_mpio(m, map_context);
1553 static int do_end_io_bio(struct multipath *m, struct bio *clone,
1554 int error, struct dm_mpath_io *mpio)
1556 unsigned long flags;
1559 return 0; /* I/O complete */
1561 if (noretry_error(error))
1565 fail_path(mpio->pgpath);
1567 if (!atomic_read(&m->nr_valid_paths)) {
1568 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1569 if (!must_push_back_bio(m))
1571 return DM_ENDIO_REQUEUE;
1573 if (error == -EBADE)
1578 /* Queue for the daemon to resubmit */
1579 dm_bio_restore(&mpio->bio_details, clone);
1581 spin_lock_irqsave(&m->lock, flags);
1582 bio_list_add(&m->queued_bios, clone);
1583 spin_unlock_irqrestore(&m->lock, flags);
1584 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1585 queue_work(kmultipathd, &m->process_queued_bios);
1587 return DM_ENDIO_INCOMPLETE;
1590 static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
1592 struct multipath *m = ti->private;
1593 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1594 struct pgpath *pgpath;
1595 struct path_selector *ps;
1600 r = do_end_io_bio(m, clone, error, mpio);
1601 pgpath = mpio->pgpath;
1603 ps = &pgpath->pg->ps;
1604 if (ps->type->end_io)
1605 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1612 * Suspend can't complete until all the I/O is processed so if
1613 * the last path fails we must error any remaining I/O.
1614 * Note that if the freeze_bdev fails while suspending, the
1615 * queue_if_no_path state is lost - userspace should reset it.
1617 static void multipath_presuspend(struct dm_target *ti)
1619 struct multipath *m = ti->private;
1621 queue_if_no_path(m, false, true);
1624 static void multipath_postsuspend(struct dm_target *ti)
1626 struct multipath *m = ti->private;
1628 mutex_lock(&m->work_mutex);
1629 flush_multipath_work(m);
1630 mutex_unlock(&m->work_mutex);
1634 * Restore the queue_if_no_path setting.
1636 static void multipath_resume(struct dm_target *ti)
1638 struct multipath *m = ti->private;
1640 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1641 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1643 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1644 smp_mb__after_atomic();
1648 * Info output has the following format:
1649 * num_multipath_feature_args [multipath_feature_args]*
1650 * num_handler_status_args [handler_status_args]*
1651 * num_groups init_group_number
1652 * [A|D|E num_ps_status_args [ps_status_args]*
1653 * num_paths num_selector_args
1654 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1656 * Table output has the following format (identical to the constructor string):
1657 * num_feature_args [features_args]*
1658 * num_handler_args hw_handler [hw_handler_args]*
1659 * num_groups init_group_number
1660 * [priority selector-name num_ps_args [ps_args]*
1661 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1663 static void multipath_status(struct dm_target *ti, status_type_t type,
1664 unsigned status_flags, char *result, unsigned maxlen)
1667 unsigned long flags;
1668 struct multipath *m = ti->private;
1669 struct priority_group *pg;
1674 spin_lock_irqsave(&m->lock, flags);
1677 if (type == STATUSTYPE_INFO)
1678 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1679 atomic_read(&m->pg_init_count));
1681 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
1682 (m->pg_init_retries > 0) * 2 +
1683 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1684 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags));
1685 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1686 DMEMIT("queue_if_no_path ");
1687 if (m->pg_init_retries)
1688 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1689 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1690 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1691 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1692 DMEMIT("retain_attached_hw_handler ");
1695 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1698 DMEMIT("1 %s ", m->hw_handler_name);
1700 DMEMIT("%u ", m->nr_priority_groups);
1703 pg_num = m->next_pg->pg_num;
1704 else if (m->current_pg)
1705 pg_num = m->current_pg->pg_num;
1707 pg_num = (m->nr_priority_groups ? 1 : 0);
1709 DMEMIT("%u ", pg_num);
1712 case STATUSTYPE_INFO:
1713 list_for_each_entry(pg, &m->priority_groups, list) {
1715 state = 'D'; /* Disabled */
1716 else if (pg == m->current_pg)
1717 state = 'A'; /* Currently Active */
1719 state = 'E'; /* Enabled */
1721 DMEMIT("%c ", state);
1723 if (pg->ps.type->status)
1724 sz += pg->ps.type->status(&pg->ps, NULL, type,
1730 DMEMIT("%u %u ", pg->nr_pgpaths,
1731 pg->ps.type->info_args);
1733 list_for_each_entry(p, &pg->pgpaths, list) {
1734 DMEMIT("%s %s %u ", p->path.dev->name,
1735 p->is_active ? "A" : "F",
1737 if (pg->ps.type->status)
1738 sz += pg->ps.type->status(&pg->ps,
1739 &p->path, type, result + sz,
1745 case STATUSTYPE_TABLE:
1746 list_for_each_entry(pg, &m->priority_groups, list) {
1747 DMEMIT("%s ", pg->ps.type->name);
1749 if (pg->ps.type->status)
1750 sz += pg->ps.type->status(&pg->ps, NULL, type,
1756 DMEMIT("%u %u ", pg->nr_pgpaths,
1757 pg->ps.type->table_args);
1759 list_for_each_entry(p, &pg->pgpaths, list) {
1760 DMEMIT("%s ", p->path.dev->name);
1761 if (pg->ps.type->status)
1762 sz += pg->ps.type->status(&pg->ps,
1763 &p->path, type, result + sz,
1770 spin_unlock_irqrestore(&m->lock, flags);
1773 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1777 struct multipath *m = ti->private;
1780 mutex_lock(&m->work_mutex);
1782 if (dm_suspended(ti)) {
1788 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1789 r = queue_if_no_path(m, true, false);
1791 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1792 r = queue_if_no_path(m, false, false);
1798 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
1802 if (!strcasecmp(argv[0], "disable_group")) {
1803 r = bypass_pg_num(m, argv[1], true);
1805 } else if (!strcasecmp(argv[0], "enable_group")) {
1806 r = bypass_pg_num(m, argv[1], false);
1808 } else if (!strcasecmp(argv[0], "switch_group")) {
1809 r = switch_pg_num(m, argv[1]);
1811 } else if (!strcasecmp(argv[0], "reinstate_path"))
1812 action = reinstate_path;
1813 else if (!strcasecmp(argv[0], "fail_path"))
1816 DMWARN("Unrecognised multipath message received: %s", argv[0]);
1820 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1822 DMWARN("message: error getting device %s",
1827 r = action_dev(m, dev, action);
1829 dm_put_device(ti, dev);
1832 mutex_unlock(&m->work_mutex);
1836 static int multipath_prepare_ioctl(struct dm_target *ti,
1837 struct block_device **bdev, fmode_t *mode)
1839 struct multipath *m = ti->private;
1840 struct pgpath *current_pgpath;
1843 current_pgpath = lockless_dereference(m->current_pgpath);
1844 if (!current_pgpath)
1845 current_pgpath = choose_pgpath(m, 0);
1847 if (current_pgpath) {
1848 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
1849 *bdev = current_pgpath->path.dev->bdev;
1850 *mode = current_pgpath->path.dev->mode;
1853 /* pg_init has not started or completed */
1857 /* No path is available */
1858 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1864 if (r == -ENOTCONN) {
1865 if (!lockless_dereference(m->current_pg)) {
1866 /* Path status changed, redo selection */
1867 (void) choose_pgpath(m, 0);
1869 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1870 pg_init_all_paths(m);
1871 dm_table_run_md_queue_async(m->ti->table);
1872 process_queued_bios_list(m);
1876 * Only pass ioctls through if the device sizes match exactly.
1878 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1883 static int multipath_iterate_devices(struct dm_target *ti,
1884 iterate_devices_callout_fn fn, void *data)
1886 struct multipath *m = ti->private;
1887 struct priority_group *pg;
1891 list_for_each_entry(pg, &m->priority_groups, list) {
1892 list_for_each_entry(p, &pg->pgpaths, list) {
1893 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1903 static int pgpath_busy(struct pgpath *pgpath)
1905 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1907 return blk_lld_busy(q);
1911 * We return "busy", only when we can map I/Os but underlying devices
1912 * are busy (so even if we map I/Os now, the I/Os will wait on
1913 * the underlying queue).
1914 * In other words, if we want to kill I/Os or queue them inside us
1915 * due to map unavailability, we don't return "busy". Otherwise,
1916 * dm core won't give us the I/Os and we can't do what we want.
1918 static int multipath_busy(struct dm_target *ti)
1920 bool busy = false, has_active = false;
1921 struct multipath *m = ti->private;
1922 struct priority_group *pg, *next_pg;
1923 struct pgpath *pgpath;
1925 /* pg_init in progress or no paths available */
1926 if (atomic_read(&m->pg_init_in_progress) ||
1927 (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)))
1930 /* Guess which priority_group will be used at next mapping time */
1931 pg = lockless_dereference(m->current_pg);
1932 next_pg = lockless_dereference(m->next_pg);
1933 if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
1938 * We don't know which pg will be used at next mapping time.
1939 * We don't call choose_pgpath() here to avoid to trigger
1940 * pg_init just by busy checking.
1941 * So we don't know whether underlying devices we will be using
1942 * at next mapping time are busy or not. Just try mapping.
1948 * If there is one non-busy active path at least, the path selector
1949 * will be able to select it. So we consider such a pg as not busy.
1952 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1953 if (pgpath->is_active) {
1955 if (!pgpath_busy(pgpath)) {
1964 * No active path in this pg, so this pg won't be used and
1965 * the current_pg will be changed at next mapping time.
1966 * We need to try mapping to determine it.
1974 /*-----------------------------------------------------------------
1976 *---------------------------------------------------------------*/
1977 static struct target_type multipath_target = {
1978 .name = "multipath",
1979 .version = {1, 11, 0},
1980 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
1981 .module = THIS_MODULE,
1982 .ctr = multipath_ctr,
1983 .dtr = multipath_dtr,
1984 .map_rq = multipath_map,
1985 .clone_and_map_rq = multipath_clone_and_map,
1986 .release_clone_rq = multipath_release_clone,
1987 .rq_end_io = multipath_end_io,
1988 .presuspend = multipath_presuspend,
1989 .postsuspend = multipath_postsuspend,
1990 .resume = multipath_resume,
1991 .status = multipath_status,
1992 .message = multipath_message,
1993 .prepare_ioctl = multipath_prepare_ioctl,
1994 .iterate_devices = multipath_iterate_devices,
1995 .busy = multipath_busy,
1998 static struct target_type multipath_bio_target = {
1999 .name = "multipath-bio",
2000 .version = {1, 0, 0},
2001 .module = THIS_MODULE,
2002 .ctr = multipath_bio_ctr,
2003 .dtr = multipath_dtr,
2004 .map = multipath_map_bio,
2005 .end_io = multipath_end_io_bio,
2006 .presuspend = multipath_presuspend,
2007 .postsuspend = multipath_postsuspend,
2008 .resume = multipath_resume,
2009 .status = multipath_status,
2010 .message = multipath_message,
2011 .prepare_ioctl = multipath_prepare_ioctl,
2012 .iterate_devices = multipath_iterate_devices,
2013 .busy = multipath_busy,
2016 static int __init dm_multipath_init(void)
2020 /* allocate a slab for the dm_mpath_ios */
2021 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
2025 r = dm_register_target(&multipath_target);
2027 DMERR("request-based register failed %d", r);
2029 goto bad_register_target;
2032 r = dm_register_target(&multipath_bio_target);
2034 DMERR("bio-based register failed %d", r);
2036 goto bad_register_bio_based_target;
2039 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2041 DMERR("failed to create workqueue kmpathd");
2043 goto bad_alloc_kmultipathd;
2047 * A separate workqueue is used to handle the device handlers
2048 * to avoid overloading existing workqueue. Overloading the
2049 * old workqueue would also create a bottleneck in the
2050 * path of the storage hardware device activation.
2052 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2054 if (!kmpath_handlerd) {
2055 DMERR("failed to create workqueue kmpath_handlerd");
2057 goto bad_alloc_kmpath_handlerd;
2062 bad_alloc_kmpath_handlerd:
2063 destroy_workqueue(kmultipathd);
2064 bad_alloc_kmultipathd:
2065 dm_unregister_target(&multipath_bio_target);
2066 bad_register_bio_based_target:
2067 dm_unregister_target(&multipath_target);
2068 bad_register_target:
2069 kmem_cache_destroy(_mpio_cache);
2074 static void __exit dm_multipath_exit(void)
2076 destroy_workqueue(kmpath_handlerd);
2077 destroy_workqueue(kmultipathd);
2079 dm_unregister_target(&multipath_target);
2080 dm_unregister_target(&multipath_bio_target);
2081 kmem_cache_destroy(_mpio_cache);
2084 module_init(dm_multipath_init);
2085 module_exit(dm_multipath_exit);
2087 MODULE_DESCRIPTION(DM_NAME " multipath target");
2088 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2089 MODULE_LICENSE("GPL");