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-bio-record.h"
12 #include "dm-uevent.h"
14 #include <linux/ctype.h>
15 #include <linux/init.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/time.h>
21 #include <linux/workqueue.h>
22 #include <scsi/scsi_dh.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define MESG_STR(x) x, sizeof(x)
30 struct list_head list;
32 struct priority_group *pg; /* Owning PG */
33 unsigned is_active; /* Path status */
34 unsigned fail_count; /* Cumulative failure count */
37 struct work_struct deactivate_path;
38 struct work_struct activate_path;
41 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
47 struct priority_group {
48 struct list_head list;
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
60 /* Multipath context */
62 struct list_head list;
67 const char *hw_handler_name;
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 bio_list 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;
103 struct dm_bio_details details;
107 typedef int (*action_fn) (struct pgpath *pgpath);
109 #define MIN_IOS 256 /* Mempool size */
111 static struct kmem_cache *_mpio_cache;
113 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
114 static void process_queued_ios(struct work_struct *work);
115 static void trigger_event(struct work_struct *work);
116 static void activate_path(struct work_struct *work);
117 static void deactivate_path(struct work_struct *work);
120 /*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
124 static struct pgpath *alloc_pgpath(void)
126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
129 pgpath->is_active = 1;
130 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
131 INIT_WORK(&pgpath->activate_path, activate_path);
137 static void free_pgpath(struct pgpath *pgpath)
142 static void deactivate_path(struct work_struct *work)
144 struct pgpath *pgpath =
145 container_of(work, struct pgpath, deactivate_path);
147 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
150 static struct priority_group *alloc_priority_group(void)
152 struct priority_group *pg;
154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
157 INIT_LIST_HEAD(&pg->pgpaths);
162 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
164 struct pgpath *pgpath, *tmp;
165 struct multipath *m = ti->private;
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
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 spin_lock_init(&m->lock);
199 INIT_WORK(&m->process_queued_ios, process_queued_ios);
200 INIT_WORK(&m->trigger_event, trigger_event);
201 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
213 static void free_multipath(struct multipath *m)
215 struct priority_group *pg, *tmp;
217 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
219 free_priority_group(pg, m->ti);
222 kfree(m->hw_handler_name);
223 mempool_destroy(m->mpio_pool);
228 /*-----------------------------------------------
230 *-----------------------------------------------*/
232 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
234 m->current_pg = pgpath->pg;
236 /* Must we initialise the PG first, and queue I/O till it's ready? */
237 if (m->hw_handler_name) {
238 m->pg_init_required = 1;
241 m->pg_init_required = 0;
245 m->pg_init_count = 0;
248 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, nr_bytes);
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, size_t nr_bytes)
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, nr_bytes))
281 /* Don't change PG until it has no remaining paths */
282 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
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, nr_bytes))
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 bio *bio,
322 struct dm_mpath_io *mpio, unsigned was_queued)
324 int r = DM_MAPIO_REMAPPED;
325 size_t nr_bytes = bio->bi_size;
327 struct pgpath *pgpath;
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 bio_list_add(&m->queued_ios, bio);
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 bio->bi_bdev = pgpath->path.dev->bdev;
353 else if (__must_push_back(m))
354 r = DM_MAPIO_REQUEUE;
356 r = -EIO; /* Failed */
358 mpio->pgpath = pgpath;
359 mpio->nr_bytes = nr_bytes;
361 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
362 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
365 spin_unlock_irqrestore(&m->lock, flags);
371 * If we run out of usable paths, should we queue I/O or error it?
373 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
374 unsigned save_old_value)
378 spin_lock_irqsave(&m->lock, flags);
381 m->saved_queue_if_no_path = m->queue_if_no_path;
383 m->saved_queue_if_no_path = queue_if_no_path;
384 m->queue_if_no_path = queue_if_no_path;
385 if (!m->queue_if_no_path && m->queue_size)
386 queue_work(kmultipathd, &m->process_queued_ios);
388 spin_unlock_irqrestore(&m->lock, flags);
393 /*-----------------------------------------------------------------
394 * The multipath daemon is responsible for resubmitting queued ios.
395 *---------------------------------------------------------------*/
397 static void dispatch_queued_ios(struct multipath *m)
401 struct bio *bio = NULL, *next;
402 struct dm_mpath_io *mpio;
403 union map_info *info;
405 spin_lock_irqsave(&m->lock, flags);
406 bio = bio_list_get(&m->queued_ios);
407 spin_unlock_irqrestore(&m->lock, flags);
413 info = dm_get_mapinfo(bio);
416 r = map_io(m, bio, mpio, 1);
419 else if (r == DM_MAPIO_REMAPPED)
420 generic_make_request(bio);
421 else if (r == DM_MAPIO_REQUEUE)
422 bio_endio(bio, -EIO);
428 static void process_queued_ios(struct work_struct *work)
430 struct multipath *m =
431 container_of(work, struct multipath, process_queued_ios);
432 struct pgpath *pgpath = NULL, *tmp;
433 unsigned must_queue = 1;
436 spin_lock_irqsave(&m->lock, flags);
441 if (!m->current_pgpath)
442 __choose_pgpath(m, 0);
444 pgpath = m->current_pgpath;
446 if ((pgpath && !m->queue_io) ||
447 (!pgpath && !m->queue_if_no_path))
450 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
452 m->pg_init_required = 0;
453 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
454 if (queue_work(kmpath_handlerd, &tmp->activate_path))
455 m->pg_init_in_progress++;
459 spin_unlock_irqrestore(&m->lock, flags);
461 dispatch_queued_ios(m);
465 * An event is triggered whenever a path is taken out of use.
466 * Includes path failure and PG bypass.
468 static void trigger_event(struct work_struct *work)
470 struct multipath *m =
471 container_of(work, struct multipath, trigger_event);
473 dm_table_event(m->ti->table);
476 /*-----------------------------------------------------------------
477 * Constructor/argument parsing:
478 * <#multipath feature args> [<arg>]*
479 * <#hw_handler args> [hw_handler [<arg>]*]
481 * <initial priority group>
482 * [<selector> <#selector args> [<arg>]*
483 * <#paths> <#per-path selector args>
484 * [<path> [<arg>]* ]+ ]+
485 *---------------------------------------------------------------*/
492 static int read_param(struct param *param, char *str, unsigned *v, char **error)
495 (sscanf(str, "%u", v) != 1) ||
498 *error = param->error;
510 static char *shift(struct arg_set *as)
524 static void consume(struct arg_set *as, unsigned n)
526 BUG_ON (as->argc < n);
531 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
532 struct dm_target *ti)
535 struct path_selector_type *pst;
538 static struct param _params[] = {
539 {0, 1024, "invalid number of path selector args"},
542 pst = dm_get_path_selector(shift(as));
544 ti->error = "unknown path selector type";
548 r = read_param(_params, shift(as), &ps_argc, &ti->error);
550 dm_put_path_selector(pst);
554 if (ps_argc > as->argc) {
555 dm_put_path_selector(pst);
556 ti->error = "not enough arguments for path selector";
560 r = pst->create(&pg->ps, ps_argc, as->argv);
562 dm_put_path_selector(pst);
563 ti->error = "path selector constructor failed";
568 consume(as, ps_argc);
573 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
574 struct dm_target *ti)
578 struct multipath *m = ti->private;
580 /* we need at least a path arg */
582 ti->error = "no device given";
583 return ERR_PTR(-EINVAL);
588 return ERR_PTR(-ENOMEM);
590 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
591 dm_table_get_mode(ti->table), &p->path.dev);
593 ti->error = "error getting device";
597 if (m->hw_handler_name) {
598 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
600 r = scsi_dh_attach(q, m->hw_handler_name);
603 * Already attached to different hw_handler,
604 * try to reattach with correct one.
607 r = scsi_dh_attach(q, m->hw_handler_name);
611 ti->error = "error attaching hardware handler";
612 dm_put_device(ti, p->path.dev);
617 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
619 dm_put_device(ti, p->path.dev);
630 static struct priority_group *parse_priority_group(struct arg_set *as,
633 static struct param _params[] = {
634 {1, 1024, "invalid number of paths"},
635 {0, 1024, "invalid number of selector args"}
639 unsigned i, nr_selector_args, nr_params;
640 struct priority_group *pg;
641 struct dm_target *ti = m->ti;
645 ti->error = "not enough priority group arguments";
646 return ERR_PTR(-EINVAL);
649 pg = alloc_priority_group();
651 ti->error = "couldn't allocate priority group";
652 return ERR_PTR(-ENOMEM);
656 r = parse_path_selector(as, pg, ti);
663 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
667 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
671 nr_params = 1 + nr_selector_args;
672 for (i = 0; i < pg->nr_pgpaths; i++) {
673 struct pgpath *pgpath;
674 struct arg_set path_args;
676 if (as->argc < nr_params) {
677 ti->error = "not enough path parameters";
681 path_args.argc = nr_params;
682 path_args.argv = as->argv;
684 pgpath = parse_path(&path_args, &pg->ps, ti);
685 if (IS_ERR(pgpath)) {
691 list_add_tail(&pgpath->list, &pg->pgpaths);
692 consume(as, nr_params);
698 free_priority_group(pg, ti);
702 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
705 struct dm_target *ti = m->ti;
707 static struct param _params[] = {
708 {0, 1024, "invalid number of hardware handler args"},
711 if (read_param(_params, shift(as), &hw_argc, &ti->error))
717 if (hw_argc > as->argc) {
718 ti->error = "not enough arguments for hardware handler";
722 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
723 request_module("scsi_dh_%s", m->hw_handler_name);
724 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
725 ti->error = "unknown hardware handler type";
726 kfree(m->hw_handler_name);
727 m->hw_handler_name = NULL;
732 DMWARN("Ignoring user-specified arguments for "
733 "hardware handler \"%s\"", m->hw_handler_name);
734 consume(as, hw_argc - 1);
739 static int parse_features(struct arg_set *as, struct multipath *m)
743 struct dm_target *ti = m->ti;
744 const char *param_name;
746 static struct param _params[] = {
747 {0, 3, "invalid number of feature args"},
748 {1, 50, "pg_init_retries must be between 1 and 50"},
751 r = read_param(_params, shift(as), &argc, &ti->error);
759 param_name = shift(as);
762 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
763 r = queue_if_no_path(m, 1, 0);
767 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
769 r = read_param(_params + 1, shift(as),
770 &m->pg_init_retries, &ti->error);
775 ti->error = "Unrecognised multipath feature request";
777 } while (argc && !r);
782 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
785 /* target parameters */
786 static struct param _params[] = {
787 {1, 1024, "invalid number of priority groups"},
788 {1, 1024, "invalid initial priority group number"},
794 unsigned pg_count = 0;
795 unsigned next_pg_num;
800 m = alloc_multipath(ti);
802 ti->error = "can't allocate multipath";
806 r = parse_features(&as, m);
810 r = parse_hw_handler(&as, m);
814 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
818 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
822 /* parse the priority groups */
824 struct priority_group *pg;
826 pg = parse_priority_group(&as, m);
832 m->nr_valid_paths += pg->nr_pgpaths;
833 list_add_tail(&pg->list, &m->priority_groups);
835 pg->pg_num = pg_count;
840 if (pg_count != m->nr_priority_groups) {
841 ti->error = "priority group count mismatch";
846 ti->num_flush_requests = 1;
855 static void multipath_dtr(struct dm_target *ti)
857 struct multipath *m = (struct multipath *) ti->private;
859 flush_workqueue(kmpath_handlerd);
860 flush_workqueue(kmultipathd);
861 flush_scheduled_work();
866 * Map bios, recording original fields for later in case we have to resubmit
868 static int multipath_map(struct dm_target *ti, struct bio *bio,
869 union map_info *map_context)
872 struct dm_mpath_io *mpio;
873 struct multipath *m = (struct multipath *) ti->private;
875 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
876 dm_bio_record(&mpio->details, bio);
878 map_context->ptr = mpio;
879 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
880 r = map_io(m, bio, mpio, 0);
881 if (r < 0 || r == DM_MAPIO_REQUEUE)
882 mempool_free(mpio, m->mpio_pool);
888 * Take a path out of use.
890 static int fail_path(struct pgpath *pgpath)
893 struct multipath *m = pgpath->pg->m;
895 spin_lock_irqsave(&m->lock, flags);
897 if (!pgpath->is_active)
900 DMWARN("Failing path %s.", pgpath->path.dev->name);
902 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
903 pgpath->is_active = 0;
904 pgpath->fail_count++;
908 if (pgpath == m->current_pgpath)
909 m->current_pgpath = NULL;
911 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
912 pgpath->path.dev->name, m->nr_valid_paths);
914 schedule_work(&m->trigger_event);
915 queue_work(kmultipathd, &pgpath->deactivate_path);
918 spin_unlock_irqrestore(&m->lock, flags);
924 * Reinstate a previously-failed path
926 static int reinstate_path(struct pgpath *pgpath)
930 struct multipath *m = pgpath->pg->m;
932 spin_lock_irqsave(&m->lock, flags);
934 if (pgpath->is_active)
937 if (!pgpath->pg->ps.type->reinstate_path) {
938 DMWARN("Reinstate path not supported by path selector %s",
939 pgpath->pg->ps.type->name);
944 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
948 pgpath->is_active = 1;
950 if (!m->nr_valid_paths++ && m->queue_size) {
951 m->current_pgpath = NULL;
952 queue_work(kmultipathd, &m->process_queued_ios);
953 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
954 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
955 m->pg_init_in_progress++;
958 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
959 pgpath->path.dev->name, m->nr_valid_paths);
961 schedule_work(&m->trigger_event);
964 spin_unlock_irqrestore(&m->lock, flags);
970 * Fail or reinstate all paths that match the provided struct dm_dev.
972 static int action_dev(struct multipath *m, struct dm_dev *dev,
976 struct pgpath *pgpath;
977 struct priority_group *pg;
979 list_for_each_entry(pg, &m->priority_groups, list) {
980 list_for_each_entry(pgpath, &pg->pgpaths, list) {
981 if (pgpath->path.dev == dev)
990 * Temporarily try to avoid having to use the specified PG
992 static void bypass_pg(struct multipath *m, struct priority_group *pg,
997 spin_lock_irqsave(&m->lock, flags);
999 pg->bypassed = bypassed;
1000 m->current_pgpath = NULL;
1001 m->current_pg = NULL;
1003 spin_unlock_irqrestore(&m->lock, flags);
1005 schedule_work(&m->trigger_event);
1009 * Switch to using the specified PG from the next I/O that gets mapped
1011 static int switch_pg_num(struct multipath *m, const char *pgstr)
1013 struct priority_group *pg;
1015 unsigned long flags;
1017 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1018 (pgnum > m->nr_priority_groups)) {
1019 DMWARN("invalid PG number supplied to switch_pg_num");
1023 spin_lock_irqsave(&m->lock, flags);
1024 list_for_each_entry(pg, &m->priority_groups, list) {
1029 m->current_pgpath = NULL;
1030 m->current_pg = NULL;
1033 spin_unlock_irqrestore(&m->lock, flags);
1035 schedule_work(&m->trigger_event);
1040 * Set/clear bypassed status of a PG.
1041 * PGs are numbered upwards from 1 in the order they were declared.
1043 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1045 struct priority_group *pg;
1048 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1049 (pgnum > m->nr_priority_groups)) {
1050 DMWARN("invalid PG number supplied to bypass_pg");
1054 list_for_each_entry(pg, &m->priority_groups, list) {
1059 bypass_pg(m, pg, bypassed);
1064 * Should we retry pg_init immediately?
1066 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1068 unsigned long flags;
1069 int limit_reached = 0;
1071 spin_lock_irqsave(&m->lock, flags);
1073 if (m->pg_init_count <= m->pg_init_retries)
1074 m->pg_init_required = 1;
1078 spin_unlock_irqrestore(&m->lock, flags);
1080 return limit_reached;
1083 static void pg_init_done(struct dm_path *path, int errors)
1085 struct pgpath *pgpath = path_to_pgpath(path);
1086 struct priority_group *pg = pgpath->pg;
1087 struct multipath *m = pg->m;
1088 unsigned long flags;
1090 /* device or driver problems */
1095 if (!m->hw_handler_name) {
1099 DMERR("Cannot failover device because scsi_dh_%s was not "
1100 "loaded.", m->hw_handler_name);
1102 * Fail path for now, so we do not ping pong
1106 case SCSI_DH_DEV_TEMP_BUSY:
1108 * Probably doing something like FW upgrade on the
1109 * controller so try the other pg.
1111 bypass_pg(m, pg, 1);
1113 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1115 case SCSI_DH_IMM_RETRY:
1116 case SCSI_DH_RES_TEMP_UNAVAIL:
1117 if (pg_init_limit_reached(m, pgpath))
1123 * We probably do not want to fail the path for a device
1124 * error, but this is what the old dm did. In future
1125 * patches we can do more advanced handling.
1130 spin_lock_irqsave(&m->lock, flags);
1132 if (pgpath == m->current_pgpath) {
1133 DMERR("Could not failover device. Error %d.", errors);
1134 m->current_pgpath = NULL;
1135 m->current_pg = NULL;
1137 } else if (!m->pg_init_required) {
1142 m->pg_init_in_progress--;
1143 if (!m->pg_init_in_progress)
1144 queue_work(kmultipathd, &m->process_queued_ios);
1145 spin_unlock_irqrestore(&m->lock, flags);
1148 static void activate_path(struct work_struct *work)
1151 struct pgpath *pgpath =
1152 container_of(work, struct pgpath, activate_path);
1154 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1155 pg_init_done(&pgpath->path, ret);
1161 static int do_end_io(struct multipath *m, struct bio *bio,
1162 int error, struct dm_mpath_io *mpio)
1164 unsigned long flags;
1167 return 0; /* I/O complete */
1169 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1172 if (error == -EOPNOTSUPP)
1175 spin_lock_irqsave(&m->lock, flags);
1176 if (!m->nr_valid_paths) {
1177 if (__must_push_back(m)) {
1178 spin_unlock_irqrestore(&m->lock, flags);
1179 return DM_ENDIO_REQUEUE;
1180 } else if (!m->queue_if_no_path) {
1181 spin_unlock_irqrestore(&m->lock, flags);
1184 spin_unlock_irqrestore(&m->lock, flags);
1188 spin_unlock_irqrestore(&m->lock, flags);
1191 fail_path(mpio->pgpath);
1194 dm_bio_restore(&mpio->details, bio);
1196 /* queue for the daemon to resubmit or fail */
1197 spin_lock_irqsave(&m->lock, flags);
1198 bio_list_add(&m->queued_ios, bio);
1201 queue_work(kmultipathd, &m->process_queued_ios);
1202 spin_unlock_irqrestore(&m->lock, flags);
1204 return DM_ENDIO_INCOMPLETE; /* io not complete */
1207 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1208 int error, union map_info *map_context)
1210 struct multipath *m = ti->private;
1211 struct dm_mpath_io *mpio = map_context->ptr;
1212 struct pgpath *pgpath = mpio->pgpath;
1213 struct path_selector *ps;
1216 r = do_end_io(m, bio, error, mpio);
1218 ps = &pgpath->pg->ps;
1219 if (ps->type->end_io)
1220 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1222 if (r != DM_ENDIO_INCOMPLETE)
1223 mempool_free(mpio, m->mpio_pool);
1229 * Suspend can't complete until all the I/O is processed so if
1230 * the last path fails we must error any remaining I/O.
1231 * Note that if the freeze_bdev fails while suspending, the
1232 * queue_if_no_path state is lost - userspace should reset it.
1234 static void multipath_presuspend(struct dm_target *ti)
1236 struct multipath *m = (struct multipath *) ti->private;
1238 queue_if_no_path(m, 0, 1);
1242 * Restore the queue_if_no_path setting.
1244 static void multipath_resume(struct dm_target *ti)
1246 struct multipath *m = (struct multipath *) ti->private;
1247 unsigned long flags;
1249 spin_lock_irqsave(&m->lock, flags);
1250 m->queue_if_no_path = m->saved_queue_if_no_path;
1251 spin_unlock_irqrestore(&m->lock, flags);
1255 * Info output has the following format:
1256 * num_multipath_feature_args [multipath_feature_args]*
1257 * num_handler_status_args [handler_status_args]*
1258 * num_groups init_group_number
1259 * [A|D|E num_ps_status_args [ps_status_args]*
1260 * num_paths num_selector_args
1261 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1263 * Table output has the following format (identical to the constructor string):
1264 * num_feature_args [features_args]*
1265 * num_handler_args hw_handler [hw_handler_args]*
1266 * num_groups init_group_number
1267 * [priority selector-name num_ps_args [ps_args]*
1268 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1270 static int multipath_status(struct dm_target *ti, status_type_t type,
1271 char *result, unsigned int maxlen)
1274 unsigned long flags;
1275 struct multipath *m = (struct multipath *) ti->private;
1276 struct priority_group *pg;
1281 spin_lock_irqsave(&m->lock, flags);
1284 if (type == STATUSTYPE_INFO)
1285 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1287 DMEMIT("%u ", m->queue_if_no_path +
1288 (m->pg_init_retries > 0) * 2);
1289 if (m->queue_if_no_path)
1290 DMEMIT("queue_if_no_path ");
1291 if (m->pg_init_retries)
1292 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1295 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1298 DMEMIT("1 %s ", m->hw_handler_name);
1300 DMEMIT("%u ", m->nr_priority_groups);
1303 pg_num = m->next_pg->pg_num;
1304 else if (m->current_pg)
1305 pg_num = m->current_pg->pg_num;
1309 DMEMIT("%u ", pg_num);
1312 case STATUSTYPE_INFO:
1313 list_for_each_entry(pg, &m->priority_groups, list) {
1315 state = 'D'; /* Disabled */
1316 else if (pg == m->current_pg)
1317 state = 'A'; /* Currently Active */
1319 state = 'E'; /* Enabled */
1321 DMEMIT("%c ", state);
1323 if (pg->ps.type->status)
1324 sz += pg->ps.type->status(&pg->ps, NULL, type,
1330 DMEMIT("%u %u ", pg->nr_pgpaths,
1331 pg->ps.type->info_args);
1333 list_for_each_entry(p, &pg->pgpaths, list) {
1334 DMEMIT("%s %s %u ", p->path.dev->name,
1335 p->is_active ? "A" : "F",
1337 if (pg->ps.type->status)
1338 sz += pg->ps.type->status(&pg->ps,
1339 &p->path, type, result + sz,
1345 case STATUSTYPE_TABLE:
1346 list_for_each_entry(pg, &m->priority_groups, list) {
1347 DMEMIT("%s ", pg->ps.type->name);
1349 if (pg->ps.type->status)
1350 sz += pg->ps.type->status(&pg->ps, NULL, type,
1356 DMEMIT("%u %u ", pg->nr_pgpaths,
1357 pg->ps.type->table_args);
1359 list_for_each_entry(p, &pg->pgpaths, list) {
1360 DMEMIT("%s ", p->path.dev->name);
1361 if (pg->ps.type->status)
1362 sz += pg->ps.type->status(&pg->ps,
1363 &p->path, type, result + sz,
1370 spin_unlock_irqrestore(&m->lock, flags);
1375 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1379 struct multipath *m = (struct multipath *) ti->private;
1383 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1384 return queue_if_no_path(m, 1, 0);
1385 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1386 return queue_if_no_path(m, 0, 0);
1392 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1393 return bypass_pg_num(m, argv[1], 1);
1394 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1395 return bypass_pg_num(m, argv[1], 0);
1396 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1397 return switch_pg_num(m, argv[1]);
1398 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1399 action = reinstate_path;
1400 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1405 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1406 dm_table_get_mode(ti->table), &dev);
1408 DMWARN("message: error getting device %s",
1413 r = action_dev(m, dev, action);
1415 dm_put_device(ti, dev);
1420 DMWARN("Unrecognised multipath message received.");
1424 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1427 struct multipath *m = (struct multipath *) ti->private;
1428 struct block_device *bdev = NULL;
1430 unsigned long flags;
1433 spin_lock_irqsave(&m->lock, flags);
1435 if (!m->current_pgpath)
1436 __choose_pgpath(m, 0);
1438 if (m->current_pgpath) {
1439 bdev = m->current_pgpath->path.dev->bdev;
1440 mode = m->current_pgpath->path.dev->mode;
1448 spin_unlock_irqrestore(&m->lock, flags);
1450 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1453 /*-----------------------------------------------------------------
1455 *---------------------------------------------------------------*/
1456 static struct target_type multipath_target = {
1457 .name = "multipath",
1458 .version = {1, 0, 5},
1459 .module = THIS_MODULE,
1460 .ctr = multipath_ctr,
1461 .dtr = multipath_dtr,
1462 .map = multipath_map,
1463 .end_io = multipath_end_io,
1464 .presuspend = multipath_presuspend,
1465 .resume = multipath_resume,
1466 .status = multipath_status,
1467 .message = multipath_message,
1468 .ioctl = multipath_ioctl,
1471 static int __init dm_multipath_init(void)
1475 /* allocate a slab for the dm_ios */
1476 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1480 r = dm_register_target(&multipath_target);
1482 DMERR("register failed %d", r);
1483 kmem_cache_destroy(_mpio_cache);
1487 kmultipathd = create_workqueue("kmpathd");
1489 DMERR("failed to create workqueue kmpathd");
1490 dm_unregister_target(&multipath_target);
1491 kmem_cache_destroy(_mpio_cache);
1496 * A separate workqueue is used to handle the device handlers
1497 * to avoid overloading existing workqueue. Overloading the
1498 * old workqueue would also create a bottleneck in the
1499 * path of the storage hardware device activation.
1501 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1502 if (!kmpath_handlerd) {
1503 DMERR("failed to create workqueue kmpath_handlerd");
1504 destroy_workqueue(kmultipathd);
1505 dm_unregister_target(&multipath_target);
1506 kmem_cache_destroy(_mpio_cache);
1510 DMINFO("version %u.%u.%u loaded",
1511 multipath_target.version[0], multipath_target.version[1],
1512 multipath_target.version[2]);
1517 static void __exit dm_multipath_exit(void)
1519 destroy_workqueue(kmpath_handlerd);
1520 destroy_workqueue(kmultipathd);
1522 dm_unregister_target(&multipath_target);
1523 kmem_cache_destroy(_mpio_cache);
1526 module_init(dm_multipath_init);
1527 module_exit(dm_multipath_exit);
1529 MODULE_DESCRIPTION(DM_NAME " multipath target");
1530 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1531 MODULE_LICENSE("GPL");