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 MESG_STR(x) x, sizeof(x)
28 struct list_head list
;
30 struct priority_group
*pg
; /* Owning PG */
31 unsigned fail_count
; /* Cumulative failure count */
36 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
39 * Paths are grouped into Priority Groups and numbered from 1 upwards.
40 * Each has a path selector which controls which path gets used.
42 struct priority_group
{
43 struct list_head list
;
45 struct multipath
*m
; /* Owning multipath instance */
46 struct path_selector ps
;
48 unsigned pg_num
; /* Reference number */
49 unsigned bypassed
; /* Temporarily bypass this PG? */
51 unsigned nr_pgpaths
; /* Number of paths in PG */
52 struct list_head pgpaths
;
55 /* Multipath context */
57 struct list_head list
;
62 struct hw_handler hw_handler
;
63 unsigned nr_priority_groups
;
64 struct list_head priority_groups
;
65 unsigned pg_init_required
; /* pg_init needs calling? */
66 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
68 unsigned nr_valid_paths
; /* Total number of usable paths */
69 struct pgpath
*current_pgpath
;
70 struct priority_group
*current_pg
;
71 struct priority_group
*next_pg
; /* Switch to this PG if set */
72 unsigned repeat_count
; /* I/Os left before calling PS again */
74 unsigned queue_io
; /* Must we queue all I/O? */
75 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
76 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
78 struct work_struct process_queued_ios
;
79 struct bio_list queued_ios
;
82 struct work_struct trigger_event
;
85 * We must use a mempool of mpath_io structs so that we
86 * can resubmit bios on error.
92 * Context information attached to each bio we process.
95 struct pgpath
*pgpath
;
96 struct dm_bio_details details
;
99 typedef int (*action_fn
) (struct pgpath
*pgpath
);
101 #define MIN_IOS 256 /* Mempool size */
103 static kmem_cache_t
*_mpio_cache
;
105 struct workqueue_struct
*kmultipathd
;
106 static void process_queued_ios(void *data
);
107 static void trigger_event(void *data
);
110 /*-----------------------------------------------
111 * Allocation routines
112 *-----------------------------------------------*/
114 static struct pgpath
*alloc_pgpath(void)
116 struct pgpath
*pgpath
= kmalloc(sizeof(*pgpath
), GFP_KERNEL
);
119 memset(pgpath
, 0, sizeof(*pgpath
));
120 pgpath
->path
.is_active
= 1;
126 static inline void free_pgpath(struct pgpath
*pgpath
)
131 static struct priority_group
*alloc_priority_group(void)
133 struct priority_group
*pg
;
135 pg
= kmalloc(sizeof(*pg
), GFP_KERNEL
);
139 memset(pg
, 0, sizeof(*pg
));
140 INIT_LIST_HEAD(&pg
->pgpaths
);
145 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
147 struct pgpath
*pgpath
, *tmp
;
149 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
150 list_del(&pgpath
->list
);
151 dm_put_device(ti
, pgpath
->path
.dev
);
156 static void free_priority_group(struct priority_group
*pg
,
157 struct dm_target
*ti
)
159 struct path_selector
*ps
= &pg
->ps
;
162 ps
->type
->destroy(ps
);
163 dm_put_path_selector(ps
->type
);
166 free_pgpaths(&pg
->pgpaths
, ti
);
170 static struct multipath
*alloc_multipath(void)
174 m
= kmalloc(sizeof(*m
), GFP_KERNEL
);
176 memset(m
, 0, sizeof(*m
));
177 INIT_LIST_HEAD(&m
->priority_groups
);
178 spin_lock_init(&m
->lock
);
180 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
, m
);
181 INIT_WORK(&m
->trigger_event
, trigger_event
, m
);
182 m
->mpio_pool
= mempool_create(MIN_IOS
, mempool_alloc_slab
,
183 mempool_free_slab
, _mpio_cache
);
193 static void free_multipath(struct multipath
*m
)
195 struct priority_group
*pg
, *tmp
;
196 struct hw_handler
*hwh
= &m
->hw_handler
;
198 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
200 free_priority_group(pg
, m
->ti
);
204 hwh
->type
->destroy(hwh
);
205 dm_put_hw_handler(hwh
->type
);
208 mempool_destroy(m
->mpio_pool
);
213 /*-----------------------------------------------
215 *-----------------------------------------------*/
217 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
219 struct hw_handler
*hwh
= &m
->hw_handler
;
221 m
->current_pg
= pgpath
->pg
;
223 /* Must we initialise the PG first, and queue I/O till it's ready? */
224 if (hwh
->type
&& hwh
->type
->pg_init
) {
225 m
->pg_init_required
= 1;
228 m
->pg_init_required
= 0;
233 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
)
237 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
);
241 m
->current_pgpath
= path_to_pgpath(path
);
243 if (m
->current_pg
!= pg
)
244 __switch_pg(m
, m
->current_pgpath
);
249 static void __choose_pgpath(struct multipath
*m
)
251 struct priority_group
*pg
;
252 unsigned bypassed
= 1;
254 if (!m
->nr_valid_paths
)
257 /* Were we instructed to switch PG? */
261 if (!__choose_path_in_pg(m
, pg
))
265 /* Don't change PG until it has no remaining paths */
266 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
))
270 * Loop through priority groups until we find a valid path.
271 * First time we skip PGs marked 'bypassed'.
272 * Second time we only try the ones we skipped.
275 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
276 if (pg
->bypassed
== bypassed
)
278 if (!__choose_path_in_pg(m
, pg
))
281 } while (bypassed
--);
284 m
->current_pgpath
= NULL
;
285 m
->current_pg
= NULL
;
288 static int map_io(struct multipath
*m
, struct bio
*bio
, struct mpath_io
*mpio
,
293 struct pgpath
*pgpath
;
295 spin_lock_irqsave(&m
->lock
, flags
);
297 /* Do we need to select a new pgpath? */
298 if (!m
->current_pgpath
||
299 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
302 pgpath
= m
->current_pgpath
;
307 if ((pgpath
&& m
->queue_io
) ||
308 (!pgpath
&& m
->queue_if_no_path
)) {
309 /* Queue for the daemon to resubmit */
310 bio_list_add(&m
->queued_ios
, bio
);
312 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
314 queue_work(kmultipathd
, &m
->process_queued_ios
);
318 r
= -EIO
; /* Failed */
320 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
322 mpio
->pgpath
= pgpath
;
324 spin_unlock_irqrestore(&m
->lock
, flags
);
330 * If we run out of usable paths, should we queue I/O or error it?
332 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
333 unsigned save_old_value
)
337 spin_lock_irqsave(&m
->lock
, flags
);
340 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
342 m
->saved_queue_if_no_path
= queue_if_no_path
;
343 m
->queue_if_no_path
= queue_if_no_path
;
344 if (!m
->queue_if_no_path
&& m
->queue_size
)
345 queue_work(kmultipathd
, &m
->process_queued_ios
);
347 spin_unlock_irqrestore(&m
->lock
, flags
);
352 /*-----------------------------------------------------------------
353 * The multipath daemon is responsible for resubmitting queued ios.
354 *---------------------------------------------------------------*/
356 static void dispatch_queued_ios(struct multipath
*m
)
360 struct bio
*bio
= NULL
, *next
;
361 struct mpath_io
*mpio
;
362 union map_info
*info
;
364 spin_lock_irqsave(&m
->lock
, flags
);
365 bio
= bio_list_get(&m
->queued_ios
);
366 spin_unlock_irqrestore(&m
->lock
, flags
);
372 info
= dm_get_mapinfo(bio
);
375 r
= map_io(m
, bio
, mpio
, 1);
377 bio_endio(bio
, bio
->bi_size
, r
);
379 generic_make_request(bio
);
385 static void process_queued_ios(void *data
)
387 struct multipath
*m
= (struct multipath
*) data
;
388 struct hw_handler
*hwh
= &m
->hw_handler
;
389 struct pgpath
*pgpath
= NULL
;
390 unsigned init_required
= 0, must_queue
= 1;
393 spin_lock_irqsave(&m
->lock
, flags
);
398 if (!m
->current_pgpath
)
401 pgpath
= m
->current_pgpath
;
403 if ((pgpath
&& !m
->queue_io
) ||
404 (!pgpath
&& !m
->queue_if_no_path
))
407 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
408 m
->pg_init_required
= 0;
409 m
->pg_init_in_progress
= 1;
414 spin_unlock_irqrestore(&m
->lock
, flags
);
417 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
420 dispatch_queued_ios(m
);
424 * An event is triggered whenever a path is taken out of use.
425 * Includes path failure and PG bypass.
427 static void trigger_event(void *data
)
429 struct multipath
*m
= (struct multipath
*) data
;
431 dm_table_event(m
->ti
->table
);
434 /*-----------------------------------------------------------------
435 * Constructor/argument parsing:
436 * <#multipath feature args> [<arg>]*
437 * <#hw_handler args> [hw_handler [<arg>]*]
439 * <initial priority group>
440 * [<selector> <#selector args> [<arg>]*
441 * <#paths> <#per-path selector args>
442 * [<path> [<arg>]* ]+ ]+
443 *---------------------------------------------------------------*/
450 #define ESTR(s) ("dm-multipath: " s)
452 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
455 (sscanf(str
, "%u", v
) != 1) ||
458 *error
= param
->error
;
470 static char *shift(struct arg_set
*as
)
484 static void consume(struct arg_set
*as
, unsigned n
)
486 BUG_ON (as
->argc
< n
);
491 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
492 struct dm_target
*ti
)
495 struct path_selector_type
*pst
;
498 static struct param _params
[] = {
499 {0, 1024, ESTR("invalid number of path selector args")},
502 pst
= dm_get_path_selector(shift(as
));
504 ti
->error
= ESTR("unknown path selector type");
508 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
512 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
514 dm_put_path_selector(pst
);
515 ti
->error
= ESTR("path selector constructor failed");
520 consume(as
, ps_argc
);
525 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
526 struct dm_target
*ti
)
531 /* we need at least a path arg */
533 ti
->error
= ESTR("no device given");
541 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
542 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
544 ti
->error
= ESTR("error getting device");
548 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
550 dm_put_device(ti
, p
->path
.dev
);
561 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
563 struct dm_target
*ti
)
565 static struct param _params
[] = {
566 {1, 1024, ESTR("invalid number of paths")},
567 {0, 1024, ESTR("invalid number of selector args")}
571 unsigned i
, nr_selector_args
, nr_params
;
572 struct priority_group
*pg
;
576 ti
->error
= ESTR("not enough priority group aruments");
580 pg
= alloc_priority_group();
582 ti
->error
= ESTR("couldn't allocate priority group");
587 r
= parse_path_selector(as
, pg
, ti
);
594 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
598 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
602 nr_params
= 1 + nr_selector_args
;
603 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
604 struct pgpath
*pgpath
;
605 struct arg_set path_args
;
607 if (as
->argc
< nr_params
)
610 path_args
.argc
= nr_params
;
611 path_args
.argv
= as
->argv
;
613 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
618 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
619 consume(as
, nr_params
);
625 free_priority_group(pg
, ti
);
629 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
,
630 struct dm_target
*ti
)
633 struct hw_handler_type
*hwht
;
636 static struct param _params
[] = {
637 {0, 1024, ESTR("invalid number of hardware handler args")},
640 r
= read_param(_params
, shift(as
), &hw_argc
, &ti
->error
);
647 hwht
= dm_get_hw_handler(shift(as
));
649 ti
->error
= ESTR("unknown hardware handler type");
653 r
= hwht
->create(&m
->hw_handler
, hw_argc
- 1, as
->argv
);
655 dm_put_hw_handler(hwht
);
656 ti
->error
= ESTR("hardware handler constructor failed");
660 m
->hw_handler
.type
= hwht
;
661 consume(as
, hw_argc
- 1);
666 static int parse_features(struct arg_set
*as
, struct multipath
*m
,
667 struct dm_target
*ti
)
672 static struct param _params
[] = {
673 {0, 1, ESTR("invalid number of feature args")},
676 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
683 if (!strnicmp(shift(as
), MESG_STR("queue_if_no_path")))
684 return queue_if_no_path(m
, 1, 0);
686 ti
->error
= "Unrecognised multipath feature request";
691 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
694 /* target parameters */
695 static struct param _params
[] = {
696 {1, 1024, ESTR("invalid number of priority groups")},
697 {1, 1024, ESTR("invalid initial priority group number")},
703 unsigned pg_count
= 0;
704 unsigned next_pg_num
;
709 m
= alloc_multipath();
711 ti
->error
= ESTR("can't allocate multipath");
715 r
= parse_features(&as
, m
, ti
);
719 r
= parse_hw_handler(&as
, m
, ti
);
723 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
727 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
731 /* parse the priority groups */
733 struct priority_group
*pg
;
735 pg
= parse_priority_group(&as
, m
, ti
);
741 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
742 list_add_tail(&pg
->list
, &m
->priority_groups
);
744 pg
->pg_num
= pg_count
;
749 if (pg_count
!= m
->nr_priority_groups
) {
750 ti
->error
= ESTR("priority group count mismatch");
765 static void multipath_dtr(struct dm_target
*ti
)
767 struct multipath
*m
= (struct multipath
*) ti
->private;
769 flush_workqueue(kmultipathd
);
774 * Map bios, recording original fields for later in case we have to resubmit
776 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
777 union map_info
*map_context
)
780 struct mpath_io
*mpio
;
781 struct multipath
*m
= (struct multipath
*) ti
->private;
783 if (bio_barrier(bio
))
786 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
787 dm_bio_record(&mpio
->details
, bio
);
789 map_context
->ptr
= mpio
;
790 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
791 r
= map_io(m
, bio
, mpio
, 0);
793 mempool_free(mpio
, m
->mpio_pool
);
799 * Take a path out of use.
801 static int fail_path(struct pgpath
*pgpath
)
804 struct multipath
*m
= pgpath
->pg
->m
;
806 spin_lock_irqsave(&m
->lock
, flags
);
808 if (!pgpath
->path
.is_active
)
811 DMWARN("dm-multipath: Failing path %s.", pgpath
->path
.dev
->name
);
813 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
814 pgpath
->path
.is_active
= 0;
815 pgpath
->fail_count
++;
819 if (pgpath
== m
->current_pgpath
)
820 m
->current_pgpath
= NULL
;
822 queue_work(kmultipathd
, &m
->trigger_event
);
825 spin_unlock_irqrestore(&m
->lock
, flags
);
831 * Reinstate a previously-failed path
833 static int reinstate_path(struct pgpath
*pgpath
)
837 struct multipath
*m
= pgpath
->pg
->m
;
839 spin_lock_irqsave(&m
->lock
, flags
);
841 if (pgpath
->path
.is_active
)
844 if (!pgpath
->pg
->ps
.type
) {
845 DMWARN("Reinstate path not supported by path selector %s",
846 pgpath
->pg
->ps
.type
->name
);
851 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
855 pgpath
->path
.is_active
= 1;
857 m
->current_pgpath
= NULL
;
858 if (!m
->nr_valid_paths
++ && m
->queue_size
)
859 queue_work(kmultipathd
, &m
->process_queued_ios
);
861 queue_work(kmultipathd
, &m
->trigger_event
);
864 spin_unlock_irqrestore(&m
->lock
, flags
);
870 * Fail or reinstate all paths that match the provided struct dm_dev.
872 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
876 struct pgpath
*pgpath
;
877 struct priority_group
*pg
;
879 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
880 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
881 if (pgpath
->path
.dev
== dev
)
890 * Temporarily try to avoid having to use the specified PG
892 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
897 spin_lock_irqsave(&m
->lock
, flags
);
899 pg
->bypassed
= bypassed
;
900 m
->current_pgpath
= NULL
;
901 m
->current_pg
= NULL
;
903 spin_unlock_irqrestore(&m
->lock
, flags
);
905 queue_work(kmultipathd
, &m
->trigger_event
);
909 * Switch to using the specified PG from the next I/O that gets mapped
911 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
913 struct priority_group
*pg
;
917 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
918 (pgnum
> m
->nr_priority_groups
)) {
919 DMWARN("invalid PG number supplied to switch_pg_num");
923 spin_lock_irqsave(&m
->lock
, flags
);
924 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
929 m
->current_pgpath
= NULL
;
930 m
->current_pg
= NULL
;
933 spin_unlock_irqrestore(&m
->lock
, flags
);
935 queue_work(kmultipathd
, &m
->trigger_event
);
940 * Set/clear bypassed status of a PG.
941 * PGs are numbered upwards from 1 in the order they were declared.
943 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
945 struct priority_group
*pg
;
948 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
949 (pgnum
> m
->nr_priority_groups
)) {
950 DMWARN("invalid PG number supplied to bypass_pg");
954 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
959 bypass_pg(m
, pg
, bypassed
);
964 * pg_init must call this when it has completed its initialisation
966 void dm_pg_init_complete(struct path
*path
, unsigned err_flags
)
968 struct pgpath
*pgpath
= path_to_pgpath(path
);
969 struct priority_group
*pg
= pgpath
->pg
;
970 struct multipath
*m
= pg
->m
;
973 /* We insist on failing the path if the PG is already bypassed. */
974 if (err_flags
&& pg
->bypassed
)
975 err_flags
|= MP_FAIL_PATH
;
977 if (err_flags
& MP_FAIL_PATH
)
980 if (err_flags
& MP_BYPASS_PG
)
983 spin_lock_irqsave(&m
->lock
, flags
);
985 m
->current_pgpath
= NULL
;
986 m
->current_pg
= NULL
;
987 } else if (!m
->pg_init_required
)
990 m
->pg_init_in_progress
= 0;
991 queue_work(kmultipathd
, &m
->process_queued_ios
);
992 spin_unlock_irqrestore(&m
->lock
, flags
);
998 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
999 int error
, struct mpath_io
*mpio
)
1001 struct hw_handler
*hwh
= &m
->hw_handler
;
1002 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
1003 unsigned long flags
;
1006 return 0; /* I/O complete */
1008 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1011 if (error
== -EOPNOTSUPP
)
1014 spin_lock_irqsave(&m
->lock
, flags
);
1015 if (!m
->nr_valid_paths
) {
1016 if (!m
->queue_if_no_path
) {
1017 spin_unlock_irqrestore(&m
->lock
, flags
);
1020 spin_unlock_irqrestore(&m
->lock
, flags
);
1024 spin_unlock_irqrestore(&m
->lock
, flags
);
1026 if (hwh
->type
&& hwh
->type
->error
)
1027 err_flags
= hwh
->type
->error(hwh
, bio
);
1030 if (err_flags
& MP_FAIL_PATH
)
1031 fail_path(mpio
->pgpath
);
1033 if (err_flags
& MP_BYPASS_PG
)
1034 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1037 if (err_flags
& MP_ERROR_IO
)
1041 dm_bio_restore(&mpio
->details
, bio
);
1043 /* queue for the daemon to resubmit or fail */
1044 spin_lock_irqsave(&m
->lock
, flags
);
1045 bio_list_add(&m
->queued_ios
, bio
);
1048 queue_work(kmultipathd
, &m
->process_queued_ios
);
1049 spin_unlock_irqrestore(&m
->lock
, flags
);
1051 return 1; /* io not complete */
1054 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1055 int error
, union map_info
*map_context
)
1057 struct multipath
*m
= (struct multipath
*) ti
->private;
1058 struct mpath_io
*mpio
= (struct mpath_io
*) map_context
->ptr
;
1059 struct pgpath
*pgpath
= mpio
->pgpath
;
1060 struct path_selector
*ps
;
1063 r
= do_end_io(m
, bio
, error
, mpio
);
1065 ps
= &pgpath
->pg
->ps
;
1066 if (ps
->type
->end_io
)
1067 ps
->type
->end_io(ps
, &pgpath
->path
);
1070 mempool_free(mpio
, m
->mpio_pool
);
1076 * Suspend can't complete until all the I/O is processed so if
1077 * the last path fails we must error any remaining I/O.
1078 * Note that if the freeze_bdev fails while suspending, the
1079 * queue_if_no_path state is lost - userspace should reset it.
1081 static void multipath_presuspend(struct dm_target
*ti
)
1083 struct multipath
*m
= (struct multipath
*) ti
->private;
1085 queue_if_no_path(m
, 0, 1);
1089 * Restore the queue_if_no_path setting.
1091 static void multipath_resume(struct dm_target
*ti
)
1093 struct multipath
*m
= (struct multipath
*) ti
->private;
1094 unsigned long flags
;
1096 spin_lock_irqsave(&m
->lock
, flags
);
1097 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1098 spin_unlock_irqrestore(&m
->lock
, flags
);
1102 * Info output has the following format:
1103 * num_multipath_feature_args [multipath_feature_args]*
1104 * num_handler_status_args [handler_status_args]*
1105 * num_groups init_group_number
1106 * [A|D|E num_ps_status_args [ps_status_args]*
1107 * num_paths num_selector_args
1108 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1110 * Table output has the following format (identical to the constructor string):
1111 * num_feature_args [features_args]*
1112 * num_handler_args hw_handler [hw_handler_args]*
1113 * num_groups init_group_number
1114 * [priority selector-name num_ps_args [ps_args]*
1115 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1117 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1118 char *result
, unsigned int maxlen
)
1121 unsigned long flags
;
1122 struct multipath
*m
= (struct multipath
*) ti
->private;
1123 struct hw_handler
*hwh
= &m
->hw_handler
;
1124 struct priority_group
*pg
;
1129 spin_lock_irqsave(&m
->lock
, flags
);
1132 if (type
== STATUSTYPE_INFO
)
1133 DMEMIT("1 %u ", m
->queue_size
);
1134 else if (m
->queue_if_no_path
)
1135 DMEMIT("1 queue_if_no_path ");
1139 if (hwh
->type
&& hwh
->type
->status
)
1140 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1141 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1144 DMEMIT("1 %s ", hwh
->type
->name
);
1146 DMEMIT("%u ", m
->nr_priority_groups
);
1149 pg_num
= m
->next_pg
->pg_num
;
1150 else if (m
->current_pg
)
1151 pg_num
= m
->current_pg
->pg_num
;
1155 DMEMIT("%u ", pg_num
);
1158 case STATUSTYPE_INFO
:
1159 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1161 state
= 'D'; /* Disabled */
1162 else if (pg
== m
->current_pg
)
1163 state
= 'A'; /* Currently Active */
1165 state
= 'E'; /* Enabled */
1167 DMEMIT("%c ", state
);
1169 if (pg
->ps
.type
->status
)
1170 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1176 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1177 pg
->ps
.type
->info_args
);
1179 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1180 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1181 p
->path
.is_active
? "A" : "F",
1183 if (pg
->ps
.type
->status
)
1184 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1185 &p
->path
, type
, result
+ sz
,
1191 case STATUSTYPE_TABLE
:
1192 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1193 DMEMIT("%s ", pg
->ps
.type
->name
);
1195 if (pg
->ps
.type
->status
)
1196 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1202 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1203 pg
->ps
.type
->table_args
);
1205 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1206 DMEMIT("%s ", p
->path
.dev
->name
);
1207 if (pg
->ps
.type
->status
)
1208 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1209 &p
->path
, type
, result
+ sz
,
1216 spin_unlock_irqrestore(&m
->lock
, flags
);
1221 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1225 struct multipath
*m
= (struct multipath
*) ti
->private;
1229 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1230 return queue_if_no_path(m
, 1, 0);
1231 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1232 return queue_if_no_path(m
, 0, 0);
1238 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1239 return bypass_pg_num(m
, argv
[1], 1);
1240 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1241 return bypass_pg_num(m
, argv
[1], 0);
1242 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1243 return switch_pg_num(m
, argv
[1]);
1244 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1245 action
= reinstate_path
;
1246 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1251 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1252 dm_table_get_mode(ti
->table
), &dev
);
1254 DMWARN("dm-multipath message: error getting device %s",
1259 r
= action_dev(m
, dev
, action
);
1261 dm_put_device(ti
, dev
);
1266 DMWARN("Unrecognised multipath message received.");
1270 /*-----------------------------------------------------------------
1272 *---------------------------------------------------------------*/
1273 static struct target_type multipath_target
= {
1274 .name
= "multipath",
1275 .version
= {1, 0, 4},
1276 .module
= THIS_MODULE
,
1277 .ctr
= multipath_ctr
,
1278 .dtr
= multipath_dtr
,
1279 .map
= multipath_map
,
1280 .end_io
= multipath_end_io
,
1281 .presuspend
= multipath_presuspend
,
1282 .resume
= multipath_resume
,
1283 .status
= multipath_status
,
1284 .message
= multipath_message
,
1287 static int __init
dm_multipath_init(void)
1291 /* allocate a slab for the dm_ios */
1292 _mpio_cache
= kmem_cache_create("dm_mpath", sizeof(struct mpath_io
),
1297 r
= dm_register_target(&multipath_target
);
1299 DMERR("%s: register failed %d", multipath_target
.name
, r
);
1300 kmem_cache_destroy(_mpio_cache
);
1304 kmultipathd
= create_workqueue("kmpathd");
1306 DMERR("%s: failed to create workqueue kmpathd",
1307 multipath_target
.name
);
1308 dm_unregister_target(&multipath_target
);
1309 kmem_cache_destroy(_mpio_cache
);
1313 DMINFO("dm-multipath version %u.%u.%u loaded",
1314 multipath_target
.version
[0], multipath_target
.version
[1],
1315 multipath_target
.version
[2]);
1320 static void __exit
dm_multipath_exit(void)
1324 destroy_workqueue(kmultipathd
);
1326 r
= dm_unregister_target(&multipath_target
);
1328 DMERR("%s: target unregister failed %d",
1329 multipath_target
.name
, r
);
1330 kmem_cache_destroy(_mpio_cache
);
1333 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1335 module_init(dm_multipath_init
);
1336 module_exit(dm_multipath_exit
);
1338 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1339 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1340 MODULE_LICENSE("GPL");