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(void *data
);
108 static void trigger_event(void *data
);
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
, m
);
177 INIT_WORK(&m
->trigger_event
, trigger_event
, m
);
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(void *data
)
384 struct multipath
*m
= (struct multipath
*) data
;
385 struct hw_handler
*hwh
= &m
->hw_handler
;
386 struct pgpath
*pgpath
= NULL
;
387 unsigned init_required
= 0, must_queue
= 1;
390 spin_lock_irqsave(&m
->lock
, flags
);
395 if (!m
->current_pgpath
)
398 pgpath
= m
->current_pgpath
;
400 if ((pgpath
&& !m
->queue_io
) ||
401 (!pgpath
&& !m
->queue_if_no_path
))
404 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
405 m
->pg_init_required
= 0;
406 m
->pg_init_in_progress
= 1;
411 spin_unlock_irqrestore(&m
->lock
, flags
);
414 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
417 dispatch_queued_ios(m
);
421 * An event is triggered whenever a path is taken out of use.
422 * Includes path failure and PG bypass.
424 static void trigger_event(void *data
)
426 struct multipath
*m
= (struct multipath
*) data
;
428 dm_table_event(m
->ti
->table
);
431 /*-----------------------------------------------------------------
432 * Constructor/argument parsing:
433 * <#multipath feature args> [<arg>]*
434 * <#hw_handler args> [hw_handler [<arg>]*]
436 * <initial priority group>
437 * [<selector> <#selector args> [<arg>]*
438 * <#paths> <#per-path selector args>
439 * [<path> [<arg>]* ]+ ]+
440 *---------------------------------------------------------------*/
447 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
450 (sscanf(str
, "%u", v
) != 1) ||
453 *error
= param
->error
;
465 static char *shift(struct arg_set
*as
)
479 static void consume(struct arg_set
*as
, unsigned n
)
481 BUG_ON (as
->argc
< n
);
486 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
487 struct dm_target
*ti
)
490 struct path_selector_type
*pst
;
493 static struct param _params
[] = {
494 {0, 1024, "invalid number of path selector args"},
497 pst
= dm_get_path_selector(shift(as
));
499 ti
->error
= "unknown path selector type";
503 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
507 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
509 dm_put_path_selector(pst
);
510 ti
->error
= "path selector constructor failed";
515 consume(as
, ps_argc
);
520 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
521 struct dm_target
*ti
)
526 /* we need at least a path arg */
528 ti
->error
= "no device given";
536 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
537 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
539 ti
->error
= "error getting device";
543 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
545 dm_put_device(ti
, p
->path
.dev
);
556 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
559 static struct param _params
[] = {
560 {1, 1024, "invalid number of paths"},
561 {0, 1024, "invalid number of selector args"}
565 unsigned i
, nr_selector_args
, nr_params
;
566 struct priority_group
*pg
;
567 struct dm_target
*ti
= m
->ti
;
571 ti
->error
= "not enough priority group aruments";
575 pg
= alloc_priority_group();
577 ti
->error
= "couldn't allocate priority group";
582 r
= parse_path_selector(as
, pg
, ti
);
589 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
593 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
597 nr_params
= 1 + nr_selector_args
;
598 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
599 struct pgpath
*pgpath
;
600 struct arg_set path_args
;
602 if (as
->argc
< nr_params
)
605 path_args
.argc
= nr_params
;
606 path_args
.argv
= as
->argv
;
608 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
613 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
614 consume(as
, nr_params
);
620 free_priority_group(pg
, ti
);
624 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
627 struct hw_handler_type
*hwht
;
629 struct dm_target
*ti
= m
->ti
;
631 static struct param _params
[] = {
632 {0, 1024, "invalid number of hardware handler args"},
635 r
= read_param(_params
, shift(as
), &hw_argc
, &ti
->error
);
642 hwht
= dm_get_hw_handler(shift(as
));
644 ti
->error
= "unknown hardware handler type";
648 r
= hwht
->create(&m
->hw_handler
, hw_argc
- 1, as
->argv
);
650 dm_put_hw_handler(hwht
);
651 ti
->error
= "hardware handler constructor failed";
655 m
->hw_handler
.type
= hwht
;
656 consume(as
, hw_argc
- 1);
661 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
665 struct dm_target
*ti
= m
->ti
;
667 static struct param _params
[] = {
668 {0, 1, "invalid number of feature args"},
671 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
678 if (!strnicmp(shift(as
), MESG_STR("queue_if_no_path")))
679 return queue_if_no_path(m
, 1, 0);
681 ti
->error
= "Unrecognised multipath feature request";
686 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
689 /* target parameters */
690 static struct param _params
[] = {
691 {1, 1024, "invalid number of priority groups"},
692 {1, 1024, "invalid initial priority group number"},
698 unsigned pg_count
= 0;
699 unsigned next_pg_num
;
704 m
= alloc_multipath(ti
);
706 ti
->error
= "can't allocate multipath";
710 r
= parse_features(&as
, m
);
714 r
= parse_hw_handler(&as
, m
);
718 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
722 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
726 /* parse the priority groups */
728 struct priority_group
*pg
;
730 pg
= parse_priority_group(&as
, m
);
736 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
737 list_add_tail(&pg
->list
, &m
->priority_groups
);
739 pg
->pg_num
= pg_count
;
744 if (pg_count
!= m
->nr_priority_groups
) {
745 ti
->error
= "priority group count mismatch";
757 static void multipath_dtr(struct dm_target
*ti
)
759 struct multipath
*m
= (struct multipath
*) ti
->private;
761 flush_workqueue(kmultipathd
);
766 * Map bios, recording original fields for later in case we have to resubmit
768 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
769 union map_info
*map_context
)
772 struct mpath_io
*mpio
;
773 struct multipath
*m
= (struct multipath
*) ti
->private;
775 if (bio_barrier(bio
))
778 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
779 dm_bio_record(&mpio
->details
, bio
);
781 map_context
->ptr
= mpio
;
782 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
783 r
= map_io(m
, bio
, mpio
, 0);
785 mempool_free(mpio
, m
->mpio_pool
);
791 * Take a path out of use.
793 static int fail_path(struct pgpath
*pgpath
)
796 struct multipath
*m
= pgpath
->pg
->m
;
798 spin_lock_irqsave(&m
->lock
, flags
);
800 if (!pgpath
->path
.is_active
)
803 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
805 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
806 pgpath
->path
.is_active
= 0;
807 pgpath
->fail_count
++;
811 if (pgpath
== m
->current_pgpath
)
812 m
->current_pgpath
= NULL
;
814 queue_work(kmultipathd
, &m
->trigger_event
);
817 spin_unlock_irqrestore(&m
->lock
, flags
);
823 * Reinstate a previously-failed path
825 static int reinstate_path(struct pgpath
*pgpath
)
829 struct multipath
*m
= pgpath
->pg
->m
;
831 spin_lock_irqsave(&m
->lock
, flags
);
833 if (pgpath
->path
.is_active
)
836 if (!pgpath
->pg
->ps
.type
) {
837 DMWARN("Reinstate path not supported by path selector %s",
838 pgpath
->pg
->ps
.type
->name
);
843 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
847 pgpath
->path
.is_active
= 1;
849 m
->current_pgpath
= NULL
;
850 if (!m
->nr_valid_paths
++ && m
->queue_size
)
851 queue_work(kmultipathd
, &m
->process_queued_ios
);
853 queue_work(kmultipathd
, &m
->trigger_event
);
856 spin_unlock_irqrestore(&m
->lock
, flags
);
862 * Fail or reinstate all paths that match the provided struct dm_dev.
864 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
868 struct pgpath
*pgpath
;
869 struct priority_group
*pg
;
871 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
872 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
873 if (pgpath
->path
.dev
== dev
)
882 * Temporarily try to avoid having to use the specified PG
884 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
889 spin_lock_irqsave(&m
->lock
, flags
);
891 pg
->bypassed
= bypassed
;
892 m
->current_pgpath
= NULL
;
893 m
->current_pg
= NULL
;
895 spin_unlock_irqrestore(&m
->lock
, flags
);
897 queue_work(kmultipathd
, &m
->trigger_event
);
901 * Switch to using the specified PG from the next I/O that gets mapped
903 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
905 struct priority_group
*pg
;
909 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
910 (pgnum
> m
->nr_priority_groups
)) {
911 DMWARN("invalid PG number supplied to switch_pg_num");
915 spin_lock_irqsave(&m
->lock
, flags
);
916 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
921 m
->current_pgpath
= NULL
;
922 m
->current_pg
= NULL
;
925 spin_unlock_irqrestore(&m
->lock
, flags
);
927 queue_work(kmultipathd
, &m
->trigger_event
);
932 * Set/clear bypassed status of a PG.
933 * PGs are numbered upwards from 1 in the order they were declared.
935 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
937 struct priority_group
*pg
;
940 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
941 (pgnum
> m
->nr_priority_groups
)) {
942 DMWARN("invalid PG number supplied to bypass_pg");
946 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
951 bypass_pg(m
, pg
, bypassed
);
956 * pg_init must call this when it has completed its initialisation
958 void dm_pg_init_complete(struct path
*path
, unsigned err_flags
)
960 struct pgpath
*pgpath
= path_to_pgpath(path
);
961 struct priority_group
*pg
= pgpath
->pg
;
962 struct multipath
*m
= pg
->m
;
965 /* We insist on failing the path if the PG is already bypassed. */
966 if (err_flags
&& pg
->bypassed
)
967 err_flags
|= MP_FAIL_PATH
;
969 if (err_flags
& MP_FAIL_PATH
)
972 if (err_flags
& MP_BYPASS_PG
)
975 spin_lock_irqsave(&m
->lock
, flags
);
977 m
->current_pgpath
= NULL
;
978 m
->current_pg
= NULL
;
979 } else if (!m
->pg_init_required
)
982 m
->pg_init_in_progress
= 0;
983 queue_work(kmultipathd
, &m
->process_queued_ios
);
984 spin_unlock_irqrestore(&m
->lock
, flags
);
990 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
991 int error
, struct mpath_io
*mpio
)
993 struct hw_handler
*hwh
= &m
->hw_handler
;
994 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
998 return 0; /* I/O complete */
1000 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1003 if (error
== -EOPNOTSUPP
)
1006 spin_lock_irqsave(&m
->lock
, flags
);
1007 if (!m
->nr_valid_paths
) {
1008 if (!m
->queue_if_no_path
) {
1009 spin_unlock_irqrestore(&m
->lock
, flags
);
1012 spin_unlock_irqrestore(&m
->lock
, flags
);
1016 spin_unlock_irqrestore(&m
->lock
, flags
);
1018 if (hwh
->type
&& hwh
->type
->error
)
1019 err_flags
= hwh
->type
->error(hwh
, bio
);
1022 if (err_flags
& MP_FAIL_PATH
)
1023 fail_path(mpio
->pgpath
);
1025 if (err_flags
& MP_BYPASS_PG
)
1026 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1029 if (err_flags
& MP_ERROR_IO
)
1033 dm_bio_restore(&mpio
->details
, bio
);
1035 /* queue for the daemon to resubmit or fail */
1036 spin_lock_irqsave(&m
->lock
, flags
);
1037 bio_list_add(&m
->queued_ios
, bio
);
1040 queue_work(kmultipathd
, &m
->process_queued_ios
);
1041 spin_unlock_irqrestore(&m
->lock
, flags
);
1043 return 1; /* io not complete */
1046 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1047 int error
, union map_info
*map_context
)
1049 struct multipath
*m
= (struct multipath
*) ti
->private;
1050 struct mpath_io
*mpio
= (struct mpath_io
*) map_context
->ptr
;
1051 struct pgpath
*pgpath
= mpio
->pgpath
;
1052 struct path_selector
*ps
;
1055 r
= do_end_io(m
, bio
, error
, mpio
);
1057 ps
= &pgpath
->pg
->ps
;
1058 if (ps
->type
->end_io
)
1059 ps
->type
->end_io(ps
, &pgpath
->path
);
1062 mempool_free(mpio
, m
->mpio_pool
);
1068 * Suspend can't complete until all the I/O is processed so if
1069 * the last path fails we must error any remaining I/O.
1070 * Note that if the freeze_bdev fails while suspending, the
1071 * queue_if_no_path state is lost - userspace should reset it.
1073 static void multipath_presuspend(struct dm_target
*ti
)
1075 struct multipath
*m
= (struct multipath
*) ti
->private;
1077 queue_if_no_path(m
, 0, 1);
1081 * Restore the queue_if_no_path setting.
1083 static void multipath_resume(struct dm_target
*ti
)
1085 struct multipath
*m
= (struct multipath
*) ti
->private;
1086 unsigned long flags
;
1088 spin_lock_irqsave(&m
->lock
, flags
);
1089 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1090 spin_unlock_irqrestore(&m
->lock
, flags
);
1094 * Info output has the following format:
1095 * num_multipath_feature_args [multipath_feature_args]*
1096 * num_handler_status_args [handler_status_args]*
1097 * num_groups init_group_number
1098 * [A|D|E num_ps_status_args [ps_status_args]*
1099 * num_paths num_selector_args
1100 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1102 * Table output has the following format (identical to the constructor string):
1103 * num_feature_args [features_args]*
1104 * num_handler_args hw_handler [hw_handler_args]*
1105 * num_groups init_group_number
1106 * [priority selector-name num_ps_args [ps_args]*
1107 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1109 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1110 char *result
, unsigned int maxlen
)
1113 unsigned long flags
;
1114 struct multipath
*m
= (struct multipath
*) ti
->private;
1115 struct hw_handler
*hwh
= &m
->hw_handler
;
1116 struct priority_group
*pg
;
1121 spin_lock_irqsave(&m
->lock
, flags
);
1124 if (type
== STATUSTYPE_INFO
)
1125 DMEMIT("1 %u ", m
->queue_size
);
1126 else if (m
->queue_if_no_path
)
1127 DMEMIT("1 queue_if_no_path ");
1131 if (hwh
->type
&& hwh
->type
->status
)
1132 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1133 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1136 DMEMIT("1 %s ", hwh
->type
->name
);
1138 DMEMIT("%u ", m
->nr_priority_groups
);
1141 pg_num
= m
->next_pg
->pg_num
;
1142 else if (m
->current_pg
)
1143 pg_num
= m
->current_pg
->pg_num
;
1147 DMEMIT("%u ", pg_num
);
1150 case STATUSTYPE_INFO
:
1151 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1153 state
= 'D'; /* Disabled */
1154 else if (pg
== m
->current_pg
)
1155 state
= 'A'; /* Currently Active */
1157 state
= 'E'; /* Enabled */
1159 DMEMIT("%c ", state
);
1161 if (pg
->ps
.type
->status
)
1162 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1168 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1169 pg
->ps
.type
->info_args
);
1171 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1172 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1173 p
->path
.is_active
? "A" : "F",
1175 if (pg
->ps
.type
->status
)
1176 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1177 &p
->path
, type
, result
+ sz
,
1183 case STATUSTYPE_TABLE
:
1184 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1185 DMEMIT("%s ", pg
->ps
.type
->name
);
1187 if (pg
->ps
.type
->status
)
1188 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1194 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1195 pg
->ps
.type
->table_args
);
1197 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1198 DMEMIT("%s ", p
->path
.dev
->name
);
1199 if (pg
->ps
.type
->status
)
1200 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1201 &p
->path
, type
, result
+ sz
,
1208 spin_unlock_irqrestore(&m
->lock
, flags
);
1213 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1217 struct multipath
*m
= (struct multipath
*) ti
->private;
1221 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1222 return queue_if_no_path(m
, 1, 0);
1223 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1224 return queue_if_no_path(m
, 0, 0);
1230 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1231 return bypass_pg_num(m
, argv
[1], 1);
1232 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1233 return bypass_pg_num(m
, argv
[1], 0);
1234 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1235 return switch_pg_num(m
, argv
[1]);
1236 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1237 action
= reinstate_path
;
1238 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1243 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1244 dm_table_get_mode(ti
->table
), &dev
);
1246 DMWARN("message: error getting device %s",
1251 r
= action_dev(m
, dev
, action
);
1253 dm_put_device(ti
, dev
);
1258 DMWARN("Unrecognised multipath message received.");
1262 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1263 struct file
*filp
, unsigned int cmd
,
1266 struct multipath
*m
= (struct multipath
*) ti
->private;
1267 struct block_device
*bdev
= NULL
;
1268 unsigned long flags
;
1269 struct file fake_file
= {};
1270 struct dentry fake_dentry
= {};
1273 fake_file
.f_dentry
= &fake_dentry
;
1275 spin_lock_irqsave(&m
->lock
, flags
);
1277 if (!m
->current_pgpath
)
1280 if (m
->current_pgpath
) {
1281 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1282 fake_dentry
.d_inode
= bdev
->bd_inode
;
1283 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1291 spin_unlock_irqrestore(&m
->lock
, flags
);
1293 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1294 bdev
->bd_disk
, cmd
, arg
);
1297 /*-----------------------------------------------------------------
1299 *---------------------------------------------------------------*/
1300 static struct target_type multipath_target
= {
1301 .name
= "multipath",
1302 .version
= {1, 0, 5},
1303 .module
= THIS_MODULE
,
1304 .ctr
= multipath_ctr
,
1305 .dtr
= multipath_dtr
,
1306 .map
= multipath_map
,
1307 .end_io
= multipath_end_io
,
1308 .presuspend
= multipath_presuspend
,
1309 .resume
= multipath_resume
,
1310 .status
= multipath_status
,
1311 .message
= multipath_message
,
1312 .ioctl
= multipath_ioctl
,
1315 static int __init
dm_multipath_init(void)
1319 /* allocate a slab for the dm_ios */
1320 _mpio_cache
= kmem_cache_create("dm_mpath", sizeof(struct mpath_io
),
1325 r
= dm_register_target(&multipath_target
);
1327 DMERR("%s: register failed %d", multipath_target
.name
, r
);
1328 kmem_cache_destroy(_mpio_cache
);
1332 kmultipathd
= create_workqueue("kmpathd");
1334 DMERR("%s: failed to create workqueue kmpathd",
1335 multipath_target
.name
);
1336 dm_unregister_target(&multipath_target
);
1337 kmem_cache_destroy(_mpio_cache
);
1341 DMINFO("version %u.%u.%u loaded",
1342 multipath_target
.version
[0], multipath_target
.version
[1],
1343 multipath_target
.version
[2]);
1348 static void __exit
dm_multipath_exit(void)
1352 destroy_workqueue(kmultipathd
);
1354 r
= dm_unregister_target(&multipath_target
);
1356 DMERR("%s: target unregister failed %d",
1357 multipath_target
.name
, r
);
1358 kmem_cache_destroy(_mpio_cache
);
1361 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1363 module_init(dm_multipath_init
);
1364 module_exit(dm_multipath_exit
);
1366 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1367 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1368 MODULE_LICENSE("GPL");