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-bio-list.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 fail_count
; /* Cumulative failure count */
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 bio_list 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
;
102 struct dm_bio_details details
;
105 typedef int (*action_fn
) (struct pgpath
*pgpath
);
107 #define MIN_IOS 256 /* Mempool size */
109 static struct kmem_cache
*_mpio_cache
;
111 static struct workqueue_struct
*kmultipathd
, *kmpath_handlerd
;
112 static void process_queued_ios(struct work_struct
*work
);
113 static void trigger_event(struct work_struct
*work
);
114 static void activate_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
->path
.is_active
= 1;
131 static void free_pgpath(struct pgpath
*pgpath
)
136 static struct priority_group
*alloc_priority_group(void)
138 struct priority_group
*pg
;
140 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
143 INIT_LIST_HEAD(&pg
->pgpaths
);
148 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
151 struct pgpath
*pgpath
, *tmp
;
152 struct multipath
*m
= ti
->private;
154 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
155 list_del(&pgpath
->list
);
156 if (m
->hw_handler_name
)
157 scsi_dh_detach(bdev_get_queue(pgpath
->path
.dev
->bdev
));
158 dm_put_device(ti
, pgpath
->path
.dev
);
159 spin_lock_irqsave(&m
->lock
, flags
);
160 if (m
->pgpath_to_activate
== pgpath
)
161 m
->pgpath_to_activate
= NULL
;
162 spin_unlock_irqrestore(&m
->lock
, flags
);
167 static void free_priority_group(struct priority_group
*pg
,
168 struct dm_target
*ti
)
170 struct path_selector
*ps
= &pg
->ps
;
173 ps
->type
->destroy(ps
);
174 dm_put_path_selector(ps
->type
);
177 free_pgpaths(&pg
->pgpaths
, ti
);
181 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
185 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
187 INIT_LIST_HEAD(&m
->priority_groups
);
188 spin_lock_init(&m
->lock
);
190 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
191 INIT_WORK(&m
->trigger_event
, trigger_event
);
192 INIT_WORK(&m
->activate_path
, activate_path
);
193 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
205 static void free_multipath(struct multipath
*m
)
207 struct priority_group
*pg
, *tmp
;
209 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
211 free_priority_group(pg
, m
->ti
);
214 kfree(m
->hw_handler_name
);
215 mempool_destroy(m
->mpio_pool
);
220 /*-----------------------------------------------
222 *-----------------------------------------------*/
224 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
226 m
->current_pg
= pgpath
->pg
;
228 /* Must we initialise the PG first, and queue I/O till it's ready? */
229 if (m
->hw_handler_name
) {
230 m
->pg_init_required
= 1;
233 m
->pg_init_required
= 0;
237 m
->pg_init_count
= 0;
240 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
)
242 struct dm_path
*path
;
244 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
);
248 m
->current_pgpath
= path_to_pgpath(path
);
250 if (m
->current_pg
!= pg
)
251 __switch_pg(m
, m
->current_pgpath
);
256 static void __choose_pgpath(struct multipath
*m
)
258 struct priority_group
*pg
;
259 unsigned bypassed
= 1;
261 if (!m
->nr_valid_paths
)
264 /* Were we instructed to switch PG? */
268 if (!__choose_path_in_pg(m
, pg
))
272 /* Don't change PG until it has no remaining paths */
273 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
))
277 * Loop through priority groups until we find a valid path.
278 * First time we skip PGs marked 'bypassed'.
279 * Second time we only try the ones we skipped.
282 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
283 if (pg
->bypassed
== bypassed
)
285 if (!__choose_path_in_pg(m
, pg
))
288 } while (bypassed
--);
291 m
->current_pgpath
= NULL
;
292 m
->current_pg
= NULL
;
296 * Check whether bios must be queued in the device-mapper core rather
297 * than here in the target.
299 * m->lock must be held on entry.
301 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
302 * same value then we are not between multipath_presuspend()
303 * and multipath_resume() calls and we have no need to check
304 * for the DMF_NOFLUSH_SUSPENDING flag.
306 static int __must_push_back(struct multipath
*m
)
308 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
309 dm_noflush_suspending(m
->ti
));
312 static int map_io(struct multipath
*m
, struct bio
*bio
,
313 struct dm_mpath_io
*mpio
, unsigned was_queued
)
315 int r
= DM_MAPIO_REMAPPED
;
317 struct pgpath
*pgpath
;
319 spin_lock_irqsave(&m
->lock
, flags
);
321 /* Do we need to select a new pgpath? */
322 if (!m
->current_pgpath
||
323 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
326 pgpath
= m
->current_pgpath
;
331 if ((pgpath
&& m
->queue_io
) ||
332 (!pgpath
&& m
->queue_if_no_path
)) {
333 /* Queue for the daemon to resubmit */
334 bio_list_add(&m
->queued_ios
, bio
);
336 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
338 queue_work(kmultipathd
, &m
->process_queued_ios
);
340 r
= DM_MAPIO_SUBMITTED
;
342 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
343 else if (__must_push_back(m
))
344 r
= DM_MAPIO_REQUEUE
;
346 r
= -EIO
; /* Failed */
348 mpio
->pgpath
= pgpath
;
350 spin_unlock_irqrestore(&m
->lock
, flags
);
356 * If we run out of usable paths, should we queue I/O or error it?
358 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
359 unsigned save_old_value
)
363 spin_lock_irqsave(&m
->lock
, flags
);
366 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
368 m
->saved_queue_if_no_path
= queue_if_no_path
;
369 m
->queue_if_no_path
= queue_if_no_path
;
370 if (!m
->queue_if_no_path
&& m
->queue_size
)
371 queue_work(kmultipathd
, &m
->process_queued_ios
);
373 spin_unlock_irqrestore(&m
->lock
, flags
);
378 /*-----------------------------------------------------------------
379 * The multipath daemon is responsible for resubmitting queued ios.
380 *---------------------------------------------------------------*/
382 static void dispatch_queued_ios(struct multipath
*m
)
386 struct bio
*bio
= NULL
, *next
;
387 struct dm_mpath_io
*mpio
;
388 union map_info
*info
;
390 spin_lock_irqsave(&m
->lock
, flags
);
391 bio
= bio_list_get(&m
->queued_ios
);
392 spin_unlock_irqrestore(&m
->lock
, flags
);
398 info
= dm_get_mapinfo(bio
);
401 r
= map_io(m
, bio
, mpio
, 1);
404 else if (r
== DM_MAPIO_REMAPPED
)
405 generic_make_request(bio
);
406 else if (r
== DM_MAPIO_REQUEUE
)
407 bio_endio(bio
, -EIO
);
413 static void process_queued_ios(struct work_struct
*work
)
415 struct multipath
*m
=
416 container_of(work
, struct multipath
, process_queued_ios
);
417 struct pgpath
*pgpath
= NULL
;
418 unsigned init_required
= 0, must_queue
= 1;
421 spin_lock_irqsave(&m
->lock
, flags
);
426 if (!m
->current_pgpath
)
429 pgpath
= m
->current_pgpath
;
430 m
->pgpath_to_activate
= m
->current_pgpath
;
432 if ((pgpath
&& !m
->queue_io
) ||
433 (!pgpath
&& !m
->queue_if_no_path
))
436 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
438 m
->pg_init_required
= 0;
439 m
->pg_init_in_progress
= 1;
444 spin_unlock_irqrestore(&m
->lock
, flags
);
447 queue_work(kmpath_handlerd
, &m
->activate_path
);
450 dispatch_queued_ios(m
);
454 * An event is triggered whenever a path is taken out of use.
455 * Includes path failure and PG bypass.
457 static void trigger_event(struct work_struct
*work
)
459 struct multipath
*m
=
460 container_of(work
, struct multipath
, trigger_event
);
462 dm_table_event(m
->ti
->table
);
465 /*-----------------------------------------------------------------
466 * Constructor/argument parsing:
467 * <#multipath feature args> [<arg>]*
468 * <#hw_handler args> [hw_handler [<arg>]*]
470 * <initial priority group>
471 * [<selector> <#selector args> [<arg>]*
472 * <#paths> <#per-path selector args>
473 * [<path> [<arg>]* ]+ ]+
474 *---------------------------------------------------------------*/
481 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
484 (sscanf(str
, "%u", v
) != 1) ||
487 *error
= param
->error
;
499 static char *shift(struct arg_set
*as
)
513 static void consume(struct arg_set
*as
, unsigned n
)
515 BUG_ON (as
->argc
< n
);
520 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
521 struct dm_target
*ti
)
524 struct path_selector_type
*pst
;
527 static struct param _params
[] = {
528 {0, 1024, "invalid number of path selector args"},
531 pst
= dm_get_path_selector(shift(as
));
533 ti
->error
= "unknown path selector type";
537 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
539 dm_put_path_selector(pst
);
543 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
545 dm_put_path_selector(pst
);
546 ti
->error
= "path selector constructor failed";
551 consume(as
, ps_argc
);
556 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
557 struct dm_target
*ti
)
561 struct multipath
*m
= ti
->private;
563 /* we need at least a path arg */
565 ti
->error
= "no device given";
573 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
574 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
576 ti
->error
= "error getting device";
580 if (m
->hw_handler_name
) {
581 r
= scsi_dh_attach(bdev_get_queue(p
->path
.dev
->bdev
),
584 dm_put_device(ti
, p
->path
.dev
);
589 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
591 dm_put_device(ti
, p
->path
.dev
);
602 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
605 static struct param _params
[] = {
606 {1, 1024, "invalid number of paths"},
607 {0, 1024, "invalid number of selector args"}
611 unsigned i
, nr_selector_args
, nr_params
;
612 struct priority_group
*pg
;
613 struct dm_target
*ti
= m
->ti
;
617 ti
->error
= "not enough priority group aruments";
621 pg
= alloc_priority_group();
623 ti
->error
= "couldn't allocate priority group";
628 r
= parse_path_selector(as
, pg
, ti
);
635 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
639 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
643 nr_params
= 1 + nr_selector_args
;
644 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
645 struct pgpath
*pgpath
;
646 struct arg_set path_args
;
648 if (as
->argc
< nr_params
) {
649 ti
->error
= "not enough path parameters";
653 path_args
.argc
= nr_params
;
654 path_args
.argv
= as
->argv
;
656 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
661 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
662 consume(as
, nr_params
);
668 free_priority_group(pg
, ti
);
672 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
675 struct dm_target
*ti
= m
->ti
;
677 static struct param _params
[] = {
678 {0, 1024, "invalid number of hardware handler args"},
681 if (read_param(_params
, shift(as
), &hw_argc
, &ti
->error
))
687 m
->hw_handler_name
= kstrdup(shift(as
), GFP_KERNEL
);
688 request_module("scsi_dh_%s", m
->hw_handler_name
);
689 if (scsi_dh_handler_exist(m
->hw_handler_name
) == 0) {
690 ti
->error
= "unknown hardware handler type";
691 kfree(m
->hw_handler_name
);
692 m
->hw_handler_name
= NULL
;
695 consume(as
, hw_argc
- 1);
700 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
704 struct dm_target
*ti
= m
->ti
;
705 const char *param_name
;
707 static struct param _params
[] = {
708 {0, 3, "invalid number of feature args"},
709 {1, 50, "pg_init_retries must be between 1 and 50"},
712 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
720 param_name
= shift(as
);
723 if (!strnicmp(param_name
, MESG_STR("queue_if_no_path"))) {
724 r
= queue_if_no_path(m
, 1, 0);
728 if (!strnicmp(param_name
, MESG_STR("pg_init_retries")) &&
730 r
= read_param(_params
+ 1, shift(as
),
731 &m
->pg_init_retries
, &ti
->error
);
736 ti
->error
= "Unrecognised multipath feature request";
738 } while (argc
&& !r
);
743 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
746 /* target parameters */
747 static struct param _params
[] = {
748 {1, 1024, "invalid number of priority groups"},
749 {1, 1024, "invalid initial priority group number"},
755 unsigned pg_count
= 0;
756 unsigned next_pg_num
;
761 m
= alloc_multipath(ti
);
763 ti
->error
= "can't allocate multipath";
767 r
= parse_features(&as
, m
);
771 r
= parse_hw_handler(&as
, m
);
775 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
779 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
783 /* parse the priority groups */
785 struct priority_group
*pg
;
787 pg
= parse_priority_group(&as
, m
);
793 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
794 list_add_tail(&pg
->list
, &m
->priority_groups
);
796 pg
->pg_num
= pg_count
;
801 if (pg_count
!= m
->nr_priority_groups
) {
802 ti
->error
= "priority group count mismatch";
814 static void multipath_dtr(struct dm_target
*ti
)
816 struct multipath
*m
= (struct multipath
*) ti
->private;
818 flush_workqueue(kmpath_handlerd
);
819 flush_workqueue(kmultipathd
);
824 * Map bios, recording original fields for later in case we have to resubmit
826 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
827 union map_info
*map_context
)
830 struct dm_mpath_io
*mpio
;
831 struct multipath
*m
= (struct multipath
*) ti
->private;
833 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
834 dm_bio_record(&mpio
->details
, bio
);
836 map_context
->ptr
= mpio
;
837 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
838 r
= map_io(m
, bio
, mpio
, 0);
839 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
840 mempool_free(mpio
, m
->mpio_pool
);
846 * Take a path out of use.
848 static int fail_path(struct pgpath
*pgpath
)
851 struct multipath
*m
= pgpath
->pg
->m
;
853 spin_lock_irqsave(&m
->lock
, flags
);
855 if (!pgpath
->path
.is_active
)
858 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
860 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
861 pgpath
->path
.is_active
= 0;
862 pgpath
->fail_count
++;
866 if (pgpath
== m
->current_pgpath
)
867 m
->current_pgpath
= NULL
;
869 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
870 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
872 queue_work(kmultipathd
, &m
->trigger_event
);
875 spin_unlock_irqrestore(&m
->lock
, flags
);
881 * Reinstate a previously-failed path
883 static int reinstate_path(struct pgpath
*pgpath
)
887 struct multipath
*m
= pgpath
->pg
->m
;
889 spin_lock_irqsave(&m
->lock
, flags
);
891 if (pgpath
->path
.is_active
)
894 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
895 DMWARN("Reinstate path not supported by path selector %s",
896 pgpath
->pg
->ps
.type
->name
);
901 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
905 pgpath
->path
.is_active
= 1;
907 m
->current_pgpath
= NULL
;
908 if (!m
->nr_valid_paths
++ && m
->queue_size
)
909 queue_work(kmultipathd
, &m
->process_queued_ios
);
911 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
912 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
914 queue_work(kmultipathd
, &m
->trigger_event
);
917 spin_unlock_irqrestore(&m
->lock
, flags
);
923 * Fail or reinstate all paths that match the provided struct dm_dev.
925 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
929 struct pgpath
*pgpath
;
930 struct priority_group
*pg
;
932 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
933 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
934 if (pgpath
->path
.dev
== dev
)
943 * Temporarily try to avoid having to use the specified PG
945 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
950 spin_lock_irqsave(&m
->lock
, flags
);
952 pg
->bypassed
= bypassed
;
953 m
->current_pgpath
= NULL
;
954 m
->current_pg
= NULL
;
956 spin_unlock_irqrestore(&m
->lock
, flags
);
958 queue_work(kmultipathd
, &m
->trigger_event
);
962 * Switch to using the specified PG from the next I/O that gets mapped
964 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
966 struct priority_group
*pg
;
970 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
971 (pgnum
> m
->nr_priority_groups
)) {
972 DMWARN("invalid PG number supplied to switch_pg_num");
976 spin_lock_irqsave(&m
->lock
, flags
);
977 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
982 m
->current_pgpath
= NULL
;
983 m
->current_pg
= NULL
;
986 spin_unlock_irqrestore(&m
->lock
, flags
);
988 queue_work(kmultipathd
, &m
->trigger_event
);
993 * Set/clear bypassed status of a PG.
994 * PGs are numbered upwards from 1 in the order they were declared.
996 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
998 struct priority_group
*pg
;
1001 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1002 (pgnum
> m
->nr_priority_groups
)) {
1003 DMWARN("invalid PG number supplied to bypass_pg");
1007 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1012 bypass_pg(m
, pg
, bypassed
);
1017 * Should we retry pg_init immediately?
1019 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1021 unsigned long flags
;
1022 int limit_reached
= 0;
1024 spin_lock_irqsave(&m
->lock
, flags
);
1026 if (m
->pg_init_count
<= m
->pg_init_retries
)
1027 m
->pg_init_required
= 1;
1031 spin_unlock_irqrestore(&m
->lock
, flags
);
1033 return limit_reached
;
1036 static void pg_init_done(struct dm_path
*path
, int errors
)
1038 struct pgpath
*pgpath
= path_to_pgpath(path
);
1039 struct priority_group
*pg
= pgpath
->pg
;
1040 struct multipath
*m
= pg
->m
;
1041 unsigned long flags
;
1043 /* device or driver problems */
1048 if (!m
->hw_handler_name
) {
1052 DMERR("Cannot failover device because scsi_dh_%s was not "
1053 "loaded.", m
->hw_handler_name
);
1055 * Fail path for now, so we do not ping pong
1059 case SCSI_DH_DEV_TEMP_BUSY
:
1061 * Probably doing something like FW upgrade on the
1062 * controller so try the other pg.
1064 bypass_pg(m
, pg
, 1);
1066 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1068 case SCSI_DH_IMM_RETRY
:
1069 case SCSI_DH_RES_TEMP_UNAVAIL
:
1070 if (pg_init_limit_reached(m
, pgpath
))
1076 * We probably do not want to fail the path for a device
1077 * error, but this is what the old dm did. In future
1078 * patches we can do more advanced handling.
1083 spin_lock_irqsave(&m
->lock
, flags
);
1085 DMERR("Could not failover device. Error %d.", errors
);
1086 m
->current_pgpath
= NULL
;
1087 m
->current_pg
= NULL
;
1088 } else if (!m
->pg_init_required
) {
1093 m
->pg_init_in_progress
= 0;
1094 queue_work(kmultipathd
, &m
->process_queued_ios
);
1095 spin_unlock_irqrestore(&m
->lock
, flags
);
1098 static void activate_path(struct work_struct
*work
)
1101 struct multipath
*m
=
1102 container_of(work
, struct multipath
, activate_path
);
1103 struct dm_path
*path
;
1104 unsigned long flags
;
1106 spin_lock_irqsave(&m
->lock
, flags
);
1107 path
= &m
->pgpath_to_activate
->path
;
1108 m
->pgpath_to_activate
= NULL
;
1109 spin_unlock_irqrestore(&m
->lock
, flags
);
1112 ret
= scsi_dh_activate(bdev_get_queue(path
->dev
->bdev
));
1113 pg_init_done(path
, ret
);
1119 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
1120 int error
, struct dm_mpath_io
*mpio
)
1122 unsigned long flags
;
1125 return 0; /* I/O complete */
1127 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1130 if (error
== -EOPNOTSUPP
)
1133 spin_lock_irqsave(&m
->lock
, flags
);
1134 if (!m
->nr_valid_paths
) {
1135 if (__must_push_back(m
)) {
1136 spin_unlock_irqrestore(&m
->lock
, flags
);
1137 return DM_ENDIO_REQUEUE
;
1138 } else if (!m
->queue_if_no_path
) {
1139 spin_unlock_irqrestore(&m
->lock
, flags
);
1142 spin_unlock_irqrestore(&m
->lock
, flags
);
1146 spin_unlock_irqrestore(&m
->lock
, flags
);
1149 fail_path(mpio
->pgpath
);
1152 dm_bio_restore(&mpio
->details
, bio
);
1154 /* queue for the daemon to resubmit or fail */
1155 spin_lock_irqsave(&m
->lock
, flags
);
1156 bio_list_add(&m
->queued_ios
, bio
);
1159 queue_work(kmultipathd
, &m
->process_queued_ios
);
1160 spin_unlock_irqrestore(&m
->lock
, flags
);
1162 return DM_ENDIO_INCOMPLETE
; /* io not complete */
1165 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1166 int error
, union map_info
*map_context
)
1168 struct multipath
*m
= ti
->private;
1169 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1170 struct pgpath
*pgpath
= mpio
->pgpath
;
1171 struct path_selector
*ps
;
1174 r
= do_end_io(m
, bio
, error
, mpio
);
1176 ps
= &pgpath
->pg
->ps
;
1177 if (ps
->type
->end_io
)
1178 ps
->type
->end_io(ps
, &pgpath
->path
);
1180 if (r
!= DM_ENDIO_INCOMPLETE
)
1181 mempool_free(mpio
, m
->mpio_pool
);
1187 * Suspend can't complete until all the I/O is processed so if
1188 * the last path fails we must error any remaining I/O.
1189 * Note that if the freeze_bdev fails while suspending, the
1190 * queue_if_no_path state is lost - userspace should reset it.
1192 static void multipath_presuspend(struct dm_target
*ti
)
1194 struct multipath
*m
= (struct multipath
*) ti
->private;
1196 queue_if_no_path(m
, 0, 1);
1200 * Restore the queue_if_no_path setting.
1202 static void multipath_resume(struct dm_target
*ti
)
1204 struct multipath
*m
= (struct multipath
*) ti
->private;
1205 unsigned long flags
;
1207 spin_lock_irqsave(&m
->lock
, flags
);
1208 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1209 spin_unlock_irqrestore(&m
->lock
, flags
);
1213 * Info output has the following format:
1214 * num_multipath_feature_args [multipath_feature_args]*
1215 * num_handler_status_args [handler_status_args]*
1216 * num_groups init_group_number
1217 * [A|D|E num_ps_status_args [ps_status_args]*
1218 * num_paths num_selector_args
1219 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1221 * Table output has the following format (identical to the constructor string):
1222 * num_feature_args [features_args]*
1223 * num_handler_args hw_handler [hw_handler_args]*
1224 * num_groups init_group_number
1225 * [priority selector-name num_ps_args [ps_args]*
1226 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1228 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1229 char *result
, unsigned int maxlen
)
1232 unsigned long flags
;
1233 struct multipath
*m
= (struct multipath
*) ti
->private;
1234 struct priority_group
*pg
;
1239 spin_lock_irqsave(&m
->lock
, flags
);
1242 if (type
== STATUSTYPE_INFO
)
1243 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1245 DMEMIT("%u ", m
->queue_if_no_path
+
1246 (m
->pg_init_retries
> 0) * 2);
1247 if (m
->queue_if_no_path
)
1248 DMEMIT("queue_if_no_path ");
1249 if (m
->pg_init_retries
)
1250 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1253 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1256 DMEMIT("1 %s ", m
->hw_handler_name
);
1258 DMEMIT("%u ", m
->nr_priority_groups
);
1261 pg_num
= m
->next_pg
->pg_num
;
1262 else if (m
->current_pg
)
1263 pg_num
= m
->current_pg
->pg_num
;
1267 DMEMIT("%u ", pg_num
);
1270 case STATUSTYPE_INFO
:
1271 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1273 state
= 'D'; /* Disabled */
1274 else if (pg
== m
->current_pg
)
1275 state
= 'A'; /* Currently Active */
1277 state
= 'E'; /* Enabled */
1279 DMEMIT("%c ", state
);
1281 if (pg
->ps
.type
->status
)
1282 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1288 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1289 pg
->ps
.type
->info_args
);
1291 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1292 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1293 p
->path
.is_active
? "A" : "F",
1295 if (pg
->ps
.type
->status
)
1296 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1297 &p
->path
, type
, result
+ sz
,
1303 case STATUSTYPE_TABLE
:
1304 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1305 DMEMIT("%s ", pg
->ps
.type
->name
);
1307 if (pg
->ps
.type
->status
)
1308 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1314 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1315 pg
->ps
.type
->table_args
);
1317 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1318 DMEMIT("%s ", p
->path
.dev
->name
);
1319 if (pg
->ps
.type
->status
)
1320 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1321 &p
->path
, type
, result
+ sz
,
1328 spin_unlock_irqrestore(&m
->lock
, flags
);
1333 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1337 struct multipath
*m
= (struct multipath
*) ti
->private;
1341 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1342 return queue_if_no_path(m
, 1, 0);
1343 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1344 return queue_if_no_path(m
, 0, 0);
1350 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1351 return bypass_pg_num(m
, argv
[1], 1);
1352 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1353 return bypass_pg_num(m
, argv
[1], 0);
1354 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1355 return switch_pg_num(m
, argv
[1]);
1356 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1357 action
= reinstate_path
;
1358 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1363 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1364 dm_table_get_mode(ti
->table
), &dev
);
1366 DMWARN("message: error getting device %s",
1371 r
= action_dev(m
, dev
, action
);
1373 dm_put_device(ti
, dev
);
1378 DMWARN("Unrecognised multipath message received.");
1382 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1383 struct file
*filp
, unsigned int cmd
,
1386 struct multipath
*m
= (struct multipath
*) ti
->private;
1387 struct block_device
*bdev
= NULL
;
1388 unsigned long flags
;
1389 struct file fake_file
= {};
1390 struct dentry fake_dentry
= {};
1393 fake_file
.f_path
.dentry
= &fake_dentry
;
1395 spin_lock_irqsave(&m
->lock
, flags
);
1397 if (!m
->current_pgpath
)
1400 if (m
->current_pgpath
) {
1401 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1402 fake_dentry
.d_inode
= bdev
->bd_inode
;
1403 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1411 spin_unlock_irqrestore(&m
->lock
, flags
);
1413 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1414 bdev
->bd_disk
, cmd
, arg
);
1417 /*-----------------------------------------------------------------
1419 *---------------------------------------------------------------*/
1420 static struct target_type multipath_target
= {
1421 .name
= "multipath",
1422 .version
= {1, 0, 5},
1423 .module
= THIS_MODULE
,
1424 .ctr
= multipath_ctr
,
1425 .dtr
= multipath_dtr
,
1426 .map
= multipath_map
,
1427 .end_io
= multipath_end_io
,
1428 .presuspend
= multipath_presuspend
,
1429 .resume
= multipath_resume
,
1430 .status
= multipath_status
,
1431 .message
= multipath_message
,
1432 .ioctl
= multipath_ioctl
,
1435 static int __init
dm_multipath_init(void)
1439 /* allocate a slab for the dm_ios */
1440 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1444 r
= dm_register_target(&multipath_target
);
1446 DMERR("register failed %d", r
);
1447 kmem_cache_destroy(_mpio_cache
);
1451 kmultipathd
= create_workqueue("kmpathd");
1453 DMERR("failed to create workqueue kmpathd");
1454 dm_unregister_target(&multipath_target
);
1455 kmem_cache_destroy(_mpio_cache
);
1460 * A separate workqueue is used to handle the device handlers
1461 * to avoid overloading existing workqueue. Overloading the
1462 * old workqueue would also create a bottleneck in the
1463 * path of the storage hardware device activation.
1465 kmpath_handlerd
= create_singlethread_workqueue("kmpath_handlerd");
1466 if (!kmpath_handlerd
) {
1467 DMERR("failed to create workqueue kmpath_handlerd");
1468 destroy_workqueue(kmultipathd
);
1469 dm_unregister_target(&multipath_target
);
1470 kmem_cache_destroy(_mpio_cache
);
1474 DMINFO("version %u.%u.%u loaded",
1475 multipath_target
.version
[0], multipath_target
.version
[1],
1476 multipath_target
.version
[2]);
1481 static void __exit
dm_multipath_exit(void)
1485 destroy_workqueue(kmpath_handlerd
);
1486 destroy_workqueue(kmultipathd
);
1488 r
= dm_unregister_target(&multipath_target
);
1490 DMERR("target unregister failed %d", r
);
1491 kmem_cache_destroy(_mpio_cache
);
1494 module_init(dm_multipath_init
);
1495 module_exit(dm_multipath_exit
);
1497 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1498 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1499 MODULE_LICENSE("GPL");