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>
10 #include "dm-path-selector.h"
11 #include "dm-uevent.h"
13 #include <linux/ctype.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/pagemap.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/workqueue.h>
21 #include <scsi/scsi_dh.h>
22 #include <asm/atomic.h>
24 #define DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
29 struct list_head list;
31 struct priority_group *pg; /* Owning PG */
32 unsigned is_active; /* Path status */
33 unsigned fail_count; /* Cumulative failure count */
36 struct work_struct deactivate_path;
37 struct work_struct activate_path;
40 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
43 * Paths are grouped into Priority Groups and numbered from 1 upwards.
44 * Each has a path selector which controls which path gets used.
46 struct priority_group {
47 struct list_head list;
49 struct multipath *m; /* Owning multipath instance */
50 struct path_selector ps;
52 unsigned pg_num; /* Reference number */
53 unsigned bypassed; /* Temporarily bypass this PG? */
55 unsigned nr_pgpaths; /* Number of paths in PG */
56 struct list_head pgpaths;
59 /* Multipath context */
61 struct list_head list;
66 const char *hw_handler_name;
67 char *hw_handler_params;
68 unsigned nr_priority_groups;
69 struct list_head priority_groups;
70 unsigned pg_init_required; /* pg_init needs calling? */
71 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
81 unsigned saved_queue_if_no_path;/* Saved state during suspension */
82 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
85 struct work_struct process_queued_ios;
86 struct list_head queued_ios;
89 struct work_struct trigger_event;
92 * We must use a mempool of dm_mpath_io structs so that we
93 * can resubmit bios on error.
97 struct mutex work_mutex;
101 * Context information attached to each bio we process.
104 struct pgpath *pgpath;
108 typedef int (*action_fn) (struct pgpath *pgpath);
110 #define MIN_IOS 256 /* Mempool size */
112 static struct kmem_cache *_mpio_cache;
114 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
115 static void process_queued_ios(struct work_struct *work);
116 static void trigger_event(struct work_struct *work);
117 static void activate_path(struct work_struct *work);
118 static void deactivate_path(struct work_struct *work);
121 /*-----------------------------------------------
122 * Allocation routines
123 *-----------------------------------------------*/
125 static struct pgpath *alloc_pgpath(void)
127 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
130 pgpath->is_active = 1;
131 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
132 INIT_WORK(&pgpath->activate_path, activate_path);
138 static void free_pgpath(struct pgpath *pgpath)
143 static void deactivate_path(struct work_struct *work)
145 struct pgpath *pgpath =
146 container_of(work, struct pgpath, deactivate_path);
148 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
151 static struct priority_group *alloc_priority_group(void)
153 struct priority_group *pg;
155 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
158 INIT_LIST_HEAD(&pg->pgpaths);
163 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
165 struct pgpath *pgpath, *tmp;
166 struct multipath *m = ti->private;
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
170 if (m->hw_handler_name)
171 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
172 dm_put_device(ti, pgpath->path.dev);
177 static void free_priority_group(struct priority_group *pg,
178 struct dm_target *ti)
180 struct path_selector *ps = &pg->ps;
183 ps->type->destroy(ps);
184 dm_put_path_selector(ps->type);
187 free_pgpaths(&pg->pgpaths, ti);
191 static struct multipath *alloc_multipath(struct dm_target *ti)
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 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
203 mutex_init(&m->work_mutex);
204 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);
232 /*-----------------------------------------------
234 *-----------------------------------------------*/
236 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
238 m->current_pg = pgpath->pg;
240 /* Must we initialise the PG first, and queue I/O till it's ready? */
241 if (m->hw_handler_name) {
242 m->pg_init_required = 1;
245 m->pg_init_required = 0;
249 m->pg_init_count = 0;
252 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
255 struct dm_path *path;
257 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
261 m->current_pgpath = path_to_pgpath(path);
263 if (m->current_pg != pg)
264 __switch_pg(m, m->current_pgpath);
269 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
271 struct priority_group *pg;
272 unsigned bypassed = 1;
274 if (!m->nr_valid_paths)
277 /* Were we instructed to switch PG? */
281 if (!__choose_path_in_pg(m, pg, nr_bytes))
285 /* Don't change PG until it has no remaining paths */
286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
290 * Loop through priority groups until we find a valid path.
291 * First time we skip PGs marked 'bypassed'.
292 * Second time we only try the ones we skipped.
295 list_for_each_entry(pg, &m->priority_groups, list) {
296 if (pg->bypassed == bypassed)
298 if (!__choose_path_in_pg(m, pg, nr_bytes))
301 } while (bypassed--);
304 m->current_pgpath = NULL;
305 m->current_pg = NULL;
309 * Check whether bios must be queued in the device-mapper core rather
310 * than here in the target.
312 * m->lock must be held on entry.
314 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
315 * same value then we are not between multipath_presuspend()
316 * and multipath_resume() calls and we have no need to check
317 * for the DMF_NOFLUSH_SUSPENDING flag.
319 static int __must_push_back(struct multipath *m)
321 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
322 dm_noflush_suspending(m->ti));
325 static int map_io(struct multipath *m, struct request *clone,
326 struct dm_mpath_io *mpio, unsigned was_queued)
328 int r = DM_MAPIO_REMAPPED;
329 size_t nr_bytes = blk_rq_bytes(clone);
331 struct pgpath *pgpath;
332 struct block_device *bdev;
334 spin_lock_irqsave(&m->lock, flags);
336 /* Do we need to select a new pgpath? */
337 if (!m->current_pgpath ||
338 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
339 __choose_pgpath(m, nr_bytes);
341 pgpath = m->current_pgpath;
346 if ((pgpath && m->queue_io) ||
347 (!pgpath && m->queue_if_no_path)) {
348 /* Queue for the daemon to resubmit */
349 list_add_tail(&clone->queuelist, &m->queued_ios);
351 if ((m->pg_init_required && !m->pg_init_in_progress) ||
353 queue_work(kmultipathd, &m->process_queued_ios);
355 r = DM_MAPIO_SUBMITTED;
357 bdev = pgpath->path.dev->bdev;
358 clone->q = bdev_get_queue(bdev);
359 clone->rq_disk = bdev->bd_disk;
360 } else if (__must_push_back(m))
361 r = DM_MAPIO_REQUEUE;
363 r = -EIO; /* Failed */
365 mpio->pgpath = pgpath;
366 mpio->nr_bytes = nr_bytes;
368 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
369 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
372 spin_unlock_irqrestore(&m->lock, flags);
378 * If we run out of usable paths, should we queue I/O or error it?
380 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
381 unsigned save_old_value)
385 spin_lock_irqsave(&m->lock, flags);
388 m->saved_queue_if_no_path = m->queue_if_no_path;
390 m->saved_queue_if_no_path = queue_if_no_path;
391 m->queue_if_no_path = queue_if_no_path;
392 if (!m->queue_if_no_path && m->queue_size)
393 queue_work(kmultipathd, &m->process_queued_ios);
395 spin_unlock_irqrestore(&m->lock, flags);
400 /*-----------------------------------------------------------------
401 * The multipath daemon is responsible for resubmitting queued ios.
402 *---------------------------------------------------------------*/
404 static void dispatch_queued_ios(struct multipath *m)
408 struct dm_mpath_io *mpio;
409 union map_info *info;
410 struct request *clone, *n;
413 spin_lock_irqsave(&m->lock, flags);
414 list_splice_init(&m->queued_ios, &cl);
415 spin_unlock_irqrestore(&m->lock, flags);
417 list_for_each_entry_safe(clone, n, &cl, queuelist) {
418 list_del_init(&clone->queuelist);
420 info = dm_get_rq_mapinfo(clone);
423 r = map_io(m, clone, mpio, 1);
425 mempool_free(mpio, m->mpio_pool);
426 dm_kill_unmapped_request(clone, r);
427 } else if (r == DM_MAPIO_REMAPPED)
428 dm_dispatch_request(clone);
429 else if (r == DM_MAPIO_REQUEUE) {
430 mempool_free(mpio, m->mpio_pool);
431 dm_requeue_unmapped_request(clone);
436 static void process_queued_ios(struct work_struct *work)
438 struct multipath *m =
439 container_of(work, struct multipath, process_queued_ios);
440 struct pgpath *pgpath = NULL, *tmp;
441 unsigned must_queue = 1;
444 spin_lock_irqsave(&m->lock, flags);
449 if (!m->current_pgpath)
450 __choose_pgpath(m, 0);
452 pgpath = m->current_pgpath;
454 if ((pgpath && !m->queue_io) ||
455 (!pgpath && !m->queue_if_no_path))
458 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
460 m->pg_init_required = 0;
461 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
462 if (queue_work(kmpath_handlerd, &tmp->activate_path))
463 m->pg_init_in_progress++;
467 spin_unlock_irqrestore(&m->lock, flags);
469 dispatch_queued_ios(m);
473 * An event is triggered whenever a path is taken out of use.
474 * Includes path failure and PG bypass.
476 static void trigger_event(struct work_struct *work)
478 struct multipath *m =
479 container_of(work, struct multipath, trigger_event);
481 dm_table_event(m->ti->table);
484 /*-----------------------------------------------------------------
485 * Constructor/argument parsing:
486 * <#multipath feature args> [<arg>]*
487 * <#hw_handler args> [hw_handler [<arg>]*]
489 * <initial priority group>
490 * [<selector> <#selector args> [<arg>]*
491 * <#paths> <#per-path selector args>
492 * [<path> [<arg>]* ]+ ]+
493 *---------------------------------------------------------------*/
500 static int read_param(struct param *param, char *str, unsigned *v, char **error)
503 (sscanf(str, "%u", v) != 1) ||
506 *error = param->error;
518 static char *shift(struct arg_set *as)
532 static void consume(struct arg_set *as, unsigned n)
534 BUG_ON (as->argc < n);
539 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
540 struct dm_target *ti)
543 struct path_selector_type *pst;
546 static struct param _params[] = {
547 {0, 1024, "invalid number of path selector args"},
550 pst = dm_get_path_selector(shift(as));
552 ti->error = "unknown path selector type";
556 r = read_param(_params, shift(as), &ps_argc, &ti->error);
558 dm_put_path_selector(pst);
562 if (ps_argc > as->argc) {
563 dm_put_path_selector(pst);
564 ti->error = "not enough arguments for path selector";
568 r = pst->create(&pg->ps, ps_argc, as->argv);
570 dm_put_path_selector(pst);
571 ti->error = "path selector constructor failed";
576 consume(as, ps_argc);
581 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
582 struct dm_target *ti)
586 struct multipath *m = ti->private;
588 /* we need at least a path arg */
590 ti->error = "no device given";
591 return ERR_PTR(-EINVAL);
596 return ERR_PTR(-ENOMEM);
598 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
599 dm_table_get_mode(ti->table), &p->path.dev);
601 ti->error = "error getting device";
605 if (m->hw_handler_name) {
606 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
608 r = scsi_dh_attach(q, m->hw_handler_name);
611 * Already attached to different hw_handler,
612 * try to reattach with correct one.
615 r = scsi_dh_attach(q, m->hw_handler_name);
619 ti->error = "error attaching hardware handler";
620 dm_put_device(ti, p->path.dev);
624 if (m->hw_handler_params) {
625 r = scsi_dh_set_params(q, m->hw_handler_params);
627 ti->error = "unable to set hardware "
628 "handler parameters";
630 dm_put_device(ti, p->path.dev);
636 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
638 dm_put_device(ti, p->path.dev);
649 static struct priority_group *parse_priority_group(struct arg_set *as,
652 static struct param _params[] = {
653 {1, 1024, "invalid number of paths"},
654 {0, 1024, "invalid number of selector args"}
658 unsigned i, nr_selector_args, nr_params;
659 struct priority_group *pg;
660 struct dm_target *ti = m->ti;
664 ti->error = "not enough priority group arguments";
665 return ERR_PTR(-EINVAL);
668 pg = alloc_priority_group();
670 ti->error = "couldn't allocate priority group";
671 return ERR_PTR(-ENOMEM);
675 r = parse_path_selector(as, pg, ti);
682 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
686 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
690 nr_params = 1 + nr_selector_args;
691 for (i = 0; i < pg->nr_pgpaths; i++) {
692 struct pgpath *pgpath;
693 struct arg_set path_args;
695 if (as->argc < nr_params) {
696 ti->error = "not enough path parameters";
700 path_args.argc = nr_params;
701 path_args.argv = as->argv;
703 pgpath = parse_path(&path_args, &pg->ps, ti);
704 if (IS_ERR(pgpath)) {
710 list_add_tail(&pgpath->list, &pg->pgpaths);
711 consume(as, nr_params);
717 free_priority_group(pg, ti);
721 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
725 struct dm_target *ti = m->ti;
727 static struct param _params[] = {
728 {0, 1024, "invalid number of hardware handler args"},
731 if (read_param(_params, shift(as), &hw_argc, &ti->error))
737 if (hw_argc > as->argc) {
738 ti->error = "not enough arguments for hardware handler";
742 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
743 request_module("scsi_dh_%s", m->hw_handler_name);
744 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
745 ti->error = "unknown hardware handler type";
754 for (i = 0; i <= hw_argc - 2; i++)
755 len += strlen(as->argv[i]) + 1;
756 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
758 ti->error = "memory allocation failed";
762 j = sprintf(p, "%d", hw_argc - 1);
763 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
764 j = sprintf(p, "%s", as->argv[i]);
766 consume(as, hw_argc - 1);
770 kfree(m->hw_handler_name);
771 m->hw_handler_name = NULL;
775 static int parse_features(struct arg_set *as, struct multipath *m)
779 struct dm_target *ti = m->ti;
780 const char *param_name;
782 static struct param _params[] = {
783 {0, 3, "invalid number of feature args"},
784 {1, 50, "pg_init_retries must be between 1 and 50"},
787 r = read_param(_params, shift(as), &argc, &ti->error);
795 param_name = shift(as);
798 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
799 r = queue_if_no_path(m, 1, 0);
803 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
805 r = read_param(_params + 1, shift(as),
806 &m->pg_init_retries, &ti->error);
811 ti->error = "Unrecognised multipath feature request";
813 } while (argc && !r);
818 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
821 /* target parameters */
822 static struct param _params[] = {
823 {1, 1024, "invalid number of priority groups"},
824 {1, 1024, "invalid initial priority group number"},
830 unsigned pg_count = 0;
831 unsigned next_pg_num;
836 m = alloc_multipath(ti);
838 ti->error = "can't allocate multipath";
842 r = parse_features(&as, m);
846 r = parse_hw_handler(&as, m);
850 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
854 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
858 /* parse the priority groups */
860 struct priority_group *pg;
862 pg = parse_priority_group(&as, m);
868 m->nr_valid_paths += pg->nr_pgpaths;
869 list_add_tail(&pg->list, &m->priority_groups);
871 pg->pg_num = pg_count;
876 if (pg_count != m->nr_priority_groups) {
877 ti->error = "priority group count mismatch";
882 ti->num_flush_requests = 1;
891 static void flush_multipath_work(void)
893 flush_workqueue(kmpath_handlerd);
894 flush_workqueue(kmultipathd);
895 flush_scheduled_work();
898 static void multipath_dtr(struct dm_target *ti)
900 struct multipath *m = ti->private;
902 flush_multipath_work();
907 * Map cloned requests
909 static int multipath_map(struct dm_target *ti, struct request *clone,
910 union map_info *map_context)
913 struct dm_mpath_io *mpio;
914 struct multipath *m = (struct multipath *) ti->private;
916 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
918 /* ENOMEM, requeue */
919 return DM_MAPIO_REQUEUE;
920 memset(mpio, 0, sizeof(*mpio));
922 map_context->ptr = mpio;
923 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
924 r = map_io(m, clone, mpio, 0);
925 if (r < 0 || r == DM_MAPIO_REQUEUE)
926 mempool_free(mpio, m->mpio_pool);
932 * Take a path out of use.
934 static int fail_path(struct pgpath *pgpath)
937 struct multipath *m = pgpath->pg->m;
939 spin_lock_irqsave(&m->lock, flags);
941 if (!pgpath->is_active)
944 DMWARN("Failing path %s.", pgpath->path.dev->name);
946 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
947 pgpath->is_active = 0;
948 pgpath->fail_count++;
952 if (pgpath == m->current_pgpath)
953 m->current_pgpath = NULL;
955 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
956 pgpath->path.dev->name, m->nr_valid_paths);
958 schedule_work(&m->trigger_event);
959 queue_work(kmultipathd, &pgpath->deactivate_path);
962 spin_unlock_irqrestore(&m->lock, flags);
968 * Reinstate a previously-failed path
970 static int reinstate_path(struct pgpath *pgpath)
974 struct multipath *m = pgpath->pg->m;
976 spin_lock_irqsave(&m->lock, flags);
978 if (pgpath->is_active)
981 if (!pgpath->pg->ps.type->reinstate_path) {
982 DMWARN("Reinstate path not supported by path selector %s",
983 pgpath->pg->ps.type->name);
988 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
992 pgpath->is_active = 1;
994 if (!m->nr_valid_paths++ && m->queue_size) {
995 m->current_pgpath = NULL;
996 queue_work(kmultipathd, &m->process_queued_ios);
997 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
998 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
999 m->pg_init_in_progress++;
1002 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1003 pgpath->path.dev->name, m->nr_valid_paths);
1005 schedule_work(&m->trigger_event);
1008 spin_unlock_irqrestore(&m->lock, flags);
1014 * Fail or reinstate all paths that match the provided struct dm_dev.
1016 static int action_dev(struct multipath *m, struct dm_dev *dev,
1020 struct pgpath *pgpath;
1021 struct priority_group *pg;
1023 list_for_each_entry(pg, &m->priority_groups, list) {
1024 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1025 if (pgpath->path.dev == dev)
1034 * Temporarily try to avoid having to use the specified PG
1036 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1039 unsigned long flags;
1041 spin_lock_irqsave(&m->lock, flags);
1043 pg->bypassed = bypassed;
1044 m->current_pgpath = NULL;
1045 m->current_pg = NULL;
1047 spin_unlock_irqrestore(&m->lock, flags);
1049 schedule_work(&m->trigger_event);
1053 * Switch to using the specified PG from the next I/O that gets mapped
1055 static int switch_pg_num(struct multipath *m, const char *pgstr)
1057 struct priority_group *pg;
1059 unsigned long flags;
1061 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1062 (pgnum > m->nr_priority_groups)) {
1063 DMWARN("invalid PG number supplied to switch_pg_num");
1067 spin_lock_irqsave(&m->lock, flags);
1068 list_for_each_entry(pg, &m->priority_groups, list) {
1073 m->current_pgpath = NULL;
1074 m->current_pg = NULL;
1077 spin_unlock_irqrestore(&m->lock, flags);
1079 schedule_work(&m->trigger_event);
1084 * Set/clear bypassed status of a PG.
1085 * PGs are numbered upwards from 1 in the order they were declared.
1087 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1089 struct priority_group *pg;
1092 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1093 (pgnum > m->nr_priority_groups)) {
1094 DMWARN("invalid PG number supplied to bypass_pg");
1098 list_for_each_entry(pg, &m->priority_groups, list) {
1103 bypass_pg(m, pg, bypassed);
1108 * Should we retry pg_init immediately?
1110 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1112 unsigned long flags;
1113 int limit_reached = 0;
1115 spin_lock_irqsave(&m->lock, flags);
1117 if (m->pg_init_count <= m->pg_init_retries)
1118 m->pg_init_required = 1;
1122 spin_unlock_irqrestore(&m->lock, flags);
1124 return limit_reached;
1127 static void pg_init_done(void *data, int errors)
1129 struct dm_path *path = data;
1130 struct pgpath *pgpath = path_to_pgpath(path);
1131 struct priority_group *pg = pgpath->pg;
1132 struct multipath *m = pg->m;
1133 unsigned long flags;
1135 /* device or driver problems */
1140 if (!m->hw_handler_name) {
1144 DMERR("Cannot failover device because scsi_dh_%s was not "
1145 "loaded.", m->hw_handler_name);
1147 * Fail path for now, so we do not ping pong
1151 case SCSI_DH_DEV_TEMP_BUSY:
1153 * Probably doing something like FW upgrade on the
1154 * controller so try the other pg.
1156 bypass_pg(m, pg, 1);
1158 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1160 case SCSI_DH_IMM_RETRY:
1161 case SCSI_DH_RES_TEMP_UNAVAIL:
1162 if (pg_init_limit_reached(m, pgpath))
1168 * We probably do not want to fail the path for a device
1169 * error, but this is what the old dm did. In future
1170 * patches we can do more advanced handling.
1175 spin_lock_irqsave(&m->lock, flags);
1177 if (pgpath == m->current_pgpath) {
1178 DMERR("Could not failover device. Error %d.", errors);
1179 m->current_pgpath = NULL;
1180 m->current_pg = NULL;
1182 } else if (!m->pg_init_required) {
1187 m->pg_init_in_progress--;
1188 if (!m->pg_init_in_progress)
1189 queue_work(kmultipathd, &m->process_queued_ios);
1190 spin_unlock_irqrestore(&m->lock, flags);
1193 static void activate_path(struct work_struct *work)
1195 struct pgpath *pgpath =
1196 container_of(work, struct pgpath, activate_path);
1198 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1199 pg_init_done, &pgpath->path);
1205 static int do_end_io(struct multipath *m, struct request *clone,
1206 int error, struct dm_mpath_io *mpio)
1209 * We don't queue any clone request inside the multipath target
1210 * during end I/O handling, since those clone requests don't have
1211 * bio clones. If we queue them inside the multipath target,
1212 * we need to make bio clones, that requires memory allocation.
1213 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1214 * don't have bio clones.)
1215 * Instead of queueing the clone request here, we queue the original
1216 * request into dm core, which will remake a clone request and
1217 * clone bios for it and resubmit it later.
1219 int r = DM_ENDIO_REQUEUE;
1220 unsigned long flags;
1222 if (!error && !clone->errors)
1223 return 0; /* I/O complete */
1225 if (error == -EOPNOTSUPP)
1229 fail_path(mpio->pgpath);
1231 spin_lock_irqsave(&m->lock, flags);
1232 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1234 spin_unlock_irqrestore(&m->lock, flags);
1239 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1240 int error, union map_info *map_context)
1242 struct multipath *m = ti->private;
1243 struct dm_mpath_io *mpio = map_context->ptr;
1244 struct pgpath *pgpath = mpio->pgpath;
1245 struct path_selector *ps;
1248 r = do_end_io(m, clone, error, mpio);
1250 ps = &pgpath->pg->ps;
1251 if (ps->type->end_io)
1252 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1254 mempool_free(mpio, m->mpio_pool);
1260 * Suspend can't complete until all the I/O is processed so if
1261 * the last path fails we must error any remaining I/O.
1262 * Note that if the freeze_bdev fails while suspending, the
1263 * queue_if_no_path state is lost - userspace should reset it.
1265 static void multipath_presuspend(struct dm_target *ti)
1267 struct multipath *m = (struct multipath *) ti->private;
1269 queue_if_no_path(m, 0, 1);
1272 static void multipath_postsuspend(struct dm_target *ti)
1274 struct multipath *m = ti->private;
1276 mutex_lock(&m->work_mutex);
1277 flush_multipath_work();
1278 mutex_unlock(&m->work_mutex);
1282 * Restore the queue_if_no_path setting.
1284 static void multipath_resume(struct dm_target *ti)
1286 struct multipath *m = (struct multipath *) ti->private;
1287 unsigned long flags;
1289 spin_lock_irqsave(&m->lock, flags);
1290 m->queue_if_no_path = m->saved_queue_if_no_path;
1291 spin_unlock_irqrestore(&m->lock, flags);
1295 * Info output has the following format:
1296 * num_multipath_feature_args [multipath_feature_args]*
1297 * num_handler_status_args [handler_status_args]*
1298 * num_groups init_group_number
1299 * [A|D|E num_ps_status_args [ps_status_args]*
1300 * num_paths num_selector_args
1301 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1303 * Table output has the following format (identical to the constructor string):
1304 * num_feature_args [features_args]*
1305 * num_handler_args hw_handler [hw_handler_args]*
1306 * num_groups init_group_number
1307 * [priority selector-name num_ps_args [ps_args]*
1308 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1310 static int multipath_status(struct dm_target *ti, status_type_t type,
1311 char *result, unsigned int maxlen)
1314 unsigned long flags;
1315 struct multipath *m = (struct multipath *) ti->private;
1316 struct priority_group *pg;
1321 spin_lock_irqsave(&m->lock, flags);
1324 if (type == STATUSTYPE_INFO)
1325 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1327 DMEMIT("%u ", m->queue_if_no_path +
1328 (m->pg_init_retries > 0) * 2);
1329 if (m->queue_if_no_path)
1330 DMEMIT("queue_if_no_path ");
1331 if (m->pg_init_retries)
1332 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1335 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1338 DMEMIT("1 %s ", m->hw_handler_name);
1340 DMEMIT("%u ", m->nr_priority_groups);
1343 pg_num = m->next_pg->pg_num;
1344 else if (m->current_pg)
1345 pg_num = m->current_pg->pg_num;
1349 DMEMIT("%u ", pg_num);
1352 case STATUSTYPE_INFO:
1353 list_for_each_entry(pg, &m->priority_groups, list) {
1355 state = 'D'; /* Disabled */
1356 else if (pg == m->current_pg)
1357 state = 'A'; /* Currently Active */
1359 state = 'E'; /* Enabled */
1361 DMEMIT("%c ", state);
1363 if (pg->ps.type->status)
1364 sz += pg->ps.type->status(&pg->ps, NULL, type,
1370 DMEMIT("%u %u ", pg->nr_pgpaths,
1371 pg->ps.type->info_args);
1373 list_for_each_entry(p, &pg->pgpaths, list) {
1374 DMEMIT("%s %s %u ", p->path.dev->name,
1375 p->is_active ? "A" : "F",
1377 if (pg->ps.type->status)
1378 sz += pg->ps.type->status(&pg->ps,
1379 &p->path, type, result + sz,
1385 case STATUSTYPE_TABLE:
1386 list_for_each_entry(pg, &m->priority_groups, list) {
1387 DMEMIT("%s ", pg->ps.type->name);
1389 if (pg->ps.type->status)
1390 sz += pg->ps.type->status(&pg->ps, NULL, type,
1396 DMEMIT("%u %u ", pg->nr_pgpaths,
1397 pg->ps.type->table_args);
1399 list_for_each_entry(p, &pg->pgpaths, list) {
1400 DMEMIT("%s ", p->path.dev->name);
1401 if (pg->ps.type->status)
1402 sz += pg->ps.type->status(&pg->ps,
1403 &p->path, type, result + sz,
1410 spin_unlock_irqrestore(&m->lock, flags);
1415 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1419 struct multipath *m = (struct multipath *) ti->private;
1422 mutex_lock(&m->work_mutex);
1425 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1426 r = queue_if_no_path(m, 1, 0);
1428 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1429 r = queue_if_no_path(m, 0, 0);
1435 DMWARN("Unrecognised multipath message received.");
1439 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1440 r = bypass_pg_num(m, argv[1], 1);
1442 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1443 r = bypass_pg_num(m, argv[1], 0);
1445 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1446 r = switch_pg_num(m, argv[1]);
1448 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1449 action = reinstate_path;
1450 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1453 DMWARN("Unrecognised multipath message received.");
1457 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1458 dm_table_get_mode(ti->table), &dev);
1460 DMWARN("message: error getting device %s",
1465 r = action_dev(m, dev, action);
1467 dm_put_device(ti, dev);
1470 mutex_unlock(&m->work_mutex);
1474 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1477 struct multipath *m = (struct multipath *) ti->private;
1478 struct block_device *bdev = NULL;
1480 unsigned long flags;
1483 spin_lock_irqsave(&m->lock, flags);
1485 if (!m->current_pgpath)
1486 __choose_pgpath(m, 0);
1488 if (m->current_pgpath) {
1489 bdev = m->current_pgpath->path.dev->bdev;
1490 mode = m->current_pgpath->path.dev->mode;
1498 spin_unlock_irqrestore(&m->lock, flags);
1500 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1503 static int multipath_iterate_devices(struct dm_target *ti,
1504 iterate_devices_callout_fn fn, void *data)
1506 struct multipath *m = ti->private;
1507 struct priority_group *pg;
1511 list_for_each_entry(pg, &m->priority_groups, list) {
1512 list_for_each_entry(p, &pg->pgpaths, list) {
1513 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1523 static int __pgpath_busy(struct pgpath *pgpath)
1525 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1527 return dm_underlying_device_busy(q);
1531 * We return "busy", only when we can map I/Os but underlying devices
1532 * are busy (so even if we map I/Os now, the I/Os will wait on
1533 * the underlying queue).
1534 * In other words, if we want to kill I/Os or queue them inside us
1535 * due to map unavailability, we don't return "busy". Otherwise,
1536 * dm core won't give us the I/Os and we can't do what we want.
1538 static int multipath_busy(struct dm_target *ti)
1540 int busy = 0, has_active = 0;
1541 struct multipath *m = ti->private;
1542 struct priority_group *pg;
1543 struct pgpath *pgpath;
1544 unsigned long flags;
1546 spin_lock_irqsave(&m->lock, flags);
1548 /* Guess which priority_group will be used at next mapping time */
1549 if (unlikely(!m->current_pgpath && m->next_pg))
1551 else if (likely(m->current_pg))
1555 * We don't know which pg will be used at next mapping time.
1556 * We don't call __choose_pgpath() here to avoid to trigger
1557 * pg_init just by busy checking.
1558 * So we don't know whether underlying devices we will be using
1559 * at next mapping time are busy or not. Just try mapping.
1564 * If there is one non-busy active path at least, the path selector
1565 * will be able to select it. So we consider such a pg as not busy.
1568 list_for_each_entry(pgpath, &pg->pgpaths, list)
1569 if (pgpath->is_active) {
1572 if (!__pgpath_busy(pgpath)) {
1580 * No active path in this pg, so this pg won't be used and
1581 * the current_pg will be changed at next mapping time.
1582 * We need to try mapping to determine it.
1587 spin_unlock_irqrestore(&m->lock, flags);
1592 /*-----------------------------------------------------------------
1594 *---------------------------------------------------------------*/
1595 static struct target_type multipath_target = {
1596 .name = "multipath",
1597 .version = {1, 1, 1},
1598 .module = THIS_MODULE,
1599 .ctr = multipath_ctr,
1600 .dtr = multipath_dtr,
1601 .map_rq = multipath_map,
1602 .rq_end_io = multipath_end_io,
1603 .presuspend = multipath_presuspend,
1604 .postsuspend = multipath_postsuspend,
1605 .resume = multipath_resume,
1606 .status = multipath_status,
1607 .message = multipath_message,
1608 .ioctl = multipath_ioctl,
1609 .iterate_devices = multipath_iterate_devices,
1610 .busy = multipath_busy,
1613 static int __init dm_multipath_init(void)
1617 /* allocate a slab for the dm_ios */
1618 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1622 r = dm_register_target(&multipath_target);
1624 DMERR("register failed %d", r);
1625 kmem_cache_destroy(_mpio_cache);
1629 kmultipathd = create_workqueue("kmpathd");
1631 DMERR("failed to create workqueue kmpathd");
1632 dm_unregister_target(&multipath_target);
1633 kmem_cache_destroy(_mpio_cache);
1638 * A separate workqueue is used to handle the device handlers
1639 * to avoid overloading existing workqueue. Overloading the
1640 * old workqueue would also create a bottleneck in the
1641 * path of the storage hardware device activation.
1643 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1644 if (!kmpath_handlerd) {
1645 DMERR("failed to create workqueue kmpath_handlerd");
1646 destroy_workqueue(kmultipathd);
1647 dm_unregister_target(&multipath_target);
1648 kmem_cache_destroy(_mpio_cache);
1652 DMINFO("version %u.%u.%u loaded",
1653 multipath_target.version[0], multipath_target.version[1],
1654 multipath_target.version[2]);
1659 static void __exit dm_multipath_exit(void)
1661 destroy_workqueue(kmpath_handlerd);
1662 destroy_workqueue(kmultipathd);
1664 dm_unregister_target(&multipath_target);
1665 kmem_cache_destroy(_mpio_cache);
1668 module_init(dm_multipath_init);
1669 module_exit(dm_multipath_exit);
1671 MODULE_DESCRIPTION(DM_NAME " multipath target");
1672 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1673 MODULE_LICENSE("GPL");