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/ctype.h>
15 #include <linux/init.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/time.h>
21 #include <linux/workqueue.h>
22 #include <linux/delay.h>
23 #include <scsi/scsi_dh.h>
24 #include <linux/atomic.h>
26 #define DM_MSG_PREFIX "multipath"
27 #define DM_PG_INIT_DELAY_MSECS 2000
28 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
32 struct list_head list;
34 struct priority_group *pg; /* Owning PG */
35 unsigned is_active; /* Path status */
36 unsigned fail_count; /* Cumulative failure count */
39 struct delayed_work activate_path;
42 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
45 * Paths are grouped into Priority Groups and numbered from 1 upwards.
46 * Each has a path selector which controls which path gets used.
48 struct priority_group {
49 struct list_head list;
51 struct multipath *m; /* Owning multipath instance */
52 struct path_selector ps;
54 unsigned pg_num; /* Reference number */
55 unsigned bypassed; /* Temporarily bypass this PG? */
57 unsigned nr_pgpaths; /* Number of paths in PG */
58 struct list_head pgpaths;
61 /* Multipath context */
63 struct list_head list;
66 const char *hw_handler_name;
67 char *hw_handler_params;
71 unsigned nr_priority_groups;
72 struct list_head priority_groups;
74 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
76 unsigned pg_init_required; /* pg_init needs calling? */
77 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
78 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
80 unsigned nr_valid_paths; /* Total number of usable paths */
81 struct pgpath *current_pgpath;
82 struct priority_group *current_pg;
83 struct priority_group *next_pg; /* Switch to this PG if set */
84 unsigned repeat_count; /* I/Os left before calling PS again */
86 unsigned queue_io:1; /* Must we queue all I/O? */
87 unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
88 unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
89 unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
90 unsigned pg_init_disabled:1; /* pg_init is not currently allowed */
92 unsigned pg_init_retries; /* Number of times to retry pg_init */
93 unsigned pg_init_count; /* Number of times pg_init called */
94 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
97 struct work_struct process_queued_ios;
98 struct list_head queued_ios;
100 struct work_struct trigger_event;
103 * We must use a mempool of dm_mpath_io structs so that we
104 * can resubmit bios on error.
106 mempool_t *mpio_pool;
108 struct mutex work_mutex;
112 * Context information attached to each bio we process.
115 struct pgpath *pgpath;
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 process_queued_ios(struct work_struct *work);
125 static void trigger_event(struct work_struct *work);
126 static void activate_path(struct work_struct *work);
129 /*-----------------------------------------------
130 * Allocation routines
131 *-----------------------------------------------*/
133 static struct pgpath *alloc_pgpath(void)
135 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
138 pgpath->is_active = 1;
139 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
145 static void free_pgpath(struct pgpath *pgpath)
150 static struct priority_group *alloc_priority_group(void)
152 struct priority_group *pg;
154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
157 INIT_LIST_HEAD(&pg->pgpaths);
162 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
164 struct pgpath *pgpath, *tmp;
165 struct multipath *m = ti->private;
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
171 dm_put_device(ti, pgpath->path.dev);
176 static void free_priority_group(struct priority_group *pg,
177 struct dm_target *ti)
179 struct path_selector *ps = &pg->ps;
182 ps->type->destroy(ps);
183 dm_put_path_selector(ps->type);
186 free_pgpaths(&pg->pgpaths, ti);
190 static struct multipath *alloc_multipath(struct dm_target *ti)
193 unsigned min_ios = dm_get_reserved_rq_based_ios();
195 m = kzalloc(sizeof(*m), GFP_KERNEL);
197 INIT_LIST_HEAD(&m->priority_groups);
198 INIT_LIST_HEAD(&m->queued_ios);
199 spin_lock_init(&m->lock);
201 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
202 INIT_WORK(&m->process_queued_ios, process_queued_ios);
203 INIT_WORK(&m->trigger_event, trigger_event);
204 init_waitqueue_head(&m->pg_init_wait);
205 mutex_init(&m->work_mutex);
206 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
218 static void free_multipath(struct multipath *m)
220 struct priority_group *pg, *tmp;
222 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
224 free_priority_group(pg, m->ti);
227 kfree(m->hw_handler_name);
228 kfree(m->hw_handler_params);
229 mempool_destroy(m->mpio_pool);
233 static int set_mapinfo(struct multipath *m, union map_info *info)
235 struct dm_mpath_io *mpio;
237 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
241 memset(mpio, 0, sizeof(*mpio));
247 static void clear_mapinfo(struct multipath *m, union map_info *info)
249 struct dm_mpath_io *mpio = info->ptr;
252 mempool_free(mpio, m->mpio_pool);
255 /*-----------------------------------------------
257 *-----------------------------------------------*/
259 static void __pg_init_all_paths(struct multipath *m)
261 struct pgpath *pgpath;
262 unsigned long pg_init_delay = 0;
265 m->pg_init_required = 0;
266 if (m->pg_init_delay_retry)
267 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
268 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
269 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
270 /* Skip failed paths */
271 if (!pgpath->is_active)
273 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
275 m->pg_init_in_progress++;
279 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
281 m->current_pg = pgpath->pg;
283 /* Must we initialise the PG first, and queue I/O till it's ready? */
284 if (m->hw_handler_name) {
285 m->pg_init_required = 1;
288 m->pg_init_required = 0;
292 m->pg_init_count = 0;
295 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
298 struct dm_path *path;
300 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
304 m->current_pgpath = path_to_pgpath(path);
306 if (m->current_pg != pg)
307 __switch_pg(m, m->current_pgpath);
312 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
314 struct priority_group *pg;
315 unsigned bypassed = 1;
317 if (!m->nr_valid_paths)
320 /* Were we instructed to switch PG? */
324 if (!__choose_path_in_pg(m, pg, nr_bytes))
328 /* Don't change PG until it has no remaining paths */
329 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
333 * Loop through priority groups until we find a valid path.
334 * First time we skip PGs marked 'bypassed'.
335 * Second time we only try the ones we skipped, but set
336 * pg_init_delay_retry so we do not hammer controllers.
339 list_for_each_entry(pg, &m->priority_groups, list) {
340 if (pg->bypassed == bypassed)
342 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
344 m->pg_init_delay_retry = 1;
348 } while (bypassed--);
351 m->current_pgpath = NULL;
352 m->current_pg = NULL;
356 * Check whether bios must be queued in the device-mapper core rather
357 * than here in the target.
359 * m->lock must be held on entry.
361 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
362 * same value then we are not between multipath_presuspend()
363 * and multipath_resume() calls and we have no need to check
364 * for the DMF_NOFLUSH_SUSPENDING flag.
366 static int __must_push_back(struct multipath *m)
368 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
369 dm_noflush_suspending(m->ti));
372 static int map_io(struct multipath *m, struct request *clone,
373 union map_info *map_context, unsigned was_queued)
375 int r = DM_MAPIO_REMAPPED;
376 size_t nr_bytes = blk_rq_bytes(clone);
378 struct pgpath *pgpath;
379 struct block_device *bdev;
380 struct dm_mpath_io *mpio = map_context->ptr;
382 spin_lock_irqsave(&m->lock, flags);
384 /* Do we need to select a new pgpath? */
385 if (!m->current_pgpath ||
386 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
387 __choose_pgpath(m, nr_bytes);
389 pgpath = m->current_pgpath;
394 if (m->pg_init_required) {
395 if (!m->pg_init_in_progress)
396 queue_work(kmultipathd, &m->process_queued_ios);
397 r = DM_MAPIO_REQUEUE;
398 } else if ((pgpath && m->queue_io) ||
399 (!pgpath && m->queue_if_no_path)) {
400 /* Queue for the daemon to resubmit */
401 list_add_tail(&clone->queuelist, &m->queued_ios);
404 queue_work(kmultipathd, &m->process_queued_ios);
406 r = DM_MAPIO_SUBMITTED;
408 bdev = pgpath->path.dev->bdev;
409 clone->q = bdev_get_queue(bdev);
410 clone->rq_disk = bdev->bd_disk;
411 } else if (__must_push_back(m))
412 r = DM_MAPIO_REQUEUE;
414 r = -EIO; /* Failed */
416 mpio->pgpath = pgpath;
417 mpio->nr_bytes = nr_bytes;
419 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
420 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
423 spin_unlock_irqrestore(&m->lock, flags);
429 * If we run out of usable paths, should we queue I/O or error it?
431 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
432 unsigned save_old_value)
436 spin_lock_irqsave(&m->lock, flags);
439 m->saved_queue_if_no_path = m->queue_if_no_path;
441 m->saved_queue_if_no_path = queue_if_no_path;
442 m->queue_if_no_path = queue_if_no_path;
443 if (!m->queue_if_no_path && m->queue_size)
444 queue_work(kmultipathd, &m->process_queued_ios);
446 spin_unlock_irqrestore(&m->lock, flags);
451 /*-----------------------------------------------------------------
452 * The multipath daemon is responsible for resubmitting queued ios.
453 *---------------------------------------------------------------*/
455 static void dispatch_queued_ios(struct multipath *m)
459 union map_info *info;
460 struct request *clone, *n;
463 spin_lock_irqsave(&m->lock, flags);
464 list_splice_init(&m->queued_ios, &cl);
465 spin_unlock_irqrestore(&m->lock, flags);
467 list_for_each_entry_safe(clone, n, &cl, queuelist) {
468 list_del_init(&clone->queuelist);
470 info = dm_get_rq_mapinfo(clone);
472 r = map_io(m, clone, info, 1);
474 clear_mapinfo(m, info);
475 dm_kill_unmapped_request(clone, r);
476 } else if (r == DM_MAPIO_REMAPPED)
477 dm_dispatch_request(clone);
478 else if (r == DM_MAPIO_REQUEUE) {
479 clear_mapinfo(m, info);
480 dm_requeue_unmapped_request(clone);
485 static void process_queued_ios(struct work_struct *work)
487 struct multipath *m =
488 container_of(work, struct multipath, process_queued_ios);
489 struct pgpath *pgpath = NULL;
490 unsigned must_queue = 1;
493 spin_lock_irqsave(&m->lock, flags);
495 if (!m->current_pgpath)
496 __choose_pgpath(m, 0);
498 pgpath = m->current_pgpath;
500 if ((pgpath && !m->queue_io) ||
501 (!pgpath && !m->queue_if_no_path))
504 if (m->pg_init_required && !m->pg_init_in_progress && pgpath &&
505 !m->pg_init_disabled)
506 __pg_init_all_paths(m);
508 spin_unlock_irqrestore(&m->lock, flags);
510 dispatch_queued_ios(m);
514 * An event is triggered whenever a path is taken out of use.
515 * Includes path failure and PG bypass.
517 static void trigger_event(struct work_struct *work)
519 struct multipath *m =
520 container_of(work, struct multipath, trigger_event);
522 dm_table_event(m->ti->table);
525 /*-----------------------------------------------------------------
526 * Constructor/argument parsing:
527 * <#multipath feature args> [<arg>]*
528 * <#hw_handler args> [hw_handler [<arg>]*]
530 * <initial priority group>
531 * [<selector> <#selector args> [<arg>]*
532 * <#paths> <#per-path selector args>
533 * [<path> [<arg>]* ]+ ]+
534 *---------------------------------------------------------------*/
535 static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
536 struct dm_target *ti)
539 struct path_selector_type *pst;
542 static struct dm_arg _args[] = {
543 {0, 1024, "invalid number of path selector args"},
546 pst = dm_get_path_selector(dm_shift_arg(as));
548 ti->error = "unknown path selector type";
552 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
554 dm_put_path_selector(pst);
558 r = pst->create(&pg->ps, ps_argc, as->argv);
560 dm_put_path_selector(pst);
561 ti->error = "path selector constructor failed";
566 dm_consume_args(as, ps_argc);
571 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
572 struct dm_target *ti)
576 struct multipath *m = ti->private;
577 struct request_queue *q = NULL;
578 const char *attached_handler_name;
580 /* we need at least a path arg */
582 ti->error = "no device given";
583 return ERR_PTR(-EINVAL);
588 return ERR_PTR(-ENOMEM);
590 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
593 ti->error = "error getting device";
597 if (m->retain_attached_hw_handler || m->hw_handler_name)
598 q = bdev_get_queue(p->path.dev->bdev);
600 if (m->retain_attached_hw_handler) {
601 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
602 if (attached_handler_name) {
604 * Reset hw_handler_name to match the attached handler
605 * and clear any hw_handler_params associated with the
608 * NB. This modifies the table line to show the actual
609 * handler instead of the original table passed in.
611 kfree(m->hw_handler_name);
612 m->hw_handler_name = attached_handler_name;
614 kfree(m->hw_handler_params);
615 m->hw_handler_params = NULL;
619 if (m->hw_handler_name) {
621 * Increments scsi_dh reference, even when using an
622 * already-attached handler.
624 r = scsi_dh_attach(q, m->hw_handler_name);
627 * Already attached to different hw_handler:
628 * try to reattach with correct one.
631 r = scsi_dh_attach(q, m->hw_handler_name);
635 ti->error = "error attaching hardware handler";
636 dm_put_device(ti, p->path.dev);
640 if (m->hw_handler_params) {
641 r = scsi_dh_set_params(q, m->hw_handler_params);
643 ti->error = "unable to set hardware "
644 "handler parameters";
646 dm_put_device(ti, p->path.dev);
652 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
654 dm_put_device(ti, p->path.dev);
665 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
668 static struct dm_arg _args[] = {
669 {1, 1024, "invalid number of paths"},
670 {0, 1024, "invalid number of selector args"}
674 unsigned i, nr_selector_args, nr_args;
675 struct priority_group *pg;
676 struct dm_target *ti = m->ti;
680 ti->error = "not enough priority group arguments";
681 return ERR_PTR(-EINVAL);
684 pg = alloc_priority_group();
686 ti->error = "couldn't allocate priority group";
687 return ERR_PTR(-ENOMEM);
691 r = parse_path_selector(as, pg, ti);
698 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
702 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
706 nr_args = 1 + nr_selector_args;
707 for (i = 0; i < pg->nr_pgpaths; i++) {
708 struct pgpath *pgpath;
709 struct dm_arg_set path_args;
711 if (as->argc < nr_args) {
712 ti->error = "not enough path parameters";
717 path_args.argc = nr_args;
718 path_args.argv = as->argv;
720 pgpath = parse_path(&path_args, &pg->ps, ti);
721 if (IS_ERR(pgpath)) {
727 list_add_tail(&pgpath->list, &pg->pgpaths);
728 dm_consume_args(as, nr_args);
734 free_priority_group(pg, ti);
738 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
742 struct dm_target *ti = m->ti;
744 static struct dm_arg _args[] = {
745 {0, 1024, "invalid number of hardware handler args"},
748 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
754 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
755 if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
756 "scsi_dh_%s", m->hw_handler_name)) {
757 ti->error = "unknown hardware handler type";
766 for (i = 0; i <= hw_argc - 2; i++)
767 len += strlen(as->argv[i]) + 1;
768 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
770 ti->error = "memory allocation failed";
774 j = sprintf(p, "%d", hw_argc - 1);
775 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
776 j = sprintf(p, "%s", as->argv[i]);
778 dm_consume_args(as, hw_argc - 1);
782 kfree(m->hw_handler_name);
783 m->hw_handler_name = NULL;
787 static int parse_features(struct dm_arg_set *as, struct multipath *m)
791 struct dm_target *ti = m->ti;
792 const char *arg_name;
794 static struct dm_arg _args[] = {
795 {0, 6, "invalid number of feature args"},
796 {1, 50, "pg_init_retries must be between 1 and 50"},
797 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
800 r = dm_read_arg_group(_args, as, &argc, &ti->error);
808 arg_name = dm_shift_arg(as);
811 if (!strcasecmp(arg_name, "queue_if_no_path")) {
812 r = queue_if_no_path(m, 1, 0);
816 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
817 m->retain_attached_hw_handler = 1;
821 if (!strcasecmp(arg_name, "pg_init_retries") &&
823 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
828 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
830 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
835 ti->error = "Unrecognised multipath feature request";
837 } while (argc && !r);
842 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
845 /* target arguments */
846 static struct dm_arg _args[] = {
847 {0, 1024, "invalid number of priority groups"},
848 {0, 1024, "invalid initial priority group number"},
853 struct dm_arg_set as;
854 unsigned pg_count = 0;
855 unsigned next_pg_num;
860 m = alloc_multipath(ti);
862 ti->error = "can't allocate multipath";
866 r = parse_features(&as, m);
870 r = parse_hw_handler(&as, m);
874 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
878 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
882 if ((!m->nr_priority_groups && next_pg_num) ||
883 (m->nr_priority_groups && !next_pg_num)) {
884 ti->error = "invalid initial priority group";
889 /* parse the priority groups */
891 struct priority_group *pg;
893 pg = parse_priority_group(&as, m);
899 m->nr_valid_paths += pg->nr_pgpaths;
900 list_add_tail(&pg->list, &m->priority_groups);
902 pg->pg_num = pg_count;
907 if (pg_count != m->nr_priority_groups) {
908 ti->error = "priority group count mismatch";
913 ti->num_flush_bios = 1;
914 ti->num_discard_bios = 1;
915 ti->num_write_same_bios = 1;
924 static void multipath_wait_for_pg_init_completion(struct multipath *m)
926 DECLARE_WAITQUEUE(wait, current);
929 add_wait_queue(&m->pg_init_wait, &wait);
932 set_current_state(TASK_UNINTERRUPTIBLE);
934 spin_lock_irqsave(&m->lock, flags);
935 if (!m->pg_init_in_progress) {
936 spin_unlock_irqrestore(&m->lock, flags);
939 spin_unlock_irqrestore(&m->lock, flags);
943 set_current_state(TASK_RUNNING);
945 remove_wait_queue(&m->pg_init_wait, &wait);
948 static void flush_multipath_work(struct multipath *m)
952 spin_lock_irqsave(&m->lock, flags);
953 m->pg_init_disabled = 1;
954 spin_unlock_irqrestore(&m->lock, flags);
956 flush_workqueue(kmpath_handlerd);
957 multipath_wait_for_pg_init_completion(m);
958 flush_workqueue(kmultipathd);
959 flush_work(&m->trigger_event);
961 spin_lock_irqsave(&m->lock, flags);
962 m->pg_init_disabled = 0;
963 spin_unlock_irqrestore(&m->lock, flags);
966 static void multipath_dtr(struct dm_target *ti)
968 struct multipath *m = ti->private;
970 flush_multipath_work(m);
975 * Map cloned requests
977 static int multipath_map(struct dm_target *ti, struct request *clone,
978 union map_info *map_context)
981 struct multipath *m = (struct multipath *) ti->private;
983 if (set_mapinfo(m, map_context) < 0)
984 /* ENOMEM, requeue */
985 return DM_MAPIO_REQUEUE;
987 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
988 r = map_io(m, clone, map_context, 0);
989 if (r < 0 || r == DM_MAPIO_REQUEUE)
990 clear_mapinfo(m, map_context);
996 * Take a path out of use.
998 static int fail_path(struct pgpath *pgpath)
1000 unsigned long flags;
1001 struct multipath *m = pgpath->pg->m;
1003 spin_lock_irqsave(&m->lock, flags);
1005 if (!pgpath->is_active)
1008 DMWARN("Failing path %s.", pgpath->path.dev->name);
1010 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1011 pgpath->is_active = 0;
1012 pgpath->fail_count++;
1014 m->nr_valid_paths--;
1016 if (pgpath == m->current_pgpath)
1017 m->current_pgpath = NULL;
1019 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1020 pgpath->path.dev->name, m->nr_valid_paths);
1022 schedule_work(&m->trigger_event);
1025 spin_unlock_irqrestore(&m->lock, flags);
1031 * Reinstate a previously-failed path
1033 static int reinstate_path(struct pgpath *pgpath)
1036 unsigned long flags;
1037 struct multipath *m = pgpath->pg->m;
1039 spin_lock_irqsave(&m->lock, flags);
1041 if (pgpath->is_active)
1044 if (!pgpath->pg->ps.type->reinstate_path) {
1045 DMWARN("Reinstate path not supported by path selector %s",
1046 pgpath->pg->ps.type->name);
1051 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1055 pgpath->is_active = 1;
1057 if (!m->nr_valid_paths++ && m->queue_size) {
1058 m->current_pgpath = NULL;
1059 queue_work(kmultipathd, &m->process_queued_ios);
1060 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1061 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1062 m->pg_init_in_progress++;
1065 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1066 pgpath->path.dev->name, m->nr_valid_paths);
1068 schedule_work(&m->trigger_event);
1071 spin_unlock_irqrestore(&m->lock, flags);
1077 * Fail or reinstate all paths that match the provided struct dm_dev.
1079 static int action_dev(struct multipath *m, struct dm_dev *dev,
1083 struct pgpath *pgpath;
1084 struct priority_group *pg;
1086 list_for_each_entry(pg, &m->priority_groups, list) {
1087 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1088 if (pgpath->path.dev == dev)
1097 * Temporarily try to avoid having to use the specified PG
1099 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1102 unsigned long flags;
1104 spin_lock_irqsave(&m->lock, flags);
1106 pg->bypassed = bypassed;
1107 m->current_pgpath = NULL;
1108 m->current_pg = NULL;
1110 spin_unlock_irqrestore(&m->lock, flags);
1112 schedule_work(&m->trigger_event);
1116 * Switch to using the specified PG from the next I/O that gets mapped
1118 static int switch_pg_num(struct multipath *m, const char *pgstr)
1120 struct priority_group *pg;
1122 unsigned long flags;
1125 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1126 (pgnum > m->nr_priority_groups)) {
1127 DMWARN("invalid PG number supplied to switch_pg_num");
1131 spin_lock_irqsave(&m->lock, flags);
1132 list_for_each_entry(pg, &m->priority_groups, list) {
1137 m->current_pgpath = NULL;
1138 m->current_pg = NULL;
1141 spin_unlock_irqrestore(&m->lock, flags);
1143 schedule_work(&m->trigger_event);
1148 * Set/clear bypassed status of a PG.
1149 * PGs are numbered upwards from 1 in the order they were declared.
1151 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1153 struct priority_group *pg;
1157 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1158 (pgnum > m->nr_priority_groups)) {
1159 DMWARN("invalid PG number supplied to bypass_pg");
1163 list_for_each_entry(pg, &m->priority_groups, list) {
1168 bypass_pg(m, pg, bypassed);
1173 * Should we retry pg_init immediately?
1175 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1177 unsigned long flags;
1178 int limit_reached = 0;
1180 spin_lock_irqsave(&m->lock, flags);
1182 if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
1183 m->pg_init_required = 1;
1187 spin_unlock_irqrestore(&m->lock, flags);
1189 return limit_reached;
1192 static void pg_init_done(void *data, int errors)
1194 struct pgpath *pgpath = data;
1195 struct priority_group *pg = pgpath->pg;
1196 struct multipath *m = pg->m;
1197 unsigned long flags;
1198 unsigned delay_retry = 0;
1200 /* device or driver problems */
1205 if (!m->hw_handler_name) {
1209 DMERR("Could not failover the device: Handler scsi_dh_%s "
1210 "Error %d.", m->hw_handler_name, errors);
1212 * Fail path for now, so we do not ping pong
1216 case SCSI_DH_DEV_TEMP_BUSY:
1218 * Probably doing something like FW upgrade on the
1219 * controller so try the other pg.
1221 bypass_pg(m, pg, 1);
1224 /* Wait before retrying. */
1226 case SCSI_DH_IMM_RETRY:
1227 case SCSI_DH_RES_TEMP_UNAVAIL:
1228 if (pg_init_limit_reached(m, pgpath))
1234 * We probably do not want to fail the path for a device
1235 * error, but this is what the old dm did. In future
1236 * patches we can do more advanced handling.
1241 spin_lock_irqsave(&m->lock, flags);
1243 if (pgpath == m->current_pgpath) {
1244 DMERR("Could not failover device. Error %d.", errors);
1245 m->current_pgpath = NULL;
1246 m->current_pg = NULL;
1248 } else if (!m->pg_init_required)
1251 if (--m->pg_init_in_progress)
1252 /* Activations of other paths are still on going */
1255 if (!m->pg_init_required)
1258 m->pg_init_delay_retry = delay_retry;
1259 queue_work(kmultipathd, &m->process_queued_ios);
1262 * Wake up any thread waiting to suspend.
1264 wake_up(&m->pg_init_wait);
1267 spin_unlock_irqrestore(&m->lock, flags);
1270 static void activate_path(struct work_struct *work)
1272 struct pgpath *pgpath =
1273 container_of(work, struct pgpath, activate_path.work);
1275 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1276 pg_init_done, pgpath);
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)) {
1318 if ((clone->cmd_flags & REQ_WRITE_SAME) &&
1319 !clone->q->limits.max_write_same_sectors) {
1320 struct queue_limits *limits;
1322 /* device doesn't really support WRITE SAME, disable it */
1323 limits = dm_get_queue_limits(dm_table_get_md(m->ti->table));
1324 limits->max_write_same_sectors = 0;
1330 fail_path(mpio->pgpath);
1332 spin_lock_irqsave(&m->lock, flags);
1333 if (!m->nr_valid_paths) {
1334 if (!m->queue_if_no_path) {
1335 if (!__must_push_back(m))
1338 if (error == -EBADE)
1342 spin_unlock_irqrestore(&m->lock, flags);
1347 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1348 int error, union map_info *map_context)
1350 struct multipath *m = ti->private;
1351 struct dm_mpath_io *mpio = map_context->ptr;
1352 struct pgpath *pgpath;
1353 struct path_selector *ps;
1358 r = do_end_io(m, clone, error, mpio);
1359 pgpath = mpio->pgpath;
1361 ps = &pgpath->pg->ps;
1362 if (ps->type->end_io)
1363 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1365 clear_mapinfo(m, map_context);
1371 * Suspend can't complete until all the I/O is processed so if
1372 * the last path fails we must error any remaining I/O.
1373 * Note that if the freeze_bdev fails while suspending, the
1374 * queue_if_no_path state is lost - userspace should reset it.
1376 static void multipath_presuspend(struct dm_target *ti)
1378 struct multipath *m = (struct multipath *) ti->private;
1380 queue_if_no_path(m, 0, 1);
1383 static void multipath_postsuspend(struct dm_target *ti)
1385 struct multipath *m = ti->private;
1387 mutex_lock(&m->work_mutex);
1388 flush_multipath_work(m);
1389 mutex_unlock(&m->work_mutex);
1393 * Restore the queue_if_no_path setting.
1395 static void multipath_resume(struct dm_target *ti)
1397 struct multipath *m = (struct multipath *) ti->private;
1398 unsigned long flags;
1400 spin_lock_irqsave(&m->lock, flags);
1401 m->queue_if_no_path = m->saved_queue_if_no_path;
1402 spin_unlock_irqrestore(&m->lock, flags);
1406 * Info output has the following format:
1407 * num_multipath_feature_args [multipath_feature_args]*
1408 * num_handler_status_args [handler_status_args]*
1409 * num_groups init_group_number
1410 * [A|D|E num_ps_status_args [ps_status_args]*
1411 * num_paths num_selector_args
1412 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1414 * Table output has the following format (identical to the constructor string):
1415 * num_feature_args [features_args]*
1416 * num_handler_args hw_handler [hw_handler_args]*
1417 * num_groups init_group_number
1418 * [priority selector-name num_ps_args [ps_args]*
1419 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1421 static void multipath_status(struct dm_target *ti, status_type_t type,
1422 unsigned status_flags, char *result, unsigned maxlen)
1425 unsigned long flags;
1426 struct multipath *m = (struct multipath *) ti->private;
1427 struct priority_group *pg;
1432 spin_lock_irqsave(&m->lock, flags);
1435 if (type == STATUSTYPE_INFO)
1436 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1438 DMEMIT("%u ", m->queue_if_no_path +
1439 (m->pg_init_retries > 0) * 2 +
1440 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1441 m->retain_attached_hw_handler);
1442 if (m->queue_if_no_path)
1443 DMEMIT("queue_if_no_path ");
1444 if (m->pg_init_retries)
1445 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1446 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1447 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1448 if (m->retain_attached_hw_handler)
1449 DMEMIT("retain_attached_hw_handler ");
1452 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1455 DMEMIT("1 %s ", m->hw_handler_name);
1457 DMEMIT("%u ", m->nr_priority_groups);
1460 pg_num = m->next_pg->pg_num;
1461 else if (m->current_pg)
1462 pg_num = m->current_pg->pg_num;
1464 pg_num = (m->nr_priority_groups ? 1 : 0);
1466 DMEMIT("%u ", pg_num);
1469 case STATUSTYPE_INFO:
1470 list_for_each_entry(pg, &m->priority_groups, list) {
1472 state = 'D'; /* Disabled */
1473 else if (pg == m->current_pg)
1474 state = 'A'; /* Currently Active */
1476 state = 'E'; /* Enabled */
1478 DMEMIT("%c ", state);
1480 if (pg->ps.type->status)
1481 sz += pg->ps.type->status(&pg->ps, NULL, type,
1487 DMEMIT("%u %u ", pg->nr_pgpaths,
1488 pg->ps.type->info_args);
1490 list_for_each_entry(p, &pg->pgpaths, list) {
1491 DMEMIT("%s %s %u ", p->path.dev->name,
1492 p->is_active ? "A" : "F",
1494 if (pg->ps.type->status)
1495 sz += pg->ps.type->status(&pg->ps,
1496 &p->path, type, result + sz,
1502 case STATUSTYPE_TABLE:
1503 list_for_each_entry(pg, &m->priority_groups, list) {
1504 DMEMIT("%s ", pg->ps.type->name);
1506 if (pg->ps.type->status)
1507 sz += pg->ps.type->status(&pg->ps, NULL, type,
1513 DMEMIT("%u %u ", pg->nr_pgpaths,
1514 pg->ps.type->table_args);
1516 list_for_each_entry(p, &pg->pgpaths, list) {
1517 DMEMIT("%s ", p->path.dev->name);
1518 if (pg->ps.type->status)
1519 sz += pg->ps.type->status(&pg->ps,
1520 &p->path, type, result + sz,
1527 spin_unlock_irqrestore(&m->lock, flags);
1530 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1534 struct multipath *m = (struct multipath *) ti->private;
1537 mutex_lock(&m->work_mutex);
1539 if (dm_suspended(ti)) {
1545 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1546 r = queue_if_no_path(m, 1, 0);
1548 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1549 r = queue_if_no_path(m, 0, 0);
1555 DMWARN("Unrecognised multipath message received.");
1559 if (!strcasecmp(argv[0], "disable_group")) {
1560 r = bypass_pg_num(m, argv[1], 1);
1562 } else if (!strcasecmp(argv[0], "enable_group")) {
1563 r = bypass_pg_num(m, argv[1], 0);
1565 } else if (!strcasecmp(argv[0], "switch_group")) {
1566 r = switch_pg_num(m, argv[1]);
1568 } else if (!strcasecmp(argv[0], "reinstate_path"))
1569 action = reinstate_path;
1570 else if (!strcasecmp(argv[0], "fail_path"))
1573 DMWARN("Unrecognised multipath message received.");
1577 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1579 DMWARN("message: error getting device %s",
1584 r = action_dev(m, dev, action);
1586 dm_put_device(ti, dev);
1589 mutex_unlock(&m->work_mutex);
1593 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1596 struct multipath *m = ti->private;
1597 struct pgpath *pgpath;
1598 struct block_device *bdev;
1600 unsigned long flags;
1607 spin_lock_irqsave(&m->lock, flags);
1609 if (!m->current_pgpath)
1610 __choose_pgpath(m, 0);
1612 pgpath = m->current_pgpath;
1615 bdev = pgpath->path.dev->bdev;
1616 mode = pgpath->path.dev->mode;
1619 if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
1624 spin_unlock_irqrestore(&m->lock, flags);
1627 * Only pass ioctls through if the device sizes match exactly.
1629 if (!bdev || ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT) {
1630 int err = scsi_verify_blk_ioctl(NULL, cmd);
1635 if (r == -ENOTCONN && !fatal_signal_pending(current))
1636 queue_work(kmultipathd, &m->process_queued_ios);
1638 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1641 static int multipath_iterate_devices(struct dm_target *ti,
1642 iterate_devices_callout_fn fn, void *data)
1644 struct multipath *m = ti->private;
1645 struct priority_group *pg;
1649 list_for_each_entry(pg, &m->priority_groups, list) {
1650 list_for_each_entry(p, &pg->pgpaths, list) {
1651 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1661 static int __pgpath_busy(struct pgpath *pgpath)
1663 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1665 return dm_underlying_device_busy(q);
1669 * We return "busy", only when we can map I/Os but underlying devices
1670 * are busy (so even if we map I/Os now, the I/Os will wait on
1671 * the underlying queue).
1672 * In other words, if we want to kill I/Os or queue them inside us
1673 * due to map unavailability, we don't return "busy". Otherwise,
1674 * dm core won't give us the I/Os and we can't do what we want.
1676 static int multipath_busy(struct dm_target *ti)
1678 int busy = 0, has_active = 0;
1679 struct multipath *m = ti->private;
1680 struct priority_group *pg;
1681 struct pgpath *pgpath;
1682 unsigned long flags;
1684 spin_lock_irqsave(&m->lock, flags);
1686 /* pg_init in progress, requeue until done */
1687 if (m->pg_init_in_progress) {
1691 /* Guess which priority_group will be used at next mapping time */
1692 if (unlikely(!m->current_pgpath && m->next_pg))
1694 else if (likely(m->current_pg))
1698 * We don't know which pg will be used at next mapping time.
1699 * We don't call __choose_pgpath() here to avoid to trigger
1700 * pg_init just by busy checking.
1701 * So we don't know whether underlying devices we will be using
1702 * at next mapping time are busy or not. Just try mapping.
1707 * If there is one non-busy active path at least, the path selector
1708 * will be able to select it. So we consider such a pg as not busy.
1711 list_for_each_entry(pgpath, &pg->pgpaths, list)
1712 if (pgpath->is_active) {
1715 if (!__pgpath_busy(pgpath)) {
1723 * No active path in this pg, so this pg won't be used and
1724 * the current_pg will be changed at next mapping time.
1725 * We need to try mapping to determine it.
1730 spin_unlock_irqrestore(&m->lock, flags);
1735 /*-----------------------------------------------------------------
1737 *---------------------------------------------------------------*/
1738 static struct target_type multipath_target = {
1739 .name = "multipath",
1740 .version = {1, 6, 0},
1741 .module = THIS_MODULE,
1742 .ctr = multipath_ctr,
1743 .dtr = multipath_dtr,
1744 .map_rq = multipath_map,
1745 .rq_end_io = multipath_end_io,
1746 .presuspend = multipath_presuspend,
1747 .postsuspend = multipath_postsuspend,
1748 .resume = multipath_resume,
1749 .status = multipath_status,
1750 .message = multipath_message,
1751 .ioctl = multipath_ioctl,
1752 .iterate_devices = multipath_iterate_devices,
1753 .busy = multipath_busy,
1756 static int __init dm_multipath_init(void)
1760 /* allocate a slab for the dm_ios */
1761 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1765 r = dm_register_target(&multipath_target);
1767 DMERR("register failed %d", r);
1768 kmem_cache_destroy(_mpio_cache);
1772 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1774 DMERR("failed to create workqueue kmpathd");
1775 dm_unregister_target(&multipath_target);
1776 kmem_cache_destroy(_mpio_cache);
1781 * A separate workqueue is used to handle the device handlers
1782 * to avoid overloading existing workqueue. Overloading the
1783 * old workqueue would also create a bottleneck in the
1784 * path of the storage hardware device activation.
1786 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1788 if (!kmpath_handlerd) {
1789 DMERR("failed to create workqueue kmpath_handlerd");
1790 destroy_workqueue(kmultipathd);
1791 dm_unregister_target(&multipath_target);
1792 kmem_cache_destroy(_mpio_cache);
1796 DMINFO("version %u.%u.%u loaded",
1797 multipath_target.version[0], multipath_target.version[1],
1798 multipath_target.version[2]);
1803 static void __exit dm_multipath_exit(void)
1805 destroy_workqueue(kmpath_handlerd);
1806 destroy_workqueue(kmultipathd);
1808 dm_unregister_target(&multipath_target);
1809 kmem_cache_destroy(_mpio_cache);
1812 module_init(dm_multipath_init);
1813 module_exit(dm_multipath_exit);
1815 MODULE_DESCRIPTION(DM_NAME " multipath target");
1816 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1817 MODULE_LICENSE("GPL");