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.
9 #include "dm-path-selector.h"
10 #include "dm-bio-list.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 fail_count; /* Cumulative failure count */
38 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
44 struct priority_group {
45 struct list_head list;
47 struct multipath *m; /* Owning multipath instance */
48 struct path_selector ps;
50 unsigned pg_num; /* Reference number */
51 unsigned bypassed; /* Temporarily bypass this PG? */
53 unsigned nr_pgpaths; /* Number of paths in PG */
54 struct list_head pgpaths;
57 /* Multipath context */
59 struct list_head list;
64 const char *hw_handler_name;
65 struct work_struct activate_path;
66 unsigned nr_priority_groups;
67 struct list_head priority_groups;
68 unsigned pg_init_required; /* pg_init needs calling? */
69 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
71 unsigned nr_valid_paths; /* Total number of usable paths */
72 struct pgpath *current_pgpath;
73 struct priority_group *current_pg;
74 struct priority_group *next_pg; /* Switch to this PG if set */
75 unsigned repeat_count; /* I/Os left before calling PS again */
77 unsigned queue_io; /* Must we queue all I/O? */
78 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
79 unsigned saved_queue_if_no_path;/* Saved state during suspension */
80 unsigned pg_init_retries; /* Number of times to retry pg_init */
81 unsigned pg_init_count; /* Number of times pg_init called */
83 struct work_struct process_queued_ios;
84 struct bio_list queued_ios;
87 struct work_struct trigger_event;
90 * We must use a mempool of dm_mpath_io structs so that we
91 * can resubmit bios on error.
97 * Context information attached to each bio we process.
100 struct pgpath *pgpath;
101 struct dm_bio_details details;
104 typedef int (*action_fn) (struct pgpath *pgpath);
106 #define MIN_IOS 256 /* Mempool size */
108 static struct kmem_cache *_mpio_cache;
110 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
111 static void process_queued_ios(struct work_struct *work);
112 static void trigger_event(struct work_struct *work);
113 static void activate_path(struct work_struct *work);
116 /*-----------------------------------------------
117 * Allocation routines
118 *-----------------------------------------------*/
120 static struct pgpath *alloc_pgpath(void)
122 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
125 pgpath->path.is_active = 1;
130 static void free_pgpath(struct pgpath *pgpath)
135 static struct priority_group *alloc_priority_group(void)
137 struct priority_group *pg;
139 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
142 INIT_LIST_HEAD(&pg->pgpaths);
147 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
149 struct pgpath *pgpath, *tmp;
151 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
152 list_del(&pgpath->list);
153 dm_put_device(ti, pgpath->path.dev);
158 static void free_priority_group(struct priority_group *pg,
159 struct dm_target *ti)
161 struct path_selector *ps = &pg->ps;
164 ps->type->destroy(ps);
165 dm_put_path_selector(ps->type);
168 free_pgpaths(&pg->pgpaths, ti);
172 static struct multipath *alloc_multipath(struct dm_target *ti)
176 m = kzalloc(sizeof(*m), GFP_KERNEL);
178 INIT_LIST_HEAD(&m->priority_groups);
179 spin_lock_init(&m->lock);
181 INIT_WORK(&m->process_queued_ios, process_queued_ios);
182 INIT_WORK(&m->trigger_event, trigger_event);
183 INIT_WORK(&m->activate_path, activate_path);
184 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
196 static void free_multipath(struct multipath *m)
198 struct priority_group *pg, *tmp;
200 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
202 free_priority_group(pg, m->ti);
205 kfree(m->hw_handler_name);
206 mempool_destroy(m->mpio_pool);
211 /*-----------------------------------------------
213 *-----------------------------------------------*/
215 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
217 m->current_pg = pgpath->pg;
219 /* Must we initialise the PG first, and queue I/O till it's ready? */
220 if (m->hw_handler_name) {
221 m->pg_init_required = 1;
224 m->pg_init_required = 0;
228 m->pg_init_count = 0;
231 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
233 struct dm_path *path;
235 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
239 m->current_pgpath = path_to_pgpath(path);
241 if (m->current_pg != pg)
242 __switch_pg(m, m->current_pgpath);
247 static void __choose_pgpath(struct multipath *m)
249 struct priority_group *pg;
250 unsigned bypassed = 1;
252 if (!m->nr_valid_paths)
255 /* Were we instructed to switch PG? */
259 if (!__choose_path_in_pg(m, pg))
263 /* Don't change PG until it has no remaining paths */
264 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
268 * Loop through priority groups until we find a valid path.
269 * First time we skip PGs marked 'bypassed'.
270 * Second time we only try the ones we skipped.
273 list_for_each_entry(pg, &m->priority_groups, list) {
274 if (pg->bypassed == bypassed)
276 if (!__choose_path_in_pg(m, pg))
279 } while (bypassed--);
282 m->current_pgpath = NULL;
283 m->current_pg = NULL;
287 * Check whether bios must be queued in the device-mapper core rather
288 * than here in the target.
290 * m->lock must be held on entry.
292 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
293 * same value then we are not between multipath_presuspend()
294 * and multipath_resume() calls and we have no need to check
295 * for the DMF_NOFLUSH_SUSPENDING flag.
297 static int __must_push_back(struct multipath *m)
299 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
300 dm_noflush_suspending(m->ti));
303 static int map_io(struct multipath *m, struct bio *bio,
304 struct dm_mpath_io *mpio, unsigned was_queued)
306 int r = DM_MAPIO_REMAPPED;
308 struct pgpath *pgpath;
310 spin_lock_irqsave(&m->lock, flags);
312 /* Do we need to select a new pgpath? */
313 if (!m->current_pgpath ||
314 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
317 pgpath = m->current_pgpath;
322 if ((pgpath && m->queue_io) ||
323 (!pgpath && m->queue_if_no_path)) {
324 /* Queue for the daemon to resubmit */
325 bio_list_add(&m->queued_ios, bio);
327 if ((m->pg_init_required && !m->pg_init_in_progress) ||
329 queue_work(kmultipathd, &m->process_queued_ios);
331 r = DM_MAPIO_SUBMITTED;
333 bio->bi_bdev = pgpath->path.dev->bdev;
334 else if (__must_push_back(m))
335 r = DM_MAPIO_REQUEUE;
337 r = -EIO; /* Failed */
339 mpio->pgpath = pgpath;
341 spin_unlock_irqrestore(&m->lock, flags);
347 * If we run out of usable paths, should we queue I/O or error it?
349 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
350 unsigned save_old_value)
354 spin_lock_irqsave(&m->lock, flags);
357 m->saved_queue_if_no_path = m->queue_if_no_path;
359 m->saved_queue_if_no_path = queue_if_no_path;
360 m->queue_if_no_path = queue_if_no_path;
361 if (!m->queue_if_no_path && m->queue_size)
362 queue_work(kmultipathd, &m->process_queued_ios);
364 spin_unlock_irqrestore(&m->lock, flags);
369 /*-----------------------------------------------------------------
370 * The multipath daemon is responsible for resubmitting queued ios.
371 *---------------------------------------------------------------*/
373 static void dispatch_queued_ios(struct multipath *m)
377 struct bio *bio = NULL, *next;
378 struct dm_mpath_io *mpio;
379 union map_info *info;
381 spin_lock_irqsave(&m->lock, flags);
382 bio = bio_list_get(&m->queued_ios);
383 spin_unlock_irqrestore(&m->lock, flags);
389 info = dm_get_mapinfo(bio);
392 r = map_io(m, bio, mpio, 1);
395 else if (r == DM_MAPIO_REMAPPED)
396 generic_make_request(bio);
397 else if (r == DM_MAPIO_REQUEUE)
398 bio_endio(bio, -EIO);
404 static void process_queued_ios(struct work_struct *work)
406 struct multipath *m =
407 container_of(work, struct multipath, process_queued_ios);
408 struct pgpath *pgpath = NULL;
409 unsigned init_required = 0, must_queue = 1;
412 spin_lock_irqsave(&m->lock, flags);
417 if (!m->current_pgpath)
420 pgpath = m->current_pgpath;
422 if ((pgpath && !m->queue_io) ||
423 (!pgpath && !m->queue_if_no_path))
426 if (m->pg_init_required && !m->pg_init_in_progress) {
428 m->pg_init_required = 0;
429 m->pg_init_in_progress = 1;
434 spin_unlock_irqrestore(&m->lock, flags);
437 queue_work(kmpath_handlerd, &m->activate_path);
440 dispatch_queued_ios(m);
444 * An event is triggered whenever a path is taken out of use.
445 * Includes path failure and PG bypass.
447 static void trigger_event(struct work_struct *work)
449 struct multipath *m =
450 container_of(work, struct multipath, trigger_event);
452 dm_table_event(m->ti->table);
455 /*-----------------------------------------------------------------
456 * Constructor/argument parsing:
457 * <#multipath feature args> [<arg>]*
458 * <#hw_handler args> [hw_handler [<arg>]*]
460 * <initial priority group>
461 * [<selector> <#selector args> [<arg>]*
462 * <#paths> <#per-path selector args>
463 * [<path> [<arg>]* ]+ ]+
464 *---------------------------------------------------------------*/
471 static int read_param(struct param *param, char *str, unsigned *v, char **error)
474 (sscanf(str, "%u", v) != 1) ||
477 *error = param->error;
489 static char *shift(struct arg_set *as)
503 static void consume(struct arg_set *as, unsigned n)
505 BUG_ON (as->argc < n);
510 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
511 struct dm_target *ti)
514 struct path_selector_type *pst;
517 static struct param _params[] = {
518 {0, 1024, "invalid number of path selector args"},
521 pst = dm_get_path_selector(shift(as));
523 ti->error = "unknown path selector type";
527 r = read_param(_params, shift(as), &ps_argc, &ti->error);
529 dm_put_path_selector(pst);
533 r = pst->create(&pg->ps, ps_argc, as->argv);
535 dm_put_path_selector(pst);
536 ti->error = "path selector constructor failed";
541 consume(as, ps_argc);
546 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
547 struct dm_target *ti)
552 /* we need at least a path arg */
554 ti->error = "no device given";
562 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
563 dm_table_get_mode(ti->table), &p->path.dev);
565 ti->error = "error getting device";
569 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
571 dm_put_device(ti, p->path.dev);
582 static struct priority_group *parse_priority_group(struct arg_set *as,
585 static struct param _params[] = {
586 {1, 1024, "invalid number of paths"},
587 {0, 1024, "invalid number of selector args"}
591 unsigned i, nr_selector_args, nr_params;
592 struct priority_group *pg;
593 struct dm_target *ti = m->ti;
597 ti->error = "not enough priority group aruments";
601 pg = alloc_priority_group();
603 ti->error = "couldn't allocate priority group";
608 r = parse_path_selector(as, pg, ti);
615 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
619 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
623 nr_params = 1 + nr_selector_args;
624 for (i = 0; i < pg->nr_pgpaths; i++) {
625 struct pgpath *pgpath;
626 struct arg_set path_args;
628 if (as->argc < nr_params)
631 path_args.argc = nr_params;
632 path_args.argv = as->argv;
634 pgpath = parse_path(&path_args, &pg->ps, ti);
639 list_add_tail(&pgpath->list, &pg->pgpaths);
640 consume(as, nr_params);
646 free_priority_group(pg, ti);
650 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
653 struct dm_target *ti = m->ti;
655 static struct param _params[] = {
656 {0, 1024, "invalid number of hardware handler args"},
659 if (read_param(_params, shift(as), &hw_argc, &ti->error))
665 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
666 request_module("scsi_dh_%s", m->hw_handler_name);
667 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
668 ti->error = "unknown hardware handler type";
669 kfree(m->hw_handler_name);
670 m->hw_handler_name = NULL;
673 consume(as, hw_argc - 1);
678 static int parse_features(struct arg_set *as, struct multipath *m)
682 struct dm_target *ti = m->ti;
683 const char *param_name;
685 static struct param _params[] = {
686 {0, 3, "invalid number of feature args"},
687 {1, 50, "pg_init_retries must be between 1 and 50"},
690 r = read_param(_params, shift(as), &argc, &ti->error);
698 param_name = shift(as);
701 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
702 r = queue_if_no_path(m, 1, 0);
706 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
708 r = read_param(_params + 1, shift(as),
709 &m->pg_init_retries, &ti->error);
714 ti->error = "Unrecognised multipath feature request";
716 } while (argc && !r);
721 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
724 /* target parameters */
725 static struct param _params[] = {
726 {1, 1024, "invalid number of priority groups"},
727 {1, 1024, "invalid initial priority group number"},
733 unsigned pg_count = 0;
734 unsigned next_pg_num;
739 m = alloc_multipath(ti);
741 ti->error = "can't allocate multipath";
745 r = parse_features(&as, m);
749 r = parse_hw_handler(&as, m);
753 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
757 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
761 /* parse the priority groups */
763 struct priority_group *pg;
765 pg = parse_priority_group(&as, m);
771 m->nr_valid_paths += pg->nr_pgpaths;
772 list_add_tail(&pg->list, &m->priority_groups);
774 pg->pg_num = pg_count;
779 if (pg_count != m->nr_priority_groups) {
780 ti->error = "priority group count mismatch";
792 static void multipath_dtr(struct dm_target *ti)
794 struct multipath *m = (struct multipath *) ti->private;
796 flush_workqueue(kmpath_handlerd);
797 flush_workqueue(kmultipathd);
802 * Map bios, recording original fields for later in case we have to resubmit
804 static int multipath_map(struct dm_target *ti, struct bio *bio,
805 union map_info *map_context)
808 struct dm_mpath_io *mpio;
809 struct multipath *m = (struct multipath *) ti->private;
811 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
812 dm_bio_record(&mpio->details, bio);
814 map_context->ptr = mpio;
815 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
816 r = map_io(m, bio, mpio, 0);
817 if (r < 0 || r == DM_MAPIO_REQUEUE)
818 mempool_free(mpio, m->mpio_pool);
824 * Take a path out of use.
826 static int fail_path(struct pgpath *pgpath)
829 struct multipath *m = pgpath->pg->m;
831 spin_lock_irqsave(&m->lock, flags);
833 if (!pgpath->path.is_active)
836 DMWARN("Failing path %s.", pgpath->path.dev->name);
838 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
839 pgpath->path.is_active = 0;
840 pgpath->fail_count++;
844 if (pgpath == m->current_pgpath)
845 m->current_pgpath = NULL;
847 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
848 pgpath->path.dev->name, m->nr_valid_paths);
850 queue_work(kmultipathd, &m->trigger_event);
853 spin_unlock_irqrestore(&m->lock, flags);
859 * Reinstate a previously-failed path
861 static int reinstate_path(struct pgpath *pgpath)
865 struct multipath *m = pgpath->pg->m;
867 spin_lock_irqsave(&m->lock, flags);
869 if (pgpath->path.is_active)
872 if (!pgpath->pg->ps.type) {
873 DMWARN("Reinstate path not supported by path selector %s",
874 pgpath->pg->ps.type->name);
879 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
883 pgpath->path.is_active = 1;
885 m->current_pgpath = NULL;
886 if (!m->nr_valid_paths++ && m->queue_size)
887 queue_work(kmultipathd, &m->process_queued_ios);
889 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
890 pgpath->path.dev->name, m->nr_valid_paths);
892 queue_work(kmultipathd, &m->trigger_event);
895 spin_unlock_irqrestore(&m->lock, flags);
901 * Fail or reinstate all paths that match the provided struct dm_dev.
903 static int action_dev(struct multipath *m, struct dm_dev *dev,
907 struct pgpath *pgpath;
908 struct priority_group *pg;
910 list_for_each_entry(pg, &m->priority_groups, list) {
911 list_for_each_entry(pgpath, &pg->pgpaths, list) {
912 if (pgpath->path.dev == dev)
921 * Temporarily try to avoid having to use the specified PG
923 static void bypass_pg(struct multipath *m, struct priority_group *pg,
928 spin_lock_irqsave(&m->lock, flags);
930 pg->bypassed = bypassed;
931 m->current_pgpath = NULL;
932 m->current_pg = NULL;
934 spin_unlock_irqrestore(&m->lock, flags);
936 queue_work(kmultipathd, &m->trigger_event);
940 * Switch to using the specified PG from the next I/O that gets mapped
942 static int switch_pg_num(struct multipath *m, const char *pgstr)
944 struct priority_group *pg;
948 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
949 (pgnum > m->nr_priority_groups)) {
950 DMWARN("invalid PG number supplied to switch_pg_num");
954 spin_lock_irqsave(&m->lock, flags);
955 list_for_each_entry(pg, &m->priority_groups, list) {
960 m->current_pgpath = NULL;
961 m->current_pg = NULL;
964 spin_unlock_irqrestore(&m->lock, flags);
966 queue_work(kmultipathd, &m->trigger_event);
971 * Set/clear bypassed status of a PG.
972 * PGs are numbered upwards from 1 in the order they were declared.
974 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
976 struct priority_group *pg;
979 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
980 (pgnum > m->nr_priority_groups)) {
981 DMWARN("invalid PG number supplied to bypass_pg");
985 list_for_each_entry(pg, &m->priority_groups, list) {
990 bypass_pg(m, pg, bypassed);
995 * Should we retry pg_init immediately?
997 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1000 int limit_reached = 0;
1002 spin_lock_irqsave(&m->lock, flags);
1004 if (m->pg_init_count <= m->pg_init_retries)
1005 m->pg_init_required = 1;
1009 spin_unlock_irqrestore(&m->lock, flags);
1011 return limit_reached;
1014 static void pg_init_done(struct dm_path *path, int errors)
1016 struct pgpath *pgpath = path_to_pgpath(path);
1017 struct priority_group *pg = pgpath->pg;
1018 struct multipath *m = pg->m;
1019 unsigned long flags;
1021 /* device or driver problems */
1026 if (!m->hw_handler_name) {
1030 DMERR("Cannot failover device because scsi_dh_%s was not "
1031 "loaded.", m->hw_handler_name);
1033 * Fail path for now, so we do not ping pong
1037 case SCSI_DH_DEV_TEMP_BUSY:
1039 * Probably doing something like FW upgrade on the
1040 * controller so try the other pg.
1042 bypass_pg(m, pg, 1);
1044 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1046 case SCSI_DH_IMM_RETRY:
1047 case SCSI_DH_RES_TEMP_UNAVAIL:
1048 if (pg_init_limit_reached(m, pgpath))
1054 * We probably do not want to fail the path for a device
1055 * error, but this is what the old dm did. In future
1056 * patches we can do more advanced handling.
1061 spin_lock_irqsave(&m->lock, flags);
1063 DMERR("Could not failover device. Error %d.", errors);
1064 m->current_pgpath = NULL;
1065 m->current_pg = NULL;
1066 } else if (!m->pg_init_required) {
1071 m->pg_init_in_progress = 0;
1072 queue_work(kmultipathd, &m->process_queued_ios);
1073 spin_unlock_irqrestore(&m->lock, flags);
1076 static void activate_path(struct work_struct *work)
1079 struct multipath *m =
1080 container_of(work, struct multipath, activate_path);
1081 struct dm_path *path = &m->current_pgpath->path;
1083 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1084 pg_init_done(path, ret);
1090 static int do_end_io(struct multipath *m, struct bio *bio,
1091 int error, struct dm_mpath_io *mpio)
1093 unsigned long flags;
1096 return 0; /* I/O complete */
1098 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1101 if (error == -EOPNOTSUPP)
1104 spin_lock_irqsave(&m->lock, flags);
1105 if (!m->nr_valid_paths) {
1106 if (__must_push_back(m)) {
1107 spin_unlock_irqrestore(&m->lock, flags);
1108 return DM_ENDIO_REQUEUE;
1109 } else if (!m->queue_if_no_path) {
1110 spin_unlock_irqrestore(&m->lock, flags);
1113 spin_unlock_irqrestore(&m->lock, flags);
1117 spin_unlock_irqrestore(&m->lock, flags);
1120 fail_path(mpio->pgpath);
1123 dm_bio_restore(&mpio->details, bio);
1125 /* queue for the daemon to resubmit or fail */
1126 spin_lock_irqsave(&m->lock, flags);
1127 bio_list_add(&m->queued_ios, bio);
1130 queue_work(kmultipathd, &m->process_queued_ios);
1131 spin_unlock_irqrestore(&m->lock, flags);
1133 return DM_ENDIO_INCOMPLETE; /* io not complete */
1136 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1137 int error, union map_info *map_context)
1139 struct multipath *m = ti->private;
1140 struct dm_mpath_io *mpio = map_context->ptr;
1141 struct pgpath *pgpath = mpio->pgpath;
1142 struct path_selector *ps;
1145 r = do_end_io(m, bio, error, mpio);
1147 ps = &pgpath->pg->ps;
1148 if (ps->type->end_io)
1149 ps->type->end_io(ps, &pgpath->path);
1151 if (r != DM_ENDIO_INCOMPLETE)
1152 mempool_free(mpio, m->mpio_pool);
1158 * Suspend can't complete until all the I/O is processed so if
1159 * the last path fails we must error any remaining I/O.
1160 * Note that if the freeze_bdev fails while suspending, the
1161 * queue_if_no_path state is lost - userspace should reset it.
1163 static void multipath_presuspend(struct dm_target *ti)
1165 struct multipath *m = (struct multipath *) ti->private;
1167 queue_if_no_path(m, 0, 1);
1171 * Restore the queue_if_no_path setting.
1173 static void multipath_resume(struct dm_target *ti)
1175 struct multipath *m = (struct multipath *) ti->private;
1176 unsigned long flags;
1178 spin_lock_irqsave(&m->lock, flags);
1179 m->queue_if_no_path = m->saved_queue_if_no_path;
1180 spin_unlock_irqrestore(&m->lock, flags);
1184 * Info output has the following format:
1185 * num_multipath_feature_args [multipath_feature_args]*
1186 * num_handler_status_args [handler_status_args]*
1187 * num_groups init_group_number
1188 * [A|D|E num_ps_status_args [ps_status_args]*
1189 * num_paths num_selector_args
1190 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1192 * Table output has the following format (identical to the constructor string):
1193 * num_feature_args [features_args]*
1194 * num_handler_args hw_handler [hw_handler_args]*
1195 * num_groups init_group_number
1196 * [priority selector-name num_ps_args [ps_args]*
1197 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1199 static int multipath_status(struct dm_target *ti, status_type_t type,
1200 char *result, unsigned int maxlen)
1203 unsigned long flags;
1204 struct multipath *m = (struct multipath *) ti->private;
1205 struct priority_group *pg;
1210 spin_lock_irqsave(&m->lock, flags);
1213 if (type == STATUSTYPE_INFO)
1214 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1216 DMEMIT("%u ", m->queue_if_no_path +
1217 (m->pg_init_retries > 0) * 2);
1218 if (m->queue_if_no_path)
1219 DMEMIT("queue_if_no_path ");
1220 if (m->pg_init_retries)
1221 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1224 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1227 DMEMIT("1 %s ", m->hw_handler_name);
1229 DMEMIT("%u ", m->nr_priority_groups);
1232 pg_num = m->next_pg->pg_num;
1233 else if (m->current_pg)
1234 pg_num = m->current_pg->pg_num;
1238 DMEMIT("%u ", pg_num);
1241 case STATUSTYPE_INFO:
1242 list_for_each_entry(pg, &m->priority_groups, list) {
1244 state = 'D'; /* Disabled */
1245 else if (pg == m->current_pg)
1246 state = 'A'; /* Currently Active */
1248 state = 'E'; /* Enabled */
1250 DMEMIT("%c ", state);
1252 if (pg->ps.type->status)
1253 sz += pg->ps.type->status(&pg->ps, NULL, type,
1259 DMEMIT("%u %u ", pg->nr_pgpaths,
1260 pg->ps.type->info_args);
1262 list_for_each_entry(p, &pg->pgpaths, list) {
1263 DMEMIT("%s %s %u ", p->path.dev->name,
1264 p->path.is_active ? "A" : "F",
1266 if (pg->ps.type->status)
1267 sz += pg->ps.type->status(&pg->ps,
1268 &p->path, type, result + sz,
1274 case STATUSTYPE_TABLE:
1275 list_for_each_entry(pg, &m->priority_groups, list) {
1276 DMEMIT("%s ", pg->ps.type->name);
1278 if (pg->ps.type->status)
1279 sz += pg->ps.type->status(&pg->ps, NULL, type,
1285 DMEMIT("%u %u ", pg->nr_pgpaths,
1286 pg->ps.type->table_args);
1288 list_for_each_entry(p, &pg->pgpaths, list) {
1289 DMEMIT("%s ", p->path.dev->name);
1290 if (pg->ps.type->status)
1291 sz += pg->ps.type->status(&pg->ps,
1292 &p->path, type, result + sz,
1299 spin_unlock_irqrestore(&m->lock, flags);
1304 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1308 struct multipath *m = (struct multipath *) ti->private;
1312 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1313 return queue_if_no_path(m, 1, 0);
1314 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1315 return queue_if_no_path(m, 0, 0);
1321 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1322 return bypass_pg_num(m, argv[1], 1);
1323 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1324 return bypass_pg_num(m, argv[1], 0);
1325 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1326 return switch_pg_num(m, argv[1]);
1327 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1328 action = reinstate_path;
1329 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1334 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1335 dm_table_get_mode(ti->table), &dev);
1337 DMWARN("message: error getting device %s",
1342 r = action_dev(m, dev, action);
1344 dm_put_device(ti, dev);
1349 DMWARN("Unrecognised multipath message received.");
1353 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1354 struct file *filp, unsigned int cmd,
1357 struct multipath *m = (struct multipath *) ti->private;
1358 struct block_device *bdev = NULL;
1359 unsigned long flags;
1360 struct file fake_file = {};
1361 struct dentry fake_dentry = {};
1364 fake_file.f_path.dentry = &fake_dentry;
1366 spin_lock_irqsave(&m->lock, flags);
1368 if (!m->current_pgpath)
1371 if (m->current_pgpath) {
1372 bdev = m->current_pgpath->path.dev->bdev;
1373 fake_dentry.d_inode = bdev->bd_inode;
1374 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1382 spin_unlock_irqrestore(&m->lock, flags);
1384 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1385 bdev->bd_disk, cmd, arg);
1388 /*-----------------------------------------------------------------
1390 *---------------------------------------------------------------*/
1391 static struct target_type multipath_target = {
1392 .name = "multipath",
1393 .version = {1, 0, 5},
1394 .module = THIS_MODULE,
1395 .ctr = multipath_ctr,
1396 .dtr = multipath_dtr,
1397 .map = multipath_map,
1398 .end_io = multipath_end_io,
1399 .presuspend = multipath_presuspend,
1400 .resume = multipath_resume,
1401 .status = multipath_status,
1402 .message = multipath_message,
1403 .ioctl = multipath_ioctl,
1406 static int __init dm_multipath_init(void)
1410 /* allocate a slab for the dm_ios */
1411 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1415 r = dm_register_target(&multipath_target);
1417 DMERR("register failed %d", r);
1418 kmem_cache_destroy(_mpio_cache);
1422 kmultipathd = create_workqueue("kmpathd");
1424 DMERR("failed to create workqueue kmpathd");
1425 dm_unregister_target(&multipath_target);
1426 kmem_cache_destroy(_mpio_cache);
1431 * A separate workqueue is used to handle the device handlers
1432 * to avoid overloading existing workqueue. Overloading the
1433 * old workqueue would also create a bottleneck in the
1434 * path of the storage hardware device activation.
1436 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1437 if (!kmpath_handlerd) {
1438 DMERR("failed to create workqueue kmpath_handlerd");
1439 destroy_workqueue(kmultipathd);
1440 dm_unregister_target(&multipath_target);
1441 kmem_cache_destroy(_mpio_cache);
1445 DMINFO("version %u.%u.%u loaded",
1446 multipath_target.version[0], multipath_target.version[1],
1447 multipath_target.version[2]);
1452 static void __exit dm_multipath_exit(void)
1456 destroy_workqueue(kmpath_handlerd);
1457 destroy_workqueue(kmultipathd);
1459 r = dm_unregister_target(&multipath_target);
1461 DMERR("target unregister failed %d", r);
1462 kmem_cache_destroy(_mpio_cache);
1465 module_init(dm_multipath_init);
1466 module_exit(dm_multipath_exit);
1468 MODULE_DESCRIPTION(DM_NAME " multipath target");
1469 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1470 MODULE_LICENSE("GPL");