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 is_active; /* Path status */
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 struct pgpath *pgpath_to_activate;
68 unsigned nr_priority_groups;
69 struct list_head priority_groups;
70 unsigned pg_init_required; /* pg_init needs calling? */
71 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
81 unsigned saved_queue_if_no_path;/* Saved state during suspension */
82 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
85 struct work_struct process_queued_ios;
86 struct bio_list queued_ios;
89 struct work_struct trigger_event;
92 * We must use a mempool of dm_mpath_io structs so that we
93 * can resubmit bios on error.
99 * Context information attached to each bio we process.
102 struct pgpath *pgpath;
103 struct dm_bio_details details;
106 typedef int (*action_fn) (struct pgpath *pgpath);
108 #define MIN_IOS 256 /* Mempool size */
110 static struct kmem_cache *_mpio_cache;
112 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113 static void process_queued_ios(struct work_struct *work);
114 static void trigger_event(struct work_struct *work);
115 static void activate_path(struct work_struct *work);
118 /*-----------------------------------------------
119 * Allocation routines
120 *-----------------------------------------------*/
122 static struct pgpath *alloc_pgpath(void)
124 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
127 pgpath->is_active = 1;
132 static void free_pgpath(struct pgpath *pgpath)
137 static struct priority_group *alloc_priority_group(void)
139 struct priority_group *pg;
141 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
144 INIT_LIST_HEAD(&pg->pgpaths);
149 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
152 struct pgpath *pgpath, *tmp;
153 struct multipath *m = ti->private;
155 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
156 list_del(&pgpath->list);
157 if (m->hw_handler_name)
158 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
159 dm_put_device(ti, pgpath->path.dev);
160 spin_lock_irqsave(&m->lock, flags);
161 if (m->pgpath_to_activate == pgpath)
162 m->pgpath_to_activate = NULL;
163 spin_unlock_irqrestore(&m->lock, flags);
168 static void free_priority_group(struct priority_group *pg,
169 struct dm_target *ti)
171 struct path_selector *ps = &pg->ps;
174 ps->type->destroy(ps);
175 dm_put_path_selector(ps->type);
178 free_pgpaths(&pg->pgpaths, ti);
182 static struct multipath *alloc_multipath(struct dm_target *ti)
186 m = kzalloc(sizeof(*m), GFP_KERNEL);
188 INIT_LIST_HEAD(&m->priority_groups);
189 spin_lock_init(&m->lock);
191 INIT_WORK(&m->process_queued_ios, process_queued_ios);
192 INIT_WORK(&m->trigger_event, trigger_event);
193 INIT_WORK(&m->activate_path, activate_path);
194 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
206 static void free_multipath(struct multipath *m)
208 struct priority_group *pg, *tmp;
210 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
212 free_priority_group(pg, m->ti);
215 kfree(m->hw_handler_name);
216 mempool_destroy(m->mpio_pool);
221 /*-----------------------------------------------
223 *-----------------------------------------------*/
225 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
227 m->current_pg = pgpath->pg;
229 /* Must we initialise the PG first, and queue I/O till it's ready? */
230 if (m->hw_handler_name) {
231 m->pg_init_required = 1;
234 m->pg_init_required = 0;
238 m->pg_init_count = 0;
241 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
243 struct dm_path *path;
245 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
249 m->current_pgpath = path_to_pgpath(path);
251 if (m->current_pg != pg)
252 __switch_pg(m, m->current_pgpath);
257 static void __choose_pgpath(struct multipath *m)
259 struct priority_group *pg;
260 unsigned bypassed = 1;
262 if (!m->nr_valid_paths)
265 /* Were we instructed to switch PG? */
269 if (!__choose_path_in_pg(m, pg))
273 /* Don't change PG until it has no remaining paths */
274 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
278 * Loop through priority groups until we find a valid path.
279 * First time we skip PGs marked 'bypassed'.
280 * Second time we only try the ones we skipped.
283 list_for_each_entry(pg, &m->priority_groups, list) {
284 if (pg->bypassed == bypassed)
286 if (!__choose_path_in_pg(m, pg))
289 } while (bypassed--);
292 m->current_pgpath = NULL;
293 m->current_pg = NULL;
297 * Check whether bios must be queued in the device-mapper core rather
298 * than here in the target.
300 * m->lock must be held on entry.
302 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
303 * same value then we are not between multipath_presuspend()
304 * and multipath_resume() calls and we have no need to check
305 * for the DMF_NOFLUSH_SUSPENDING flag.
307 static int __must_push_back(struct multipath *m)
309 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
310 dm_noflush_suspending(m->ti));
313 static int map_io(struct multipath *m, struct bio *bio,
314 struct dm_mpath_io *mpio, unsigned was_queued)
316 int r = DM_MAPIO_REMAPPED;
318 struct pgpath *pgpath;
320 spin_lock_irqsave(&m->lock, flags);
322 /* Do we need to select a new pgpath? */
323 if (!m->current_pgpath ||
324 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
327 pgpath = m->current_pgpath;
332 if ((pgpath && m->queue_io) ||
333 (!pgpath && m->queue_if_no_path)) {
334 /* Queue for the daemon to resubmit */
335 bio_list_add(&m->queued_ios, bio);
337 if ((m->pg_init_required && !m->pg_init_in_progress) ||
339 queue_work(kmultipathd, &m->process_queued_ios);
341 r = DM_MAPIO_SUBMITTED;
343 bio->bi_bdev = pgpath->path.dev->bdev;
344 else if (__must_push_back(m))
345 r = DM_MAPIO_REQUEUE;
347 r = -EIO; /* Failed */
349 mpio->pgpath = pgpath;
351 spin_unlock_irqrestore(&m->lock, flags);
357 * If we run out of usable paths, should we queue I/O or error it?
359 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
360 unsigned save_old_value)
364 spin_lock_irqsave(&m->lock, flags);
367 m->saved_queue_if_no_path = m->queue_if_no_path;
369 m->saved_queue_if_no_path = queue_if_no_path;
370 m->queue_if_no_path = queue_if_no_path;
371 if (!m->queue_if_no_path && m->queue_size)
372 queue_work(kmultipathd, &m->process_queued_ios);
374 spin_unlock_irqrestore(&m->lock, flags);
379 /*-----------------------------------------------------------------
380 * The multipath daemon is responsible for resubmitting queued ios.
381 *---------------------------------------------------------------*/
383 static void dispatch_queued_ios(struct multipath *m)
387 struct bio *bio = NULL, *next;
388 struct dm_mpath_io *mpio;
389 union map_info *info;
391 spin_lock_irqsave(&m->lock, flags);
392 bio = bio_list_get(&m->queued_ios);
393 spin_unlock_irqrestore(&m->lock, flags);
399 info = dm_get_mapinfo(bio);
402 r = map_io(m, bio, mpio, 1);
405 else if (r == DM_MAPIO_REMAPPED)
406 generic_make_request(bio);
407 else if (r == DM_MAPIO_REQUEUE)
408 bio_endio(bio, -EIO);
414 static void process_queued_ios(struct work_struct *work)
416 struct multipath *m =
417 container_of(work, struct multipath, process_queued_ios);
418 struct pgpath *pgpath = NULL;
419 unsigned init_required = 0, must_queue = 1;
422 spin_lock_irqsave(&m->lock, flags);
427 if (!m->current_pgpath)
430 pgpath = m->current_pgpath;
431 m->pgpath_to_activate = m->current_pgpath;
433 if ((pgpath && !m->queue_io) ||
434 (!pgpath && !m->queue_if_no_path))
437 if (m->pg_init_required && !m->pg_init_in_progress) {
439 m->pg_init_required = 0;
440 m->pg_init_in_progress = 1;
445 spin_unlock_irqrestore(&m->lock, flags);
448 queue_work(kmpath_handlerd, &m->activate_path);
451 dispatch_queued_ios(m);
455 * An event is triggered whenever a path is taken out of use.
456 * Includes path failure and PG bypass.
458 static void trigger_event(struct work_struct *work)
460 struct multipath *m =
461 container_of(work, struct multipath, trigger_event);
463 dm_table_event(m->ti->table);
466 /*-----------------------------------------------------------------
467 * Constructor/argument parsing:
468 * <#multipath feature args> [<arg>]*
469 * <#hw_handler args> [hw_handler [<arg>]*]
471 * <initial priority group>
472 * [<selector> <#selector args> [<arg>]*
473 * <#paths> <#per-path selector args>
474 * [<path> [<arg>]* ]+ ]+
475 *---------------------------------------------------------------*/
482 static int read_param(struct param *param, char *str, unsigned *v, char **error)
485 (sscanf(str, "%u", v) != 1) ||
488 *error = param->error;
500 static char *shift(struct arg_set *as)
514 static void consume(struct arg_set *as, unsigned n)
516 BUG_ON (as->argc < n);
521 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
522 struct dm_target *ti)
525 struct path_selector_type *pst;
528 static struct param _params[] = {
529 {0, 1024, "invalid number of path selector args"},
532 pst = dm_get_path_selector(shift(as));
534 ti->error = "unknown path selector type";
538 r = read_param(_params, shift(as), &ps_argc, &ti->error);
540 dm_put_path_selector(pst);
544 r = pst->create(&pg->ps, ps_argc, as->argv);
546 dm_put_path_selector(pst);
547 ti->error = "path selector constructor failed";
552 consume(as, ps_argc);
557 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
558 struct dm_target *ti)
562 struct multipath *m = ti->private;
564 /* we need at least a path arg */
566 ti->error = "no device given";
567 return ERR_PTR(-EINVAL);
572 return ERR_PTR(-ENOMEM);
574 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
575 dm_table_get_mode(ti->table), &p->path.dev);
577 ti->error = "error getting device";
581 if (m->hw_handler_name) {
582 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
585 dm_put_device(ti, p->path.dev);
590 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
592 dm_put_device(ti, p->path.dev);
603 static struct priority_group *parse_priority_group(struct arg_set *as,
606 static struct param _params[] = {
607 {1, 1024, "invalid number of paths"},
608 {0, 1024, "invalid number of selector args"}
612 unsigned i, nr_selector_args, nr_params;
613 struct priority_group *pg;
614 struct dm_target *ti = m->ti;
618 ti->error = "not enough priority group arguments";
619 return ERR_PTR(-EINVAL);
622 pg = alloc_priority_group();
624 ti->error = "couldn't allocate priority group";
625 return ERR_PTR(-ENOMEM);
629 r = parse_path_selector(as, pg, ti);
636 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
640 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
644 nr_params = 1 + nr_selector_args;
645 for (i = 0; i < pg->nr_pgpaths; i++) {
646 struct pgpath *pgpath;
647 struct arg_set path_args;
649 if (as->argc < nr_params) {
650 ti->error = "not enough path parameters";
654 path_args.argc = nr_params;
655 path_args.argv = as->argv;
657 pgpath = parse_path(&path_args, &pg->ps, ti);
658 if (IS_ERR(pgpath)) {
664 list_add_tail(&pgpath->list, &pg->pgpaths);
665 consume(as, nr_params);
671 free_priority_group(pg, ti);
675 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
678 struct dm_target *ti = m->ti;
680 static struct param _params[] = {
681 {0, 1024, "invalid number of hardware handler args"},
684 if (read_param(_params, shift(as), &hw_argc, &ti->error))
690 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
691 request_module("scsi_dh_%s", m->hw_handler_name);
692 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
693 ti->error = "unknown hardware handler type";
694 kfree(m->hw_handler_name);
695 m->hw_handler_name = NULL;
698 consume(as, hw_argc - 1);
703 static int parse_features(struct arg_set *as, struct multipath *m)
707 struct dm_target *ti = m->ti;
708 const char *param_name;
710 static struct param _params[] = {
711 {0, 3, "invalid number of feature args"},
712 {1, 50, "pg_init_retries must be between 1 and 50"},
715 r = read_param(_params, shift(as), &argc, &ti->error);
723 param_name = shift(as);
726 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
727 r = queue_if_no_path(m, 1, 0);
731 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
733 r = read_param(_params + 1, shift(as),
734 &m->pg_init_retries, &ti->error);
739 ti->error = "Unrecognised multipath feature request";
741 } while (argc && !r);
746 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
749 /* target parameters */
750 static struct param _params[] = {
751 {1, 1024, "invalid number of priority groups"},
752 {1, 1024, "invalid initial priority group number"},
758 unsigned pg_count = 0;
759 unsigned next_pg_num;
764 m = alloc_multipath(ti);
766 ti->error = "can't allocate multipath";
770 r = parse_features(&as, m);
774 r = parse_hw_handler(&as, m);
778 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
782 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
786 /* parse the priority groups */
788 struct priority_group *pg;
790 pg = parse_priority_group(&as, m);
796 m->nr_valid_paths += pg->nr_pgpaths;
797 list_add_tail(&pg->list, &m->priority_groups);
799 pg->pg_num = pg_count;
804 if (pg_count != m->nr_priority_groups) {
805 ti->error = "priority group count mismatch";
817 static void multipath_dtr(struct dm_target *ti)
819 struct multipath *m = (struct multipath *) ti->private;
821 flush_workqueue(kmpath_handlerd);
822 flush_workqueue(kmultipathd);
827 * Map bios, recording original fields for later in case we have to resubmit
829 static int multipath_map(struct dm_target *ti, struct bio *bio,
830 union map_info *map_context)
833 struct dm_mpath_io *mpio;
834 struct multipath *m = (struct multipath *) ti->private;
836 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
837 dm_bio_record(&mpio->details, bio);
839 map_context->ptr = mpio;
840 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
841 r = map_io(m, bio, mpio, 0);
842 if (r < 0 || r == DM_MAPIO_REQUEUE)
843 mempool_free(mpio, m->mpio_pool);
849 * Take a path out of use.
851 static int fail_path(struct pgpath *pgpath)
854 struct multipath *m = pgpath->pg->m;
856 spin_lock_irqsave(&m->lock, flags);
858 if (!pgpath->is_active)
861 DMWARN("Failing path %s.", pgpath->path.dev->name);
863 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
864 pgpath->is_active = 0;
865 pgpath->fail_count++;
869 if (pgpath == m->current_pgpath)
870 m->current_pgpath = NULL;
872 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
873 pgpath->path.dev->name, m->nr_valid_paths);
875 queue_work(kmultipathd, &m->trigger_event);
878 spin_unlock_irqrestore(&m->lock, flags);
884 * Reinstate a previously-failed path
886 static int reinstate_path(struct pgpath *pgpath)
890 struct multipath *m = pgpath->pg->m;
892 spin_lock_irqsave(&m->lock, flags);
894 if (pgpath->is_active)
897 if (!pgpath->pg->ps.type->reinstate_path) {
898 DMWARN("Reinstate path not supported by path selector %s",
899 pgpath->pg->ps.type->name);
904 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
908 pgpath->is_active = 1;
910 m->current_pgpath = NULL;
911 if (!m->nr_valid_paths++ && m->queue_size)
912 queue_work(kmultipathd, &m->process_queued_ios);
914 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
915 pgpath->path.dev->name, m->nr_valid_paths);
917 queue_work(kmultipathd, &m->trigger_event);
920 spin_unlock_irqrestore(&m->lock, flags);
926 * Fail or reinstate all paths that match the provided struct dm_dev.
928 static int action_dev(struct multipath *m, struct dm_dev *dev,
932 struct pgpath *pgpath;
933 struct priority_group *pg;
935 list_for_each_entry(pg, &m->priority_groups, list) {
936 list_for_each_entry(pgpath, &pg->pgpaths, list) {
937 if (pgpath->path.dev == dev)
946 * Temporarily try to avoid having to use the specified PG
948 static void bypass_pg(struct multipath *m, struct priority_group *pg,
953 spin_lock_irqsave(&m->lock, flags);
955 pg->bypassed = bypassed;
956 m->current_pgpath = NULL;
957 m->current_pg = NULL;
959 spin_unlock_irqrestore(&m->lock, flags);
961 queue_work(kmultipathd, &m->trigger_event);
965 * Switch to using the specified PG from the next I/O that gets mapped
967 static int switch_pg_num(struct multipath *m, const char *pgstr)
969 struct priority_group *pg;
973 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
974 (pgnum > m->nr_priority_groups)) {
975 DMWARN("invalid PG number supplied to switch_pg_num");
979 spin_lock_irqsave(&m->lock, flags);
980 list_for_each_entry(pg, &m->priority_groups, list) {
985 m->current_pgpath = NULL;
986 m->current_pg = NULL;
989 spin_unlock_irqrestore(&m->lock, flags);
991 queue_work(kmultipathd, &m->trigger_event);
996 * Set/clear bypassed status of a PG.
997 * PGs are numbered upwards from 1 in the order they were declared.
999 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1001 struct priority_group *pg;
1004 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1005 (pgnum > m->nr_priority_groups)) {
1006 DMWARN("invalid PG number supplied to bypass_pg");
1010 list_for_each_entry(pg, &m->priority_groups, list) {
1015 bypass_pg(m, pg, bypassed);
1020 * Should we retry pg_init immediately?
1022 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1024 unsigned long flags;
1025 int limit_reached = 0;
1027 spin_lock_irqsave(&m->lock, flags);
1029 if (m->pg_init_count <= m->pg_init_retries)
1030 m->pg_init_required = 1;
1034 spin_unlock_irqrestore(&m->lock, flags);
1036 return limit_reached;
1039 static void pg_init_done(struct dm_path *path, int errors)
1041 struct pgpath *pgpath = path_to_pgpath(path);
1042 struct priority_group *pg = pgpath->pg;
1043 struct multipath *m = pg->m;
1044 unsigned long flags;
1046 /* device or driver problems */
1051 if (!m->hw_handler_name) {
1055 DMERR("Cannot failover device because scsi_dh_%s was not "
1056 "loaded.", m->hw_handler_name);
1058 * Fail path for now, so we do not ping pong
1062 case SCSI_DH_DEV_TEMP_BUSY:
1064 * Probably doing something like FW upgrade on the
1065 * controller so try the other pg.
1067 bypass_pg(m, pg, 1);
1069 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1071 case SCSI_DH_IMM_RETRY:
1072 case SCSI_DH_RES_TEMP_UNAVAIL:
1073 if (pg_init_limit_reached(m, pgpath))
1079 * We probably do not want to fail the path for a device
1080 * error, but this is what the old dm did. In future
1081 * patches we can do more advanced handling.
1086 spin_lock_irqsave(&m->lock, flags);
1088 DMERR("Could not failover device. Error %d.", errors);
1089 m->current_pgpath = NULL;
1090 m->current_pg = NULL;
1091 } else if (!m->pg_init_required) {
1096 m->pg_init_in_progress = 0;
1097 queue_work(kmultipathd, &m->process_queued_ios);
1098 spin_unlock_irqrestore(&m->lock, flags);
1101 static void activate_path(struct work_struct *work)
1104 struct multipath *m =
1105 container_of(work, struct multipath, activate_path);
1106 struct dm_path *path;
1107 unsigned long flags;
1109 spin_lock_irqsave(&m->lock, flags);
1110 path = &m->pgpath_to_activate->path;
1111 m->pgpath_to_activate = NULL;
1112 spin_unlock_irqrestore(&m->lock, flags);
1115 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1116 pg_init_done(path, ret);
1122 static int do_end_io(struct multipath *m, struct bio *bio,
1123 int error, struct dm_mpath_io *mpio)
1125 unsigned long flags;
1128 return 0; /* I/O complete */
1130 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1133 if (error == -EOPNOTSUPP)
1136 spin_lock_irqsave(&m->lock, flags);
1137 if (!m->nr_valid_paths) {
1138 if (__must_push_back(m)) {
1139 spin_unlock_irqrestore(&m->lock, flags);
1140 return DM_ENDIO_REQUEUE;
1141 } else if (!m->queue_if_no_path) {
1142 spin_unlock_irqrestore(&m->lock, flags);
1145 spin_unlock_irqrestore(&m->lock, flags);
1149 spin_unlock_irqrestore(&m->lock, flags);
1152 fail_path(mpio->pgpath);
1155 dm_bio_restore(&mpio->details, bio);
1157 /* queue for the daemon to resubmit or fail */
1158 spin_lock_irqsave(&m->lock, flags);
1159 bio_list_add(&m->queued_ios, bio);
1162 queue_work(kmultipathd, &m->process_queued_ios);
1163 spin_unlock_irqrestore(&m->lock, flags);
1165 return DM_ENDIO_INCOMPLETE; /* io not complete */
1168 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1169 int error, union map_info *map_context)
1171 struct multipath *m = ti->private;
1172 struct dm_mpath_io *mpio = map_context->ptr;
1173 struct pgpath *pgpath = mpio->pgpath;
1174 struct path_selector *ps;
1177 r = do_end_io(m, bio, error, mpio);
1179 ps = &pgpath->pg->ps;
1180 if (ps->type->end_io)
1181 ps->type->end_io(ps, &pgpath->path);
1183 if (r != DM_ENDIO_INCOMPLETE)
1184 mempool_free(mpio, m->mpio_pool);
1190 * Suspend can't complete until all the I/O is processed so if
1191 * the last path fails we must error any remaining I/O.
1192 * Note that if the freeze_bdev fails while suspending, the
1193 * queue_if_no_path state is lost - userspace should reset it.
1195 static void multipath_presuspend(struct dm_target *ti)
1197 struct multipath *m = (struct multipath *) ti->private;
1199 queue_if_no_path(m, 0, 1);
1203 * Restore the queue_if_no_path setting.
1205 static void multipath_resume(struct dm_target *ti)
1207 struct multipath *m = (struct multipath *) ti->private;
1208 unsigned long flags;
1210 spin_lock_irqsave(&m->lock, flags);
1211 m->queue_if_no_path = m->saved_queue_if_no_path;
1212 spin_unlock_irqrestore(&m->lock, flags);
1216 * Info output has the following format:
1217 * num_multipath_feature_args [multipath_feature_args]*
1218 * num_handler_status_args [handler_status_args]*
1219 * num_groups init_group_number
1220 * [A|D|E num_ps_status_args [ps_status_args]*
1221 * num_paths num_selector_args
1222 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1224 * Table output has the following format (identical to the constructor string):
1225 * num_feature_args [features_args]*
1226 * num_handler_args hw_handler [hw_handler_args]*
1227 * num_groups init_group_number
1228 * [priority selector-name num_ps_args [ps_args]*
1229 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1231 static int multipath_status(struct dm_target *ti, status_type_t type,
1232 char *result, unsigned int maxlen)
1235 unsigned long flags;
1236 struct multipath *m = (struct multipath *) ti->private;
1237 struct priority_group *pg;
1242 spin_lock_irqsave(&m->lock, flags);
1245 if (type == STATUSTYPE_INFO)
1246 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1248 DMEMIT("%u ", m->queue_if_no_path +
1249 (m->pg_init_retries > 0) * 2);
1250 if (m->queue_if_no_path)
1251 DMEMIT("queue_if_no_path ");
1252 if (m->pg_init_retries)
1253 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1256 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1259 DMEMIT("1 %s ", m->hw_handler_name);
1261 DMEMIT("%u ", m->nr_priority_groups);
1264 pg_num = m->next_pg->pg_num;
1265 else if (m->current_pg)
1266 pg_num = m->current_pg->pg_num;
1270 DMEMIT("%u ", pg_num);
1273 case STATUSTYPE_INFO:
1274 list_for_each_entry(pg, &m->priority_groups, list) {
1276 state = 'D'; /* Disabled */
1277 else if (pg == m->current_pg)
1278 state = 'A'; /* Currently Active */
1280 state = 'E'; /* Enabled */
1282 DMEMIT("%c ", state);
1284 if (pg->ps.type->status)
1285 sz += pg->ps.type->status(&pg->ps, NULL, type,
1291 DMEMIT("%u %u ", pg->nr_pgpaths,
1292 pg->ps.type->info_args);
1294 list_for_each_entry(p, &pg->pgpaths, list) {
1295 DMEMIT("%s %s %u ", p->path.dev->name,
1296 p->is_active ? "A" : "F",
1298 if (pg->ps.type->status)
1299 sz += pg->ps.type->status(&pg->ps,
1300 &p->path, type, result + sz,
1306 case STATUSTYPE_TABLE:
1307 list_for_each_entry(pg, &m->priority_groups, list) {
1308 DMEMIT("%s ", pg->ps.type->name);
1310 if (pg->ps.type->status)
1311 sz += pg->ps.type->status(&pg->ps, NULL, type,
1317 DMEMIT("%u %u ", pg->nr_pgpaths,
1318 pg->ps.type->table_args);
1320 list_for_each_entry(p, &pg->pgpaths, list) {
1321 DMEMIT("%s ", p->path.dev->name);
1322 if (pg->ps.type->status)
1323 sz += pg->ps.type->status(&pg->ps,
1324 &p->path, type, result + sz,
1331 spin_unlock_irqrestore(&m->lock, flags);
1336 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1340 struct multipath *m = (struct multipath *) ti->private;
1344 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1345 return queue_if_no_path(m, 1, 0);
1346 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1347 return queue_if_no_path(m, 0, 0);
1353 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1354 return bypass_pg_num(m, argv[1], 1);
1355 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1356 return bypass_pg_num(m, argv[1], 0);
1357 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1358 return switch_pg_num(m, argv[1]);
1359 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1360 action = reinstate_path;
1361 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1366 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1367 dm_table_get_mode(ti->table), &dev);
1369 DMWARN("message: error getting device %s",
1374 r = action_dev(m, dev, action);
1376 dm_put_device(ti, dev);
1381 DMWARN("Unrecognised multipath message received.");
1385 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1386 struct file *filp, unsigned int cmd,
1389 struct multipath *m = (struct multipath *) ti->private;
1390 struct block_device *bdev = NULL;
1391 unsigned long flags;
1392 struct file fake_file = {};
1393 struct dentry fake_dentry = {};
1396 fake_file.f_path.dentry = &fake_dentry;
1398 spin_lock_irqsave(&m->lock, flags);
1400 if (!m->current_pgpath)
1403 if (m->current_pgpath) {
1404 bdev = m->current_pgpath->path.dev->bdev;
1405 fake_dentry.d_inode = bdev->bd_inode;
1406 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1414 spin_unlock_irqrestore(&m->lock, flags);
1416 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1417 bdev->bd_disk, cmd, arg);
1420 /*-----------------------------------------------------------------
1422 *---------------------------------------------------------------*/
1423 static struct target_type multipath_target = {
1424 .name = "multipath",
1425 .version = {1, 0, 5},
1426 .module = THIS_MODULE,
1427 .ctr = multipath_ctr,
1428 .dtr = multipath_dtr,
1429 .map = multipath_map,
1430 .end_io = multipath_end_io,
1431 .presuspend = multipath_presuspend,
1432 .resume = multipath_resume,
1433 .status = multipath_status,
1434 .message = multipath_message,
1435 .ioctl = multipath_ioctl,
1438 static int __init dm_multipath_init(void)
1442 /* allocate a slab for the dm_ios */
1443 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1447 r = dm_register_target(&multipath_target);
1449 DMERR("register failed %d", r);
1450 kmem_cache_destroy(_mpio_cache);
1454 kmultipathd = create_workqueue("kmpathd");
1456 DMERR("failed to create workqueue kmpathd");
1457 dm_unregister_target(&multipath_target);
1458 kmem_cache_destroy(_mpio_cache);
1463 * A separate workqueue is used to handle the device handlers
1464 * to avoid overloading existing workqueue. Overloading the
1465 * old workqueue would also create a bottleneck in the
1466 * path of the storage hardware device activation.
1468 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1469 if (!kmpath_handlerd) {
1470 DMERR("failed to create workqueue kmpath_handlerd");
1471 destroy_workqueue(kmultipathd);
1472 dm_unregister_target(&multipath_target);
1473 kmem_cache_destroy(_mpio_cache);
1477 DMINFO("version %u.%u.%u loaded",
1478 multipath_target.version[0], multipath_target.version[1],
1479 multipath_target.version[2]);
1484 static void __exit dm_multipath_exit(void)
1488 destroy_workqueue(kmpath_handlerd);
1489 destroy_workqueue(kmultipathd);
1491 r = dm_unregister_target(&multipath_target);
1493 DMERR("target unregister failed %d", r);
1494 kmem_cache_destroy(_mpio_cache);
1497 module_init(dm_multipath_init);
1498 module_exit(dm_multipath_exit);
1500 MODULE_DESCRIPTION(DM_NAME " multipath target");
1501 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1502 MODULE_LICENSE("GPL");