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"
13 #include "dm-uevent.h"
15 #include <linux/ctype.h>
16 #include <linux/init.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/slab.h>
21 #include <linux/time.h>
22 #include <linux/workqueue.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define MESG_STR(x) x, sizeof(x)
30 struct list_head list
;
32 struct priority_group
*pg
; /* Owning PG */
33 unsigned fail_count
; /* Cumulative failure count */
38 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
44 struct priority_group
{
45 struct list_head list
;
47 struct multipath
*m
; /* Owning multipath instance */
48 struct path_selector ps
;
50 unsigned pg_num
; /* Reference number */
51 unsigned bypassed
; /* Temporarily bypass this PG? */
53 unsigned nr_pgpaths
; /* Number of paths in PG */
54 struct list_head pgpaths
;
57 /* Multipath context */
59 struct list_head list
;
64 struct hw_handler hw_handler
;
65 unsigned nr_priority_groups
;
66 struct list_head priority_groups
;
67 unsigned pg_init_required
; /* pg_init needs calling? */
68 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
70 unsigned nr_valid_paths
; /* Total number of usable paths */
71 struct pgpath
*current_pgpath
;
72 struct priority_group
*current_pg
;
73 struct priority_group
*next_pg
; /* Switch to this PG if set */
74 unsigned repeat_count
; /* I/Os left before calling PS again */
76 unsigned queue_io
; /* Must we queue all I/O? */
77 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
78 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
79 unsigned pg_init_retries
; /* Number of times to retry pg_init */
80 unsigned pg_init_count
; /* Number of times pg_init called */
82 struct work_struct process_queued_ios
;
83 struct bio_list queued_ios
;
86 struct work_struct trigger_event
;
89 * We must use a mempool of dm_mpath_io structs so that we
90 * can resubmit bios on error.
96 * Context information attached to each bio we process.
99 struct pgpath
*pgpath
;
100 struct dm_bio_details details
;
103 typedef int (*action_fn
) (struct pgpath
*pgpath
);
105 #define MIN_IOS 256 /* Mempool size */
107 static struct kmem_cache
*_mpio_cache
;
109 static struct workqueue_struct
*kmultipathd
;
110 static void process_queued_ios(struct work_struct
*work
);
111 static void trigger_event(struct work_struct
*work
);
114 /*-----------------------------------------------
115 * Allocation routines
116 *-----------------------------------------------*/
118 static struct pgpath
*alloc_pgpath(void)
120 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
123 pgpath
->path
.is_active
= 1;
128 static void free_pgpath(struct pgpath
*pgpath
)
133 static struct priority_group
*alloc_priority_group(void)
135 struct priority_group
*pg
;
137 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
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(struct dm_target
*ti
)
174 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
176 INIT_LIST_HEAD(&m
->priority_groups
);
177 spin_lock_init(&m
->lock
);
179 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
180 INIT_WORK(&m
->trigger_event
, trigger_event
);
181 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _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;
232 m
->pg_init_count
= 0;
235 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
)
237 struct dm_path
*path
;
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
;
291 * Check whether bios must be queued in the device-mapper core rather
292 * than here in the target.
294 * m->lock must be held on entry.
296 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
297 * same value then we are not between multipath_presuspend()
298 * and multipath_resume() calls and we have no need to check
299 * for the DMF_NOFLUSH_SUSPENDING flag.
301 static int __must_push_back(struct multipath
*m
)
303 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
304 dm_noflush_suspending(m
->ti
));
307 static int map_io(struct multipath
*m
, struct bio
*bio
,
308 struct dm_mpath_io
*mpio
, unsigned was_queued
)
310 int r
= DM_MAPIO_REMAPPED
;
312 struct pgpath
*pgpath
;
314 spin_lock_irqsave(&m
->lock
, flags
);
316 /* Do we need to select a new pgpath? */
317 if (!m
->current_pgpath
||
318 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
321 pgpath
= m
->current_pgpath
;
326 if ((pgpath
&& m
->queue_io
) ||
327 (!pgpath
&& m
->queue_if_no_path
)) {
328 /* Queue for the daemon to resubmit */
329 bio_list_add(&m
->queued_ios
, bio
);
331 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
333 queue_work(kmultipathd
, &m
->process_queued_ios
);
335 r
= DM_MAPIO_SUBMITTED
;
337 bio
->bi_bdev
= pgpath
->path
.dev
->bdev
;
338 else if (__must_push_back(m
))
339 r
= DM_MAPIO_REQUEUE
;
341 r
= -EIO
; /* Failed */
343 mpio
->pgpath
= pgpath
;
345 spin_unlock_irqrestore(&m
->lock
, flags
);
351 * If we run out of usable paths, should we queue I/O or error it?
353 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
354 unsigned save_old_value
)
358 spin_lock_irqsave(&m
->lock
, flags
);
361 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
363 m
->saved_queue_if_no_path
= queue_if_no_path
;
364 m
->queue_if_no_path
= queue_if_no_path
;
365 if (!m
->queue_if_no_path
&& m
->queue_size
)
366 queue_work(kmultipathd
, &m
->process_queued_ios
);
368 spin_unlock_irqrestore(&m
->lock
, flags
);
373 /*-----------------------------------------------------------------
374 * The multipath daemon is responsible for resubmitting queued ios.
375 *---------------------------------------------------------------*/
377 static void dispatch_queued_ios(struct multipath
*m
)
381 struct bio
*bio
= NULL
, *next
;
382 struct dm_mpath_io
*mpio
;
383 union map_info
*info
;
385 spin_lock_irqsave(&m
->lock
, flags
);
386 bio
= bio_list_get(&m
->queued_ios
);
387 spin_unlock_irqrestore(&m
->lock
, flags
);
393 info
= dm_get_mapinfo(bio
);
396 r
= map_io(m
, bio
, mpio
, 1);
399 else if (r
== DM_MAPIO_REMAPPED
)
400 generic_make_request(bio
);
401 else if (r
== DM_MAPIO_REQUEUE
)
402 bio_endio(bio
, -EIO
);
408 static void process_queued_ios(struct work_struct
*work
)
410 struct multipath
*m
=
411 container_of(work
, struct multipath
, process_queued_ios
);
412 struct hw_handler
*hwh
= &m
->hw_handler
;
413 struct pgpath
*pgpath
= NULL
;
414 unsigned init_required
= 0, must_queue
= 1;
417 spin_lock_irqsave(&m
->lock
, flags
);
422 if (!m
->current_pgpath
)
425 pgpath
= m
->current_pgpath
;
427 if ((pgpath
&& !m
->queue_io
) ||
428 (!pgpath
&& !m
->queue_if_no_path
))
431 if (m
->pg_init_required
&& !m
->pg_init_in_progress
) {
433 m
->pg_init_required
= 0;
434 m
->pg_init_in_progress
= 1;
439 spin_unlock_irqrestore(&m
->lock
, flags
);
442 hwh
->type
->pg_init(hwh
, pgpath
->pg
->bypassed
, &pgpath
->path
);
445 dispatch_queued_ios(m
);
449 * An event is triggered whenever a path is taken out of use.
450 * Includes path failure and PG bypass.
452 static void trigger_event(struct work_struct
*work
)
454 struct multipath
*m
=
455 container_of(work
, struct multipath
, trigger_event
);
457 dm_table_event(m
->ti
->table
);
460 /*-----------------------------------------------------------------
461 * Constructor/argument parsing:
462 * <#multipath feature args> [<arg>]*
463 * <#hw_handler args> [hw_handler [<arg>]*]
465 * <initial priority group>
466 * [<selector> <#selector args> [<arg>]*
467 * <#paths> <#per-path selector args>
468 * [<path> [<arg>]* ]+ ]+
469 *---------------------------------------------------------------*/
476 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
479 (sscanf(str
, "%u", v
) != 1) ||
482 *error
= param
->error
;
494 static char *shift(struct arg_set
*as
)
508 static void consume(struct arg_set
*as
, unsigned n
)
510 BUG_ON (as
->argc
< n
);
515 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
516 struct dm_target
*ti
)
519 struct path_selector_type
*pst
;
522 static struct param _params
[] = {
523 {0, 1024, "invalid number of path selector args"},
526 pst
= dm_get_path_selector(shift(as
));
528 ti
->error
= "unknown path selector type";
532 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
536 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
538 dm_put_path_selector(pst
);
539 ti
->error
= "path selector constructor failed";
544 consume(as
, ps_argc
);
549 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
550 struct dm_target
*ti
)
555 /* we need at least a path arg */
557 ti
->error
= "no device given";
565 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
566 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
568 ti
->error
= "error getting device";
572 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
574 dm_put_device(ti
, p
->path
.dev
);
585 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
588 static struct param _params
[] = {
589 {1, 1024, "invalid number of paths"},
590 {0, 1024, "invalid number of selector args"}
594 unsigned i
, nr_selector_args
, nr_params
;
595 struct priority_group
*pg
;
596 struct dm_target
*ti
= m
->ti
;
600 ti
->error
= "not enough priority group aruments";
604 pg
= alloc_priority_group();
606 ti
->error
= "couldn't allocate priority group";
611 r
= parse_path_selector(as
, pg
, ti
);
618 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
622 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
626 nr_params
= 1 + nr_selector_args
;
627 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
628 struct pgpath
*pgpath
;
629 struct arg_set path_args
;
631 if (as
->argc
< nr_params
)
634 path_args
.argc
= nr_params
;
635 path_args
.argv
= as
->argv
;
637 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
642 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
643 consume(as
, nr_params
);
649 free_priority_group(pg
, ti
);
653 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
656 struct hw_handler_type
*hwht
;
658 struct dm_target
*ti
= m
->ti
;
660 static struct param _params
[] = {
661 {0, 1024, "invalid number of hardware handler args"},
664 r
= read_param(_params
, shift(as
), &hw_argc
, &ti
->error
);
671 hwht
= dm_get_hw_handler(shift(as
));
673 ti
->error
= "unknown hardware handler type";
677 m
->hw_handler
.md
= dm_table_get_md(ti
->table
);
678 dm_put(m
->hw_handler
.md
);
680 r
= hwht
->create(&m
->hw_handler
, hw_argc
- 1, as
->argv
);
682 dm_put_hw_handler(hwht
);
683 ti
->error
= "hardware handler constructor failed";
687 m
->hw_handler
.type
= hwht
;
688 consume(as
, hw_argc
- 1);
693 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
697 struct dm_target
*ti
= m
->ti
;
698 const char *param_name
;
700 static struct param _params
[] = {
701 {0, 3, "invalid number of feature args"},
702 {1, 50, "pg_init_retries must be between 1 and 50"},
705 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
713 param_name
= shift(as
);
716 if (!strnicmp(param_name
, MESG_STR("queue_if_no_path"))) {
717 r
= queue_if_no_path(m
, 1, 0);
721 if (!strnicmp(param_name
, MESG_STR("pg_init_retries")) &&
723 r
= read_param(_params
+ 1, shift(as
),
724 &m
->pg_init_retries
, &ti
->error
);
729 ti
->error
= "Unrecognised multipath feature request";
731 } while (argc
&& !r
);
736 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
739 /* target parameters */
740 static struct param _params
[] = {
741 {1, 1024, "invalid number of priority groups"},
742 {1, 1024, "invalid initial priority group number"},
748 unsigned pg_count
= 0;
749 unsigned next_pg_num
;
754 m
= alloc_multipath(ti
);
756 ti
->error
= "can't allocate multipath";
760 r
= parse_features(&as
, m
);
764 r
= parse_hw_handler(&as
, m
);
768 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
772 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
776 /* parse the priority groups */
778 struct priority_group
*pg
;
780 pg
= parse_priority_group(&as
, m
);
786 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
787 list_add_tail(&pg
->list
, &m
->priority_groups
);
789 pg
->pg_num
= pg_count
;
794 if (pg_count
!= m
->nr_priority_groups
) {
795 ti
->error
= "priority group count mismatch";
807 static void multipath_dtr(struct dm_target
*ti
)
809 struct multipath
*m
= (struct multipath
*) ti
->private;
811 flush_workqueue(kmultipathd
);
816 * Map bios, recording original fields for later in case we have to resubmit
818 static int multipath_map(struct dm_target
*ti
, struct bio
*bio
,
819 union map_info
*map_context
)
822 struct dm_mpath_io
*mpio
;
823 struct multipath
*m
= (struct multipath
*) ti
->private;
825 mpio
= mempool_alloc(m
->mpio_pool
, GFP_NOIO
);
826 dm_bio_record(&mpio
->details
, bio
);
828 map_context
->ptr
= mpio
;
829 bio
->bi_rw
|= (1 << BIO_RW_FAILFAST
);
830 r
= map_io(m
, bio
, mpio
, 0);
831 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
832 mempool_free(mpio
, m
->mpio_pool
);
838 * Take a path out of use.
840 static int fail_path(struct pgpath
*pgpath
)
843 struct multipath
*m
= pgpath
->pg
->m
;
845 spin_lock_irqsave(&m
->lock
, flags
);
847 if (!pgpath
->path
.is_active
)
850 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
852 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
853 pgpath
->path
.is_active
= 0;
854 pgpath
->fail_count
++;
858 if (pgpath
== m
->current_pgpath
)
859 m
->current_pgpath
= NULL
;
861 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
862 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
864 queue_work(kmultipathd
, &m
->trigger_event
);
867 spin_unlock_irqrestore(&m
->lock
, flags
);
873 * Reinstate a previously-failed path
875 static int reinstate_path(struct pgpath
*pgpath
)
879 struct multipath
*m
= pgpath
->pg
->m
;
881 spin_lock_irqsave(&m
->lock
, flags
);
883 if (pgpath
->path
.is_active
)
886 if (!pgpath
->pg
->ps
.type
) {
887 DMWARN("Reinstate path not supported by path selector %s",
888 pgpath
->pg
->ps
.type
->name
);
893 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
897 pgpath
->path
.is_active
= 1;
899 m
->current_pgpath
= NULL
;
900 if (!m
->nr_valid_paths
++ && m
->queue_size
)
901 queue_work(kmultipathd
, &m
->process_queued_ios
);
903 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
904 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
906 queue_work(kmultipathd
, &m
->trigger_event
);
909 spin_unlock_irqrestore(&m
->lock
, flags
);
915 * Fail or reinstate all paths that match the provided struct dm_dev.
917 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
921 struct pgpath
*pgpath
;
922 struct priority_group
*pg
;
924 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
925 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
926 if (pgpath
->path
.dev
== dev
)
935 * Temporarily try to avoid having to use the specified PG
937 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
942 spin_lock_irqsave(&m
->lock
, flags
);
944 pg
->bypassed
= bypassed
;
945 m
->current_pgpath
= NULL
;
946 m
->current_pg
= NULL
;
948 spin_unlock_irqrestore(&m
->lock
, flags
);
950 queue_work(kmultipathd
, &m
->trigger_event
);
954 * Switch to using the specified PG from the next I/O that gets mapped
956 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
958 struct priority_group
*pg
;
962 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
963 (pgnum
> m
->nr_priority_groups
)) {
964 DMWARN("invalid PG number supplied to switch_pg_num");
968 spin_lock_irqsave(&m
->lock
, flags
);
969 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
974 m
->current_pgpath
= NULL
;
975 m
->current_pg
= NULL
;
978 spin_unlock_irqrestore(&m
->lock
, flags
);
980 queue_work(kmultipathd
, &m
->trigger_event
);
985 * Set/clear bypassed status of a PG.
986 * PGs are numbered upwards from 1 in the order they were declared.
988 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
990 struct priority_group
*pg
;
993 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
994 (pgnum
> m
->nr_priority_groups
)) {
995 DMWARN("invalid PG number supplied to bypass_pg");
999 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1004 bypass_pg(m
, pg
, bypassed
);
1009 * Should we retry pg_init immediately?
1011 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1013 unsigned long flags
;
1014 int limit_reached
= 0;
1016 spin_lock_irqsave(&m
->lock
, flags
);
1018 if (m
->pg_init_count
<= m
->pg_init_retries
)
1019 m
->pg_init_required
= 1;
1023 spin_unlock_irqrestore(&m
->lock
, flags
);
1025 return limit_reached
;
1029 * pg_init must call this when it has completed its initialisation
1031 void dm_pg_init_complete(struct dm_path
*path
, unsigned err_flags
)
1033 struct pgpath
*pgpath
= path_to_pgpath(path
);
1034 struct priority_group
*pg
= pgpath
->pg
;
1035 struct multipath
*m
= pg
->m
;
1036 unsigned long flags
;
1039 * If requested, retry pg_init until maximum number of retries exceeded.
1040 * If retry not requested and PG already bypassed, always fail the path.
1042 if (err_flags
& MP_RETRY
) {
1043 if (pg_init_limit_reached(m
, pgpath
))
1044 err_flags
|= MP_FAIL_PATH
;
1045 } else if (err_flags
&& pg
->bypassed
)
1046 err_flags
|= MP_FAIL_PATH
;
1048 if (err_flags
& MP_FAIL_PATH
)
1051 if (err_flags
& MP_BYPASS_PG
)
1052 bypass_pg(m
, pg
, 1);
1054 spin_lock_irqsave(&m
->lock
, flags
);
1055 if (err_flags
& ~MP_RETRY
) {
1056 m
->current_pgpath
= NULL
;
1057 m
->current_pg
= NULL
;
1058 } else if (!m
->pg_init_required
)
1061 m
->pg_init_in_progress
= 0;
1062 queue_work(kmultipathd
, &m
->process_queued_ios
);
1063 spin_unlock_irqrestore(&m
->lock
, flags
);
1069 static int do_end_io(struct multipath
*m
, struct bio
*bio
,
1070 int error
, struct dm_mpath_io
*mpio
)
1072 struct hw_handler
*hwh
= &m
->hw_handler
;
1073 unsigned err_flags
= MP_FAIL_PATH
; /* Default behavior */
1074 unsigned long flags
;
1077 return 0; /* I/O complete */
1079 if ((error
== -EWOULDBLOCK
) && bio_rw_ahead(bio
))
1082 if (error
== -EOPNOTSUPP
)
1085 spin_lock_irqsave(&m
->lock
, flags
);
1086 if (!m
->nr_valid_paths
) {
1087 if (__must_push_back(m
)) {
1088 spin_unlock_irqrestore(&m
->lock
, flags
);
1089 return DM_ENDIO_REQUEUE
;
1090 } else if (!m
->queue_if_no_path
) {
1091 spin_unlock_irqrestore(&m
->lock
, flags
);
1094 spin_unlock_irqrestore(&m
->lock
, flags
);
1098 spin_unlock_irqrestore(&m
->lock
, flags
);
1100 if (hwh
->type
&& hwh
->type
->error
)
1101 err_flags
= hwh
->type
->error(hwh
, bio
);
1104 if (err_flags
& MP_FAIL_PATH
)
1105 fail_path(mpio
->pgpath
);
1107 if (err_flags
& MP_BYPASS_PG
)
1108 bypass_pg(m
, mpio
->pgpath
->pg
, 1);
1111 if (err_flags
& MP_ERROR_IO
)
1115 dm_bio_restore(&mpio
->details
, bio
);
1117 /* queue for the daemon to resubmit or fail */
1118 spin_lock_irqsave(&m
->lock
, flags
);
1119 bio_list_add(&m
->queued_ios
, bio
);
1122 queue_work(kmultipathd
, &m
->process_queued_ios
);
1123 spin_unlock_irqrestore(&m
->lock
, flags
);
1125 return DM_ENDIO_INCOMPLETE
; /* io not complete */
1128 static int multipath_end_io(struct dm_target
*ti
, struct bio
*bio
,
1129 int error
, union map_info
*map_context
)
1131 struct multipath
*m
= ti
->private;
1132 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1133 struct pgpath
*pgpath
= mpio
->pgpath
;
1134 struct path_selector
*ps
;
1137 r
= do_end_io(m
, bio
, error
, mpio
);
1139 ps
= &pgpath
->pg
->ps
;
1140 if (ps
->type
->end_io
)
1141 ps
->type
->end_io(ps
, &pgpath
->path
);
1143 if (r
!= DM_ENDIO_INCOMPLETE
)
1144 mempool_free(mpio
, m
->mpio_pool
);
1150 * Suspend can't complete until all the I/O is processed so if
1151 * the last path fails we must error any remaining I/O.
1152 * Note that if the freeze_bdev fails while suspending, the
1153 * queue_if_no_path state is lost - userspace should reset it.
1155 static void multipath_presuspend(struct dm_target
*ti
)
1157 struct multipath
*m
= (struct multipath
*) ti
->private;
1159 queue_if_no_path(m
, 0, 1);
1163 * Restore the queue_if_no_path setting.
1165 static void multipath_resume(struct dm_target
*ti
)
1167 struct multipath
*m
= (struct multipath
*) ti
->private;
1168 unsigned long flags
;
1170 spin_lock_irqsave(&m
->lock
, flags
);
1171 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1172 spin_unlock_irqrestore(&m
->lock
, flags
);
1176 * Info output has the following format:
1177 * num_multipath_feature_args [multipath_feature_args]*
1178 * num_handler_status_args [handler_status_args]*
1179 * num_groups init_group_number
1180 * [A|D|E num_ps_status_args [ps_status_args]*
1181 * num_paths num_selector_args
1182 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1184 * Table output has the following format (identical to the constructor string):
1185 * num_feature_args [features_args]*
1186 * num_handler_args hw_handler [hw_handler_args]*
1187 * num_groups init_group_number
1188 * [priority selector-name num_ps_args [ps_args]*
1189 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1191 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1192 char *result
, unsigned int maxlen
)
1195 unsigned long flags
;
1196 struct multipath
*m
= (struct multipath
*) ti
->private;
1197 struct hw_handler
*hwh
= &m
->hw_handler
;
1198 struct priority_group
*pg
;
1203 spin_lock_irqsave(&m
->lock
, flags
);
1206 if (type
== STATUSTYPE_INFO
)
1207 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1209 DMEMIT("%u ", m
->queue_if_no_path
+
1210 (m
->pg_init_retries
> 0) * 2);
1211 if (m
->queue_if_no_path
)
1212 DMEMIT("queue_if_no_path ");
1213 if (m
->pg_init_retries
)
1214 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1217 if (hwh
->type
&& hwh
->type
->status
)
1218 sz
+= hwh
->type
->status(hwh
, type
, result
+ sz
, maxlen
- sz
);
1219 else if (!hwh
->type
|| type
== STATUSTYPE_INFO
)
1222 DMEMIT("1 %s ", hwh
->type
->name
);
1224 DMEMIT("%u ", m
->nr_priority_groups
);
1227 pg_num
= m
->next_pg
->pg_num
;
1228 else if (m
->current_pg
)
1229 pg_num
= m
->current_pg
->pg_num
;
1233 DMEMIT("%u ", pg_num
);
1236 case STATUSTYPE_INFO
:
1237 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1239 state
= 'D'; /* Disabled */
1240 else if (pg
== m
->current_pg
)
1241 state
= 'A'; /* Currently Active */
1243 state
= 'E'; /* Enabled */
1245 DMEMIT("%c ", state
);
1247 if (pg
->ps
.type
->status
)
1248 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1254 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1255 pg
->ps
.type
->info_args
);
1257 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1258 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1259 p
->path
.is_active
? "A" : "F",
1261 if (pg
->ps
.type
->status
)
1262 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1263 &p
->path
, type
, result
+ sz
,
1269 case STATUSTYPE_TABLE
:
1270 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1271 DMEMIT("%s ", pg
->ps
.type
->name
);
1273 if (pg
->ps
.type
->status
)
1274 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1280 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1281 pg
->ps
.type
->table_args
);
1283 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1284 DMEMIT("%s ", p
->path
.dev
->name
);
1285 if (pg
->ps
.type
->status
)
1286 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1287 &p
->path
, type
, result
+ sz
,
1294 spin_unlock_irqrestore(&m
->lock
, flags
);
1299 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1303 struct multipath
*m
= (struct multipath
*) ti
->private;
1307 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1308 return queue_if_no_path(m
, 1, 0);
1309 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1310 return queue_if_no_path(m
, 0, 0);
1316 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1317 return bypass_pg_num(m
, argv
[1], 1);
1318 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1319 return bypass_pg_num(m
, argv
[1], 0);
1320 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1321 return switch_pg_num(m
, argv
[1]);
1322 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1323 action
= reinstate_path
;
1324 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1329 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1330 dm_table_get_mode(ti
->table
), &dev
);
1332 DMWARN("message: error getting device %s",
1337 r
= action_dev(m
, dev
, action
);
1339 dm_put_device(ti
, dev
);
1344 DMWARN("Unrecognised multipath message received.");
1348 static int multipath_ioctl(struct dm_target
*ti
, struct inode
*inode
,
1349 struct file
*filp
, unsigned int cmd
,
1352 struct multipath
*m
= (struct multipath
*) ti
->private;
1353 struct block_device
*bdev
= NULL
;
1354 unsigned long flags
;
1355 struct file fake_file
= {};
1356 struct dentry fake_dentry
= {};
1359 fake_file
.f_path
.dentry
= &fake_dentry
;
1361 spin_lock_irqsave(&m
->lock
, flags
);
1363 if (!m
->current_pgpath
)
1366 if (m
->current_pgpath
) {
1367 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1368 fake_dentry
.d_inode
= bdev
->bd_inode
;
1369 fake_file
.f_mode
= m
->current_pgpath
->path
.dev
->mode
;
1377 spin_unlock_irqrestore(&m
->lock
, flags
);
1379 return r
? : blkdev_driver_ioctl(bdev
->bd_inode
, &fake_file
,
1380 bdev
->bd_disk
, cmd
, arg
);
1383 /*-----------------------------------------------------------------
1385 *---------------------------------------------------------------*/
1386 static struct target_type multipath_target
= {
1387 .name
= "multipath",
1388 .version
= {1, 0, 5},
1389 .module
= THIS_MODULE
,
1390 .ctr
= multipath_ctr
,
1391 .dtr
= multipath_dtr
,
1392 .map
= multipath_map
,
1393 .end_io
= multipath_end_io
,
1394 .presuspend
= multipath_presuspend
,
1395 .resume
= multipath_resume
,
1396 .status
= multipath_status
,
1397 .message
= multipath_message
,
1398 .ioctl
= multipath_ioctl
,
1401 static int __init
dm_multipath_init(void)
1405 /* allocate a slab for the dm_ios */
1406 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1410 r
= dm_register_target(&multipath_target
);
1412 DMERR("register failed %d", r
);
1413 kmem_cache_destroy(_mpio_cache
);
1417 kmultipathd
= create_workqueue("kmpathd");
1419 DMERR("failed to create workqueue kmpathd");
1420 dm_unregister_target(&multipath_target
);
1421 kmem_cache_destroy(_mpio_cache
);
1425 DMINFO("version %u.%u.%u loaded",
1426 multipath_target
.version
[0], multipath_target
.version
[1],
1427 multipath_target
.version
[2]);
1432 static void __exit
dm_multipath_exit(void)
1436 destroy_workqueue(kmultipathd
);
1438 r
= dm_unregister_target(&multipath_target
);
1440 DMERR("target unregister failed %d", r
);
1441 kmem_cache_destroy(_mpio_cache
);
1444 EXPORT_SYMBOL_GPL(dm_pg_init_complete
);
1446 module_init(dm_multipath_init
);
1447 module_exit(dm_multipath_exit
);
1449 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1450 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1451 MODULE_LICENSE("GPL");