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 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
70 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
72 unsigned nr_valid_paths; /* Total number of usable paths */
73 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
76 unsigned repeat_count; /* I/Os left before calling PS again */
78 unsigned queue_io; /* Must we queue all I/O? */
79 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
80 unsigned saved_queue_if_no_path;/* Saved state during suspension */
81 unsigned pg_init_retries; /* Number of times to retry pg_init */
82 unsigned pg_init_count; /* Number of times pg_init called */
84 struct work_struct process_queued_ios;
85 struct list_head queued_ios;
88 struct work_struct trigger_event;
91 * We must use a mempool of dm_mpath_io structs so that we
92 * can resubmit bios on error.
98 * Context information attached to each bio we process.
101 struct pgpath *pgpath;
105 typedef int (*action_fn) (struct pgpath *pgpath);
107 #define MIN_IOS 256 /* Mempool size */
109 static struct kmem_cache *_mpio_cache;
111 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
112 static void process_queued_ios(struct work_struct *work);
113 static void trigger_event(struct work_struct *work);
114 static void activate_path(struct work_struct *work);
115 static void deactivate_path(struct work_struct *work);
118 /*-----------------------------------------------
119 * Allocation routines
120 *-----------------------------------------------*/
122 static struct pgpath *alloc_pgpath(void)
124 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
127 pgpath->is_active = 1;
128 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
129 INIT_WORK(&pgpath->activate_path, activate_path);
135 static void free_pgpath(struct pgpath *pgpath)
140 static void deactivate_path(struct work_struct *work)
142 struct pgpath *pgpath =
143 container_of(work, struct pgpath, deactivate_path);
145 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
148 static struct priority_group *alloc_priority_group(void)
150 struct priority_group *pg;
152 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
155 INIT_LIST_HEAD(&pg->pgpaths);
160 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
162 struct pgpath *pgpath, *tmp;
163 struct multipath *m = ti->private;
165 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
166 list_del(&pgpath->list);
167 dm_put_device(ti, pgpath->path.dev);
172 static void free_priority_group(struct priority_group *pg,
173 struct dm_target *ti)
175 struct path_selector *ps = &pg->ps;
178 ps->type->destroy(ps);
179 dm_put_path_selector(ps->type);
182 free_pgpaths(&pg->pgpaths, ti);
186 static struct multipath *alloc_multipath(struct dm_target *ti)
190 m = kzalloc(sizeof(*m), GFP_KERNEL);
192 INIT_LIST_HEAD(&m->priority_groups);
193 INIT_LIST_HEAD(&m->queued_ios);
194 spin_lock_init(&m->lock);
196 INIT_WORK(&m->process_queued_ios, process_queued_ios);
197 INIT_WORK(&m->trigger_event, trigger_event);
198 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
210 static void free_multipath(struct multipath *m)
212 struct priority_group *pg, *tmp;
214 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
216 free_priority_group(pg, m->ti);
219 kfree(m->hw_handler_name);
220 mempool_destroy(m->mpio_pool);
225 /*-----------------------------------------------
227 *-----------------------------------------------*/
229 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
231 m->current_pg = pgpath->pg;
233 /* Must we initialise the PG first, and queue I/O till it's ready? */
234 if (m->hw_handler_name) {
235 m->pg_init_required = 1;
238 m->pg_init_required = 0;
242 m->pg_init_count = 0;
245 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
248 struct dm_path *path;
250 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
254 m->current_pgpath = path_to_pgpath(path);
256 if (m->current_pg != pg)
257 __switch_pg(m, m->current_pgpath);
262 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
264 struct priority_group *pg;
265 unsigned bypassed = 1;
267 if (!m->nr_valid_paths)
270 /* Were we instructed to switch PG? */
274 if (!__choose_path_in_pg(m, pg, nr_bytes))
278 /* Don't change PG until it has no remaining paths */
279 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
283 * Loop through priority groups until we find a valid path.
284 * First time we skip PGs marked 'bypassed'.
285 * Second time we only try the ones we skipped.
288 list_for_each_entry(pg, &m->priority_groups, list) {
289 if (pg->bypassed == bypassed)
291 if (!__choose_path_in_pg(m, pg, nr_bytes))
294 } while (bypassed--);
297 m->current_pgpath = NULL;
298 m->current_pg = NULL;
302 * Check whether bios must be queued in the device-mapper core rather
303 * than here in the target.
305 * m->lock must be held on entry.
307 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
308 * same value then we are not between multipath_presuspend()
309 * and multipath_resume() calls and we have no need to check
310 * for the DMF_NOFLUSH_SUSPENDING flag.
312 static int __must_push_back(struct multipath *m)
314 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
315 dm_noflush_suspending(m->ti));
318 static int map_io(struct multipath *m, struct request *clone,
319 struct dm_mpath_io *mpio, unsigned was_queued)
321 int r = DM_MAPIO_REMAPPED;
322 size_t nr_bytes = blk_rq_bytes(clone);
324 struct pgpath *pgpath;
325 struct block_device *bdev;
327 spin_lock_irqsave(&m->lock, flags);
329 /* Do we need to select a new pgpath? */
330 if (!m->current_pgpath ||
331 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
332 __choose_pgpath(m, nr_bytes);
334 pgpath = m->current_pgpath;
339 if ((pgpath && m->queue_io) ||
340 (!pgpath && m->queue_if_no_path)) {
341 /* Queue for the daemon to resubmit */
342 list_add_tail(&clone->queuelist, &m->queued_ios);
344 if ((m->pg_init_required && !m->pg_init_in_progress) ||
346 queue_work(kmultipathd, &m->process_queued_ios);
348 r = DM_MAPIO_SUBMITTED;
350 bdev = pgpath->path.dev->bdev;
351 clone->q = bdev_get_queue(bdev);
352 clone->rq_disk = bdev->bd_disk;
353 } else if (__must_push_back(m))
354 r = DM_MAPIO_REQUEUE;
356 r = -EIO; /* Failed */
358 mpio->pgpath = pgpath;
359 mpio->nr_bytes = nr_bytes;
361 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
362 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
365 spin_unlock_irqrestore(&m->lock, flags);
371 * If we run out of usable paths, should we queue I/O or error it?
373 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
374 unsigned save_old_value)
378 spin_lock_irqsave(&m->lock, flags);
381 m->saved_queue_if_no_path = m->queue_if_no_path;
383 m->saved_queue_if_no_path = queue_if_no_path;
384 m->queue_if_no_path = queue_if_no_path;
385 if (!m->queue_if_no_path && m->queue_size)
386 queue_work(kmultipathd, &m->process_queued_ios);
388 spin_unlock_irqrestore(&m->lock, flags);
393 /*-----------------------------------------------------------------
394 * The multipath daemon is responsible for resubmitting queued ios.
395 *---------------------------------------------------------------*/
397 static void dispatch_queued_ios(struct multipath *m)
401 struct dm_mpath_io *mpio;
402 union map_info *info;
403 struct request *clone, *n;
406 spin_lock_irqsave(&m->lock, flags);
407 list_splice_init(&m->queued_ios, &cl);
408 spin_unlock_irqrestore(&m->lock, flags);
410 list_for_each_entry_safe(clone, n, &cl, queuelist) {
411 list_del_init(&clone->queuelist);
413 info = dm_get_rq_mapinfo(clone);
416 r = map_io(m, clone, mpio, 1);
418 mempool_free(mpio, m->mpio_pool);
419 dm_kill_unmapped_request(clone, r);
420 } else if (r == DM_MAPIO_REMAPPED)
421 dm_dispatch_request(clone);
422 else if (r == DM_MAPIO_REQUEUE) {
423 mempool_free(mpio, m->mpio_pool);
424 dm_requeue_unmapped_request(clone);
429 static void process_queued_ios(struct work_struct *work)
431 struct multipath *m =
432 container_of(work, struct multipath, process_queued_ios);
433 struct pgpath *pgpath = NULL, *tmp;
434 unsigned must_queue = 1;
437 spin_lock_irqsave(&m->lock, flags);
442 if (!m->current_pgpath)
443 __choose_pgpath(m, 0);
445 pgpath = m->current_pgpath;
447 if ((pgpath && !m->queue_io) ||
448 (!pgpath && !m->queue_if_no_path))
451 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
453 m->pg_init_required = 0;
454 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
455 if (queue_work(kmpath_handlerd, &tmp->activate_path))
456 m->pg_init_in_progress++;
460 spin_unlock_irqrestore(&m->lock, flags);
462 dispatch_queued_ios(m);
466 * An event is triggered whenever a path is taken out of use.
467 * Includes path failure and PG bypass.
469 static void trigger_event(struct work_struct *work)
471 struct multipath *m =
472 container_of(work, struct multipath, trigger_event);
474 dm_table_event(m->ti->table);
477 /*-----------------------------------------------------------------
478 * Constructor/argument parsing:
479 * <#multipath feature args> [<arg>]*
480 * <#hw_handler args> [hw_handler [<arg>]*]
482 * <initial priority group>
483 * [<selector> <#selector args> [<arg>]*
484 * <#paths> <#per-path selector args>
485 * [<path> [<arg>]* ]+ ]+
486 *---------------------------------------------------------------*/
493 static int read_param(struct param *param, char *str, unsigned *v, char **error)
496 (sscanf(str, "%u", v) != 1) ||
499 *error = param->error;
511 static char *shift(struct arg_set *as)
525 static void consume(struct arg_set *as, unsigned n)
527 BUG_ON (as->argc < n);
532 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
533 struct dm_target *ti)
536 struct path_selector_type *pst;
539 static struct param _params[] = {
540 {0, 1024, "invalid number of path selector args"},
543 pst = dm_get_path_selector(shift(as));
545 ti->error = "unknown path selector type";
549 r = read_param(_params, shift(as), &ps_argc, &ti->error);
551 dm_put_path_selector(pst);
555 if (ps_argc > as->argc) {
556 dm_put_path_selector(pst);
557 ti->error = "not enough arguments for path selector";
561 r = pst->create(&pg->ps, ps_argc, as->argv);
563 dm_put_path_selector(pst);
564 ti->error = "path selector constructor failed";
569 consume(as, ps_argc);
574 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
575 struct dm_target *ti)
579 struct multipath *m = ti->private;
581 /* we need at least a path arg */
583 ti->error = "no device given";
584 return ERR_PTR(-EINVAL);
589 return ERR_PTR(-ENOMEM);
591 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
592 dm_table_get_mode(ti->table), &p->path.dev);
594 ti->error = "error getting device";
598 if (m->hw_handler_name) {
599 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
601 r = scsi_dh_attach(q, m->hw_handler_name);
604 * Already attached to different hw_handler,
605 * try to reattach with correct one.
608 r = scsi_dh_attach(q, m->hw_handler_name);
612 ti->error = "error attaching hardware handler";
613 dm_put_device(ti, p->path.dev);
618 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
620 dm_put_device(ti, p->path.dev);
631 static struct priority_group *parse_priority_group(struct arg_set *as,
634 static struct param _params[] = {
635 {1, 1024, "invalid number of paths"},
636 {0, 1024, "invalid number of selector args"}
640 unsigned i, nr_selector_args, nr_params;
641 struct priority_group *pg;
642 struct dm_target *ti = m->ti;
646 ti->error = "not enough priority group arguments";
647 return ERR_PTR(-EINVAL);
650 pg = alloc_priority_group();
652 ti->error = "couldn't allocate priority group";
653 return ERR_PTR(-ENOMEM);
657 r = parse_path_selector(as, pg, ti);
664 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
668 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
672 nr_params = 1 + nr_selector_args;
673 for (i = 0; i < pg->nr_pgpaths; i++) {
674 struct pgpath *pgpath;
675 struct arg_set path_args;
677 if (as->argc < nr_params) {
678 ti->error = "not enough path parameters";
682 path_args.argc = nr_params;
683 path_args.argv = as->argv;
685 pgpath = parse_path(&path_args, &pg->ps, ti);
686 if (IS_ERR(pgpath)) {
692 list_add_tail(&pgpath->list, &pg->pgpaths);
693 consume(as, nr_params);
699 free_priority_group(pg, ti);
703 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
706 struct dm_target *ti = m->ti;
708 static struct param _params[] = {
709 {0, 1024, "invalid number of hardware handler args"},
712 if (read_param(_params, shift(as), &hw_argc, &ti->error))
718 if (hw_argc > as->argc) {
719 ti->error = "not enough arguments for hardware handler";
723 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
724 request_module("scsi_dh_%s", m->hw_handler_name);
725 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
726 ti->error = "unknown hardware handler type";
727 kfree(m->hw_handler_name);
728 m->hw_handler_name = NULL;
733 DMWARN("Ignoring user-specified arguments for "
734 "hardware handler \"%s\"", m->hw_handler_name);
735 consume(as, hw_argc - 1);
740 static int parse_features(struct arg_set *as, struct multipath *m)
744 struct dm_target *ti = m->ti;
745 const char *param_name;
747 static struct param _params[] = {
748 {0, 3, "invalid number of feature args"},
749 {1, 50, "pg_init_retries must be between 1 and 50"},
752 r = read_param(_params, shift(as), &argc, &ti->error);
760 param_name = shift(as);
763 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
764 r = queue_if_no_path(m, 1, 0);
768 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
770 r = read_param(_params + 1, shift(as),
771 &m->pg_init_retries, &ti->error);
776 ti->error = "Unrecognised multipath feature request";
778 } while (argc && !r);
783 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
786 /* target parameters */
787 static struct param _params[] = {
788 {0, 1024, "invalid number of priority groups"},
789 {0, 1024, "invalid initial priority group number"},
795 unsigned pg_count = 0;
796 unsigned next_pg_num;
801 m = alloc_multipath(ti);
803 ti->error = "can't allocate multipath";
807 r = parse_features(&as, m);
811 r = parse_hw_handler(&as, m);
815 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
819 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
823 /* parse the priority groups */
825 struct priority_group *pg;
827 pg = parse_priority_group(&as, m);
833 m->nr_valid_paths += pg->nr_pgpaths;
834 list_add_tail(&pg->list, &m->priority_groups);
836 pg->pg_num = pg_count;
841 if (pg_count != m->nr_priority_groups) {
842 ti->error = "priority group count mismatch";
847 ti->num_flush_requests = 1;
856 static void multipath_dtr(struct dm_target *ti)
858 struct multipath *m = (struct multipath *) ti->private;
860 flush_workqueue(kmpath_handlerd);
861 flush_workqueue(kmultipathd);
862 flush_scheduled_work();
867 * Map cloned requests
869 static int multipath_map(struct dm_target *ti, struct request *clone,
870 union map_info *map_context)
873 struct dm_mpath_io *mpio;
874 struct multipath *m = (struct multipath *) ti->private;
876 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
878 /* ENOMEM, requeue */
879 return DM_MAPIO_REQUEUE;
880 memset(mpio, 0, sizeof(*mpio));
882 map_context->ptr = mpio;
883 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
884 r = map_io(m, clone, mpio, 0);
885 if (r < 0 || r == DM_MAPIO_REQUEUE)
886 mempool_free(mpio, m->mpio_pool);
892 * Take a path out of use.
894 static int fail_path(struct pgpath *pgpath)
897 struct multipath *m = pgpath->pg->m;
899 spin_lock_irqsave(&m->lock, flags);
901 if (!pgpath->is_active)
904 DMWARN("Failing path %s.", pgpath->path.dev->name);
906 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
907 pgpath->is_active = 0;
908 pgpath->fail_count++;
912 if (pgpath == m->current_pgpath)
913 m->current_pgpath = NULL;
915 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
916 pgpath->path.dev->name, m->nr_valid_paths);
918 schedule_work(&m->trigger_event);
919 queue_work(kmultipathd, &pgpath->deactivate_path);
922 spin_unlock_irqrestore(&m->lock, flags);
928 * Reinstate a previously-failed path
930 static int reinstate_path(struct pgpath *pgpath)
934 struct multipath *m = pgpath->pg->m;
936 spin_lock_irqsave(&m->lock, flags);
938 if (pgpath->is_active)
941 if (!pgpath->pg->ps.type->reinstate_path) {
942 DMWARN("Reinstate path not supported by path selector %s",
943 pgpath->pg->ps.type->name);
948 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
952 pgpath->is_active = 1;
954 if (!m->nr_valid_paths++ && m->queue_size) {
955 m->current_pgpath = NULL;
956 queue_work(kmultipathd, &m->process_queued_ios);
957 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
958 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
959 m->pg_init_in_progress++;
962 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
963 pgpath->path.dev->name, m->nr_valid_paths);
965 schedule_work(&m->trigger_event);
968 spin_unlock_irqrestore(&m->lock, flags);
974 * Fail or reinstate all paths that match the provided struct dm_dev.
976 static int action_dev(struct multipath *m, struct dm_dev *dev,
980 struct pgpath *pgpath;
981 struct priority_group *pg;
983 list_for_each_entry(pg, &m->priority_groups, list) {
984 list_for_each_entry(pgpath, &pg->pgpaths, list) {
985 if (pgpath->path.dev == dev)
994 * Temporarily try to avoid having to use the specified PG
996 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1001 spin_lock_irqsave(&m->lock, flags);
1003 pg->bypassed = bypassed;
1004 m->current_pgpath = NULL;
1005 m->current_pg = NULL;
1007 spin_unlock_irqrestore(&m->lock, flags);
1009 schedule_work(&m->trigger_event);
1013 * Switch to using the specified PG from the next I/O that gets mapped
1015 static int switch_pg_num(struct multipath *m, const char *pgstr)
1017 struct priority_group *pg;
1019 unsigned long flags;
1021 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1022 (pgnum > m->nr_priority_groups)) {
1023 DMWARN("invalid PG number supplied to switch_pg_num");
1027 spin_lock_irqsave(&m->lock, flags);
1028 list_for_each_entry(pg, &m->priority_groups, list) {
1033 m->current_pgpath = NULL;
1034 m->current_pg = NULL;
1037 spin_unlock_irqrestore(&m->lock, flags);
1039 schedule_work(&m->trigger_event);
1044 * Set/clear bypassed status of a PG.
1045 * PGs are numbered upwards from 1 in the order they were declared.
1047 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1049 struct priority_group *pg;
1052 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1053 (pgnum > m->nr_priority_groups)) {
1054 DMWARN("invalid PG number supplied to bypass_pg");
1058 list_for_each_entry(pg, &m->priority_groups, list) {
1063 bypass_pg(m, pg, bypassed);
1068 * Should we retry pg_init immediately?
1070 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1072 unsigned long flags;
1073 int limit_reached = 0;
1075 spin_lock_irqsave(&m->lock, flags);
1077 if (m->pg_init_count <= m->pg_init_retries)
1078 m->pg_init_required = 1;
1082 spin_unlock_irqrestore(&m->lock, flags);
1084 return limit_reached;
1087 static void pg_init_done(struct dm_path *path, int errors)
1089 struct pgpath *pgpath = path_to_pgpath(path);
1090 struct priority_group *pg = pgpath->pg;
1091 struct multipath *m = pg->m;
1092 unsigned long flags;
1094 /* device or driver problems */
1099 if (!m->hw_handler_name) {
1103 DMERR("Cannot failover device because scsi_dh_%s was not "
1104 "loaded.", m->hw_handler_name);
1106 * Fail path for now, so we do not ping pong
1110 case SCSI_DH_DEV_TEMP_BUSY:
1112 * Probably doing something like FW upgrade on the
1113 * controller so try the other pg.
1115 bypass_pg(m, pg, 1);
1117 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1119 case SCSI_DH_IMM_RETRY:
1120 case SCSI_DH_RES_TEMP_UNAVAIL:
1121 if (pg_init_limit_reached(m, pgpath))
1127 * We probably do not want to fail the path for a device
1128 * error, but this is what the old dm did. In future
1129 * patches we can do more advanced handling.
1134 spin_lock_irqsave(&m->lock, flags);
1136 if (pgpath == m->current_pgpath) {
1137 DMERR("Could not failover device. Error %d.", errors);
1138 m->current_pgpath = NULL;
1139 m->current_pg = NULL;
1141 } else if (!m->pg_init_required) {
1146 m->pg_init_in_progress--;
1147 if (!m->pg_init_in_progress)
1148 queue_work(kmultipathd, &m->process_queued_ios);
1149 spin_unlock_irqrestore(&m->lock, flags);
1152 static void activate_path(struct work_struct *work)
1155 struct pgpath *pgpath =
1156 container_of(work, struct pgpath, activate_path);
1158 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1159 pg_init_done(&pgpath->path, ret);
1165 static int do_end_io(struct multipath *m, struct request *clone,
1166 int error, struct dm_mpath_io *mpio)
1169 * We don't queue any clone request inside the multipath target
1170 * during end I/O handling, since those clone requests don't have
1171 * bio clones. If we queue them inside the multipath target,
1172 * we need to make bio clones, that requires memory allocation.
1173 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1174 * don't have bio clones.)
1175 * Instead of queueing the clone request here, we queue the original
1176 * request into dm core, which will remake a clone request and
1177 * clone bios for it and resubmit it later.
1179 int r = DM_ENDIO_REQUEUE;
1180 unsigned long flags;
1182 if (!error && !clone->errors)
1183 return 0; /* I/O complete */
1185 if (error == -EOPNOTSUPP)
1189 fail_path(mpio->pgpath);
1191 spin_lock_irqsave(&m->lock, flags);
1192 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1194 spin_unlock_irqrestore(&m->lock, flags);
1199 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1200 int error, union map_info *map_context)
1202 struct multipath *m = ti->private;
1203 struct dm_mpath_io *mpio = map_context->ptr;
1204 struct pgpath *pgpath = mpio->pgpath;
1205 struct path_selector *ps;
1208 r = do_end_io(m, clone, error, mpio);
1210 ps = &pgpath->pg->ps;
1211 if (ps->type->end_io)
1212 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1214 mempool_free(mpio, m->mpio_pool);
1220 * Suspend can't complete until all the I/O is processed so if
1221 * the last path fails we must error any remaining I/O.
1222 * Note that if the freeze_bdev fails while suspending, the
1223 * queue_if_no_path state is lost - userspace should reset it.
1225 static void multipath_presuspend(struct dm_target *ti)
1227 struct multipath *m = (struct multipath *) ti->private;
1229 queue_if_no_path(m, 0, 1);
1233 * Restore the queue_if_no_path setting.
1235 static void multipath_resume(struct dm_target *ti)
1237 struct multipath *m = (struct multipath *) ti->private;
1238 unsigned long flags;
1240 spin_lock_irqsave(&m->lock, flags);
1241 m->queue_if_no_path = m->saved_queue_if_no_path;
1242 spin_unlock_irqrestore(&m->lock, flags);
1246 * Info output has the following format:
1247 * num_multipath_feature_args [multipath_feature_args]*
1248 * num_handler_status_args [handler_status_args]*
1249 * num_groups init_group_number
1250 * [A|D|E num_ps_status_args [ps_status_args]*
1251 * num_paths num_selector_args
1252 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1254 * Table output has the following format (identical to the constructor string):
1255 * num_feature_args [features_args]*
1256 * num_handler_args hw_handler [hw_handler_args]*
1257 * num_groups init_group_number
1258 * [priority selector-name num_ps_args [ps_args]*
1259 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1261 static int multipath_status(struct dm_target *ti, status_type_t type,
1262 char *result, unsigned int maxlen)
1265 unsigned long flags;
1266 struct multipath *m = (struct multipath *) ti->private;
1267 struct priority_group *pg;
1272 spin_lock_irqsave(&m->lock, flags);
1275 if (type == STATUSTYPE_INFO)
1276 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1278 DMEMIT("%u ", m->queue_if_no_path +
1279 (m->pg_init_retries > 0) * 2);
1280 if (m->queue_if_no_path)
1281 DMEMIT("queue_if_no_path ");
1282 if (m->pg_init_retries)
1283 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1286 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1289 DMEMIT("1 %s ", m->hw_handler_name);
1291 DMEMIT("%u ", m->nr_priority_groups);
1294 pg_num = m->next_pg->pg_num;
1295 else if (m->current_pg)
1296 pg_num = m->current_pg->pg_num;
1300 DMEMIT("%u ", pg_num);
1303 case STATUSTYPE_INFO:
1304 list_for_each_entry(pg, &m->priority_groups, list) {
1306 state = 'D'; /* Disabled */
1307 else if (pg == m->current_pg)
1308 state = 'A'; /* Currently Active */
1310 state = 'E'; /* Enabled */
1312 DMEMIT("%c ", state);
1314 if (pg->ps.type->status)
1315 sz += pg->ps.type->status(&pg->ps, NULL, type,
1321 DMEMIT("%u %u ", pg->nr_pgpaths,
1322 pg->ps.type->info_args);
1324 list_for_each_entry(p, &pg->pgpaths, list) {
1325 DMEMIT("%s %s %u ", p->path.dev->name,
1326 p->is_active ? "A" : "F",
1328 if (pg->ps.type->status)
1329 sz += pg->ps.type->status(&pg->ps,
1330 &p->path, type, result + sz,
1336 case STATUSTYPE_TABLE:
1337 list_for_each_entry(pg, &m->priority_groups, list) {
1338 DMEMIT("%s ", pg->ps.type->name);
1340 if (pg->ps.type->status)
1341 sz += pg->ps.type->status(&pg->ps, NULL, type,
1347 DMEMIT("%u %u ", pg->nr_pgpaths,
1348 pg->ps.type->table_args);
1350 list_for_each_entry(p, &pg->pgpaths, list) {
1351 DMEMIT("%s ", p->path.dev->name);
1352 if (pg->ps.type->status)
1353 sz += pg->ps.type->status(&pg->ps,
1354 &p->path, type, result + sz,
1361 spin_unlock_irqrestore(&m->lock, flags);
1366 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1370 struct multipath *m = (struct multipath *) ti->private;
1374 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1375 return queue_if_no_path(m, 1, 0);
1376 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1377 return queue_if_no_path(m, 0, 0);
1383 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1384 return bypass_pg_num(m, argv[1], 1);
1385 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1386 return bypass_pg_num(m, argv[1], 0);
1387 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1388 return switch_pg_num(m, argv[1]);
1389 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1390 action = reinstate_path;
1391 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1396 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1397 dm_table_get_mode(ti->table), &dev);
1399 DMWARN("message: error getting device %s",
1404 r = action_dev(m, dev, action);
1406 dm_put_device(ti, dev);
1411 DMWARN("Unrecognised multipath message received.");
1415 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1418 struct multipath *m = (struct multipath *) ti->private;
1419 struct block_device *bdev = NULL;
1421 unsigned long flags;
1424 spin_lock_irqsave(&m->lock, flags);
1426 if (!m->current_pgpath)
1427 __choose_pgpath(m, 0);
1429 if (m->current_pgpath) {
1430 bdev = m->current_pgpath->path.dev->bdev;
1431 mode = m->current_pgpath->path.dev->mode;
1439 spin_unlock_irqrestore(&m->lock, flags);
1441 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1444 static int multipath_iterate_devices(struct dm_target *ti,
1445 iterate_devices_callout_fn fn, void *data)
1447 struct multipath *m = ti->private;
1448 struct priority_group *pg;
1452 list_for_each_entry(pg, &m->priority_groups, list) {
1453 list_for_each_entry(p, &pg->pgpaths, list) {
1454 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1464 static int __pgpath_busy(struct pgpath *pgpath)
1466 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1468 return dm_underlying_device_busy(q);
1472 * We return "busy", only when we can map I/Os but underlying devices
1473 * are busy (so even if we map I/Os now, the I/Os will wait on
1474 * the underlying queue).
1475 * In other words, if we want to kill I/Os or queue them inside us
1476 * due to map unavailability, we don't return "busy". Otherwise,
1477 * dm core won't give us the I/Os and we can't do what we want.
1479 static int multipath_busy(struct dm_target *ti)
1481 int busy = 0, has_active = 0;
1482 struct multipath *m = ti->private;
1483 struct priority_group *pg;
1484 struct pgpath *pgpath;
1485 unsigned long flags;
1487 spin_lock_irqsave(&m->lock, flags);
1489 /* Guess which priority_group will be used at next mapping time */
1490 if (unlikely(!m->current_pgpath && m->next_pg))
1492 else if (likely(m->current_pg))
1496 * We don't know which pg will be used at next mapping time.
1497 * We don't call __choose_pgpath() here to avoid to trigger
1498 * pg_init just by busy checking.
1499 * So we don't know whether underlying devices we will be using
1500 * at next mapping time are busy or not. Just try mapping.
1505 * If there is one non-busy active path at least, the path selector
1506 * will be able to select it. So we consider such a pg as not busy.
1509 list_for_each_entry(pgpath, &pg->pgpaths, list)
1510 if (pgpath->is_active) {
1513 if (!__pgpath_busy(pgpath)) {
1521 * No active path in this pg, so this pg won't be used and
1522 * the current_pg will be changed at next mapping time.
1523 * We need to try mapping to determine it.
1528 spin_unlock_irqrestore(&m->lock, flags);
1533 /*-----------------------------------------------------------------
1535 *---------------------------------------------------------------*/
1536 static struct target_type multipath_target = {
1537 .name = "multipath",
1538 .version = {1, 1, 0},
1539 .module = THIS_MODULE,
1540 .ctr = multipath_ctr,
1541 .dtr = multipath_dtr,
1542 .map_rq = multipath_map,
1543 .rq_end_io = multipath_end_io,
1544 .presuspend = multipath_presuspend,
1545 .resume = multipath_resume,
1546 .status = multipath_status,
1547 .message = multipath_message,
1548 .ioctl = multipath_ioctl,
1549 .iterate_devices = multipath_iterate_devices,
1550 .busy = multipath_busy,
1553 static int __init dm_multipath_init(void)
1557 /* allocate a slab for the dm_ios */
1558 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1562 r = dm_register_target(&multipath_target);
1564 DMERR("register failed %d", r);
1565 kmem_cache_destroy(_mpio_cache);
1569 kmultipathd = create_workqueue("kmpathd");
1571 DMERR("failed to create workqueue kmpathd");
1572 dm_unregister_target(&multipath_target);
1573 kmem_cache_destroy(_mpio_cache);
1578 * A separate workqueue is used to handle the device handlers
1579 * to avoid overloading existing workqueue. Overloading the
1580 * old workqueue would also create a bottleneck in the
1581 * path of the storage hardware device activation.
1583 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1584 if (!kmpath_handlerd) {
1585 DMERR("failed to create workqueue kmpath_handlerd");
1586 destroy_workqueue(kmultipathd);
1587 dm_unregister_target(&multipath_target);
1588 kmem_cache_destroy(_mpio_cache);
1592 DMINFO("version %u.%u.%u loaded",
1593 multipath_target.version[0], multipath_target.version[1],
1594 multipath_target.version[2]);
1599 static void __exit dm_multipath_exit(void)
1601 destroy_workqueue(kmpath_handlerd);
1602 destroy_workqueue(kmultipathd);
1604 dm_unregister_target(&multipath_target);
1605 kmem_cache_destroy(_mpio_cache);
1608 module_init(dm_multipath_init);
1609 module_exit(dm_multipath_exit);
1611 MODULE_DESCRIPTION(DM_NAME " multipath target");
1612 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1613 MODULE_LICENSE("GPL");