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 DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
29 struct list_head list;
31 struct priority_group *pg; /* Owning PG */
32 unsigned fail_count; /* Cumulative failure count */
37 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40 * Paths are grouped into Priority Groups and numbered from 1 upwards.
41 * Each has a path selector which controls which path gets used.
43 struct priority_group {
44 struct list_head list;
46 struct multipath *m; /* Owning multipath instance */
47 struct path_selector ps;
49 unsigned pg_num; /* Reference number */
50 unsigned bypassed; /* Temporarily bypass this PG? */
52 unsigned nr_pgpaths; /* Number of paths in PG */
53 struct list_head pgpaths;
56 /* Multipath context */
58 struct list_head list;
63 struct hw_handler hw_handler;
64 unsigned nr_priority_groups;
65 struct list_head priority_groups;
66 unsigned pg_init_required; /* pg_init needs calling? */
67 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
69 unsigned nr_valid_paths; /* Total number of usable paths */
70 struct pgpath *current_pgpath;
71 struct priority_group *current_pg;
72 struct priority_group *next_pg; /* Switch to this PG if set */
73 unsigned repeat_count; /* I/Os left before calling PS again */
75 unsigned queue_io; /* Must we queue all I/O? */
76 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
77 unsigned saved_queue_if_no_path;/* Saved state during suspension */
78 unsigned pg_init_retries; /* Number of times to retry pg_init */
79 unsigned pg_init_count; /* Number of times pg_init called */
81 struct work_struct process_queued_ios;
82 struct bio_list queued_ios;
85 struct work_struct trigger_event;
88 * We must use a mempool of dm_mpath_io structs so that we
89 * can resubmit bios on error.
95 * Context information attached to each bio we process.
98 struct pgpath *pgpath;
99 struct dm_bio_details details;
102 typedef int (*action_fn) (struct pgpath *pgpath);
104 #define MIN_IOS 256 /* Mempool size */
106 static struct kmem_cache *_mpio_cache;
108 struct workqueue_struct *kmultipathd;
109 static void process_queued_ios(struct work_struct *work);
110 static void trigger_event(struct work_struct *work);
113 /*-----------------------------------------------
114 * Allocation routines
115 *-----------------------------------------------*/
117 static struct pgpath *alloc_pgpath(void)
119 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
122 pgpath->path.is_active = 1;
127 static void free_pgpath(struct pgpath *pgpath)
132 static struct priority_group *alloc_priority_group(void)
134 struct priority_group *pg;
136 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
139 INIT_LIST_HEAD(&pg->pgpaths);
144 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
146 struct pgpath *pgpath, *tmp;
148 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
149 list_del(&pgpath->list);
150 dm_put_device(ti, pgpath->path.dev);
155 static void free_priority_group(struct priority_group *pg,
156 struct dm_target *ti)
158 struct path_selector *ps = &pg->ps;
161 ps->type->destroy(ps);
162 dm_put_path_selector(ps->type);
165 free_pgpaths(&pg->pgpaths, ti);
169 static struct multipath *alloc_multipath(struct dm_target *ti)
173 m = kzalloc(sizeof(*m), GFP_KERNEL);
175 INIT_LIST_HEAD(&m->priority_groups);
176 spin_lock_init(&m->lock);
178 INIT_WORK(&m->process_queued_ios, process_queued_ios);
179 INIT_WORK(&m->trigger_event, trigger_event);
180 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;
231 m->pg_init_count = 0;
234 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
236 struct dm_path *path;
238 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
242 m->current_pgpath = path_to_pgpath(path);
244 if (m->current_pg != pg)
245 __switch_pg(m, m->current_pgpath);
250 static void __choose_pgpath(struct multipath *m)
252 struct priority_group *pg;
253 unsigned bypassed = 1;
255 if (!m->nr_valid_paths)
258 /* Were we instructed to switch PG? */
262 if (!__choose_path_in_pg(m, pg))
266 /* Don't change PG until it has no remaining paths */
267 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
271 * Loop through priority groups until we find a valid path.
272 * First time we skip PGs marked 'bypassed'.
273 * Second time we only try the ones we skipped.
276 list_for_each_entry(pg, &m->priority_groups, list) {
277 if (pg->bypassed == bypassed)
279 if (!__choose_path_in_pg(m, pg))
282 } while (bypassed--);
285 m->current_pgpath = NULL;
286 m->current_pg = NULL;
290 * Check whether bios must be queued in the device-mapper core rather
291 * than here in the target.
293 * m->lock must be held on entry.
295 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
296 * same value then we are not between multipath_presuspend()
297 * and multipath_resume() calls and we have no need to check
298 * for the DMF_NOFLUSH_SUSPENDING flag.
300 static int __must_push_back(struct multipath *m)
302 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
303 dm_noflush_suspending(m->ti));
306 static int map_io(struct multipath *m, struct bio *bio,
307 struct dm_mpath_io *mpio, unsigned was_queued)
309 int r = DM_MAPIO_REMAPPED;
311 struct pgpath *pgpath;
313 spin_lock_irqsave(&m->lock, flags);
315 /* Do we need to select a new pgpath? */
316 if (!m->current_pgpath ||
317 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
320 pgpath = m->current_pgpath;
325 if ((pgpath && m->queue_io) ||
326 (!pgpath && m->queue_if_no_path)) {
327 /* Queue for the daemon to resubmit */
328 bio_list_add(&m->queued_ios, bio);
330 if ((m->pg_init_required && !m->pg_init_in_progress) ||
332 queue_work(kmultipathd, &m->process_queued_ios);
334 r = DM_MAPIO_SUBMITTED;
336 bio->bi_bdev = pgpath->path.dev->bdev;
337 else if (__must_push_back(m))
338 r = DM_MAPIO_REQUEUE;
340 r = -EIO; /* Failed */
342 mpio->pgpath = pgpath;
344 spin_unlock_irqrestore(&m->lock, flags);
350 * If we run out of usable paths, should we queue I/O or error it?
352 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
353 unsigned save_old_value)
357 spin_lock_irqsave(&m->lock, flags);
360 m->saved_queue_if_no_path = m->queue_if_no_path;
362 m->saved_queue_if_no_path = queue_if_no_path;
363 m->queue_if_no_path = queue_if_no_path;
364 if (!m->queue_if_no_path && m->queue_size)
365 queue_work(kmultipathd, &m->process_queued_ios);
367 spin_unlock_irqrestore(&m->lock, flags);
372 /*-----------------------------------------------------------------
373 * The multipath daemon is responsible for resubmitting queued ios.
374 *---------------------------------------------------------------*/
376 static void dispatch_queued_ios(struct multipath *m)
380 struct bio *bio = NULL, *next;
381 struct dm_mpath_io *mpio;
382 union map_info *info;
384 spin_lock_irqsave(&m->lock, flags);
385 bio = bio_list_get(&m->queued_ios);
386 spin_unlock_irqrestore(&m->lock, flags);
392 info = dm_get_mapinfo(bio);
395 r = map_io(m, bio, mpio, 1);
398 else if (r == DM_MAPIO_REMAPPED)
399 generic_make_request(bio);
400 else if (r == DM_MAPIO_REQUEUE)
401 bio_endio(bio, -EIO);
407 static void process_queued_ios(struct work_struct *work)
409 struct multipath *m =
410 container_of(work, struct multipath, process_queued_ios);
411 struct hw_handler *hwh = &m->hw_handler;
412 struct pgpath *pgpath = NULL;
413 unsigned init_required = 0, must_queue = 1;
416 spin_lock_irqsave(&m->lock, flags);
421 if (!m->current_pgpath)
424 pgpath = m->current_pgpath;
426 if ((pgpath && !m->queue_io) ||
427 (!pgpath && !m->queue_if_no_path))
430 if (m->pg_init_required && !m->pg_init_in_progress) {
432 m->pg_init_required = 0;
433 m->pg_init_in_progress = 1;
438 spin_unlock_irqrestore(&m->lock, flags);
441 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
444 dispatch_queued_ios(m);
448 * An event is triggered whenever a path is taken out of use.
449 * Includes path failure and PG bypass.
451 static void trigger_event(struct work_struct *work)
453 struct multipath *m =
454 container_of(work, struct multipath, trigger_event);
456 dm_table_event(m->ti->table);
459 /*-----------------------------------------------------------------
460 * Constructor/argument parsing:
461 * <#multipath feature args> [<arg>]*
462 * <#hw_handler args> [hw_handler [<arg>]*]
464 * <initial priority group>
465 * [<selector> <#selector args> [<arg>]*
466 * <#paths> <#per-path selector args>
467 * [<path> [<arg>]* ]+ ]+
468 *---------------------------------------------------------------*/
475 static int read_param(struct param *param, char *str, unsigned *v, char **error)
478 (sscanf(str, "%u", v) != 1) ||
481 *error = param->error;
493 static char *shift(struct arg_set *as)
507 static void consume(struct arg_set *as, unsigned n)
509 BUG_ON (as->argc < n);
514 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
515 struct dm_target *ti)
518 struct path_selector_type *pst;
521 static struct param _params[] = {
522 {0, 1024, "invalid number of path selector args"},
525 pst = dm_get_path_selector(shift(as));
527 ti->error = "unknown path selector type";
531 r = read_param(_params, shift(as), &ps_argc, &ti->error);
535 r = pst->create(&pg->ps, ps_argc, as->argv);
537 dm_put_path_selector(pst);
538 ti->error = "path selector constructor failed";
543 consume(as, ps_argc);
548 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
549 struct dm_target *ti)
554 /* we need at least a path arg */
556 ti->error = "no device given";
564 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
565 dm_table_get_mode(ti->table), &p->path.dev);
567 ti->error = "error getting device";
571 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
573 dm_put_device(ti, p->path.dev);
584 static struct priority_group *parse_priority_group(struct arg_set *as,
587 static struct param _params[] = {
588 {1, 1024, "invalid number of paths"},
589 {0, 1024, "invalid number of selector args"}
593 unsigned i, nr_selector_args, nr_params;
594 struct priority_group *pg;
595 struct dm_target *ti = m->ti;
599 ti->error = "not enough priority group aruments";
603 pg = alloc_priority_group();
605 ti->error = "couldn't allocate priority group";
610 r = parse_path_selector(as, pg, ti);
617 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
621 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
625 nr_params = 1 + nr_selector_args;
626 for (i = 0; i < pg->nr_pgpaths; i++) {
627 struct pgpath *pgpath;
628 struct arg_set path_args;
630 if (as->argc < nr_params)
633 path_args.argc = nr_params;
634 path_args.argv = as->argv;
636 pgpath = parse_path(&path_args, &pg->ps, ti);
641 list_add_tail(&pgpath->list, &pg->pgpaths);
642 consume(as, nr_params);
648 free_priority_group(pg, ti);
652 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
655 struct hw_handler_type *hwht;
657 struct dm_target *ti = m->ti;
659 static struct param _params[] = {
660 {0, 1024, "invalid number of hardware handler args"},
663 r = read_param(_params, shift(as), &hw_argc, &ti->error);
670 hwht = dm_get_hw_handler(shift(as));
672 ti->error = "unknown hardware handler type";
676 m->hw_handler.md = dm_table_get_md(ti->table);
677 dm_put(m->hw_handler.md);
679 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
681 dm_put_hw_handler(hwht);
682 ti->error = "hardware handler constructor failed";
686 m->hw_handler.type = hwht;
687 consume(as, hw_argc - 1);
692 static int parse_features(struct arg_set *as, struct multipath *m)
696 struct dm_target *ti = m->ti;
697 const char *param_name;
699 static struct param _params[] = {
700 {0, 3, "invalid number of feature args"},
701 {1, 50, "pg_init_retries must be between 1 and 50"},
704 r = read_param(_params, shift(as), &argc, &ti->error);
712 param_name = shift(as);
715 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
716 r = queue_if_no_path(m, 1, 0);
720 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
722 r = read_param(_params + 1, shift(as),
723 &m->pg_init_retries, &ti->error);
728 ti->error = "Unrecognised multipath feature request";
730 } while (argc && !r);
735 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
738 /* target parameters */
739 static struct param _params[] = {
740 {1, 1024, "invalid number of priority groups"},
741 {1, 1024, "invalid initial priority group number"},
747 unsigned pg_count = 0;
748 unsigned next_pg_num;
753 m = alloc_multipath(ti);
755 ti->error = "can't allocate multipath";
759 r = parse_features(&as, m);
763 r = parse_hw_handler(&as, m);
767 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
771 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
775 /* parse the priority groups */
777 struct priority_group *pg;
779 pg = parse_priority_group(&as, m);
785 m->nr_valid_paths += pg->nr_pgpaths;
786 list_add_tail(&pg->list, &m->priority_groups);
788 pg->pg_num = pg_count;
793 if (pg_count != m->nr_priority_groups) {
794 ti->error = "priority group count mismatch";
806 static void multipath_dtr(struct dm_target *ti)
808 struct multipath *m = (struct multipath *) ti->private;
810 flush_workqueue(kmultipathd);
815 * Map bios, recording original fields for later in case we have to resubmit
817 static int multipath_map(struct dm_target *ti, struct bio *bio,
818 union map_info *map_context)
821 struct dm_mpath_io *mpio;
822 struct multipath *m = (struct multipath *) ti->private;
824 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
825 dm_bio_record(&mpio->details, bio);
827 map_context->ptr = mpio;
828 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
829 r = map_io(m, bio, mpio, 0);
830 if (r < 0 || r == DM_MAPIO_REQUEUE)
831 mempool_free(mpio, m->mpio_pool);
837 * Take a path out of use.
839 static int fail_path(struct pgpath *pgpath)
842 struct multipath *m = pgpath->pg->m;
844 spin_lock_irqsave(&m->lock, flags);
846 if (!pgpath->path.is_active)
849 DMWARN("Failing path %s.", pgpath->path.dev->name);
851 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
852 pgpath->path.is_active = 0;
853 pgpath->fail_count++;
857 if (pgpath == m->current_pgpath)
858 m->current_pgpath = NULL;
860 queue_work(kmultipathd, &m->trigger_event);
863 spin_unlock_irqrestore(&m->lock, flags);
869 * Reinstate a previously-failed path
871 static int reinstate_path(struct pgpath *pgpath)
875 struct multipath *m = pgpath->pg->m;
877 spin_lock_irqsave(&m->lock, flags);
879 if (pgpath->path.is_active)
882 if (!pgpath->pg->ps.type) {
883 DMWARN("Reinstate path not supported by path selector %s",
884 pgpath->pg->ps.type->name);
889 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
893 pgpath->path.is_active = 1;
895 m->current_pgpath = NULL;
896 if (!m->nr_valid_paths++ && m->queue_size)
897 queue_work(kmultipathd, &m->process_queued_ios);
899 queue_work(kmultipathd, &m->trigger_event);
902 spin_unlock_irqrestore(&m->lock, flags);
908 * Fail or reinstate all paths that match the provided struct dm_dev.
910 static int action_dev(struct multipath *m, struct dm_dev *dev,
914 struct pgpath *pgpath;
915 struct priority_group *pg;
917 list_for_each_entry(pg, &m->priority_groups, list) {
918 list_for_each_entry(pgpath, &pg->pgpaths, list) {
919 if (pgpath->path.dev == dev)
928 * Temporarily try to avoid having to use the specified PG
930 static void bypass_pg(struct multipath *m, struct priority_group *pg,
935 spin_lock_irqsave(&m->lock, flags);
937 pg->bypassed = bypassed;
938 m->current_pgpath = NULL;
939 m->current_pg = NULL;
941 spin_unlock_irqrestore(&m->lock, flags);
943 queue_work(kmultipathd, &m->trigger_event);
947 * Switch to using the specified PG from the next I/O that gets mapped
949 static int switch_pg_num(struct multipath *m, const char *pgstr)
951 struct priority_group *pg;
955 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
956 (pgnum > m->nr_priority_groups)) {
957 DMWARN("invalid PG number supplied to switch_pg_num");
961 spin_lock_irqsave(&m->lock, flags);
962 list_for_each_entry(pg, &m->priority_groups, list) {
967 m->current_pgpath = NULL;
968 m->current_pg = NULL;
971 spin_unlock_irqrestore(&m->lock, flags);
973 queue_work(kmultipathd, &m->trigger_event);
978 * Set/clear bypassed status of a PG.
979 * PGs are numbered upwards from 1 in the order they were declared.
981 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
983 struct priority_group *pg;
986 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
987 (pgnum > m->nr_priority_groups)) {
988 DMWARN("invalid PG number supplied to bypass_pg");
992 list_for_each_entry(pg, &m->priority_groups, list) {
997 bypass_pg(m, pg, bypassed);
1002 * Should we retry pg_init immediately?
1004 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1006 unsigned long flags;
1007 int limit_reached = 0;
1009 spin_lock_irqsave(&m->lock, flags);
1011 if (m->pg_init_count <= m->pg_init_retries)
1012 m->pg_init_required = 1;
1016 spin_unlock_irqrestore(&m->lock, flags);
1018 return limit_reached;
1022 * pg_init must call this when it has completed its initialisation
1024 void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
1026 struct pgpath *pgpath = path_to_pgpath(path);
1027 struct priority_group *pg = pgpath->pg;
1028 struct multipath *m = pg->m;
1029 unsigned long flags;
1032 * If requested, retry pg_init until maximum number of retries exceeded.
1033 * If retry not requested and PG already bypassed, always fail the path.
1035 if (err_flags & MP_RETRY) {
1036 if (pg_init_limit_reached(m, pgpath))
1037 err_flags |= MP_FAIL_PATH;
1038 } else if (err_flags && pg->bypassed)
1039 err_flags |= MP_FAIL_PATH;
1041 if (err_flags & MP_FAIL_PATH)
1044 if (err_flags & MP_BYPASS_PG)
1045 bypass_pg(m, pg, 1);
1047 spin_lock_irqsave(&m->lock, flags);
1048 if (err_flags & ~MP_RETRY) {
1049 m->current_pgpath = NULL;
1050 m->current_pg = NULL;
1051 } else if (!m->pg_init_required)
1054 m->pg_init_in_progress = 0;
1055 queue_work(kmultipathd, &m->process_queued_ios);
1056 spin_unlock_irqrestore(&m->lock, flags);
1062 static int do_end_io(struct multipath *m, struct bio *bio,
1063 int error, struct dm_mpath_io *mpio)
1065 struct hw_handler *hwh = &m->hw_handler;
1066 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
1067 unsigned long flags;
1070 return 0; /* I/O complete */
1072 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1075 if (error == -EOPNOTSUPP)
1078 spin_lock_irqsave(&m->lock, flags);
1079 if (!m->nr_valid_paths) {
1080 if (__must_push_back(m)) {
1081 spin_unlock_irqrestore(&m->lock, flags);
1082 return DM_ENDIO_REQUEUE;
1083 } else if (!m->queue_if_no_path) {
1084 spin_unlock_irqrestore(&m->lock, flags);
1087 spin_unlock_irqrestore(&m->lock, flags);
1091 spin_unlock_irqrestore(&m->lock, flags);
1093 if (hwh->type && hwh->type->error)
1094 err_flags = hwh->type->error(hwh, bio);
1097 if (err_flags & MP_FAIL_PATH)
1098 fail_path(mpio->pgpath);
1100 if (err_flags & MP_BYPASS_PG)
1101 bypass_pg(m, mpio->pgpath->pg, 1);
1104 if (err_flags & MP_ERROR_IO)
1108 dm_bio_restore(&mpio->details, bio);
1110 /* queue for the daemon to resubmit or fail */
1111 spin_lock_irqsave(&m->lock, flags);
1112 bio_list_add(&m->queued_ios, bio);
1115 queue_work(kmultipathd, &m->process_queued_ios);
1116 spin_unlock_irqrestore(&m->lock, flags);
1118 return DM_ENDIO_INCOMPLETE; /* io not complete */
1121 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1122 int error, union map_info *map_context)
1124 struct multipath *m = ti->private;
1125 struct dm_mpath_io *mpio = map_context->ptr;
1126 struct pgpath *pgpath = mpio->pgpath;
1127 struct path_selector *ps;
1130 r = do_end_io(m, bio, error, mpio);
1132 ps = &pgpath->pg->ps;
1133 if (ps->type->end_io)
1134 ps->type->end_io(ps, &pgpath->path);
1136 if (r != DM_ENDIO_INCOMPLETE)
1137 mempool_free(mpio, m->mpio_pool);
1143 * Suspend can't complete until all the I/O is processed so if
1144 * the last path fails we must error any remaining I/O.
1145 * Note that if the freeze_bdev fails while suspending, the
1146 * queue_if_no_path state is lost - userspace should reset it.
1148 static void multipath_presuspend(struct dm_target *ti)
1150 struct multipath *m = (struct multipath *) ti->private;
1152 queue_if_no_path(m, 0, 1);
1156 * Restore the queue_if_no_path setting.
1158 static void multipath_resume(struct dm_target *ti)
1160 struct multipath *m = (struct multipath *) ti->private;
1161 unsigned long flags;
1163 spin_lock_irqsave(&m->lock, flags);
1164 m->queue_if_no_path = m->saved_queue_if_no_path;
1165 spin_unlock_irqrestore(&m->lock, flags);
1169 * Info output has the following format:
1170 * num_multipath_feature_args [multipath_feature_args]*
1171 * num_handler_status_args [handler_status_args]*
1172 * num_groups init_group_number
1173 * [A|D|E num_ps_status_args [ps_status_args]*
1174 * num_paths num_selector_args
1175 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1177 * Table output has the following format (identical to the constructor string):
1178 * num_feature_args [features_args]*
1179 * num_handler_args hw_handler [hw_handler_args]*
1180 * num_groups init_group_number
1181 * [priority selector-name num_ps_args [ps_args]*
1182 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1184 static int multipath_status(struct dm_target *ti, status_type_t type,
1185 char *result, unsigned int maxlen)
1188 unsigned long flags;
1189 struct multipath *m = (struct multipath *) ti->private;
1190 struct hw_handler *hwh = &m->hw_handler;
1191 struct priority_group *pg;
1196 spin_lock_irqsave(&m->lock, flags);
1199 if (type == STATUSTYPE_INFO)
1200 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1202 DMEMIT("%u ", m->queue_if_no_path +
1203 (m->pg_init_retries > 0) * 2);
1204 if (m->queue_if_no_path)
1205 DMEMIT("queue_if_no_path ");
1206 if (m->pg_init_retries)
1207 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1210 if (hwh->type && hwh->type->status)
1211 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1212 else if (!hwh->type || type == STATUSTYPE_INFO)
1215 DMEMIT("1 %s ", hwh->type->name);
1217 DMEMIT("%u ", m->nr_priority_groups);
1220 pg_num = m->next_pg->pg_num;
1221 else if (m->current_pg)
1222 pg_num = m->current_pg->pg_num;
1226 DMEMIT("%u ", pg_num);
1229 case STATUSTYPE_INFO:
1230 list_for_each_entry(pg, &m->priority_groups, list) {
1232 state = 'D'; /* Disabled */
1233 else if (pg == m->current_pg)
1234 state = 'A'; /* Currently Active */
1236 state = 'E'; /* Enabled */
1238 DMEMIT("%c ", state);
1240 if (pg->ps.type->status)
1241 sz += pg->ps.type->status(&pg->ps, NULL, type,
1247 DMEMIT("%u %u ", pg->nr_pgpaths,
1248 pg->ps.type->info_args);
1250 list_for_each_entry(p, &pg->pgpaths, list) {
1251 DMEMIT("%s %s %u ", p->path.dev->name,
1252 p->path.is_active ? "A" : "F",
1254 if (pg->ps.type->status)
1255 sz += pg->ps.type->status(&pg->ps,
1256 &p->path, type, result + sz,
1262 case STATUSTYPE_TABLE:
1263 list_for_each_entry(pg, &m->priority_groups, list) {
1264 DMEMIT("%s ", pg->ps.type->name);
1266 if (pg->ps.type->status)
1267 sz += pg->ps.type->status(&pg->ps, NULL, type,
1273 DMEMIT("%u %u ", pg->nr_pgpaths,
1274 pg->ps.type->table_args);
1276 list_for_each_entry(p, &pg->pgpaths, list) {
1277 DMEMIT("%s ", p->path.dev->name);
1278 if (pg->ps.type->status)
1279 sz += pg->ps.type->status(&pg->ps,
1280 &p->path, type, result + sz,
1287 spin_unlock_irqrestore(&m->lock, flags);
1292 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1296 struct multipath *m = (struct multipath *) ti->private;
1300 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1301 return queue_if_no_path(m, 1, 0);
1302 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1303 return queue_if_no_path(m, 0, 0);
1309 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1310 return bypass_pg_num(m, argv[1], 1);
1311 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1312 return bypass_pg_num(m, argv[1], 0);
1313 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1314 return switch_pg_num(m, argv[1]);
1315 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1316 action = reinstate_path;
1317 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1322 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1323 dm_table_get_mode(ti->table), &dev);
1325 DMWARN("message: error getting device %s",
1330 r = action_dev(m, dev, action);
1332 dm_put_device(ti, dev);
1337 DMWARN("Unrecognised multipath message received.");
1341 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1342 struct file *filp, unsigned int cmd,
1345 struct multipath *m = (struct multipath *) ti->private;
1346 struct block_device *bdev = NULL;
1347 unsigned long flags;
1348 struct file fake_file = {};
1349 struct dentry fake_dentry = {};
1352 fake_file.f_path.dentry = &fake_dentry;
1354 spin_lock_irqsave(&m->lock, flags);
1356 if (!m->current_pgpath)
1359 if (m->current_pgpath) {
1360 bdev = m->current_pgpath->path.dev->bdev;
1361 fake_dentry.d_inode = bdev->bd_inode;
1362 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1370 spin_unlock_irqrestore(&m->lock, flags);
1372 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1373 bdev->bd_disk, cmd, arg);
1376 /*-----------------------------------------------------------------
1378 *---------------------------------------------------------------*/
1379 static struct target_type multipath_target = {
1380 .name = "multipath",
1381 .version = {1, 0, 5},
1382 .module = THIS_MODULE,
1383 .ctr = multipath_ctr,
1384 .dtr = multipath_dtr,
1385 .map = multipath_map,
1386 .end_io = multipath_end_io,
1387 .presuspend = multipath_presuspend,
1388 .resume = multipath_resume,
1389 .status = multipath_status,
1390 .message = multipath_message,
1391 .ioctl = multipath_ioctl,
1394 static int __init dm_multipath_init(void)
1398 /* allocate a slab for the dm_ios */
1399 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1403 r = dm_register_target(&multipath_target);
1405 DMERR("register failed %d", r);
1406 kmem_cache_destroy(_mpio_cache);
1410 kmultipathd = create_workqueue("kmpathd");
1412 DMERR("failed to create workqueue kmpathd");
1413 dm_unregister_target(&multipath_target);
1414 kmem_cache_destroy(_mpio_cache);
1418 DMINFO("version %u.%u.%u loaded",
1419 multipath_target.version[0], multipath_target.version[1],
1420 multipath_target.version[2]);
1425 static void __exit dm_multipath_exit(void)
1429 destroy_workqueue(kmultipathd);
1431 r = dm_unregister_target(&multipath_target);
1433 DMERR("target unregister failed %d", r);
1434 kmem_cache_destroy(_mpio_cache);
1437 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1439 module_init(dm_multipath_init);
1440 module_exit(dm_multipath_exit);
1442 MODULE_DESCRIPTION(DM_NAME " multipath target");
1443 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1444 MODULE_LICENSE("GPL");