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 */
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
81 unsigned saved_queue_if_no_path;/* Saved state during suspension */
82 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
85 struct work_struct process_queued_ios;
86 struct list_head queued_ios;
89 struct work_struct trigger_event;
92 * We must use a mempool of dm_mpath_io structs so that we
93 * can resubmit bios on error.
99 * Context information attached to each bio we process.
102 struct pgpath *pgpath;
106 typedef int (*action_fn) (struct pgpath *pgpath);
108 #define MIN_IOS 256 /* Mempool size */
110 static struct kmem_cache *_mpio_cache;
112 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113 static void process_queued_ios(struct work_struct *work);
114 static void trigger_event(struct work_struct *work);
115 static void activate_path(struct work_struct *work);
116 static void deactivate_path(struct work_struct *work);
119 /*-----------------------------------------------
120 * Allocation routines
121 *-----------------------------------------------*/
123 static struct pgpath *alloc_pgpath(void)
125 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
128 pgpath->is_active = 1;
129 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
130 INIT_WORK(&pgpath->activate_path, activate_path);
136 static void free_pgpath(struct pgpath *pgpath)
141 static void deactivate_path(struct work_struct *work)
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
149 static struct priority_group *alloc_priority_group(void)
151 struct priority_group *pg;
153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
156 INIT_LIST_HEAD(&pg->pgpaths);
161 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163 struct pgpath *pgpath, *tmp;
164 struct multipath *m = ti->private;
166 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
167 list_del(&pgpath->list);
168 dm_put_device(ti, pgpath->path.dev);
173 static void free_priority_group(struct priority_group *pg,
174 struct dm_target *ti)
176 struct path_selector *ps = &pg->ps;
179 ps->type->destroy(ps);
180 dm_put_path_selector(ps->type);
183 free_pgpaths(&pg->pgpaths, ti);
187 static struct multipath *alloc_multipath(struct dm_target *ti)
191 m = kzalloc(sizeof(*m), GFP_KERNEL);
193 INIT_LIST_HEAD(&m->priority_groups);
194 INIT_LIST_HEAD(&m->queued_ios);
195 spin_lock_init(&m->lock);
197 INIT_WORK(&m->process_queued_ios, process_queued_ios);
198 INIT_WORK(&m->trigger_event, trigger_event);
199 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
211 static void free_multipath(struct multipath *m)
213 struct priority_group *pg, *tmp;
215 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
217 free_priority_group(pg, m->ti);
220 kfree(m->hw_handler_name);
221 kfree(m->hw_handler_params);
222 mempool_destroy(m->mpio_pool);
227 /*-----------------------------------------------
229 *-----------------------------------------------*/
231 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
233 m->current_pg = pgpath->pg;
235 /* Must we initialise the PG first, and queue I/O till it's ready? */
236 if (m->hw_handler_name) {
237 m->pg_init_required = 1;
240 m->pg_init_required = 0;
244 m->pg_init_count = 0;
247 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
250 struct dm_path *path;
252 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
256 m->current_pgpath = path_to_pgpath(path);
258 if (m->current_pg != pg)
259 __switch_pg(m, m->current_pgpath);
264 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
266 struct priority_group *pg;
267 unsigned bypassed = 1;
269 if (!m->nr_valid_paths)
272 /* Were we instructed to switch PG? */
276 if (!__choose_path_in_pg(m, pg, nr_bytes))
280 /* Don't change PG until it has no remaining paths */
281 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
285 * Loop through priority groups until we find a valid path.
286 * First time we skip PGs marked 'bypassed'.
287 * Second time we only try the ones we skipped.
290 list_for_each_entry(pg, &m->priority_groups, list) {
291 if (pg->bypassed == bypassed)
293 if (!__choose_path_in_pg(m, pg, nr_bytes))
296 } while (bypassed--);
299 m->current_pgpath = NULL;
300 m->current_pg = NULL;
304 * Check whether bios must be queued in the device-mapper core rather
305 * than here in the target.
307 * m->lock must be held on entry.
309 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
310 * same value then we are not between multipath_presuspend()
311 * and multipath_resume() calls and we have no need to check
312 * for the DMF_NOFLUSH_SUSPENDING flag.
314 static int __must_push_back(struct multipath *m)
316 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
317 dm_noflush_suspending(m->ti));
320 static int map_io(struct multipath *m, struct request *clone,
321 struct dm_mpath_io *mpio, unsigned was_queued)
323 int r = DM_MAPIO_REMAPPED;
324 size_t nr_bytes = blk_rq_bytes(clone);
326 struct pgpath *pgpath;
327 struct block_device *bdev;
329 spin_lock_irqsave(&m->lock, flags);
331 /* Do we need to select a new pgpath? */
332 if (!m->current_pgpath ||
333 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
334 __choose_pgpath(m, nr_bytes);
336 pgpath = m->current_pgpath;
341 if ((pgpath && m->queue_io) ||
342 (!pgpath && m->queue_if_no_path)) {
343 /* Queue for the daemon to resubmit */
344 list_add_tail(&clone->queuelist, &m->queued_ios);
346 if ((m->pg_init_required && !m->pg_init_in_progress) ||
348 queue_work(kmultipathd, &m->process_queued_ios);
350 r = DM_MAPIO_SUBMITTED;
352 bdev = pgpath->path.dev->bdev;
353 clone->q = bdev_get_queue(bdev);
354 clone->rq_disk = bdev->bd_disk;
355 } else if (__must_push_back(m))
356 r = DM_MAPIO_REQUEUE;
358 r = -EIO; /* Failed */
360 mpio->pgpath = pgpath;
361 mpio->nr_bytes = nr_bytes;
363 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
364 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
367 spin_unlock_irqrestore(&m->lock, flags);
373 * If we run out of usable paths, should we queue I/O or error it?
375 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
376 unsigned save_old_value)
380 spin_lock_irqsave(&m->lock, flags);
383 m->saved_queue_if_no_path = m->queue_if_no_path;
385 m->saved_queue_if_no_path = queue_if_no_path;
386 m->queue_if_no_path = queue_if_no_path;
387 if (!m->queue_if_no_path && m->queue_size)
388 queue_work(kmultipathd, &m->process_queued_ios);
390 spin_unlock_irqrestore(&m->lock, flags);
395 /*-----------------------------------------------------------------
396 * The multipath daemon is responsible for resubmitting queued ios.
397 *---------------------------------------------------------------*/
399 static void dispatch_queued_ios(struct multipath *m)
403 struct dm_mpath_io *mpio;
404 union map_info *info;
405 struct request *clone, *n;
408 spin_lock_irqsave(&m->lock, flags);
409 list_splice_init(&m->queued_ios, &cl);
410 spin_unlock_irqrestore(&m->lock, flags);
412 list_for_each_entry_safe(clone, n, &cl, queuelist) {
413 list_del_init(&clone->queuelist);
415 info = dm_get_rq_mapinfo(clone);
418 r = map_io(m, clone, mpio, 1);
420 mempool_free(mpio, m->mpio_pool);
421 dm_kill_unmapped_request(clone, r);
422 } else if (r == DM_MAPIO_REMAPPED)
423 dm_dispatch_request(clone);
424 else if (r == DM_MAPIO_REQUEUE) {
425 mempool_free(mpio, m->mpio_pool);
426 dm_requeue_unmapped_request(clone);
431 static void process_queued_ios(struct work_struct *work)
433 struct multipath *m =
434 container_of(work, struct multipath, process_queued_ios);
435 struct pgpath *pgpath = NULL, *tmp;
436 unsigned must_queue = 1;
439 spin_lock_irqsave(&m->lock, flags);
444 if (!m->current_pgpath)
445 __choose_pgpath(m, 0);
447 pgpath = m->current_pgpath;
449 if ((pgpath && !m->queue_io) ||
450 (!pgpath && !m->queue_if_no_path))
453 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
455 m->pg_init_required = 0;
456 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
457 if (queue_work(kmpath_handlerd, &tmp->activate_path))
458 m->pg_init_in_progress++;
462 spin_unlock_irqrestore(&m->lock, flags);
464 dispatch_queued_ios(m);
468 * An event is triggered whenever a path is taken out of use.
469 * Includes path failure and PG bypass.
471 static void trigger_event(struct work_struct *work)
473 struct multipath *m =
474 container_of(work, struct multipath, trigger_event);
476 dm_table_event(m->ti->table);
479 /*-----------------------------------------------------------------
480 * Constructor/argument parsing:
481 * <#multipath feature args> [<arg>]*
482 * <#hw_handler args> [hw_handler [<arg>]*]
484 * <initial priority group>
485 * [<selector> <#selector args> [<arg>]*
486 * <#paths> <#per-path selector args>
487 * [<path> [<arg>]* ]+ ]+
488 *---------------------------------------------------------------*/
495 static int read_param(struct param *param, char *str, unsigned *v, char **error)
498 (sscanf(str, "%u", v) != 1) ||
501 *error = param->error;
513 static char *shift(struct arg_set *as)
527 static void consume(struct arg_set *as, unsigned n)
529 BUG_ON (as->argc < n);
534 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
535 struct dm_target *ti)
538 struct path_selector_type *pst;
541 static struct param _params[] = {
542 {0, 1024, "invalid number of path selector args"},
545 pst = dm_get_path_selector(shift(as));
547 ti->error = "unknown path selector type";
551 r = read_param(_params, shift(as), &ps_argc, &ti->error);
553 dm_put_path_selector(pst);
557 if (ps_argc > as->argc) {
558 dm_put_path_selector(pst);
559 ti->error = "not enough arguments for path selector";
563 r = pst->create(&pg->ps, ps_argc, as->argv);
565 dm_put_path_selector(pst);
566 ti->error = "path selector constructor failed";
571 consume(as, ps_argc);
576 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
577 struct dm_target *ti)
581 struct multipath *m = ti->private;
583 /* we need at least a path arg */
585 ti->error = "no device given";
586 return ERR_PTR(-EINVAL);
591 return ERR_PTR(-ENOMEM);
593 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
594 dm_table_get_mode(ti->table), &p->path.dev);
596 ti->error = "error getting device";
600 if (m->hw_handler_name) {
601 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
603 r = scsi_dh_attach(q, m->hw_handler_name);
606 * Already attached to different hw_handler,
607 * try to reattach with correct one.
610 r = scsi_dh_attach(q, m->hw_handler_name);
614 ti->error = "error attaching hardware handler";
615 dm_put_device(ti, p->path.dev);
619 if (m->hw_handler_params) {
620 r = scsi_dh_set_params(q, m->hw_handler_params);
622 ti->error = "unable to set hardware "
623 "handler parameters";
625 dm_put_device(ti, p->path.dev);
631 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
633 dm_put_device(ti, p->path.dev);
644 static struct priority_group *parse_priority_group(struct arg_set *as,
647 static struct param _params[] = {
648 {1, 1024, "invalid number of paths"},
649 {0, 1024, "invalid number of selector args"}
653 unsigned i, nr_selector_args, nr_params;
654 struct priority_group *pg;
655 struct dm_target *ti = m->ti;
659 ti->error = "not enough priority group arguments";
660 return ERR_PTR(-EINVAL);
663 pg = alloc_priority_group();
665 ti->error = "couldn't allocate priority group";
666 return ERR_PTR(-ENOMEM);
670 r = parse_path_selector(as, pg, ti);
677 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
681 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
685 nr_params = 1 + nr_selector_args;
686 for (i = 0; i < pg->nr_pgpaths; i++) {
687 struct pgpath *pgpath;
688 struct arg_set path_args;
690 if (as->argc < nr_params) {
691 ti->error = "not enough path parameters";
695 path_args.argc = nr_params;
696 path_args.argv = as->argv;
698 pgpath = parse_path(&path_args, &pg->ps, ti);
699 if (IS_ERR(pgpath)) {
705 list_add_tail(&pgpath->list, &pg->pgpaths);
706 consume(as, nr_params);
712 free_priority_group(pg, ti);
716 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
720 struct dm_target *ti = m->ti;
722 static struct param _params[] = {
723 {0, 1024, "invalid number of hardware handler args"},
726 if (read_param(_params, shift(as), &hw_argc, &ti->error))
732 if (hw_argc > as->argc) {
733 ti->error = "not enough arguments for hardware handler";
737 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
738 request_module("scsi_dh_%s", m->hw_handler_name);
739 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
740 ti->error = "unknown hardware handler type";
749 for (i = 0; i <= hw_argc - 2; i++)
750 len += strlen(as->argv[i]) + 1;
751 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
753 ti->error = "memory allocation failed";
757 j = sprintf(p, "%d", hw_argc - 1);
758 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
759 j = sprintf(p, "%s", as->argv[i]);
761 consume(as, hw_argc - 1);
765 kfree(m->hw_handler_name);
766 m->hw_handler_name = NULL;
770 static int parse_features(struct arg_set *as, struct multipath *m)
774 struct dm_target *ti = m->ti;
775 const char *param_name;
777 static struct param _params[] = {
778 {0, 3, "invalid number of feature args"},
779 {1, 50, "pg_init_retries must be between 1 and 50"},
782 r = read_param(_params, shift(as), &argc, &ti->error);
790 param_name = shift(as);
793 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
794 r = queue_if_no_path(m, 1, 0);
798 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
800 r = read_param(_params + 1, shift(as),
801 &m->pg_init_retries, &ti->error);
806 ti->error = "Unrecognised multipath feature request";
808 } while (argc && !r);
813 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
816 /* target parameters */
817 static struct param _params[] = {
818 {0, 1024, "invalid number of priority groups"},
819 {0, 1024, "invalid initial priority group number"},
825 unsigned pg_count = 0;
826 unsigned next_pg_num;
831 m = alloc_multipath(ti);
833 ti->error = "can't allocate multipath";
837 r = parse_features(&as, m);
841 r = parse_hw_handler(&as, m);
845 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
849 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
853 /* parse the priority groups */
855 struct priority_group *pg;
857 pg = parse_priority_group(&as, m);
863 m->nr_valid_paths += pg->nr_pgpaths;
864 list_add_tail(&pg->list, &m->priority_groups);
866 pg->pg_num = pg_count;
871 if (pg_count != m->nr_priority_groups) {
872 ti->error = "priority group count mismatch";
877 ti->num_flush_requests = 1;
886 static void multipath_dtr(struct dm_target *ti)
888 struct multipath *m = (struct multipath *) ti->private;
890 flush_workqueue(kmpath_handlerd);
891 flush_workqueue(kmultipathd);
892 flush_scheduled_work();
897 * Map cloned requests
899 static int multipath_map(struct dm_target *ti, struct request *clone,
900 union map_info *map_context)
903 struct dm_mpath_io *mpio;
904 struct multipath *m = (struct multipath *) ti->private;
906 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
908 /* ENOMEM, requeue */
909 return DM_MAPIO_REQUEUE;
910 memset(mpio, 0, sizeof(*mpio));
912 map_context->ptr = mpio;
913 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
914 r = map_io(m, clone, mpio, 0);
915 if (r < 0 || r == DM_MAPIO_REQUEUE)
916 mempool_free(mpio, m->mpio_pool);
922 * Take a path out of use.
924 static int fail_path(struct pgpath *pgpath)
927 struct multipath *m = pgpath->pg->m;
929 spin_lock_irqsave(&m->lock, flags);
931 if (!pgpath->is_active)
934 DMWARN("Failing path %s.", pgpath->path.dev->name);
936 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
937 pgpath->is_active = 0;
938 pgpath->fail_count++;
942 if (pgpath == m->current_pgpath)
943 m->current_pgpath = NULL;
945 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
946 pgpath->path.dev->name, m->nr_valid_paths);
948 schedule_work(&m->trigger_event);
949 queue_work(kmultipathd, &pgpath->deactivate_path);
952 spin_unlock_irqrestore(&m->lock, flags);
958 * Reinstate a previously-failed path
960 static int reinstate_path(struct pgpath *pgpath)
964 struct multipath *m = pgpath->pg->m;
966 spin_lock_irqsave(&m->lock, flags);
968 if (pgpath->is_active)
971 if (!pgpath->pg->ps.type->reinstate_path) {
972 DMWARN("Reinstate path not supported by path selector %s",
973 pgpath->pg->ps.type->name);
978 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
982 pgpath->is_active = 1;
984 if (!m->nr_valid_paths++ && m->queue_size) {
985 m->current_pgpath = NULL;
986 queue_work(kmultipathd, &m->process_queued_ios);
987 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
988 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
989 m->pg_init_in_progress++;
992 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
993 pgpath->path.dev->name, m->nr_valid_paths);
995 schedule_work(&m->trigger_event);
998 spin_unlock_irqrestore(&m->lock, flags);
1004 * Fail or reinstate all paths that match the provided struct dm_dev.
1006 static int action_dev(struct multipath *m, struct dm_dev *dev,
1010 struct pgpath *pgpath;
1011 struct priority_group *pg;
1013 list_for_each_entry(pg, &m->priority_groups, list) {
1014 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1015 if (pgpath->path.dev == dev)
1024 * Temporarily try to avoid having to use the specified PG
1026 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1029 unsigned long flags;
1031 spin_lock_irqsave(&m->lock, flags);
1033 pg->bypassed = bypassed;
1034 m->current_pgpath = NULL;
1035 m->current_pg = NULL;
1037 spin_unlock_irqrestore(&m->lock, flags);
1039 schedule_work(&m->trigger_event);
1043 * Switch to using the specified PG from the next I/O that gets mapped
1045 static int switch_pg_num(struct multipath *m, const char *pgstr)
1047 struct priority_group *pg;
1049 unsigned long flags;
1051 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1052 (pgnum > m->nr_priority_groups)) {
1053 DMWARN("invalid PG number supplied to switch_pg_num");
1057 spin_lock_irqsave(&m->lock, flags);
1058 list_for_each_entry(pg, &m->priority_groups, list) {
1063 m->current_pgpath = NULL;
1064 m->current_pg = NULL;
1067 spin_unlock_irqrestore(&m->lock, flags);
1069 schedule_work(&m->trigger_event);
1074 * Set/clear bypassed status of a PG.
1075 * PGs are numbered upwards from 1 in the order they were declared.
1077 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1079 struct priority_group *pg;
1082 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1083 (pgnum > m->nr_priority_groups)) {
1084 DMWARN("invalid PG number supplied to bypass_pg");
1088 list_for_each_entry(pg, &m->priority_groups, list) {
1093 bypass_pg(m, pg, bypassed);
1098 * Should we retry pg_init immediately?
1100 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1102 unsigned long flags;
1103 int limit_reached = 0;
1105 spin_lock_irqsave(&m->lock, flags);
1107 if (m->pg_init_count <= m->pg_init_retries)
1108 m->pg_init_required = 1;
1112 spin_unlock_irqrestore(&m->lock, flags);
1114 return limit_reached;
1117 static void pg_init_done(struct dm_path *path, int errors)
1119 struct pgpath *pgpath = path_to_pgpath(path);
1120 struct priority_group *pg = pgpath->pg;
1121 struct multipath *m = pg->m;
1122 unsigned long flags;
1124 /* device or driver problems */
1129 if (!m->hw_handler_name) {
1133 DMERR("Cannot failover device because scsi_dh_%s was not "
1134 "loaded.", m->hw_handler_name);
1136 * Fail path for now, so we do not ping pong
1140 case SCSI_DH_DEV_TEMP_BUSY:
1142 * Probably doing something like FW upgrade on the
1143 * controller so try the other pg.
1145 bypass_pg(m, pg, 1);
1147 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1149 case SCSI_DH_IMM_RETRY:
1150 case SCSI_DH_RES_TEMP_UNAVAIL:
1151 if (pg_init_limit_reached(m, pgpath))
1157 * We probably do not want to fail the path for a device
1158 * error, but this is what the old dm did. In future
1159 * patches we can do more advanced handling.
1164 spin_lock_irqsave(&m->lock, flags);
1166 if (pgpath == m->current_pgpath) {
1167 DMERR("Could not failover device. Error %d.", errors);
1168 m->current_pgpath = NULL;
1169 m->current_pg = NULL;
1171 } else if (!m->pg_init_required) {
1176 m->pg_init_in_progress--;
1177 if (!m->pg_init_in_progress)
1178 queue_work(kmultipathd, &m->process_queued_ios);
1179 spin_unlock_irqrestore(&m->lock, flags);
1182 static void activate_path(struct work_struct *work)
1185 struct pgpath *pgpath =
1186 container_of(work, struct pgpath, activate_path);
1188 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1189 pg_init_done(&pgpath->path, ret);
1195 static int do_end_io(struct multipath *m, struct request *clone,
1196 int error, struct dm_mpath_io *mpio)
1199 * We don't queue any clone request inside the multipath target
1200 * during end I/O handling, since those clone requests don't have
1201 * bio clones. If we queue them inside the multipath target,
1202 * we need to make bio clones, that requires memory allocation.
1203 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1204 * don't have bio clones.)
1205 * Instead of queueing the clone request here, we queue the original
1206 * request into dm core, which will remake a clone request and
1207 * clone bios for it and resubmit it later.
1209 int r = DM_ENDIO_REQUEUE;
1210 unsigned long flags;
1212 if (!error && !clone->errors)
1213 return 0; /* I/O complete */
1215 if (error == -EOPNOTSUPP)
1219 fail_path(mpio->pgpath);
1221 spin_lock_irqsave(&m->lock, flags);
1222 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1224 spin_unlock_irqrestore(&m->lock, flags);
1229 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1230 int error, union map_info *map_context)
1232 struct multipath *m = ti->private;
1233 struct dm_mpath_io *mpio = map_context->ptr;
1234 struct pgpath *pgpath = mpio->pgpath;
1235 struct path_selector *ps;
1238 r = do_end_io(m, clone, error, mpio);
1240 ps = &pgpath->pg->ps;
1241 if (ps->type->end_io)
1242 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1244 mempool_free(mpio, m->mpio_pool);
1250 * Suspend can't complete until all the I/O is processed so if
1251 * the last path fails we must error any remaining I/O.
1252 * Note that if the freeze_bdev fails while suspending, the
1253 * queue_if_no_path state is lost - userspace should reset it.
1255 static void multipath_presuspend(struct dm_target *ti)
1257 struct multipath *m = (struct multipath *) ti->private;
1259 queue_if_no_path(m, 0, 1);
1263 * Restore the queue_if_no_path setting.
1265 static void multipath_resume(struct dm_target *ti)
1267 struct multipath *m = (struct multipath *) ti->private;
1268 unsigned long flags;
1270 spin_lock_irqsave(&m->lock, flags);
1271 m->queue_if_no_path = m->saved_queue_if_no_path;
1272 spin_unlock_irqrestore(&m->lock, flags);
1276 * Info output has the following format:
1277 * num_multipath_feature_args [multipath_feature_args]*
1278 * num_handler_status_args [handler_status_args]*
1279 * num_groups init_group_number
1280 * [A|D|E num_ps_status_args [ps_status_args]*
1281 * num_paths num_selector_args
1282 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1284 * Table output has the following format (identical to the constructor string):
1285 * num_feature_args [features_args]*
1286 * num_handler_args hw_handler [hw_handler_args]*
1287 * num_groups init_group_number
1288 * [priority selector-name num_ps_args [ps_args]*
1289 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1291 static int multipath_status(struct dm_target *ti, status_type_t type,
1292 char *result, unsigned int maxlen)
1295 unsigned long flags;
1296 struct multipath *m = (struct multipath *) ti->private;
1297 struct priority_group *pg;
1302 spin_lock_irqsave(&m->lock, flags);
1305 if (type == STATUSTYPE_INFO)
1306 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1308 DMEMIT("%u ", m->queue_if_no_path +
1309 (m->pg_init_retries > 0) * 2);
1310 if (m->queue_if_no_path)
1311 DMEMIT("queue_if_no_path ");
1312 if (m->pg_init_retries)
1313 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1316 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1319 DMEMIT("1 %s ", m->hw_handler_name);
1321 DMEMIT("%u ", m->nr_priority_groups);
1324 pg_num = m->next_pg->pg_num;
1325 else if (m->current_pg)
1326 pg_num = m->current_pg->pg_num;
1330 DMEMIT("%u ", pg_num);
1333 case STATUSTYPE_INFO:
1334 list_for_each_entry(pg, &m->priority_groups, list) {
1336 state = 'D'; /* Disabled */
1337 else if (pg == m->current_pg)
1338 state = 'A'; /* Currently Active */
1340 state = 'E'; /* Enabled */
1342 DMEMIT("%c ", state);
1344 if (pg->ps.type->status)
1345 sz += pg->ps.type->status(&pg->ps, NULL, type,
1351 DMEMIT("%u %u ", pg->nr_pgpaths,
1352 pg->ps.type->info_args);
1354 list_for_each_entry(p, &pg->pgpaths, list) {
1355 DMEMIT("%s %s %u ", p->path.dev->name,
1356 p->is_active ? "A" : "F",
1358 if (pg->ps.type->status)
1359 sz += pg->ps.type->status(&pg->ps,
1360 &p->path, type, result + sz,
1366 case STATUSTYPE_TABLE:
1367 list_for_each_entry(pg, &m->priority_groups, list) {
1368 DMEMIT("%s ", pg->ps.type->name);
1370 if (pg->ps.type->status)
1371 sz += pg->ps.type->status(&pg->ps, NULL, type,
1377 DMEMIT("%u %u ", pg->nr_pgpaths,
1378 pg->ps.type->table_args);
1380 list_for_each_entry(p, &pg->pgpaths, list) {
1381 DMEMIT("%s ", p->path.dev->name);
1382 if (pg->ps.type->status)
1383 sz += pg->ps.type->status(&pg->ps,
1384 &p->path, type, result + sz,
1391 spin_unlock_irqrestore(&m->lock, flags);
1396 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1400 struct multipath *m = (struct multipath *) ti->private;
1404 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1405 return queue_if_no_path(m, 1, 0);
1406 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1407 return queue_if_no_path(m, 0, 0);
1413 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1414 return bypass_pg_num(m, argv[1], 1);
1415 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1416 return bypass_pg_num(m, argv[1], 0);
1417 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1418 return switch_pg_num(m, argv[1]);
1419 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1420 action = reinstate_path;
1421 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1426 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1427 dm_table_get_mode(ti->table), &dev);
1429 DMWARN("message: error getting device %s",
1434 r = action_dev(m, dev, action);
1436 dm_put_device(ti, dev);
1441 DMWARN("Unrecognised multipath message received.");
1445 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1448 struct multipath *m = (struct multipath *) ti->private;
1449 struct block_device *bdev = NULL;
1451 unsigned long flags;
1454 spin_lock_irqsave(&m->lock, flags);
1456 if (!m->current_pgpath)
1457 __choose_pgpath(m, 0);
1459 if (m->current_pgpath) {
1460 bdev = m->current_pgpath->path.dev->bdev;
1461 mode = m->current_pgpath->path.dev->mode;
1469 spin_unlock_irqrestore(&m->lock, flags);
1471 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1474 static int multipath_iterate_devices(struct dm_target *ti,
1475 iterate_devices_callout_fn fn, void *data)
1477 struct multipath *m = ti->private;
1478 struct priority_group *pg;
1482 list_for_each_entry(pg, &m->priority_groups, list) {
1483 list_for_each_entry(p, &pg->pgpaths, list) {
1484 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1494 static int __pgpath_busy(struct pgpath *pgpath)
1496 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1498 return dm_underlying_device_busy(q);
1502 * We return "busy", only when we can map I/Os but underlying devices
1503 * are busy (so even if we map I/Os now, the I/Os will wait on
1504 * the underlying queue).
1505 * In other words, if we want to kill I/Os or queue them inside us
1506 * due to map unavailability, we don't return "busy". Otherwise,
1507 * dm core won't give us the I/Os and we can't do what we want.
1509 static int multipath_busy(struct dm_target *ti)
1511 int busy = 0, has_active = 0;
1512 struct multipath *m = ti->private;
1513 struct priority_group *pg;
1514 struct pgpath *pgpath;
1515 unsigned long flags;
1517 spin_lock_irqsave(&m->lock, flags);
1519 /* Guess which priority_group will be used at next mapping time */
1520 if (unlikely(!m->current_pgpath && m->next_pg))
1522 else if (likely(m->current_pg))
1526 * We don't know which pg will be used at next mapping time.
1527 * We don't call __choose_pgpath() here to avoid to trigger
1528 * pg_init just by busy checking.
1529 * So we don't know whether underlying devices we will be using
1530 * at next mapping time are busy or not. Just try mapping.
1535 * If there is one non-busy active path at least, the path selector
1536 * will be able to select it. So we consider such a pg as not busy.
1539 list_for_each_entry(pgpath, &pg->pgpaths, list)
1540 if (pgpath->is_active) {
1543 if (!__pgpath_busy(pgpath)) {
1551 * No active path in this pg, so this pg won't be used and
1552 * the current_pg will be changed at next mapping time.
1553 * We need to try mapping to determine it.
1558 spin_unlock_irqrestore(&m->lock, flags);
1563 /*-----------------------------------------------------------------
1565 *---------------------------------------------------------------*/
1566 static struct target_type multipath_target = {
1567 .name = "multipath",
1568 .version = {1, 1, 0},
1569 .module = THIS_MODULE,
1570 .ctr = multipath_ctr,
1571 .dtr = multipath_dtr,
1572 .map_rq = multipath_map,
1573 .rq_end_io = multipath_end_io,
1574 .presuspend = multipath_presuspend,
1575 .resume = multipath_resume,
1576 .status = multipath_status,
1577 .message = multipath_message,
1578 .ioctl = multipath_ioctl,
1579 .iterate_devices = multipath_iterate_devices,
1580 .busy = multipath_busy,
1583 static int __init dm_multipath_init(void)
1587 /* allocate a slab for the dm_ios */
1588 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1592 r = dm_register_target(&multipath_target);
1594 DMERR("register failed %d", r);
1595 kmem_cache_destroy(_mpio_cache);
1599 kmultipathd = create_workqueue("kmpathd");
1601 DMERR("failed to create workqueue kmpathd");
1602 dm_unregister_target(&multipath_target);
1603 kmem_cache_destroy(_mpio_cache);
1608 * A separate workqueue is used to handle the device handlers
1609 * to avoid overloading existing workqueue. Overloading the
1610 * old workqueue would also create a bottleneck in the
1611 * path of the storage hardware device activation.
1613 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1614 if (!kmpath_handlerd) {
1615 DMERR("failed to create workqueue kmpath_handlerd");
1616 destroy_workqueue(kmultipathd);
1617 dm_unregister_target(&multipath_target);
1618 kmem_cache_destroy(_mpio_cache);
1622 DMINFO("version %u.%u.%u loaded",
1623 multipath_target.version[0], multipath_target.version[1],
1624 multipath_target.version[2]);
1629 static void __exit dm_multipath_exit(void)
1631 destroy_workqueue(kmpath_handlerd);
1632 destroy_workqueue(kmultipathd);
1634 dm_unregister_target(&multipath_target);
1635 kmem_cache_destroy(_mpio_cache);
1638 module_init(dm_multipath_init);
1639 module_exit(dm_multipath_exit);
1641 MODULE_DESCRIPTION(DM_NAME " multipath target");
1642 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1643 MODULE_LICENSE("GPL");