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
= kmalloc(sizeof(*pgpath
), GFP_KERNEL
);
120 memset(pgpath
, 0, sizeof(*pgpath
));
121 pgpath
->path
.is_active
= 1;
127 static inline void free_pgpath(struct pgpath
*pgpath
)
132 static struct priority_group
*alloc_priority_group(void)
134 struct priority_group
*pg
;
136 pg
= kmalloc(sizeof(*pg
), GFP_KERNEL
);
140 memset(pg
, 0, sizeof(*pg
));
141 INIT_LIST_HEAD(&pg
->pgpaths
);
146 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
148 struct pgpath
*pgpath
, *tmp
;
150 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
151 list_del(&pgpath
->list
);
152 dm_put_device(ti
, pgpath
->path
.dev
);
157 static void free_priority_group(struct priority_group
*pg
,
158 struct dm_target
*ti
)
160 struct path_selector
*ps
= &pg
->ps
;
163 ps
->type
->destroy(ps
);
164 dm_put_path_selector(ps
->type
);
167 free_pgpaths(&pg
->pgpaths
, ti
);
171 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
175 m
= kmalloc(sizeof(*m
), GFP_KERNEL
);
177 memset(m
, 0, sizeof(*m
));
178 INIT_LIST_HEAD(&m
->priority_groups
);
179 spin_lock_init(&m
->lock
);
181 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
, m
);
182 INIT_WORK(&m
->trigger_event
, trigger_event
, m
);
183 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
195 static void free_multipath(struct multipath
*m
)
197 struct priority_group
*pg
, *tmp
;
198 struct hw_handler
*hwh
= &m
->hw_handler
;
200 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
202 free_priority_group(pg
, m
->ti
);
206 hwh
->type
->destroy(hwh
);
207 dm_put_hw_handler(hwh
->type
);
210 mempool_destroy(m
->mpio_pool
);
215 /*-----------------------------------------------
217 *-----------------------------------------------*/
219 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
221 struct hw_handler
*hwh
= &m
->hw_handler
;
223 m
->current_pg
= pgpath
->pg
;
225 /* Must we initialise the PG first, and queue I/O till it's ready? */
226 if (hwh
->type
&& hwh
->type
->pg_init
) {
227 m
->pg_init_required
= 1;
230 m
->pg_init_required
= 0;
235 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
)
239 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
);
243 m
->current_pgpath
= path_to_pgpath(path
);
245 if (m
->current_pg
!= pg
)
246 __switch_pg(m
, m
->current_pgpath
);
251 static void __choose_pgpath(struct multipath
*m
)
253 struct priority_group
*pg
;
254 unsigned bypassed
= 1;
256 if (!m
->nr_valid_paths
)
259 /* Were we instructed to switch PG? */
263 if (!__choose_path_in_pg(m
, pg
))
267 /* Don't change PG until it has no remaining paths */
268 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
))
272 * Loop through priority groups until we find a valid path.
273 * First time we skip PGs marked 'bypassed'.
274 * Second time we only try the ones we skipped.
277 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
278 if (pg
->bypassed
== bypassed
)
280 if (!__choose_path_in_pg(m
, pg
))
283 } while (bypassed
--);
286 m
->current_pgpath
= NULL
;
287 m
->current_pg
= NULL
;
290 static int map_io(struct multipath
*m
, struct bio
*bio
, struct mpath_io
*mpio
,
295 struct pgpath
*pgpath
;
297 spin_lock_irqsave(&m
->lock
, flags
);
299 /* Do we need to select a new pgpath? */
300 if (!m
->current_pgpath
||
301 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
304 pgpath
= m
->current_pgpath
;
309 if ((pgpath
&& m
->queue_io
) ||
310 (!pgpath
&& m
->queue_if_no_path
)) {
311 /* Queue for the daemon to resubmit */
312 bio_list_add(&m
->queued_ios
, bio
);
314 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
316 queue_work(kmultipathd
, &m
->process_queued_ios
);
320 r
= -EIO
; /* Failed */
322 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
324 mpio
->pgpath
= pgpath
;
326 spin_unlock_irqrestore(&m
->lock
, flags
);
332 * If we run out of usable paths, should we queue I/O or error it?
334 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
335 unsigned save_old_value
)
339 spin_lock_irqsave(&m
->lock
, flags
);
342 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
344 m
->saved_queue_if_no_path
= queue_if_no_path
;
345 m
->queue_if_no_path
= queue_if_no_path
;
346 if (!m
->queue_if_no_path
&& m
->queue_size
)
347 queue_work(kmultipathd
, &m
->process_queued_ios
);
349 spin_unlock_irqrestore(&m
->lock
, flags
);
354 /*-----------------------------------------------------------------
355 * The multipath daemon is responsible for resubmitting queued ios.
356 *---------------------------------------------------------------*/
358 static void dispatch_queued_ios(struct multipath
*m
)
362 struct bio
*bio
= NULL
, *next
;
363 struct mpath_io
*mpio
;
364 union map_info
*info
;
366 spin_lock_irqsave(&m
->lock
, flags
);
367 bio
= bio_list_get(&m
->queued_ios
);
368 spin_unlock_irqrestore(&m
->lock
, flags
);
374 info
= dm_get_mapinfo(bio
);
377 r
= map_io(m
, bio
, mpio
, 1);
379 bio_endio(bio
, bio
->bi_size
, r
);
381 generic_make_request(bio
);
387 static void process_queued_ios(void *data
)
389 struct multipath
*m
= (struct multipath
*) data
;
390 struct hw_handler
*hwh
= &m
->hw_handler
;
391 struct pgpath
*pgpath
= NULL
;
392 unsigned init_required
= 0, must_queue
= 1;
395 spin_lock_irqsave(&m
->lock
, flags
);
400 if (!m
->current_pgpath
)
403 pgpath
= m
->current_pgpath
;
405 if ((pgpath
&& !m
->queue_io
) ||
406 (!pgpath
&& !m
->queue_if_no_path
))
409 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
410 m
->pg_init_required
= 0;
411 m
->pg_init_in_progress
= 1;
416 spin_unlock_irqrestore(&m
->lock
, flags
);
419 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
422 dispatch_queued_ios(m
);
426 * An event is triggered whenever a path is taken out of use.
427 * Includes path failure and PG bypass.
429 static void trigger_event(void *data
)
431 struct multipath
*m
= (struct multipath
*) data
;
433 dm_table_event(m
->ti
->table
);
436 /*-----------------------------------------------------------------
437 * Constructor/argument parsing:
438 * <#multipath feature args> [<arg>]*
439 * <#hw_handler args> [hw_handler [<arg>]*]
441 * <initial priority group>
442 * [<selector> <#selector args> [<arg>]*
443 * <#paths> <#per-path selector args>
444 * [<path> [<arg>]* ]+ ]+
445 *---------------------------------------------------------------*/
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, "invalid number of path selector args"},
502 pst
= dm_get_path_selector(shift(as
));
504 ti
->error
= "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
= "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
= "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
= "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
,
564 static struct param _params
[] = {
565 {1, 1024, "invalid number of paths"},
566 {0, 1024, "invalid number of selector args"}
570 unsigned i
, nr_selector_args
, nr_params
;
571 struct priority_group
*pg
;
572 struct dm_target
*ti
= m
->ti
;
576 ti
->error
= "not enough priority group aruments";
580 pg
= alloc_priority_group();
582 ti
->error
= "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
)
632 struct hw_handler_type
*hwht
;
634 struct dm_target
*ti
= m
->ti
;
636 static struct param _params
[] = {
637 {0, 1024, "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
= "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
= "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
)
670 struct dm_target
*ti
= m
->ti
;
672 static struct param _params
[] = {
673 {0, 1, "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, "invalid number of priority groups"},
697 {1, 1024, "invalid initial priority group number"},
703 unsigned pg_count
= 0;
704 unsigned next_pg_num
;
709 m
= alloc_multipath(ti
);
711 ti
->error
= "can't allocate multipath";
715 r
= parse_features(&as
, m
);
719 r
= parse_hw_handler(&as
, m
);
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
);
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
= "priority group count mismatch";
762 static void multipath_dtr(struct dm_target
*ti
)
764 struct multipath
*m
= (struct multipath
*) ti
->private;
766 flush_workqueue(kmultipathd
);
771 * Map bios, recording original fields for later in case we have to resubmit
773 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
774 union map_info
*map_context
)
777 struct mpath_io
*mpio
;
778 struct multipath
*m
= (struct multipath
*) ti
->private;
780 if (bio_barrier(bio
))
783 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
784 dm_bio_record(&mpio
->details
, bio
);
786 map_context
->ptr
= mpio
;
787 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
788 r
= map_io(m
, bio
, mpio
, 0);
790 mempool_free(mpio
, m
->mpio_pool
);
796 * Take a path out of use.
798 static int fail_path(struct pgpath
*pgpath
)
801 struct multipath
*m
= pgpath
->pg
->m
;
803 spin_lock_irqsave(&m
->lock
, flags
);
805 if (!pgpath
->path
.is_active
)
808 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
810 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
811 pgpath
->path
.is_active
= 0;
812 pgpath
->fail_count
++;
816 if (pgpath
== m
->current_pgpath
)
817 m
->current_pgpath
= NULL
;
819 queue_work(kmultipathd
, &m
->trigger_event
);
822 spin_unlock_irqrestore(&m
->lock
, flags
);
828 * Reinstate a previously-failed path
830 static int reinstate_path(struct pgpath
*pgpath
)
834 struct multipath
*m
= pgpath
->pg
->m
;
836 spin_lock_irqsave(&m
->lock
, flags
);
838 if (pgpath
->path
.is_active
)
841 if (!pgpath
->pg
->ps
.type
) {
842 DMWARN("Reinstate path not supported by path selector %s",
843 pgpath
->pg
->ps
.type
->name
);
848 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
852 pgpath
->path
.is_active
= 1;
854 m
->current_pgpath
= NULL
;
855 if (!m
->nr_valid_paths
++ && m
->queue_size
)
856 queue_work(kmultipathd
, &m
->process_queued_ios
);
858 queue_work(kmultipathd
, &m
->trigger_event
);
861 spin_unlock_irqrestore(&m
->lock
, flags
);
867 * Fail or reinstate all paths that match the provided struct dm_dev.
869 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
873 struct pgpath
*pgpath
;
874 struct priority_group
*pg
;
876 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
877 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
878 if (pgpath
->path
.dev
== dev
)
887 * Temporarily try to avoid having to use the specified PG
889 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
894 spin_lock_irqsave(&m
->lock
, flags
);
896 pg
->bypassed
= bypassed
;
897 m
->current_pgpath
= NULL
;
898 m
->current_pg
= NULL
;
900 spin_unlock_irqrestore(&m
->lock
, flags
);
902 queue_work(kmultipathd
, &m
->trigger_event
);
906 * Switch to using the specified PG from the next I/O that gets mapped
908 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
910 struct priority_group
*pg
;
914 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
915 (pgnum
> m
->nr_priority_groups
)) {
916 DMWARN("invalid PG number supplied to switch_pg_num");
920 spin_lock_irqsave(&m
->lock
, flags
);
921 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
926 m
->current_pgpath
= NULL
;
927 m
->current_pg
= NULL
;
930 spin_unlock_irqrestore(&m
->lock
, flags
);
932 queue_work(kmultipathd
, &m
->trigger_event
);
937 * Set/clear bypassed status of a PG.
938 * PGs are numbered upwards from 1 in the order they were declared.
940 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
942 struct priority_group
*pg
;
945 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
946 (pgnum
> m
->nr_priority_groups
)) {
947 DMWARN("invalid PG number supplied to bypass_pg");
951 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
956 bypass_pg(m
, pg
, bypassed
);
961 * pg_init must call this when it has completed its initialisation
963 void dm_pg_init_complete(struct path
*path
, unsigned err_flags
)
965 struct pgpath
*pgpath
= path_to_pgpath(path
);
966 struct priority_group
*pg
= pgpath
->pg
;
967 struct multipath
*m
= pg
->m
;
970 /* We insist on failing the path if the PG is already bypassed. */
971 if (err_flags
&& pg
->bypassed
)
972 err_flags
|= MP_FAIL_PATH
;
974 if (err_flags
& MP_FAIL_PATH
)
977 if (err_flags
& MP_BYPASS_PG
)
980 spin_lock_irqsave(&m
->lock
, flags
);
982 m
->current_pgpath
= NULL
;
983 m
->current_pg
= NULL
;
984 } else if (!m
->pg_init_required
)
987 m
->pg_init_in_progress
= 0;
988 queue_work(kmultipathd
, &m
->process_queued_ios
);
989 spin_unlock_irqrestore(&m
->lock
, flags
);
995 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
996 int error
, struct mpath_io
*mpio
)
998 struct hw_handler
*hwh
= &m
->hw_handler
;
999 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
1000 unsigned long flags
;
1003 return 0; /* I/O complete */
1005 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1008 if (error
== -EOPNOTSUPP
)
1011 spin_lock_irqsave(&m
->lock
, flags
);
1012 if (!m
->nr_valid_paths
) {
1013 if (!m
->queue_if_no_path
) {
1014 spin_unlock_irqrestore(&m
->lock
, flags
);
1017 spin_unlock_irqrestore(&m
->lock
, flags
);
1021 spin_unlock_irqrestore(&m
->lock
, flags
);
1023 if (hwh
->type
&& hwh
->type
->error
)
1024 err_flags
= hwh
->type
->error(hwh
, bio
);
1027 if (err_flags
& MP_FAIL_PATH
)
1028 fail_path(mpio
->pgpath
);
1030 if (err_flags
& MP_BYPASS_PG
)
1031 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1034 if (err_flags
& MP_ERROR_IO
)
1038 dm_bio_restore(&mpio
->details
, bio
);
1040 /* queue for the daemon to resubmit or fail */
1041 spin_lock_irqsave(&m
->lock
, flags
);
1042 bio_list_add(&m
->queued_ios
, bio
);
1045 queue_work(kmultipathd
, &m
->process_queued_ios
);
1046 spin_unlock_irqrestore(&m
->lock
, flags
);
1048 return 1; /* io not complete */
1051 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1052 int error
, union map_info
*map_context
)
1054 struct multipath
*m
= (struct multipath
*) ti
->private;
1055 struct mpath_io
*mpio
= (struct mpath_io
*) map_context
->ptr
;
1056 struct pgpath
*pgpath
= mpio
->pgpath
;
1057 struct path_selector
*ps
;
1060 r
= do_end_io(m
, bio
, error
, mpio
);
1062 ps
= &pgpath
->pg
->ps
;
1063 if (ps
->type
->end_io
)
1064 ps
->type
->end_io(ps
, &pgpath
->path
);
1067 mempool_free(mpio
, m
->mpio_pool
);
1073 * Suspend can't complete until all the I/O is processed so if
1074 * the last path fails we must error any remaining I/O.
1075 * Note that if the freeze_bdev fails while suspending, the
1076 * queue_if_no_path state is lost - userspace should reset it.
1078 static void multipath_presuspend(struct dm_target
*ti
)
1080 struct multipath
*m
= (struct multipath
*) ti
->private;
1082 queue_if_no_path(m
, 0, 1);
1086 * Restore the queue_if_no_path setting.
1088 static void multipath_resume(struct dm_target
*ti
)
1090 struct multipath
*m
= (struct multipath
*) ti
->private;
1091 unsigned long flags
;
1093 spin_lock_irqsave(&m
->lock
, flags
);
1094 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1095 spin_unlock_irqrestore(&m
->lock
, flags
);
1099 * Info output has the following format:
1100 * num_multipath_feature_args [multipath_feature_args]*
1101 * num_handler_status_args [handler_status_args]*
1102 * num_groups init_group_number
1103 * [A|D|E num_ps_status_args [ps_status_args]*
1104 * num_paths num_selector_args
1105 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1107 * Table output has the following format (identical to the constructor string):
1108 * num_feature_args [features_args]*
1109 * num_handler_args hw_handler [hw_handler_args]*
1110 * num_groups init_group_number
1111 * [priority selector-name num_ps_args [ps_args]*
1112 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1114 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1115 char *result
, unsigned int maxlen
)
1118 unsigned long flags
;
1119 struct multipath
*m
= (struct multipath
*) ti
->private;
1120 struct hw_handler
*hwh
= &m
->hw_handler
;
1121 struct priority_group
*pg
;
1126 spin_lock_irqsave(&m
->lock
, flags
);
1129 if (type
== STATUSTYPE_INFO
)
1130 DMEMIT("1 %u ", m
->queue_size
);
1131 else if (m
->queue_if_no_path
)
1132 DMEMIT("1 queue_if_no_path ");
1136 if (hwh
->type
&& hwh
->type
->status
)
1137 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1138 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1141 DMEMIT("1 %s ", hwh
->type
->name
);
1143 DMEMIT("%u ", m
->nr_priority_groups
);
1146 pg_num
= m
->next_pg
->pg_num
;
1147 else if (m
->current_pg
)
1148 pg_num
= m
->current_pg
->pg_num
;
1152 DMEMIT("%u ", pg_num
);
1155 case STATUSTYPE_INFO
:
1156 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1158 state
= 'D'; /* Disabled */
1159 else if (pg
== m
->current_pg
)
1160 state
= 'A'; /* Currently Active */
1162 state
= 'E'; /* Enabled */
1164 DMEMIT("%c ", state
);
1166 if (pg
->ps
.type
->status
)
1167 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1173 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1174 pg
->ps
.type
->info_args
);
1176 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1177 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1178 p
->path
.is_active
? "A" : "F",
1180 if (pg
->ps
.type
->status
)
1181 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1182 &p
->path
, type
, result
+ sz
,
1188 case STATUSTYPE_TABLE
:
1189 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1190 DMEMIT("%s ", pg
->ps
.type
->name
);
1192 if (pg
->ps
.type
->status
)
1193 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1199 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1200 pg
->ps
.type
->table_args
);
1202 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1203 DMEMIT("%s ", p
->path
.dev
->name
);
1204 if (pg
->ps
.type
->status
)
1205 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1206 &p
->path
, type
, result
+ sz
,
1213 spin_unlock_irqrestore(&m
->lock
, flags
);
1218 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1222 struct multipath
*m
= (struct multipath
*) ti
->private;
1226 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1227 return queue_if_no_path(m
, 1, 0);
1228 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1229 return queue_if_no_path(m
, 0, 0);
1235 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1236 return bypass_pg_num(m
, argv
[1], 1);
1237 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1238 return bypass_pg_num(m
, argv
[1], 0);
1239 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1240 return switch_pg_num(m
, argv
[1]);
1241 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1242 action
= reinstate_path
;
1243 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1248 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1249 dm_table_get_mode(ti
->table
), &dev
);
1251 DMWARN("message: error getting device %s",
1256 r
= action_dev(m
, dev
, action
);
1258 dm_put_device(ti
, dev
);
1263 DMWARN("Unrecognised multipath message received.");
1267 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1268 struct file
*filp
, unsigned int cmd
,
1271 struct multipath
*m
= (struct multipath
*) ti
->private;
1272 struct block_device
*bdev
= NULL
;
1273 unsigned long flags
;
1274 struct file fake_file
= {};
1275 struct dentry fake_dentry
= {};
1278 fake_file
.f_dentry
= &fake_dentry
;
1280 spin_lock_irqsave(&m
->lock
, flags
);
1282 if (!m
->current_pgpath
)
1285 if (m
->current_pgpath
) {
1286 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1287 fake_dentry
.d_inode
= bdev
->bd_inode
;
1288 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1296 spin_unlock_irqrestore(&m
->lock
, flags
);
1298 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1299 bdev
->bd_disk
, cmd
, arg
);
1302 /*-----------------------------------------------------------------
1304 *---------------------------------------------------------------*/
1305 static struct target_type multipath_target
= {
1306 .name
= "multipath",
1307 .version
= {1, 0, 5},
1308 .module
= THIS_MODULE
,
1309 .ctr
= multipath_ctr
,
1310 .dtr
= multipath_dtr
,
1311 .map
= multipath_map
,
1312 .end_io
= multipath_end_io
,
1313 .presuspend
= multipath_presuspend
,
1314 .resume
= multipath_resume
,
1315 .status
= multipath_status
,
1316 .message
= multipath_message
,
1317 .ioctl
= multipath_ioctl
,
1320 static int __init
dm_multipath_init(void)
1324 /* allocate a slab for the dm_ios */
1325 _mpio_cache
= kmem_cache_create("dm_mpath", sizeof(struct mpath_io
),
1330 r
= dm_register_target(&multipath_target
);
1332 DMERR("%s: register failed %d", multipath_target
.name
, r
);
1333 kmem_cache_destroy(_mpio_cache
);
1337 kmultipathd
= create_workqueue("kmpathd");
1339 DMERR("%s: failed to create workqueue kmpathd",
1340 multipath_target
.name
);
1341 dm_unregister_target(&multipath_target
);
1342 kmem_cache_destroy(_mpio_cache
);
1346 DMINFO("version %u.%u.%u loaded",
1347 multipath_target
.version
[0], multipath_target
.version
[1],
1348 multipath_target
.version
[2]);
1353 static void __exit
dm_multipath_exit(void)
1357 destroy_workqueue(kmultipathd
);
1359 r
= dm_unregister_target(&multipath_target
);
1361 DMERR("%s: target unregister failed %d",
1362 multipath_target
.name
, r
);
1363 kmem_cache_destroy(_mpio_cache
);
1366 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1368 module_init(dm_multipath_init
);
1369 module_exit(dm_multipath_exit
);
1371 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1372 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1373 MODULE_LICENSE("GPL");