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>
27 #define DM_MSG_PREFIX "multipath"
28 #define DM_PG_INIT_DELAY_MSECS 2000
29 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
33 struct list_head list;
35 struct priority_group *pg; /* Owning PG */
36 unsigned is_active; /* Path status */
37 unsigned fail_count; /* Cumulative failure count */
40 struct delayed_work activate_path;
43 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
46 * Paths are grouped into Priority Groups and numbered from 1 upwards.
47 * Each has a path selector which controls which path gets used.
49 struct priority_group {
50 struct list_head list;
52 struct multipath *m; /* Owning multipath instance */
53 struct path_selector ps;
55 unsigned pg_num; /* Reference number */
56 unsigned bypassed; /* Temporarily bypass this PG? */
58 unsigned nr_pgpaths; /* Number of paths in PG */
59 struct list_head pgpaths;
62 /* Multipath context */
64 struct list_head list;
67 const char *hw_handler_name;
68 char *hw_handler_params;
72 unsigned nr_priority_groups;
73 struct list_head priority_groups;
75 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
77 unsigned pg_init_required; /* pg_init needs calling? */
78 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
79 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
81 unsigned nr_valid_paths; /* Total number of usable paths */
82 struct pgpath *current_pgpath;
83 struct priority_group *current_pg;
84 struct priority_group *next_pg; /* Switch to this PG if set */
85 unsigned repeat_count; /* I/Os left before calling PS again */
87 unsigned queue_io:1; /* Must we queue all I/O? */
88 unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
89 unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
90 unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
91 unsigned pg_init_disabled:1; /* pg_init is not currently allowed */
93 unsigned pg_init_retries; /* Number of times to retry pg_init */
94 unsigned pg_init_count; /* Number of times pg_init called */
95 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
97 struct work_struct trigger_event;
100 * We must use a mempool of dm_mpath_io structs so that we
101 * can resubmit bios on error.
103 mempool_t *mpio_pool;
105 struct mutex work_mutex;
109 * Context information attached to each bio we process.
112 struct pgpath *pgpath;
116 typedef int (*action_fn) (struct pgpath *pgpath);
118 static struct kmem_cache *_mpio_cache;
120 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
121 static void trigger_event(struct work_struct *work);
122 static void activate_path(struct work_struct *work);
123 static int __pgpath_busy(struct pgpath *pgpath);
126 /*-----------------------------------------------
127 * Allocation routines
128 *-----------------------------------------------*/
130 static struct pgpath *alloc_pgpath(void)
132 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
135 pgpath->is_active = 1;
136 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
142 static void free_pgpath(struct pgpath *pgpath)
147 static struct priority_group *alloc_priority_group(void)
149 struct priority_group *pg;
151 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
154 INIT_LIST_HEAD(&pg->pgpaths);
159 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
161 struct pgpath *pgpath, *tmp;
163 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
164 list_del(&pgpath->list);
165 dm_put_device(ti, pgpath->path.dev);
170 static void free_priority_group(struct priority_group *pg,
171 struct dm_target *ti)
173 struct path_selector *ps = &pg->ps;
176 ps->type->destroy(ps);
177 dm_put_path_selector(ps->type);
180 free_pgpaths(&pg->pgpaths, ti);
184 static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq)
188 m = kzalloc(sizeof(*m), GFP_KERNEL);
190 INIT_LIST_HEAD(&m->priority_groups);
191 spin_lock_init(&m->lock);
193 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
194 INIT_WORK(&m->trigger_event, trigger_event);
195 init_waitqueue_head(&m->pg_init_wait);
196 mutex_init(&m->work_mutex);
200 unsigned min_ios = dm_get_reserved_rq_based_ios();
202 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
216 static void free_multipath(struct multipath *m)
218 struct priority_group *pg, *tmp;
220 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
222 free_priority_group(pg, m->ti);
225 kfree(m->hw_handler_name);
226 kfree(m->hw_handler_params);
227 mempool_destroy(m->mpio_pool);
231 static int set_mapinfo(struct multipath *m, union map_info *info)
233 struct dm_mpath_io *mpio;
236 /* Use blk-mq pdu memory requested via per_io_data_size */
238 memset(mpio, 0, sizeof(*mpio));
242 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
246 memset(mpio, 0, sizeof(*mpio));
252 static void clear_mapinfo(struct multipath *m, union map_info *info)
254 /* Only needed for non blk-mq */
256 struct dm_mpath_io *mpio = info->ptr;
259 mempool_free(mpio, m->mpio_pool);
263 /*-----------------------------------------------
265 *-----------------------------------------------*/
267 static int __pg_init_all_paths(struct multipath *m)
269 struct pgpath *pgpath;
270 unsigned long pg_init_delay = 0;
272 if (m->pg_init_in_progress || m->pg_init_disabled)
276 m->pg_init_required = 0;
278 /* Check here to reset pg_init_required */
282 if (m->pg_init_delay_retry)
283 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
284 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
285 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
286 /* Skip failed paths */
287 if (!pgpath->is_active)
289 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
291 m->pg_init_in_progress++;
293 return m->pg_init_in_progress;
296 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
298 m->current_pg = pgpath->pg;
300 /* Must we initialise the PG first, and queue I/O till it's ready? */
301 if (m->hw_handler_name) {
302 m->pg_init_required = 1;
305 m->pg_init_required = 0;
309 m->pg_init_count = 0;
312 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
315 struct dm_path *path;
317 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
321 m->current_pgpath = path_to_pgpath(path);
323 if (m->current_pg != pg)
324 __switch_pg(m, m->current_pgpath);
329 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
331 struct priority_group *pg;
332 unsigned bypassed = 1;
334 if (!m->nr_valid_paths) {
339 /* Were we instructed to switch PG? */
343 if (!__choose_path_in_pg(m, pg, nr_bytes))
347 /* Don't change PG until it has no remaining paths */
348 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
352 * Loop through priority groups until we find a valid path.
353 * First time we skip PGs marked 'bypassed'.
354 * Second time we only try the ones we skipped, but set
355 * pg_init_delay_retry so we do not hammer controllers.
358 list_for_each_entry(pg, &m->priority_groups, list) {
359 if (pg->bypassed == bypassed)
361 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
363 m->pg_init_delay_retry = 1;
367 } while (bypassed--);
370 m->current_pgpath = NULL;
371 m->current_pg = NULL;
375 * Check whether bios must be queued in the device-mapper core rather
376 * than here in the target.
378 * m->lock must be held on entry.
380 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
381 * same value then we are not between multipath_presuspend()
382 * and multipath_resume() calls and we have no need to check
383 * for the DMF_NOFLUSH_SUSPENDING flag.
385 static int __must_push_back(struct multipath *m)
387 return (m->queue_if_no_path ||
388 (m->queue_if_no_path != m->saved_queue_if_no_path &&
389 dm_noflush_suspending(m->ti)));
393 * Map cloned requests
395 static int __multipath_map(struct dm_target *ti, struct request *clone,
396 union map_info *map_context,
397 struct request *rq, struct request **__clone)
399 struct multipath *m = (struct multipath *) ti->private;
400 int r = DM_MAPIO_REQUEUE;
401 size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
402 struct pgpath *pgpath;
403 struct block_device *bdev;
404 struct dm_mpath_io *mpio;
406 spin_lock_irq(&m->lock);
408 /* Do we need to select a new pgpath? */
409 if (!m->current_pgpath ||
410 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
411 __choose_pgpath(m, nr_bytes);
413 pgpath = m->current_pgpath;
416 if (!__must_push_back(m))
417 r = -EIO; /* Failed */
419 } else if (m->queue_io || m->pg_init_required) {
420 __pg_init_all_paths(m);
424 if (set_mapinfo(m, map_context) < 0)
425 /* ENOMEM, requeue */
428 mpio = map_context->ptr;
429 mpio->pgpath = pgpath;
430 mpio->nr_bytes = nr_bytes;
432 bdev = pgpath->path.dev->bdev;
434 spin_unlock_irq(&m->lock);
438 * Old request-based interface: allocated clone is passed in.
439 * Used by: .request_fn stacked on .request_fn path(s).
441 clone->q = bdev_get_queue(bdev);
442 clone->rq_disk = bdev->bd_disk;
443 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
446 * blk-mq request-based interface; used by both:
447 * .request_fn stacked on blk-mq path(s) and
448 * blk-mq stacked on blk-mq path(s).
450 *__clone = blk_get_request(bdev_get_queue(bdev),
451 rq_data_dir(rq), GFP_ATOMIC);
452 if (IS_ERR(*__clone)) {
453 /* ENOMEM, requeue */
454 clear_mapinfo(m, map_context);
457 (*__clone)->bio = (*__clone)->biotail = NULL;
458 (*__clone)->rq_disk = bdev->bd_disk;
459 (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
462 if (pgpath->pg->ps.type->start_io)
463 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
466 return DM_MAPIO_REMAPPED;
469 spin_unlock_irq(&m->lock);
474 static int multipath_map(struct dm_target *ti, struct request *clone,
475 union map_info *map_context)
477 return __multipath_map(ti, clone, map_context, NULL, NULL);
480 static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
481 union map_info *map_context,
482 struct request **clone)
484 return __multipath_map(ti, NULL, map_context, rq, clone);
487 static void multipath_release_clone(struct request *clone)
489 blk_put_request(clone);
493 * If we run out of usable paths, should we queue I/O or error it?
495 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
496 unsigned save_old_value)
500 spin_lock_irqsave(&m->lock, flags);
503 m->saved_queue_if_no_path = m->queue_if_no_path;
505 m->saved_queue_if_no_path = queue_if_no_path;
506 m->queue_if_no_path = queue_if_no_path;
507 spin_unlock_irqrestore(&m->lock, flags);
509 if (!queue_if_no_path)
510 dm_table_run_md_queue_async(m->ti->table);
516 * An event is triggered whenever a path is taken out of use.
517 * Includes path failure and PG bypass.
519 static void trigger_event(struct work_struct *work)
521 struct multipath *m =
522 container_of(work, struct multipath, trigger_event);
524 dm_table_event(m->ti->table);
527 /*-----------------------------------------------------------------
528 * Constructor/argument parsing:
529 * <#multipath feature args> [<arg>]*
530 * <#hw_handler args> [hw_handler [<arg>]*]
532 * <initial priority group>
533 * [<selector> <#selector args> [<arg>]*
534 * <#paths> <#per-path selector args>
535 * [<path> [<arg>]* ]+ ]+
536 *---------------------------------------------------------------*/
537 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
538 struct dm_target *ti)
541 struct path_selector_type *pst;
544 static struct dm_arg _args[] = {
545 {0, 1024, "invalid number of path selector args"},
548 pst = dm_get_path_selector(dm_shift_arg(as));
550 ti->error = "unknown path selector type";
554 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
556 dm_put_path_selector(pst);
560 r = pst->create(&pg->ps, ps_argc, as->argv);
562 dm_put_path_selector(pst);
563 ti->error = "path selector constructor failed";
568 dm_consume_args(as, ps_argc);
573 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
574 struct dm_target *ti)
578 struct multipath *m = ti->private;
579 struct request_queue *q = NULL;
580 const char *attached_handler_name;
582 /* we need at least a path arg */
584 ti->error = "no device given";
585 return ERR_PTR(-EINVAL);
590 return ERR_PTR(-ENOMEM);
592 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
595 ti->error = "error getting device";
599 if (m->retain_attached_hw_handler || m->hw_handler_name)
600 q = bdev_get_queue(p->path.dev->bdev);
602 if (m->retain_attached_hw_handler) {
604 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
605 if (attached_handler_name) {
607 * Reset hw_handler_name to match the attached handler
608 * and clear any hw_handler_params associated with the
611 * NB. This modifies the table line to show the actual
612 * handler instead of the original table passed in.
614 kfree(m->hw_handler_name);
615 m->hw_handler_name = attached_handler_name;
617 kfree(m->hw_handler_params);
618 m->hw_handler_params = NULL;
622 if (m->hw_handler_name) {
623 r = scsi_dh_attach(q, m->hw_handler_name);
625 char b[BDEVNAME_SIZE];
627 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
628 bdevname(p->path.dev->bdev, b));
632 ti->error = "error attaching hardware handler";
633 dm_put_device(ti, p->path.dev);
637 if (m->hw_handler_params) {
638 r = scsi_dh_set_params(q, m->hw_handler_params);
640 ti->error = "unable to set hardware "
641 "handler parameters";
642 dm_put_device(ti, p->path.dev);
648 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
650 dm_put_device(ti, p->path.dev);
661 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
664 static struct dm_arg _args[] = {
665 {1, 1024, "invalid number of paths"},
666 {0, 1024, "invalid number of selector args"}
670 unsigned i, nr_selector_args, nr_args;
671 struct priority_group *pg;
672 struct dm_target *ti = m->ti;
676 ti->error = "not enough priority group arguments";
677 return ERR_PTR(-EINVAL);
680 pg = alloc_priority_group();
682 ti->error = "couldn't allocate priority group";
683 return ERR_PTR(-ENOMEM);
687 r = parse_path_selector(as, pg, ti);
694 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
698 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
702 nr_args = 1 + nr_selector_args;
703 for (i = 0; i < pg->nr_pgpaths; i++) {
704 struct pgpath *pgpath;
705 struct dm_arg_set path_args;
707 if (as->argc < nr_args) {
708 ti->error = "not enough path parameters";
713 path_args.argc = nr_args;
714 path_args.argv = as->argv;
716 pgpath = parse_path(&path_args, &pg->ps, ti);
717 if (IS_ERR(pgpath)) {
723 list_add_tail(&pgpath->list, &pg->pgpaths);
724 dm_consume_args(as, nr_args);
730 free_priority_group(pg, ti);
734 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
738 struct dm_target *ti = m->ti;
740 static struct dm_arg _args[] = {
741 {0, 1024, "invalid number of hardware handler args"},
744 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
750 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
756 for (i = 0; i <= hw_argc - 2; i++)
757 len += strlen(as->argv[i]) + 1;
758 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
760 ti->error = "memory allocation failed";
764 j = sprintf(p, "%d", hw_argc - 1);
765 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
766 j = sprintf(p, "%s", as->argv[i]);
768 dm_consume_args(as, hw_argc - 1);
772 kfree(m->hw_handler_name);
773 m->hw_handler_name = NULL;
777 static int parse_features(struct dm_arg_set *as, struct multipath *m)
781 struct dm_target *ti = m->ti;
782 const char *arg_name;
784 static struct dm_arg _args[] = {
785 {0, 6, "invalid number of feature args"},
786 {1, 50, "pg_init_retries must be between 1 and 50"},
787 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
790 r = dm_read_arg_group(_args, as, &argc, &ti->error);
798 arg_name = dm_shift_arg(as);
801 if (!strcasecmp(arg_name, "queue_if_no_path")) {
802 r = queue_if_no_path(m, 1, 0);
806 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
807 m->retain_attached_hw_handler = 1;
811 if (!strcasecmp(arg_name, "pg_init_retries") &&
813 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
818 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
820 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
825 ti->error = "Unrecognised multipath feature request";
827 } while (argc && !r);
832 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
835 /* target arguments */
836 static struct dm_arg _args[] = {
837 {0, 1024, "invalid number of priority groups"},
838 {0, 1024, "invalid initial priority group number"},
843 struct dm_arg_set as;
844 unsigned pg_count = 0;
845 unsigned next_pg_num;
846 bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table));
851 m = alloc_multipath(ti, use_blk_mq);
853 ti->error = "can't allocate multipath";
857 r = parse_features(&as, m);
861 r = parse_hw_handler(&as, m);
865 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
869 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
873 if ((!m->nr_priority_groups && next_pg_num) ||
874 (m->nr_priority_groups && !next_pg_num)) {
875 ti->error = "invalid initial priority group";
880 /* parse the priority groups */
882 struct priority_group *pg;
884 pg = parse_priority_group(&as, m);
890 m->nr_valid_paths += pg->nr_pgpaths;
891 list_add_tail(&pg->list, &m->priority_groups);
893 pg->pg_num = pg_count;
898 if (pg_count != m->nr_priority_groups) {
899 ti->error = "priority group count mismatch";
904 ti->num_flush_bios = 1;
905 ti->num_discard_bios = 1;
906 ti->num_write_same_bios = 1;
908 ti->per_io_data_size = sizeof(struct dm_mpath_io);
917 static void multipath_wait_for_pg_init_completion(struct multipath *m)
919 DECLARE_WAITQUEUE(wait, current);
922 add_wait_queue(&m->pg_init_wait, &wait);
925 set_current_state(TASK_UNINTERRUPTIBLE);
927 spin_lock_irqsave(&m->lock, flags);
928 if (!m->pg_init_in_progress) {
929 spin_unlock_irqrestore(&m->lock, flags);
932 spin_unlock_irqrestore(&m->lock, flags);
936 set_current_state(TASK_RUNNING);
938 remove_wait_queue(&m->pg_init_wait, &wait);
941 static void flush_multipath_work(struct multipath *m)
945 spin_lock_irqsave(&m->lock, flags);
946 m->pg_init_disabled = 1;
947 spin_unlock_irqrestore(&m->lock, flags);
949 flush_workqueue(kmpath_handlerd);
950 multipath_wait_for_pg_init_completion(m);
951 flush_workqueue(kmultipathd);
952 flush_work(&m->trigger_event);
954 spin_lock_irqsave(&m->lock, flags);
955 m->pg_init_disabled = 0;
956 spin_unlock_irqrestore(&m->lock, flags);
959 static void multipath_dtr(struct dm_target *ti)
961 struct multipath *m = ti->private;
963 flush_multipath_work(m);
968 * Take a path out of use.
970 static int fail_path(struct pgpath *pgpath)
973 struct multipath *m = pgpath->pg->m;
975 spin_lock_irqsave(&m->lock, flags);
977 if (!pgpath->is_active)
980 DMWARN("Failing path %s.", pgpath->path.dev->name);
982 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
983 pgpath->is_active = 0;
984 pgpath->fail_count++;
988 if (pgpath == m->current_pgpath)
989 m->current_pgpath = NULL;
991 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
992 pgpath->path.dev->name, m->nr_valid_paths);
994 schedule_work(&m->trigger_event);
997 spin_unlock_irqrestore(&m->lock, flags);
1003 * Reinstate a previously-failed path
1005 static int reinstate_path(struct pgpath *pgpath)
1007 int r = 0, run_queue = 0;
1008 unsigned long flags;
1009 struct multipath *m = pgpath->pg->m;
1011 spin_lock_irqsave(&m->lock, flags);
1013 if (pgpath->is_active)
1016 if (!pgpath->pg->ps.type->reinstate_path) {
1017 DMWARN("Reinstate path not supported by path selector %s",
1018 pgpath->pg->ps.type->name);
1023 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1027 pgpath->is_active = 1;
1029 if (!m->nr_valid_paths++) {
1030 m->current_pgpath = NULL;
1032 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1033 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1034 m->pg_init_in_progress++;
1037 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1038 pgpath->path.dev->name, m->nr_valid_paths);
1040 schedule_work(&m->trigger_event);
1043 spin_unlock_irqrestore(&m->lock, flags);
1045 dm_table_run_md_queue_async(m->ti->table);
1051 * Fail or reinstate all paths that match the provided struct dm_dev.
1053 static int action_dev(struct multipath *m, struct dm_dev *dev,
1057 struct pgpath *pgpath;
1058 struct priority_group *pg;
1060 list_for_each_entry(pg, &m->priority_groups, list) {
1061 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1062 if (pgpath->path.dev == dev)
1071 * Temporarily try to avoid having to use the specified PG
1073 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1076 unsigned long flags;
1078 spin_lock_irqsave(&m->lock, flags);
1080 pg->bypassed = bypassed;
1081 m->current_pgpath = NULL;
1082 m->current_pg = NULL;
1084 spin_unlock_irqrestore(&m->lock, flags);
1086 schedule_work(&m->trigger_event);
1090 * Switch to using the specified PG from the next I/O that gets mapped
1092 static int switch_pg_num(struct multipath *m, const char *pgstr)
1094 struct priority_group *pg;
1096 unsigned long flags;
1099 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1100 (pgnum > m->nr_priority_groups)) {
1101 DMWARN("invalid PG number supplied to switch_pg_num");
1105 spin_lock_irqsave(&m->lock, flags);
1106 list_for_each_entry(pg, &m->priority_groups, list) {
1111 m->current_pgpath = NULL;
1112 m->current_pg = NULL;
1115 spin_unlock_irqrestore(&m->lock, flags);
1117 schedule_work(&m->trigger_event);
1122 * Set/clear bypassed status of a PG.
1123 * PGs are numbered upwards from 1 in the order they were declared.
1125 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1127 struct priority_group *pg;
1131 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1132 (pgnum > m->nr_priority_groups)) {
1133 DMWARN("invalid PG number supplied to bypass_pg");
1137 list_for_each_entry(pg, &m->priority_groups, list) {
1142 bypass_pg(m, pg, bypassed);
1147 * Should we retry pg_init immediately?
1149 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1151 unsigned long flags;
1152 int limit_reached = 0;
1154 spin_lock_irqsave(&m->lock, flags);
1156 if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
1157 m->pg_init_required = 1;
1161 spin_unlock_irqrestore(&m->lock, flags);
1163 return limit_reached;
1166 static void pg_init_done(void *data, int errors)
1168 struct pgpath *pgpath = data;
1169 struct priority_group *pg = pgpath->pg;
1170 struct multipath *m = pg->m;
1171 unsigned long flags;
1172 unsigned delay_retry = 0;
1174 /* device or driver problems */
1179 if (!m->hw_handler_name) {
1183 DMERR("Could not failover the device: Handler scsi_dh_%s "
1184 "Error %d.", m->hw_handler_name, errors);
1186 * Fail path for now, so we do not ping pong
1190 case SCSI_DH_DEV_TEMP_BUSY:
1192 * Probably doing something like FW upgrade on the
1193 * controller so try the other pg.
1195 bypass_pg(m, pg, 1);
1198 /* Wait before retrying. */
1200 case SCSI_DH_IMM_RETRY:
1201 case SCSI_DH_RES_TEMP_UNAVAIL:
1202 if (pg_init_limit_reached(m, pgpath))
1208 * We probably do not want to fail the path for a device
1209 * error, but this is what the old dm did. In future
1210 * patches we can do more advanced handling.
1215 spin_lock_irqsave(&m->lock, flags);
1217 if (pgpath == m->current_pgpath) {
1218 DMERR("Could not failover device. Error %d.", errors);
1219 m->current_pgpath = NULL;
1220 m->current_pg = NULL;
1222 } else if (!m->pg_init_required)
1225 if (--m->pg_init_in_progress)
1226 /* Activations of other paths are still on going */
1229 if (m->pg_init_required) {
1230 m->pg_init_delay_retry = delay_retry;
1231 if (__pg_init_all_paths(m))
1237 * Wake up any thread waiting to suspend.
1239 wake_up(&m->pg_init_wait);
1242 spin_unlock_irqrestore(&m->lock, flags);
1245 static void activate_path(struct work_struct *work)
1247 struct pgpath *pgpath =
1248 container_of(work, struct pgpath, activate_path.work);
1250 if (pgpath->is_active)
1251 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1252 pg_init_done, pgpath);
1254 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1257 static int noretry_error(int error)
1268 /* Anything else could be a path failure, so should be retried */
1275 static int do_end_io(struct multipath *m, struct request *clone,
1276 int error, struct dm_mpath_io *mpio)
1279 * We don't queue any clone request inside the multipath target
1280 * during end I/O handling, since those clone requests don't have
1281 * bio clones. If we queue them inside the multipath target,
1282 * we need to make bio clones, that requires memory allocation.
1283 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1284 * don't have bio clones.)
1285 * Instead of queueing the clone request here, we queue the original
1286 * request into dm core, which will remake a clone request and
1287 * clone bios for it and resubmit it later.
1289 int r = DM_ENDIO_REQUEUE;
1290 unsigned long flags;
1292 if (!error && !clone->errors)
1293 return 0; /* I/O complete */
1295 if (noretry_error(error))
1299 fail_path(mpio->pgpath);
1301 spin_lock_irqsave(&m->lock, flags);
1302 if (!m->nr_valid_paths) {
1303 if (!m->queue_if_no_path) {
1304 if (!__must_push_back(m))
1307 if (error == -EBADE)
1311 spin_unlock_irqrestore(&m->lock, flags);
1316 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1317 int error, union map_info *map_context)
1319 struct multipath *m = ti->private;
1320 struct dm_mpath_io *mpio = map_context->ptr;
1321 struct pgpath *pgpath;
1322 struct path_selector *ps;
1327 r = do_end_io(m, clone, error, mpio);
1328 pgpath = mpio->pgpath;
1330 ps = &pgpath->pg->ps;
1331 if (ps->type->end_io)
1332 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1334 clear_mapinfo(m, map_context);
1340 * Suspend can't complete until all the I/O is processed so if
1341 * the last path fails we must error any remaining I/O.
1342 * Note that if the freeze_bdev fails while suspending, the
1343 * queue_if_no_path state is lost - userspace should reset it.
1345 static void multipath_presuspend(struct dm_target *ti)
1347 struct multipath *m = (struct multipath *) ti->private;
1349 queue_if_no_path(m, 0, 1);
1352 static void multipath_postsuspend(struct dm_target *ti)
1354 struct multipath *m = ti->private;
1356 mutex_lock(&m->work_mutex);
1357 flush_multipath_work(m);
1358 mutex_unlock(&m->work_mutex);
1362 * Restore the queue_if_no_path setting.
1364 static void multipath_resume(struct dm_target *ti)
1366 struct multipath *m = (struct multipath *) ti->private;
1367 unsigned long flags;
1369 spin_lock_irqsave(&m->lock, flags);
1370 m->queue_if_no_path = m->saved_queue_if_no_path;
1371 spin_unlock_irqrestore(&m->lock, flags);
1375 * Info output has the following format:
1376 * num_multipath_feature_args [multipath_feature_args]*
1377 * num_handler_status_args [handler_status_args]*
1378 * num_groups init_group_number
1379 * [A|D|E num_ps_status_args [ps_status_args]*
1380 * num_paths num_selector_args
1381 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1383 * Table output has the following format (identical to the constructor string):
1384 * num_feature_args [features_args]*
1385 * num_handler_args hw_handler [hw_handler_args]*
1386 * num_groups init_group_number
1387 * [priority selector-name num_ps_args [ps_args]*
1388 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1390 static void multipath_status(struct dm_target *ti, status_type_t type,
1391 unsigned status_flags, char *result, unsigned maxlen)
1394 unsigned long flags;
1395 struct multipath *m = (struct multipath *) ti->private;
1396 struct priority_group *pg;
1401 spin_lock_irqsave(&m->lock, flags);
1404 if (type == STATUSTYPE_INFO)
1405 DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
1407 DMEMIT("%u ", m->queue_if_no_path +
1408 (m->pg_init_retries > 0) * 2 +
1409 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1410 m->retain_attached_hw_handler);
1411 if (m->queue_if_no_path)
1412 DMEMIT("queue_if_no_path ");
1413 if (m->pg_init_retries)
1414 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1415 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1416 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1417 if (m->retain_attached_hw_handler)
1418 DMEMIT("retain_attached_hw_handler ");
1421 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1424 DMEMIT("1 %s ", m->hw_handler_name);
1426 DMEMIT("%u ", m->nr_priority_groups);
1429 pg_num = m->next_pg->pg_num;
1430 else if (m->current_pg)
1431 pg_num = m->current_pg->pg_num;
1433 pg_num = (m->nr_priority_groups ? 1 : 0);
1435 DMEMIT("%u ", pg_num);
1438 case STATUSTYPE_INFO:
1439 list_for_each_entry(pg, &m->priority_groups, list) {
1441 state = 'D'; /* Disabled */
1442 else if (pg == m->current_pg)
1443 state = 'A'; /* Currently Active */
1445 state = 'E'; /* Enabled */
1447 DMEMIT("%c ", state);
1449 if (pg->ps.type->status)
1450 sz += pg->ps.type->status(&pg->ps, NULL, type,
1456 DMEMIT("%u %u ", pg->nr_pgpaths,
1457 pg->ps.type->info_args);
1459 list_for_each_entry(p, &pg->pgpaths, list) {
1460 DMEMIT("%s %s %u ", p->path.dev->name,
1461 p->is_active ? "A" : "F",
1463 if (pg->ps.type->status)
1464 sz += pg->ps.type->status(&pg->ps,
1465 &p->path, type, result + sz,
1471 case STATUSTYPE_TABLE:
1472 list_for_each_entry(pg, &m->priority_groups, list) {
1473 DMEMIT("%s ", pg->ps.type->name);
1475 if (pg->ps.type->status)
1476 sz += pg->ps.type->status(&pg->ps, NULL, type,
1482 DMEMIT("%u %u ", pg->nr_pgpaths,
1483 pg->ps.type->table_args);
1485 list_for_each_entry(p, &pg->pgpaths, list) {
1486 DMEMIT("%s ", p->path.dev->name);
1487 if (pg->ps.type->status)
1488 sz += pg->ps.type->status(&pg->ps,
1489 &p->path, type, result + sz,
1496 spin_unlock_irqrestore(&m->lock, flags);
1499 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1503 struct multipath *m = (struct multipath *) ti->private;
1506 mutex_lock(&m->work_mutex);
1508 if (dm_suspended(ti)) {
1514 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1515 r = queue_if_no_path(m, 1, 0);
1517 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1518 r = queue_if_no_path(m, 0, 0);
1524 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
1528 if (!strcasecmp(argv[0], "disable_group")) {
1529 r = bypass_pg_num(m, argv[1], 1);
1531 } else if (!strcasecmp(argv[0], "enable_group")) {
1532 r = bypass_pg_num(m, argv[1], 0);
1534 } else if (!strcasecmp(argv[0], "switch_group")) {
1535 r = switch_pg_num(m, argv[1]);
1537 } else if (!strcasecmp(argv[0], "reinstate_path"))
1538 action = reinstate_path;
1539 else if (!strcasecmp(argv[0], "fail_path"))
1542 DMWARN("Unrecognised multipath message received: %s", argv[0]);
1546 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1548 DMWARN("message: error getting device %s",
1553 r = action_dev(m, dev, action);
1555 dm_put_device(ti, dev);
1558 mutex_unlock(&m->work_mutex);
1562 static int multipath_prepare_ioctl(struct dm_target *ti,
1563 struct block_device **bdev, fmode_t *mode)
1565 struct multipath *m = ti->private;
1566 unsigned long flags;
1569 spin_lock_irqsave(&m->lock, flags);
1571 if (!m->current_pgpath)
1572 __choose_pgpath(m, 0);
1574 if (m->current_pgpath) {
1576 *bdev = m->current_pgpath->path.dev->bdev;
1577 *mode = m->current_pgpath->path.dev->mode;
1580 /* pg_init has not started or completed */
1584 /* No path is available */
1585 if (m->queue_if_no_path)
1591 spin_unlock_irqrestore(&m->lock, flags);
1593 if (r == -ENOTCONN) {
1594 spin_lock_irqsave(&m->lock, flags);
1595 if (!m->current_pg) {
1596 /* Path status changed, redo selection */
1597 __choose_pgpath(m, 0);
1599 if (m->pg_init_required)
1600 __pg_init_all_paths(m);
1601 spin_unlock_irqrestore(&m->lock, flags);
1602 dm_table_run_md_queue_async(m->ti->table);
1606 * Only pass ioctls through if the device sizes match exactly.
1608 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1613 static int multipath_iterate_devices(struct dm_target *ti,
1614 iterate_devices_callout_fn fn, void *data)
1616 struct multipath *m = ti->private;
1617 struct priority_group *pg;
1621 list_for_each_entry(pg, &m->priority_groups, list) {
1622 list_for_each_entry(p, &pg->pgpaths, list) {
1623 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1633 static int __pgpath_busy(struct pgpath *pgpath)
1635 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1637 return blk_lld_busy(q);
1641 * We return "busy", only when we can map I/Os but underlying devices
1642 * are busy (so even if we map I/Os now, the I/Os will wait on
1643 * the underlying queue).
1644 * In other words, if we want to kill I/Os or queue them inside us
1645 * due to map unavailability, we don't return "busy". Otherwise,
1646 * dm core won't give us the I/Os and we can't do what we want.
1648 static int multipath_busy(struct dm_target *ti)
1650 int busy = 0, has_active = 0;
1651 struct multipath *m = ti->private;
1652 struct priority_group *pg;
1653 struct pgpath *pgpath;
1654 unsigned long flags;
1656 spin_lock_irqsave(&m->lock, flags);
1658 /* pg_init in progress or no paths available */
1659 if (m->pg_init_in_progress ||
1660 (!m->nr_valid_paths && m->queue_if_no_path)) {
1664 /* Guess which priority_group will be used at next mapping time */
1665 if (unlikely(!m->current_pgpath && m->next_pg))
1667 else if (likely(m->current_pg))
1671 * We don't know which pg will be used at next mapping time.
1672 * We don't call __choose_pgpath() here to avoid to trigger
1673 * pg_init just by busy checking.
1674 * So we don't know whether underlying devices we will be using
1675 * at next mapping time are busy or not. Just try mapping.
1680 * If there is one non-busy active path at least, the path selector
1681 * will be able to select it. So we consider such a pg as not busy.
1684 list_for_each_entry(pgpath, &pg->pgpaths, list)
1685 if (pgpath->is_active) {
1688 if (!__pgpath_busy(pgpath)) {
1696 * No active path in this pg, so this pg won't be used and
1697 * the current_pg will be changed at next mapping time.
1698 * We need to try mapping to determine it.
1703 spin_unlock_irqrestore(&m->lock, flags);
1708 /*-----------------------------------------------------------------
1710 *---------------------------------------------------------------*/
1711 static struct target_type multipath_target = {
1712 .name = "multipath",
1713 .version = {1, 11, 0},
1714 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
1715 .module = THIS_MODULE,
1716 .ctr = multipath_ctr,
1717 .dtr = multipath_dtr,
1718 .map_rq = multipath_map,
1719 .clone_and_map_rq = multipath_clone_and_map,
1720 .release_clone_rq = multipath_release_clone,
1721 .rq_end_io = multipath_end_io,
1722 .presuspend = multipath_presuspend,
1723 .postsuspend = multipath_postsuspend,
1724 .resume = multipath_resume,
1725 .status = multipath_status,
1726 .message = multipath_message,
1727 .prepare_ioctl = multipath_prepare_ioctl,
1728 .iterate_devices = multipath_iterate_devices,
1729 .busy = multipath_busy,
1732 static int __init dm_multipath_init(void)
1736 /* allocate a slab for the dm_ios */
1737 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1741 r = dm_register_target(&multipath_target);
1743 DMERR("register failed %d", r);
1745 goto bad_register_target;
1748 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1750 DMERR("failed to create workqueue kmpathd");
1752 goto bad_alloc_kmultipathd;
1756 * A separate workqueue is used to handle the device handlers
1757 * to avoid overloading existing workqueue. Overloading the
1758 * old workqueue would also create a bottleneck in the
1759 * path of the storage hardware device activation.
1761 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1763 if (!kmpath_handlerd) {
1764 DMERR("failed to create workqueue kmpath_handlerd");
1766 goto bad_alloc_kmpath_handlerd;
1769 DMINFO("version %u.%u.%u loaded",
1770 multipath_target.version[0], multipath_target.version[1],
1771 multipath_target.version[2]);
1775 bad_alloc_kmpath_handlerd:
1776 destroy_workqueue(kmultipathd);
1777 bad_alloc_kmultipathd:
1778 dm_unregister_target(&multipath_target);
1779 bad_register_target:
1780 kmem_cache_destroy(_mpio_cache);
1785 static void __exit dm_multipath_exit(void)
1787 destroy_workqueue(kmpath_handlerd);
1788 destroy_workqueue(kmultipathd);
1790 dm_unregister_target(&multipath_target);
1791 kmem_cache_destroy(_mpio_cache);
1794 module_init(dm_multipath_init);
1795 module_exit(dm_multipath_exit);
1797 MODULE_DESCRIPTION(DM_NAME " multipath target");
1798 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1799 MODULE_LICENSE("GPL");