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-uevent.h"
12 #include <linux/ctype.h>
13 #include <linux/init.h>
14 #include <linux/mempool.h>
15 #include <linux/module.h>
16 #include <linux/pagemap.h>
17 #include <linux/slab.h>
18 #include <linux/time.h>
19 #include <linux/workqueue.h>
20 #include <scsi/scsi_dh.h>
21 #include <asm/atomic.h>
23 #define DM_MSG_PREFIX "multipath"
24 #define MESG_STR(x) x, sizeof(x)
28 struct list_head list;
30 struct priority_group *pg; /* Owning PG */
31 unsigned is_active; /* Path status */
32 unsigned fail_count; /* Cumulative failure count */
35 struct work_struct deactivate_path;
38 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
44 struct priority_group {
45 struct list_head list;
47 struct multipath *m; /* Owning multipath instance */
48 struct path_selector ps;
50 unsigned pg_num; /* Reference number */
51 unsigned bypassed; /* Temporarily bypass this PG? */
53 unsigned nr_pgpaths; /* Number of paths in PG */
54 struct list_head pgpaths;
57 /* Multipath context */
59 struct list_head list;
64 const char *hw_handler_name;
65 struct work_struct activate_path;
66 struct pgpath *pgpath_to_activate;
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 list_head 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;
104 typedef int (*action_fn) (struct pgpath *pgpath);
106 #define MIN_IOS 256 /* Mempool size */
108 static struct kmem_cache *_mpio_cache;
110 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
111 static void process_queued_ios(struct work_struct *work);
112 static void trigger_event(struct work_struct *work);
113 static void activate_path(struct work_struct *work);
114 static void deactivate_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->is_active = 1;
127 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
133 static void free_pgpath(struct pgpath *pgpath)
138 static void deactivate_path(struct work_struct *work)
140 struct pgpath *pgpath =
141 container_of(work, struct pgpath, deactivate_path);
143 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
146 static struct priority_group *alloc_priority_group(void)
148 struct priority_group *pg;
150 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
153 INIT_LIST_HEAD(&pg->pgpaths);
158 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
161 struct pgpath *pgpath, *tmp;
162 struct multipath *m = ti->private;
164 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
165 list_del(&pgpath->list);
166 if (m->hw_handler_name)
167 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
168 dm_put_device(ti, pgpath->path.dev);
169 spin_lock_irqsave(&m->lock, flags);
170 if (m->pgpath_to_activate == pgpath)
171 m->pgpath_to_activate = NULL;
172 spin_unlock_irqrestore(&m->lock, flags);
177 static void free_priority_group(struct priority_group *pg,
178 struct dm_target *ti)
180 struct path_selector *ps = &pg->ps;
183 ps->type->destroy(ps);
184 dm_put_path_selector(ps->type);
187 free_pgpaths(&pg->pgpaths, ti);
191 static struct multipath *alloc_multipath(struct dm_target *ti)
195 m = kzalloc(sizeof(*m), GFP_KERNEL);
197 INIT_LIST_HEAD(&m->priority_groups);
198 INIT_LIST_HEAD(&m->queued_ios);
199 spin_lock_init(&m->lock);
201 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
203 INIT_WORK(&m->activate_path, activate_path);
204 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
216 static void free_multipath(struct multipath *m)
218 struct priority_group *pg, *tmp;
220 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
222 free_priority_group(pg, m->ti);
225 kfree(m->hw_handler_name);
226 mempool_destroy(m->mpio_pool);
231 /*-----------------------------------------------
233 *-----------------------------------------------*/
235 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
237 m->current_pg = pgpath->pg;
239 /* Must we initialise the PG first, and queue I/O till it's ready? */
240 if (m->hw_handler_name) {
241 m->pg_init_required = 1;
244 m->pg_init_required = 0;
248 m->pg_init_count = 0;
251 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
253 struct dm_path *path;
255 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
259 m->current_pgpath = path_to_pgpath(path);
261 if (m->current_pg != pg)
262 __switch_pg(m, m->current_pgpath);
267 static void __choose_pgpath(struct multipath *m)
269 struct priority_group *pg;
270 unsigned bypassed = 1;
272 if (!m->nr_valid_paths)
275 /* Were we instructed to switch PG? */
279 if (!__choose_path_in_pg(m, pg))
283 /* Don't change PG until it has no remaining paths */
284 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
288 * Loop through priority groups until we find a valid path.
289 * First time we skip PGs marked 'bypassed'.
290 * Second time we only try the ones we skipped.
293 list_for_each_entry(pg, &m->priority_groups, list) {
294 if (pg->bypassed == bypassed)
296 if (!__choose_path_in_pg(m, pg))
299 } while (bypassed--);
302 m->current_pgpath = NULL;
303 m->current_pg = NULL;
307 * Check whether bios must be queued in the device-mapper core rather
308 * than here in the target.
310 * m->lock must be held on entry.
312 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
313 * same value then we are not between multipath_presuspend()
314 * and multipath_resume() calls and we have no need to check
315 * for the DMF_NOFLUSH_SUSPENDING flag.
317 static int __must_push_back(struct multipath *m)
319 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
320 dm_noflush_suspending(m->ti));
323 static int map_io(struct multipath *m, struct request *clone,
324 struct dm_mpath_io *mpio, unsigned was_queued)
326 int r = DM_MAPIO_REMAPPED;
328 struct pgpath *pgpath;
329 struct block_device *bdev;
331 spin_lock_irqsave(&m->lock, flags);
333 /* Do we need to select a new pgpath? */
334 if (!m->current_pgpath ||
335 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
338 pgpath = m->current_pgpath;
343 if ((pgpath && m->queue_io) ||
344 (!pgpath && m->queue_if_no_path)) {
345 /* Queue for the daemon to resubmit */
346 list_add_tail(&clone->queuelist, &m->queued_ios);
348 if ((m->pg_init_required && !m->pg_init_in_progress) ||
350 queue_work(kmultipathd, &m->process_queued_ios);
352 r = DM_MAPIO_SUBMITTED;
354 bdev = pgpath->path.dev->bdev;
355 clone->q = bdev_get_queue(bdev);
356 clone->rq_disk = bdev->bd_disk;
357 } else if (__must_push_back(m))
358 r = DM_MAPIO_REQUEUE;
360 r = -EIO; /* Failed */
362 mpio->pgpath = pgpath;
364 spin_unlock_irqrestore(&m->lock, flags);
370 * If we run out of usable paths, should we queue I/O or error it?
372 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
373 unsigned save_old_value)
377 spin_lock_irqsave(&m->lock, flags);
380 m->saved_queue_if_no_path = m->queue_if_no_path;
382 m->saved_queue_if_no_path = queue_if_no_path;
383 m->queue_if_no_path = queue_if_no_path;
384 if (!m->queue_if_no_path && m->queue_size)
385 queue_work(kmultipathd, &m->process_queued_ios);
387 spin_unlock_irqrestore(&m->lock, flags);
392 /*-----------------------------------------------------------------
393 * The multipath daemon is responsible for resubmitting queued ios.
394 *---------------------------------------------------------------*/
396 static void dispatch_queued_ios(struct multipath *m)
400 struct dm_mpath_io *mpio;
401 union map_info *info;
402 struct request *clone, *n;
405 spin_lock_irqsave(&m->lock, flags);
406 list_splice_init(&m->queued_ios, &cl);
407 spin_unlock_irqrestore(&m->lock, flags);
409 list_for_each_entry_safe(clone, n, &cl, queuelist) {
410 list_del_init(&clone->queuelist);
412 info = dm_get_rq_mapinfo(clone);
415 r = map_io(m, clone, mpio, 1);
417 mempool_free(mpio, m->mpio_pool);
418 dm_kill_request(clone, r);
419 } else if (r == DM_MAPIO_REMAPPED)
420 dm_dispatch_request(clone);
421 else if (r == DM_MAPIO_REQUEUE) {
422 mempool_free(mpio, m->mpio_pool);
423 dm_requeue_request(clone);
428 static void process_queued_ios(struct work_struct *work)
430 struct multipath *m =
431 container_of(work, struct multipath, process_queued_ios);
432 struct pgpath *pgpath = NULL;
433 unsigned init_required = 0, must_queue = 1;
436 spin_lock_irqsave(&m->lock, flags);
441 if (!m->current_pgpath)
444 pgpath = m->current_pgpath;
445 m->pgpath_to_activate = m->current_pgpath;
447 if ((pgpath && !m->queue_io) ||
448 (!pgpath && !m->queue_if_no_path))
451 if (m->pg_init_required && !m->pg_init_in_progress) {
453 m->pg_init_required = 0;
454 m->pg_init_in_progress = 1;
459 spin_unlock_irqrestore(&m->lock, flags);
462 queue_work(kmpath_handlerd, &m->activate_path);
465 dispatch_queued_ios(m);
469 * An event is triggered whenever a path is taken out of use.
470 * Includes path failure and PG bypass.
472 static void trigger_event(struct work_struct *work)
474 struct multipath *m =
475 container_of(work, struct multipath, trigger_event);
477 dm_table_event(m->ti->table);
480 /*-----------------------------------------------------------------
481 * Constructor/argument parsing:
482 * <#multipath feature args> [<arg>]*
483 * <#hw_handler args> [hw_handler [<arg>]*]
485 * <initial priority group>
486 * [<selector> <#selector args> [<arg>]*
487 * <#paths> <#per-path selector args>
488 * [<path> [<arg>]* ]+ ]+
489 *---------------------------------------------------------------*/
496 static int read_param(struct param *param, char *str, unsigned *v, char **error)
499 (sscanf(str, "%u", v) != 1) ||
502 *error = param->error;
514 static char *shift(struct arg_set *as)
528 static void consume(struct arg_set *as, unsigned n)
530 BUG_ON (as->argc < n);
535 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
536 struct dm_target *ti)
539 struct path_selector_type *pst;
542 static struct param _params[] = {
543 {0, 1024, "invalid number of path selector args"},
546 pst = dm_get_path_selector(shift(as));
548 ti->error = "unknown path selector type";
552 r = read_param(_params, shift(as), &ps_argc, &ti->error);
554 dm_put_path_selector(pst);
558 r = pst->create(&pg->ps, ps_argc, as->argv);
560 dm_put_path_selector(pst);
561 ti->error = "path selector constructor failed";
566 consume(as, ps_argc);
571 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
572 struct dm_target *ti)
576 struct multipath *m = ti->private;
578 /* we need at least a path arg */
580 ti->error = "no device given";
588 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
589 dm_table_get_mode(ti->table), &p->path.dev);
591 ti->error = "error getting device";
595 if (m->hw_handler_name) {
596 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
598 r = scsi_dh_attach(q, m->hw_handler_name);
601 * Already attached to different hw_handler,
602 * try to reattach with correct one.
605 r = scsi_dh_attach(q, m->hw_handler_name);
608 ti->error = "error attaching hardware handler";
609 dm_put_device(ti, p->path.dev);
614 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
616 dm_put_device(ti, p->path.dev);
627 static struct priority_group *parse_priority_group(struct arg_set *as,
630 static struct param _params[] = {
631 {1, 1024, "invalid number of paths"},
632 {0, 1024, "invalid number of selector args"}
636 unsigned i, nr_selector_args, nr_params;
637 struct priority_group *pg;
638 struct dm_target *ti = m->ti;
642 ti->error = "not enough priority group aruments";
646 pg = alloc_priority_group();
648 ti->error = "couldn't allocate priority group";
653 r = parse_path_selector(as, pg, ti);
660 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
664 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
668 nr_params = 1 + nr_selector_args;
669 for (i = 0; i < pg->nr_pgpaths; i++) {
670 struct pgpath *pgpath;
671 struct arg_set path_args;
673 if (as->argc < nr_params) {
674 ti->error = "not enough path parameters";
678 path_args.argc = nr_params;
679 path_args.argv = as->argv;
681 pgpath = parse_path(&path_args, &pg->ps, ti);
686 list_add_tail(&pgpath->list, &pg->pgpaths);
687 consume(as, nr_params);
693 free_priority_group(pg, ti);
697 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
700 struct dm_target *ti = m->ti;
702 static struct param _params[] = {
703 {0, 1024, "invalid number of hardware handler args"},
706 if (read_param(_params, shift(as), &hw_argc, &ti->error))
712 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
713 request_module("scsi_dh_%s", m->hw_handler_name);
714 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
715 ti->error = "unknown hardware handler type";
716 kfree(m->hw_handler_name);
717 m->hw_handler_name = NULL;
720 consume(as, hw_argc - 1);
725 static int parse_features(struct arg_set *as, struct multipath *m)
729 struct dm_target *ti = m->ti;
730 const char *param_name;
732 static struct param _params[] = {
733 {0, 3, "invalid number of feature args"},
734 {1, 50, "pg_init_retries must be between 1 and 50"},
737 r = read_param(_params, shift(as), &argc, &ti->error);
745 param_name = shift(as);
748 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
749 r = queue_if_no_path(m, 1, 0);
753 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
755 r = read_param(_params + 1, shift(as),
756 &m->pg_init_retries, &ti->error);
761 ti->error = "Unrecognised multipath feature request";
763 } while (argc && !r);
768 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
771 /* target parameters */
772 static struct param _params[] = {
773 {1, 1024, "invalid number of priority groups"},
774 {1, 1024, "invalid initial priority group number"},
780 unsigned pg_count = 0;
781 unsigned next_pg_num;
786 m = alloc_multipath(ti);
788 ti->error = "can't allocate multipath";
792 r = parse_features(&as, m);
796 r = parse_hw_handler(&as, m);
800 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
804 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
808 /* parse the priority groups */
810 struct priority_group *pg;
812 pg = parse_priority_group(&as, m);
818 m->nr_valid_paths += pg->nr_pgpaths;
819 list_add_tail(&pg->list, &m->priority_groups);
821 pg->pg_num = pg_count;
826 if (pg_count != m->nr_priority_groups) {
827 ti->error = "priority group count mismatch";
839 static void multipath_dtr(struct dm_target *ti)
841 struct multipath *m = (struct multipath *) ti->private;
843 flush_workqueue(kmpath_handlerd);
844 flush_workqueue(kmultipathd);
849 * Map cloned requests
851 static int multipath_map(struct dm_target *ti, struct request *clone,
852 union map_info *map_context)
855 struct dm_mpath_io *mpio;
856 struct multipath *m = (struct multipath *) ti->private;
858 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
860 /* ENOMEM, requeue */
861 return DM_MAPIO_REQUEUE;
862 memset(mpio, 0, sizeof(*mpio));
864 map_context->ptr = mpio;
865 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
866 r = map_io(m, clone, mpio, 0);
867 if (r < 0 || r == DM_MAPIO_REQUEUE)
868 mempool_free(mpio, m->mpio_pool);
874 * Take a path out of use.
876 static int fail_path(struct pgpath *pgpath)
879 struct multipath *m = pgpath->pg->m;
881 spin_lock_irqsave(&m->lock, flags);
883 if (!pgpath->is_active)
886 DMWARN("Failing path %s.", pgpath->path.dev->name);
888 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
889 pgpath->is_active = 0;
890 pgpath->fail_count++;
894 if (pgpath == m->current_pgpath)
895 m->current_pgpath = NULL;
897 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
898 pgpath->path.dev->name, m->nr_valid_paths);
900 queue_work(kmultipathd, &m->trigger_event);
901 queue_work(kmultipathd, &pgpath->deactivate_path);
904 spin_unlock_irqrestore(&m->lock, flags);
910 * Reinstate a previously-failed path
912 static int reinstate_path(struct pgpath *pgpath)
916 struct multipath *m = pgpath->pg->m;
918 spin_lock_irqsave(&m->lock, flags);
920 if (pgpath->is_active)
923 if (!pgpath->pg->ps.type->reinstate_path) {
924 DMWARN("Reinstate path not supported by path selector %s",
925 pgpath->pg->ps.type->name);
930 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
934 pgpath->is_active = 1;
936 m->current_pgpath = NULL;
937 if (!m->nr_valid_paths++ && m->queue_size)
938 queue_work(kmultipathd, &m->process_queued_ios);
940 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
941 pgpath->path.dev->name, m->nr_valid_paths);
943 queue_work(kmultipathd, &m->trigger_event);
946 spin_unlock_irqrestore(&m->lock, flags);
952 * Fail or reinstate all paths that match the provided struct dm_dev.
954 static int action_dev(struct multipath *m, struct dm_dev *dev,
958 struct pgpath *pgpath;
959 struct priority_group *pg;
961 list_for_each_entry(pg, &m->priority_groups, list) {
962 list_for_each_entry(pgpath, &pg->pgpaths, list) {
963 if (pgpath->path.dev == dev)
972 * Temporarily try to avoid having to use the specified PG
974 static void bypass_pg(struct multipath *m, struct priority_group *pg,
979 spin_lock_irqsave(&m->lock, flags);
981 pg->bypassed = bypassed;
982 m->current_pgpath = NULL;
983 m->current_pg = NULL;
985 spin_unlock_irqrestore(&m->lock, flags);
987 queue_work(kmultipathd, &m->trigger_event);
991 * Switch to using the specified PG from the next I/O that gets mapped
993 static int switch_pg_num(struct multipath *m, const char *pgstr)
995 struct priority_group *pg;
999 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1000 (pgnum > m->nr_priority_groups)) {
1001 DMWARN("invalid PG number supplied to switch_pg_num");
1005 spin_lock_irqsave(&m->lock, flags);
1006 list_for_each_entry(pg, &m->priority_groups, list) {
1011 m->current_pgpath = NULL;
1012 m->current_pg = NULL;
1015 spin_unlock_irqrestore(&m->lock, flags);
1017 queue_work(kmultipathd, &m->trigger_event);
1022 * Set/clear bypassed status of a PG.
1023 * PGs are numbered upwards from 1 in the order they were declared.
1025 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1027 struct priority_group *pg;
1030 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1031 (pgnum > m->nr_priority_groups)) {
1032 DMWARN("invalid PG number supplied to bypass_pg");
1036 list_for_each_entry(pg, &m->priority_groups, list) {
1041 bypass_pg(m, pg, bypassed);
1046 * Should we retry pg_init immediately?
1048 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1050 unsigned long flags;
1051 int limit_reached = 0;
1053 spin_lock_irqsave(&m->lock, flags);
1055 if (m->pg_init_count <= m->pg_init_retries)
1056 m->pg_init_required = 1;
1060 spin_unlock_irqrestore(&m->lock, flags);
1062 return limit_reached;
1065 static void pg_init_done(struct dm_path *path, int errors)
1067 struct pgpath *pgpath = path_to_pgpath(path);
1068 struct priority_group *pg = pgpath->pg;
1069 struct multipath *m = pg->m;
1070 unsigned long flags;
1072 /* device or driver problems */
1077 if (!m->hw_handler_name) {
1081 DMERR("Cannot failover device because scsi_dh_%s was not "
1082 "loaded.", m->hw_handler_name);
1084 * Fail path for now, so we do not ping pong
1088 case SCSI_DH_DEV_TEMP_BUSY:
1090 * Probably doing something like FW upgrade on the
1091 * controller so try the other pg.
1093 bypass_pg(m, pg, 1);
1095 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1097 case SCSI_DH_IMM_RETRY:
1098 case SCSI_DH_RES_TEMP_UNAVAIL:
1099 if (pg_init_limit_reached(m, pgpath))
1105 * We probably do not want to fail the path for a device
1106 * error, but this is what the old dm did. In future
1107 * patches we can do more advanced handling.
1112 spin_lock_irqsave(&m->lock, flags);
1114 DMERR("Could not failover device. Error %d.", errors);
1115 m->current_pgpath = NULL;
1116 m->current_pg = NULL;
1117 } else if (!m->pg_init_required) {
1122 m->pg_init_in_progress = 0;
1123 queue_work(kmultipathd, &m->process_queued_ios);
1124 spin_unlock_irqrestore(&m->lock, flags);
1127 static void activate_path(struct work_struct *work)
1130 struct multipath *m =
1131 container_of(work, struct multipath, activate_path);
1132 struct dm_path *path;
1133 unsigned long flags;
1135 spin_lock_irqsave(&m->lock, flags);
1136 path = &m->pgpath_to_activate->path;
1137 m->pgpath_to_activate = NULL;
1138 spin_unlock_irqrestore(&m->lock, flags);
1141 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1142 pg_init_done(path, ret);
1148 static int do_end_io(struct multipath *m, struct request *clone,
1149 int error, struct dm_mpath_io *mpio)
1152 * We don't queue any clone request inside the multipath target
1153 * during end I/O handling, since those clone requests don't have
1154 * bio clones. If we queue them inside the multipath target,
1155 * we need to make bio clones, that requires memory allocation.
1156 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1157 * don't have bio clones.)
1158 * Instead of queueing the clone request here, we queue the original
1159 * request into dm core, which will remake a clone request and
1160 * clone bios for it and resubmit it later.
1162 int r = DM_ENDIO_REQUEUE;
1163 unsigned long flags;
1165 if (!error && !clone->errors)
1166 return 0; /* I/O complete */
1168 if (error == -EOPNOTSUPP)
1172 fail_path(mpio->pgpath);
1174 spin_lock_irqsave(&m->lock, flags);
1175 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1177 spin_unlock_irqrestore(&m->lock, flags);
1182 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1183 int error, union map_info *map_context)
1185 struct multipath *m = ti->private;
1186 struct dm_mpath_io *mpio = map_context->ptr;
1187 struct pgpath *pgpath = mpio->pgpath;
1188 struct path_selector *ps;
1191 r = do_end_io(m, clone, error, mpio);
1193 ps = &pgpath->pg->ps;
1194 if (ps->type->end_io)
1195 ps->type->end_io(ps, &pgpath->path);
1197 mempool_free(mpio, m->mpio_pool);
1203 * Suspend can't complete until all the I/O is processed so if
1204 * the last path fails we must error any remaining I/O.
1205 * Note that if the freeze_bdev fails while suspending, the
1206 * queue_if_no_path state is lost - userspace should reset it.
1208 static void multipath_presuspend(struct dm_target *ti)
1210 struct multipath *m = (struct multipath *) ti->private;
1212 queue_if_no_path(m, 0, 1);
1216 * Restore the queue_if_no_path setting.
1218 static void multipath_resume(struct dm_target *ti)
1220 struct multipath *m = (struct multipath *) ti->private;
1221 unsigned long flags;
1223 spin_lock_irqsave(&m->lock, flags);
1224 m->queue_if_no_path = m->saved_queue_if_no_path;
1225 spin_unlock_irqrestore(&m->lock, flags);
1229 * Info output has the following format:
1230 * num_multipath_feature_args [multipath_feature_args]*
1231 * num_handler_status_args [handler_status_args]*
1232 * num_groups init_group_number
1233 * [A|D|E num_ps_status_args [ps_status_args]*
1234 * num_paths num_selector_args
1235 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1237 * Table output has the following format (identical to the constructor string):
1238 * num_feature_args [features_args]*
1239 * num_handler_args hw_handler [hw_handler_args]*
1240 * num_groups init_group_number
1241 * [priority selector-name num_ps_args [ps_args]*
1242 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1244 static int multipath_status(struct dm_target *ti, status_type_t type,
1245 char *result, unsigned int maxlen)
1248 unsigned long flags;
1249 struct multipath *m = (struct multipath *) ti->private;
1250 struct priority_group *pg;
1255 spin_lock_irqsave(&m->lock, flags);
1258 if (type == STATUSTYPE_INFO)
1259 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1261 DMEMIT("%u ", m->queue_if_no_path +
1262 (m->pg_init_retries > 0) * 2);
1263 if (m->queue_if_no_path)
1264 DMEMIT("queue_if_no_path ");
1265 if (m->pg_init_retries)
1266 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1269 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1272 DMEMIT("1 %s ", m->hw_handler_name);
1274 DMEMIT("%u ", m->nr_priority_groups);
1277 pg_num = m->next_pg->pg_num;
1278 else if (m->current_pg)
1279 pg_num = m->current_pg->pg_num;
1283 DMEMIT("%u ", pg_num);
1286 case STATUSTYPE_INFO:
1287 list_for_each_entry(pg, &m->priority_groups, list) {
1289 state = 'D'; /* Disabled */
1290 else if (pg == m->current_pg)
1291 state = 'A'; /* Currently Active */
1293 state = 'E'; /* Enabled */
1295 DMEMIT("%c ", state);
1297 if (pg->ps.type->status)
1298 sz += pg->ps.type->status(&pg->ps, NULL, type,
1304 DMEMIT("%u %u ", pg->nr_pgpaths,
1305 pg->ps.type->info_args);
1307 list_for_each_entry(p, &pg->pgpaths, list) {
1308 DMEMIT("%s %s %u ", p->path.dev->name,
1309 p->is_active ? "A" : "F",
1311 if (pg->ps.type->status)
1312 sz += pg->ps.type->status(&pg->ps,
1313 &p->path, type, result + sz,
1319 case STATUSTYPE_TABLE:
1320 list_for_each_entry(pg, &m->priority_groups, list) {
1321 DMEMIT("%s ", pg->ps.type->name);
1323 if (pg->ps.type->status)
1324 sz += pg->ps.type->status(&pg->ps, NULL, type,
1330 DMEMIT("%u %u ", pg->nr_pgpaths,
1331 pg->ps.type->table_args);
1333 list_for_each_entry(p, &pg->pgpaths, list) {
1334 DMEMIT("%s ", p->path.dev->name);
1335 if (pg->ps.type->status)
1336 sz += pg->ps.type->status(&pg->ps,
1337 &p->path, type, result + sz,
1344 spin_unlock_irqrestore(&m->lock, flags);
1349 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1353 struct multipath *m = (struct multipath *) ti->private;
1357 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1358 return queue_if_no_path(m, 1, 0);
1359 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1360 return queue_if_no_path(m, 0, 0);
1366 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1367 return bypass_pg_num(m, argv[1], 1);
1368 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1369 return bypass_pg_num(m, argv[1], 0);
1370 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1371 return switch_pg_num(m, argv[1]);
1372 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1373 action = reinstate_path;
1374 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1379 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1380 dm_table_get_mode(ti->table), &dev);
1382 DMWARN("message: error getting device %s",
1387 r = action_dev(m, dev, action);
1389 dm_put_device(ti, dev);
1394 DMWARN("Unrecognised multipath message received.");
1398 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1399 struct file *filp, unsigned int cmd,
1402 struct multipath *m = (struct multipath *) ti->private;
1403 struct block_device *bdev = NULL;
1404 unsigned long flags;
1405 struct file fake_file = {};
1406 struct dentry fake_dentry = {};
1409 fake_file.f_path.dentry = &fake_dentry;
1411 spin_lock_irqsave(&m->lock, flags);
1413 if (!m->current_pgpath)
1416 if (m->current_pgpath) {
1417 bdev = m->current_pgpath->path.dev->bdev;
1418 fake_dentry.d_inode = bdev->bd_inode;
1419 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1427 spin_unlock_irqrestore(&m->lock, flags);
1429 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1430 bdev->bd_disk, cmd, arg);
1433 static int __pgpath_busy(struct pgpath *pgpath)
1435 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1437 return dm_underlying_device_busy(q);
1441 * We return "busy", only when we can map I/Os but underlying devices
1442 * are busy (so even if we map I/Os now, the I/Os will wait on
1443 * the underlying queue).
1444 * In other words, if we want to kill I/Os or queue them inside us
1445 * due to map unavailability, we don't return "busy". Otherwise,
1446 * dm core won't give us the I/Os and we can't do what we want.
1448 static int multipath_busy(struct dm_target *ti)
1450 int busy = 0, has_active = 0;
1451 struct multipath *m = (struct multipath *) ti->private;
1452 struct priority_group *pg;
1453 struct pgpath *pgpath;
1454 unsigned long flags;
1456 spin_lock_irqsave(&m->lock, flags);
1458 /* Guess which priority_group will be used at next mapping time */
1459 if (unlikely(!m->current_pgpath && m->next_pg))
1461 else if (likely(m->current_pg))
1465 * We don't know which pg will be used at next mapping time.
1466 * We don't call __choose_pgpath() here to avoid to trigger
1467 * pg_init just by busy checking.
1468 * So we don't know whether underlying devices we will be using
1469 * at next mapping time are busy or not. Just try mapping.
1474 * If there is one non-busy active path at least, the path selector
1475 * will be able to select it. So we consider such a pg as not busy.
1478 list_for_each_entry(pgpath, &pg->pgpaths, list)
1479 if (pgpath->is_active) {
1482 if (!__pgpath_busy(pgpath)) {
1490 * No active path in this pg, so this pg won't be used and
1491 * the current_pg will be changed at next mapping time.
1492 * We need to try mapping to determine it.
1497 spin_unlock_irqrestore(&m->lock, flags);
1502 /*-----------------------------------------------------------------
1504 *---------------------------------------------------------------*/
1505 static struct target_type multipath_target = {
1506 .name = "multipath",
1507 .version = {1, 0, 5},
1508 .module = THIS_MODULE,
1509 .ctr = multipath_ctr,
1510 .dtr = multipath_dtr,
1511 .map_rq = multipath_map,
1512 .rq_end_io = multipath_end_io,
1513 .presuspend = multipath_presuspend,
1514 .resume = multipath_resume,
1515 .status = multipath_status,
1516 .message = multipath_message,
1517 .ioctl = multipath_ioctl,
1518 .busy = multipath_busy,
1521 static int __init dm_multipath_init(void)
1525 /* allocate a slab for the dm_ios */
1526 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1530 r = dm_register_target(&multipath_target);
1532 DMERR("register failed %d", r);
1533 kmem_cache_destroy(_mpio_cache);
1537 kmultipathd = create_workqueue("kmpathd");
1539 DMERR("failed to create workqueue kmpathd");
1540 dm_unregister_target(&multipath_target);
1541 kmem_cache_destroy(_mpio_cache);
1546 * A separate workqueue is used to handle the device handlers
1547 * to avoid overloading existing workqueue. Overloading the
1548 * old workqueue would also create a bottleneck in the
1549 * path of the storage hardware device activation.
1551 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1552 if (!kmpath_handlerd) {
1553 DMERR("failed to create workqueue kmpath_handlerd");
1554 destroy_workqueue(kmultipathd);
1555 dm_unregister_target(&multipath_target);
1556 kmem_cache_destroy(_mpio_cache);
1560 DMINFO("version %u.%u.%u loaded",
1561 multipath_target.version[0], multipath_target.version[1],
1562 multipath_target.version[2]);
1567 static void __exit dm_multipath_exit(void)
1571 destroy_workqueue(kmpath_handlerd);
1572 destroy_workqueue(kmultipathd);
1574 r = dm_unregister_target(&multipath_target);
1576 DMERR("target unregister failed %d", r);
1577 kmem_cache_destroy(_mpio_cache);
1580 module_init(dm_multipath_init);
1581 module_exit(dm_multipath_exit);
1583 MODULE_DESCRIPTION(DM_NAME " multipath target");
1584 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1585 MODULE_LICENSE("GPL");