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-path-selector.h"
12 #include "dm-uevent.h"
14 #include <linux/blkdev.h>
15 #include <linux/ctype.h>
16 #include <linux/init.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/slab.h>
21 #include <linux/time.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <scsi/scsi_dh.h>
25 #include <linux/atomic.h>
26 #include <linux/blk-mq.h>
28 #define DM_MSG_PREFIX "multipath"
29 #define DM_PG_INIT_DELAY_MSECS 2000
30 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
34 struct list_head list;
36 struct priority_group *pg; /* Owning PG */
37 unsigned fail_count; /* Cumulative failure count */
40 struct delayed_work activate_path;
42 bool is_active:1; /* Path status */
45 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
48 * Paths are grouped into Priority Groups and numbered from 1 upwards.
49 * Each has a path selector which controls which path gets used.
51 struct priority_group {
52 struct list_head list;
54 struct multipath *m; /* Owning multipath instance */
55 struct path_selector ps;
57 unsigned pg_num; /* Reference number */
58 unsigned nr_pgpaths; /* Number of paths in PG */
59 struct list_head pgpaths;
61 bool bypassed:1; /* Temporarily bypass this PG? */
64 /* Multipath context */
66 struct list_head list;
69 const char *hw_handler_name;
70 char *hw_handler_params;
74 unsigned nr_priority_groups;
75 struct list_head priority_groups;
77 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
79 struct pgpath *current_pgpath;
80 struct priority_group *current_pg;
81 struct priority_group *next_pg; /* Switch to this PG if set */
83 unsigned long flags; /* Multipath state flags */
85 unsigned pg_init_retries; /* Number of times to retry pg_init */
86 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
88 atomic_t nr_valid_paths; /* Total number of usable paths */
89 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
90 atomic_t pg_init_count; /* Number of times pg_init called */
92 struct work_struct trigger_event;
95 * We must use a mempool of dm_mpath_io structs so that we
96 * can resubmit bios on error.
100 struct mutex work_mutex;
104 * Context information attached to each bio we process.
107 struct pgpath *pgpath;
111 typedef int (*action_fn) (struct pgpath *pgpath);
113 static struct kmem_cache *_mpio_cache;
115 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
116 static void trigger_event(struct work_struct *work);
117 static void activate_path(struct work_struct *work);
119 /*-----------------------------------------------
120 * Multipath state flags.
121 *-----------------------------------------------*/
123 #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
124 #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
125 #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
126 #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
127 #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
128 #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
129 #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
131 /*-----------------------------------------------
132 * Allocation routines
133 *-----------------------------------------------*/
135 static struct pgpath *alloc_pgpath(void)
137 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
140 pgpath->is_active = true;
141 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
147 static void free_pgpath(struct pgpath *pgpath)
152 static struct priority_group *alloc_priority_group(void)
154 struct priority_group *pg;
156 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
159 INIT_LIST_HEAD(&pg->pgpaths);
164 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
166 struct pgpath *pgpath, *tmp;
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
170 dm_put_device(ti, pgpath->path.dev);
175 static void free_priority_group(struct priority_group *pg,
176 struct dm_target *ti)
178 struct path_selector *ps = &pg->ps;
181 ps->type->destroy(ps);
182 dm_put_path_selector(ps->type);
185 free_pgpaths(&pg->pgpaths, ti);
189 static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq)
193 m = kzalloc(sizeof(*m), GFP_KERNEL);
195 INIT_LIST_HEAD(&m->priority_groups);
196 spin_lock_init(&m->lock);
197 set_bit(MPATHF_QUEUE_IO, &m->flags);
198 atomic_set(&m->nr_valid_paths, 0);
199 atomic_set(&m->pg_init_in_progress, 0);
200 atomic_set(&m->pg_init_count, 0);
201 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
202 INIT_WORK(&m->trigger_event, trigger_event);
203 init_waitqueue_head(&m->pg_init_wait);
204 mutex_init(&m->work_mutex);
208 unsigned min_ios = dm_get_reserved_rq_based_ios();
210 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
224 static void free_multipath(struct multipath *m)
226 struct priority_group *pg, *tmp;
228 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
230 free_priority_group(pg, m->ti);
233 kfree(m->hw_handler_name);
234 kfree(m->hw_handler_params);
235 mempool_destroy(m->mpio_pool);
239 static struct dm_mpath_io *get_mpio(union map_info *info)
244 static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
246 struct dm_mpath_io *mpio;
249 /* Use blk-mq pdu memory requested via per_io_data_size */
250 mpio = get_mpio(info);
251 memset(mpio, 0, sizeof(*mpio));
255 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
259 memset(mpio, 0, sizeof(*mpio));
265 static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
267 /* Only needed for non blk-mq (.request_fn) multipath */
269 struct dm_mpath_io *mpio = info->ptr;
272 mempool_free(mpio, m->mpio_pool);
276 /*-----------------------------------------------
278 *-----------------------------------------------*/
280 static int __pg_init_all_paths(struct multipath *m)
282 struct pgpath *pgpath;
283 unsigned long pg_init_delay = 0;
285 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
288 atomic_inc(&m->pg_init_count);
289 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
291 /* Check here to reset pg_init_required */
295 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
296 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
297 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
298 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
299 /* Skip failed paths */
300 if (!pgpath->is_active)
302 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
304 atomic_inc(&m->pg_init_in_progress);
306 return atomic_read(&m->pg_init_in_progress);
309 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
311 m->current_pg = pgpath->pg;
313 /* Must we initialise the PG first, and queue I/O till it's ready? */
314 if (m->hw_handler_name) {
315 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
316 set_bit(MPATHF_QUEUE_IO, &m->flags);
318 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
319 clear_bit(MPATHF_QUEUE_IO, &m->flags);
322 atomic_set(&m->pg_init_count, 0);
325 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
328 struct dm_path *path;
330 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
334 m->current_pgpath = path_to_pgpath(path);
336 if (m->current_pg != pg)
337 __switch_pg(m, m->current_pgpath);
342 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
344 struct priority_group *pg;
345 bool bypassed = true;
347 if (!atomic_read(&m->nr_valid_paths)) {
348 clear_bit(MPATHF_QUEUE_IO, &m->flags);
352 /* Were we instructed to switch PG? */
356 if (!__choose_path_in_pg(m, pg, nr_bytes))
360 /* Don't change PG until it has no remaining paths */
361 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
365 * Loop through priority groups until we find a valid path.
366 * First time we skip PGs marked 'bypassed'.
367 * Second time we only try the ones we skipped, but set
368 * pg_init_delay_retry so we do not hammer controllers.
371 list_for_each_entry(pg, &m->priority_groups, list) {
372 if (pg->bypassed == bypassed)
374 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
376 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
380 } while (bypassed--);
383 m->current_pgpath = NULL;
384 m->current_pg = NULL;
388 * Check whether bios must be queued in the device-mapper core rather
389 * than here in the target.
391 * m->lock must be held on entry.
393 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
394 * same value then we are not between multipath_presuspend()
395 * and multipath_resume() calls and we have no need to check
396 * for the DMF_NOFLUSH_SUSPENDING flag.
398 static int __must_push_back(struct multipath *m)
400 return (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
401 ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
402 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
403 dm_noflush_suspending(m->ti)));
407 * Map cloned requests
409 static int __multipath_map(struct dm_target *ti, struct request *clone,
410 union map_info *map_context,
411 struct request *rq, struct request **__clone)
413 struct multipath *m = ti->private;
414 int r = DM_MAPIO_REQUEUE;
415 size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
416 struct pgpath *pgpath;
417 struct block_device *bdev;
418 struct dm_mpath_io *mpio;
420 spin_lock_irq(&m->lock);
422 /* Do we need to select a new pgpath? */
423 if (!m->current_pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
424 __choose_pgpath(m, nr_bytes);
426 pgpath = m->current_pgpath;
429 if (!__must_push_back(m))
430 r = -EIO; /* Failed */
432 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
433 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
434 __pg_init_all_paths(m);
438 mpio = set_mpio(m, map_context);
440 /* ENOMEM, requeue */
443 mpio->pgpath = pgpath;
444 mpio->nr_bytes = nr_bytes;
446 bdev = pgpath->path.dev->bdev;
448 spin_unlock_irq(&m->lock);
452 * Old request-based interface: allocated clone is passed in.
453 * Used by: .request_fn stacked on .request_fn path(s).
455 clone->q = bdev_get_queue(bdev);
456 clone->rq_disk = bdev->bd_disk;
457 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
460 * blk-mq request-based interface; used by both:
461 * .request_fn stacked on blk-mq path(s) and
462 * blk-mq stacked on blk-mq path(s).
464 *__clone = blk_mq_alloc_request(bdev_get_queue(bdev),
465 rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
466 if (IS_ERR(*__clone)) {
467 /* ENOMEM, requeue */
468 clear_request_fn_mpio(m, map_context);
471 (*__clone)->bio = (*__clone)->biotail = NULL;
472 (*__clone)->rq_disk = bdev->bd_disk;
473 (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
476 if (pgpath->pg->ps.type->start_io)
477 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
480 return DM_MAPIO_REMAPPED;
483 spin_unlock_irq(&m->lock);
488 static int multipath_map(struct dm_target *ti, struct request *clone,
489 union map_info *map_context)
491 return __multipath_map(ti, clone, map_context, NULL, NULL);
494 static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
495 union map_info *map_context,
496 struct request **clone)
498 return __multipath_map(ti, NULL, map_context, rq, clone);
501 static void multipath_release_clone(struct request *clone)
503 blk_mq_free_request(clone);
507 * If we run out of usable paths, should we queue I/O or error it?
509 static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
514 spin_lock_irqsave(&m->lock, flags);
516 if (save_old_value) {
517 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
518 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
520 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
522 if (queue_if_no_path)
523 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
525 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
527 if (queue_if_no_path)
528 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
530 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
532 spin_unlock_irqrestore(&m->lock, flags);
534 if (!queue_if_no_path)
535 dm_table_run_md_queue_async(m->ti->table);
541 * An event is triggered whenever a path is taken out of use.
542 * Includes path failure and PG bypass.
544 static void trigger_event(struct work_struct *work)
546 struct multipath *m =
547 container_of(work, struct multipath, trigger_event);
549 dm_table_event(m->ti->table);
552 /*-----------------------------------------------------------------
553 * Constructor/argument parsing:
554 * <#multipath feature args> [<arg>]*
555 * <#hw_handler args> [hw_handler [<arg>]*]
557 * <initial priority group>
558 * [<selector> <#selector args> [<arg>]*
559 * <#paths> <#per-path selector args>
560 * [<path> [<arg>]* ]+ ]+
561 *---------------------------------------------------------------*/
562 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
563 struct dm_target *ti)
566 struct path_selector_type *pst;
569 static struct dm_arg _args[] = {
570 {0, 1024, "invalid number of path selector args"},
573 pst = dm_get_path_selector(dm_shift_arg(as));
575 ti->error = "unknown path selector type";
579 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
581 dm_put_path_selector(pst);
585 r = pst->create(&pg->ps, ps_argc, as->argv);
587 dm_put_path_selector(pst);
588 ti->error = "path selector constructor failed";
593 dm_consume_args(as, ps_argc);
598 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
599 struct dm_target *ti)
603 struct multipath *m = ti->private;
604 struct request_queue *q = NULL;
605 const char *attached_handler_name;
607 /* we need at least a path arg */
609 ti->error = "no device given";
610 return ERR_PTR(-EINVAL);
615 return ERR_PTR(-ENOMEM);
617 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
620 ti->error = "error getting device";
624 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
625 q = bdev_get_queue(p->path.dev->bdev);
627 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
629 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
630 if (attached_handler_name) {
632 * Reset hw_handler_name to match the attached handler
633 * and clear any hw_handler_params associated with the
636 * NB. This modifies the table line to show the actual
637 * handler instead of the original table passed in.
639 kfree(m->hw_handler_name);
640 m->hw_handler_name = attached_handler_name;
642 kfree(m->hw_handler_params);
643 m->hw_handler_params = NULL;
647 if (m->hw_handler_name) {
648 r = scsi_dh_attach(q, m->hw_handler_name);
650 char b[BDEVNAME_SIZE];
652 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
653 bdevname(p->path.dev->bdev, b));
657 ti->error = "error attaching hardware handler";
658 dm_put_device(ti, p->path.dev);
662 if (m->hw_handler_params) {
663 r = scsi_dh_set_params(q, m->hw_handler_params);
665 ti->error = "unable to set hardware "
666 "handler parameters";
667 dm_put_device(ti, p->path.dev);
673 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
675 dm_put_device(ti, p->path.dev);
686 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
689 static struct dm_arg _args[] = {
690 {1, 1024, "invalid number of paths"},
691 {0, 1024, "invalid number of selector args"}
695 unsigned i, nr_selector_args, nr_args;
696 struct priority_group *pg;
697 struct dm_target *ti = m->ti;
701 ti->error = "not enough priority group arguments";
702 return ERR_PTR(-EINVAL);
705 pg = alloc_priority_group();
707 ti->error = "couldn't allocate priority group";
708 return ERR_PTR(-ENOMEM);
712 r = parse_path_selector(as, pg, ti);
719 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
723 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
727 nr_args = 1 + nr_selector_args;
728 for (i = 0; i < pg->nr_pgpaths; i++) {
729 struct pgpath *pgpath;
730 struct dm_arg_set path_args;
732 if (as->argc < nr_args) {
733 ti->error = "not enough path parameters";
738 path_args.argc = nr_args;
739 path_args.argv = as->argv;
741 pgpath = parse_path(&path_args, &pg->ps, ti);
742 if (IS_ERR(pgpath)) {
748 list_add_tail(&pgpath->list, &pg->pgpaths);
749 dm_consume_args(as, nr_args);
755 free_priority_group(pg, ti);
759 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
763 struct dm_target *ti = m->ti;
765 static struct dm_arg _args[] = {
766 {0, 1024, "invalid number of hardware handler args"},
769 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
775 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
781 for (i = 0; i <= hw_argc - 2; i++)
782 len += strlen(as->argv[i]) + 1;
783 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
785 ti->error = "memory allocation failed";
789 j = sprintf(p, "%d", hw_argc - 1);
790 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
791 j = sprintf(p, "%s", as->argv[i]);
793 dm_consume_args(as, hw_argc - 1);
797 kfree(m->hw_handler_name);
798 m->hw_handler_name = NULL;
802 static int parse_features(struct dm_arg_set *as, struct multipath *m)
806 struct dm_target *ti = m->ti;
807 const char *arg_name;
809 static struct dm_arg _args[] = {
810 {0, 6, "invalid number of feature args"},
811 {1, 50, "pg_init_retries must be between 1 and 50"},
812 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
815 r = dm_read_arg_group(_args, as, &argc, &ti->error);
823 arg_name = dm_shift_arg(as);
826 if (!strcasecmp(arg_name, "queue_if_no_path")) {
827 r = queue_if_no_path(m, true, false);
831 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
832 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
836 if (!strcasecmp(arg_name, "pg_init_retries") &&
838 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
843 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
845 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
850 ti->error = "Unrecognised multipath feature request";
852 } while (argc && !r);
857 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
860 /* target arguments */
861 static struct dm_arg _args[] = {
862 {0, 1024, "invalid number of priority groups"},
863 {0, 1024, "invalid initial priority group number"},
868 struct dm_arg_set as;
869 unsigned pg_count = 0;
870 unsigned next_pg_num;
871 bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table));
876 m = alloc_multipath(ti, use_blk_mq);
878 ti->error = "can't allocate multipath";
882 r = parse_features(&as, m);
886 r = parse_hw_handler(&as, m);
890 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
894 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
898 if ((!m->nr_priority_groups && next_pg_num) ||
899 (m->nr_priority_groups && !next_pg_num)) {
900 ti->error = "invalid initial priority group";
905 /* parse the priority groups */
907 struct priority_group *pg;
908 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
910 pg = parse_priority_group(&as, m);
916 nr_valid_paths += pg->nr_pgpaths;
917 atomic_set(&m->nr_valid_paths, nr_valid_paths);
919 list_add_tail(&pg->list, &m->priority_groups);
921 pg->pg_num = pg_count;
926 if (pg_count != m->nr_priority_groups) {
927 ti->error = "priority group count mismatch";
932 ti->num_flush_bios = 1;
933 ti->num_discard_bios = 1;
934 ti->num_write_same_bios = 1;
936 ti->per_io_data_size = sizeof(struct dm_mpath_io);
945 static void multipath_wait_for_pg_init_completion(struct multipath *m)
947 DECLARE_WAITQUEUE(wait, current);
949 add_wait_queue(&m->pg_init_wait, &wait);
952 set_current_state(TASK_UNINTERRUPTIBLE);
954 if (!atomic_read(&m->pg_init_in_progress))
959 set_current_state(TASK_RUNNING);
961 remove_wait_queue(&m->pg_init_wait, &wait);
964 static void flush_multipath_work(struct multipath *m)
966 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
967 smp_mb__after_atomic();
969 flush_workqueue(kmpath_handlerd);
970 multipath_wait_for_pg_init_completion(m);
971 flush_workqueue(kmultipathd);
972 flush_work(&m->trigger_event);
974 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
975 smp_mb__after_atomic();
978 static void multipath_dtr(struct dm_target *ti)
980 struct multipath *m = ti->private;
982 flush_multipath_work(m);
987 * Take a path out of use.
989 static int fail_path(struct pgpath *pgpath)
992 struct multipath *m = pgpath->pg->m;
994 spin_lock_irqsave(&m->lock, flags);
996 if (!pgpath->is_active)
999 DMWARN("Failing path %s.", pgpath->path.dev->name);
1001 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1002 pgpath->is_active = false;
1003 pgpath->fail_count++;
1005 atomic_dec(&m->nr_valid_paths);
1007 if (pgpath == m->current_pgpath)
1008 m->current_pgpath = NULL;
1010 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1011 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1013 schedule_work(&m->trigger_event);
1016 spin_unlock_irqrestore(&m->lock, flags);
1022 * Reinstate a previously-failed path
1024 static int reinstate_path(struct pgpath *pgpath)
1026 int r = 0, run_queue = 0;
1027 unsigned long flags;
1028 struct multipath *m = pgpath->pg->m;
1029 unsigned nr_valid_paths;
1031 spin_lock_irqsave(&m->lock, flags);
1033 if (pgpath->is_active)
1036 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
1038 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1042 pgpath->is_active = true;
1044 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1045 if (nr_valid_paths == 1) {
1046 m->current_pgpath = NULL;
1048 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1049 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1050 atomic_inc(&m->pg_init_in_progress);
1053 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1054 pgpath->path.dev->name, nr_valid_paths);
1056 schedule_work(&m->trigger_event);
1059 spin_unlock_irqrestore(&m->lock, flags);
1061 dm_table_run_md_queue_async(m->ti->table);
1067 * Fail or reinstate all paths that match the provided struct dm_dev.
1069 static int action_dev(struct multipath *m, struct dm_dev *dev,
1073 struct pgpath *pgpath;
1074 struct priority_group *pg;
1076 list_for_each_entry(pg, &m->priority_groups, list) {
1077 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1078 if (pgpath->path.dev == dev)
1087 * Temporarily try to avoid having to use the specified PG
1089 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1092 unsigned long flags;
1094 spin_lock_irqsave(&m->lock, flags);
1096 pg->bypassed = bypassed;
1097 m->current_pgpath = NULL;
1098 m->current_pg = NULL;
1100 spin_unlock_irqrestore(&m->lock, flags);
1102 schedule_work(&m->trigger_event);
1106 * Switch to using the specified PG from the next I/O that gets mapped
1108 static int switch_pg_num(struct multipath *m, const char *pgstr)
1110 struct priority_group *pg;
1112 unsigned long flags;
1115 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1116 (pgnum > m->nr_priority_groups)) {
1117 DMWARN("invalid PG number supplied to switch_pg_num");
1121 spin_lock_irqsave(&m->lock, flags);
1122 list_for_each_entry(pg, &m->priority_groups, list) {
1123 pg->bypassed = false;
1127 m->current_pgpath = NULL;
1128 m->current_pg = NULL;
1131 spin_unlock_irqrestore(&m->lock, flags);
1133 schedule_work(&m->trigger_event);
1138 * Set/clear bypassed status of a PG.
1139 * PGs are numbered upwards from 1 in the order they were declared.
1141 static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
1143 struct priority_group *pg;
1147 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1148 (pgnum > m->nr_priority_groups)) {
1149 DMWARN("invalid PG number supplied to bypass_pg");
1153 list_for_each_entry(pg, &m->priority_groups, list) {
1158 bypass_pg(m, pg, bypassed);
1163 * Should we retry pg_init immediately?
1165 static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1167 unsigned long flags;
1168 bool limit_reached = false;
1170 spin_lock_irqsave(&m->lock, flags);
1172 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1173 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1174 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1176 limit_reached = true;
1178 spin_unlock_irqrestore(&m->lock, flags);
1180 return limit_reached;
1183 static void pg_init_done(void *data, int errors)
1185 struct pgpath *pgpath = data;
1186 struct priority_group *pg = pgpath->pg;
1187 struct multipath *m = pg->m;
1188 unsigned long flags;
1189 bool delay_retry = false;
1191 /* device or driver problems */
1196 if (!m->hw_handler_name) {
1200 DMERR("Could not failover the device: Handler scsi_dh_%s "
1201 "Error %d.", m->hw_handler_name, errors);
1203 * Fail path for now, so we do not ping pong
1207 case SCSI_DH_DEV_TEMP_BUSY:
1209 * Probably doing something like FW upgrade on the
1210 * controller so try the other pg.
1212 bypass_pg(m, pg, true);
1215 /* Wait before retrying. */
1217 case SCSI_DH_IMM_RETRY:
1218 case SCSI_DH_RES_TEMP_UNAVAIL:
1219 if (pg_init_limit_reached(m, pgpath))
1223 case SCSI_DH_DEV_OFFLINED:
1226 * We probably do not want to fail the path for a device
1227 * error, but this is what the old dm did. In future
1228 * patches we can do more advanced handling.
1233 spin_lock_irqsave(&m->lock, flags);
1235 if (pgpath == m->current_pgpath) {
1236 DMERR("Could not failover device. Error %d.", errors);
1237 m->current_pgpath = NULL;
1238 m->current_pg = NULL;
1240 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1241 pg->bypassed = false;
1243 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1244 /* Activations of other paths are still on going */
1247 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1249 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1251 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1253 if (__pg_init_all_paths(m))
1256 clear_bit(MPATHF_QUEUE_IO, &m->flags);
1259 * Wake up any thread waiting to suspend.
1261 wake_up(&m->pg_init_wait);
1264 spin_unlock_irqrestore(&m->lock, flags);
1267 static void activate_path(struct work_struct *work)
1269 struct pgpath *pgpath =
1270 container_of(work, struct pgpath, activate_path.work);
1272 if (pgpath->is_active)
1273 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1274 pg_init_done, pgpath);
1276 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1279 static int noretry_error(int error)
1290 /* Anything else could be a path failure, so should be retried */
1297 static int do_end_io(struct multipath *m, struct request *clone,
1298 int error, struct dm_mpath_io *mpio)
1301 * We don't queue any clone request inside the multipath target
1302 * during end I/O handling, since those clone requests don't have
1303 * bio clones. If we queue them inside the multipath target,
1304 * we need to make bio clones, that requires memory allocation.
1305 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1306 * don't have bio clones.)
1307 * Instead of queueing the clone request here, we queue the original
1308 * request into dm core, which will remake a clone request and
1309 * clone bios for it and resubmit it later.
1311 int r = DM_ENDIO_REQUEUE;
1312 unsigned long flags;
1314 if (!error && !clone->errors)
1315 return 0; /* I/O complete */
1317 if (noretry_error(error))
1321 fail_path(mpio->pgpath);
1323 spin_lock_irqsave(&m->lock, flags);
1324 if (!atomic_read(&m->nr_valid_paths)) {
1325 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1326 if (!__must_push_back(m))
1329 if (error == -EBADE)
1333 spin_unlock_irqrestore(&m->lock, flags);
1338 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1339 int error, union map_info *map_context)
1341 struct multipath *m = ti->private;
1342 struct dm_mpath_io *mpio = get_mpio(map_context);
1343 struct pgpath *pgpath;
1344 struct path_selector *ps;
1349 r = do_end_io(m, clone, error, mpio);
1350 pgpath = mpio->pgpath;
1352 ps = &pgpath->pg->ps;
1353 if (ps->type->end_io)
1354 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1356 clear_request_fn_mpio(m, map_context);
1362 * Suspend can't complete until all the I/O is processed so if
1363 * the last path fails we must error any remaining I/O.
1364 * Note that if the freeze_bdev fails while suspending, the
1365 * queue_if_no_path state is lost - userspace should reset it.
1367 static void multipath_presuspend(struct dm_target *ti)
1369 struct multipath *m = ti->private;
1371 queue_if_no_path(m, false, true);
1374 static void multipath_postsuspend(struct dm_target *ti)
1376 struct multipath *m = ti->private;
1378 mutex_lock(&m->work_mutex);
1379 flush_multipath_work(m);
1380 mutex_unlock(&m->work_mutex);
1384 * Restore the queue_if_no_path setting.
1386 static void multipath_resume(struct dm_target *ti)
1388 struct multipath *m = ti->private;
1390 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1391 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1393 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1394 smp_mb__after_atomic();
1398 * Info output has the following format:
1399 * num_multipath_feature_args [multipath_feature_args]*
1400 * num_handler_status_args [handler_status_args]*
1401 * num_groups init_group_number
1402 * [A|D|E num_ps_status_args [ps_status_args]*
1403 * num_paths num_selector_args
1404 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1406 * Table output has the following format (identical to the constructor string):
1407 * num_feature_args [features_args]*
1408 * num_handler_args hw_handler [hw_handler_args]*
1409 * num_groups init_group_number
1410 * [priority selector-name num_ps_args [ps_args]*
1411 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1413 static void multipath_status(struct dm_target *ti, status_type_t type,
1414 unsigned status_flags, char *result, unsigned maxlen)
1417 unsigned long flags;
1418 struct multipath *m = ti->private;
1419 struct priority_group *pg;
1424 spin_lock_irqsave(&m->lock, flags);
1427 if (type == STATUSTYPE_INFO)
1428 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1429 atomic_read(&m->pg_init_count));
1431 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
1432 (m->pg_init_retries > 0) * 2 +
1433 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1434 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags));
1435 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1436 DMEMIT("queue_if_no_path ");
1437 if (m->pg_init_retries)
1438 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1439 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1440 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1441 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1442 DMEMIT("retain_attached_hw_handler ");
1445 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1448 DMEMIT("1 %s ", m->hw_handler_name);
1450 DMEMIT("%u ", m->nr_priority_groups);
1453 pg_num = m->next_pg->pg_num;
1454 else if (m->current_pg)
1455 pg_num = m->current_pg->pg_num;
1457 pg_num = (m->nr_priority_groups ? 1 : 0);
1459 DMEMIT("%u ", pg_num);
1462 case STATUSTYPE_INFO:
1463 list_for_each_entry(pg, &m->priority_groups, list) {
1465 state = 'D'; /* Disabled */
1466 else if (pg == m->current_pg)
1467 state = 'A'; /* Currently Active */
1469 state = 'E'; /* Enabled */
1471 DMEMIT("%c ", state);
1473 if (pg->ps.type->status)
1474 sz += pg->ps.type->status(&pg->ps, NULL, type,
1480 DMEMIT("%u %u ", pg->nr_pgpaths,
1481 pg->ps.type->info_args);
1483 list_for_each_entry(p, &pg->pgpaths, list) {
1484 DMEMIT("%s %s %u ", p->path.dev->name,
1485 p->is_active ? "A" : "F",
1487 if (pg->ps.type->status)
1488 sz += pg->ps.type->status(&pg->ps,
1489 &p->path, type, result + sz,
1495 case STATUSTYPE_TABLE:
1496 list_for_each_entry(pg, &m->priority_groups, list) {
1497 DMEMIT("%s ", pg->ps.type->name);
1499 if (pg->ps.type->status)
1500 sz += pg->ps.type->status(&pg->ps, NULL, type,
1506 DMEMIT("%u %u ", pg->nr_pgpaths,
1507 pg->ps.type->table_args);
1509 list_for_each_entry(p, &pg->pgpaths, list) {
1510 DMEMIT("%s ", p->path.dev->name);
1511 if (pg->ps.type->status)
1512 sz += pg->ps.type->status(&pg->ps,
1513 &p->path, type, result + sz,
1520 spin_unlock_irqrestore(&m->lock, flags);
1523 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1527 struct multipath *m = ti->private;
1530 mutex_lock(&m->work_mutex);
1532 if (dm_suspended(ti)) {
1538 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1539 r = queue_if_no_path(m, true, false);
1541 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1542 r = queue_if_no_path(m, false, false);
1548 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
1552 if (!strcasecmp(argv[0], "disable_group")) {
1553 r = bypass_pg_num(m, argv[1], true);
1555 } else if (!strcasecmp(argv[0], "enable_group")) {
1556 r = bypass_pg_num(m, argv[1], false);
1558 } else if (!strcasecmp(argv[0], "switch_group")) {
1559 r = switch_pg_num(m, argv[1]);
1561 } else if (!strcasecmp(argv[0], "reinstate_path"))
1562 action = reinstate_path;
1563 else if (!strcasecmp(argv[0], "fail_path"))
1566 DMWARN("Unrecognised multipath message received: %s", argv[0]);
1570 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1572 DMWARN("message: error getting device %s",
1577 r = action_dev(m, dev, action);
1579 dm_put_device(ti, dev);
1582 mutex_unlock(&m->work_mutex);
1586 static int multipath_prepare_ioctl(struct dm_target *ti,
1587 struct block_device **bdev, fmode_t *mode)
1589 struct multipath *m = ti->private;
1590 unsigned long flags;
1593 spin_lock_irqsave(&m->lock, flags);
1595 if (!m->current_pgpath)
1596 __choose_pgpath(m, 0);
1598 if (m->current_pgpath) {
1599 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
1600 *bdev = m->current_pgpath->path.dev->bdev;
1601 *mode = m->current_pgpath->path.dev->mode;
1604 /* pg_init has not started or completed */
1608 /* No path is available */
1609 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1615 spin_unlock_irqrestore(&m->lock, flags);
1617 if (r == -ENOTCONN) {
1618 spin_lock_irqsave(&m->lock, flags);
1619 if (!m->current_pg) {
1620 /* Path status changed, redo selection */
1621 __choose_pgpath(m, 0);
1623 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1624 __pg_init_all_paths(m);
1625 spin_unlock_irqrestore(&m->lock, flags);
1626 dm_table_run_md_queue_async(m->ti->table);
1630 * Only pass ioctls through if the device sizes match exactly.
1632 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1637 static int multipath_iterate_devices(struct dm_target *ti,
1638 iterate_devices_callout_fn fn, void *data)
1640 struct multipath *m = ti->private;
1641 struct priority_group *pg;
1645 list_for_each_entry(pg, &m->priority_groups, list) {
1646 list_for_each_entry(p, &pg->pgpaths, list) {
1647 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1657 static int pgpath_busy(struct pgpath *pgpath)
1659 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1661 return blk_lld_busy(q);
1665 * We return "busy", only when we can map I/Os but underlying devices
1666 * are busy (so even if we map I/Os now, the I/Os will wait on
1667 * the underlying queue).
1668 * In other words, if we want to kill I/Os or queue them inside us
1669 * due to map unavailability, we don't return "busy". Otherwise,
1670 * dm core won't give us the I/Os and we can't do what we want.
1672 static int multipath_busy(struct dm_target *ti)
1674 bool busy = false, has_active = false;
1675 struct multipath *m = ti->private;
1676 struct priority_group *pg;
1677 struct pgpath *pgpath;
1678 unsigned long flags;
1680 spin_lock_irqsave(&m->lock, flags);
1682 /* pg_init in progress or no paths available */
1683 if (atomic_read(&m->pg_init_in_progress) ||
1684 (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
1688 /* Guess which priority_group will be used at next mapping time */
1689 if (unlikely(!m->current_pgpath && m->next_pg))
1691 else if (likely(m->current_pg))
1695 * We don't know which pg will be used at next mapping time.
1696 * We don't call __choose_pgpath() here to avoid to trigger
1697 * pg_init just by busy checking.
1698 * So we don't know whether underlying devices we will be using
1699 * at next mapping time are busy or not. Just try mapping.
1704 * If there is one non-busy active path at least, the path selector
1705 * will be able to select it. So we consider such a pg as not busy.
1708 list_for_each_entry(pgpath, &pg->pgpaths, list)
1709 if (pgpath->is_active) {
1711 if (!pgpath_busy(pgpath)) {
1719 * No active path in this pg, so this pg won't be used and
1720 * the current_pg will be changed at next mapping time.
1721 * We need to try mapping to determine it.
1726 spin_unlock_irqrestore(&m->lock, flags);
1731 /*-----------------------------------------------------------------
1733 *---------------------------------------------------------------*/
1734 static struct target_type multipath_target = {
1735 .name = "multipath",
1736 .version = {1, 11, 0},
1737 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
1738 .module = THIS_MODULE,
1739 .ctr = multipath_ctr,
1740 .dtr = multipath_dtr,
1741 .map_rq = multipath_map,
1742 .clone_and_map_rq = multipath_clone_and_map,
1743 .release_clone_rq = multipath_release_clone,
1744 .rq_end_io = multipath_end_io,
1745 .presuspend = multipath_presuspend,
1746 .postsuspend = multipath_postsuspend,
1747 .resume = multipath_resume,
1748 .status = multipath_status,
1749 .message = multipath_message,
1750 .prepare_ioctl = multipath_prepare_ioctl,
1751 .iterate_devices = multipath_iterate_devices,
1752 .busy = multipath_busy,
1755 static int __init dm_multipath_init(void)
1759 /* allocate a slab for the dm_ios */
1760 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1764 r = dm_register_target(&multipath_target);
1766 DMERR("register failed %d", r);
1768 goto bad_register_target;
1771 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1773 DMERR("failed to create workqueue kmpathd");
1775 goto bad_alloc_kmultipathd;
1779 * A separate workqueue is used to handle the device handlers
1780 * to avoid overloading existing workqueue. Overloading the
1781 * old workqueue would also create a bottleneck in the
1782 * path of the storage hardware device activation.
1784 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1786 if (!kmpath_handlerd) {
1787 DMERR("failed to create workqueue kmpath_handlerd");
1789 goto bad_alloc_kmpath_handlerd;
1792 DMINFO("version %u.%u.%u loaded",
1793 multipath_target.version[0], multipath_target.version[1],
1794 multipath_target.version[2]);
1798 bad_alloc_kmpath_handlerd:
1799 destroy_workqueue(kmultipathd);
1800 bad_alloc_kmultipathd:
1801 dm_unregister_target(&multipath_target);
1802 bad_register_target:
1803 kmem_cache_destroy(_mpio_cache);
1808 static void __exit dm_multipath_exit(void)
1810 destroy_workqueue(kmpath_handlerd);
1811 destroy_workqueue(kmultipathd);
1813 dm_unregister_target(&multipath_target);
1814 kmem_cache_destroy(_mpio_cache);
1817 module_init(dm_multipath_init);
1818 module_exit(dm_multipath_exit);
1820 MODULE_DESCRIPTION(DM_NAME " multipath target");
1821 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1822 MODULE_LICENSE("GPL");