2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
9 #include "dm-path-selector.h"
10 #include "dm-uevent.h"
12 #include <linux/ctype.h>
13 #include <linux/init.h>
14 #include <linux/mempool.h>
15 #include <linux/module.h>
16 #include <linux/pagemap.h>
17 #include <linux/slab.h>
18 #include <linux/time.h>
19 #include <linux/workqueue.h>
20 #include <scsi/scsi_dh.h>
21 #include <asm/atomic.h>
23 #define DM_MSG_PREFIX "multipath"
24 #define MESG_STR(x) x, sizeof(x)
28 struct list_head list;
30 struct priority_group *pg; /* Owning PG */
31 unsigned is_active; /* Path status */
32 unsigned fail_count; /* Cumulative failure count */
35 struct work_struct deactivate_path;
38 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
44 struct priority_group {
45 struct list_head list;
47 struct multipath *m; /* Owning multipath instance */
48 struct path_selector ps;
50 unsigned pg_num; /* Reference number */
51 unsigned bypassed; /* Temporarily bypass this PG? */
53 unsigned nr_pgpaths; /* Number of paths in PG */
54 struct list_head pgpaths;
57 /* Multipath context */
59 struct list_head list;
64 const char *hw_handler_name;
65 struct work_struct activate_path;
66 struct pgpath *pgpath_to_activate;
67 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
70 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
72 unsigned nr_valid_paths; /* Total number of usable paths */
73 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
76 unsigned repeat_count; /* I/Os left before calling PS again */
78 unsigned queue_io; /* Must we queue all I/O? */
79 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
80 unsigned saved_queue_if_no_path;/* Saved state during suspension */
81 unsigned pg_init_retries; /* Number of times to retry pg_init */
82 unsigned pg_init_count; /* Number of times pg_init called */
84 struct work_struct process_queued_ios;
85 struct list_head queued_ios;
88 struct work_struct trigger_event;
91 * We must use a mempool of dm_mpath_io structs so that we
92 * can resubmit bios on error.
98 * Context information attached to each bio we process.
101 struct pgpath *pgpath;
104 typedef int (*action_fn) (struct pgpath *pgpath);
106 #define MIN_IOS 256 /* Mempool size */
108 static struct kmem_cache *_mpio_cache;
110 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
111 static void process_queued_ios(struct work_struct *work);
112 static void trigger_event(struct work_struct *work);
113 static void activate_path(struct work_struct *work);
114 static void deactivate_path(struct work_struct *work);
117 /*-----------------------------------------------
118 * Allocation routines
119 *-----------------------------------------------*/
121 static struct pgpath *alloc_pgpath(void)
123 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
126 pgpath->is_active = 1;
127 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
133 static void free_pgpath(struct pgpath *pgpath)
138 static void deactivate_path(struct work_struct *work)
140 struct pgpath *pgpath =
141 container_of(work, struct pgpath, deactivate_path);
143 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
146 static struct priority_group *alloc_priority_group(void)
148 struct priority_group *pg;
150 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
153 INIT_LIST_HEAD(&pg->pgpaths);
158 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
161 struct pgpath *pgpath, *tmp;
162 struct multipath *m = ti->private;
164 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
165 list_del(&pgpath->list);
166 dm_put_device(ti, pgpath->path.dev);
167 spin_lock_irqsave(&m->lock, flags);
168 if (m->pgpath_to_activate == pgpath)
169 m->pgpath_to_activate = NULL;
170 spin_unlock_irqrestore(&m->lock, flags);
175 static void free_priority_group(struct priority_group *pg,
176 struct dm_target *ti)
178 struct path_selector *ps = &pg->ps;
181 ps->type->destroy(ps);
182 dm_put_path_selector(ps->type);
185 free_pgpaths(&pg->pgpaths, ti);
189 static struct multipath *alloc_multipath(struct dm_target *ti)
193 m = kzalloc(sizeof(*m), GFP_KERNEL);
195 INIT_LIST_HEAD(&m->priority_groups);
196 INIT_LIST_HEAD(&m->queued_ios);
197 spin_lock_init(&m->lock);
199 INIT_WORK(&m->process_queued_ios, process_queued_ios);
200 INIT_WORK(&m->trigger_event, trigger_event);
201 INIT_WORK(&m->activate_path, activate_path);
202 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
214 static void free_multipath(struct multipath *m)
216 struct priority_group *pg, *tmp;
218 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
220 free_priority_group(pg, m->ti);
223 kfree(m->hw_handler_name);
224 mempool_destroy(m->mpio_pool);
229 /*-----------------------------------------------
231 *-----------------------------------------------*/
233 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
235 m->current_pg = pgpath->pg;
237 /* Must we initialise the PG first, and queue I/O till it's ready? */
238 if (m->hw_handler_name) {
239 m->pg_init_required = 1;
242 m->pg_init_required = 0;
246 m->pg_init_count = 0;
249 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
251 struct dm_path *path;
253 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
257 m->current_pgpath = path_to_pgpath(path);
259 if (m->current_pg != pg)
260 __switch_pg(m, m->current_pgpath);
265 static void __choose_pgpath(struct multipath *m)
267 struct priority_group *pg;
268 unsigned bypassed = 1;
270 if (!m->nr_valid_paths)
273 /* Were we instructed to switch PG? */
277 if (!__choose_path_in_pg(m, pg))
281 /* Don't change PG until it has no remaining paths */
282 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
286 * Loop through priority groups until we find a valid path.
287 * First time we skip PGs marked 'bypassed'.
288 * Second time we only try the ones we skipped.
291 list_for_each_entry(pg, &m->priority_groups, list) {
292 if (pg->bypassed == bypassed)
294 if (!__choose_path_in_pg(m, pg))
297 } while (bypassed--);
300 m->current_pgpath = NULL;
301 m->current_pg = NULL;
305 * Check whether bios must be queued in the device-mapper core rather
306 * than here in the target.
308 * m->lock must be held on entry.
310 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
311 * same value then we are not between multipath_presuspend()
312 * and multipath_resume() calls and we have no need to check
313 * for the DMF_NOFLUSH_SUSPENDING flag.
315 static int __must_push_back(struct multipath *m)
317 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
318 dm_noflush_suspending(m->ti));
321 static int map_io(struct multipath *m, struct request *clone,
322 struct dm_mpath_io *mpio, unsigned was_queued)
324 int r = DM_MAPIO_REMAPPED;
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)))
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;
362 spin_unlock_irqrestore(&m->lock, flags);
368 * If we run out of usable paths, should we queue I/O or error it?
370 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
371 unsigned save_old_value)
375 spin_lock_irqsave(&m->lock, flags);
378 m->saved_queue_if_no_path = m->queue_if_no_path;
380 m->saved_queue_if_no_path = queue_if_no_path;
381 m->queue_if_no_path = queue_if_no_path;
382 if (!m->queue_if_no_path && m->queue_size)
383 queue_work(kmultipathd, &m->process_queued_ios);
385 spin_unlock_irqrestore(&m->lock, flags);
390 /*-----------------------------------------------------------------
391 * The multipath daemon is responsible for resubmitting queued ios.
392 *---------------------------------------------------------------*/
394 static void dispatch_queued_ios(struct multipath *m)
398 struct dm_mpath_io *mpio;
399 union map_info *info;
400 struct request *clone, *n;
403 spin_lock_irqsave(&m->lock, flags);
404 list_splice_init(&m->queued_ios, &cl);
405 spin_unlock_irqrestore(&m->lock, flags);
407 list_for_each_entry_safe(clone, n, &cl, queuelist) {
408 list_del_init(&clone->queuelist);
410 info = dm_get_rq_mapinfo(clone);
413 r = map_io(m, clone, mpio, 1);
415 mempool_free(mpio, m->mpio_pool);
416 dm_kill_request(clone, r);
417 } else if (r == DM_MAPIO_REMAPPED)
418 dm_dispatch_request(clone);
419 else if (r == DM_MAPIO_REQUEUE) {
420 mempool_free(mpio, m->mpio_pool);
421 dm_requeue_request(clone);
426 static void process_queued_ios(struct work_struct *work)
428 struct multipath *m =
429 container_of(work, struct multipath, process_queued_ios);
430 struct pgpath *pgpath = NULL;
431 unsigned init_required = 0, must_queue = 1;
434 spin_lock_irqsave(&m->lock, flags);
439 if (!m->current_pgpath)
442 pgpath = m->current_pgpath;
444 if ((pgpath && !m->queue_io) ||
445 (!pgpath && !m->queue_if_no_path))
448 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
449 m->pgpath_to_activate = pgpath;
451 m->pg_init_required = 0;
452 m->pg_init_in_progress = 1;
457 spin_unlock_irqrestore(&m->lock, flags);
460 queue_work(kmpath_handlerd, &m->activate_path);
463 dispatch_queued_ios(m);
467 * An event is triggered whenever a path is taken out of use.
468 * Includes path failure and PG bypass.
470 static void trigger_event(struct work_struct *work)
472 struct multipath *m =
473 container_of(work, struct multipath, trigger_event);
475 dm_table_event(m->ti->table);
478 /*-----------------------------------------------------------------
479 * Constructor/argument parsing:
480 * <#multipath feature args> [<arg>]*
481 * <#hw_handler args> [hw_handler [<arg>]*]
483 * <initial priority group>
484 * [<selector> <#selector args> [<arg>]*
485 * <#paths> <#per-path selector args>
486 * [<path> [<arg>]* ]+ ]+
487 *---------------------------------------------------------------*/
494 static int read_param(struct param *param, char *str, unsigned *v, char **error)
497 (sscanf(str, "%u", v) != 1) ||
500 *error = param->error;
512 static char *shift(struct arg_set *as)
526 static void consume(struct arg_set *as, unsigned n)
528 BUG_ON (as->argc < n);
533 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
534 struct dm_target *ti)
537 struct path_selector_type *pst;
540 static struct param _params[] = {
541 {0, 1024, "invalid number of path selector args"},
544 pst = dm_get_path_selector(shift(as));
546 ti->error = "unknown path selector type";
550 r = read_param(_params, shift(as), &ps_argc, &ti->error);
552 dm_put_path_selector(pst);
556 r = pst->create(&pg->ps, ps_argc, as->argv);
558 dm_put_path_selector(pst);
559 ti->error = "path selector constructor failed";
564 consume(as, ps_argc);
569 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
570 struct dm_target *ti)
574 struct multipath *m = ti->private;
576 /* we need at least a path arg */
578 ti->error = "no device given";
586 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
587 dm_table_get_mode(ti->table), &p->path.dev);
589 ti->error = "error getting device";
593 if (m->hw_handler_name) {
594 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
596 r = scsi_dh_attach(q, m->hw_handler_name);
599 * Already attached to different hw_handler,
600 * try to reattach with correct one.
603 r = scsi_dh_attach(q, m->hw_handler_name);
606 ti->error = "error attaching hardware handler";
607 dm_put_device(ti, p->path.dev);
612 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
614 dm_put_device(ti, p->path.dev);
625 static struct priority_group *parse_priority_group(struct arg_set *as,
628 static struct param _params[] = {
629 {1, 1024, "invalid number of paths"},
630 {0, 1024, "invalid number of selector args"}
634 unsigned i, nr_selector_args, nr_params;
635 struct priority_group *pg;
636 struct dm_target *ti = m->ti;
640 ti->error = "not enough priority group aruments";
644 pg = alloc_priority_group();
646 ti->error = "couldn't allocate priority group";
651 r = parse_path_selector(as, pg, ti);
658 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
662 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
666 nr_params = 1 + nr_selector_args;
667 for (i = 0; i < pg->nr_pgpaths; i++) {
668 struct pgpath *pgpath;
669 struct arg_set path_args;
671 if (as->argc < nr_params) {
672 ti->error = "not enough path parameters";
676 path_args.argc = nr_params;
677 path_args.argv = as->argv;
679 pgpath = parse_path(&path_args, &pg->ps, ti);
684 list_add_tail(&pgpath->list, &pg->pgpaths);
685 consume(as, nr_params);
691 free_priority_group(pg, ti);
695 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
698 struct dm_target *ti = m->ti;
700 static struct param _params[] = {
701 {0, 1024, "invalid number of hardware handler args"},
704 if (read_param(_params, shift(as), &hw_argc, &ti->error))
710 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
711 request_module("scsi_dh_%s", m->hw_handler_name);
712 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
713 ti->error = "unknown hardware handler type";
714 kfree(m->hw_handler_name);
715 m->hw_handler_name = NULL;
718 consume(as, hw_argc - 1);
723 static int parse_features(struct arg_set *as, struct multipath *m)
727 struct dm_target *ti = m->ti;
728 const char *param_name;
730 static struct param _params[] = {
731 {0, 3, "invalid number of feature args"},
732 {1, 50, "pg_init_retries must be between 1 and 50"},
735 r = read_param(_params, shift(as), &argc, &ti->error);
743 param_name = shift(as);
746 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
747 r = queue_if_no_path(m, 1, 0);
751 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
753 r = read_param(_params + 1, shift(as),
754 &m->pg_init_retries, &ti->error);
759 ti->error = "Unrecognised multipath feature request";
761 } while (argc && !r);
766 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
769 /* target parameters */
770 static struct param _params[] = {
771 {0, 1024, "invalid number of priority groups"},
772 {0, 1024, "invalid initial priority group number"},
778 unsigned pg_count = 0;
779 unsigned next_pg_num;
784 m = alloc_multipath(ti);
786 ti->error = "can't allocate multipath";
790 r = parse_features(&as, m);
794 r = parse_hw_handler(&as, m);
798 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
802 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
806 /* parse the priority groups */
808 struct priority_group *pg;
810 pg = parse_priority_group(&as, m);
816 m->nr_valid_paths += pg->nr_pgpaths;
817 list_add_tail(&pg->list, &m->priority_groups);
819 pg->pg_num = pg_count;
824 if (pg_count != m->nr_priority_groups) {
825 ti->error = "priority group count mismatch";
837 static void multipath_dtr(struct dm_target *ti)
839 struct multipath *m = (struct multipath *) ti->private;
841 flush_workqueue(kmpath_handlerd);
842 flush_workqueue(kmultipathd);
847 * Map cloned requests
849 static int multipath_map(struct dm_target *ti, struct request *clone,
850 union map_info *map_context)
853 struct dm_mpath_io *mpio;
854 struct multipath *m = (struct multipath *) ti->private;
856 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
858 /* ENOMEM, requeue */
859 return DM_MAPIO_REQUEUE;
860 memset(mpio, 0, sizeof(*mpio));
862 map_context->ptr = mpio;
863 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
864 r = map_io(m, clone, mpio, 0);
865 if (r < 0 || r == DM_MAPIO_REQUEUE)
866 mempool_free(mpio, m->mpio_pool);
872 * Take a path out of use.
874 static int fail_path(struct pgpath *pgpath)
877 struct multipath *m = pgpath->pg->m;
879 spin_lock_irqsave(&m->lock, flags);
881 if (!pgpath->is_active)
884 DMWARN("Failing path %s.", pgpath->path.dev->name);
886 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
887 pgpath->is_active = 0;
888 pgpath->fail_count++;
892 if (pgpath == m->current_pgpath)
893 m->current_pgpath = NULL;
895 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
896 pgpath->path.dev->name, m->nr_valid_paths);
898 queue_work(kmultipathd, &m->trigger_event);
899 queue_work(kmultipathd, &pgpath->deactivate_path);
902 spin_unlock_irqrestore(&m->lock, flags);
908 * Reinstate a previously-failed path
910 static int reinstate_path(struct pgpath *pgpath)
914 struct multipath *m = pgpath->pg->m;
916 spin_lock_irqsave(&m->lock, flags);
918 if (pgpath->is_active)
921 if (!pgpath->pg->ps.type->reinstate_path) {
922 DMWARN("Reinstate path not supported by path selector %s",
923 pgpath->pg->ps.type->name);
928 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
932 pgpath->is_active = 1;
934 m->current_pgpath = NULL;
935 if (!m->nr_valid_paths++ && m->queue_size)
936 queue_work(kmultipathd, &m->process_queued_ios);
938 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
939 pgpath->path.dev->name, m->nr_valid_paths);
941 queue_work(kmultipathd, &m->trigger_event);
944 spin_unlock_irqrestore(&m->lock, flags);
950 * Fail or reinstate all paths that match the provided struct dm_dev.
952 static int action_dev(struct multipath *m, struct dm_dev *dev,
956 struct pgpath *pgpath;
957 struct priority_group *pg;
959 list_for_each_entry(pg, &m->priority_groups, list) {
960 list_for_each_entry(pgpath, &pg->pgpaths, list) {
961 if (pgpath->path.dev == dev)
970 * Temporarily try to avoid having to use the specified PG
972 static void bypass_pg(struct multipath *m, struct priority_group *pg,
977 spin_lock_irqsave(&m->lock, flags);
979 pg->bypassed = bypassed;
980 m->current_pgpath = NULL;
981 m->current_pg = NULL;
983 spin_unlock_irqrestore(&m->lock, flags);
985 queue_work(kmultipathd, &m->trigger_event);
989 * Switch to using the specified PG from the next I/O that gets mapped
991 static int switch_pg_num(struct multipath *m, const char *pgstr)
993 struct priority_group *pg;
997 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
998 (pgnum > m->nr_priority_groups)) {
999 DMWARN("invalid PG number supplied to switch_pg_num");
1003 spin_lock_irqsave(&m->lock, flags);
1004 list_for_each_entry(pg, &m->priority_groups, list) {
1009 m->current_pgpath = NULL;
1010 m->current_pg = NULL;
1013 spin_unlock_irqrestore(&m->lock, flags);
1015 queue_work(kmultipathd, &m->trigger_event);
1020 * Set/clear bypassed status of a PG.
1021 * PGs are numbered upwards from 1 in the order they were declared.
1023 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1025 struct priority_group *pg;
1028 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1029 (pgnum > m->nr_priority_groups)) {
1030 DMWARN("invalid PG number supplied to bypass_pg");
1034 list_for_each_entry(pg, &m->priority_groups, list) {
1039 bypass_pg(m, pg, bypassed);
1044 * Should we retry pg_init immediately?
1046 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1048 unsigned long flags;
1049 int limit_reached = 0;
1051 spin_lock_irqsave(&m->lock, flags);
1053 if (m->pg_init_count <= m->pg_init_retries)
1054 m->pg_init_required = 1;
1058 spin_unlock_irqrestore(&m->lock, flags);
1060 return limit_reached;
1063 static void pg_init_done(struct dm_path *path, int errors)
1065 struct pgpath *pgpath = path_to_pgpath(path);
1066 struct priority_group *pg = pgpath->pg;
1067 struct multipath *m = pg->m;
1068 unsigned long flags;
1070 /* device or driver problems */
1075 if (!m->hw_handler_name) {
1079 DMERR("Cannot failover device because scsi_dh_%s was not "
1080 "loaded.", m->hw_handler_name);
1082 * Fail path for now, so we do not ping pong
1086 case SCSI_DH_DEV_TEMP_BUSY:
1088 * Probably doing something like FW upgrade on the
1089 * controller so try the other pg.
1091 bypass_pg(m, pg, 1);
1093 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1095 case SCSI_DH_IMM_RETRY:
1096 case SCSI_DH_RES_TEMP_UNAVAIL:
1097 if (pg_init_limit_reached(m, pgpath))
1103 * We probably do not want to fail the path for a device
1104 * error, but this is what the old dm did. In future
1105 * patches we can do more advanced handling.
1110 spin_lock_irqsave(&m->lock, flags);
1112 DMERR("Could not failover device. Error %d.", errors);
1113 m->current_pgpath = NULL;
1114 m->current_pg = NULL;
1115 } else if (!m->pg_init_required) {
1120 m->pg_init_in_progress = 0;
1121 queue_work(kmultipathd, &m->process_queued_ios);
1122 spin_unlock_irqrestore(&m->lock, flags);
1125 static void activate_path(struct work_struct *work)
1128 struct multipath *m =
1129 container_of(work, struct multipath, activate_path);
1130 struct dm_path *path;
1131 unsigned long flags;
1133 spin_lock_irqsave(&m->lock, flags);
1134 path = &m->pgpath_to_activate->path;
1135 m->pgpath_to_activate = NULL;
1136 spin_unlock_irqrestore(&m->lock, flags);
1139 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1140 pg_init_done(path, ret);
1146 static int do_end_io(struct multipath *m, struct request *clone,
1147 int error, struct dm_mpath_io *mpio)
1150 * We don't queue any clone request inside the multipath target
1151 * during end I/O handling, since those clone requests don't have
1152 * bio clones. If we queue them inside the multipath target,
1153 * we need to make bio clones, that requires memory allocation.
1154 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1155 * don't have bio clones.)
1156 * Instead of queueing the clone request here, we queue the original
1157 * request into dm core, which will remake a clone request and
1158 * clone bios for it and resubmit it later.
1160 int r = DM_ENDIO_REQUEUE;
1161 unsigned long flags;
1163 if (!error && !clone->errors)
1164 return 0; /* I/O complete */
1166 if (error == -EOPNOTSUPP)
1170 fail_path(mpio->pgpath);
1172 spin_lock_irqsave(&m->lock, flags);
1173 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1175 spin_unlock_irqrestore(&m->lock, flags);
1180 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1181 int error, union map_info *map_context)
1183 struct multipath *m = ti->private;
1184 struct dm_mpath_io *mpio = map_context->ptr;
1185 struct pgpath *pgpath = mpio->pgpath;
1186 struct path_selector *ps;
1189 r = do_end_io(m, clone, error, mpio);
1191 ps = &pgpath->pg->ps;
1192 if (ps->type->end_io)
1193 ps->type->end_io(ps, &pgpath->path);
1195 mempool_free(mpio, m->mpio_pool);
1201 * Suspend can't complete until all the I/O is processed so if
1202 * the last path fails we must error any remaining I/O.
1203 * Note that if the freeze_bdev fails while suspending, the
1204 * queue_if_no_path state is lost - userspace should reset it.
1206 static void multipath_presuspend(struct dm_target *ti)
1208 struct multipath *m = (struct multipath *) ti->private;
1210 queue_if_no_path(m, 0, 1);
1214 * Restore the queue_if_no_path setting.
1216 static void multipath_resume(struct dm_target *ti)
1218 struct multipath *m = (struct multipath *) ti->private;
1219 unsigned long flags;
1221 spin_lock_irqsave(&m->lock, flags);
1222 m->queue_if_no_path = m->saved_queue_if_no_path;
1223 spin_unlock_irqrestore(&m->lock, flags);
1227 * Info output has the following format:
1228 * num_multipath_feature_args [multipath_feature_args]*
1229 * num_handler_status_args [handler_status_args]*
1230 * num_groups init_group_number
1231 * [A|D|E num_ps_status_args [ps_status_args]*
1232 * num_paths num_selector_args
1233 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1235 * Table output has the following format (identical to the constructor string):
1236 * num_feature_args [features_args]*
1237 * num_handler_args hw_handler [hw_handler_args]*
1238 * num_groups init_group_number
1239 * [priority selector-name num_ps_args [ps_args]*
1240 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1242 static int multipath_status(struct dm_target *ti, status_type_t type,
1243 char *result, unsigned int maxlen)
1246 unsigned long flags;
1247 struct multipath *m = (struct multipath *) ti->private;
1248 struct priority_group *pg;
1253 spin_lock_irqsave(&m->lock, flags);
1256 if (type == STATUSTYPE_INFO)
1257 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1259 DMEMIT("%u ", m->queue_if_no_path +
1260 (m->pg_init_retries > 0) * 2);
1261 if (m->queue_if_no_path)
1262 DMEMIT("queue_if_no_path ");
1263 if (m->pg_init_retries)
1264 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1267 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1270 DMEMIT("1 %s ", m->hw_handler_name);
1272 DMEMIT("%u ", m->nr_priority_groups);
1275 pg_num = m->next_pg->pg_num;
1276 else if (m->current_pg)
1277 pg_num = m->current_pg->pg_num;
1281 DMEMIT("%u ", pg_num);
1284 case STATUSTYPE_INFO:
1285 list_for_each_entry(pg, &m->priority_groups, list) {
1287 state = 'D'; /* Disabled */
1288 else if (pg == m->current_pg)
1289 state = 'A'; /* Currently Active */
1291 state = 'E'; /* Enabled */
1293 DMEMIT("%c ", state);
1295 if (pg->ps.type->status)
1296 sz += pg->ps.type->status(&pg->ps, NULL, type,
1302 DMEMIT("%u %u ", pg->nr_pgpaths,
1303 pg->ps.type->info_args);
1305 list_for_each_entry(p, &pg->pgpaths, list) {
1306 DMEMIT("%s %s %u ", p->path.dev->name,
1307 p->is_active ? "A" : "F",
1309 if (pg->ps.type->status)
1310 sz += pg->ps.type->status(&pg->ps,
1311 &p->path, type, result + sz,
1317 case STATUSTYPE_TABLE:
1318 list_for_each_entry(pg, &m->priority_groups, list) {
1319 DMEMIT("%s ", pg->ps.type->name);
1321 if (pg->ps.type->status)
1322 sz += pg->ps.type->status(&pg->ps, NULL, type,
1328 DMEMIT("%u %u ", pg->nr_pgpaths,
1329 pg->ps.type->table_args);
1331 list_for_each_entry(p, &pg->pgpaths, list) {
1332 DMEMIT("%s ", p->path.dev->name);
1333 if (pg->ps.type->status)
1334 sz += pg->ps.type->status(&pg->ps,
1335 &p->path, type, result + sz,
1342 spin_unlock_irqrestore(&m->lock, flags);
1347 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1351 struct multipath *m = (struct multipath *) ti->private;
1355 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1356 return queue_if_no_path(m, 1, 0);
1357 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1358 return queue_if_no_path(m, 0, 0);
1364 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1365 return bypass_pg_num(m, argv[1], 1);
1366 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1367 return bypass_pg_num(m, argv[1], 0);
1368 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1369 return switch_pg_num(m, argv[1]);
1370 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1371 action = reinstate_path;
1372 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1377 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1378 dm_table_get_mode(ti->table), &dev);
1380 DMWARN("message: error getting device %s",
1385 r = action_dev(m, dev, action);
1387 dm_put_device(ti, dev);
1392 DMWARN("Unrecognised multipath message received.");
1396 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1397 struct file *filp, unsigned int cmd,
1400 struct multipath *m = (struct multipath *) ti->private;
1401 struct block_device *bdev = NULL;
1402 unsigned long flags;
1403 struct file fake_file = {};
1404 struct dentry fake_dentry = {};
1407 fake_file.f_path.dentry = &fake_dentry;
1409 spin_lock_irqsave(&m->lock, flags);
1411 if (!m->current_pgpath)
1414 if (m->current_pgpath) {
1415 bdev = m->current_pgpath->path.dev->bdev;
1416 fake_dentry.d_inode = bdev->bd_inode;
1417 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1425 spin_unlock_irqrestore(&m->lock, flags);
1427 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1428 bdev->bd_disk, cmd, arg);
1431 static int __pgpath_busy(struct pgpath *pgpath)
1433 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1435 return dm_underlying_device_busy(q);
1439 * We return "busy", only when we can map I/Os but underlying devices
1440 * are busy (so even if we map I/Os now, the I/Os will wait on
1441 * the underlying queue).
1442 * In other words, if we want to kill I/Os or queue them inside us
1443 * due to map unavailability, we don't return "busy". Otherwise,
1444 * dm core won't give us the I/Os and we can't do what we want.
1446 static int multipath_busy(struct dm_target *ti)
1448 int busy = 0, has_active = 0;
1449 struct multipath *m = (struct multipath *) ti->private;
1450 struct priority_group *pg;
1451 struct pgpath *pgpath;
1452 unsigned long flags;
1454 spin_lock_irqsave(&m->lock, flags);
1456 /* Guess which priority_group will be used at next mapping time */
1457 if (unlikely(!m->current_pgpath && m->next_pg))
1459 else if (likely(m->current_pg))
1463 * We don't know which pg will be used at next mapping time.
1464 * We don't call __choose_pgpath() here to avoid to trigger
1465 * pg_init just by busy checking.
1466 * So we don't know whether underlying devices we will be using
1467 * at next mapping time are busy or not. Just try mapping.
1472 * If there is one non-busy active path at least, the path selector
1473 * will be able to select it. So we consider such a pg as not busy.
1476 list_for_each_entry(pgpath, &pg->pgpaths, list)
1477 if (pgpath->is_active) {
1480 if (!__pgpath_busy(pgpath)) {
1488 * No active path in this pg, so this pg won't be used and
1489 * the current_pg will be changed at next mapping time.
1490 * We need to try mapping to determine it.
1495 spin_unlock_irqrestore(&m->lock, flags);
1500 /*-----------------------------------------------------------------
1502 *---------------------------------------------------------------*/
1503 static struct target_type multipath_target = {
1504 .name = "multipath",
1505 .version = {1, 0, 5},
1506 .module = THIS_MODULE,
1507 .ctr = multipath_ctr,
1508 .dtr = multipath_dtr,
1509 .map_rq = multipath_map,
1510 .rq_end_io = multipath_end_io,
1511 .presuspend = multipath_presuspend,
1512 .resume = multipath_resume,
1513 .status = multipath_status,
1514 .message = multipath_message,
1515 .ioctl = multipath_ioctl,
1516 .busy = multipath_busy,
1519 static int __init dm_multipath_init(void)
1523 /* allocate a slab for the dm_ios */
1524 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1528 r = dm_register_target(&multipath_target);
1530 DMERR("register failed %d", r);
1531 kmem_cache_destroy(_mpio_cache);
1535 kmultipathd = create_workqueue("kmpathd");
1537 DMERR("failed to create workqueue kmpathd");
1538 dm_unregister_target(&multipath_target);
1539 kmem_cache_destroy(_mpio_cache);
1544 * A separate workqueue is used to handle the device handlers
1545 * to avoid overloading existing workqueue. Overloading the
1546 * old workqueue would also create a bottleneck in the
1547 * path of the storage hardware device activation.
1549 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1550 if (!kmpath_handlerd) {
1551 DMERR("failed to create workqueue kmpath_handlerd");
1552 destroy_workqueue(kmultipathd);
1553 dm_unregister_target(&multipath_target);
1554 kmem_cache_destroy(_mpio_cache);
1558 DMINFO("version %u.%u.%u loaded",
1559 multipath_target.version[0], multipath_target.version[1],
1560 multipath_target.version[2]);
1565 static void __exit dm_multipath_exit(void)
1569 destroy_workqueue(kmpath_handlerd);
1570 destroy_workqueue(kmultipathd);
1572 r = dm_unregister_target(&multipath_target);
1574 DMERR("target unregister failed %d", r);
1575 kmem_cache_destroy(_mpio_cache);
1578 module_init(dm_multipath_init);
1579 module_exit(dm_multipath_exit);
1581 MODULE_DESCRIPTION(DM_NAME " multipath target");
1582 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1583 MODULE_LICENSE("GPL");