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;
36 struct work_struct activate_path;
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 unsigned nr_priority_groups;
67 struct list_head priority_groups;
68 unsigned pg_init_required; /* pg_init needs calling? */
69 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
71 unsigned nr_valid_paths; /* Total number of usable paths */
72 struct pgpath *current_pgpath;
73 struct priority_group *current_pg;
74 struct priority_group *next_pg; /* Switch to this PG if set */
75 unsigned repeat_count; /* I/Os left before calling PS again */
77 unsigned queue_io; /* Must we queue all I/O? */
78 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
79 unsigned saved_queue_if_no_path;/* Saved state during suspension */
80 unsigned pg_init_retries; /* Number of times to retry pg_init */
81 unsigned pg_init_count; /* Number of times pg_init called */
83 struct work_struct process_queued_ios;
84 struct list_head queued_ios;
87 struct work_struct trigger_event;
90 * We must use a mempool of dm_mpath_io structs so that we
91 * can resubmit bios on error.
97 * Context information attached to each bio we process.
100 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);
128 INIT_WORK(&pgpath->activate_path, activate_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)
162 struct pgpath *pgpath, *tmp;
164 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
165 list_del(&pgpath->list);
166 dm_put_device(ti, pgpath->path.dev);
171 static void free_priority_group(struct priority_group *pg,
172 struct dm_target *ti)
174 struct path_selector *ps = &pg->ps;
177 ps->type->destroy(ps);
178 dm_put_path_selector(ps->type);
181 free_pgpaths(&pg->pgpaths, ti);
185 static struct multipath *alloc_multipath(struct dm_target *ti)
189 m = kzalloc(sizeof(*m), GFP_KERNEL);
191 INIT_LIST_HEAD(&m->priority_groups);
192 INIT_LIST_HEAD(&m->queued_ios);
193 spin_lock_init(&m->lock);
195 INIT_WORK(&m->process_queued_ios, process_queued_ios);
196 INIT_WORK(&m->trigger_event, trigger_event);
197 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
209 static void free_multipath(struct multipath *m)
211 struct priority_group *pg, *tmp;
213 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
215 free_priority_group(pg, m->ti);
218 kfree(m->hw_handler_name);
219 mempool_destroy(m->mpio_pool);
224 /*-----------------------------------------------
226 *-----------------------------------------------*/
228 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
230 m->current_pg = pgpath->pg;
232 /* Must we initialise the PG first, and queue I/O till it's ready? */
233 if (m->hw_handler_name) {
234 m->pg_init_required = 1;
237 m->pg_init_required = 0;
241 m->pg_init_count = 0;
244 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
247 struct dm_path *path;
249 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
253 m->current_pgpath = path_to_pgpath(path);
255 if (!m->current_pgpath->path.dev) {
256 m->current_pgpath = NULL;
260 if (m->current_pg != pg)
261 __switch_pg(m, m->current_pgpath);
266 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
268 struct priority_group *pg;
269 unsigned bypassed = 1;
271 if (!m->nr_valid_paths)
274 /* Were we instructed to switch PG? */
278 if (!__choose_path_in_pg(m, pg, nr_bytes))
282 /* Don't change PG until it has no remaining paths */
283 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
287 * Loop through priority groups until we find a valid path.
288 * First time we skip PGs marked 'bypassed'.
289 * Second time we only try the ones we skipped.
292 list_for_each_entry(pg, &m->priority_groups, list) {
293 if (pg->bypassed == bypassed)
295 if (!__choose_path_in_pg(m, pg, nr_bytes))
298 } while (bypassed--);
301 m->current_pgpath = NULL;
302 m->current_pg = NULL;
306 * Check whether bios must be queued in the device-mapper core rather
307 * than here in the target.
309 * m->lock must be held on entry.
311 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
312 * same value then we are not between multipath_presuspend()
313 * and multipath_resume() calls and we have no need to check
314 * for the DMF_NOFLUSH_SUSPENDING flag.
316 static int __must_push_back(struct multipath *m)
318 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
319 dm_noflush_suspending(m->ti));
322 static int map_io(struct multipath *m, struct request *clone,
323 struct dm_mpath_io *mpio, unsigned was_queued)
325 int r = DM_MAPIO_REMAPPED;
326 size_t nr_bytes = blk_rq_bytes(clone);
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)))
336 __choose_pgpath(m, nr_bytes);
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;
363 mpio->nr_bytes = nr_bytes;
365 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
366 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
369 spin_unlock_irqrestore(&m->lock, flags);
375 * If we run out of usable paths, should we queue I/O or error it?
377 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
378 unsigned save_old_value)
382 spin_lock_irqsave(&m->lock, flags);
385 m->saved_queue_if_no_path = m->queue_if_no_path;
387 m->saved_queue_if_no_path = queue_if_no_path;
388 m->queue_if_no_path = queue_if_no_path;
389 if (!m->queue_if_no_path && m->queue_size)
390 queue_work(kmultipathd, &m->process_queued_ios);
392 spin_unlock_irqrestore(&m->lock, flags);
397 /*-----------------------------------------------------------------
398 * The multipath daemon is responsible for resubmitting queued ios.
399 *---------------------------------------------------------------*/
401 static void dispatch_queued_ios(struct multipath *m)
405 struct dm_mpath_io *mpio;
406 union map_info *info;
407 struct request *clone, *n;
410 spin_lock_irqsave(&m->lock, flags);
411 list_splice_init(&m->queued_ios, &cl);
412 spin_unlock_irqrestore(&m->lock, flags);
414 list_for_each_entry_safe(clone, n, &cl, queuelist) {
415 list_del_init(&clone->queuelist);
417 info = dm_get_rq_mapinfo(clone);
420 r = map_io(m, clone, mpio, 1);
422 mempool_free(mpio, m->mpio_pool);
423 dm_kill_request(clone, r);
424 } else if (r == DM_MAPIO_REMAPPED)
425 dm_dispatch_request(clone);
426 else if (r == DM_MAPIO_REQUEUE) {
427 mempool_free(mpio, m->mpio_pool);
428 dm_requeue_request(clone);
433 static void process_queued_ios(struct work_struct *work)
435 struct multipath *m =
436 container_of(work, struct multipath, process_queued_ios);
437 struct pgpath *pgpath = NULL, *tmp;
438 unsigned must_queue = 1;
441 spin_lock_irqsave(&m->lock, flags);
446 if (!m->current_pgpath)
447 __choose_pgpath(m, 1 << 19); /* Assume 512 KB */
449 pgpath = m->current_pgpath;
452 struct block_device *bdev = pgpath->path.dev->bdev;
453 if (unlikely(blk_queue_stopped(bdev_get_queue(bdev))))
457 if ((pgpath && !m->queue_io) ||
458 (!pgpath && !m->queue_if_no_path))
461 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
463 m->pg_init_required = 0;
464 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
465 queue_work(kmpath_handlerd, &tmp->activate_path);
466 m->pg_init_in_progress++;
470 spin_unlock_irqrestore(&m->lock, flags);
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 (r == -ENXIO && sscanf(path, "%u:%u", &major, &minor) == 2) {
606 /* Extract the major/minor numbers */
607 dev = MKDEV(major, minor);
608 if (MAJOR(dev) != major || MINOR(dev) != minor) {
609 /* Nice try, didn't work */
610 DMWARN("Invalid device path %s", path);
611 ti->error = "error converting devnum";
614 DMWARN("adding disabled device %d:%d", major, minor);
616 format_dev_t(p->path.pdev, dev);
619 ti->error = "error getting device";
623 memcpy(p->path.pdev, p->path.dev->name, 16);
627 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
629 if (m->hw_handler_name) {
630 r = scsi_dh_attach(q, m->hw_handler_name);
633 * Already attached to different hw_handler,
634 * try to reattach with correct one.
637 r = scsi_dh_attach(q, m->hw_handler_name);
640 ti->error = "error attaching hardware handler";
641 dm_put_device(ti, p->path.dev);
645 /* Play safe and detach hardware handler */
650 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
652 dm_put_device(ti, p->path.dev);
657 ps->type->fail_path(ps, &p->path);
668 static struct priority_group *parse_priority_group(struct arg_set *as,
671 static struct param _params[] = {
672 {1, 1024, "invalid number of paths"},
673 {0, 1024, "invalid number of selector args"}
677 unsigned i, nr_selector_args, nr_params;
678 struct priority_group *pg;
679 struct dm_target *ti = m->ti;
683 ti->error = "not enough priority group aruments";
687 pg = alloc_priority_group();
689 ti->error = "couldn't allocate priority group";
694 r = parse_path_selector(as, pg, ti);
701 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
705 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
709 nr_params = 1 + nr_selector_args;
710 for (i = 0; i < pg->nr_pgpaths; i++) {
711 struct pgpath *pgpath;
712 struct arg_set path_args;
714 if (as->argc < nr_params) {
715 ti->error = "not enough path parameters";
719 path_args.argc = nr_params;
720 path_args.argv = as->argv;
722 pgpath = parse_path(&path_args, &pg->ps, ti);
727 list_add_tail(&pgpath->list, &pg->pgpaths);
728 consume(as, nr_params);
734 free_priority_group(pg, ti);
738 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
741 struct dm_target *ti = m->ti;
743 static struct param _params[] = {
744 {0, 1024, "invalid number of hardware handler args"},
747 if (read_param(_params, shift(as), &hw_argc, &ti->error))
753 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
754 request_module("scsi_dh_%s", m->hw_handler_name);
755 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
756 ti->error = "unknown hardware handler type";
757 kfree(m->hw_handler_name);
758 m->hw_handler_name = NULL;
761 consume(as, hw_argc - 1);
766 static int parse_features(struct arg_set *as, struct multipath *m)
770 struct dm_target *ti = m->ti;
771 const char *param_name;
773 static struct param _params[] = {
774 {0, 3, "invalid number of feature args"},
775 {1, 50, "pg_init_retries must be between 1 and 50"},
778 r = read_param(_params, shift(as), &argc, &ti->error);
786 param_name = shift(as);
789 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
790 r = queue_if_no_path(m, 1, 0);
794 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
796 r = read_param(_params + 1, shift(as),
797 &m->pg_init_retries, &ti->error);
802 ti->error = "Unrecognised multipath feature request";
804 } while (argc && !r);
809 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
812 /* target parameters */
813 static struct param _params[] = {
814 {0, 1024, "invalid number of priority groups"},
815 {0, 1024, "invalid initial priority group number"},
821 unsigned pg_count = 0;
822 unsigned next_pg_num;
827 m = alloc_multipath(ti);
829 ti->error = "can't allocate multipath";
833 r = parse_features(&as, m);
837 r = parse_hw_handler(&as, m);
841 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
845 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
849 /* parse the priority groups */
851 struct priority_group *pg;
853 pg = parse_priority_group(&as, m);
859 m->nr_valid_paths += pg->nr_pgpaths;
860 list_add_tail(&pg->list, &m->priority_groups);
862 pg->pg_num = pg_count;
867 if (pg_count != m->nr_priority_groups) {
868 ti->error = "priority group count mismatch";
880 static void multipath_dtr(struct dm_target *ti)
882 struct multipath *m = (struct multipath *) ti->private;
884 flush_workqueue(kmpath_handlerd);
885 flush_workqueue(kmultipathd);
890 * Map cloned requests
892 static int multipath_map(struct dm_target *ti, struct request *clone,
893 union map_info *map_context)
896 struct dm_mpath_io *mpio;
897 struct multipath *m = (struct multipath *) ti->private;
899 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
901 /* ENOMEM, requeue */
902 return DM_MAPIO_REQUEUE;
903 memset(mpio, 0, sizeof(*mpio));
905 map_context->ptr = mpio;
906 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
907 r = map_io(m, clone, mpio, 0);
908 if (r < 0 || r == DM_MAPIO_REQUEUE)
909 mempool_free(mpio, m->mpio_pool);
915 * Take a path out of use.
917 static int fail_path(struct pgpath *pgpath)
920 struct multipath *m = pgpath->pg->m;
922 spin_lock_irqsave(&m->lock, flags);
924 if (!pgpath->is_active)
927 DMWARN("Failing path %s.", pgpath->path.pdev);
929 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
930 pgpath->is_active = 0;
931 pgpath->fail_count++;
935 if (pgpath == m->current_pgpath)
936 m->current_pgpath = NULL;
938 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
939 pgpath->path.pdev, m->nr_valid_paths);
941 queue_work(kmultipathd, &m->trigger_event);
942 queue_work(kmultipathd, &pgpath->deactivate_path);
945 spin_unlock_irqrestore(&m->lock, flags);
951 * Reinstate a previously-failed path
953 static int reinstate_path(struct pgpath *pgpath)
957 struct multipath *m = pgpath->pg->m;
959 spin_lock_irqsave(&m->lock, flags);
961 if (pgpath->is_active)
964 if (!pgpath->path.dev) {
965 DMWARN("Cannot reinstate disabled path %s", pgpath->path.pdev);
970 if (!pgpath->pg->ps.type->reinstate_path) {
971 DMWARN("Reinstate path not supported by path selector %s",
972 pgpath->pg->ps.type->name);
977 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
981 pgpath->is_active = 1;
983 m->current_pgpath = NULL;
984 if (!m->nr_valid_paths++ && m->queue_size)
985 queue_work(kmultipathd, &m->process_queued_ios);
987 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
988 pgpath->path.pdev, m->nr_valid_paths);
990 queue_work(kmultipathd, &m->trigger_event);
993 spin_unlock_irqrestore(&m->lock, flags);
999 * Fail or reinstate all paths that match the provided struct dm_dev.
1001 static int action_dev(struct multipath *m, struct dm_dev *dev,
1005 struct pgpath *pgpath;
1006 struct priority_group *pg;
1011 list_for_each_entry(pg, &m->priority_groups, list) {
1012 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1013 if (pgpath->path.dev == dev)
1022 * Temporarily try to avoid having to use the specified PG
1024 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1027 unsigned long flags;
1029 spin_lock_irqsave(&m->lock, flags);
1031 pg->bypassed = bypassed;
1032 m->current_pgpath = NULL;
1033 m->current_pg = NULL;
1035 spin_unlock_irqrestore(&m->lock, flags);
1037 queue_work(kmultipathd, &m->trigger_event);
1041 * Switch to using the specified PG from the next I/O that gets mapped
1043 static int switch_pg_num(struct multipath *m, const char *pgstr)
1045 struct priority_group *pg;
1047 unsigned long flags;
1049 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1050 (pgnum > m->nr_priority_groups)) {
1051 DMWARN("invalid PG number supplied to switch_pg_num");
1055 spin_lock_irqsave(&m->lock, flags);
1056 list_for_each_entry(pg, &m->priority_groups, list) {
1061 m->current_pgpath = NULL;
1062 m->current_pg = NULL;
1065 spin_unlock_irqrestore(&m->lock, flags);
1067 queue_work(kmultipathd, &m->trigger_event);
1072 * Set/clear bypassed status of a PG.
1073 * PGs are numbered upwards from 1 in the order they were declared.
1075 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1077 struct priority_group *pg;
1080 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1081 (pgnum > m->nr_priority_groups)) {
1082 DMWARN("invalid PG number supplied to bypass_pg");
1086 list_for_each_entry(pg, &m->priority_groups, list) {
1091 bypass_pg(m, pg, bypassed);
1096 * Should we retry pg_init immediately?
1098 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1100 unsigned long flags;
1101 int limit_reached = 0;
1103 spin_lock_irqsave(&m->lock, flags);
1105 if (m->pg_init_count <= m->pg_init_retries)
1106 m->pg_init_required = 1;
1110 spin_unlock_irqrestore(&m->lock, flags);
1112 return limit_reached;
1115 static void pg_init_done(struct dm_path *path, int errors)
1117 struct pgpath *pgpath = path_to_pgpath(path);
1118 struct priority_group *pg = pgpath->pg;
1119 struct multipath *m = pg->m;
1120 unsigned long flags;
1122 /* device or driver problems */
1127 if (!m->hw_handler_name) {
1131 DMERR("Cannot failover device because scsi_dh_%s was not "
1132 "loaded.", m->hw_handler_name);
1134 * Fail path for now, so we do not ping pong
1138 case SCSI_DH_DEV_TEMP_BUSY:
1140 * Probably doing something like FW upgrade on the
1141 * controller so try the other pg.
1143 bypass_pg(m, pg, 1);
1145 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1147 case SCSI_DH_IMM_RETRY:
1148 case SCSI_DH_RES_TEMP_UNAVAIL:
1149 if (pg_init_limit_reached(m, pgpath))
1155 * We probably do not want to fail the path for a device
1156 * error, but this is what the old dm did. In future
1157 * patches we can do more advanced handling.
1162 spin_lock_irqsave(&m->lock, flags);
1164 DMERR("Could not failover device. Error %d.", errors);
1165 m->current_pgpath = NULL;
1166 m->current_pg = NULL;
1167 } else if (!m->pg_init_required) {
1172 m->pg_init_in_progress--;
1173 if (!m->pg_init_in_progress)
1174 queue_work(kmultipathd, &m->process_queued_ios);
1175 spin_unlock_irqrestore(&m->lock, flags);
1178 static void activate_path(struct work_struct *work)
1180 int ret = SCSI_DH_DEV_OFFLINED;
1181 struct pgpath *pgpath =
1182 container_of(work, struct pgpath, activate_path);
1184 if (pgpath->path.dev)
1185 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1186 pg_init_done(&pgpath->path, ret);
1192 static int do_end_io(struct multipath *m, struct request *clone,
1193 int error, struct dm_mpath_io *mpio)
1196 * We don't queue any clone request inside the multipath target
1197 * during end I/O handling, since those clone requests don't have
1198 * bio clones. If we queue them inside the multipath target,
1199 * we need to make bio clones, that requires memory allocation.
1200 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1201 * don't have bio clones.)
1202 * Instead of queueing the clone request here, we queue the original
1203 * request into dm core, which will remake a clone request and
1204 * clone bios for it and resubmit it later.
1206 int r = DM_ENDIO_REQUEUE;
1207 unsigned long flags;
1209 if (!error && !clone->errors)
1210 return 0; /* I/O complete */
1212 if (error == -EOPNOTSUPP)
1216 fail_path(mpio->pgpath);
1218 spin_lock_irqsave(&m->lock, flags);
1219 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1221 spin_unlock_irqrestore(&m->lock, flags);
1226 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1227 int error, union map_info *map_context)
1229 struct multipath *m = ti->private;
1230 struct dm_mpath_io *mpio = map_context->ptr;
1231 struct pgpath *pgpath = mpio->pgpath;
1232 struct path_selector *ps;
1235 r = do_end_io(m, clone, error, mpio);
1237 ps = &pgpath->pg->ps;
1238 if (ps->type->end_io)
1239 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1241 mempool_free(mpio, m->mpio_pool);
1247 * Suspend can't complete until all the I/O is processed so if
1248 * the last path fails we must error any remaining I/O.
1249 * Note that if the freeze_bdev fails while suspending, the
1250 * queue_if_no_path state is lost - userspace should reset it.
1252 static void multipath_presuspend(struct dm_target *ti)
1254 struct multipath *m = (struct multipath *) ti->private;
1256 queue_if_no_path(m, 0, 1);
1260 * Restore the queue_if_no_path setting.
1262 static void multipath_resume(struct dm_target *ti)
1264 struct multipath *m = (struct multipath *) ti->private;
1265 unsigned long flags;
1267 spin_lock_irqsave(&m->lock, flags);
1268 m->queue_if_no_path = m->saved_queue_if_no_path;
1269 spin_unlock_irqrestore(&m->lock, flags);
1273 * Info output has the following format:
1274 * num_multipath_feature_args [multipath_feature_args]*
1275 * num_handler_status_args [handler_status_args]*
1276 * num_groups init_group_number
1277 * [A|D|E num_ps_status_args [ps_status_args]*
1278 * num_paths num_selector_args
1279 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1281 * Table output has the following format (identical to the constructor string):
1282 * num_feature_args [features_args]*
1283 * num_handler_args hw_handler [hw_handler_args]*
1284 * num_groups init_group_number
1285 * [priority selector-name num_ps_args [ps_args]*
1286 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1288 static int multipath_status(struct dm_target *ti, status_type_t type,
1289 char *result, unsigned int maxlen)
1292 unsigned long flags;
1293 struct multipath *m = (struct multipath *) ti->private;
1294 struct priority_group *pg;
1299 spin_lock_irqsave(&m->lock, flags);
1302 if (type == STATUSTYPE_INFO)
1303 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1305 DMEMIT("%u ", m->queue_if_no_path +
1306 (m->pg_init_retries > 0) * 2);
1307 if (m->queue_if_no_path)
1308 DMEMIT("queue_if_no_path ");
1309 if (m->pg_init_retries)
1310 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1313 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1316 DMEMIT("1 %s ", m->hw_handler_name);
1318 DMEMIT("%u ", m->nr_priority_groups);
1321 pg_num = m->next_pg->pg_num;
1322 else if (m->current_pg)
1323 pg_num = m->current_pg->pg_num;
1327 DMEMIT("%u ", pg_num);
1330 case STATUSTYPE_INFO:
1331 list_for_each_entry(pg, &m->priority_groups, list) {
1333 state = 'D'; /* Disabled */
1334 else if (pg == m->current_pg)
1335 state = 'A'; /* Currently Active */
1337 state = 'E'; /* Enabled */
1339 DMEMIT("%c ", state);
1341 if (pg->ps.type->status)
1342 sz += pg->ps.type->status(&pg->ps, NULL, type,
1348 DMEMIT("%u %u ", pg->nr_pgpaths,
1349 pg->ps.type->info_args);
1351 list_for_each_entry(p, &pg->pgpaths, list) {
1352 DMEMIT("%s %s %u ", p->path.pdev,
1353 p->is_active ? "A" : "F",
1355 if (pg->ps.type->status)
1356 sz += pg->ps.type->status(&pg->ps,
1357 &p->path, type, result + sz,
1363 case STATUSTYPE_TABLE:
1364 list_for_each_entry(pg, &m->priority_groups, list) {
1365 DMEMIT("%s ", pg->ps.type->name);
1367 if (pg->ps.type->status)
1368 sz += pg->ps.type->status(&pg->ps, NULL, type,
1374 DMEMIT("%u %u ", pg->nr_pgpaths,
1375 pg->ps.type->table_args);
1377 list_for_each_entry(p, &pg->pgpaths, list) {
1378 DMEMIT("%s ", p->path.pdev);
1379 if (pg->ps.type->status)
1380 sz += pg->ps.type->status(&pg->ps,
1381 &p->path, type, result + sz,
1388 spin_unlock_irqrestore(&m->lock, flags);
1393 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1397 struct multipath *m = (struct multipath *) ti->private;
1401 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1402 return queue_if_no_path(m, 1, 0);
1403 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1404 return queue_if_no_path(m, 0, 0);
1410 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1411 return bypass_pg_num(m, argv[1], 1);
1412 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1413 return bypass_pg_num(m, argv[1], 0);
1414 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1415 return switch_pg_num(m, argv[1]);
1416 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1417 action = reinstate_path;
1418 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1423 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1424 dm_table_get_mode(ti->table), &dev);
1426 DMWARN("message: error getting device %s",
1431 r = action_dev(m, dev, action);
1433 dm_put_device(ti, dev);
1438 DMWARN("Unrecognised multipath message received.");
1442 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1443 struct file *filp, unsigned int cmd,
1446 struct multipath *m = (struct multipath *) ti->private;
1447 struct block_device *bdev = NULL;
1448 unsigned long flags;
1449 struct file fake_file = {};
1450 struct dentry fake_dentry = {};
1453 fake_file.f_path.dentry = &fake_dentry;
1455 spin_lock_irqsave(&m->lock, flags);
1457 if (!m->current_pgpath)
1458 __choose_pgpath(m, 1 << 19); /* Assume 512KB */
1460 if (m->current_pgpath && m->current_pgpath->path.dev) {
1461 bdev = m->current_pgpath->path.dev->bdev;
1462 fake_dentry.d_inode = bdev->bd_inode;
1463 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1471 spin_unlock_irqrestore(&m->lock, flags);
1473 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1474 bdev->bd_disk, cmd, arg);
1477 static int __pgpath_busy(struct pgpath *pgpath)
1479 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1481 return dm_underlying_device_busy(q);
1485 * We return "busy", only when we can map I/Os but underlying devices
1486 * are busy (so even if we map I/Os now, the I/Os will wait on
1487 * the underlying queue).
1488 * In other words, if we want to kill I/Os or queue them inside us
1489 * due to map unavailability, we don't return "busy". Otherwise,
1490 * dm core won't give us the I/Os and we can't do what we want.
1492 static int multipath_busy(struct dm_target *ti)
1494 int busy = 0, has_active = 0;
1495 struct multipath *m = (struct multipath *) ti->private;
1496 struct priority_group *pg;
1497 struct pgpath *pgpath;
1498 unsigned long flags;
1500 spin_lock_irqsave(&m->lock, flags);
1502 /* Guess which priority_group will be used at next mapping time */
1503 if (unlikely(!m->current_pgpath && m->next_pg))
1505 else if (likely(m->current_pg))
1509 * We don't know which pg will be used at next mapping time.
1510 * We don't call __choose_pgpath() here to avoid to trigger
1511 * pg_init just by busy checking.
1512 * So we don't know whether underlying devices we will be using
1513 * at next mapping time are busy or not. Just try mapping.
1518 * If there is one non-busy active path at least, the path selector
1519 * will be able to select it. So we consider such a pg as not busy.
1522 list_for_each_entry(pgpath, &pg->pgpaths, list)
1523 if (pgpath->is_active) {
1526 if (!__pgpath_busy(pgpath)) {
1534 * No active path in this pg, so this pg won't be used and
1535 * the current_pg will be changed at next mapping time.
1536 * We need to try mapping to determine it.
1541 spin_unlock_irqrestore(&m->lock, flags);
1546 /*-----------------------------------------------------------------
1548 *---------------------------------------------------------------*/
1549 static struct target_type multipath_target = {
1550 .name = "multipath",
1551 .version = {1, 0, 5},
1552 .module = THIS_MODULE,
1553 .ctr = multipath_ctr,
1554 .dtr = multipath_dtr,
1555 .map_rq = multipath_map,
1556 .rq_end_io = multipath_end_io,
1557 .presuspend = multipath_presuspend,
1558 .resume = multipath_resume,
1559 .status = multipath_status,
1560 .message = multipath_message,
1561 .ioctl = multipath_ioctl,
1562 .busy = multipath_busy,
1565 static int __init dm_multipath_init(void)
1569 /* allocate a slab for the dm_ios */
1570 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1574 r = dm_register_target(&multipath_target);
1576 DMERR("register failed %d", r);
1577 kmem_cache_destroy(_mpio_cache);
1581 kmultipathd = create_workqueue("kmpathd");
1583 DMERR("failed to create workqueue kmpathd");
1584 dm_unregister_target(&multipath_target);
1585 kmem_cache_destroy(_mpio_cache);
1590 * A separate workqueue is used to handle the device handlers
1591 * to avoid overloading existing workqueue. Overloading the
1592 * old workqueue would also create a bottleneck in the
1593 * path of the storage hardware device activation.
1595 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1596 if (!kmpath_handlerd) {
1597 DMERR("failed to create workqueue kmpath_handlerd");
1598 destroy_workqueue(kmultipathd);
1599 dm_unregister_target(&multipath_target);
1600 kmem_cache_destroy(_mpio_cache);
1604 DMINFO("version %u.%u.%u loaded",
1605 multipath_target.version[0], multipath_target.version[1],
1606 multipath_target.version[2]);
1611 static void __exit dm_multipath_exit(void)
1615 destroy_workqueue(kmpath_handlerd);
1616 destroy_workqueue(kmultipathd);
1618 r = dm_unregister_target(&multipath_target);
1620 DMERR("target unregister failed %d", r);
1621 kmem_cache_destroy(_mpio_cache);
1624 module_init(dm_multipath_init);
1625 module_exit(dm_multipath_exit);
1627 MODULE_DESCRIPTION(DM_NAME " multipath target");
1628 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1629 MODULE_LICENSE("GPL");