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;
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);
115 static void deactivate_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;
128 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
134 static void free_pgpath(struct pgpath *pgpath)
139 static void deactivate_path(struct work_struct *work)
141 struct pgpath *pgpath =
142 container_of(work, struct pgpath, deactivate_path);
144 if (pgpath->path.dev)
145 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
148 static struct priority_group *alloc_priority_group(void)
150 struct priority_group *pg;
152 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
155 INIT_LIST_HEAD(&pg->pgpaths);
160 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163 struct pgpath *pgpath, *tmp;
164 struct multipath *m = ti->private;
166 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
167 list_del(&pgpath->list);
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,
254 struct dm_path *path;
256 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
260 m->current_pgpath = path_to_pgpath(path);
262 if (m->current_pg != pg)
263 __switch_pg(m, m->current_pgpath);
268 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
270 struct priority_group *pg;
271 unsigned bypassed = 1;
273 if (!m->nr_valid_paths)
276 /* Were we instructed to switch PG? */
280 if (!__choose_path_in_pg(m, pg, nr_bytes))
284 /* Don't change PG until it has no remaining paths */
285 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
289 * Loop through priority groups until we find a valid path.
290 * First time we skip PGs marked 'bypassed'.
291 * Second time we only try the ones we skipped.
294 list_for_each_entry(pg, &m->priority_groups, list) {
295 if (pg->bypassed == bypassed)
297 if (!__choose_path_in_pg(m, pg, nr_bytes))
300 } while (bypassed--);
303 m->current_pgpath = NULL;
304 m->current_pg = NULL;
308 * Check whether bios must be queued in the device-mapper core rather
309 * than here in the target.
311 * m->lock must be held on entry.
313 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
314 * same value then we are not between multipath_presuspend()
315 * and multipath_resume() calls and we have no need to check
316 * for the DMF_NOFLUSH_SUSPENDING flag.
318 static int __must_push_back(struct multipath *m)
320 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
321 dm_noflush_suspending(m->ti));
324 static int map_io(struct multipath *m, struct request *clone,
325 struct dm_mpath_io *mpio, unsigned was_queued)
327 int r = DM_MAPIO_REMAPPED;
328 size_t nr_bytes = blk_rq_bytes(clone);
330 struct pgpath *pgpath;
331 struct block_device *bdev;
333 spin_lock_irqsave(&m->lock, flags);
335 /* Do we need to select a new pgpath? */
336 if (!m->current_pgpath ||
337 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
338 __choose_pgpath(m, nr_bytes);
340 pgpath = m->current_pgpath;
345 if ((pgpath && m->queue_io) ||
346 (!pgpath && m->queue_if_no_path)) {
347 /* Queue for the daemon to resubmit */
348 list_add_tail(&clone->queuelist, &m->queued_ios);
350 if ((m->pg_init_required && !m->pg_init_in_progress) ||
352 queue_work(kmultipathd, &m->process_queued_ios);
354 r = DM_MAPIO_SUBMITTED;
356 bdev = pgpath->path.dev->bdev;
357 clone->q = bdev_get_queue(bdev);
358 clone->rq_disk = bdev->bd_disk;
359 } else if (__must_push_back(m))
360 r = DM_MAPIO_REQUEUE;
362 r = -EIO; /* Failed */
364 mpio->pgpath = pgpath;
365 mpio->nr_bytes = nr_bytes;
367 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
368 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
371 spin_unlock_irqrestore(&m->lock, flags);
377 * If we run out of usable paths, should we queue I/O or error it?
379 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
380 unsigned save_old_value)
384 spin_lock_irqsave(&m->lock, flags);
387 m->saved_queue_if_no_path = m->queue_if_no_path;
389 m->saved_queue_if_no_path = queue_if_no_path;
390 m->queue_if_no_path = queue_if_no_path;
391 if (!m->queue_if_no_path && m->queue_size)
392 queue_work(kmultipathd, &m->process_queued_ios);
394 spin_unlock_irqrestore(&m->lock, flags);
399 /*-----------------------------------------------------------------
400 * The multipath daemon is responsible for resubmitting queued ios.
401 *---------------------------------------------------------------*/
403 static void dispatch_queued_ios(struct multipath *m)
407 struct dm_mpath_io *mpio;
408 union map_info *info;
409 struct request *clone, *n;
412 spin_lock_irqsave(&m->lock, flags);
413 list_splice_init(&m->queued_ios, &cl);
414 spin_unlock_irqrestore(&m->lock, flags);
416 list_for_each_entry_safe(clone, n, &cl, queuelist) {
417 list_del_init(&clone->queuelist);
419 info = dm_get_rq_mapinfo(clone);
422 r = map_io(m, clone, mpio, 1);
424 mempool_free(mpio, m->mpio_pool);
425 dm_kill_request(clone, r);
426 } else if (r == DM_MAPIO_REMAPPED)
427 dm_dispatch_request(clone);
428 else if (r == DM_MAPIO_REQUEUE) {
429 mempool_free(mpio, m->mpio_pool);
430 dm_requeue_request(clone);
435 static void process_queued_ios(struct work_struct *work)
437 struct multipath *m =
438 container_of(work, struct multipath, process_queued_ios);
439 struct pgpath *pgpath = NULL;
440 unsigned init_required = 0, must_queue = 1;
443 spin_lock_irqsave(&m->lock, flags);
448 if (!m->current_pgpath)
449 __choose_pgpath(m, 1 << 19); /* Assume 512 KB */
451 pgpath = m->current_pgpath;
453 if ((pgpath && !m->queue_io) ||
454 (!pgpath && !m->queue_if_no_path))
457 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
458 m->pgpath_to_activate = pgpath;
460 m->pg_init_required = 0;
461 m->pg_init_in_progress = 1;
466 spin_unlock_irqrestore(&m->lock, flags);
469 queue_work(kmpath_handlerd, &m->activate_path);
472 dispatch_queued_ios(m);
476 * An event is triggered whenever a path is taken out of use.
477 * Includes path failure and PG bypass.
479 static void trigger_event(struct work_struct *work)
481 struct multipath *m =
482 container_of(work, struct multipath, trigger_event);
484 dm_table_event(m->ti->table);
487 /*-----------------------------------------------------------------
488 * Constructor/argument parsing:
489 * <#multipath feature args> [<arg>]*
490 * <#hw_handler args> [hw_handler [<arg>]*]
492 * <initial priority group>
493 * [<selector> <#selector args> [<arg>]*
494 * <#paths> <#per-path selector args>
495 * [<path> [<arg>]* ]+ ]+
496 *---------------------------------------------------------------*/
503 static int read_param(struct param *param, char *str, unsigned *v, char **error)
506 (sscanf(str, "%u", v) != 1) ||
509 *error = param->error;
521 static char *shift(struct arg_set *as)
535 static void consume(struct arg_set *as, unsigned n)
537 BUG_ON (as->argc < n);
542 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
543 struct dm_target *ti)
546 struct path_selector_type *pst;
549 static struct param _params[] = {
550 {0, 1024, "invalid number of path selector args"},
553 pst = dm_get_path_selector(shift(as));
555 ti->error = "unknown path selector type";
559 r = read_param(_params, shift(as), &ps_argc, &ti->error);
561 dm_put_path_selector(pst);
565 r = pst->create(&pg->ps, ps_argc, as->argv);
567 dm_put_path_selector(pst);
568 ti->error = "path selector constructor failed";
573 consume(as, ps_argc);
578 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
579 struct dm_target *ti)
584 struct multipath *m = ti->private;
586 /* we need at least a path arg */
588 ti->error = "no device given";
597 r = dm_get_device(ti, path, ti->begin, ti->len,
598 dm_table_get_mode(ti->table), &p->path.dev);
600 unsigned major, minor;
602 /* Try to add a failed device */
603 if (sscanf(path, "%u:%u", &major, &minor) == 2) {
604 /* Extract the major/minor numbers */
605 p->path.pdev = MKDEV(major, minor);
606 if (MAJOR(p->path.pdev) != major ||
607 MINOR(p->path.pdev) != minor) {
608 /* Nice try, didn't work */
609 DMWARN("Invalid device path %s", path);
610 ti->error = "error converting devnum";
613 DMWARN("adding disabled device %d:%d", major, minor);
617 ti->error = "error getting device";
622 if (m->hw_handler_name && p->path.dev) {
623 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
625 r = scsi_dh_attach(q, m->hw_handler_name);
628 * Already attached to different hw_handler,
629 * try to reattach with correct one.
632 r = scsi_dh_attach(q, m->hw_handler_name);
635 ti->error = "error attaching hardware handler";
636 dm_put_device(ti, p->path.dev);
641 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
643 dm_put_device(ti, p->path.dev);
654 static struct priority_group *parse_priority_group(struct arg_set *as,
657 static struct param _params[] = {
658 {1, 1024, "invalid number of paths"},
659 {0, 1024, "invalid number of selector args"}
663 unsigned i, nr_selector_args, nr_params;
664 struct priority_group *pg;
665 struct dm_target *ti = m->ti;
669 ti->error = "not enough priority group aruments";
673 pg = alloc_priority_group();
675 ti->error = "couldn't allocate priority group";
680 r = parse_path_selector(as, pg, ti);
687 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
691 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
695 nr_params = 1 + nr_selector_args;
696 for (i = 0; i < pg->nr_pgpaths; i++) {
697 struct pgpath *pgpath;
698 struct arg_set path_args;
700 if (as->argc < nr_params) {
701 ti->error = "not enough path parameters";
705 path_args.argc = nr_params;
706 path_args.argv = as->argv;
708 pgpath = parse_path(&path_args, &pg->ps, ti);
713 list_add_tail(&pgpath->list, &pg->pgpaths);
714 consume(as, nr_params);
720 free_priority_group(pg, ti);
724 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
727 struct dm_target *ti = m->ti;
729 static struct param _params[] = {
730 {0, 1024, "invalid number of hardware handler args"},
733 if (read_param(_params, shift(as), &hw_argc, &ti->error))
739 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
740 request_module("scsi_dh_%s", m->hw_handler_name);
741 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
742 ti->error = "unknown hardware handler type";
743 kfree(m->hw_handler_name);
744 m->hw_handler_name = NULL;
747 consume(as, hw_argc - 1);
752 static int parse_features(struct arg_set *as, struct multipath *m)
756 struct dm_target *ti = m->ti;
757 const char *param_name;
759 static struct param _params[] = {
760 {0, 3, "invalid number of feature args"},
761 {1, 50, "pg_init_retries must be between 1 and 50"},
764 r = read_param(_params, shift(as), &argc, &ti->error);
772 param_name = shift(as);
775 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
776 r = queue_if_no_path(m, 1, 0);
780 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
782 r = read_param(_params + 1, shift(as),
783 &m->pg_init_retries, &ti->error);
788 ti->error = "Unrecognised multipath feature request";
790 } while (argc && !r);
795 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
798 /* target parameters */
799 static struct param _params[] = {
800 {0, 1024, "invalid number of priority groups"},
801 {0, 1024, "invalid initial priority group number"},
807 unsigned pg_count = 0;
808 unsigned next_pg_num;
813 m = alloc_multipath(ti);
815 ti->error = "can't allocate multipath";
819 r = parse_features(&as, m);
823 r = parse_hw_handler(&as, m);
827 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
831 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
835 /* parse the priority groups */
837 struct priority_group *pg;
839 pg = parse_priority_group(&as, m);
845 m->nr_valid_paths += pg->nr_pgpaths;
846 list_add_tail(&pg->list, &m->priority_groups);
848 pg->pg_num = pg_count;
853 if (pg_count != m->nr_priority_groups) {
854 ti->error = "priority group count mismatch";
866 static void multipath_dtr(struct dm_target *ti)
868 struct multipath *m = (struct multipath *) ti->private;
870 flush_workqueue(kmpath_handlerd);
871 flush_workqueue(kmultipathd);
876 * Map cloned requests
878 static int multipath_map(struct dm_target *ti, struct request *clone,
879 union map_info *map_context)
882 struct dm_mpath_io *mpio;
883 struct multipath *m = (struct multipath *) ti->private;
885 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
887 /* ENOMEM, requeue */
888 return DM_MAPIO_REQUEUE;
889 memset(mpio, 0, sizeof(*mpio));
891 map_context->ptr = mpio;
892 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
893 r = map_io(m, clone, mpio, 0);
894 if (r < 0 || r == DM_MAPIO_REQUEUE)
895 mempool_free(mpio, m->mpio_pool);
901 * Take a path out of use.
903 static int fail_path(struct pgpath *pgpath)
906 struct multipath *m = pgpath->pg->m;
908 spin_lock_irqsave(&m->lock, flags);
910 if (!pgpath->is_active)
913 DMWARN("Failing path %s.", pgpath->path.dev->name);
915 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
916 pgpath->is_active = 0;
917 pgpath->fail_count++;
921 if (pgpath == m->current_pgpath)
922 m->current_pgpath = NULL;
924 if (pgpath->path.dev)
925 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
926 pgpath->path.dev->name, m->nr_valid_paths);
928 queue_work(kmultipathd, &m->trigger_event);
929 queue_work(kmultipathd, &pgpath->deactivate_path);
932 spin_unlock_irqrestore(&m->lock, flags);
938 * Reinstate a previously-failed path
940 static int reinstate_path(struct pgpath *pgpath)
944 struct multipath *m = pgpath->pg->m;
946 spin_lock_irqsave(&m->lock, flags);
948 if (pgpath->is_active)
951 if (!pgpath->path.dev) {
952 DMWARN("Cannot reinstate disabled path %d:%d",
953 MAJOR(pgpath->path.pdev), MINOR(pgpath->path.pdev));
958 if (!pgpath->pg->ps.type->reinstate_path) {
959 DMWARN("Reinstate path not supported by path selector %s",
960 pgpath->pg->ps.type->name);
965 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
969 pgpath->is_active = 1;
971 m->current_pgpath = NULL;
972 if (!m->nr_valid_paths++ && m->queue_size)
973 queue_work(kmultipathd, &m->process_queued_ios);
975 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
976 pgpath->path.dev->name, m->nr_valid_paths);
978 queue_work(kmultipathd, &m->trigger_event);
981 spin_unlock_irqrestore(&m->lock, flags);
987 * Fail or reinstate all paths that match the provided struct dm_dev.
989 static int action_dev(struct multipath *m, struct dm_dev *dev,
993 struct pgpath *pgpath;
994 struct priority_group *pg;
999 list_for_each_entry(pg, &m->priority_groups, list) {
1000 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1001 if (pgpath->path.dev == dev)
1010 * Temporarily try to avoid having to use the specified PG
1012 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1015 unsigned long flags;
1017 spin_lock_irqsave(&m->lock, flags);
1019 pg->bypassed = bypassed;
1020 m->current_pgpath = NULL;
1021 m->current_pg = NULL;
1023 spin_unlock_irqrestore(&m->lock, flags);
1025 queue_work(kmultipathd, &m->trigger_event);
1029 * Switch to using the specified PG from the next I/O that gets mapped
1031 static int switch_pg_num(struct multipath *m, const char *pgstr)
1033 struct priority_group *pg;
1035 unsigned long flags;
1037 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1038 (pgnum > m->nr_priority_groups)) {
1039 DMWARN("invalid PG number supplied to switch_pg_num");
1043 spin_lock_irqsave(&m->lock, flags);
1044 list_for_each_entry(pg, &m->priority_groups, list) {
1049 m->current_pgpath = NULL;
1050 m->current_pg = NULL;
1053 spin_unlock_irqrestore(&m->lock, flags);
1055 queue_work(kmultipathd, &m->trigger_event);
1060 * Set/clear bypassed status of a PG.
1061 * PGs are numbered upwards from 1 in the order they were declared.
1063 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1065 struct priority_group *pg;
1068 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1069 (pgnum > m->nr_priority_groups)) {
1070 DMWARN("invalid PG number supplied to bypass_pg");
1074 list_for_each_entry(pg, &m->priority_groups, list) {
1079 bypass_pg(m, pg, bypassed);
1084 * Should we retry pg_init immediately?
1086 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1088 unsigned long flags;
1089 int limit_reached = 0;
1091 spin_lock_irqsave(&m->lock, flags);
1093 if (m->pg_init_count <= m->pg_init_retries)
1094 m->pg_init_required = 1;
1098 spin_unlock_irqrestore(&m->lock, flags);
1100 return limit_reached;
1103 static void pg_init_done(struct dm_path *path, int errors)
1105 struct pgpath *pgpath = path_to_pgpath(path);
1106 struct priority_group *pg = pgpath->pg;
1107 struct multipath *m = pg->m;
1108 unsigned long flags;
1110 /* device or driver problems */
1115 if (!m->hw_handler_name) {
1119 DMERR("Cannot failover device because scsi_dh_%s was not "
1120 "loaded.", m->hw_handler_name);
1122 * Fail path for now, so we do not ping pong
1126 case SCSI_DH_DEV_TEMP_BUSY:
1128 * Probably doing something like FW upgrade on the
1129 * controller so try the other pg.
1131 bypass_pg(m, pg, 1);
1133 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1135 case SCSI_DH_IMM_RETRY:
1136 case SCSI_DH_RES_TEMP_UNAVAIL:
1137 if (pg_init_limit_reached(m, pgpath))
1143 * We probably do not want to fail the path for a device
1144 * error, but this is what the old dm did. In future
1145 * patches we can do more advanced handling.
1150 spin_lock_irqsave(&m->lock, flags);
1152 DMERR("Could not failover device. Error %d.", errors);
1153 m->current_pgpath = NULL;
1154 m->current_pg = NULL;
1155 } else if (!m->pg_init_required) {
1160 m->pg_init_in_progress = 0;
1161 queue_work(kmultipathd, &m->process_queued_ios);
1162 spin_unlock_irqrestore(&m->lock, flags);
1165 static void activate_path(struct work_struct *work)
1168 struct multipath *m =
1169 container_of(work, struct multipath, activate_path);
1170 struct dm_path *path;
1171 unsigned long flags;
1173 spin_lock_irqsave(&m->lock, flags);
1174 path = &m->pgpath_to_activate->path;
1175 m->pgpath_to_activate = NULL;
1176 spin_unlock_irqrestore(&m->lock, flags);
1179 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1180 pg_init_done(path, ret);
1186 static int do_end_io(struct multipath *m, struct request *clone,
1187 int error, struct dm_mpath_io *mpio)
1190 * We don't queue any clone request inside the multipath target
1191 * during end I/O handling, since those clone requests don't have
1192 * bio clones. If we queue them inside the multipath target,
1193 * we need to make bio clones, that requires memory allocation.
1194 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1195 * don't have bio clones.)
1196 * Instead of queueing the clone request here, we queue the original
1197 * request into dm core, which will remake a clone request and
1198 * clone bios for it and resubmit it later.
1200 int r = DM_ENDIO_REQUEUE;
1201 unsigned long flags;
1203 if (!error && !clone->errors)
1204 return 0; /* I/O complete */
1206 if (error == -EOPNOTSUPP)
1210 fail_path(mpio->pgpath);
1212 spin_lock_irqsave(&m->lock, flags);
1213 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1215 spin_unlock_irqrestore(&m->lock, flags);
1220 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1221 int error, union map_info *map_context)
1223 struct multipath *m = ti->private;
1224 struct dm_mpath_io *mpio = map_context->ptr;
1225 struct pgpath *pgpath = mpio->pgpath;
1226 struct path_selector *ps;
1229 r = do_end_io(m, clone, error, mpio);
1231 ps = &pgpath->pg->ps;
1232 if (ps->type->end_io)
1233 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1235 mempool_free(mpio, m->mpio_pool);
1241 * Suspend can't complete until all the I/O is processed so if
1242 * the last path fails we must error any remaining I/O.
1243 * Note that if the freeze_bdev fails while suspending, the
1244 * queue_if_no_path state is lost - userspace should reset it.
1246 static void multipath_presuspend(struct dm_target *ti)
1248 struct multipath *m = (struct multipath *) ti->private;
1250 queue_if_no_path(m, 0, 1);
1254 * Restore the queue_if_no_path setting.
1256 static void multipath_resume(struct dm_target *ti)
1258 struct multipath *m = (struct multipath *) ti->private;
1259 unsigned long flags;
1261 spin_lock_irqsave(&m->lock, flags);
1262 m->queue_if_no_path = m->saved_queue_if_no_path;
1263 spin_unlock_irqrestore(&m->lock, flags);
1267 * Info output has the following format:
1268 * num_multipath_feature_args [multipath_feature_args]*
1269 * num_handler_status_args [handler_status_args]*
1270 * num_groups init_group_number
1271 * [A|D|E num_ps_status_args [ps_status_args]*
1272 * num_paths num_selector_args
1273 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1275 * Table output has the following format (identical to the constructor string):
1276 * num_feature_args [features_args]*
1277 * num_handler_args hw_handler [hw_handler_args]*
1278 * num_groups init_group_number
1279 * [priority selector-name num_ps_args [ps_args]*
1280 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1282 static int multipath_status(struct dm_target *ti, status_type_t type,
1283 char *result, unsigned int maxlen)
1286 unsigned long flags;
1287 struct multipath *m = (struct multipath *) ti->private;
1288 struct priority_group *pg;
1293 spin_lock_irqsave(&m->lock, flags);
1296 if (type == STATUSTYPE_INFO)
1297 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1299 DMEMIT("%u ", m->queue_if_no_path +
1300 (m->pg_init_retries > 0) * 2);
1301 if (m->queue_if_no_path)
1302 DMEMIT("queue_if_no_path ");
1303 if (m->pg_init_retries)
1304 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1307 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1310 DMEMIT("1 %s ", m->hw_handler_name);
1312 DMEMIT("%u ", m->nr_priority_groups);
1315 pg_num = m->next_pg->pg_num;
1316 else if (m->current_pg)
1317 pg_num = m->current_pg->pg_num;
1321 DMEMIT("%u ", pg_num);
1324 case STATUSTYPE_INFO:
1325 list_for_each_entry(pg, &m->priority_groups, list) {
1327 state = 'D'; /* Disabled */
1328 else if (pg == m->current_pg)
1329 state = 'A'; /* Currently Active */
1331 state = 'E'; /* Enabled */
1333 DMEMIT("%c ", state);
1335 if (pg->ps.type->status)
1336 sz += pg->ps.type->status(&pg->ps, NULL, type,
1342 DMEMIT("%u %u ", pg->nr_pgpaths,
1343 pg->ps.type->info_args);
1345 list_for_each_entry(p, &pg->pgpaths, list) {
1347 DMEMIT("%s %s %u ", p->path.dev->name,
1348 p->is_active ? "A" : "F",
1351 DMEMIT("%d:%d F %u ",
1352 MAJOR(p->path.pdev),
1353 MINOR(p->path.pdev),
1356 if (pg->ps.type->status)
1357 sz += pg->ps.type->status(&pg->ps,
1358 &p->path, type, result + sz,
1364 case STATUSTYPE_TABLE:
1365 list_for_each_entry(pg, &m->priority_groups, list) {
1366 DMEMIT("%s ", pg->ps.type->name);
1368 if (pg->ps.type->status)
1369 sz += pg->ps.type->status(&pg->ps, NULL, type,
1375 DMEMIT("%u %u ", pg->nr_pgpaths,
1376 pg->ps.type->table_args);
1378 list_for_each_entry(p, &pg->pgpaths, list) {
1380 DMEMIT("%s ", p->path.dev->name);
1383 MAJOR(p->path.pdev),
1384 MINOR(p->path.pdev));
1385 if (pg->ps.type->status)
1386 sz += pg->ps.type->status(&pg->ps,
1387 &p->path, type, result + sz,
1394 spin_unlock_irqrestore(&m->lock, flags);
1399 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1403 struct multipath *m = (struct multipath *) ti->private;
1407 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1408 return queue_if_no_path(m, 1, 0);
1409 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1410 return queue_if_no_path(m, 0, 0);
1416 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1417 return bypass_pg_num(m, argv[1], 1);
1418 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1419 return bypass_pg_num(m, argv[1], 0);
1420 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1421 return switch_pg_num(m, argv[1]);
1422 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1423 action = reinstate_path;
1424 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1429 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1430 dm_table_get_mode(ti->table), &dev);
1432 DMWARN("message: error getting device %s",
1437 r = action_dev(m, dev, action);
1439 dm_put_device(ti, dev);
1444 DMWARN("Unrecognised multipath message received.");
1448 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1449 struct file *filp, unsigned int cmd,
1452 struct multipath *m = (struct multipath *) ti->private;
1453 struct block_device *bdev = NULL;
1454 unsigned long flags;
1455 struct file fake_file = {};
1456 struct dentry fake_dentry = {};
1459 fake_file.f_path.dentry = &fake_dentry;
1461 spin_lock_irqsave(&m->lock, flags);
1463 if (!m->current_pgpath)
1464 __choose_pgpath(m, 1 << 19); /* Assume 512KB */
1466 if (m->current_pgpath && m->current_pgpath->path.dev) {
1467 bdev = m->current_pgpath->path.dev->bdev;
1468 fake_dentry.d_inode = bdev->bd_inode;
1469 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1477 spin_unlock_irqrestore(&m->lock, flags);
1479 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1480 bdev->bd_disk, cmd, arg);
1483 static int __pgpath_busy(struct pgpath *pgpath)
1485 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1487 return dm_underlying_device_busy(q);
1491 * We return "busy", only when we can map I/Os but underlying devices
1492 * are busy (so even if we map I/Os now, the I/Os will wait on
1493 * the underlying queue).
1494 * In other words, if we want to kill I/Os or queue them inside us
1495 * due to map unavailability, we don't return "busy". Otherwise,
1496 * dm core won't give us the I/Os and we can't do what we want.
1498 static int multipath_busy(struct dm_target *ti)
1500 int busy = 0, has_active = 0;
1501 struct multipath *m = (struct multipath *) ti->private;
1502 struct priority_group *pg;
1503 struct pgpath *pgpath;
1504 unsigned long flags;
1506 spin_lock_irqsave(&m->lock, flags);
1508 /* Guess which priority_group will be used at next mapping time */
1509 if (unlikely(!m->current_pgpath && m->next_pg))
1511 else if (likely(m->current_pg))
1515 * We don't know which pg will be used at next mapping time.
1516 * We don't call __choose_pgpath() here to avoid to trigger
1517 * pg_init just by busy checking.
1518 * So we don't know whether underlying devices we will be using
1519 * at next mapping time are busy or not. Just try mapping.
1524 * If there is one non-busy active path at least, the path selector
1525 * will be able to select it. So we consider such a pg as not busy.
1528 list_for_each_entry(pgpath, &pg->pgpaths, list)
1529 if (pgpath->is_active) {
1532 if (!__pgpath_busy(pgpath)) {
1540 * No active path in this pg, so this pg won't be used and
1541 * the current_pg will be changed at next mapping time.
1542 * We need to try mapping to determine it.
1547 spin_unlock_irqrestore(&m->lock, flags);
1552 /*-----------------------------------------------------------------
1554 *---------------------------------------------------------------*/
1555 static struct target_type multipath_target = {
1556 .name = "multipath",
1557 .version = {1, 0, 5},
1558 .module = THIS_MODULE,
1559 .ctr = multipath_ctr,
1560 .dtr = multipath_dtr,
1561 .map_rq = multipath_map,
1562 .rq_end_io = multipath_end_io,
1563 .presuspend = multipath_presuspend,
1564 .resume = multipath_resume,
1565 .status = multipath_status,
1566 .message = multipath_message,
1567 .ioctl = multipath_ioctl,
1568 .busy = multipath_busy,
1571 static int __init dm_multipath_init(void)
1575 /* allocate a slab for the dm_ios */
1576 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1580 r = dm_register_target(&multipath_target);
1582 DMERR("register failed %d", r);
1583 kmem_cache_destroy(_mpio_cache);
1587 kmultipathd = create_workqueue("kmpathd");
1589 DMERR("failed to create workqueue kmpathd");
1590 dm_unregister_target(&multipath_target);
1591 kmem_cache_destroy(_mpio_cache);
1596 * A separate workqueue is used to handle the device handlers
1597 * to avoid overloading existing workqueue. Overloading the
1598 * old workqueue would also create a bottleneck in the
1599 * path of the storage hardware device activation.
1601 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1602 if (!kmpath_handlerd) {
1603 DMERR("failed to create workqueue kmpath_handlerd");
1604 destroy_workqueue(kmultipathd);
1605 dm_unregister_target(&multipath_target);
1606 kmem_cache_destroy(_mpio_cache);
1610 DMINFO("version %u.%u.%u loaded",
1611 multipath_target.version[0], multipath_target.version[1],
1612 multipath_target.version[2]);
1617 static void __exit dm_multipath_exit(void)
1621 destroy_workqueue(kmpath_handlerd);
1622 destroy_workqueue(kmultipathd);
1624 r = dm_unregister_target(&multipath_target);
1626 DMERR("target unregister failed %d", r);
1627 kmem_cache_destroy(_mpio_cache);
1630 module_init(dm_multipath_init);
1631 module_exit(dm_multipath_exit);
1633 MODULE_DESCRIPTION(DM_NAME " multipath target");
1634 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1635 MODULE_LICENSE("GPL");