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-bio-record.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 <scsi/scsi_dh.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define MESG_STR(x) x, sizeof(x)
30 struct list_head list;
32 struct priority_group *pg; /* Owning PG */
33 unsigned is_active; /* Path status */
34 unsigned fail_count; /* Cumulative failure count */
37 struct work_struct deactivate_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 struct work_struct activate_path;
68 struct pgpath *pgpath_to_activate;
69 unsigned nr_priority_groups;
70 struct list_head priority_groups;
71 unsigned pg_init_required; /* pg_init needs calling? */
72 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
74 unsigned nr_valid_paths; /* Total number of usable paths */
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg; /* Switch to this PG if set */
78 unsigned repeat_count; /* I/Os left before calling PS again */
80 unsigned queue_io; /* Must we queue all I/O? */
81 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
82 unsigned saved_queue_if_no_path;/* Saved state during suspension */
83 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_count; /* Number of times pg_init called */
86 struct work_struct process_queued_ios;
87 struct bio_list queued_ios;
90 struct work_struct trigger_event;
93 * We must use a mempool of dm_mpath_io structs so that we
94 * can resubmit bios on error.
100 * Context information attached to each bio we process.
103 struct pgpath *pgpath;
104 struct dm_bio_details details;
107 typedef int (*action_fn) (struct pgpath *pgpath);
109 #define MIN_IOS 256 /* Mempool size */
111 static struct kmem_cache *_mpio_cache;
113 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
114 static void process_queued_ios(struct work_struct *work);
115 static void trigger_event(struct work_struct *work);
116 static void activate_path(struct work_struct *work);
117 static void deactivate_path(struct work_struct *work);
120 /*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
124 static struct pgpath *alloc_pgpath(void)
126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
129 pgpath->is_active = 1;
130 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
136 static void free_pgpath(struct pgpath *pgpath)
141 static void deactivate_path(struct work_struct *work)
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
149 static struct priority_group *alloc_priority_group(void)
151 struct priority_group *pg;
153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
156 INIT_LIST_HEAD(&pg->pgpaths);
161 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);
172 spin_lock_irqsave(&m->lock, flags);
173 if (m->pgpath_to_activate == pgpath)
174 m->pgpath_to_activate = NULL;
175 spin_unlock_irqrestore(&m->lock, flags);
180 static void free_priority_group(struct priority_group *pg,
181 struct dm_target *ti)
183 struct path_selector *ps = &pg->ps;
186 ps->type->destroy(ps);
187 dm_put_path_selector(ps->type);
190 free_pgpaths(&pg->pgpaths, ti);
194 static struct multipath *alloc_multipath(struct dm_target *ti)
198 m = kzalloc(sizeof(*m), GFP_KERNEL);
200 INIT_LIST_HEAD(&m->priority_groups);
201 spin_lock_init(&m->lock);
203 INIT_WORK(&m->process_queued_ios, process_queued_ios);
204 INIT_WORK(&m->trigger_event, trigger_event);
205 INIT_WORK(&m->activate_path, activate_path);
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 mempool_destroy(m->mpio_pool);
233 /*-----------------------------------------------
235 *-----------------------------------------------*/
237 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
239 m->current_pg = pgpath->pg;
241 /* Must we initialise the PG first, and queue I/O till it's ready? */
242 if (m->hw_handler_name) {
243 m->pg_init_required = 1;
246 m->pg_init_required = 0;
250 m->pg_init_count = 0;
253 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);
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)
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))
285 /* Don't change PG until it has no remaining paths */
286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
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))
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 bio *bio,
326 struct dm_mpath_io *mpio, unsigned was_queued)
328 int r = DM_MAPIO_REMAPPED;
330 struct pgpath *pgpath;
332 spin_lock_irqsave(&m->lock, flags);
334 /* Do we need to select a new pgpath? */
335 if (!m->current_pgpath ||
336 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
339 pgpath = m->current_pgpath;
344 if ((pgpath && m->queue_io) ||
345 (!pgpath && m->queue_if_no_path)) {
346 /* Queue for the daemon to resubmit */
347 bio_list_add(&m->queued_ios, bio);
349 if ((m->pg_init_required && !m->pg_init_in_progress) ||
351 queue_work(kmultipathd, &m->process_queued_ios);
353 r = DM_MAPIO_SUBMITTED;
355 bio->bi_bdev = pgpath->path.dev->bdev;
356 else if (__must_push_back(m))
357 r = DM_MAPIO_REQUEUE;
359 r = -EIO; /* Failed */
361 mpio->pgpath = pgpath;
363 spin_unlock_irqrestore(&m->lock, flags);
369 * If we run out of usable paths, should we queue I/O or error it?
371 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
372 unsigned save_old_value)
376 spin_lock_irqsave(&m->lock, flags);
379 m->saved_queue_if_no_path = m->queue_if_no_path;
381 m->saved_queue_if_no_path = queue_if_no_path;
382 m->queue_if_no_path = queue_if_no_path;
383 if (!m->queue_if_no_path && m->queue_size)
384 queue_work(kmultipathd, &m->process_queued_ios);
386 spin_unlock_irqrestore(&m->lock, flags);
391 /*-----------------------------------------------------------------
392 * The multipath daemon is responsible for resubmitting queued ios.
393 *---------------------------------------------------------------*/
395 static void dispatch_queued_ios(struct multipath *m)
399 struct bio *bio = NULL, *next;
400 struct dm_mpath_io *mpio;
401 union map_info *info;
403 spin_lock_irqsave(&m->lock, flags);
404 bio = bio_list_get(&m->queued_ios);
405 spin_unlock_irqrestore(&m->lock, flags);
411 info = dm_get_mapinfo(bio);
414 r = map_io(m, bio, mpio, 1);
417 else if (r == DM_MAPIO_REMAPPED)
418 generic_make_request(bio);
419 else if (r == DM_MAPIO_REQUEUE)
420 bio_endio(bio, -EIO);
426 static void process_queued_ios(struct work_struct *work)
428 struct multipath *m =
429 container_of(work, struct multipath, process_queued_ios);
430 struct pgpath *pgpath = NULL;
431 unsigned init_required = 0, must_queue = 1;
434 spin_lock_irqsave(&m->lock, flags);
439 if (!m->current_pgpath)
442 pgpath = m->current_pgpath;
444 if ((pgpath && !m->queue_io) ||
445 (!pgpath && !m->queue_if_no_path))
448 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
449 m->pgpath_to_activate = pgpath;
451 m->pg_init_required = 0;
452 m->pg_init_in_progress = 1;
457 spin_unlock_irqrestore(&m->lock, flags);
460 queue_work(kmpath_handlerd, &m->activate_path);
463 dispatch_queued_ios(m);
467 * An event is triggered whenever a path is taken out of use.
468 * Includes path failure and PG bypass.
470 static void trigger_event(struct work_struct *work)
472 struct multipath *m =
473 container_of(work, struct multipath, trigger_event);
475 dm_table_event(m->ti->table);
478 /*-----------------------------------------------------------------
479 * Constructor/argument parsing:
480 * <#multipath feature args> [<arg>]*
481 * <#hw_handler args> [hw_handler [<arg>]*]
483 * <initial priority group>
484 * [<selector> <#selector args> [<arg>]*
485 * <#paths> <#per-path selector args>
486 * [<path> [<arg>]* ]+ ]+
487 *---------------------------------------------------------------*/
494 static int read_param(struct param *param, char *str, unsigned *v, char **error)
497 (sscanf(str, "%u", v) != 1) ||
500 *error = param->error;
512 static char *shift(struct arg_set *as)
526 static void consume(struct arg_set *as, unsigned n)
528 BUG_ON (as->argc < n);
533 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
534 struct dm_target *ti)
537 struct path_selector_type *pst;
540 static struct param _params[] = {
541 {0, 1024, "invalid number of path selector args"},
544 pst = dm_get_path_selector(shift(as));
546 ti->error = "unknown path selector type";
550 r = read_param(_params, shift(as), &ps_argc, &ti->error);
552 dm_put_path_selector(pst);
556 if (ps_argc > as->argc) {
557 dm_put_path_selector(pst);
558 ti->error = "not enough arguments for path selector";
562 r = pst->create(&pg->ps, ps_argc, as->argv);
564 dm_put_path_selector(pst);
565 ti->error = "path selector constructor failed";
570 consume(as, ps_argc);
575 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
576 struct dm_target *ti)
580 struct multipath *m = ti->private;
582 /* we need at least a path arg */
584 ti->error = "no device given";
585 return ERR_PTR(-EINVAL);
590 return ERR_PTR(-ENOMEM);
592 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
593 dm_table_get_mode(ti->table), &p->path.dev);
595 ti->error = "error getting device";
599 if (m->hw_handler_name) {
600 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
603 dm_put_device(ti, p->path.dev);
608 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
610 dm_put_device(ti, p->path.dev);
621 static struct priority_group *parse_priority_group(struct arg_set *as,
624 static struct param _params[] = {
625 {1, 1024, "invalid number of paths"},
626 {0, 1024, "invalid number of selector args"}
630 unsigned i, nr_selector_args, nr_params;
631 struct priority_group *pg;
632 struct dm_target *ti = m->ti;
636 ti->error = "not enough priority group arguments";
637 return ERR_PTR(-EINVAL);
640 pg = alloc_priority_group();
642 ti->error = "couldn't allocate priority group";
643 return ERR_PTR(-ENOMEM);
647 r = parse_path_selector(as, pg, ti);
654 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
658 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
662 nr_params = 1 + nr_selector_args;
663 for (i = 0; i < pg->nr_pgpaths; i++) {
664 struct pgpath *pgpath;
665 struct arg_set path_args;
667 if (as->argc < nr_params) {
668 ti->error = "not enough path parameters";
672 path_args.argc = nr_params;
673 path_args.argv = as->argv;
675 pgpath = parse_path(&path_args, &pg->ps, ti);
676 if (IS_ERR(pgpath)) {
682 list_add_tail(&pgpath->list, &pg->pgpaths);
683 consume(as, nr_params);
689 free_priority_group(pg, ti);
693 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
696 struct dm_target *ti = m->ti;
698 static struct param _params[] = {
699 {0, 1024, "invalid number of hardware handler args"},
702 if (read_param(_params, shift(as), &hw_argc, &ti->error))
708 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
709 request_module("scsi_dh_%s", m->hw_handler_name);
710 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
711 ti->error = "unknown hardware handler type";
712 kfree(m->hw_handler_name);
713 m->hw_handler_name = NULL;
718 DMWARN("Ignoring user-specified arguments for "
719 "hardware handler \"%s\"", m->hw_handler_name);
720 consume(as, hw_argc - 1);
725 static int parse_features(struct arg_set *as, struct multipath *m)
729 struct dm_target *ti = m->ti;
730 const char *param_name;
732 static struct param _params[] = {
733 {0, 3, "invalid number of feature args"},
734 {1, 50, "pg_init_retries must be between 1 and 50"},
737 r = read_param(_params, shift(as), &argc, &ti->error);
745 param_name = shift(as);
748 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
749 r = queue_if_no_path(m, 1, 0);
753 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
755 r = read_param(_params + 1, shift(as),
756 &m->pg_init_retries, &ti->error);
761 ti->error = "Unrecognised multipath feature request";
763 } while (argc && !r);
768 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
771 /* target parameters */
772 static struct param _params[] = {
773 {1, 1024, "invalid number of priority groups"},
774 {1, 1024, "invalid initial priority group number"},
780 unsigned pg_count = 0;
781 unsigned next_pg_num;
786 m = alloc_multipath(ti);
788 ti->error = "can't allocate multipath";
792 r = parse_features(&as, m);
796 r = parse_hw_handler(&as, m);
800 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
804 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
808 /* parse the priority groups */
810 struct priority_group *pg;
812 pg = parse_priority_group(&as, m);
818 m->nr_valid_paths += pg->nr_pgpaths;
819 list_add_tail(&pg->list, &m->priority_groups);
821 pg->pg_num = pg_count;
826 if (pg_count != m->nr_priority_groups) {
827 ti->error = "priority group count mismatch";
839 static void multipath_dtr(struct dm_target *ti)
841 struct multipath *m = (struct multipath *) ti->private;
843 flush_workqueue(kmpath_handlerd);
844 flush_workqueue(kmultipathd);
849 * Map bios, recording original fields for later in case we have to resubmit
851 static int multipath_map(struct dm_target *ti, struct bio *bio,
852 union map_info *map_context)
855 struct dm_mpath_io *mpio;
856 struct multipath *m = (struct multipath *) ti->private;
858 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
859 dm_bio_record(&mpio->details, bio);
861 map_context->ptr = mpio;
862 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
863 r = map_io(m, bio, mpio, 0);
864 if (r < 0 || r == DM_MAPIO_REQUEUE)
865 mempool_free(mpio, m->mpio_pool);
871 * Take a path out of use.
873 static int fail_path(struct pgpath *pgpath)
876 struct multipath *m = pgpath->pg->m;
878 spin_lock_irqsave(&m->lock, flags);
880 if (!pgpath->is_active)
883 DMWARN("Failing path %s.", pgpath->path.dev->name);
885 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
886 pgpath->is_active = 0;
887 pgpath->fail_count++;
891 if (pgpath == m->current_pgpath)
892 m->current_pgpath = NULL;
894 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
895 pgpath->path.dev->name, m->nr_valid_paths);
897 schedule_work(&m->trigger_event);
898 queue_work(kmultipathd, &pgpath->deactivate_path);
901 spin_unlock_irqrestore(&m->lock, flags);
907 * Reinstate a previously-failed path
909 static int reinstate_path(struct pgpath *pgpath)
913 struct multipath *m = pgpath->pg->m;
915 spin_lock_irqsave(&m->lock, flags);
917 if (pgpath->is_active)
920 if (!pgpath->pg->ps.type->reinstate_path) {
921 DMWARN("Reinstate path not supported by path selector %s",
922 pgpath->pg->ps.type->name);
927 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
931 pgpath->is_active = 1;
933 m->current_pgpath = NULL;
934 if (!m->nr_valid_paths++ && m->queue_size)
935 queue_work(kmultipathd, &m->process_queued_ios);
937 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
938 pgpath->path.dev->name, m->nr_valid_paths);
940 schedule_work(&m->trigger_event);
943 spin_unlock_irqrestore(&m->lock, flags);
949 * Fail or reinstate all paths that match the provided struct dm_dev.
951 static int action_dev(struct multipath *m, struct dm_dev *dev,
955 struct pgpath *pgpath;
956 struct priority_group *pg;
958 list_for_each_entry(pg, &m->priority_groups, list) {
959 list_for_each_entry(pgpath, &pg->pgpaths, list) {
960 if (pgpath->path.dev == dev)
969 * Temporarily try to avoid having to use the specified PG
971 static void bypass_pg(struct multipath *m, struct priority_group *pg,
976 spin_lock_irqsave(&m->lock, flags);
978 pg->bypassed = bypassed;
979 m->current_pgpath = NULL;
980 m->current_pg = NULL;
982 spin_unlock_irqrestore(&m->lock, flags);
984 schedule_work(&m->trigger_event);
988 * Switch to using the specified PG from the next I/O that gets mapped
990 static int switch_pg_num(struct multipath *m, const char *pgstr)
992 struct priority_group *pg;
996 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
997 (pgnum > m->nr_priority_groups)) {
998 DMWARN("invalid PG number supplied to switch_pg_num");
1002 spin_lock_irqsave(&m->lock, flags);
1003 list_for_each_entry(pg, &m->priority_groups, list) {
1008 m->current_pgpath = NULL;
1009 m->current_pg = NULL;
1012 spin_unlock_irqrestore(&m->lock, flags);
1014 schedule_work(&m->trigger_event);
1019 * Set/clear bypassed status of a PG.
1020 * PGs are numbered upwards from 1 in the order they were declared.
1022 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1024 struct priority_group *pg;
1027 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1028 (pgnum > m->nr_priority_groups)) {
1029 DMWARN("invalid PG number supplied to bypass_pg");
1033 list_for_each_entry(pg, &m->priority_groups, list) {
1038 bypass_pg(m, pg, bypassed);
1043 * Should we retry pg_init immediately?
1045 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1047 unsigned long flags;
1048 int limit_reached = 0;
1050 spin_lock_irqsave(&m->lock, flags);
1052 if (m->pg_init_count <= m->pg_init_retries)
1053 m->pg_init_required = 1;
1057 spin_unlock_irqrestore(&m->lock, flags);
1059 return limit_reached;
1062 static void pg_init_done(struct dm_path *path, int errors)
1064 struct pgpath *pgpath = path_to_pgpath(path);
1065 struct priority_group *pg = pgpath->pg;
1066 struct multipath *m = pg->m;
1067 unsigned long flags;
1069 /* device or driver problems */
1074 if (!m->hw_handler_name) {
1078 DMERR("Cannot failover device because scsi_dh_%s was not "
1079 "loaded.", m->hw_handler_name);
1081 * Fail path for now, so we do not ping pong
1085 case SCSI_DH_DEV_TEMP_BUSY:
1087 * Probably doing something like FW upgrade on the
1088 * controller so try the other pg.
1090 bypass_pg(m, pg, 1);
1092 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1094 case SCSI_DH_IMM_RETRY:
1095 case SCSI_DH_RES_TEMP_UNAVAIL:
1096 if (pg_init_limit_reached(m, pgpath))
1102 * We probably do not want to fail the path for a device
1103 * error, but this is what the old dm did. In future
1104 * patches we can do more advanced handling.
1109 spin_lock_irqsave(&m->lock, flags);
1111 DMERR("Could not failover device. Error %d.", errors);
1112 m->current_pgpath = NULL;
1113 m->current_pg = NULL;
1114 } else if (!m->pg_init_required) {
1119 m->pg_init_in_progress = 0;
1120 queue_work(kmultipathd, &m->process_queued_ios);
1121 spin_unlock_irqrestore(&m->lock, flags);
1124 static void activate_path(struct work_struct *work)
1127 struct multipath *m =
1128 container_of(work, struct multipath, activate_path);
1129 struct dm_path *path;
1130 unsigned long flags;
1132 spin_lock_irqsave(&m->lock, flags);
1133 path = &m->pgpath_to_activate->path;
1134 m->pgpath_to_activate = NULL;
1135 spin_unlock_irqrestore(&m->lock, flags);
1138 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1139 pg_init_done(path, ret);
1145 static int do_end_io(struct multipath *m, struct bio *bio,
1146 int error, struct dm_mpath_io *mpio)
1148 unsigned long flags;
1151 return 0; /* I/O complete */
1153 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1156 if (error == -EOPNOTSUPP)
1159 spin_lock_irqsave(&m->lock, flags);
1160 if (!m->nr_valid_paths) {
1161 if (__must_push_back(m)) {
1162 spin_unlock_irqrestore(&m->lock, flags);
1163 return DM_ENDIO_REQUEUE;
1164 } else if (!m->queue_if_no_path) {
1165 spin_unlock_irqrestore(&m->lock, flags);
1168 spin_unlock_irqrestore(&m->lock, flags);
1172 spin_unlock_irqrestore(&m->lock, flags);
1175 fail_path(mpio->pgpath);
1178 dm_bio_restore(&mpio->details, bio);
1180 /* queue for the daemon to resubmit or fail */
1181 spin_lock_irqsave(&m->lock, flags);
1182 bio_list_add(&m->queued_ios, bio);
1185 queue_work(kmultipathd, &m->process_queued_ios);
1186 spin_unlock_irqrestore(&m->lock, flags);
1188 return DM_ENDIO_INCOMPLETE; /* io not complete */
1191 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1192 int error, union map_info *map_context)
1194 struct multipath *m = ti->private;
1195 struct dm_mpath_io *mpio = map_context->ptr;
1196 struct pgpath *pgpath = mpio->pgpath;
1197 struct path_selector *ps;
1200 r = do_end_io(m, bio, error, mpio);
1202 ps = &pgpath->pg->ps;
1203 if (ps->type->end_io)
1204 ps->type->end_io(ps, &pgpath->path);
1206 if (r != DM_ENDIO_INCOMPLETE)
1207 mempool_free(mpio, m->mpio_pool);
1213 * Suspend can't complete until all the I/O is processed so if
1214 * the last path fails we must error any remaining I/O.
1215 * Note that if the freeze_bdev fails while suspending, the
1216 * queue_if_no_path state is lost - userspace should reset it.
1218 static void multipath_presuspend(struct dm_target *ti)
1220 struct multipath *m = (struct multipath *) ti->private;
1222 queue_if_no_path(m, 0, 1);
1226 * Restore the queue_if_no_path setting.
1228 static void multipath_resume(struct dm_target *ti)
1230 struct multipath *m = (struct multipath *) ti->private;
1231 unsigned long flags;
1233 spin_lock_irqsave(&m->lock, flags);
1234 m->queue_if_no_path = m->saved_queue_if_no_path;
1235 spin_unlock_irqrestore(&m->lock, flags);
1239 * Info output has the following format:
1240 * num_multipath_feature_args [multipath_feature_args]*
1241 * num_handler_status_args [handler_status_args]*
1242 * num_groups init_group_number
1243 * [A|D|E num_ps_status_args [ps_status_args]*
1244 * num_paths num_selector_args
1245 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1247 * Table output has the following format (identical to the constructor string):
1248 * num_feature_args [features_args]*
1249 * num_handler_args hw_handler [hw_handler_args]*
1250 * num_groups init_group_number
1251 * [priority selector-name num_ps_args [ps_args]*
1252 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1254 static int multipath_status(struct dm_target *ti, status_type_t type,
1255 char *result, unsigned int maxlen)
1258 unsigned long flags;
1259 struct multipath *m = (struct multipath *) ti->private;
1260 struct priority_group *pg;
1265 spin_lock_irqsave(&m->lock, flags);
1268 if (type == STATUSTYPE_INFO)
1269 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1271 DMEMIT("%u ", m->queue_if_no_path +
1272 (m->pg_init_retries > 0) * 2);
1273 if (m->queue_if_no_path)
1274 DMEMIT("queue_if_no_path ");
1275 if (m->pg_init_retries)
1276 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1279 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1282 DMEMIT("1 %s ", m->hw_handler_name);
1284 DMEMIT("%u ", m->nr_priority_groups);
1287 pg_num = m->next_pg->pg_num;
1288 else if (m->current_pg)
1289 pg_num = m->current_pg->pg_num;
1293 DMEMIT("%u ", pg_num);
1296 case STATUSTYPE_INFO:
1297 list_for_each_entry(pg, &m->priority_groups, list) {
1299 state = 'D'; /* Disabled */
1300 else if (pg == m->current_pg)
1301 state = 'A'; /* Currently Active */
1303 state = 'E'; /* Enabled */
1305 DMEMIT("%c ", state);
1307 if (pg->ps.type->status)
1308 sz += pg->ps.type->status(&pg->ps, NULL, type,
1314 DMEMIT("%u %u ", pg->nr_pgpaths,
1315 pg->ps.type->info_args);
1317 list_for_each_entry(p, &pg->pgpaths, list) {
1318 DMEMIT("%s %s %u ", p->path.dev->name,
1319 p->is_active ? "A" : "F",
1321 if (pg->ps.type->status)
1322 sz += pg->ps.type->status(&pg->ps,
1323 &p->path, type, result + sz,
1329 case STATUSTYPE_TABLE:
1330 list_for_each_entry(pg, &m->priority_groups, list) {
1331 DMEMIT("%s ", pg->ps.type->name);
1333 if (pg->ps.type->status)
1334 sz += pg->ps.type->status(&pg->ps, NULL, type,
1340 DMEMIT("%u %u ", pg->nr_pgpaths,
1341 pg->ps.type->table_args);
1343 list_for_each_entry(p, &pg->pgpaths, list) {
1344 DMEMIT("%s ", p->path.dev->name);
1345 if (pg->ps.type->status)
1346 sz += pg->ps.type->status(&pg->ps,
1347 &p->path, type, result + sz,
1354 spin_unlock_irqrestore(&m->lock, flags);
1359 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1363 struct multipath *m = (struct multipath *) ti->private;
1367 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1368 return queue_if_no_path(m, 1, 0);
1369 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1370 return queue_if_no_path(m, 0, 0);
1376 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1377 return bypass_pg_num(m, argv[1], 1);
1378 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1379 return bypass_pg_num(m, argv[1], 0);
1380 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1381 return switch_pg_num(m, argv[1]);
1382 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1383 action = reinstate_path;
1384 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1389 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1390 dm_table_get_mode(ti->table), &dev);
1392 DMWARN("message: error getting device %s",
1397 r = action_dev(m, dev, action);
1399 dm_put_device(ti, dev);
1404 DMWARN("Unrecognised multipath message received.");
1408 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1411 struct multipath *m = (struct multipath *) ti->private;
1412 struct block_device *bdev = NULL;
1414 unsigned long flags;
1417 spin_lock_irqsave(&m->lock, flags);
1419 if (!m->current_pgpath)
1422 if (m->current_pgpath) {
1423 bdev = m->current_pgpath->path.dev->bdev;
1424 mode = m->current_pgpath->path.dev->mode;
1432 spin_unlock_irqrestore(&m->lock, flags);
1434 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1437 /*-----------------------------------------------------------------
1439 *---------------------------------------------------------------*/
1440 static struct target_type multipath_target = {
1441 .name = "multipath",
1442 .version = {1, 0, 5},
1443 .module = THIS_MODULE,
1444 .ctr = multipath_ctr,
1445 .dtr = multipath_dtr,
1446 .map = multipath_map,
1447 .end_io = multipath_end_io,
1448 .presuspend = multipath_presuspend,
1449 .resume = multipath_resume,
1450 .status = multipath_status,
1451 .message = multipath_message,
1452 .ioctl = multipath_ioctl,
1455 static int __init dm_multipath_init(void)
1459 /* allocate a slab for the dm_ios */
1460 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1464 r = dm_register_target(&multipath_target);
1466 DMERR("register failed %d", r);
1467 kmem_cache_destroy(_mpio_cache);
1471 kmultipathd = create_workqueue("kmpathd");
1473 DMERR("failed to create workqueue kmpathd");
1474 dm_unregister_target(&multipath_target);
1475 kmem_cache_destroy(_mpio_cache);
1480 * A separate workqueue is used to handle the device handlers
1481 * to avoid overloading existing workqueue. Overloading the
1482 * old workqueue would also create a bottleneck in the
1483 * path of the storage hardware device activation.
1485 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1486 if (!kmpath_handlerd) {
1487 DMERR("failed to create workqueue kmpath_handlerd");
1488 destroy_workqueue(kmultipathd);
1489 dm_unregister_target(&multipath_target);
1490 kmem_cache_destroy(_mpio_cache);
1494 DMINFO("version %u.%u.%u loaded",
1495 multipath_target.version[0], multipath_target.version[1],
1496 multipath_target.version[2]);
1501 static void __exit dm_multipath_exit(void)
1503 destroy_workqueue(kmpath_handlerd);
1504 destroy_workqueue(kmultipathd);
1506 dm_unregister_target(&multipath_target);
1507 kmem_cache_destroy(_mpio_cache);
1510 module_init(dm_multipath_init);
1511 module_exit(dm_multipath_exit);
1513 MODULE_DESCRIPTION(DM_NAME " multipath target");
1514 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1515 MODULE_LICENSE("GPL");