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-list.h"
12 #include "dm-bio-record.h"
13 #include "dm-uevent.h"
15 #include <linux/ctype.h>
16 #include <linux/init.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/slab.h>
21 #include <linux/time.h>
22 #include <linux/workqueue.h>
23 #include <scsi/scsi_dh.h>
24 #include <asm/atomic.h>
26 #define DM_MSG_PREFIX "multipath"
27 #define MESG_STR(x) x, sizeof(x)
31 struct list_head list;
33 struct priority_group *pg; /* Owning PG */
34 unsigned is_active; /* Path status */
35 unsigned fail_count; /* Cumulative failure count */
38 struct work_struct deactivate_path;
41 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
47 struct priority_group {
48 struct list_head list;
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
60 /* Multipath context */
62 struct list_head list;
67 const char *hw_handler_name;
68 struct work_struct activate_path;
69 struct pgpath *pgpath_to_activate;
70 unsigned nr_priority_groups;
71 struct list_head priority_groups;
72 unsigned pg_init_required; /* pg_init needs calling? */
73 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
75 unsigned nr_valid_paths; /* Total number of usable paths */
76 struct pgpath *current_pgpath;
77 struct priority_group *current_pg;
78 struct priority_group *next_pg; /* Switch to this PG if set */
79 unsigned repeat_count; /* I/Os left before calling PS again */
81 unsigned queue_io; /* Must we queue all I/O? */
82 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
83 unsigned saved_queue_if_no_path;/* Saved state during suspension */
84 unsigned pg_init_retries; /* Number of times to retry pg_init */
85 unsigned pg_init_count; /* Number of times pg_init called */
87 struct work_struct process_queued_ios;
88 struct bio_list queued_ios;
91 struct work_struct trigger_event;
94 * We must use a mempool of dm_mpath_io structs so that we
95 * can resubmit bios on error.
101 * Context information attached to each bio we process.
104 struct pgpath *pgpath;
105 struct dm_bio_details details;
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);
137 static void free_pgpath(struct pgpath *pgpath)
142 static void deactivate_path(struct work_struct *work)
144 struct pgpath *pgpath =
145 container_of(work, struct pgpath, deactivate_path);
147 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
150 static struct priority_group *alloc_priority_group(void)
152 struct priority_group *pg;
154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
157 INIT_LIST_HEAD(&pg->pgpaths);
162 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
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);
173 spin_lock_irqsave(&m->lock, flags);
174 if (m->pgpath_to_activate == pgpath)
175 m->pgpath_to_activate = NULL;
176 spin_unlock_irqrestore(&m->lock, flags);
181 static void free_priority_group(struct priority_group *pg,
182 struct dm_target *ti)
184 struct path_selector *ps = &pg->ps;
187 ps->type->destroy(ps);
188 dm_put_path_selector(ps->type);
191 free_pgpaths(&pg->pgpaths, ti);
195 static struct multipath *alloc_multipath(struct dm_target *ti)
199 m = kzalloc(sizeof(*m), GFP_KERNEL);
201 INIT_LIST_HEAD(&m->priority_groups);
202 spin_lock_init(&m->lock);
204 INIT_WORK(&m->process_queued_ios, process_queued_ios);
205 INIT_WORK(&m->trigger_event, trigger_event);
206 INIT_WORK(&m->activate_path, activate_path);
207 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
219 static void free_multipath(struct multipath *m)
221 struct priority_group *pg, *tmp;
223 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
225 free_priority_group(pg, m->ti);
228 kfree(m->hw_handler_name);
229 mempool_destroy(m->mpio_pool);
234 /*-----------------------------------------------
236 *-----------------------------------------------*/
238 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
240 m->current_pg = pgpath->pg;
242 /* Must we initialise the PG first, and queue I/O till it's ready? */
243 if (m->hw_handler_name) {
244 m->pg_init_required = 1;
247 m->pg_init_required = 0;
251 m->pg_init_count = 0;
254 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
256 struct dm_path *path;
258 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
262 m->current_pgpath = path_to_pgpath(path);
264 if (m->current_pg != pg)
265 __switch_pg(m, m->current_pgpath);
270 static void __choose_pgpath(struct multipath *m)
272 struct priority_group *pg;
273 unsigned bypassed = 1;
275 if (!m->nr_valid_paths)
278 /* Were we instructed to switch PG? */
282 if (!__choose_path_in_pg(m, pg))
286 /* Don't change PG until it has no remaining paths */
287 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
291 * Loop through priority groups until we find a valid path.
292 * First time we skip PGs marked 'bypassed'.
293 * Second time we only try the ones we skipped.
296 list_for_each_entry(pg, &m->priority_groups, list) {
297 if (pg->bypassed == bypassed)
299 if (!__choose_path_in_pg(m, pg))
302 } while (bypassed--);
305 m->current_pgpath = NULL;
306 m->current_pg = NULL;
310 * Check whether bios must be queued in the device-mapper core rather
311 * than here in the target.
313 * m->lock must be held on entry.
315 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
316 * same value then we are not between multipath_presuspend()
317 * and multipath_resume() calls and we have no need to check
318 * for the DMF_NOFLUSH_SUSPENDING flag.
320 static int __must_push_back(struct multipath *m)
322 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
323 dm_noflush_suspending(m->ti));
326 static int map_io(struct multipath *m, struct bio *bio,
327 struct dm_mpath_io *mpio, unsigned was_queued)
329 int r = DM_MAPIO_REMAPPED;
331 struct pgpath *pgpath;
333 spin_lock_irqsave(&m->lock, flags);
335 /* Do we need to select a new pgpath? */
336 if (!m->current_pgpath ||
337 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
340 pgpath = m->current_pgpath;
345 if ((pgpath && m->queue_io) ||
346 (!pgpath && m->queue_if_no_path)) {
347 /* Queue for the daemon to resubmit */
348 bio_list_add(&m->queued_ios, bio);
350 if ((m->pg_init_required && !m->pg_init_in_progress) ||
352 queue_work(kmultipathd, &m->process_queued_ios);
354 r = DM_MAPIO_SUBMITTED;
356 bio->bi_bdev = pgpath->path.dev->bdev;
357 else if (__must_push_back(m))
358 r = DM_MAPIO_REQUEUE;
360 r = -EIO; /* Failed */
362 mpio->pgpath = pgpath;
364 spin_unlock_irqrestore(&m->lock, flags);
370 * If we run out of usable paths, should we queue I/O or error it?
372 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
373 unsigned save_old_value)
377 spin_lock_irqsave(&m->lock, flags);
380 m->saved_queue_if_no_path = m->queue_if_no_path;
382 m->saved_queue_if_no_path = queue_if_no_path;
383 m->queue_if_no_path = queue_if_no_path;
384 if (!m->queue_if_no_path && m->queue_size)
385 queue_work(kmultipathd, &m->process_queued_ios);
387 spin_unlock_irqrestore(&m->lock, flags);
392 /*-----------------------------------------------------------------
393 * The multipath daemon is responsible for resubmitting queued ios.
394 *---------------------------------------------------------------*/
396 static void dispatch_queued_ios(struct multipath *m)
400 struct bio *bio = NULL, *next;
401 struct dm_mpath_io *mpio;
402 union map_info *info;
404 spin_lock_irqsave(&m->lock, flags);
405 bio = bio_list_get(&m->queued_ios);
406 spin_unlock_irqrestore(&m->lock, flags);
412 info = dm_get_mapinfo(bio);
415 r = map_io(m, bio, mpio, 1);
418 else if (r == DM_MAPIO_REMAPPED)
419 generic_make_request(bio);
420 else if (r == DM_MAPIO_REQUEUE)
421 bio_endio(bio, -EIO);
427 static void process_queued_ios(struct work_struct *work)
429 struct multipath *m =
430 container_of(work, struct multipath, process_queued_ios);
431 struct pgpath *pgpath = NULL;
432 unsigned init_required = 0, must_queue = 1;
435 spin_lock_irqsave(&m->lock, flags);
440 if (!m->current_pgpath)
443 pgpath = m->current_pgpath;
445 if ((pgpath && !m->queue_io) ||
446 (!pgpath && !m->queue_if_no_path))
449 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
450 m->pgpath_to_activate = pgpath;
452 m->pg_init_required = 0;
453 m->pg_init_in_progress = 1;
458 spin_unlock_irqrestore(&m->lock, flags);
461 queue_work(kmpath_handlerd, &m->activate_path);
464 dispatch_queued_ios(m);
468 * An event is triggered whenever a path is taken out of use.
469 * Includes path failure and PG bypass.
471 static void trigger_event(struct work_struct *work)
473 struct multipath *m =
474 container_of(work, struct multipath, trigger_event);
476 dm_table_event(m->ti->table);
479 /*-----------------------------------------------------------------
480 * Constructor/argument parsing:
481 * <#multipath feature args> [<arg>]*
482 * <#hw_handler args> [hw_handler [<arg>]*]
484 * <initial priority group>
485 * [<selector> <#selector args> [<arg>]*
486 * <#paths> <#per-path selector args>
487 * [<path> [<arg>]* ]+ ]+
488 *---------------------------------------------------------------*/
495 static int read_param(struct param *param, char *str, unsigned *v, char **error)
498 (sscanf(str, "%u", v) != 1) ||
501 *error = param->error;
513 static char *shift(struct arg_set *as)
527 static void consume(struct arg_set *as, unsigned n)
529 BUG_ON (as->argc < n);
534 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
535 struct dm_target *ti)
538 struct path_selector_type *pst;
541 static struct param _params[] = {
542 {0, 1024, "invalid number of path selector args"},
545 pst = dm_get_path_selector(shift(as));
547 ti->error = "unknown path selector type";
551 r = read_param(_params, shift(as), &ps_argc, &ti->error);
553 dm_put_path_selector(pst);
557 r = pst->create(&pg->ps, ps_argc, as->argv);
559 dm_put_path_selector(pst);
560 ti->error = "path selector constructor failed";
565 consume(as, ps_argc);
570 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
571 struct dm_target *ti)
575 struct multipath *m = ti->private;
577 /* we need at least a path arg */
579 ti->error = "no device given";
580 return ERR_PTR(-EINVAL);
585 return ERR_PTR(-ENOMEM);
587 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
588 dm_table_get_mode(ti->table), &p->path.dev);
590 ti->error = "error getting device";
594 if (m->hw_handler_name) {
595 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
598 dm_put_device(ti, p->path.dev);
603 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
605 dm_put_device(ti, p->path.dev);
616 static struct priority_group *parse_priority_group(struct arg_set *as,
619 static struct param _params[] = {
620 {1, 1024, "invalid number of paths"},
621 {0, 1024, "invalid number of selector args"}
625 unsigned i, nr_selector_args, nr_params;
626 struct priority_group *pg;
627 struct dm_target *ti = m->ti;
631 ti->error = "not enough priority group arguments";
632 return ERR_PTR(-EINVAL);
635 pg = alloc_priority_group();
637 ti->error = "couldn't allocate priority group";
638 return ERR_PTR(-ENOMEM);
642 r = parse_path_selector(as, pg, ti);
649 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
653 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
657 nr_params = 1 + nr_selector_args;
658 for (i = 0; i < pg->nr_pgpaths; i++) {
659 struct pgpath *pgpath;
660 struct arg_set path_args;
662 if (as->argc < nr_params) {
663 ti->error = "not enough path parameters";
667 path_args.argc = nr_params;
668 path_args.argv = as->argv;
670 pgpath = parse_path(&path_args, &pg->ps, ti);
671 if (IS_ERR(pgpath)) {
677 list_add_tail(&pgpath->list, &pg->pgpaths);
678 consume(as, nr_params);
684 free_priority_group(pg, ti);
688 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
691 struct dm_target *ti = m->ti;
693 static struct param _params[] = {
694 {0, 1024, "invalid number of hardware handler args"},
697 if (read_param(_params, shift(as), &hw_argc, &ti->error))
703 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
704 request_module("scsi_dh_%s", m->hw_handler_name);
705 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
706 ti->error = "unknown hardware handler type";
707 kfree(m->hw_handler_name);
708 m->hw_handler_name = NULL;
713 DMWARN("Ignoring user-specified arguments for "
714 "hardware handler \"%s\"", m->hw_handler_name);
715 consume(as, hw_argc - 1);
720 static int parse_features(struct arg_set *as, struct multipath *m)
724 struct dm_target *ti = m->ti;
725 const char *param_name;
727 static struct param _params[] = {
728 {0, 3, "invalid number of feature args"},
729 {1, 50, "pg_init_retries must be between 1 and 50"},
732 r = read_param(_params, shift(as), &argc, &ti->error);
740 param_name = shift(as);
743 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
744 r = queue_if_no_path(m, 1, 0);
748 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
750 r = read_param(_params + 1, shift(as),
751 &m->pg_init_retries, &ti->error);
756 ti->error = "Unrecognised multipath feature request";
758 } while (argc && !r);
763 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
766 /* target parameters */
767 static struct param _params[] = {
768 {1, 1024, "invalid number of priority groups"},
769 {1, 1024, "invalid initial priority group number"},
775 unsigned pg_count = 0;
776 unsigned next_pg_num;
781 m = alloc_multipath(ti);
783 ti->error = "can't allocate multipath";
787 r = parse_features(&as, m);
791 r = parse_hw_handler(&as, m);
795 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
799 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
803 /* parse the priority groups */
805 struct priority_group *pg;
807 pg = parse_priority_group(&as, m);
813 m->nr_valid_paths += pg->nr_pgpaths;
814 list_add_tail(&pg->list, &m->priority_groups);
816 pg->pg_num = pg_count;
821 if (pg_count != m->nr_priority_groups) {
822 ti->error = "priority group count mismatch";
834 static void multipath_dtr(struct dm_target *ti)
836 struct multipath *m = (struct multipath *) ti->private;
838 flush_workqueue(kmpath_handlerd);
839 flush_workqueue(kmultipathd);
844 * Map bios, recording original fields for later in case we have to resubmit
846 static int multipath_map(struct dm_target *ti, struct bio *bio,
847 union map_info *map_context)
850 struct dm_mpath_io *mpio;
851 struct multipath *m = (struct multipath *) ti->private;
853 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
854 dm_bio_record(&mpio->details, bio);
856 map_context->ptr = mpio;
857 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
858 r = map_io(m, bio, mpio, 0);
859 if (r < 0 || r == DM_MAPIO_REQUEUE)
860 mempool_free(mpio, m->mpio_pool);
866 * Take a path out of use.
868 static int fail_path(struct pgpath *pgpath)
871 struct multipath *m = pgpath->pg->m;
873 spin_lock_irqsave(&m->lock, flags);
875 if (!pgpath->is_active)
878 DMWARN("Failing path %s.", pgpath->path.dev->name);
880 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
881 pgpath->is_active = 0;
882 pgpath->fail_count++;
886 if (pgpath == m->current_pgpath)
887 m->current_pgpath = NULL;
889 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
890 pgpath->path.dev->name, m->nr_valid_paths);
892 queue_work(kmultipathd, &m->trigger_event);
893 queue_work(kmultipathd, &pgpath->deactivate_path);
896 spin_unlock_irqrestore(&m->lock, flags);
902 * Reinstate a previously-failed path
904 static int reinstate_path(struct pgpath *pgpath)
908 struct multipath *m = pgpath->pg->m;
910 spin_lock_irqsave(&m->lock, flags);
912 if (pgpath->is_active)
915 if (!pgpath->pg->ps.type->reinstate_path) {
916 DMWARN("Reinstate path not supported by path selector %s",
917 pgpath->pg->ps.type->name);
922 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
926 pgpath->is_active = 1;
928 m->current_pgpath = NULL;
929 if (!m->nr_valid_paths++ && m->queue_size)
930 queue_work(kmultipathd, &m->process_queued_ios);
932 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
933 pgpath->path.dev->name, m->nr_valid_paths);
935 queue_work(kmultipathd, &m->trigger_event);
938 spin_unlock_irqrestore(&m->lock, flags);
944 * Fail or reinstate all paths that match the provided struct dm_dev.
946 static int action_dev(struct multipath *m, struct dm_dev *dev,
950 struct pgpath *pgpath;
951 struct priority_group *pg;
953 list_for_each_entry(pg, &m->priority_groups, list) {
954 list_for_each_entry(pgpath, &pg->pgpaths, list) {
955 if (pgpath->path.dev == dev)
964 * Temporarily try to avoid having to use the specified PG
966 static void bypass_pg(struct multipath *m, struct priority_group *pg,
971 spin_lock_irqsave(&m->lock, flags);
973 pg->bypassed = bypassed;
974 m->current_pgpath = NULL;
975 m->current_pg = NULL;
977 spin_unlock_irqrestore(&m->lock, flags);
979 queue_work(kmultipathd, &m->trigger_event);
983 * Switch to using the specified PG from the next I/O that gets mapped
985 static int switch_pg_num(struct multipath *m, const char *pgstr)
987 struct priority_group *pg;
991 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
992 (pgnum > m->nr_priority_groups)) {
993 DMWARN("invalid PG number supplied to switch_pg_num");
997 spin_lock_irqsave(&m->lock, flags);
998 list_for_each_entry(pg, &m->priority_groups, list) {
1003 m->current_pgpath = NULL;
1004 m->current_pg = NULL;
1007 spin_unlock_irqrestore(&m->lock, flags);
1009 queue_work(kmultipathd, &m->trigger_event);
1014 * Set/clear bypassed status of a PG.
1015 * PGs are numbered upwards from 1 in the order they were declared.
1017 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1019 struct priority_group *pg;
1022 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1023 (pgnum > m->nr_priority_groups)) {
1024 DMWARN("invalid PG number supplied to bypass_pg");
1028 list_for_each_entry(pg, &m->priority_groups, list) {
1033 bypass_pg(m, pg, bypassed);
1038 * Should we retry pg_init immediately?
1040 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1042 unsigned long flags;
1043 int limit_reached = 0;
1045 spin_lock_irqsave(&m->lock, flags);
1047 if (m->pg_init_count <= m->pg_init_retries)
1048 m->pg_init_required = 1;
1052 spin_unlock_irqrestore(&m->lock, flags);
1054 return limit_reached;
1057 static void pg_init_done(struct dm_path *path, int errors)
1059 struct pgpath *pgpath = path_to_pgpath(path);
1060 struct priority_group *pg = pgpath->pg;
1061 struct multipath *m = pg->m;
1062 unsigned long flags;
1064 /* device or driver problems */
1069 if (!m->hw_handler_name) {
1073 DMERR("Cannot failover device because scsi_dh_%s was not "
1074 "loaded.", m->hw_handler_name);
1076 * Fail path for now, so we do not ping pong
1080 case SCSI_DH_DEV_TEMP_BUSY:
1082 * Probably doing something like FW upgrade on the
1083 * controller so try the other pg.
1085 bypass_pg(m, pg, 1);
1087 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1089 case SCSI_DH_IMM_RETRY:
1090 case SCSI_DH_RES_TEMP_UNAVAIL:
1091 if (pg_init_limit_reached(m, pgpath))
1097 * We probably do not want to fail the path for a device
1098 * error, but this is what the old dm did. In future
1099 * patches we can do more advanced handling.
1104 spin_lock_irqsave(&m->lock, flags);
1106 DMERR("Could not failover device. Error %d.", errors);
1107 m->current_pgpath = NULL;
1108 m->current_pg = NULL;
1109 } else if (!m->pg_init_required) {
1114 m->pg_init_in_progress = 0;
1115 queue_work(kmultipathd, &m->process_queued_ios);
1116 spin_unlock_irqrestore(&m->lock, flags);
1119 static void activate_path(struct work_struct *work)
1122 struct multipath *m =
1123 container_of(work, struct multipath, activate_path);
1124 struct dm_path *path;
1125 unsigned long flags;
1127 spin_lock_irqsave(&m->lock, flags);
1128 path = &m->pgpath_to_activate->path;
1129 m->pgpath_to_activate = NULL;
1130 spin_unlock_irqrestore(&m->lock, flags);
1133 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1134 pg_init_done(path, ret);
1140 static int do_end_io(struct multipath *m, struct bio *bio,
1141 int error, struct dm_mpath_io *mpio)
1143 unsigned long flags;
1146 return 0; /* I/O complete */
1148 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1151 if (error == -EOPNOTSUPP)
1154 spin_lock_irqsave(&m->lock, flags);
1155 if (!m->nr_valid_paths) {
1156 if (__must_push_back(m)) {
1157 spin_unlock_irqrestore(&m->lock, flags);
1158 return DM_ENDIO_REQUEUE;
1159 } else if (!m->queue_if_no_path) {
1160 spin_unlock_irqrestore(&m->lock, flags);
1163 spin_unlock_irqrestore(&m->lock, flags);
1167 spin_unlock_irqrestore(&m->lock, flags);
1170 fail_path(mpio->pgpath);
1173 dm_bio_restore(&mpio->details, bio);
1175 /* queue for the daemon to resubmit or fail */
1176 spin_lock_irqsave(&m->lock, flags);
1177 bio_list_add(&m->queued_ios, bio);
1180 queue_work(kmultipathd, &m->process_queued_ios);
1181 spin_unlock_irqrestore(&m->lock, flags);
1183 return DM_ENDIO_INCOMPLETE; /* io not complete */
1186 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1187 int error, union map_info *map_context)
1189 struct multipath *m = ti->private;
1190 struct dm_mpath_io *mpio = map_context->ptr;
1191 struct pgpath *pgpath = mpio->pgpath;
1192 struct path_selector *ps;
1195 r = do_end_io(m, bio, error, mpio);
1197 ps = &pgpath->pg->ps;
1198 if (ps->type->end_io)
1199 ps->type->end_io(ps, &pgpath->path);
1201 if (r != DM_ENDIO_INCOMPLETE)
1202 mempool_free(mpio, m->mpio_pool);
1208 * Suspend can't complete until all the I/O is processed so if
1209 * the last path fails we must error any remaining I/O.
1210 * Note that if the freeze_bdev fails while suspending, the
1211 * queue_if_no_path state is lost - userspace should reset it.
1213 static void multipath_presuspend(struct dm_target *ti)
1215 struct multipath *m = (struct multipath *) ti->private;
1217 queue_if_no_path(m, 0, 1);
1221 * Restore the queue_if_no_path setting.
1223 static void multipath_resume(struct dm_target *ti)
1225 struct multipath *m = (struct multipath *) ti->private;
1226 unsigned long flags;
1228 spin_lock_irqsave(&m->lock, flags);
1229 m->queue_if_no_path = m->saved_queue_if_no_path;
1230 spin_unlock_irqrestore(&m->lock, flags);
1234 * Info output has the following format:
1235 * num_multipath_feature_args [multipath_feature_args]*
1236 * num_handler_status_args [handler_status_args]*
1237 * num_groups init_group_number
1238 * [A|D|E num_ps_status_args [ps_status_args]*
1239 * num_paths num_selector_args
1240 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1242 * Table output has the following format (identical to the constructor string):
1243 * num_feature_args [features_args]*
1244 * num_handler_args hw_handler [hw_handler_args]*
1245 * num_groups init_group_number
1246 * [priority selector-name num_ps_args [ps_args]*
1247 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1249 static int multipath_status(struct dm_target *ti, status_type_t type,
1250 char *result, unsigned int maxlen)
1253 unsigned long flags;
1254 struct multipath *m = (struct multipath *) ti->private;
1255 struct priority_group *pg;
1260 spin_lock_irqsave(&m->lock, flags);
1263 if (type == STATUSTYPE_INFO)
1264 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1266 DMEMIT("%u ", m->queue_if_no_path +
1267 (m->pg_init_retries > 0) * 2);
1268 if (m->queue_if_no_path)
1269 DMEMIT("queue_if_no_path ");
1270 if (m->pg_init_retries)
1271 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1274 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1277 DMEMIT("1 %s ", m->hw_handler_name);
1279 DMEMIT("%u ", m->nr_priority_groups);
1282 pg_num = m->next_pg->pg_num;
1283 else if (m->current_pg)
1284 pg_num = m->current_pg->pg_num;
1288 DMEMIT("%u ", pg_num);
1291 case STATUSTYPE_INFO:
1292 list_for_each_entry(pg, &m->priority_groups, list) {
1294 state = 'D'; /* Disabled */
1295 else if (pg == m->current_pg)
1296 state = 'A'; /* Currently Active */
1298 state = 'E'; /* Enabled */
1300 DMEMIT("%c ", state);
1302 if (pg->ps.type->status)
1303 sz += pg->ps.type->status(&pg->ps, NULL, type,
1309 DMEMIT("%u %u ", pg->nr_pgpaths,
1310 pg->ps.type->info_args);
1312 list_for_each_entry(p, &pg->pgpaths, list) {
1313 DMEMIT("%s %s %u ", p->path.dev->name,
1314 p->is_active ? "A" : "F",
1316 if (pg->ps.type->status)
1317 sz += pg->ps.type->status(&pg->ps,
1318 &p->path, type, result + sz,
1324 case STATUSTYPE_TABLE:
1325 list_for_each_entry(pg, &m->priority_groups, list) {
1326 DMEMIT("%s ", pg->ps.type->name);
1328 if (pg->ps.type->status)
1329 sz += pg->ps.type->status(&pg->ps, NULL, type,
1335 DMEMIT("%u %u ", pg->nr_pgpaths,
1336 pg->ps.type->table_args);
1338 list_for_each_entry(p, &pg->pgpaths, list) {
1339 DMEMIT("%s ", p->path.dev->name);
1340 if (pg->ps.type->status)
1341 sz += pg->ps.type->status(&pg->ps,
1342 &p->path, type, result + sz,
1349 spin_unlock_irqrestore(&m->lock, flags);
1354 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1358 struct multipath *m = (struct multipath *) ti->private;
1362 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1363 return queue_if_no_path(m, 1, 0);
1364 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1365 return queue_if_no_path(m, 0, 0);
1371 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1372 return bypass_pg_num(m, argv[1], 1);
1373 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1374 return bypass_pg_num(m, argv[1], 0);
1375 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1376 return switch_pg_num(m, argv[1]);
1377 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1378 action = reinstate_path;
1379 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1384 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1385 dm_table_get_mode(ti->table), &dev);
1387 DMWARN("message: error getting device %s",
1392 r = action_dev(m, dev, action);
1394 dm_put_device(ti, dev);
1399 DMWARN("Unrecognised multipath message received.");
1403 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1406 struct multipath *m = (struct multipath *) ti->private;
1407 struct block_device *bdev = NULL;
1409 unsigned long flags;
1412 spin_lock_irqsave(&m->lock, flags);
1414 if (!m->current_pgpath)
1417 if (m->current_pgpath) {
1418 bdev = m->current_pgpath->path.dev->bdev;
1419 mode = m->current_pgpath->path.dev->mode;
1427 spin_unlock_irqrestore(&m->lock, flags);
1429 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1432 /*-----------------------------------------------------------------
1434 *---------------------------------------------------------------*/
1435 static struct target_type multipath_target = {
1436 .name = "multipath",
1437 .version = {1, 0, 5},
1438 .module = THIS_MODULE,
1439 .ctr = multipath_ctr,
1440 .dtr = multipath_dtr,
1441 .map = multipath_map,
1442 .end_io = multipath_end_io,
1443 .presuspend = multipath_presuspend,
1444 .resume = multipath_resume,
1445 .status = multipath_status,
1446 .message = multipath_message,
1447 .ioctl = multipath_ioctl,
1450 static int __init dm_multipath_init(void)
1454 /* allocate a slab for the dm_ios */
1455 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1459 r = dm_register_target(&multipath_target);
1461 DMERR("register failed %d", r);
1462 kmem_cache_destroy(_mpio_cache);
1466 kmultipathd = create_workqueue("kmpathd");
1468 DMERR("failed to create workqueue kmpathd");
1469 dm_unregister_target(&multipath_target);
1470 kmem_cache_destroy(_mpio_cache);
1475 * A separate workqueue is used to handle the device handlers
1476 * to avoid overloading existing workqueue. Overloading the
1477 * old workqueue would also create a bottleneck in the
1478 * path of the storage hardware device activation.
1480 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1481 if (!kmpath_handlerd) {
1482 DMERR("failed to create workqueue kmpath_handlerd");
1483 destroy_workqueue(kmultipathd);
1484 dm_unregister_target(&multipath_target);
1485 kmem_cache_destroy(_mpio_cache);
1489 DMINFO("version %u.%u.%u loaded",
1490 multipath_target.version[0], multipath_target.version[1],
1491 multipath_target.version[2]);
1496 static void __exit dm_multipath_exit(void)
1500 destroy_workqueue(kmpath_handlerd);
1501 destroy_workqueue(kmultipathd);
1503 r = dm_unregister_target(&multipath_target);
1505 DMERR("target unregister failed %d", r);
1506 kmem_cache_destroy(_mpio_cache);
1509 module_init(dm_multipath_init);
1510 module_exit(dm_multipath_exit);
1512 MODULE_DESCRIPTION(DM_NAME " multipath target");
1513 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1514 MODULE_LICENSE("GPL");