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-hw-handler.h"
11 #include "dm-bio-list.h"
12 #include "dm-bio-record.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 <asm/atomic.h>
24 #define DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
29 struct list_head list
;
31 struct priority_group
*pg
; /* Owning PG */
32 unsigned fail_count
; /* Cumulative failure count */
37 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40 * Paths are grouped into Priority Groups and numbered from 1 upwards.
41 * Each has a path selector which controls which path gets used.
43 struct priority_group
{
44 struct list_head list
;
46 struct multipath
*m
; /* Owning multipath instance */
47 struct path_selector ps
;
49 unsigned pg_num
; /* Reference number */
50 unsigned bypassed
; /* Temporarily bypass this PG? */
52 unsigned nr_pgpaths
; /* Number of paths in PG */
53 struct list_head pgpaths
;
56 /* Multipath context */
58 struct list_head list
;
63 struct hw_handler hw_handler
;
64 unsigned nr_priority_groups
;
65 struct list_head priority_groups
;
66 unsigned pg_init_required
; /* pg_init needs calling? */
67 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
69 unsigned nr_valid_paths
; /* Total number of usable paths */
70 struct pgpath
*current_pgpath
;
71 struct priority_group
*current_pg
;
72 struct priority_group
*next_pg
; /* Switch to this PG if set */
73 unsigned repeat_count
; /* I/Os left before calling PS again */
75 unsigned queue_io
; /* Must we queue all I/O? */
76 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
77 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
79 struct work_struct process_queued_ios
;
80 struct bio_list queued_ios
;
83 struct work_struct trigger_event
;
86 * We must use a mempool of mpath_io structs so that we
87 * can resubmit bios on error.
93 * Context information attached to each bio we process.
96 struct pgpath
*pgpath
;
97 struct dm_bio_details details
;
100 typedef int (*action_fn
) (struct pgpath
*pgpath
);
102 #define MIN_IOS 256 /* Mempool size */
104 static kmem_cache_t
*_mpio_cache
;
106 struct workqueue_struct
*kmultipathd
;
107 static void process_queued_ios(struct work_struct
*work
);
108 static void trigger_event(struct work_struct
*work
);
111 /*-----------------------------------------------
112 * Allocation routines
113 *-----------------------------------------------*/
115 static struct pgpath
*alloc_pgpath(void)
117 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
120 pgpath
->path
.is_active
= 1;
125 static inline void free_pgpath(struct pgpath
*pgpath
)
130 static struct priority_group
*alloc_priority_group(void)
132 struct priority_group
*pg
;
134 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
137 INIT_LIST_HEAD(&pg
->pgpaths
);
142 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
144 struct pgpath
*pgpath
, *tmp
;
146 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
147 list_del(&pgpath
->list
);
148 dm_put_device(ti
, pgpath
->path
.dev
);
153 static void free_priority_group(struct priority_group
*pg
,
154 struct dm_target
*ti
)
156 struct path_selector
*ps
= &pg
->ps
;
159 ps
->type
->destroy(ps
);
160 dm_put_path_selector(ps
->type
);
163 free_pgpaths(&pg
->pgpaths
, ti
);
167 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
171 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
173 INIT_LIST_HEAD(&m
->priority_groups
);
174 spin_lock_init(&m
->lock
);
176 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
177 INIT_WORK(&m
->trigger_event
, trigger_event
);
178 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
190 static void free_multipath(struct multipath
*m
)
192 struct priority_group
*pg
, *tmp
;
193 struct hw_handler
*hwh
= &m
->hw_handler
;
195 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
197 free_priority_group(pg
, m
->ti
);
201 hwh
->type
->destroy(hwh
);
202 dm_put_hw_handler(hwh
->type
);
205 mempool_destroy(m
->mpio_pool
);
210 /*-----------------------------------------------
212 *-----------------------------------------------*/
214 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
216 struct hw_handler
*hwh
= &m
->hw_handler
;
218 m
->current_pg
= pgpath
->pg
;
220 /* Must we initialise the PG first, and queue I/O till it's ready? */
221 if (hwh
->type
&& hwh
->type
->pg_init
) {
222 m
->pg_init_required
= 1;
225 m
->pg_init_required
= 0;
230 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
)
234 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
);
238 m
->current_pgpath
= path_to_pgpath(path
);
240 if (m
->current_pg
!= pg
)
241 __switch_pg(m
, m
->current_pgpath
);
246 static void __choose_pgpath(struct multipath
*m
)
248 struct priority_group
*pg
;
249 unsigned bypassed
= 1;
251 if (!m
->nr_valid_paths
)
254 /* Were we instructed to switch PG? */
258 if (!__choose_path_in_pg(m
, pg
))
262 /* Don't change PG until it has no remaining paths */
263 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
))
267 * Loop through priority groups until we find a valid path.
268 * First time we skip PGs marked 'bypassed'.
269 * Second time we only try the ones we skipped.
272 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
273 if (pg
->bypassed
== bypassed
)
275 if (!__choose_path_in_pg(m
, pg
))
278 } while (bypassed
--);
281 m
->current_pgpath
= NULL
;
282 m
->current_pg
= NULL
;
285 static int map_io(struct multipath
*m
, struct bio
*bio
, struct mpath_io
*mpio
,
290 struct pgpath
*pgpath
;
292 spin_lock_irqsave(&m
->lock
, flags
);
294 /* Do we need to select a new pgpath? */
295 if (!m
->current_pgpath
||
296 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
299 pgpath
= m
->current_pgpath
;
304 if ((pgpath
&& m
->queue_io
) ||
305 (!pgpath
&& m
->queue_if_no_path
)) {
306 /* Queue for the daemon to resubmit */
307 bio_list_add(&m
->queued_ios
, bio
);
309 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
311 queue_work(kmultipathd
, &m
->process_queued_ios
);
315 r
= -EIO
; /* Failed */
317 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
319 mpio
->pgpath
= pgpath
;
321 spin_unlock_irqrestore(&m
->lock
, flags
);
327 * If we run out of usable paths, should we queue I/O or error it?
329 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
330 unsigned save_old_value
)
334 spin_lock_irqsave(&m
->lock
, flags
);
337 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
339 m
->saved_queue_if_no_path
= queue_if_no_path
;
340 m
->queue_if_no_path
= queue_if_no_path
;
341 if (!m
->queue_if_no_path
&& m
->queue_size
)
342 queue_work(kmultipathd
, &m
->process_queued_ios
);
344 spin_unlock_irqrestore(&m
->lock
, flags
);
349 /*-----------------------------------------------------------------
350 * The multipath daemon is responsible for resubmitting queued ios.
351 *---------------------------------------------------------------*/
353 static void dispatch_queued_ios(struct multipath
*m
)
357 struct bio
*bio
= NULL
, *next
;
358 struct mpath_io
*mpio
;
359 union map_info
*info
;
361 spin_lock_irqsave(&m
->lock
, flags
);
362 bio
= bio_list_get(&m
->queued_ios
);
363 spin_unlock_irqrestore(&m
->lock
, flags
);
369 info
= dm_get_mapinfo(bio
);
372 r
= map_io(m
, bio
, mpio
, 1);
374 bio_endio(bio
, bio
->bi_size
, r
);
376 generic_make_request(bio
);
382 static void process_queued_ios(struct work_struct
*work
)
384 struct multipath
*m
=
385 container_of(work
, struct multipath
, process_queued_ios
);
386 struct hw_handler
*hwh
= &m
->hw_handler
;
387 struct pgpath
*pgpath
= NULL
;
388 unsigned init_required
= 0, must_queue
= 1;
391 spin_lock_irqsave(&m
->lock
, flags
);
396 if (!m
->current_pgpath
)
399 pgpath
= m
->current_pgpath
;
401 if ((pgpath
&& !m
->queue_io
) ||
402 (!pgpath
&& !m
->queue_if_no_path
))
405 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
406 m
->pg_init_required
= 0;
407 m
->pg_init_in_progress
= 1;
412 spin_unlock_irqrestore(&m
->lock
, flags
);
415 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
418 dispatch_queued_ios(m
);
422 * An event is triggered whenever a path is taken out of use.
423 * Includes path failure and PG bypass.
425 static void trigger_event(struct work_struct
*work
)
427 struct multipath
*m
=
428 container_of(work
, struct multipath
, trigger_event
);
430 dm_table_event(m
->ti
->table
);
433 /*-----------------------------------------------------------------
434 * Constructor/argument parsing:
435 * <#multipath feature args> [<arg>]*
436 * <#hw_handler args> [hw_handler [<arg>]*]
438 * <initial priority group>
439 * [<selector> <#selector args> [<arg>]*
440 * <#paths> <#per-path selector args>
441 * [<path> [<arg>]* ]+ ]+
442 *---------------------------------------------------------------*/
449 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
452 (sscanf(str
, "%u", v
) != 1) ||
455 *error
= param
->error
;
467 static char *shift(struct arg_set
*as
)
481 static void consume(struct arg_set
*as
, unsigned n
)
483 BUG_ON (as
->argc
< n
);
488 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
489 struct dm_target
*ti
)
492 struct path_selector_type
*pst
;
495 static struct param _params
[] = {
496 {0, 1024, "invalid number of path selector args"},
499 pst
= dm_get_path_selector(shift(as
));
501 ti
->error
= "unknown path selector type";
505 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
509 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
511 dm_put_path_selector(pst
);
512 ti
->error
= "path selector constructor failed";
517 consume(as
, ps_argc
);
522 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
523 struct dm_target
*ti
)
528 /* we need at least a path arg */
530 ti
->error
= "no device given";
538 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
539 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
541 ti
->error
= "error getting device";
545 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
547 dm_put_device(ti
, p
->path
.dev
);
558 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
561 static struct param _params
[] = {
562 {1, 1024, "invalid number of paths"},
563 {0, 1024, "invalid number of selector args"}
567 unsigned i
, nr_selector_args
, nr_params
;
568 struct priority_group
*pg
;
569 struct dm_target
*ti
= m
->ti
;
573 ti
->error
= "not enough priority group aruments";
577 pg
= alloc_priority_group();
579 ti
->error
= "couldn't allocate priority group";
584 r
= parse_path_selector(as
, pg
, ti
);
591 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
595 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
599 nr_params
= 1 + nr_selector_args
;
600 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
601 struct pgpath
*pgpath
;
602 struct arg_set path_args
;
604 if (as
->argc
< nr_params
)
607 path_args
.argc
= nr_params
;
608 path_args
.argv
= as
->argv
;
610 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
615 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
616 consume(as
, nr_params
);
622 free_priority_group(pg
, ti
);
626 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
629 struct hw_handler_type
*hwht
;
631 struct dm_target
*ti
= m
->ti
;
633 static struct param _params
[] = {
634 {0, 1024, "invalid number of hardware handler args"},
637 r
= read_param(_params
, shift(as
), &hw_argc
, &ti
->error
);
644 hwht
= dm_get_hw_handler(shift(as
));
646 ti
->error
= "unknown hardware handler type";
650 r
= hwht
->create(&m
->hw_handler
, hw_argc
- 1, as
->argv
);
652 dm_put_hw_handler(hwht
);
653 ti
->error
= "hardware handler constructor failed";
657 m
->hw_handler
.type
= hwht
;
658 consume(as
, hw_argc
- 1);
663 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
667 struct dm_target
*ti
= m
->ti
;
669 static struct param _params
[] = {
670 {0, 1, "invalid number of feature args"},
673 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
680 if (!strnicmp(shift(as
), MESG_STR("queue_if_no_path")))
681 return queue_if_no_path(m
, 1, 0);
683 ti
->error
= "Unrecognised multipath feature request";
688 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
691 /* target parameters */
692 static struct param _params
[] = {
693 {1, 1024, "invalid number of priority groups"},
694 {1, 1024, "invalid initial priority group number"},
700 unsigned pg_count
= 0;
701 unsigned next_pg_num
;
706 m
= alloc_multipath(ti
);
708 ti
->error
= "can't allocate multipath";
712 r
= parse_features(&as
, m
);
716 r
= parse_hw_handler(&as
, m
);
720 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
724 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
728 /* parse the priority groups */
730 struct priority_group
*pg
;
732 pg
= parse_priority_group(&as
, m
);
738 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
739 list_add_tail(&pg
->list
, &m
->priority_groups
);
741 pg
->pg_num
= pg_count
;
746 if (pg_count
!= m
->nr_priority_groups
) {
747 ti
->error
= "priority group count mismatch";
759 static void multipath_dtr(struct dm_target
*ti
)
761 struct multipath
*m
= (struct multipath
*) ti
->private;
763 flush_workqueue(kmultipathd
);
768 * Map bios, recording original fields for later in case we have to resubmit
770 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
771 union map_info
*map_context
)
774 struct mpath_io
*mpio
;
775 struct multipath
*m
= (struct multipath
*) ti
->private;
777 if (bio_barrier(bio
))
780 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
781 dm_bio_record(&mpio
->details
, bio
);
783 map_context
->ptr
= mpio
;
784 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
785 r
= map_io(m
, bio
, mpio
, 0);
787 mempool_free(mpio
, m
->mpio_pool
);
793 * Take a path out of use.
795 static int fail_path(struct pgpath
*pgpath
)
798 struct multipath
*m
= pgpath
->pg
->m
;
800 spin_lock_irqsave(&m
->lock
, flags
);
802 if (!pgpath
->path
.is_active
)
805 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
807 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
808 pgpath
->path
.is_active
= 0;
809 pgpath
->fail_count
++;
813 if (pgpath
== m
->current_pgpath
)
814 m
->current_pgpath
= NULL
;
816 queue_work(kmultipathd
, &m
->trigger_event
);
819 spin_unlock_irqrestore(&m
->lock
, flags
);
825 * Reinstate a previously-failed path
827 static int reinstate_path(struct pgpath
*pgpath
)
831 struct multipath
*m
= pgpath
->pg
->m
;
833 spin_lock_irqsave(&m
->lock
, flags
);
835 if (pgpath
->path
.is_active
)
838 if (!pgpath
->pg
->ps
.type
) {
839 DMWARN("Reinstate path not supported by path selector %s",
840 pgpath
->pg
->ps
.type
->name
);
845 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
849 pgpath
->path
.is_active
= 1;
851 m
->current_pgpath
= NULL
;
852 if (!m
->nr_valid_paths
++ && m
->queue_size
)
853 queue_work(kmultipathd
, &m
->process_queued_ios
);
855 queue_work(kmultipathd
, &m
->trigger_event
);
858 spin_unlock_irqrestore(&m
->lock
, flags
);
864 * Fail or reinstate all paths that match the provided struct dm_dev.
866 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
870 struct pgpath
*pgpath
;
871 struct priority_group
*pg
;
873 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
874 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
875 if (pgpath
->path
.dev
== dev
)
884 * Temporarily try to avoid having to use the specified PG
886 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
891 spin_lock_irqsave(&m
->lock
, flags
);
893 pg
->bypassed
= bypassed
;
894 m
->current_pgpath
= NULL
;
895 m
->current_pg
= NULL
;
897 spin_unlock_irqrestore(&m
->lock
, flags
);
899 queue_work(kmultipathd
, &m
->trigger_event
);
903 * Switch to using the specified PG from the next I/O that gets mapped
905 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
907 struct priority_group
*pg
;
911 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
912 (pgnum
> m
->nr_priority_groups
)) {
913 DMWARN("invalid PG number supplied to switch_pg_num");
917 spin_lock_irqsave(&m
->lock
, flags
);
918 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
923 m
->current_pgpath
= NULL
;
924 m
->current_pg
= NULL
;
927 spin_unlock_irqrestore(&m
->lock
, flags
);
929 queue_work(kmultipathd
, &m
->trigger_event
);
934 * Set/clear bypassed status of a PG.
935 * PGs are numbered upwards from 1 in the order they were declared.
937 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
939 struct priority_group
*pg
;
942 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
943 (pgnum
> m
->nr_priority_groups
)) {
944 DMWARN("invalid PG number supplied to bypass_pg");
948 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
953 bypass_pg(m
, pg
, bypassed
);
958 * pg_init must call this when it has completed its initialisation
960 void dm_pg_init_complete(struct path
*path
, unsigned err_flags
)
962 struct pgpath
*pgpath
= path_to_pgpath(path
);
963 struct priority_group
*pg
= pgpath
->pg
;
964 struct multipath
*m
= pg
->m
;
967 /* We insist on failing the path if the PG is already bypassed. */
968 if (err_flags
&& pg
->bypassed
)
969 err_flags
|= MP_FAIL_PATH
;
971 if (err_flags
& MP_FAIL_PATH
)
974 if (err_flags
& MP_BYPASS_PG
)
977 spin_lock_irqsave(&m
->lock
, flags
);
979 m
->current_pgpath
= NULL
;
980 m
->current_pg
= NULL
;
981 } else if (!m
->pg_init_required
)
984 m
->pg_init_in_progress
= 0;
985 queue_work(kmultipathd
, &m
->process_queued_ios
);
986 spin_unlock_irqrestore(&m
->lock
, flags
);
992 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
993 int error
, struct mpath_io
*mpio
)
995 struct hw_handler
*hwh
= &m
->hw_handler
;
996 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
1000 return 0; /* I/O complete */
1002 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1005 if (error
== -EOPNOTSUPP
)
1008 spin_lock_irqsave(&m
->lock
, flags
);
1009 if (!m
->nr_valid_paths
) {
1010 if (!m
->queue_if_no_path
) {
1011 spin_unlock_irqrestore(&m
->lock
, flags
);
1014 spin_unlock_irqrestore(&m
->lock
, flags
);
1018 spin_unlock_irqrestore(&m
->lock
, flags
);
1020 if (hwh
->type
&& hwh
->type
->error
)
1021 err_flags
= hwh
->type
->error(hwh
, bio
);
1024 if (err_flags
& MP_FAIL_PATH
)
1025 fail_path(mpio
->pgpath
);
1027 if (err_flags
& MP_BYPASS_PG
)
1028 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1031 if (err_flags
& MP_ERROR_IO
)
1035 dm_bio_restore(&mpio
->details
, bio
);
1037 /* queue for the daemon to resubmit or fail */
1038 spin_lock_irqsave(&m
->lock
, flags
);
1039 bio_list_add(&m
->queued_ios
, bio
);
1042 queue_work(kmultipathd
, &m
->process_queued_ios
);
1043 spin_unlock_irqrestore(&m
->lock
, flags
);
1045 return 1; /* io not complete */
1048 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1049 int error
, union map_info
*map_context
)
1051 struct multipath
*m
= (struct multipath
*) ti
->private;
1052 struct mpath_io
*mpio
= (struct mpath_io
*) map_context
->ptr
;
1053 struct pgpath
*pgpath
= mpio
->pgpath
;
1054 struct path_selector
*ps
;
1057 r
= do_end_io(m
, bio
, error
, mpio
);
1059 ps
= &pgpath
->pg
->ps
;
1060 if (ps
->type
->end_io
)
1061 ps
->type
->end_io(ps
, &pgpath
->path
);
1064 mempool_free(mpio
, m
->mpio_pool
);
1070 * Suspend can't complete until all the I/O is processed so if
1071 * the last path fails we must error any remaining I/O.
1072 * Note that if the freeze_bdev fails while suspending, the
1073 * queue_if_no_path state is lost - userspace should reset it.
1075 static void multipath_presuspend(struct dm_target
*ti
)
1077 struct multipath
*m
= (struct multipath
*) ti
->private;
1079 queue_if_no_path(m
, 0, 1);
1083 * Restore the queue_if_no_path setting.
1085 static void multipath_resume(struct dm_target
*ti
)
1087 struct multipath
*m
= (struct multipath
*) ti
->private;
1088 unsigned long flags
;
1090 spin_lock_irqsave(&m
->lock
, flags
);
1091 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1092 spin_unlock_irqrestore(&m
->lock
, flags
);
1096 * Info output has the following format:
1097 * num_multipath_feature_args [multipath_feature_args]*
1098 * num_handler_status_args [handler_status_args]*
1099 * num_groups init_group_number
1100 * [A|D|E num_ps_status_args [ps_status_args]*
1101 * num_paths num_selector_args
1102 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1104 * Table output has the following format (identical to the constructor string):
1105 * num_feature_args [features_args]*
1106 * num_handler_args hw_handler [hw_handler_args]*
1107 * num_groups init_group_number
1108 * [priority selector-name num_ps_args [ps_args]*
1109 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1111 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1112 char *result
, unsigned int maxlen
)
1115 unsigned long flags
;
1116 struct multipath
*m
= (struct multipath
*) ti
->private;
1117 struct hw_handler
*hwh
= &m
->hw_handler
;
1118 struct priority_group
*pg
;
1123 spin_lock_irqsave(&m
->lock
, flags
);
1126 if (type
== STATUSTYPE_INFO
)
1127 DMEMIT("1 %u ", m
->queue_size
);
1128 else if (m
->queue_if_no_path
)
1129 DMEMIT("1 queue_if_no_path ");
1133 if (hwh
->type
&& hwh
->type
->status
)
1134 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1135 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1138 DMEMIT("1 %s ", hwh
->type
->name
);
1140 DMEMIT("%u ", m
->nr_priority_groups
);
1143 pg_num
= m
->next_pg
->pg_num
;
1144 else if (m
->current_pg
)
1145 pg_num
= m
->current_pg
->pg_num
;
1149 DMEMIT("%u ", pg_num
);
1152 case STATUSTYPE_INFO
:
1153 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1155 state
= 'D'; /* Disabled */
1156 else if (pg
== m
->current_pg
)
1157 state
= 'A'; /* Currently Active */
1159 state
= 'E'; /* Enabled */
1161 DMEMIT("%c ", state
);
1163 if (pg
->ps
.type
->status
)
1164 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1170 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1171 pg
->ps
.type
->info_args
);
1173 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1174 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1175 p
->path
.is_active
? "A" : "F",
1177 if (pg
->ps
.type
->status
)
1178 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1179 &p
->path
, type
, result
+ sz
,
1185 case STATUSTYPE_TABLE
:
1186 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1187 DMEMIT("%s ", pg
->ps
.type
->name
);
1189 if (pg
->ps
.type
->status
)
1190 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1196 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1197 pg
->ps
.type
->table_args
);
1199 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1200 DMEMIT("%s ", p
->path
.dev
->name
);
1201 if (pg
->ps
.type
->status
)
1202 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1203 &p
->path
, type
, result
+ sz
,
1210 spin_unlock_irqrestore(&m
->lock
, flags
);
1215 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1219 struct multipath
*m
= (struct multipath
*) ti
->private;
1223 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1224 return queue_if_no_path(m
, 1, 0);
1225 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1226 return queue_if_no_path(m
, 0, 0);
1232 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1233 return bypass_pg_num(m
, argv
[1], 1);
1234 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1235 return bypass_pg_num(m
, argv
[1], 0);
1236 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1237 return switch_pg_num(m
, argv
[1]);
1238 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1239 action
= reinstate_path
;
1240 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1245 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1246 dm_table_get_mode(ti
->table
), &dev
);
1248 DMWARN("message: error getting device %s",
1253 r
= action_dev(m
, dev
, action
);
1255 dm_put_device(ti
, dev
);
1260 DMWARN("Unrecognised multipath message received.");
1264 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1265 struct file
*filp
, unsigned int cmd
,
1268 struct multipath
*m
= (struct multipath
*) ti
->private;
1269 struct block_device
*bdev
= NULL
;
1270 unsigned long flags
;
1271 struct file fake_file
= {};
1272 struct dentry fake_dentry
= {};
1275 fake_file
.f_dentry
= &fake_dentry
;
1277 spin_lock_irqsave(&m
->lock
, flags
);
1279 if (!m
->current_pgpath
)
1282 if (m
->current_pgpath
) {
1283 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1284 fake_dentry
.d_inode
= bdev
->bd_inode
;
1285 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1293 spin_unlock_irqrestore(&m
->lock
, flags
);
1295 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1296 bdev
->bd_disk
, cmd
, arg
);
1299 /*-----------------------------------------------------------------
1301 *---------------------------------------------------------------*/
1302 static struct target_type multipath_target
= {
1303 .name
= "multipath",
1304 .version
= {1, 0, 5},
1305 .module
= THIS_MODULE
,
1306 .ctr
= multipath_ctr
,
1307 .dtr
= multipath_dtr
,
1308 .map
= multipath_map
,
1309 .end_io
= multipath_end_io
,
1310 .presuspend
= multipath_presuspend
,
1311 .resume
= multipath_resume
,
1312 .status
= multipath_status
,
1313 .message
= multipath_message
,
1314 .ioctl
= multipath_ioctl
,
1317 static int __init
dm_multipath_init(void)
1321 /* allocate a slab for the dm_ios */
1322 _mpio_cache
= kmem_cache_create("dm_mpath", sizeof(struct mpath_io
),
1327 r
= dm_register_target(&multipath_target
);
1329 DMERR("%s: register failed %d", multipath_target
.name
, r
);
1330 kmem_cache_destroy(_mpio_cache
);
1334 kmultipathd
= create_workqueue("kmpathd");
1336 DMERR("%s: failed to create workqueue kmpathd",
1337 multipath_target
.name
);
1338 dm_unregister_target(&multipath_target
);
1339 kmem_cache_destroy(_mpio_cache
);
1343 DMINFO("version %u.%u.%u loaded",
1344 multipath_target
.version
[0], multipath_target
.version
[1],
1345 multipath_target
.version
[2]);
1350 static void __exit
dm_multipath_exit(void)
1354 destroy_workqueue(kmultipathd
);
1356 r
= dm_unregister_target(&multipath_target
);
1358 DMERR("%s: target unregister failed %d",
1359 multipath_target
.name
, r
);
1360 kmem_cache_destroy(_mpio_cache
);
1363 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1365 module_init(dm_multipath_init
);
1366 module_exit(dm_multipath_exit
);
1368 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1369 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1370 MODULE_LICENSE("GPL");