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 <scsi/scsi_eh.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define MESG_STR(x) x, sizeof(x)
27 #define DM_PG_INIT_DELAY_MSECS 2000
28 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
32 struct list_head list;
34 struct priority_group *pg; /* Owning PG */
35 unsigned is_active; /* Path status */
36 unsigned fail_count; /* Cumulative failure count */
39 struct delayed_work activate_path;
42 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
45 * Paths are grouped into Priority Groups and numbered from 1 upwards.
46 * Each has a path selector which controls which path gets used.
48 struct priority_group {
49 struct list_head list;
51 struct multipath *m; /* Owning multipath instance */
52 struct path_selector ps;
54 unsigned pg_num; /* Reference number */
55 unsigned bypassed; /* Temporarily bypass this PG? */
57 unsigned nr_pgpaths; /* Number of paths in PG */
58 struct list_head pgpaths;
61 #define FEATURE_NO_PARTITIONS 1
63 /* Multipath context */
65 struct list_head list;
70 const char *hw_handler_name;
71 char *hw_handler_params;
73 unsigned nr_priority_groups;
74 struct list_head priority_groups;
76 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
78 unsigned pg_init_required; /* pg_init needs calling? */
79 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
80 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
82 unsigned nr_valid_paths; /* Total number of usable paths */
83 struct pgpath *current_pgpath;
84 struct priority_group *current_pg;
85 struct priority_group *next_pg; /* Switch to this PG if set */
86 unsigned repeat_count; /* I/Os left before calling PS again */
88 unsigned queue_io; /* Must we queue all I/O? */
89 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
90 unsigned saved_queue_if_no_path;/* Saved state during suspension */
91 unsigned pg_init_retries; /* Number of times to retry pg_init */
92 unsigned pg_init_count; /* Number of times pg_init called */
93 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
94 unsigned features; /* Additional selected features */
96 struct work_struct process_queued_ios;
97 struct list_head queued_ios;
100 struct work_struct trigger_event;
103 * We must use a mempool of dm_mpath_io structs so that we
104 * can resubmit bios on error.
106 mempool_t *mpio_pool;
108 struct mutex work_mutex;
112 * Context information attached to each bio we process.
115 struct pgpath *pgpath;
117 char sense[SCSI_SENSE_BUFFERSIZE];
120 typedef int (*action_fn) (struct pgpath *pgpath);
122 #define MIN_IOS 256 /* Mempool size */
124 static struct kmem_cache *_mpio_cache;
126 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
127 static void process_queued_ios(struct work_struct *work);
128 static void trigger_event(struct work_struct *work);
129 static void activate_path(struct work_struct *work);
132 /*-----------------------------------------------
133 * Allocation routines
134 *-----------------------------------------------*/
136 static struct pgpath *alloc_pgpath(void)
138 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
141 pgpath->is_active = 1;
142 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
148 static void free_pgpath(struct pgpath *pgpath)
153 static struct priority_group *alloc_priority_group(void)
155 struct priority_group *pg;
157 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
160 INIT_LIST_HEAD(&pg->pgpaths);
165 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
167 struct pgpath *pgpath, *tmp;
169 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
170 list_del(&pgpath->list);
171 dm_put_device(ti, pgpath->path.dev);
176 static void free_priority_group(struct priority_group *pg,
177 struct dm_target *ti)
179 struct path_selector *ps = &pg->ps;
182 ps->type->destroy(ps);
183 dm_put_path_selector(ps->type);
186 free_pgpaths(&pg->pgpaths, ti);
190 static struct multipath *alloc_multipath(struct dm_target *ti)
194 m = kzalloc(sizeof(*m), GFP_KERNEL);
196 INIT_LIST_HEAD(&m->priority_groups);
197 INIT_LIST_HEAD(&m->queued_ios);
198 spin_lock_init(&m->lock);
200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
201 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
203 init_waitqueue_head(&m->pg_init_wait);
204 mutex_init(&m->work_mutex);
205 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
217 static void free_multipath(struct multipath *m)
219 struct priority_group *pg, *tmp;
221 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
223 free_priority_group(pg, m->ti);
226 kfree(m->hw_handler_name);
227 kfree(m->hw_handler_params);
228 mempool_destroy(m->mpio_pool);
233 /*-----------------------------------------------
235 *-----------------------------------------------*/
237 static void __pg_init_all_paths(struct multipath *m)
239 struct pgpath *pgpath;
240 unsigned long pg_init_delay = 0;
243 m->pg_init_required = 0;
244 if (m->pg_init_delay_retry)
245 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
246 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
247 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
248 /* Skip failed paths */
249 if (!pgpath->is_active)
251 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
253 m->pg_init_in_progress++;
257 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
259 m->current_pg = pgpath->pg;
261 /* Must we initialise the PG first, and queue I/O till it's ready? */
262 if (m->hw_handler_name) {
263 m->pg_init_required = 1;
266 m->pg_init_required = 0;
270 m->pg_init_count = 0;
273 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
276 struct dm_path *path;
278 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
282 m->current_pgpath = path_to_pgpath(path);
284 if (m->current_pg != pg)
285 __switch_pg(m, m->current_pgpath);
290 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
292 struct priority_group *pg;
293 unsigned bypassed = 1;
295 if (!m->nr_valid_paths)
298 /* Were we instructed to switch PG? */
302 if (!__choose_path_in_pg(m, pg, nr_bytes))
306 /* Don't change PG until it has no remaining paths */
307 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
311 * Loop through priority groups until we find a valid path.
312 * First time we skip PGs marked 'bypassed'.
313 * Second time we only try the ones we skipped.
316 list_for_each_entry(pg, &m->priority_groups, list) {
317 if (pg->bypassed == bypassed)
319 if (!__choose_path_in_pg(m, pg, nr_bytes))
322 } while (bypassed--);
325 m->current_pgpath = NULL;
326 m->current_pg = NULL;
330 * Check whether bios must be queued in the device-mapper core rather
331 * than here in the target.
333 * m->lock must be held on entry.
335 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
336 * same value then we are not between multipath_presuspend()
337 * and multipath_resume() calls and we have no need to check
338 * for the DMF_NOFLUSH_SUSPENDING flag.
340 static int __must_push_back(struct multipath *m)
342 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
343 dm_noflush_suspending(m->ti));
346 static int map_io(struct multipath *m, struct request *clone,
347 struct dm_mpath_io *mpio, unsigned was_queued)
349 int r = DM_MAPIO_REMAPPED;
350 size_t nr_bytes = blk_rq_bytes(clone);
352 struct pgpath *pgpath;
353 struct block_device *bdev;
355 spin_lock_irqsave(&m->lock, flags);
357 /* Do we need to select a new pgpath? */
358 if (!m->current_pgpath ||
359 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
360 __choose_pgpath(m, nr_bytes);
362 pgpath = m->current_pgpath;
367 if ((pgpath && m->queue_io) ||
368 (!pgpath && m->queue_if_no_path)) {
369 /* Queue for the daemon to resubmit */
370 list_add_tail(&clone->queuelist, &m->queued_ios);
372 if ((m->pg_init_required && !m->pg_init_in_progress) ||
374 queue_work(kmultipathd, &m->process_queued_ios);
376 r = DM_MAPIO_SUBMITTED;
378 bdev = pgpath->path.dev->bdev;
379 clone->q = bdev_get_queue(bdev);
380 clone->rq_disk = bdev->bd_disk;
381 } else if (__must_push_back(m))
382 r = DM_MAPIO_REQUEUE;
384 r = -EIO; /* Failed */
386 mpio->pgpath = pgpath;
387 mpio->nr_bytes = nr_bytes;
389 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
390 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
393 spin_unlock_irqrestore(&m->lock, flags);
399 * If we run out of usable paths, should we queue I/O or error it?
401 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
402 unsigned save_old_value)
406 spin_lock_irqsave(&m->lock, flags);
409 m->saved_queue_if_no_path = m->queue_if_no_path;
411 m->saved_queue_if_no_path = queue_if_no_path;
412 m->queue_if_no_path = queue_if_no_path;
413 if (!m->queue_if_no_path && m->queue_size)
414 queue_work(kmultipathd, &m->process_queued_ios);
416 spin_unlock_irqrestore(&m->lock, flags);
421 /*-----------------------------------------------------------------
422 * The multipath daemon is responsible for resubmitting queued ios.
423 *---------------------------------------------------------------*/
425 static void dispatch_queued_ios(struct multipath *m)
429 struct dm_mpath_io *mpio;
430 union map_info *info;
431 struct request *clone, *n;
434 spin_lock_irqsave(&m->lock, flags);
435 list_splice_init(&m->queued_ios, &cl);
436 spin_unlock_irqrestore(&m->lock, flags);
438 list_for_each_entry_safe(clone, n, &cl, queuelist) {
439 list_del_init(&clone->queuelist);
441 info = dm_get_rq_mapinfo(clone);
444 r = map_io(m, clone, mpio, 1);
446 mempool_free(mpio, m->mpio_pool);
447 dm_kill_unmapped_request(clone, r);
448 } else if (r == DM_MAPIO_REMAPPED)
449 dm_dispatch_request(clone);
450 else if (r == DM_MAPIO_REQUEUE) {
451 mempool_free(mpio, m->mpio_pool);
452 dm_requeue_unmapped_request(clone);
457 static void process_queued_ios(struct work_struct *work)
459 struct multipath *m =
460 container_of(work, struct multipath, process_queued_ios);
461 struct pgpath *pgpath = NULL;
462 unsigned must_queue = 1;
465 spin_lock_irqsave(&m->lock, flags);
470 if (!m->current_pgpath)
471 __choose_pgpath(m, 0);
473 pgpath = m->current_pgpath;
475 if ((pgpath && !m->queue_io) ||
476 (!pgpath && !m->queue_if_no_path))
479 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
480 __pg_init_all_paths(m);
483 spin_unlock_irqrestore(&m->lock, flags);
485 dispatch_queued_ios(m);
489 * An event is triggered whenever a path is taken out of use.
490 * Includes path failure and PG bypass.
492 static void trigger_event(struct work_struct *work)
494 struct multipath *m =
495 container_of(work, struct multipath, trigger_event);
497 dm_table_event(m->ti->table);
500 /*-----------------------------------------------------------------
501 * Constructor/argument parsing:
502 * <#multipath feature args> [<arg>]*
503 * <#hw_handler args> [hw_handler [<arg>]*]
505 * <initial priority group>
506 * [<selector> <#selector args> [<arg>]*
507 * <#paths> <#per-path selector args>
508 * [<path> [<arg>]* ]+ ]+
509 *---------------------------------------------------------------*/
516 static int read_param(struct param *param, char *str, unsigned *v, char **error)
519 (sscanf(str, "%u", v) != 1) ||
522 *error = param->error;
534 static char *shift(struct arg_set *as)
548 static void consume(struct arg_set *as, unsigned n)
550 BUG_ON (as->argc < n);
555 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
556 struct dm_target *ti)
559 struct path_selector_type *pst;
562 static struct param _params[] = {
563 {0, 1024, "invalid number of path selector args"},
566 pst = dm_get_path_selector(shift(as));
568 ti->error = "unknown path selector type";
572 r = read_param(_params, shift(as), &ps_argc, &ti->error);
574 dm_put_path_selector(pst);
578 if (ps_argc > as->argc) {
579 dm_put_path_selector(pst);
580 ti->error = "not enough arguments for path selector";
584 r = pst->create(&pg->ps, ps_argc, as->argv);
586 dm_put_path_selector(pst);
587 ti->error = "path selector constructor failed";
592 consume(as, ps_argc);
597 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
598 struct dm_target *ti)
602 struct multipath *m = ti->private;
604 /* we need at least a path arg */
606 ti->error = "no device given";
607 return ERR_PTR(-EINVAL);
612 return ERR_PTR(-ENOMEM);
614 r = dm_get_device(ti, shift(as), dm_table_get_mode(ti->table),
617 ti->error = "error getting device";
621 if (m->hw_handler_name) {
622 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
624 r = scsi_dh_attach(q, m->hw_handler_name);
627 * Already attached to different hw_handler,
628 * try to reattach with correct one.
631 r = scsi_dh_attach(q, m->hw_handler_name);
635 ti->error = "error attaching hardware handler";
636 dm_put_device(ti, p->path.dev);
640 if (m->hw_handler_params) {
641 r = scsi_dh_set_params(q, m->hw_handler_params);
643 ti->error = "unable to set hardware "
644 "handler parameters";
646 dm_put_device(ti, p->path.dev);
652 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
654 dm_put_device(ti, p->path.dev);
665 static struct priority_group *parse_priority_group(struct arg_set *as,
668 static struct param _params[] = {
669 {1, 1024, "invalid number of paths"},
670 {0, 1024, "invalid number of selector args"}
674 unsigned i, nr_selector_args, nr_params;
675 struct priority_group *pg;
676 struct dm_target *ti = m->ti;
680 ti->error = "not enough priority group arguments";
681 return ERR_PTR(-EINVAL);
684 pg = alloc_priority_group();
686 ti->error = "couldn't allocate priority group";
687 return ERR_PTR(-ENOMEM);
691 r = parse_path_selector(as, pg, ti);
698 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
702 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
706 nr_params = 1 + nr_selector_args;
707 for (i = 0; i < pg->nr_pgpaths; i++) {
708 struct pgpath *pgpath;
709 struct arg_set path_args;
711 if (as->argc < nr_params) {
712 ti->error = "not enough path parameters";
717 path_args.argc = nr_params;
718 path_args.argv = as->argv;
720 pgpath = parse_path(&path_args, &pg->ps, ti);
721 if (IS_ERR(pgpath)) {
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)
742 struct dm_target *ti = m->ti;
744 static struct param _params[] = {
745 {0, 1024, "invalid number of hardware handler args"},
748 if (read_param(_params, shift(as), &hw_argc, &ti->error))
754 if (hw_argc > as->argc) {
755 ti->error = "not enough arguments for hardware handler";
759 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
760 request_module("scsi_dh_%s", m->hw_handler_name);
761 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
762 ti->error = "unknown hardware handler type";
771 for (i = 0; i <= hw_argc - 2; i++)
772 len += strlen(as->argv[i]) + 1;
773 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
775 ti->error = "memory allocation failed";
779 j = sprintf(p, "%d", hw_argc - 1);
780 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
781 j = sprintf(p, "%s", as->argv[i]);
783 consume(as, hw_argc - 1);
787 kfree(m->hw_handler_name);
788 m->hw_handler_name = NULL;
792 static int parse_features(struct arg_set *as, struct multipath *m)
796 struct dm_target *ti = m->ti;
797 const char *param_name;
799 static struct param _params[] = {
800 {0, 5, "invalid number of feature args"},
801 {1, 50, "pg_init_retries must be between 1 and 50"},
802 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
805 r = read_param(_params, shift(as), &argc, &ti->error);
813 param_name = shift(as);
816 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
817 r = queue_if_no_path(m, 1, 0);
821 if (!strnicmp(param_name, MESG_STR("no_partitions"))) {
822 m->features |= FEATURE_NO_PARTITIONS;
825 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
827 r = read_param(_params + 1, shift(as),
828 &m->pg_init_retries, &ti->error);
833 if (!strnicmp(param_name, MESG_STR("pg_init_delay_msecs")) &&
835 r = read_param(_params + 2, shift(as),
836 &m->pg_init_delay_msecs, &ti->error);
841 ti->error = "Unrecognised multipath feature request";
843 } while (argc && !r);
848 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
851 /* target parameters */
852 static struct param _params[] = {
853 {0, 1024, "invalid number of priority groups"},
854 {0, 1024, "invalid initial priority group number"},
860 unsigned pg_count = 0;
861 unsigned next_pg_num;
866 m = alloc_multipath(ti);
868 ti->error = "can't allocate multipath";
872 r = parse_features(&as, m);
876 r = parse_hw_handler(&as, m);
880 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
884 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
888 /* parse the priority groups */
890 struct priority_group *pg;
892 pg = parse_priority_group(&as, m);
898 m->nr_valid_paths += pg->nr_pgpaths;
899 list_add_tail(&pg->list, &m->priority_groups);
901 pg->pg_num = pg_count;
906 if (pg_count != m->nr_priority_groups) {
907 ti->error = "priority group count mismatch";
912 ti->num_flush_requests = 1;
913 ti->num_discard_requests = 1;
922 static void multipath_wait_for_pg_init_completion(struct multipath *m)
924 DECLARE_WAITQUEUE(wait, current);
927 add_wait_queue(&m->pg_init_wait, &wait);
930 set_current_state(TASK_UNINTERRUPTIBLE);
932 spin_lock_irqsave(&m->lock, flags);
933 if (!m->pg_init_in_progress) {
934 spin_unlock_irqrestore(&m->lock, flags);
937 spin_unlock_irqrestore(&m->lock, flags);
941 set_current_state(TASK_RUNNING);
943 remove_wait_queue(&m->pg_init_wait, &wait);
946 static void flush_multipath_work(struct multipath *m)
948 flush_workqueue(kmpath_handlerd);
949 multipath_wait_for_pg_init_completion(m);
950 flush_workqueue(kmultipathd);
951 flush_work_sync(&m->trigger_event);
954 static void multipath_dtr(struct dm_target *ti)
956 struct multipath *m = ti->private;
958 flush_multipath_work(m);
963 * Map cloned requests
965 static int multipath_map(struct dm_target *ti, struct request *clone,
966 union map_info *map_context)
969 struct dm_mpath_io *mpio;
970 struct multipath *m = (struct multipath *) ti->private;
972 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
974 /* ENOMEM, requeue */
975 return DM_MAPIO_REQUEUE;
976 memset(mpio, 0, sizeof(*mpio));
978 map_context->ptr = mpio;
979 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
980 /* Always attach a sense buffer */
982 clone->sense = mpio->sense;
983 r = map_io(m, clone, mpio, 0);
984 if (r < 0 || r == DM_MAPIO_REQUEUE)
985 mempool_free(mpio, m->mpio_pool);
991 * Take a path out of use.
993 static int fail_path(struct pgpath *pgpath)
996 struct multipath *m = pgpath->pg->m;
998 spin_lock_irqsave(&m->lock, flags);
1000 if (!pgpath->is_active)
1003 DMWARN("Failing path %s.", pgpath->path.dev->name);
1005 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1006 pgpath->is_active = 0;
1007 pgpath->fail_count++;
1009 m->nr_valid_paths--;
1011 if (pgpath == m->current_pgpath)
1012 m->current_pgpath = NULL;
1014 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1015 pgpath->path.dev->name, m->nr_valid_paths);
1017 schedule_work(&m->trigger_event);
1020 spin_unlock_irqrestore(&m->lock, flags);
1026 * Reinstate a previously-failed path
1028 static int reinstate_path(struct pgpath *pgpath)
1031 unsigned long flags;
1032 struct multipath *m = pgpath->pg->m;
1034 spin_lock_irqsave(&m->lock, flags);
1036 if (pgpath->is_active)
1039 if (!pgpath->pg->ps.type->reinstate_path) {
1040 DMWARN("Reinstate path not supported by path selector %s",
1041 pgpath->pg->ps.type->name);
1046 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1050 pgpath->is_active = 1;
1052 if (!m->nr_valid_paths++ && m->queue_size) {
1053 m->current_pgpath = NULL;
1054 queue_work(kmultipathd, &m->process_queued_ios);
1055 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1056 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1057 m->pg_init_in_progress++;
1060 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1061 pgpath->path.dev->name, m->nr_valid_paths);
1063 schedule_work(&m->trigger_event);
1066 spin_unlock_irqrestore(&m->lock, flags);
1072 * Fail or reinstate all paths that match the provided struct dm_dev.
1074 static int action_dev(struct multipath *m, struct dm_dev *dev,
1078 struct pgpath *pgpath;
1079 struct priority_group *pg;
1081 list_for_each_entry(pg, &m->priority_groups, list) {
1082 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1083 if (pgpath->path.dev == dev)
1092 * Temporarily try to avoid having to use the specified PG
1094 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1097 unsigned long flags;
1099 spin_lock_irqsave(&m->lock, flags);
1101 pg->bypassed = bypassed;
1102 m->current_pgpath = NULL;
1103 m->current_pg = NULL;
1105 spin_unlock_irqrestore(&m->lock, flags);
1107 schedule_work(&m->trigger_event);
1111 * Switch to using the specified PG from the next I/O that gets mapped
1113 static int switch_pg_num(struct multipath *m, const char *pgstr)
1115 struct priority_group *pg;
1117 unsigned long flags;
1119 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1120 (pgnum > m->nr_priority_groups)) {
1121 DMWARN("invalid PG number supplied to switch_pg_num");
1125 spin_lock_irqsave(&m->lock, flags);
1126 list_for_each_entry(pg, &m->priority_groups, list) {
1131 m->current_pgpath = NULL;
1132 m->current_pg = NULL;
1135 spin_unlock_irqrestore(&m->lock, flags);
1137 schedule_work(&m->trigger_event);
1142 * Set/clear bypassed status of a PG.
1143 * PGs are numbered upwards from 1 in the order they were declared.
1145 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1147 struct priority_group *pg;
1150 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1151 (pgnum > m->nr_priority_groups)) {
1152 DMWARN("invalid PG number supplied to bypass_pg");
1156 list_for_each_entry(pg, &m->priority_groups, list) {
1161 bypass_pg(m, pg, bypassed);
1166 * Should we retry pg_init immediately?
1168 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1170 unsigned long flags;
1171 int limit_reached = 0;
1173 spin_lock_irqsave(&m->lock, flags);
1175 if (m->pg_init_count <= m->pg_init_retries)
1176 m->pg_init_required = 1;
1180 spin_unlock_irqrestore(&m->lock, flags);
1182 return limit_reached;
1185 static void pg_init_done(void *data, int errors)
1187 struct pgpath *pgpath = data;
1188 struct priority_group *pg = pgpath->pg;
1189 struct multipath *m = pg->m;
1190 unsigned long flags;
1191 unsigned delay_retry = 0;
1193 /* device or driver problems */
1198 if (!m->hw_handler_name) {
1202 DMERR("Count not failover device %s: Handler scsi_dh_%s "
1203 "was not loaded.", pgpath->path.dev->name,
1204 m->hw_handler_name);
1206 * Fail path for now, so we do not ping pong
1210 case SCSI_DH_DEV_TEMP_BUSY:
1212 * Probably doing something like FW upgrade on the
1213 * controller so try the other pg.
1215 bypass_pg(m, pg, 1);
1217 case SCSI_DH_DEV_OFFLINED:
1218 DMWARN("Device %s offlined.", pgpath->path.dev->name);
1222 /* Wait before retrying. */
1224 case SCSI_DH_IMM_RETRY:
1225 case SCSI_DH_RES_TEMP_UNAVAIL:
1226 if (pg_init_limit_reached(m, pgpath))
1232 * We probably do not want to fail the path for a device
1233 * error, but this is what the old dm did. In future
1234 * patches we can do more advanced handling.
1239 spin_lock_irqsave(&m->lock, flags);
1241 if (pgpath == m->current_pgpath) {
1242 DMERR("Could not failover device %s, error %d.",
1243 pgpath->path.dev->name, errors);
1244 m->current_pgpath = NULL;
1245 m->current_pg = NULL;
1247 } else if (!m->pg_init_required)
1250 if (--m->pg_init_in_progress)
1251 /* Activations of other paths are still on going */
1254 if (!m->pg_init_required)
1257 m->pg_init_delay_retry = delay_retry;
1258 queue_work(kmultipathd, &m->process_queued_ios);
1261 * Wake up any thread waiting to suspend.
1263 wake_up(&m->pg_init_wait);
1266 spin_unlock_irqrestore(&m->lock, flags);
1269 static void activate_path(struct work_struct *work)
1271 struct pgpath *pgpath =
1272 container_of(work, struct pgpath, activate_path.work);
1274 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1275 pg_init_done, pgpath);
1279 * Evaluate scsi return code
1281 static int eval_scsi_error(int result, char *sense, int sense_len)
1283 struct scsi_sense_hdr sshdr;
1284 int r = DM_ENDIO_REQUEUE;
1286 if (host_byte(result) != DID_OK)
1289 if (msg_byte(result) != COMMAND_COMPLETE)
1292 if (status_byte(result) == RESERVATION_CONFLICT)
1293 /* Do not retry here, possible data corruption */
1296 #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
1297 if (status_byte(result) == CHECK_CONDITION &&
1298 !scsi_normalize_sense(sense, sense_len, &sshdr)) {
1300 switch (sshdr.sense_key) {
1305 case VOLUME_OVERFLOW:
1319 static int do_end_io(struct multipath *m, struct request *clone,
1320 int error, struct dm_mpath_io *mpio)
1323 * We don't queue any clone request inside the multipath target
1324 * during end I/O handling, since those clone requests don't have
1325 * bio clones. If we queue them inside the multipath target,
1326 * we need to make bio clones, that requires memory allocation.
1327 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1328 * don't have bio clones.)
1329 * Instead of queueing the clone request here, we queue the original
1330 * request into dm core, which will remake a clone request and
1331 * clone bios for it and resubmit it later.
1333 int r = DM_ENDIO_REQUEUE;
1334 unsigned long flags;
1336 if (!error && !clone->errors)
1337 return 0; /* I/O complete */
1339 if (error == -EOPNOTSUPP)
1342 r = eval_scsi_error(clone->errors, clone->sense, clone->sense_len);
1343 if (r != DM_ENDIO_REQUEUE)
1346 if (clone->cmd_flags & REQ_DISCARD)
1348 * Pass all discard request failures up.
1349 * FIXME: only fail_path if the discard failed due to a
1350 * transport problem. This requires precise understanding
1351 * of the underlying failure (e.g. the SCSI sense).
1356 fail_path(mpio->pgpath);
1358 spin_lock_irqsave(&m->lock, flags);
1359 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1361 spin_unlock_irqrestore(&m->lock, flags);
1366 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1367 int error, union map_info *map_context)
1369 struct multipath *m = ti->private;
1370 struct dm_mpath_io *mpio = map_context->ptr;
1371 struct pgpath *pgpath = mpio->pgpath;
1372 struct path_selector *ps;
1375 r = do_end_io(m, clone, error, mpio);
1377 ps = &pgpath->pg->ps;
1378 if (ps->type->end_io)
1379 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1381 if (clone->sense == mpio->sense) {
1382 clone->sense = NULL;
1383 clone->sense_len = 0;
1385 mempool_free(mpio, m->mpio_pool);
1391 * Suspend can't complete until all the I/O is processed so if
1392 * the last path fails we must error any remaining I/O.
1393 * Note that if the freeze_bdev fails while suspending, the
1394 * queue_if_no_path state is lost - userspace should reset it.
1396 static void multipath_presuspend(struct dm_target *ti)
1398 struct multipath *m = (struct multipath *) ti->private;
1400 queue_if_no_path(m, 0, 1);
1403 static void multipath_postsuspend(struct dm_target *ti)
1405 struct multipath *m = ti->private;
1407 mutex_lock(&m->work_mutex);
1408 flush_multipath_work(m);
1409 mutex_unlock(&m->work_mutex);
1413 * Restore the queue_if_no_path setting.
1415 static void multipath_resume(struct dm_target *ti)
1417 struct multipath *m = (struct multipath *) ti->private;
1418 unsigned long flags;
1420 spin_lock_irqsave(&m->lock, flags);
1421 m->queue_if_no_path = m->saved_queue_if_no_path;
1422 spin_unlock_irqrestore(&m->lock, flags);
1426 * Info output has the following format:
1427 * num_multipath_feature_args [multipath_feature_args]*
1428 * num_handler_status_args [handler_status_args]*
1429 * num_groups init_group_number
1430 * [A|D|E num_ps_status_args [ps_status_args]*
1431 * num_paths num_selector_args
1432 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1434 * Table output has the following format (identical to the constructor string):
1435 * num_feature_args [features_args]*
1436 * num_handler_args hw_handler [hw_handler_args]*
1437 * num_groups init_group_number
1438 * [priority selector-name num_ps_args [ps_args]*
1439 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1441 static int multipath_status(struct dm_target *ti, status_type_t type,
1442 char *result, unsigned int maxlen)
1445 unsigned long flags;
1446 struct multipath *m = (struct multipath *) ti->private;
1447 struct priority_group *pg;
1452 spin_lock_irqsave(&m->lock, flags);
1455 if (type == STATUSTYPE_INFO)
1456 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1458 DMEMIT("%u ", m->queue_if_no_path +
1459 (m->pg_init_retries > 0) * 2 +
1460 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1461 (m->features & FEATURE_NO_PARTITIONS));
1462 if (m->queue_if_no_path)
1463 DMEMIT("queue_if_no_path ");
1464 if (m->pg_init_retries)
1465 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1466 if (m->features & FEATURE_NO_PARTITIONS)
1467 DMEMIT("no_partitions ");
1468 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1469 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1472 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1475 DMEMIT("1 %s ", m->hw_handler_name);
1477 DMEMIT("%u ", m->nr_priority_groups);
1480 pg_num = m->next_pg->pg_num;
1481 else if (m->current_pg)
1482 pg_num = m->current_pg->pg_num;
1486 DMEMIT("%u ", pg_num);
1489 case STATUSTYPE_INFO:
1490 list_for_each_entry(pg, &m->priority_groups, list) {
1492 state = 'D'; /* Disabled */
1493 else if (pg == m->current_pg)
1494 state = 'A'; /* Currently Active */
1496 state = 'E'; /* Enabled */
1498 DMEMIT("%c ", state);
1500 if (pg->ps.type->status)
1501 sz += pg->ps.type->status(&pg->ps, NULL, type,
1507 DMEMIT("%u %u ", pg->nr_pgpaths,
1508 pg->ps.type->info_args);
1510 list_for_each_entry(p, &pg->pgpaths, list) {
1511 DMEMIT("%s %s %u ", p->path.dev->name,
1512 p->is_active ? "A" : "F",
1514 if (pg->ps.type->status)
1515 sz += pg->ps.type->status(&pg->ps,
1516 &p->path, type, result + sz,
1522 case STATUSTYPE_TABLE:
1523 list_for_each_entry(pg, &m->priority_groups, list) {
1524 DMEMIT("%s ", pg->ps.type->name);
1526 if (pg->ps.type->status)
1527 sz += pg->ps.type->status(&pg->ps, NULL, type,
1533 DMEMIT("%u %u ", pg->nr_pgpaths,
1534 pg->ps.type->table_args);
1536 list_for_each_entry(p, &pg->pgpaths, list) {
1537 DMEMIT("%s ", p->path.dev->name);
1538 if (pg->ps.type->status)
1539 sz += pg->ps.type->status(&pg->ps,
1540 &p->path, type, result + sz,
1547 spin_unlock_irqrestore(&m->lock, flags);
1552 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1556 struct multipath *m = (struct multipath *) ti->private;
1559 mutex_lock(&m->work_mutex);
1561 if (dm_suspended(ti)) {
1567 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1568 r = queue_if_no_path(m, 1, 0);
1570 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1571 r = queue_if_no_path(m, 0, 0);
1577 DMWARN("Unrecognised multipath message received.");
1581 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1582 r = bypass_pg_num(m, argv[1], 1);
1584 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1585 r = bypass_pg_num(m, argv[1], 0);
1587 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1588 r = switch_pg_num(m, argv[1]);
1590 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1591 action = reinstate_path;
1592 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1595 DMWARN("Unrecognised multipath message received.");
1599 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1601 DMWARN("message: error getting device %s",
1606 r = action_dev(m, dev, action);
1608 dm_put_device(ti, dev);
1611 mutex_unlock(&m->work_mutex);
1615 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1618 struct multipath *m = (struct multipath *) ti->private;
1619 struct block_device *bdev = NULL;
1621 unsigned long flags;
1624 spin_lock_irqsave(&m->lock, flags);
1626 if (!m->current_pgpath)
1627 __choose_pgpath(m, 0);
1629 if (m->current_pgpath) {
1630 bdev = m->current_pgpath->path.dev->bdev;
1631 mode = m->current_pgpath->path.dev->mode;
1639 spin_unlock_irqrestore(&m->lock, flags);
1641 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1644 static int multipath_iterate_devices(struct dm_target *ti,
1645 iterate_devices_callout_fn fn, void *data)
1647 struct multipath *m = ti->private;
1648 struct priority_group *pg;
1652 list_for_each_entry(pg, &m->priority_groups, list) {
1653 list_for_each_entry(p, &pg->pgpaths, list) {
1654 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1664 static int __pgpath_busy(struct pgpath *pgpath)
1666 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1668 return dm_underlying_device_busy(q);
1672 * We return "busy", only when we can map I/Os but underlying devices
1673 * are busy (so even if we map I/Os now, the I/Os will wait on
1674 * the underlying queue).
1675 * In other words, if we want to kill I/Os or queue them inside us
1676 * due to map unavailability, we don't return "busy". Otherwise,
1677 * dm core won't give us the I/Os and we can't do what we want.
1679 static int multipath_busy(struct dm_target *ti)
1681 int busy = 0, has_active = 0;
1682 struct multipath *m = ti->private;
1683 struct priority_group *pg;
1684 struct pgpath *pgpath;
1685 unsigned long flags;
1687 spin_lock_irqsave(&m->lock, flags);
1689 /* Guess which priority_group will be used at next mapping time */
1690 if (unlikely(!m->current_pgpath && m->next_pg))
1692 else if (likely(m->current_pg))
1696 * We don't know which pg will be used at next mapping time.
1697 * We don't call __choose_pgpath() here to avoid to trigger
1698 * pg_init just by busy checking.
1699 * So we don't know whether underlying devices we will be using
1700 * at next mapping time are busy or not. Just try mapping.
1705 * If there is one non-busy active path at least, the path selector
1706 * will be able to select it. So we consider such a pg as not busy.
1709 list_for_each_entry(pgpath, &pg->pgpaths, list)
1710 if (pgpath->is_active) {
1713 if (!__pgpath_busy(pgpath)) {
1721 * No active path in this pg, so this pg won't be used and
1722 * the current_pg will be changed at next mapping time.
1723 * We need to try mapping to determine it.
1728 spin_unlock_irqrestore(&m->lock, flags);
1733 /*-----------------------------------------------------------------
1735 *---------------------------------------------------------------*/
1736 static struct target_type multipath_target = {
1737 .name = "multipath",
1738 .version = {1, 2, 0},
1739 .module = THIS_MODULE,
1740 .ctr = multipath_ctr,
1741 .dtr = multipath_dtr,
1742 .map_rq = multipath_map,
1743 .rq_end_io = multipath_end_io,
1744 .presuspend = multipath_presuspend,
1745 .postsuspend = multipath_postsuspend,
1746 .resume = multipath_resume,
1747 .status = multipath_status,
1748 .message = multipath_message,
1749 .ioctl = multipath_ioctl,
1750 .iterate_devices = multipath_iterate_devices,
1751 .busy = multipath_busy,
1754 static int __init dm_multipath_init(void)
1758 /* allocate a slab for the dm_ios */
1759 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1763 r = dm_register_target(&multipath_target);
1765 DMERR("register failed %d", r);
1766 kmem_cache_destroy(_mpio_cache);
1770 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1772 DMERR("failed to create workqueue kmpathd");
1773 dm_unregister_target(&multipath_target);
1774 kmem_cache_destroy(_mpio_cache);
1779 * A separate workqueue is used to handle the device handlers
1780 * to avoid overloading existing workqueue. Overloading the
1781 * old workqueue would also create a bottleneck in the
1782 * path of the storage hardware device activation.
1784 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1786 if (!kmpath_handlerd) {
1787 DMERR("failed to create workqueue kmpath_handlerd");
1788 destroy_workqueue(kmultipathd);
1789 dm_unregister_target(&multipath_target);
1790 kmem_cache_destroy(_mpio_cache);
1794 DMINFO("version %u.%u.%u loaded",
1795 multipath_target.version[0], multipath_target.version[1],
1796 multipath_target.version[2]);
1801 static void __exit dm_multipath_exit(void)
1803 destroy_workqueue(kmpath_handlerd);
1804 destroy_workqueue(kmultipathd);
1806 dm_unregister_target(&multipath_target);
1807 kmem_cache_destroy(_mpio_cache);
1810 module_init(dm_multipath_init);
1811 module_exit(dm_multipath_exit);
1813 MODULE_DESCRIPTION(DM_NAME " multipath target");
1814 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1815 MODULE_LICENSE("GPL");