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-hw-handler.h"
11 #include "dm-bio-list.h"
12 #include "dm-bio-record.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 <asm/atomic.h>
24 #define MESG_STR(x) x, sizeof(x)
28 struct list_head list;
30 struct priority_group *pg; /* Owning PG */
31 unsigned fail_count; /* Cumulative failure count */
36 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
39 * Paths are grouped into Priority Groups and numbered from 1 upwards.
40 * Each has a path selector which controls which path gets used.
42 struct priority_group {
43 struct list_head list;
45 struct multipath *m; /* Owning multipath instance */
46 struct path_selector ps;
48 unsigned pg_num; /* Reference number */
49 unsigned bypassed; /* Temporarily bypass this PG? */
51 unsigned nr_pgpaths; /* Number of paths in PG */
52 struct list_head pgpaths;
55 /* Multipath context */
57 struct list_head list;
62 struct hw_handler hw_handler;
63 unsigned nr_priority_groups;
64 struct list_head priority_groups;
65 unsigned pg_init_required; /* pg_init needs calling? */
66 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
68 unsigned nr_valid_paths; /* Total number of usable paths */
69 struct pgpath *current_pgpath;
70 struct priority_group *current_pg;
71 struct priority_group *next_pg; /* Switch to this PG if set */
72 unsigned repeat_count; /* I/Os left before calling PS again */
74 unsigned queue_io; /* Must we queue all I/O? */
75 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
76 unsigned saved_queue_if_no_path;/* Saved state during suspension */
78 struct work_struct process_queued_ios;
79 struct bio_list queued_ios;
82 struct work_struct trigger_event;
85 * We must use a mempool of mpath_io structs so that we
86 * can resubmit bios on error.
92 * Context information attached to each bio we process.
95 struct pgpath *pgpath;
96 struct dm_bio_details details;
99 typedef int (*action_fn) (struct pgpath *pgpath);
101 #define MIN_IOS 256 /* Mempool size */
103 static kmem_cache_t *_mpio_cache;
105 struct workqueue_struct *kmultipathd;
106 static void process_queued_ios(void *data);
107 static void trigger_event(void *data);
110 /*-----------------------------------------------
111 * Allocation routines
112 *-----------------------------------------------*/
114 static struct pgpath *alloc_pgpath(void)
116 struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
119 memset(pgpath, 0, sizeof(*pgpath));
120 pgpath->path.is_active = 1;
126 static inline void free_pgpath(struct pgpath *pgpath)
131 static struct priority_group *alloc_priority_group(void)
133 struct priority_group *pg;
135 pg = kmalloc(sizeof(*pg), GFP_KERNEL);
139 memset(pg, 0, sizeof(*pg));
140 INIT_LIST_HEAD(&pg->pgpaths);
145 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
147 struct pgpath *pgpath, *tmp;
149 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
150 list_del(&pgpath->list);
151 dm_put_device(ti, pgpath->path.dev);
156 static void free_priority_group(struct priority_group *pg,
157 struct dm_target *ti)
159 struct path_selector *ps = &pg->ps;
162 ps->type->destroy(ps);
163 dm_put_path_selector(ps->type);
166 free_pgpaths(&pg->pgpaths, ti);
170 static struct multipath *alloc_multipath(void)
174 m = kmalloc(sizeof(*m), GFP_KERNEL);
176 memset(m, 0, sizeof(*m));
177 INIT_LIST_HEAD(&m->priority_groups);
178 spin_lock_init(&m->lock);
180 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
181 INIT_WORK(&m->trigger_event, trigger_event, m);
182 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
192 static void free_multipath(struct multipath *m)
194 struct priority_group *pg, *tmp;
195 struct hw_handler *hwh = &m->hw_handler;
197 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
199 free_priority_group(pg, m->ti);
203 hwh->type->destroy(hwh);
204 dm_put_hw_handler(hwh->type);
207 mempool_destroy(m->mpio_pool);
212 /*-----------------------------------------------
214 *-----------------------------------------------*/
216 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
218 struct hw_handler *hwh = &m->hw_handler;
220 m->current_pg = pgpath->pg;
222 /* Must we initialise the PG first, and queue I/O till it's ready? */
223 if (hwh->type && hwh->type->pg_init) {
224 m->pg_init_required = 1;
227 m->pg_init_required = 0;
232 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
236 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
240 m->current_pgpath = path_to_pgpath(path);
242 if (m->current_pg != pg)
243 __switch_pg(m, m->current_pgpath);
248 static void __choose_pgpath(struct multipath *m)
250 struct priority_group *pg;
251 unsigned bypassed = 1;
253 if (!m->nr_valid_paths)
256 /* Were we instructed to switch PG? */
260 if (!__choose_path_in_pg(m, pg))
264 /* Don't change PG until it has no remaining paths */
265 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
269 * Loop through priority groups until we find a valid path.
270 * First time we skip PGs marked 'bypassed'.
271 * Second time we only try the ones we skipped.
274 list_for_each_entry(pg, &m->priority_groups, list) {
275 if (pg->bypassed == bypassed)
277 if (!__choose_path_in_pg(m, pg))
280 } while (bypassed--);
283 m->current_pgpath = NULL;
284 m->current_pg = NULL;
287 static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
292 struct pgpath *pgpath;
294 spin_lock_irqsave(&m->lock, flags);
296 /* Do we need to select a new pgpath? */
297 if (!m->current_pgpath ||
298 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
301 pgpath = m->current_pgpath;
306 if ((pgpath && m->queue_io) ||
307 (!pgpath && m->queue_if_no_path)) {
308 /* Queue for the daemon to resubmit */
309 bio_list_add(&m->queued_ios, bio);
311 if ((m->pg_init_required && !m->pg_init_in_progress) ||
313 queue_work(kmultipathd, &m->process_queued_ios);
317 r = -EIO; /* Failed */
319 bio->bi_bdev = pgpath->path.dev->bdev;
321 mpio->pgpath = pgpath;
323 spin_unlock_irqrestore(&m->lock, flags);
329 * If we run out of usable paths, should we queue I/O or error it?
331 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
332 unsigned save_old_value)
336 spin_lock_irqsave(&m->lock, flags);
339 m->saved_queue_if_no_path = m->queue_if_no_path;
341 m->saved_queue_if_no_path = queue_if_no_path;
342 m->queue_if_no_path = queue_if_no_path;
343 if (!m->queue_if_no_path && m->queue_size)
344 queue_work(kmultipathd, &m->process_queued_ios);
346 spin_unlock_irqrestore(&m->lock, flags);
351 /*-----------------------------------------------------------------
352 * The multipath daemon is responsible for resubmitting queued ios.
353 *---------------------------------------------------------------*/
355 static void dispatch_queued_ios(struct multipath *m)
359 struct bio *bio = NULL, *next;
360 struct mpath_io *mpio;
361 union map_info *info;
363 spin_lock_irqsave(&m->lock, flags);
364 bio = bio_list_get(&m->queued_ios);
365 spin_unlock_irqrestore(&m->lock, flags);
371 info = dm_get_mapinfo(bio);
374 r = map_io(m, bio, mpio, 1);
376 bio_endio(bio, bio->bi_size, r);
378 generic_make_request(bio);
384 static void process_queued_ios(void *data)
386 struct multipath *m = (struct multipath *) data;
387 struct hw_handler *hwh = &m->hw_handler;
388 struct pgpath *pgpath = NULL;
389 unsigned init_required = 0, must_queue = 1;
392 spin_lock_irqsave(&m->lock, flags);
397 if (!m->current_pgpath)
400 pgpath = m->current_pgpath;
402 if ((pgpath && !m->queue_io) ||
403 (!pgpath && !m->queue_if_no_path))
406 if (m->pg_init_required && !m->pg_init_in_progress) {
407 m->pg_init_required = 0;
408 m->pg_init_in_progress = 1;
413 spin_unlock_irqrestore(&m->lock, flags);
416 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
419 dispatch_queued_ios(m);
423 * An event is triggered whenever a path is taken out of use.
424 * Includes path failure and PG bypass.
426 static void trigger_event(void *data)
428 struct multipath *m = (struct multipath *) data;
430 dm_table_event(m->ti->table);
433 /*-----------------------------------------------------------------
434 * Constructor/argument parsing:
435 * <#multipath feature args> [<arg>]*
436 * <#hw_handler args> [hw_handler [<arg>]*]
438 * <initial priority group>
439 * [<selector> <#selector args> [<arg>]*
440 * <#paths> <#per-path selector args>
441 * [<path> [<arg>]* ]+ ]+
442 *---------------------------------------------------------------*/
449 #define ESTR(s) ("dm-multipath: " s)
451 static int read_param(struct param *param, char *str, unsigned *v, char **error)
454 (sscanf(str, "%u", v) != 1) ||
457 *error = param->error;
469 static char *shift(struct arg_set *as)
483 static void consume(struct arg_set *as, unsigned n)
485 BUG_ON (as->argc < n);
490 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
491 struct dm_target *ti)
494 struct path_selector_type *pst;
497 static struct param _params[] = {
498 {0, 1024, ESTR("invalid number of path selector args")},
501 pst = dm_get_path_selector(shift(as));
503 ti->error = ESTR("unknown path selector type");
507 r = read_param(_params, shift(as), &ps_argc, &ti->error);
511 r = pst->create(&pg->ps, ps_argc, as->argv);
513 dm_put_path_selector(pst);
514 ti->error = ESTR("path selector constructor failed");
519 consume(as, ps_argc);
524 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
525 struct dm_target *ti)
530 /* we need at least a path arg */
532 ti->error = ESTR("no device given");
540 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
541 dm_table_get_mode(ti->table), &p->path.dev);
543 ti->error = ESTR("error getting device");
547 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
549 dm_put_device(ti, p->path.dev);
560 static struct priority_group *parse_priority_group(struct arg_set *as,
562 struct dm_target *ti)
564 static struct param _params[] = {
565 {1, 1024, ESTR("invalid number of paths")},
566 {0, 1024, ESTR("invalid number of selector args")}
570 unsigned i, nr_selector_args, nr_params;
571 struct priority_group *pg;
575 ti->error = ESTR("not enough priority group aruments");
579 pg = alloc_priority_group();
581 ti->error = ESTR("couldn't allocate priority group");
586 r = parse_path_selector(as, pg, ti);
593 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
597 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
601 nr_params = 1 + nr_selector_args;
602 for (i = 0; i < pg->nr_pgpaths; i++) {
603 struct pgpath *pgpath;
604 struct arg_set path_args;
606 if (as->argc < nr_params)
609 path_args.argc = nr_params;
610 path_args.argv = as->argv;
612 pgpath = parse_path(&path_args, &pg->ps, ti);
617 list_add_tail(&pgpath->list, &pg->pgpaths);
618 consume(as, nr_params);
624 free_priority_group(pg, ti);
628 static int parse_hw_handler(struct arg_set *as, struct multipath *m,
629 struct dm_target *ti)
632 struct hw_handler_type *hwht;
635 static struct param _params[] = {
636 {0, 1024, ESTR("invalid number of hardware handler args")},
639 r = read_param(_params, shift(as), &hw_argc, &ti->error);
646 hwht = dm_get_hw_handler(shift(as));
648 ti->error = ESTR("unknown hardware handler type");
652 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
654 dm_put_hw_handler(hwht);
655 ti->error = ESTR("hardware handler constructor failed");
659 m->hw_handler.type = hwht;
660 consume(as, hw_argc - 1);
665 static int parse_features(struct arg_set *as, struct multipath *m,
666 struct dm_target *ti)
671 static struct param _params[] = {
672 {0, 1, ESTR("invalid number of feature args")},
675 r = read_param(_params, shift(as), &argc, &ti->error);
682 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
683 return queue_if_no_path(m, 1, 0);
685 ti->error = "Unrecognised multipath feature request";
690 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
693 /* target parameters */
694 static struct param _params[] = {
695 {1, 1024, ESTR("invalid number of priority groups")},
696 {1, 1024, ESTR("invalid initial priority group number")},
702 unsigned pg_count = 0;
703 unsigned next_pg_num;
708 m = alloc_multipath();
710 ti->error = ESTR("can't allocate multipath");
714 r = parse_features(&as, m, ti);
718 r = parse_hw_handler(&as, m, ti);
722 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
726 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
730 /* parse the priority groups */
732 struct priority_group *pg;
734 pg = parse_priority_group(&as, m, ti);
740 m->nr_valid_paths += pg->nr_pgpaths;
741 list_add_tail(&pg->list, &m->priority_groups);
743 pg->pg_num = pg_count;
748 if (pg_count != m->nr_priority_groups) {
749 ti->error = ESTR("priority group count mismatch");
764 static void multipath_dtr(struct dm_target *ti)
766 struct multipath *m = (struct multipath *) ti->private;
768 flush_workqueue(kmultipathd);
773 * Map bios, recording original fields for later in case we have to resubmit
775 static int multipath_map(struct dm_target *ti, struct bio *bio,
776 union map_info *map_context)
779 struct mpath_io *mpio;
780 struct multipath *m = (struct multipath *) ti->private;
782 if (bio_barrier(bio))
785 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
786 dm_bio_record(&mpio->details, bio);
788 map_context->ptr = mpio;
789 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
790 r = map_io(m, bio, mpio, 0);
792 mempool_free(mpio, m->mpio_pool);
798 * Take a path out of use.
800 static int fail_path(struct pgpath *pgpath)
803 struct multipath *m = pgpath->pg->m;
805 spin_lock_irqsave(&m->lock, flags);
807 if (!pgpath->path.is_active)
810 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
812 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
813 pgpath->path.is_active = 0;
814 pgpath->fail_count++;
818 if (pgpath == m->current_pgpath)
819 m->current_pgpath = NULL;
821 queue_work(kmultipathd, &m->trigger_event);
824 spin_unlock_irqrestore(&m->lock, flags);
830 * Reinstate a previously-failed path
832 static int reinstate_path(struct pgpath *pgpath)
836 struct multipath *m = pgpath->pg->m;
838 spin_lock_irqsave(&m->lock, flags);
840 if (pgpath->path.is_active)
843 if (!pgpath->pg->ps.type) {
844 DMWARN("Reinstate path not supported by path selector %s",
845 pgpath->pg->ps.type->name);
850 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
854 pgpath->path.is_active = 1;
856 m->current_pgpath = NULL;
857 if (!m->nr_valid_paths++ && m->queue_size)
858 queue_work(kmultipathd, &m->process_queued_ios);
860 queue_work(kmultipathd, &m->trigger_event);
863 spin_unlock_irqrestore(&m->lock, flags);
869 * Fail or reinstate all paths that match the provided struct dm_dev.
871 static int action_dev(struct multipath *m, struct dm_dev *dev,
875 struct pgpath *pgpath;
876 struct priority_group *pg;
878 list_for_each_entry(pg, &m->priority_groups, list) {
879 list_for_each_entry(pgpath, &pg->pgpaths, list) {
880 if (pgpath->path.dev == dev)
889 * Temporarily try to avoid having to use the specified PG
891 static void bypass_pg(struct multipath *m, struct priority_group *pg,
896 spin_lock_irqsave(&m->lock, flags);
898 pg->bypassed = bypassed;
899 m->current_pgpath = NULL;
900 m->current_pg = NULL;
902 spin_unlock_irqrestore(&m->lock, flags);
904 queue_work(kmultipathd, &m->trigger_event);
908 * Switch to using the specified PG from the next I/O that gets mapped
910 static int switch_pg_num(struct multipath *m, const char *pgstr)
912 struct priority_group *pg;
916 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
917 (pgnum > m->nr_priority_groups)) {
918 DMWARN("invalid PG number supplied to switch_pg_num");
922 spin_lock_irqsave(&m->lock, flags);
923 list_for_each_entry(pg, &m->priority_groups, list) {
928 m->current_pgpath = NULL;
929 m->current_pg = NULL;
932 spin_unlock_irqrestore(&m->lock, flags);
934 queue_work(kmultipathd, &m->trigger_event);
939 * Set/clear bypassed status of a PG.
940 * PGs are numbered upwards from 1 in the order they were declared.
942 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
944 struct priority_group *pg;
947 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
948 (pgnum > m->nr_priority_groups)) {
949 DMWARN("invalid PG number supplied to bypass_pg");
953 list_for_each_entry(pg, &m->priority_groups, list) {
958 bypass_pg(m, pg, bypassed);
963 * pg_init must call this when it has completed its initialisation
965 void dm_pg_init_complete(struct path *path, unsigned err_flags)
967 struct pgpath *pgpath = path_to_pgpath(path);
968 struct priority_group *pg = pgpath->pg;
969 struct multipath *m = pg->m;
972 /* We insist on failing the path if the PG is already bypassed. */
973 if (err_flags && pg->bypassed)
974 err_flags |= MP_FAIL_PATH;
976 if (err_flags & MP_FAIL_PATH)
979 if (err_flags & MP_BYPASS_PG)
982 spin_lock_irqsave(&m->lock, flags);
984 m->current_pgpath = NULL;
985 m->current_pg = NULL;
986 } else if (!m->pg_init_required)
989 m->pg_init_in_progress = 0;
990 queue_work(kmultipathd, &m->process_queued_ios);
991 spin_unlock_irqrestore(&m->lock, flags);
997 static int do_end_io(struct multipath *m, struct bio *bio,
998 int error, struct mpath_io *mpio)
1000 struct hw_handler *hwh = &m->hw_handler;
1001 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
1002 unsigned long flags;
1005 return 0; /* I/O complete */
1007 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1010 if (error == -EOPNOTSUPP)
1013 spin_lock_irqsave(&m->lock, flags);
1014 if (!m->nr_valid_paths) {
1015 if (!m->queue_if_no_path) {
1016 spin_unlock_irqrestore(&m->lock, flags);
1019 spin_unlock_irqrestore(&m->lock, flags);
1023 spin_unlock_irqrestore(&m->lock, flags);
1025 if (hwh->type && hwh->type->error)
1026 err_flags = hwh->type->error(hwh, bio);
1029 if (err_flags & MP_FAIL_PATH)
1030 fail_path(mpio->pgpath);
1032 if (err_flags & MP_BYPASS_PG)
1033 bypass_pg(m, mpio->pgpath->pg, 1);
1036 if (err_flags & MP_ERROR_IO)
1040 dm_bio_restore(&mpio->details, bio);
1042 /* queue for the daemon to resubmit or fail */
1043 spin_lock_irqsave(&m->lock, flags);
1044 bio_list_add(&m->queued_ios, bio);
1047 queue_work(kmultipathd, &m->process_queued_ios);
1048 spin_unlock_irqrestore(&m->lock, flags);
1050 return 1; /* io not complete */
1053 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1054 int error, union map_info *map_context)
1056 struct multipath *m = (struct multipath *) ti->private;
1057 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1058 struct pgpath *pgpath = mpio->pgpath;
1059 struct path_selector *ps;
1062 r = do_end_io(m, bio, error, mpio);
1064 ps = &pgpath->pg->ps;
1065 if (ps->type->end_io)
1066 ps->type->end_io(ps, &pgpath->path);
1069 mempool_free(mpio, m->mpio_pool);
1075 * Suspend can't complete until all the I/O is processed so if
1076 * the last path fails we must error any remaining I/O.
1077 * Note that if the freeze_bdev fails while suspending, the
1078 * queue_if_no_path state is lost - userspace should reset it.
1080 static void multipath_presuspend(struct dm_target *ti)
1082 struct multipath *m = (struct multipath *) ti->private;
1084 queue_if_no_path(m, 0, 1);
1088 * Restore the queue_if_no_path setting.
1090 static void multipath_resume(struct dm_target *ti)
1092 struct multipath *m = (struct multipath *) ti->private;
1093 unsigned long flags;
1095 spin_lock_irqsave(&m->lock, flags);
1096 m->queue_if_no_path = m->saved_queue_if_no_path;
1097 spin_unlock_irqrestore(&m->lock, flags);
1101 * Info output has the following format:
1102 * num_multipath_feature_args [multipath_feature_args]*
1103 * num_handler_status_args [handler_status_args]*
1104 * num_groups init_group_number
1105 * [A|D|E num_ps_status_args [ps_status_args]*
1106 * num_paths num_selector_args
1107 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1109 * Table output has the following format (identical to the constructor string):
1110 * num_feature_args [features_args]*
1111 * num_handler_args hw_handler [hw_handler_args]*
1112 * num_groups init_group_number
1113 * [priority selector-name num_ps_args [ps_args]*
1114 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1116 static int multipath_status(struct dm_target *ti, status_type_t type,
1117 char *result, unsigned int maxlen)
1120 unsigned long flags;
1121 struct multipath *m = (struct multipath *) ti->private;
1122 struct hw_handler *hwh = &m->hw_handler;
1123 struct priority_group *pg;
1128 spin_lock_irqsave(&m->lock, flags);
1131 if (type == STATUSTYPE_INFO)
1132 DMEMIT("1 %u ", m->queue_size);
1133 else if (m->queue_if_no_path)
1134 DMEMIT("1 queue_if_no_path ");
1138 if (hwh->type && hwh->type->status)
1139 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1140 else if (!hwh->type || type == STATUSTYPE_INFO)
1143 DMEMIT("1 %s ", hwh->type->name);
1145 DMEMIT("%u ", m->nr_priority_groups);
1148 pg_num = m->next_pg->pg_num;
1149 else if (m->current_pg)
1150 pg_num = m->current_pg->pg_num;
1154 DMEMIT("%u ", pg_num);
1157 case STATUSTYPE_INFO:
1158 list_for_each_entry(pg, &m->priority_groups, list) {
1160 state = 'D'; /* Disabled */
1161 else if (pg == m->current_pg)
1162 state = 'A'; /* Currently Active */
1164 state = 'E'; /* Enabled */
1166 DMEMIT("%c ", state);
1168 if (pg->ps.type->status)
1169 sz += pg->ps.type->status(&pg->ps, NULL, type,
1175 DMEMIT("%u %u ", pg->nr_pgpaths,
1176 pg->ps.type->info_args);
1178 list_for_each_entry(p, &pg->pgpaths, list) {
1179 DMEMIT("%s %s %u ", p->path.dev->name,
1180 p->path.is_active ? "A" : "F",
1182 if (pg->ps.type->status)
1183 sz += pg->ps.type->status(&pg->ps,
1184 &p->path, type, result + sz,
1190 case STATUSTYPE_TABLE:
1191 list_for_each_entry(pg, &m->priority_groups, list) {
1192 DMEMIT("%s ", pg->ps.type->name);
1194 if (pg->ps.type->status)
1195 sz += pg->ps.type->status(&pg->ps, NULL, type,
1201 DMEMIT("%u %u ", pg->nr_pgpaths,
1202 pg->ps.type->table_args);
1204 list_for_each_entry(p, &pg->pgpaths, list) {
1205 DMEMIT("%s ", p->path.dev->name);
1206 if (pg->ps.type->status)
1207 sz += pg->ps.type->status(&pg->ps,
1208 &p->path, type, result + sz,
1215 spin_unlock_irqrestore(&m->lock, flags);
1220 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1224 struct multipath *m = (struct multipath *) ti->private;
1228 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1229 return queue_if_no_path(m, 1, 0);
1230 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1231 return queue_if_no_path(m, 0, 0);
1237 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1238 return bypass_pg_num(m, argv[1], 1);
1239 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1240 return bypass_pg_num(m, argv[1], 0);
1241 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1242 return switch_pg_num(m, argv[1]);
1243 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1244 action = reinstate_path;
1245 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1250 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1251 dm_table_get_mode(ti->table), &dev);
1253 DMWARN("dm-multipath message: error getting device %s",
1258 r = action_dev(m, dev, action);
1260 dm_put_device(ti, dev);
1265 DMWARN("Unrecognised multipath message received.");
1269 /*-----------------------------------------------------------------
1271 *---------------------------------------------------------------*/
1272 static struct target_type multipath_target = {
1273 .name = "multipath",
1274 .version = {1, 0, 4},
1275 .module = THIS_MODULE,
1276 .ctr = multipath_ctr,
1277 .dtr = multipath_dtr,
1278 .map = multipath_map,
1279 .end_io = multipath_end_io,
1280 .presuspend = multipath_presuspend,
1281 .resume = multipath_resume,
1282 .status = multipath_status,
1283 .message = multipath_message,
1286 static int __init dm_multipath_init(void)
1290 /* allocate a slab for the dm_ios */
1291 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1296 r = dm_register_target(&multipath_target);
1298 DMERR("%s: register failed %d", multipath_target.name, r);
1299 kmem_cache_destroy(_mpio_cache);
1303 kmultipathd = create_workqueue("kmpathd");
1305 DMERR("%s: failed to create workqueue kmpathd",
1306 multipath_target.name);
1307 dm_unregister_target(&multipath_target);
1308 kmem_cache_destroy(_mpio_cache);
1312 DMINFO("dm-multipath version %u.%u.%u loaded",
1313 multipath_target.version[0], multipath_target.version[1],
1314 multipath_target.version[2]);
1319 static void __exit dm_multipath_exit(void)
1323 destroy_workqueue(kmultipathd);
1325 r = dm_unregister_target(&multipath_target);
1327 DMERR("%s: target unregister failed %d",
1328 multipath_target.name, r);
1329 kmem_cache_destroy(_mpio_cache);
1332 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1334 module_init(dm_multipath_init);
1335 module_exit(dm_multipath_exit);
1337 MODULE_DESCRIPTION(DM_NAME " multipath target");
1338 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1339 MODULE_LICENSE("GPL");