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"
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 fail_count; /* Cumulative failure count */
39 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
45 struct priority_group {
46 struct list_head list;
48 struct multipath *m; /* Owning multipath instance */
49 struct path_selector ps;
51 unsigned pg_num; /* Reference number */
52 unsigned bypassed; /* Temporarily bypass this PG? */
54 unsigned nr_pgpaths; /* Number of paths in PG */
55 struct list_head pgpaths;
58 /* Multipath context */
60 struct list_head list;
65 const char *hw_handler_name;
66 struct work_struct activate_path;
67 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
70 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
72 unsigned nr_valid_paths; /* Total number of usable paths */
73 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
76 unsigned repeat_count; /* I/Os left before calling PS again */
78 unsigned queue_io; /* Must we queue all I/O? */
79 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
80 unsigned saved_queue_if_no_path;/* Saved state during suspension */
81 unsigned pg_init_retries; /* Number of times to retry pg_init */
82 unsigned pg_init_count; /* Number of times pg_init called */
84 struct work_struct process_queued_ios;
85 struct bio_list queued_ios;
88 struct work_struct trigger_event;
91 * We must use a mempool of dm_mpath_io structs so that we
92 * can resubmit bios on error.
98 * Context information attached to each bio we process.
101 struct pgpath *pgpath;
102 struct dm_bio_details details;
105 typedef int (*action_fn) (struct pgpath *pgpath);
107 #define MIN_IOS 256 /* Mempool size */
109 static struct kmem_cache *_mpio_cache;
111 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
112 static void process_queued_ios(struct work_struct *work);
113 static void trigger_event(struct work_struct *work);
114 static void activate_path(struct work_struct *work);
117 /*-----------------------------------------------
118 * Allocation routines
119 *-----------------------------------------------*/
121 static struct pgpath *alloc_pgpath(void)
123 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
126 pgpath->path.is_active = 1;
131 static void free_pgpath(struct pgpath *pgpath)
136 static struct priority_group *alloc_priority_group(void)
138 struct priority_group *pg;
140 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
143 INIT_LIST_HEAD(&pg->pgpaths);
148 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
150 struct pgpath *pgpath, *tmp;
152 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
153 list_del(&pgpath->list);
154 dm_put_device(ti, pgpath->path.dev);
159 static void free_priority_group(struct priority_group *pg,
160 struct dm_target *ti)
162 struct path_selector *ps = &pg->ps;
165 ps->type->destroy(ps);
166 dm_put_path_selector(ps->type);
169 free_pgpaths(&pg->pgpaths, ti);
173 static struct multipath *alloc_multipath(struct dm_target *ti)
177 m = kzalloc(sizeof(*m), GFP_KERNEL);
179 INIT_LIST_HEAD(&m->priority_groups);
180 spin_lock_init(&m->lock);
182 INIT_WORK(&m->process_queued_ios, process_queued_ios);
183 INIT_WORK(&m->trigger_event, trigger_event);
184 INIT_WORK(&m->activate_path, activate_path);
185 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
197 static void free_multipath(struct multipath *m)
199 struct priority_group *pg, *tmp;
201 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
203 free_priority_group(pg, m->ti);
206 kfree(m->hw_handler_name);
207 mempool_destroy(m->mpio_pool);
212 /*-----------------------------------------------
214 *-----------------------------------------------*/
216 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
218 m->current_pg = pgpath->pg;
220 /* Must we initialise the PG first, and queue I/O till it's ready? */
221 if (m->hw_handler_name) {
222 m->pg_init_required = 1;
225 m->pg_init_required = 0;
229 m->pg_init_count = 0;
232 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
234 struct dm_path *path;
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;
288 * Check whether bios must be queued in the device-mapper core rather
289 * than here in the target.
291 * m->lock must be held on entry.
293 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
294 * same value then we are not between multipath_presuspend()
295 * and multipath_resume() calls and we have no need to check
296 * for the DMF_NOFLUSH_SUSPENDING flag.
298 static int __must_push_back(struct multipath *m)
300 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
301 dm_noflush_suspending(m->ti));
304 static int map_io(struct multipath *m, struct bio *bio,
305 struct dm_mpath_io *mpio, unsigned was_queued)
307 int r = DM_MAPIO_REMAPPED;
309 struct pgpath *pgpath;
311 spin_lock_irqsave(&m->lock, flags);
313 /* Do we need to select a new pgpath? */
314 if (!m->current_pgpath ||
315 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
318 pgpath = m->current_pgpath;
323 if ((pgpath && m->queue_io) ||
324 (!pgpath && m->queue_if_no_path)) {
325 /* Queue for the daemon to resubmit */
326 bio_list_add(&m->queued_ios, bio);
328 if ((m->pg_init_required && !m->pg_init_in_progress) ||
330 queue_work(kmultipathd, &m->process_queued_ios);
332 r = DM_MAPIO_SUBMITTED;
334 bio->bi_bdev = pgpath->path.dev->bdev;
335 else if (__must_push_back(m))
336 r = DM_MAPIO_REQUEUE;
338 r = -EIO; /* Failed */
340 mpio->pgpath = pgpath;
342 spin_unlock_irqrestore(&m->lock, flags);
348 * If we run out of usable paths, should we queue I/O or error it?
350 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
351 unsigned save_old_value)
355 spin_lock_irqsave(&m->lock, flags);
358 m->saved_queue_if_no_path = m->queue_if_no_path;
360 m->saved_queue_if_no_path = queue_if_no_path;
361 m->queue_if_no_path = queue_if_no_path;
362 if (!m->queue_if_no_path && m->queue_size)
363 queue_work(kmultipathd, &m->process_queued_ios);
365 spin_unlock_irqrestore(&m->lock, flags);
370 /*-----------------------------------------------------------------
371 * The multipath daemon is responsible for resubmitting queued ios.
372 *---------------------------------------------------------------*/
374 static void dispatch_queued_ios(struct multipath *m)
378 struct bio *bio = NULL, *next;
379 struct dm_mpath_io *mpio;
380 union map_info *info;
382 spin_lock_irqsave(&m->lock, flags);
383 bio = bio_list_get(&m->queued_ios);
384 spin_unlock_irqrestore(&m->lock, flags);
390 info = dm_get_mapinfo(bio);
393 r = map_io(m, bio, mpio, 1);
396 else if (r == DM_MAPIO_REMAPPED)
397 generic_make_request(bio);
398 else if (r == DM_MAPIO_REQUEUE)
399 bio_endio(bio, -EIO);
405 static void process_queued_ios(struct work_struct *work)
407 struct multipath *m =
408 container_of(work, struct multipath, process_queued_ios);
409 struct pgpath *pgpath = NULL;
410 unsigned init_required = 0, must_queue = 1;
413 spin_lock_irqsave(&m->lock, flags);
418 if (!m->current_pgpath)
421 pgpath = m->current_pgpath;
423 if ((pgpath && !m->queue_io) ||
424 (!pgpath && !m->queue_if_no_path))
427 if (m->pg_init_required && !m->pg_init_in_progress) {
429 m->pg_init_required = 0;
430 m->pg_init_in_progress = 1;
435 spin_unlock_irqrestore(&m->lock, flags);
438 queue_work(kmpath_handlerd, &m->activate_path);
441 dispatch_queued_ios(m);
445 * An event is triggered whenever a path is taken out of use.
446 * Includes path failure and PG bypass.
448 static void trigger_event(struct work_struct *work)
450 struct multipath *m =
451 container_of(work, struct multipath, trigger_event);
453 dm_table_event(m->ti->table);
456 /*-----------------------------------------------------------------
457 * Constructor/argument parsing:
458 * <#multipath feature args> [<arg>]*
459 * <#hw_handler args> [hw_handler [<arg>]*]
461 * <initial priority group>
462 * [<selector> <#selector args> [<arg>]*
463 * <#paths> <#per-path selector args>
464 * [<path> [<arg>]* ]+ ]+
465 *---------------------------------------------------------------*/
472 static int read_param(struct param *param, char *str, unsigned *v, char **error)
475 (sscanf(str, "%u", v) != 1) ||
478 *error = param->error;
490 static char *shift(struct arg_set *as)
504 static void consume(struct arg_set *as, unsigned n)
506 BUG_ON (as->argc < n);
511 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
512 struct dm_target *ti)
515 struct path_selector_type *pst;
518 static struct param _params[] = {
519 {0, 1024, "invalid number of path selector args"},
522 pst = dm_get_path_selector(shift(as));
524 ti->error = "unknown path selector type";
528 r = read_param(_params, shift(as), &ps_argc, &ti->error);
532 r = pst->create(&pg->ps, ps_argc, as->argv);
534 dm_put_path_selector(pst);
535 ti->error = "path selector constructor failed";
540 consume(as, ps_argc);
545 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
546 struct dm_target *ti)
551 /* we need at least a path arg */
553 ti->error = "no device given";
561 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
562 dm_table_get_mode(ti->table), &p->path.dev);
564 ti->error = "error getting device";
568 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
570 dm_put_device(ti, p->path.dev);
581 static struct priority_group *parse_priority_group(struct arg_set *as,
584 static struct param _params[] = {
585 {1, 1024, "invalid number of paths"},
586 {0, 1024, "invalid number of selector args"}
590 unsigned i, nr_selector_args, nr_params;
591 struct priority_group *pg;
592 struct dm_target *ti = m->ti;
596 ti->error = "not enough priority group aruments";
600 pg = alloc_priority_group();
602 ti->error = "couldn't allocate priority group";
607 r = parse_path_selector(as, pg, ti);
614 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
618 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
622 nr_params = 1 + nr_selector_args;
623 for (i = 0; i < pg->nr_pgpaths; i++) {
624 struct pgpath *pgpath;
625 struct arg_set path_args;
627 if (as->argc < nr_params)
630 path_args.argc = nr_params;
631 path_args.argv = as->argv;
633 pgpath = parse_path(&path_args, &pg->ps, ti);
638 list_add_tail(&pgpath->list, &pg->pgpaths);
639 consume(as, nr_params);
645 free_priority_group(pg, ti);
649 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
652 struct dm_target *ti = m->ti;
654 static struct param _params[] = {
655 {0, 1024, "invalid number of hardware handler args"},
658 if (read_param(_params, shift(as), &hw_argc, &ti->error))
664 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
665 request_module("scsi_dh_%s", m->hw_handler_name);
666 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
667 ti->error = "unknown hardware handler type";
670 consume(as, hw_argc - 1);
675 static int parse_features(struct arg_set *as, struct multipath *m)
679 struct dm_target *ti = m->ti;
680 const char *param_name;
682 static struct param _params[] = {
683 {0, 3, "invalid number of feature args"},
684 {1, 50, "pg_init_retries must be between 1 and 50"},
687 r = read_param(_params, shift(as), &argc, &ti->error);
695 param_name = shift(as);
698 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
699 r = queue_if_no_path(m, 1, 0);
703 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
705 r = read_param(_params + 1, shift(as),
706 &m->pg_init_retries, &ti->error);
711 ti->error = "Unrecognised multipath feature request";
713 } while (argc && !r);
718 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
721 /* target parameters */
722 static struct param _params[] = {
723 {1, 1024, "invalid number of priority groups"},
724 {1, 1024, "invalid initial priority group number"},
730 unsigned pg_count = 0;
731 unsigned next_pg_num;
736 m = alloc_multipath(ti);
738 ti->error = "can't allocate multipath";
742 r = parse_features(&as, m);
746 r = parse_hw_handler(&as, m);
750 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
754 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
758 /* parse the priority groups */
760 struct priority_group *pg;
762 pg = parse_priority_group(&as, m);
768 m->nr_valid_paths += pg->nr_pgpaths;
769 list_add_tail(&pg->list, &m->priority_groups);
771 pg->pg_num = pg_count;
776 if (pg_count != m->nr_priority_groups) {
777 ti->error = "priority group count mismatch";
789 static void multipath_dtr(struct dm_target *ti)
791 struct multipath *m = (struct multipath *) ti->private;
793 flush_workqueue(kmpath_handlerd);
794 flush_workqueue(kmultipathd);
799 * Map bios, recording original fields for later in case we have to resubmit
801 static int multipath_map(struct dm_target *ti, struct bio *bio,
802 union map_info *map_context)
805 struct dm_mpath_io *mpio;
806 struct multipath *m = (struct multipath *) ti->private;
808 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
809 dm_bio_record(&mpio->details, bio);
811 map_context->ptr = mpio;
812 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
813 r = map_io(m, bio, mpio, 0);
814 if (r < 0 || r == DM_MAPIO_REQUEUE)
815 mempool_free(mpio, m->mpio_pool);
821 * Take a path out of use.
823 static int fail_path(struct pgpath *pgpath)
826 struct multipath *m = pgpath->pg->m;
828 spin_lock_irqsave(&m->lock, flags);
830 if (!pgpath->path.is_active)
833 DMWARN("Failing path %s.", pgpath->path.dev->name);
835 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
836 pgpath->path.is_active = 0;
837 pgpath->fail_count++;
841 if (pgpath == m->current_pgpath)
842 m->current_pgpath = NULL;
844 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
845 pgpath->path.dev->name, m->nr_valid_paths);
847 queue_work(kmultipathd, &m->trigger_event);
850 spin_unlock_irqrestore(&m->lock, flags);
856 * Reinstate a previously-failed path
858 static int reinstate_path(struct pgpath *pgpath)
862 struct multipath *m = pgpath->pg->m;
864 spin_lock_irqsave(&m->lock, flags);
866 if (pgpath->path.is_active)
869 if (!pgpath->pg->ps.type) {
870 DMWARN("Reinstate path not supported by path selector %s",
871 pgpath->pg->ps.type->name);
876 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
880 pgpath->path.is_active = 1;
882 m->current_pgpath = NULL;
883 if (!m->nr_valid_paths++ && m->queue_size)
884 queue_work(kmultipathd, &m->process_queued_ios);
886 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
887 pgpath->path.dev->name, m->nr_valid_paths);
889 queue_work(kmultipathd, &m->trigger_event);
892 spin_unlock_irqrestore(&m->lock, flags);
898 * Fail or reinstate all paths that match the provided struct dm_dev.
900 static int action_dev(struct multipath *m, struct dm_dev *dev,
904 struct pgpath *pgpath;
905 struct priority_group *pg;
907 list_for_each_entry(pg, &m->priority_groups, list) {
908 list_for_each_entry(pgpath, &pg->pgpaths, list) {
909 if (pgpath->path.dev == dev)
918 * Temporarily try to avoid having to use the specified PG
920 static void bypass_pg(struct multipath *m, struct priority_group *pg,
925 spin_lock_irqsave(&m->lock, flags);
927 pg->bypassed = bypassed;
928 m->current_pgpath = NULL;
929 m->current_pg = NULL;
931 spin_unlock_irqrestore(&m->lock, flags);
933 queue_work(kmultipathd, &m->trigger_event);
937 * Switch to using the specified PG from the next I/O that gets mapped
939 static int switch_pg_num(struct multipath *m, const char *pgstr)
941 struct priority_group *pg;
945 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
946 (pgnum > m->nr_priority_groups)) {
947 DMWARN("invalid PG number supplied to switch_pg_num");
951 spin_lock_irqsave(&m->lock, flags);
952 list_for_each_entry(pg, &m->priority_groups, list) {
957 m->current_pgpath = NULL;
958 m->current_pg = NULL;
961 spin_unlock_irqrestore(&m->lock, flags);
963 queue_work(kmultipathd, &m->trigger_event);
968 * Set/clear bypassed status of a PG.
969 * PGs are numbered upwards from 1 in the order they were declared.
971 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
973 struct priority_group *pg;
976 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
977 (pgnum > m->nr_priority_groups)) {
978 DMWARN("invalid PG number supplied to bypass_pg");
982 list_for_each_entry(pg, &m->priority_groups, list) {
987 bypass_pg(m, pg, bypassed);
992 * Should we retry pg_init immediately?
994 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
997 int limit_reached = 0;
999 spin_lock_irqsave(&m->lock, flags);
1001 if (m->pg_init_count <= m->pg_init_retries)
1002 m->pg_init_required = 1;
1006 spin_unlock_irqrestore(&m->lock, flags);
1008 return limit_reached;
1012 * pg_init must call this when it has completed its initialisation
1014 void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
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;
1022 * If requested, retry pg_init until maximum number of retries exceeded.
1023 * If retry not requested and PG already bypassed, always fail the path.
1025 if (err_flags & MP_RETRY) {
1026 if (pg_init_limit_reached(m, pgpath))
1027 err_flags |= MP_FAIL_PATH;
1028 } else if (err_flags && pg->bypassed)
1029 err_flags |= MP_FAIL_PATH;
1031 if (err_flags & MP_FAIL_PATH)
1034 if (err_flags & MP_BYPASS_PG)
1035 bypass_pg(m, pg, 1);
1037 spin_lock_irqsave(&m->lock, flags);
1038 if (err_flags & ~MP_RETRY) {
1039 m->current_pgpath = NULL;
1040 m->current_pg = NULL;
1041 } else if (!m->pg_init_required)
1044 m->pg_init_in_progress = 0;
1045 queue_work(kmultipathd, &m->process_queued_ios);
1046 spin_unlock_irqrestore(&m->lock, flags);
1049 static void pg_init_done(struct dm_path *path, int errors)
1051 struct pgpath *pgpath = path_to_pgpath(path);
1052 struct priority_group *pg = pgpath->pg;
1053 struct multipath *m = pg->m;
1054 unsigned long flags;
1056 /* device or driver problems */
1061 if (!m->hw_handler_name) {
1065 DMERR("Cannot failover device because scsi_dh_%s was not "
1066 "loaded.", m->hw_handler_name);
1068 * Fail path for now, so we do not ping pong
1072 case SCSI_DH_DEV_TEMP_BUSY:
1074 * Probably doing something like FW upgrade on the
1075 * controller so try the other pg.
1077 bypass_pg(m, pg, 1);
1079 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1081 case SCSI_DH_IMM_RETRY:
1082 case SCSI_DH_RES_TEMP_UNAVAIL:
1083 if (pg_init_limit_reached(m, pgpath))
1089 * We probably do not want to fail the path for a device
1090 * error, but this is what the old dm did. In future
1091 * patches we can do more advanced handling.
1096 spin_lock_irqsave(&m->lock, flags);
1098 DMERR("Could not failover device. Error %d.", errors);
1099 m->current_pgpath = NULL;
1100 m->current_pg = NULL;
1101 } else if (!m->pg_init_required) {
1106 m->pg_init_in_progress = 0;
1107 queue_work(kmultipathd, &m->process_queued_ios);
1108 spin_unlock_irqrestore(&m->lock, flags);
1111 static void activate_path(struct work_struct *work)
1114 struct multipath *m =
1115 container_of(work, struct multipath, activate_path);
1116 struct dm_path *path = &m->current_pgpath->path;
1118 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1119 pg_init_done(path, ret);
1125 static int do_end_io(struct multipath *m, struct bio *bio,
1126 int error, struct dm_mpath_io *mpio)
1128 unsigned long flags;
1131 return 0; /* I/O complete */
1133 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1136 if (error == -EOPNOTSUPP)
1139 spin_lock_irqsave(&m->lock, flags);
1140 if (!m->nr_valid_paths) {
1141 if (__must_push_back(m)) {
1142 spin_unlock_irqrestore(&m->lock, flags);
1143 return DM_ENDIO_REQUEUE;
1144 } else if (!m->queue_if_no_path) {
1145 spin_unlock_irqrestore(&m->lock, flags);
1148 spin_unlock_irqrestore(&m->lock, flags);
1152 spin_unlock_irqrestore(&m->lock, flags);
1155 fail_path(mpio->pgpath);
1158 dm_bio_restore(&mpio->details, bio);
1160 /* queue for the daemon to resubmit or fail */
1161 spin_lock_irqsave(&m->lock, flags);
1162 bio_list_add(&m->queued_ios, bio);
1165 queue_work(kmultipathd, &m->process_queued_ios);
1166 spin_unlock_irqrestore(&m->lock, flags);
1168 return DM_ENDIO_INCOMPLETE; /* io not complete */
1171 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1172 int error, union map_info *map_context)
1174 struct multipath *m = ti->private;
1175 struct dm_mpath_io *mpio = map_context->ptr;
1176 struct pgpath *pgpath = mpio->pgpath;
1177 struct path_selector *ps;
1180 r = do_end_io(m, bio, error, mpio);
1182 ps = &pgpath->pg->ps;
1183 if (ps->type->end_io)
1184 ps->type->end_io(ps, &pgpath->path);
1186 if (r != DM_ENDIO_INCOMPLETE)
1187 mempool_free(mpio, m->mpio_pool);
1193 * Suspend can't complete until all the I/O is processed so if
1194 * the last path fails we must error any remaining I/O.
1195 * Note that if the freeze_bdev fails while suspending, the
1196 * queue_if_no_path state is lost - userspace should reset it.
1198 static void multipath_presuspend(struct dm_target *ti)
1200 struct multipath *m = (struct multipath *) ti->private;
1202 queue_if_no_path(m, 0, 1);
1206 * Restore the queue_if_no_path setting.
1208 static void multipath_resume(struct dm_target *ti)
1210 struct multipath *m = (struct multipath *) ti->private;
1211 unsigned long flags;
1213 spin_lock_irqsave(&m->lock, flags);
1214 m->queue_if_no_path = m->saved_queue_if_no_path;
1215 spin_unlock_irqrestore(&m->lock, flags);
1219 * Info output has the following format:
1220 * num_multipath_feature_args [multipath_feature_args]*
1221 * num_handler_status_args [handler_status_args]*
1222 * num_groups init_group_number
1223 * [A|D|E num_ps_status_args [ps_status_args]*
1224 * num_paths num_selector_args
1225 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1227 * Table output has the following format (identical to the constructor string):
1228 * num_feature_args [features_args]*
1229 * num_handler_args hw_handler [hw_handler_args]*
1230 * num_groups init_group_number
1231 * [priority selector-name num_ps_args [ps_args]*
1232 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1234 static int multipath_status(struct dm_target *ti, status_type_t type,
1235 char *result, unsigned int maxlen)
1238 unsigned long flags;
1239 struct multipath *m = (struct multipath *) ti->private;
1240 struct priority_group *pg;
1245 spin_lock_irqsave(&m->lock, flags);
1248 if (type == STATUSTYPE_INFO)
1249 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1251 DMEMIT("%u ", m->queue_if_no_path +
1252 (m->pg_init_retries > 0) * 2);
1253 if (m->queue_if_no_path)
1254 DMEMIT("queue_if_no_path ");
1255 if (m->pg_init_retries)
1256 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1259 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1262 DMEMIT("1 %s ", m->hw_handler_name);
1264 DMEMIT("%u ", m->nr_priority_groups);
1267 pg_num = m->next_pg->pg_num;
1268 else if (m->current_pg)
1269 pg_num = m->current_pg->pg_num;
1273 DMEMIT("%u ", pg_num);
1276 case STATUSTYPE_INFO:
1277 list_for_each_entry(pg, &m->priority_groups, list) {
1279 state = 'D'; /* Disabled */
1280 else if (pg == m->current_pg)
1281 state = 'A'; /* Currently Active */
1283 state = 'E'; /* Enabled */
1285 DMEMIT("%c ", state);
1287 if (pg->ps.type->status)
1288 sz += pg->ps.type->status(&pg->ps, NULL, type,
1294 DMEMIT("%u %u ", pg->nr_pgpaths,
1295 pg->ps.type->info_args);
1297 list_for_each_entry(p, &pg->pgpaths, list) {
1298 DMEMIT("%s %s %u ", p->path.dev->name,
1299 p->path.is_active ? "A" : "F",
1301 if (pg->ps.type->status)
1302 sz += pg->ps.type->status(&pg->ps,
1303 &p->path, type, result + sz,
1309 case STATUSTYPE_TABLE:
1310 list_for_each_entry(pg, &m->priority_groups, list) {
1311 DMEMIT("%s ", pg->ps.type->name);
1313 if (pg->ps.type->status)
1314 sz += pg->ps.type->status(&pg->ps, NULL, type,
1320 DMEMIT("%u %u ", pg->nr_pgpaths,
1321 pg->ps.type->table_args);
1323 list_for_each_entry(p, &pg->pgpaths, list) {
1324 DMEMIT("%s ", p->path.dev->name);
1325 if (pg->ps.type->status)
1326 sz += pg->ps.type->status(&pg->ps,
1327 &p->path, type, result + sz,
1334 spin_unlock_irqrestore(&m->lock, flags);
1339 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1343 struct multipath *m = (struct multipath *) ti->private;
1347 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1348 return queue_if_no_path(m, 1, 0);
1349 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1350 return queue_if_no_path(m, 0, 0);
1356 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1357 return bypass_pg_num(m, argv[1], 1);
1358 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1359 return bypass_pg_num(m, argv[1], 0);
1360 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1361 return switch_pg_num(m, argv[1]);
1362 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1363 action = reinstate_path;
1364 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1369 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1370 dm_table_get_mode(ti->table), &dev);
1372 DMWARN("message: error getting device %s",
1377 r = action_dev(m, dev, action);
1379 dm_put_device(ti, dev);
1384 DMWARN("Unrecognised multipath message received.");
1388 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1389 struct file *filp, unsigned int cmd,
1392 struct multipath *m = (struct multipath *) ti->private;
1393 struct block_device *bdev = NULL;
1394 unsigned long flags;
1395 struct file fake_file = {};
1396 struct dentry fake_dentry = {};
1399 fake_file.f_path.dentry = &fake_dentry;
1401 spin_lock_irqsave(&m->lock, flags);
1403 if (!m->current_pgpath)
1406 if (m->current_pgpath) {
1407 bdev = m->current_pgpath->path.dev->bdev;
1408 fake_dentry.d_inode = bdev->bd_inode;
1409 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1417 spin_unlock_irqrestore(&m->lock, flags);
1419 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1420 bdev->bd_disk, cmd, arg);
1423 /*-----------------------------------------------------------------
1425 *---------------------------------------------------------------*/
1426 static struct target_type multipath_target = {
1427 .name = "multipath",
1428 .version = {1, 0, 5},
1429 .module = THIS_MODULE,
1430 .ctr = multipath_ctr,
1431 .dtr = multipath_dtr,
1432 .map = multipath_map,
1433 .end_io = multipath_end_io,
1434 .presuspend = multipath_presuspend,
1435 .resume = multipath_resume,
1436 .status = multipath_status,
1437 .message = multipath_message,
1438 .ioctl = multipath_ioctl,
1441 static int __init dm_multipath_init(void)
1445 /* allocate a slab for the dm_ios */
1446 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1450 r = dm_register_target(&multipath_target);
1452 DMERR("register failed %d", r);
1453 kmem_cache_destroy(_mpio_cache);
1457 kmultipathd = create_workqueue("kmpathd");
1459 DMERR("failed to create workqueue kmpathd");
1460 dm_unregister_target(&multipath_target);
1461 kmem_cache_destroy(_mpio_cache);
1466 * A separate workqueue is used to handle the device handlers
1467 * to avoid overloading existing workqueue. Overloading the
1468 * old workqueue would also create a bottleneck in the
1469 * path of the storage hardware device activation.
1471 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1472 if (!kmpath_handlerd) {
1473 DMERR("failed to create workqueue kmpath_handlerd");
1474 destroy_workqueue(kmultipathd);
1475 dm_unregister_target(&multipath_target);
1476 kmem_cache_destroy(_mpio_cache);
1480 DMINFO("version %u.%u.%u loaded",
1481 multipath_target.version[0], multipath_target.version[1],
1482 multipath_target.version[2]);
1487 static void __exit dm_multipath_exit(void)
1491 destroy_workqueue(kmpath_handlerd);
1492 destroy_workqueue(kmultipathd);
1494 r = dm_unregister_target(&multipath_target);
1496 DMERR("target unregister failed %d", r);
1497 kmem_cache_destroy(_mpio_cache);
1500 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1502 module_init(dm_multipath_init);
1503 module_exit(dm_multipath_exit);
1505 MODULE_DESCRIPTION(DM_NAME " multipath target");
1506 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1507 MODULE_LICENSE("GPL");