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.
8 #include <linux/device-mapper.h>
10 #include "dm-path-selector.h"
11 #include "dm-uevent.h"
13 #include <linux/ctype.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/pagemap.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/workqueue.h>
21 #include <scsi/scsi_dh.h>
22 #include <asm/atomic.h>
24 #define DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
29 struct list_head list;
31 struct priority_group *pg; /* Owning PG */
32 unsigned is_active; /* Path status */
33 unsigned fail_count; /* Cumulative failure count */
36 struct work_struct deactivate_path;
37 struct work_struct activate_path;
40 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
43 * Paths are grouped into Priority Groups and numbered from 1 upwards.
44 * Each has a path selector which controls which path gets used.
46 struct priority_group {
47 struct list_head list;
49 struct multipath *m; /* Owning multipath instance */
50 struct path_selector ps;
52 unsigned pg_num; /* Reference number */
53 unsigned bypassed; /* Temporarily bypass this PG? */
55 unsigned nr_pgpaths; /* Number of paths in PG */
56 struct list_head pgpaths;
59 /* Multipath context */
61 struct list_head list;
66 const char *hw_handler_name;
67 char *hw_handler_params;
68 unsigned nr_priority_groups;
69 struct list_head priority_groups;
70 unsigned pg_init_required; /* pg_init needs calling? */
71 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
72 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
74 unsigned nr_valid_paths; /* Total number of usable paths */
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg; /* Switch to this PG if set */
78 unsigned repeat_count; /* I/Os left before calling PS again */
80 unsigned queue_io; /* Must we queue all I/O? */
81 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
82 unsigned saved_queue_if_no_path;/* Saved state during suspension */
83 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_count; /* Number of times pg_init called */
86 struct work_struct process_queued_ios;
87 struct list_head queued_ios;
90 struct work_struct trigger_event;
93 * We must use a mempool of dm_mpath_io structs so that we
94 * can resubmit bios on error.
98 struct mutex work_mutex;
102 * Context information attached to each bio we process.
105 struct pgpath *pgpath;
109 typedef int (*action_fn) (struct pgpath *pgpath);
111 #define MIN_IOS 256 /* Mempool size */
113 static struct kmem_cache *_mpio_cache;
115 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
116 static void process_queued_ios(struct work_struct *work);
117 static void trigger_event(struct work_struct *work);
118 static void activate_path(struct work_struct *work);
119 static void deactivate_path(struct work_struct *work);
122 /*-----------------------------------------------
123 * Allocation routines
124 *-----------------------------------------------*/
126 static struct pgpath *alloc_pgpath(void)
128 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
131 pgpath->is_active = 1;
132 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
133 INIT_WORK(&pgpath->activate_path, activate_path);
139 static void free_pgpath(struct pgpath *pgpath)
144 static void deactivate_path(struct work_struct *work)
146 struct pgpath *pgpath =
147 container_of(work, struct pgpath, deactivate_path);
149 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
152 static struct priority_group *alloc_priority_group(void)
154 struct priority_group *pg;
156 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
159 INIT_LIST_HEAD(&pg->pgpaths);
164 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
166 struct pgpath *pgpath, *tmp;
167 struct multipath *m = ti->private;
169 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
170 list_del(&pgpath->list);
171 if (m->hw_handler_name)
172 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
173 dm_put_device(ti, pgpath->path.dev);
178 static void free_priority_group(struct priority_group *pg,
179 struct dm_target *ti)
181 struct path_selector *ps = &pg->ps;
184 ps->type->destroy(ps);
185 dm_put_path_selector(ps->type);
188 free_pgpaths(&pg->pgpaths, ti);
192 static struct multipath *alloc_multipath(struct dm_target *ti)
196 m = kzalloc(sizeof(*m), GFP_KERNEL);
198 INIT_LIST_HEAD(&m->priority_groups);
199 INIT_LIST_HEAD(&m->queued_ios);
200 spin_lock_init(&m->lock);
202 INIT_WORK(&m->process_queued_ios, process_queued_ios);
203 INIT_WORK(&m->trigger_event, trigger_event);
204 init_waitqueue_head(&m->pg_init_wait);
205 mutex_init(&m->work_mutex);
206 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
218 static void free_multipath(struct multipath *m)
220 struct priority_group *pg, *tmp;
222 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
224 free_priority_group(pg, m->ti);
227 kfree(m->hw_handler_name);
228 kfree(m->hw_handler_params);
229 mempool_destroy(m->mpio_pool);
234 /*-----------------------------------------------
236 *-----------------------------------------------*/
238 static void __pg_init_all_paths(struct multipath *m)
240 struct pgpath *pgpath;
243 m->pg_init_required = 0;
244 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
245 /* Skip failed paths */
246 if (!pgpath->is_active)
248 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
249 m->pg_init_in_progress++;
253 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
255 m->current_pg = pgpath->pg;
257 /* Must we initialise the PG first, and queue I/O till it's ready? */
258 if (m->hw_handler_name) {
259 m->pg_init_required = 1;
262 m->pg_init_required = 0;
266 m->pg_init_count = 0;
269 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
272 struct dm_path *path;
274 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
278 m->current_pgpath = path_to_pgpath(path);
280 if (m->current_pg != pg)
281 __switch_pg(m, m->current_pgpath);
286 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
288 struct priority_group *pg;
289 unsigned bypassed = 1;
291 if (!m->nr_valid_paths)
294 /* Were we instructed to switch PG? */
298 if (!__choose_path_in_pg(m, pg, nr_bytes))
302 /* Don't change PG until it has no remaining paths */
303 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
307 * Loop through priority groups until we find a valid path.
308 * First time we skip PGs marked 'bypassed'.
309 * Second time we only try the ones we skipped.
312 list_for_each_entry(pg, &m->priority_groups, list) {
313 if (pg->bypassed == bypassed)
315 if (!__choose_path_in_pg(m, pg, nr_bytes))
318 } while (bypassed--);
321 m->current_pgpath = NULL;
322 m->current_pg = NULL;
326 * Check whether bios must be queued in the device-mapper core rather
327 * than here in the target.
329 * m->lock must be held on entry.
331 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
332 * same value then we are not between multipath_presuspend()
333 * and multipath_resume() calls and we have no need to check
334 * for the DMF_NOFLUSH_SUSPENDING flag.
336 static int __must_push_back(struct multipath *m)
338 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
339 dm_noflush_suspending(m->ti));
342 static int map_io(struct multipath *m, struct request *clone,
343 struct dm_mpath_io *mpio, unsigned was_queued)
345 int r = DM_MAPIO_REMAPPED;
346 size_t nr_bytes = blk_rq_bytes(clone);
348 struct pgpath *pgpath;
349 struct block_device *bdev;
351 spin_lock_irqsave(&m->lock, flags);
353 /* Do we need to select a new pgpath? */
354 if (!m->current_pgpath ||
355 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
356 __choose_pgpath(m, nr_bytes);
358 pgpath = m->current_pgpath;
363 if ((pgpath && m->queue_io) ||
364 (!pgpath && m->queue_if_no_path)) {
365 /* Queue for the daemon to resubmit */
366 list_add_tail(&clone->queuelist, &m->queued_ios);
368 if ((m->pg_init_required && !m->pg_init_in_progress) ||
370 queue_work(kmultipathd, &m->process_queued_ios);
372 r = DM_MAPIO_SUBMITTED;
374 bdev = pgpath->path.dev->bdev;
375 clone->q = bdev_get_queue(bdev);
376 clone->rq_disk = bdev->bd_disk;
377 } else if (__must_push_back(m))
378 r = DM_MAPIO_REQUEUE;
380 r = -EIO; /* Failed */
382 mpio->pgpath = pgpath;
383 mpio->nr_bytes = nr_bytes;
385 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
386 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
389 spin_unlock_irqrestore(&m->lock, flags);
395 * If we run out of usable paths, should we queue I/O or error it?
397 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
398 unsigned save_old_value)
402 spin_lock_irqsave(&m->lock, flags);
405 m->saved_queue_if_no_path = m->queue_if_no_path;
407 m->saved_queue_if_no_path = queue_if_no_path;
408 m->queue_if_no_path = queue_if_no_path;
409 if (!m->queue_if_no_path && m->queue_size)
410 queue_work(kmultipathd, &m->process_queued_ios);
412 spin_unlock_irqrestore(&m->lock, flags);
417 /*-----------------------------------------------------------------
418 * The multipath daemon is responsible for resubmitting queued ios.
419 *---------------------------------------------------------------*/
421 static void dispatch_queued_ios(struct multipath *m)
425 struct dm_mpath_io *mpio;
426 union map_info *info;
427 struct request *clone, *n;
430 spin_lock_irqsave(&m->lock, flags);
431 list_splice_init(&m->queued_ios, &cl);
432 spin_unlock_irqrestore(&m->lock, flags);
434 list_for_each_entry_safe(clone, n, &cl, queuelist) {
435 list_del_init(&clone->queuelist);
437 info = dm_get_rq_mapinfo(clone);
440 r = map_io(m, clone, mpio, 1);
442 mempool_free(mpio, m->mpio_pool);
443 dm_kill_unmapped_request(clone, r);
444 } else if (r == DM_MAPIO_REMAPPED)
445 dm_dispatch_request(clone);
446 else if (r == DM_MAPIO_REQUEUE) {
447 mempool_free(mpio, m->mpio_pool);
448 dm_requeue_unmapped_request(clone);
453 static void process_queued_ios(struct work_struct *work)
455 struct multipath *m =
456 container_of(work, struct multipath, process_queued_ios);
457 struct pgpath *pgpath = NULL;
458 unsigned must_queue = 1;
461 spin_lock_irqsave(&m->lock, flags);
466 if (!m->current_pgpath)
467 __choose_pgpath(m, 0);
469 pgpath = m->current_pgpath;
471 if ((pgpath && !m->queue_io) ||
472 (!pgpath && !m->queue_if_no_path))
475 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
476 __pg_init_all_paths(m);
479 spin_unlock_irqrestore(&m->lock, flags);
481 dispatch_queued_ios(m);
485 * An event is triggered whenever a path is taken out of use.
486 * Includes path failure and PG bypass.
488 static void trigger_event(struct work_struct *work)
490 struct multipath *m =
491 container_of(work, struct multipath, trigger_event);
493 dm_table_event(m->ti->table);
496 /*-----------------------------------------------------------------
497 * Constructor/argument parsing:
498 * <#multipath feature args> [<arg>]*
499 * <#hw_handler args> [hw_handler [<arg>]*]
501 * <initial priority group>
502 * [<selector> <#selector args> [<arg>]*
503 * <#paths> <#per-path selector args>
504 * [<path> [<arg>]* ]+ ]+
505 *---------------------------------------------------------------*/
512 static int read_param(struct param *param, char *str, unsigned *v, char **error)
515 (sscanf(str, "%u", v) != 1) ||
518 *error = param->error;
530 static char *shift(struct arg_set *as)
544 static void consume(struct arg_set *as, unsigned n)
546 BUG_ON (as->argc < n);
551 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
552 struct dm_target *ti)
555 struct path_selector_type *pst;
558 static struct param _params[] = {
559 {0, 1024, "invalid number of path selector args"},
562 pst = dm_get_path_selector(shift(as));
564 ti->error = "unknown path selector type";
568 r = read_param(_params, shift(as), &ps_argc, &ti->error);
570 dm_put_path_selector(pst);
574 if (ps_argc > as->argc) {
575 dm_put_path_selector(pst);
576 ti->error = "not enough arguments for path selector";
580 r = pst->create(&pg->ps, ps_argc, as->argv);
582 dm_put_path_selector(pst);
583 ti->error = "path selector constructor failed";
588 consume(as, ps_argc);
593 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
594 struct dm_target *ti)
598 struct multipath *m = ti->private;
600 /* we need at least a path arg */
602 ti->error = "no device given";
603 return ERR_PTR(-EINVAL);
608 return ERR_PTR(-ENOMEM);
610 r = dm_get_device(ti, shift(as), dm_table_get_mode(ti->table),
613 ti->error = "error getting device";
617 if (m->hw_handler_name) {
618 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
620 r = scsi_dh_attach(q, m->hw_handler_name);
623 * Already attached to different hw_handler,
624 * try to reattach with correct one.
627 r = scsi_dh_attach(q, m->hw_handler_name);
631 ti->error = "error attaching hardware handler";
632 dm_put_device(ti, p->path.dev);
636 if (m->hw_handler_params) {
637 r = scsi_dh_set_params(q, m->hw_handler_params);
639 ti->error = "unable to set hardware "
640 "handler parameters";
642 dm_put_device(ti, p->path.dev);
648 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
650 dm_put_device(ti, p->path.dev);
661 static struct priority_group *parse_priority_group(struct arg_set *as,
664 static struct param _params[] = {
665 {1, 1024, "invalid number of paths"},
666 {0, 1024, "invalid number of selector args"}
670 unsigned i, nr_selector_args, nr_params;
671 struct priority_group *pg;
672 struct dm_target *ti = m->ti;
676 ti->error = "not enough priority group arguments";
677 return ERR_PTR(-EINVAL);
680 pg = alloc_priority_group();
682 ti->error = "couldn't allocate priority group";
683 return ERR_PTR(-ENOMEM);
687 r = parse_path_selector(as, pg, ti);
694 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
698 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
702 nr_params = 1 + nr_selector_args;
703 for (i = 0; i < pg->nr_pgpaths; i++) {
704 struct pgpath *pgpath;
705 struct arg_set path_args;
707 if (as->argc < nr_params) {
708 ti->error = "not enough path parameters";
713 path_args.argc = nr_params;
714 path_args.argv = as->argv;
716 pgpath = parse_path(&path_args, &pg->ps, ti);
717 if (IS_ERR(pgpath)) {
723 list_add_tail(&pgpath->list, &pg->pgpaths);
724 consume(as, nr_params);
730 free_priority_group(pg, ti);
734 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
738 struct dm_target *ti = m->ti;
740 static struct param _params[] = {
741 {0, 1024, "invalid number of hardware handler args"},
744 if (read_param(_params, shift(as), &hw_argc, &ti->error))
750 if (hw_argc > as->argc) {
751 ti->error = "not enough arguments for hardware handler";
755 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
756 request_module("scsi_dh_%s", m->hw_handler_name);
757 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
758 ti->error = "unknown hardware handler type";
767 for (i = 0; i <= hw_argc - 2; i++)
768 len += strlen(as->argv[i]) + 1;
769 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
771 ti->error = "memory allocation failed";
775 j = sprintf(p, "%d", hw_argc - 1);
776 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
777 j = sprintf(p, "%s", as->argv[i]);
779 consume(as, hw_argc - 1);
783 kfree(m->hw_handler_name);
784 m->hw_handler_name = NULL;
788 static int parse_features(struct arg_set *as, struct multipath *m)
792 struct dm_target *ti = m->ti;
793 const char *param_name;
795 static struct param _params[] = {
796 {0, 3, "invalid number of feature args"},
797 {1, 50, "pg_init_retries must be between 1 and 50"},
800 r = read_param(_params, shift(as), &argc, &ti->error);
808 param_name = shift(as);
811 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
812 r = queue_if_no_path(m, 1, 0);
816 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
818 r = read_param(_params + 1, shift(as),
819 &m->pg_init_retries, &ti->error);
824 ti->error = "Unrecognised multipath feature request";
826 } while (argc && !r);
831 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
834 /* target parameters */
835 static struct param _params[] = {
836 {1, 1024, "invalid number of priority groups"},
837 {1, 1024, "invalid initial priority group number"},
843 unsigned pg_count = 0;
844 unsigned next_pg_num;
849 m = alloc_multipath(ti);
851 ti->error = "can't allocate multipath";
855 r = parse_features(&as, m);
859 r = parse_hw_handler(&as, m);
863 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
867 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
871 /* parse the priority groups */
873 struct priority_group *pg;
875 pg = parse_priority_group(&as, m);
881 m->nr_valid_paths += pg->nr_pgpaths;
882 list_add_tail(&pg->list, &m->priority_groups);
884 pg->pg_num = pg_count;
889 if (pg_count != m->nr_priority_groups) {
890 ti->error = "priority group count mismatch";
895 ti->num_flush_requests = 1;
896 ti->num_discard_requests = 1;
905 static void multipath_wait_for_pg_init_completion(struct multipath *m)
907 DECLARE_WAITQUEUE(wait, current);
910 add_wait_queue(&m->pg_init_wait, &wait);
913 set_current_state(TASK_UNINTERRUPTIBLE);
915 spin_lock_irqsave(&m->lock, flags);
916 if (!m->pg_init_in_progress) {
917 spin_unlock_irqrestore(&m->lock, flags);
920 spin_unlock_irqrestore(&m->lock, flags);
924 set_current_state(TASK_RUNNING);
926 remove_wait_queue(&m->pg_init_wait, &wait);
929 static void flush_multipath_work(struct multipath *m)
931 flush_workqueue(kmpath_handlerd);
932 multipath_wait_for_pg_init_completion(m);
933 flush_workqueue(kmultipathd);
934 flush_scheduled_work();
937 static void multipath_dtr(struct dm_target *ti)
939 struct multipath *m = ti->private;
941 flush_multipath_work(m);
946 * Map cloned requests
948 static int multipath_map(struct dm_target *ti, struct request *clone,
949 union map_info *map_context)
952 struct dm_mpath_io *mpio;
953 struct multipath *m = (struct multipath *) ti->private;
955 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
957 /* ENOMEM, requeue */
958 return DM_MAPIO_REQUEUE;
959 memset(mpio, 0, sizeof(*mpio));
961 map_context->ptr = mpio;
962 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
963 r = map_io(m, clone, mpio, 0);
964 if (r < 0 || r == DM_MAPIO_REQUEUE)
965 mempool_free(mpio, m->mpio_pool);
971 * Take a path out of use.
973 static int fail_path(struct pgpath *pgpath)
976 struct multipath *m = pgpath->pg->m;
978 spin_lock_irqsave(&m->lock, flags);
980 if (!pgpath->is_active)
983 DMWARN("Failing path %s.", pgpath->path.dev->name);
985 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
986 pgpath->is_active = 0;
987 pgpath->fail_count++;
991 if (pgpath == m->current_pgpath)
992 m->current_pgpath = NULL;
994 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
995 pgpath->path.dev->name, m->nr_valid_paths);
997 schedule_work(&m->trigger_event);
998 queue_work(kmultipathd, &pgpath->deactivate_path);
1001 spin_unlock_irqrestore(&m->lock, flags);
1007 * Reinstate a previously-failed path
1009 static int reinstate_path(struct pgpath *pgpath)
1012 unsigned long flags;
1013 struct multipath *m = pgpath->pg->m;
1015 spin_lock_irqsave(&m->lock, flags);
1017 if (pgpath->is_active)
1020 if (!pgpath->pg->ps.type->reinstate_path) {
1021 DMWARN("Reinstate path not supported by path selector %s",
1022 pgpath->pg->ps.type->name);
1027 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1031 pgpath->is_active = 1;
1033 if (!m->nr_valid_paths++ && m->queue_size) {
1034 m->current_pgpath = NULL;
1035 queue_work(kmultipathd, &m->process_queued_ios);
1036 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1037 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
1038 m->pg_init_in_progress++;
1041 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1042 pgpath->path.dev->name, m->nr_valid_paths);
1044 schedule_work(&m->trigger_event);
1047 spin_unlock_irqrestore(&m->lock, flags);
1053 * Fail or reinstate all paths that match the provided struct dm_dev.
1055 static int action_dev(struct multipath *m, struct dm_dev *dev,
1059 struct pgpath *pgpath;
1060 struct priority_group *pg;
1062 list_for_each_entry(pg, &m->priority_groups, list) {
1063 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1064 if (pgpath->path.dev == dev)
1073 * Temporarily try to avoid having to use the specified PG
1075 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1078 unsigned long flags;
1080 spin_lock_irqsave(&m->lock, flags);
1082 pg->bypassed = bypassed;
1083 m->current_pgpath = NULL;
1084 m->current_pg = NULL;
1086 spin_unlock_irqrestore(&m->lock, flags);
1088 schedule_work(&m->trigger_event);
1092 * Switch to using the specified PG from the next I/O that gets mapped
1094 static int switch_pg_num(struct multipath *m, const char *pgstr)
1096 struct priority_group *pg;
1098 unsigned long flags;
1100 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1101 (pgnum > m->nr_priority_groups)) {
1102 DMWARN("invalid PG number supplied to switch_pg_num");
1106 spin_lock_irqsave(&m->lock, flags);
1107 list_for_each_entry(pg, &m->priority_groups, list) {
1112 m->current_pgpath = NULL;
1113 m->current_pg = NULL;
1116 spin_unlock_irqrestore(&m->lock, flags);
1118 schedule_work(&m->trigger_event);
1123 * Set/clear bypassed status of a PG.
1124 * PGs are numbered upwards from 1 in the order they were declared.
1126 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1128 struct priority_group *pg;
1131 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1132 (pgnum > m->nr_priority_groups)) {
1133 DMWARN("invalid PG number supplied to bypass_pg");
1137 list_for_each_entry(pg, &m->priority_groups, list) {
1142 bypass_pg(m, pg, bypassed);
1147 * Should we retry pg_init immediately?
1149 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1151 unsigned long flags;
1152 int limit_reached = 0;
1154 spin_lock_irqsave(&m->lock, flags);
1156 if (m->pg_init_count <= m->pg_init_retries)
1157 m->pg_init_required = 1;
1161 spin_unlock_irqrestore(&m->lock, flags);
1163 return limit_reached;
1166 static void pg_init_done(void *data, int errors)
1168 struct pgpath *pgpath = data;
1169 struct priority_group *pg = pgpath->pg;
1170 struct multipath *m = pg->m;
1171 unsigned long flags;
1173 /* device or driver problems */
1178 if (!m->hw_handler_name) {
1182 DMERR("Could not failover the device: Handler scsi_dh_%s "
1183 "Error %d.", m->hw_handler_name, errors);
1185 * Fail path for now, so we do not ping pong
1189 case SCSI_DH_DEV_TEMP_BUSY:
1191 * Probably doing something like FW upgrade on the
1192 * controller so try the other pg.
1194 bypass_pg(m, pg, 1);
1196 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1198 case SCSI_DH_IMM_RETRY:
1199 case SCSI_DH_RES_TEMP_UNAVAIL:
1200 if (pg_init_limit_reached(m, pgpath))
1206 * We probably do not want to fail the path for a device
1207 * error, but this is what the old dm did. In future
1208 * patches we can do more advanced handling.
1213 spin_lock_irqsave(&m->lock, flags);
1215 if (pgpath == m->current_pgpath) {
1216 DMERR("Could not failover device. Error %d.", errors);
1217 m->current_pgpath = NULL;
1218 m->current_pg = NULL;
1220 } else if (!m->pg_init_required)
1223 if (--m->pg_init_in_progress)
1224 /* Activations of other paths are still on going */
1227 if (!m->pg_init_required)
1230 queue_work(kmultipathd, &m->process_queued_ios);
1233 * Wake up any thread waiting to suspend.
1235 wake_up(&m->pg_init_wait);
1238 spin_unlock_irqrestore(&m->lock, flags);
1241 static void activate_path(struct work_struct *work)
1243 struct pgpath *pgpath =
1244 container_of(work, struct pgpath, activate_path);
1246 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1247 pg_init_done, pgpath);
1253 static int do_end_io(struct multipath *m, struct request *clone,
1254 int error, struct dm_mpath_io *mpio)
1257 * We don't queue any clone request inside the multipath target
1258 * during end I/O handling, since those clone requests don't have
1259 * bio clones. If we queue them inside the multipath target,
1260 * we need to make bio clones, that requires memory allocation.
1261 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1262 * don't have bio clones.)
1263 * Instead of queueing the clone request here, we queue the original
1264 * request into dm core, which will remake a clone request and
1265 * clone bios for it and resubmit it later.
1267 int r = DM_ENDIO_REQUEUE;
1268 unsigned long flags;
1270 if (!error && !clone->errors)
1271 return 0; /* I/O complete */
1273 if (error == -EOPNOTSUPP)
1276 if (clone->cmd_flags & REQ_DISCARD)
1278 * Pass all discard request failures up.
1279 * FIXME: only fail_path if the discard failed due to a
1280 * transport problem. This requires precise understanding
1281 * of the underlying failure (e.g. the SCSI sense).
1286 fail_path(mpio->pgpath);
1288 spin_lock_irqsave(&m->lock, flags);
1289 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1291 spin_unlock_irqrestore(&m->lock, flags);
1296 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1297 int error, union map_info *map_context)
1299 struct multipath *m = ti->private;
1300 struct dm_mpath_io *mpio = map_context->ptr;
1301 struct pgpath *pgpath = mpio->pgpath;
1302 struct path_selector *ps;
1305 r = do_end_io(m, clone, error, mpio);
1307 ps = &pgpath->pg->ps;
1308 if (ps->type->end_io)
1309 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1311 mempool_free(mpio, m->mpio_pool);
1317 * Suspend can't complete until all the I/O is processed so if
1318 * the last path fails we must error any remaining I/O.
1319 * Note that if the freeze_bdev fails while suspending, the
1320 * queue_if_no_path state is lost - userspace should reset it.
1322 static void multipath_presuspend(struct dm_target *ti)
1324 struct multipath *m = (struct multipath *) ti->private;
1326 queue_if_no_path(m, 0, 1);
1329 static void multipath_postsuspend(struct dm_target *ti)
1331 struct multipath *m = ti->private;
1333 mutex_lock(&m->work_mutex);
1334 flush_multipath_work(m);
1335 mutex_unlock(&m->work_mutex);
1339 * Restore the queue_if_no_path setting.
1341 static void multipath_resume(struct dm_target *ti)
1343 struct multipath *m = (struct multipath *) ti->private;
1344 unsigned long flags;
1346 spin_lock_irqsave(&m->lock, flags);
1347 m->queue_if_no_path = m->saved_queue_if_no_path;
1348 spin_unlock_irqrestore(&m->lock, flags);
1352 * Info output has the following format:
1353 * num_multipath_feature_args [multipath_feature_args]*
1354 * num_handler_status_args [handler_status_args]*
1355 * num_groups init_group_number
1356 * [A|D|E num_ps_status_args [ps_status_args]*
1357 * num_paths num_selector_args
1358 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1360 * Table output has the following format (identical to the constructor string):
1361 * num_feature_args [features_args]*
1362 * num_handler_args hw_handler [hw_handler_args]*
1363 * num_groups init_group_number
1364 * [priority selector-name num_ps_args [ps_args]*
1365 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1367 static int multipath_status(struct dm_target *ti, status_type_t type,
1368 char *result, unsigned int maxlen)
1371 unsigned long flags;
1372 struct multipath *m = (struct multipath *) ti->private;
1373 struct priority_group *pg;
1378 spin_lock_irqsave(&m->lock, flags);
1381 if (type == STATUSTYPE_INFO)
1382 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1384 DMEMIT("%u ", m->queue_if_no_path +
1385 (m->pg_init_retries > 0) * 2);
1386 if (m->queue_if_no_path)
1387 DMEMIT("queue_if_no_path ");
1388 if (m->pg_init_retries)
1389 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1392 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1395 DMEMIT("1 %s ", m->hw_handler_name);
1397 DMEMIT("%u ", m->nr_priority_groups);
1400 pg_num = m->next_pg->pg_num;
1401 else if (m->current_pg)
1402 pg_num = m->current_pg->pg_num;
1406 DMEMIT("%u ", pg_num);
1409 case STATUSTYPE_INFO:
1410 list_for_each_entry(pg, &m->priority_groups, list) {
1412 state = 'D'; /* Disabled */
1413 else if (pg == m->current_pg)
1414 state = 'A'; /* Currently Active */
1416 state = 'E'; /* Enabled */
1418 DMEMIT("%c ", state);
1420 if (pg->ps.type->status)
1421 sz += pg->ps.type->status(&pg->ps, NULL, type,
1427 DMEMIT("%u %u ", pg->nr_pgpaths,
1428 pg->ps.type->info_args);
1430 list_for_each_entry(p, &pg->pgpaths, list) {
1431 DMEMIT("%s %s %u ", p->path.dev->name,
1432 p->is_active ? "A" : "F",
1434 if (pg->ps.type->status)
1435 sz += pg->ps.type->status(&pg->ps,
1436 &p->path, type, result + sz,
1442 case STATUSTYPE_TABLE:
1443 list_for_each_entry(pg, &m->priority_groups, list) {
1444 DMEMIT("%s ", pg->ps.type->name);
1446 if (pg->ps.type->status)
1447 sz += pg->ps.type->status(&pg->ps, NULL, type,
1453 DMEMIT("%u %u ", pg->nr_pgpaths,
1454 pg->ps.type->table_args);
1456 list_for_each_entry(p, &pg->pgpaths, list) {
1457 DMEMIT("%s ", p->path.dev->name);
1458 if (pg->ps.type->status)
1459 sz += pg->ps.type->status(&pg->ps,
1460 &p->path, type, result + sz,
1467 spin_unlock_irqrestore(&m->lock, flags);
1472 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1476 struct multipath *m = (struct multipath *) ti->private;
1479 mutex_lock(&m->work_mutex);
1481 if (dm_suspended(ti)) {
1487 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1488 r = queue_if_no_path(m, 1, 0);
1490 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1491 r = queue_if_no_path(m, 0, 0);
1497 DMWARN("Unrecognised multipath message received.");
1501 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1502 r = bypass_pg_num(m, argv[1], 1);
1504 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1505 r = bypass_pg_num(m, argv[1], 0);
1507 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1508 r = switch_pg_num(m, argv[1]);
1510 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1511 action = reinstate_path;
1512 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1515 DMWARN("Unrecognised multipath message received.");
1519 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1521 DMWARN("message: error getting device %s",
1526 r = action_dev(m, dev, action);
1528 dm_put_device(ti, dev);
1531 mutex_unlock(&m->work_mutex);
1535 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1538 struct multipath *m = (struct multipath *) ti->private;
1539 struct block_device *bdev = NULL;
1541 unsigned long flags;
1544 spin_lock_irqsave(&m->lock, flags);
1546 if (!m->current_pgpath)
1547 __choose_pgpath(m, 0);
1549 if (m->current_pgpath) {
1550 bdev = m->current_pgpath->path.dev->bdev;
1551 mode = m->current_pgpath->path.dev->mode;
1559 spin_unlock_irqrestore(&m->lock, flags);
1561 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1564 static int multipath_iterate_devices(struct dm_target *ti,
1565 iterate_devices_callout_fn fn, void *data)
1567 struct multipath *m = ti->private;
1568 struct priority_group *pg;
1572 list_for_each_entry(pg, &m->priority_groups, list) {
1573 list_for_each_entry(p, &pg->pgpaths, list) {
1574 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1584 static int __pgpath_busy(struct pgpath *pgpath)
1586 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1588 return dm_underlying_device_busy(q);
1592 * We return "busy", only when we can map I/Os but underlying devices
1593 * are busy (so even if we map I/Os now, the I/Os will wait on
1594 * the underlying queue).
1595 * In other words, if we want to kill I/Os or queue them inside us
1596 * due to map unavailability, we don't return "busy". Otherwise,
1597 * dm core won't give us the I/Os and we can't do what we want.
1599 static int multipath_busy(struct dm_target *ti)
1601 int busy = 0, has_active = 0;
1602 struct multipath *m = ti->private;
1603 struct priority_group *pg;
1604 struct pgpath *pgpath;
1605 unsigned long flags;
1607 spin_lock_irqsave(&m->lock, flags);
1609 /* Guess which priority_group will be used at next mapping time */
1610 if (unlikely(!m->current_pgpath && m->next_pg))
1612 else if (likely(m->current_pg))
1616 * We don't know which pg will be used at next mapping time.
1617 * We don't call __choose_pgpath() here to avoid to trigger
1618 * pg_init just by busy checking.
1619 * So we don't know whether underlying devices we will be using
1620 * at next mapping time are busy or not. Just try mapping.
1625 * If there is one non-busy active path at least, the path selector
1626 * will be able to select it. So we consider such a pg as not busy.
1629 list_for_each_entry(pgpath, &pg->pgpaths, list)
1630 if (pgpath->is_active) {
1633 if (!__pgpath_busy(pgpath)) {
1641 * No active path in this pg, so this pg won't be used and
1642 * the current_pg will be changed at next mapping time.
1643 * We need to try mapping to determine it.
1648 spin_unlock_irqrestore(&m->lock, flags);
1653 /*-----------------------------------------------------------------
1655 *---------------------------------------------------------------*/
1656 static struct target_type multipath_target = {
1657 .name = "multipath",
1658 .version = {1, 1, 1},
1659 .module = THIS_MODULE,
1660 .ctr = multipath_ctr,
1661 .dtr = multipath_dtr,
1662 .map_rq = multipath_map,
1663 .rq_end_io = multipath_end_io,
1664 .presuspend = multipath_presuspend,
1665 .postsuspend = multipath_postsuspend,
1666 .resume = multipath_resume,
1667 .status = multipath_status,
1668 .message = multipath_message,
1669 .ioctl = multipath_ioctl,
1670 .iterate_devices = multipath_iterate_devices,
1671 .busy = multipath_busy,
1674 static int __init dm_multipath_init(void)
1678 /* allocate a slab for the dm_ios */
1679 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1683 r = dm_register_target(&multipath_target);
1685 DMERR("register failed %d", r);
1686 kmem_cache_destroy(_mpio_cache);
1690 kmultipathd = create_workqueue("kmpathd");
1692 DMERR("failed to create workqueue kmpathd");
1693 dm_unregister_target(&multipath_target);
1694 kmem_cache_destroy(_mpio_cache);
1699 * A separate workqueue is used to handle the device handlers
1700 * to avoid overloading existing workqueue. Overloading the
1701 * old workqueue would also create a bottleneck in the
1702 * path of the storage hardware device activation.
1704 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1705 if (!kmpath_handlerd) {
1706 DMERR("failed to create workqueue kmpath_handlerd");
1707 destroy_workqueue(kmultipathd);
1708 dm_unregister_target(&multipath_target);
1709 kmem_cache_destroy(_mpio_cache);
1713 DMINFO("version %u.%u.%u loaded",
1714 multipath_target.version[0], multipath_target.version[1],
1715 multipath_target.version[2]);
1720 static void __exit dm_multipath_exit(void)
1722 destroy_workqueue(kmpath_handlerd);
1723 destroy_workqueue(kmultipathd);
1725 dm_unregister_target(&multipath_target);
1726 kmem_cache_destroy(_mpio_cache);
1729 module_init(dm_multipath_init);
1730 module_exit(dm_multipath_exit);
1732 MODULE_DESCRIPTION(DM_NAME " multipath target");
1733 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1734 MODULE_LICENSE("GPL");