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 struct kmem_cache
*_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
)
232 struct dm_path
*path
;
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
;
286 * Check whether bios must be queued in the device-mapper core rather
287 * than here in the target.
289 * m->lock must be held on entry.
291 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
292 * same value then we are not between multipath_presuspend()
293 * and multipath_resume() calls and we have no need to check
294 * for the DMF_NOFLUSH_SUSPENDING flag.
296 static int __must_push_back(struct multipath
*m
)
298 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
299 dm_noflush_suspending(m
->ti
));
302 static int map_io(struct multipath
*m
, struct bio
*bio
, struct mpath_io
*mpio
,
305 int r
= DM_MAPIO_REMAPPED
;
307 struct pgpath
*pgpath
;
309 spin_lock_irqsave(&m
->lock
, flags
);
311 /* Do we need to select a new pgpath? */
312 if (!m
->current_pgpath
||
313 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
316 pgpath
= m
->current_pgpath
;
321 if ((pgpath
&& m
->queue_io
) ||
322 (!pgpath
&& m
->queue_if_no_path
)) {
323 /* Queue for the daemon to resubmit */
324 bio_list_add(&m
->queued_ios
, bio
);
326 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
328 queue_work(kmultipathd
, &m
->process_queued_ios
);
330 r
= DM_MAPIO_SUBMITTED
;
332 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
333 else if (__must_push_back(m
))
334 r
= DM_MAPIO_REQUEUE
;
336 r
= -EIO
; /* Failed */
338 mpio
->pgpath
= pgpath
;
340 spin_unlock_irqrestore(&m
->lock
, flags
);
346 * If we run out of usable paths, should we queue I/O or error it?
348 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
349 unsigned save_old_value
)
353 spin_lock_irqsave(&m
->lock
, flags
);
356 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
358 m
->saved_queue_if_no_path
= queue_if_no_path
;
359 m
->queue_if_no_path
= queue_if_no_path
;
360 if (!m
->queue_if_no_path
&& m
->queue_size
)
361 queue_work(kmultipathd
, &m
->process_queued_ios
);
363 spin_unlock_irqrestore(&m
->lock
, flags
);
368 /*-----------------------------------------------------------------
369 * The multipath daemon is responsible for resubmitting queued ios.
370 *---------------------------------------------------------------*/
372 static void dispatch_queued_ios(struct multipath
*m
)
376 struct bio
*bio
= NULL
, *next
;
377 struct mpath_io
*mpio
;
378 union map_info
*info
;
380 spin_lock_irqsave(&m
->lock
, flags
);
381 bio
= bio_list_get(&m
->queued_ios
);
382 spin_unlock_irqrestore(&m
->lock
, flags
);
388 info
= dm_get_mapinfo(bio
);
391 r
= map_io(m
, bio
, mpio
, 1);
393 bio_endio(bio
, bio
->bi_size
, r
);
394 else if (r
== DM_MAPIO_REMAPPED
)
395 generic_make_request(bio
);
396 else if (r
== DM_MAPIO_REQUEUE
)
397 bio_endio(bio
, bio
->bi_size
, -EIO
);
403 static void process_queued_ios(struct work_struct
*work
)
405 struct multipath
*m
=
406 container_of(work
, struct multipath
, process_queued_ios
);
407 struct hw_handler
*hwh
= &m
->hw_handler
;
408 struct pgpath
*pgpath
= NULL
;
409 unsigned init_required
= 0, must_queue
= 1;
412 spin_lock_irqsave(&m
->lock
, flags
);
417 if (!m
->current_pgpath
)
420 pgpath
= m
->current_pgpath
;
422 if ((pgpath
&& !m
->queue_io
) ||
423 (!pgpath
&& !m
->queue_if_no_path
))
426 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
427 m
->pg_init_required
= 0;
428 m
->pg_init_in_progress
= 1;
433 spin_unlock_irqrestore(&m
->lock
, flags
);
436 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
439 dispatch_queued_ios(m
);
443 * An event is triggered whenever a path is taken out of use.
444 * Includes path failure and PG bypass.
446 static void trigger_event(struct work_struct
*work
)
448 struct multipath
*m
=
449 container_of(work
, struct multipath
, trigger_event
);
451 dm_table_event(m
->ti
->table
);
454 /*-----------------------------------------------------------------
455 * Constructor/argument parsing:
456 * <#multipath feature args> [<arg>]*
457 * <#hw_handler args> [hw_handler [<arg>]*]
459 * <initial priority group>
460 * [<selector> <#selector args> [<arg>]*
461 * <#paths> <#per-path selector args>
462 * [<path> [<arg>]* ]+ ]+
463 *---------------------------------------------------------------*/
470 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
473 (sscanf(str
, "%u", v
) != 1) ||
476 *error
= param
->error
;
488 static char *shift(struct arg_set
*as
)
502 static void consume(struct arg_set
*as
, unsigned n
)
504 BUG_ON (as
->argc
< n
);
509 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
510 struct dm_target
*ti
)
513 struct path_selector_type
*pst
;
516 static struct param _params
[] = {
517 {0, 1024, "invalid number of path selector args"},
520 pst
= dm_get_path_selector(shift(as
));
522 ti
->error
= "unknown path selector type";
526 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
530 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
532 dm_put_path_selector(pst
);
533 ti
->error
= "path selector constructor failed";
538 consume(as
, ps_argc
);
543 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
544 struct dm_target
*ti
)
549 /* we need at least a path arg */
551 ti
->error
= "no device given";
559 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
560 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
562 ti
->error
= "error getting device";
566 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
568 dm_put_device(ti
, p
->path
.dev
);
579 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
582 static struct param _params
[] = {
583 {1, 1024, "invalid number of paths"},
584 {0, 1024, "invalid number of selector args"}
588 unsigned i
, nr_selector_args
, nr_params
;
589 struct priority_group
*pg
;
590 struct dm_target
*ti
= m
->ti
;
594 ti
->error
= "not enough priority group aruments";
598 pg
= alloc_priority_group();
600 ti
->error
= "couldn't allocate priority group";
605 r
= parse_path_selector(as
, pg
, ti
);
612 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
616 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
620 nr_params
= 1 + nr_selector_args
;
621 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
622 struct pgpath
*pgpath
;
623 struct arg_set path_args
;
625 if (as
->argc
< nr_params
)
628 path_args
.argc
= nr_params
;
629 path_args
.argv
= as
->argv
;
631 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
636 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
637 consume(as
, nr_params
);
643 free_priority_group(pg
, ti
);
647 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
650 struct hw_handler_type
*hwht
;
652 struct dm_target
*ti
= m
->ti
;
654 static struct param _params
[] = {
655 {0, 1024, "invalid number of hardware handler args"},
658 r
= read_param(_params
, shift(as
), &hw_argc
, &ti
->error
);
665 hwht
= dm_get_hw_handler(shift(as
));
667 ti
->error
= "unknown hardware handler type";
671 m
->hw_handler
.md
= dm_table_get_md(ti
->table
);
672 dm_put(m
->hw_handler
.md
);
674 r
= hwht
->create(&m
->hw_handler
, hw_argc
- 1, as
->argv
);
676 dm_put_hw_handler(hwht
);
677 ti
->error
= "hardware handler constructor failed";
681 m
->hw_handler
.type
= hwht
;
682 consume(as
, hw_argc
- 1);
687 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
691 struct dm_target
*ti
= m
->ti
;
693 static struct param _params
[] = {
694 {0, 1, "invalid number of feature args"},
697 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
704 if (!strnicmp(shift(as
), MESG_STR("queue_if_no_path")))
705 return queue_if_no_path(m
, 1, 0);
707 ti
->error
= "Unrecognised multipath feature request";
712 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
715 /* target parameters */
716 static struct param _params
[] = {
717 {1, 1024, "invalid number of priority groups"},
718 {1, 1024, "invalid initial priority group number"},
724 unsigned pg_count
= 0;
725 unsigned next_pg_num
;
730 m
= alloc_multipath(ti
);
732 ti
->error
= "can't allocate multipath";
736 r
= parse_features(&as
, m
);
740 r
= parse_hw_handler(&as
, m
);
744 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
748 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
752 /* parse the priority groups */
754 struct priority_group
*pg
;
756 pg
= parse_priority_group(&as
, m
);
762 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
763 list_add_tail(&pg
->list
, &m
->priority_groups
);
765 pg
->pg_num
= pg_count
;
770 if (pg_count
!= m
->nr_priority_groups
) {
771 ti
->error
= "priority group count mismatch";
783 static void multipath_dtr(struct dm_target
*ti
)
785 struct multipath
*m
= (struct multipath
*) ti
->private;
787 flush_workqueue(kmultipathd
);
792 * Map bios, recording original fields for later in case we have to resubmit
794 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
795 union map_info
*map_context
)
798 struct mpath_io
*mpio
;
799 struct multipath
*m
= (struct multipath
*) ti
->private;
801 if (bio_barrier(bio
))
804 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
805 dm_bio_record(&mpio
->details
, bio
);
807 map_context
->ptr
= mpio
;
808 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
809 r
= map_io(m
, bio
, mpio
, 0);
810 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
811 mempool_free(mpio
, m
->mpio_pool
);
817 * Take a path out of use.
819 static int fail_path(struct pgpath
*pgpath
)
822 struct multipath
*m
= pgpath
->pg
->m
;
824 spin_lock_irqsave(&m
->lock
, flags
);
826 if (!pgpath
->path
.is_active
)
829 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
831 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
832 pgpath
->path
.is_active
= 0;
833 pgpath
->fail_count
++;
837 if (pgpath
== m
->current_pgpath
)
838 m
->current_pgpath
= NULL
;
840 queue_work(kmultipathd
, &m
->trigger_event
);
843 spin_unlock_irqrestore(&m
->lock
, flags
);
849 * Reinstate a previously-failed path
851 static int reinstate_path(struct pgpath
*pgpath
)
855 struct multipath
*m
= pgpath
->pg
->m
;
857 spin_lock_irqsave(&m
->lock
, flags
);
859 if (pgpath
->path
.is_active
)
862 if (!pgpath
->pg
->ps
.type
) {
863 DMWARN("Reinstate path not supported by path selector %s",
864 pgpath
->pg
->ps
.type
->name
);
869 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
873 pgpath
->path
.is_active
= 1;
875 m
->current_pgpath
= NULL
;
876 if (!m
->nr_valid_paths
++ && m
->queue_size
)
877 queue_work(kmultipathd
, &m
->process_queued_ios
);
879 queue_work(kmultipathd
, &m
->trigger_event
);
882 spin_unlock_irqrestore(&m
->lock
, flags
);
888 * Fail or reinstate all paths that match the provided struct dm_dev.
890 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
894 struct pgpath
*pgpath
;
895 struct priority_group
*pg
;
897 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
898 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
899 if (pgpath
->path
.dev
== dev
)
908 * Temporarily try to avoid having to use the specified PG
910 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
915 spin_lock_irqsave(&m
->lock
, flags
);
917 pg
->bypassed
= bypassed
;
918 m
->current_pgpath
= NULL
;
919 m
->current_pg
= NULL
;
921 spin_unlock_irqrestore(&m
->lock
, flags
);
923 queue_work(kmultipathd
, &m
->trigger_event
);
927 * Switch to using the specified PG from the next I/O that gets mapped
929 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
931 struct priority_group
*pg
;
935 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
936 (pgnum
> m
->nr_priority_groups
)) {
937 DMWARN("invalid PG number supplied to switch_pg_num");
941 spin_lock_irqsave(&m
->lock
, flags
);
942 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
947 m
->current_pgpath
= NULL
;
948 m
->current_pg
= NULL
;
951 spin_unlock_irqrestore(&m
->lock
, flags
);
953 queue_work(kmultipathd
, &m
->trigger_event
);
958 * Set/clear bypassed status of a PG.
959 * PGs are numbered upwards from 1 in the order they were declared.
961 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
963 struct priority_group
*pg
;
966 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
967 (pgnum
> m
->nr_priority_groups
)) {
968 DMWARN("invalid PG number supplied to bypass_pg");
972 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
977 bypass_pg(m
, pg
, bypassed
);
982 * pg_init must call this when it has completed its initialisation
984 void dm_pg_init_complete(struct dm_path
*path
, unsigned err_flags
)
986 struct pgpath
*pgpath
= path_to_pgpath(path
);
987 struct priority_group
*pg
= pgpath
->pg
;
988 struct multipath
*m
= pg
->m
;
991 /* We insist on failing the path if the PG is already bypassed. */
992 if (err_flags
&& pg
->bypassed
)
993 err_flags
|= MP_FAIL_PATH
;
995 if (err_flags
& MP_FAIL_PATH
)
998 if (err_flags
& MP_BYPASS_PG
)
1001 spin_lock_irqsave(&m
->lock
, flags
);
1003 m
->current_pgpath
= NULL
;
1004 m
->current_pg
= NULL
;
1005 } else if (!m
->pg_init_required
)
1008 m
->pg_init_in_progress
= 0;
1009 queue_work(kmultipathd
, &m
->process_queued_ios
);
1010 spin_unlock_irqrestore(&m
->lock
, flags
);
1016 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
1017 int error
, struct mpath_io
*mpio
)
1019 struct hw_handler
*hwh
= &m
->hw_handler
;
1020 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
1021 unsigned long flags
;
1024 return 0; /* I/O complete */
1026 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1029 if (error
== -EOPNOTSUPP
)
1032 spin_lock_irqsave(&m
->lock
, flags
);
1033 if (!m
->nr_valid_paths
) {
1034 if (__must_push_back(m
)) {
1035 spin_unlock_irqrestore(&m
->lock
, flags
);
1036 return DM_ENDIO_REQUEUE
;
1037 } else if (!m
->queue_if_no_path
) {
1038 spin_unlock_irqrestore(&m
->lock
, flags
);
1041 spin_unlock_irqrestore(&m
->lock
, flags
);
1045 spin_unlock_irqrestore(&m
->lock
, flags
);
1047 if (hwh
->type
&& hwh
->type
->error
)
1048 err_flags
= hwh
->type
->error(hwh
, bio
);
1051 if (err_flags
& MP_FAIL_PATH
)
1052 fail_path(mpio
->pgpath
);
1054 if (err_flags
& MP_BYPASS_PG
)
1055 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1058 if (err_flags
& MP_ERROR_IO
)
1062 dm_bio_restore(&mpio
->details
, bio
);
1064 /* queue for the daemon to resubmit or fail */
1065 spin_lock_irqsave(&m
->lock
, flags
);
1066 bio_list_add(&m
->queued_ios
, bio
);
1069 queue_work(kmultipathd
, &m
->process_queued_ios
);
1070 spin_unlock_irqrestore(&m
->lock
, flags
);
1072 return DM_ENDIO_INCOMPLETE
; /* io not complete */
1075 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1076 int error
, union map_info
*map_context
)
1078 struct multipath
*m
= (struct multipath
*) ti
->private;
1079 struct mpath_io
*mpio
= (struct mpath_io
*) map_context
->ptr
;
1080 struct pgpath
*pgpath
= mpio
->pgpath
;
1081 struct path_selector
*ps
;
1084 r
= do_end_io(m
, bio
, error
, mpio
);
1086 ps
= &pgpath
->pg
->ps
;
1087 if (ps
->type
->end_io
)
1088 ps
->type
->end_io(ps
, &pgpath
->path
);
1090 if (r
!= DM_ENDIO_INCOMPLETE
)
1091 mempool_free(mpio
, m
->mpio_pool
);
1097 * Suspend can't complete until all the I/O is processed so if
1098 * the last path fails we must error any remaining I/O.
1099 * Note that if the freeze_bdev fails while suspending, the
1100 * queue_if_no_path state is lost - userspace should reset it.
1102 static void multipath_presuspend(struct dm_target
*ti
)
1104 struct multipath
*m
= (struct multipath
*) ti
->private;
1106 queue_if_no_path(m
, 0, 1);
1110 * Restore the queue_if_no_path setting.
1112 static void multipath_resume(struct dm_target
*ti
)
1114 struct multipath
*m
= (struct multipath
*) ti
->private;
1115 unsigned long flags
;
1117 spin_lock_irqsave(&m
->lock
, flags
);
1118 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1119 spin_unlock_irqrestore(&m
->lock
, flags
);
1123 * Info output has the following format:
1124 * num_multipath_feature_args [multipath_feature_args]*
1125 * num_handler_status_args [handler_status_args]*
1126 * num_groups init_group_number
1127 * [A|D|E num_ps_status_args [ps_status_args]*
1128 * num_paths num_selector_args
1129 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1131 * Table output has the following format (identical to the constructor string):
1132 * num_feature_args [features_args]*
1133 * num_handler_args hw_handler [hw_handler_args]*
1134 * num_groups init_group_number
1135 * [priority selector-name num_ps_args [ps_args]*
1136 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1138 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1139 char *result
, unsigned int maxlen
)
1142 unsigned long flags
;
1143 struct multipath
*m
= (struct multipath
*) ti
->private;
1144 struct hw_handler
*hwh
= &m
->hw_handler
;
1145 struct priority_group
*pg
;
1150 spin_lock_irqsave(&m
->lock
, flags
);
1153 if (type
== STATUSTYPE_INFO
)
1154 DMEMIT("1 %u ", m
->queue_size
);
1155 else if (m
->queue_if_no_path
)
1156 DMEMIT("1 queue_if_no_path ");
1160 if (hwh
->type
&& hwh
->type
->status
)
1161 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1162 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1165 DMEMIT("1 %s ", hwh
->type
->name
);
1167 DMEMIT("%u ", m
->nr_priority_groups
);
1170 pg_num
= m
->next_pg
->pg_num
;
1171 else if (m
->current_pg
)
1172 pg_num
= m
->current_pg
->pg_num
;
1176 DMEMIT("%u ", pg_num
);
1179 case STATUSTYPE_INFO
:
1180 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1182 state
= 'D'; /* Disabled */
1183 else if (pg
== m
->current_pg
)
1184 state
= 'A'; /* Currently Active */
1186 state
= 'E'; /* Enabled */
1188 DMEMIT("%c ", state
);
1190 if (pg
->ps
.type
->status
)
1191 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1197 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1198 pg
->ps
.type
->info_args
);
1200 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1201 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1202 p
->path
.is_active
? "A" : "F",
1204 if (pg
->ps
.type
->status
)
1205 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1206 &p
->path
, type
, result
+ sz
,
1212 case STATUSTYPE_TABLE
:
1213 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1214 DMEMIT("%s ", pg
->ps
.type
->name
);
1216 if (pg
->ps
.type
->status
)
1217 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1223 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1224 pg
->ps
.type
->table_args
);
1226 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1227 DMEMIT("%s ", p
->path
.dev
->name
);
1228 if (pg
->ps
.type
->status
)
1229 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1230 &p
->path
, type
, result
+ sz
,
1237 spin_unlock_irqrestore(&m
->lock
, flags
);
1242 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1246 struct multipath
*m
= (struct multipath
*) ti
->private;
1250 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1251 return queue_if_no_path(m
, 1, 0);
1252 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1253 return queue_if_no_path(m
, 0, 0);
1259 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1260 return bypass_pg_num(m
, argv
[1], 1);
1261 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1262 return bypass_pg_num(m
, argv
[1], 0);
1263 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1264 return switch_pg_num(m
, argv
[1]);
1265 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1266 action
= reinstate_path
;
1267 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1272 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1273 dm_table_get_mode(ti
->table
), &dev
);
1275 DMWARN("message: error getting device %s",
1280 r
= action_dev(m
, dev
, action
);
1282 dm_put_device(ti
, dev
);
1287 DMWARN("Unrecognised multipath message received.");
1291 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1292 struct file
*filp
, unsigned int cmd
,
1295 struct multipath
*m
= (struct multipath
*) ti
->private;
1296 struct block_device
*bdev
= NULL
;
1297 unsigned long flags
;
1298 struct file fake_file
= {};
1299 struct dentry fake_dentry
= {};
1302 fake_file
.f_path
.dentry
= &fake_dentry
;
1304 spin_lock_irqsave(&m
->lock
, flags
);
1306 if (!m
->current_pgpath
)
1309 if (m
->current_pgpath
) {
1310 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1311 fake_dentry
.d_inode
= bdev
->bd_inode
;
1312 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1320 spin_unlock_irqrestore(&m
->lock
, flags
);
1322 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1323 bdev
->bd_disk
, cmd
, arg
);
1326 /*-----------------------------------------------------------------
1328 *---------------------------------------------------------------*/
1329 static struct target_type multipath_target
= {
1330 .name
= "multipath",
1331 .version
= {1, 0, 5},
1332 .module
= THIS_MODULE
,
1333 .ctr
= multipath_ctr
,
1334 .dtr
= multipath_dtr
,
1335 .map
= multipath_map
,
1336 .end_io
= multipath_end_io
,
1337 .presuspend
= multipath_presuspend
,
1338 .resume
= multipath_resume
,
1339 .status
= multipath_status
,
1340 .message
= multipath_message
,
1341 .ioctl
= multipath_ioctl
,
1344 static int __init
dm_multipath_init(void)
1348 /* allocate a slab for the dm_ios */
1349 _mpio_cache
= kmem_cache_create("dm_mpath", sizeof(struct mpath_io
),
1354 r
= dm_register_target(&multipath_target
);
1356 DMERR("%s: register failed %d", multipath_target
.name
, r
);
1357 kmem_cache_destroy(_mpio_cache
);
1361 kmultipathd
= create_workqueue("kmpathd");
1363 DMERR("%s: failed to create workqueue kmpathd",
1364 multipath_target
.name
);
1365 dm_unregister_target(&multipath_target
);
1366 kmem_cache_destroy(_mpio_cache
);
1370 DMINFO("version %u.%u.%u loaded",
1371 multipath_target
.version
[0], multipath_target
.version
[1],
1372 multipath_target
.version
[2]);
1377 static void __exit
dm_multipath_exit(void)
1381 destroy_workqueue(kmultipathd
);
1383 r
= dm_unregister_target(&multipath_target
);
1385 DMERR("%s: target unregister failed %d",
1386 multipath_target
.name
, r
);
1387 kmem_cache_destroy(_mpio_cache
);
1390 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1392 module_init(dm_multipath_init
);
1393 module_exit(dm_multipath_exit
);
1395 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1396 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1397 MODULE_LICENSE("GPL");