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.
8 #include <linux/device-mapper.h>
10 #include "dm-path-selector.h"
11 #include "dm-uevent.h"
13 #include <linux/ctype.h>
14 #include <linux/init.h>
15 #include <linux/mempool.h>
16 #include <linux/module.h>
17 #include <linux/pagemap.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
20 #include <linux/workqueue.h>
21 #include <scsi/scsi_dh.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 is_active
; /* Path status */
33 unsigned fail_count
; /* Cumulative failure count */
36 struct work_struct activate_path
;
39 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
45 struct priority_group
{
46 struct list_head list
;
48 struct multipath
*m
; /* Owning multipath instance */
49 struct path_selector ps
;
51 unsigned pg_num
; /* Reference number */
52 unsigned bypassed
; /* Temporarily bypass this PG? */
54 unsigned nr_pgpaths
; /* Number of paths in PG */
55 struct list_head pgpaths
;
58 /* Multipath context */
60 struct list_head list
;
65 const char *hw_handler_name
;
66 char *hw_handler_params
;
67 unsigned nr_priority_groups
;
68 struct list_head priority_groups
;
69 unsigned pg_init_required
; /* pg_init needs calling? */
70 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
72 unsigned nr_valid_paths
; /* Total number of usable paths */
73 struct pgpath
*current_pgpath
;
74 struct priority_group
*current_pg
;
75 struct priority_group
*next_pg
; /* Switch to this PG if set */
76 unsigned repeat_count
; /* I/Os left before calling PS again */
78 unsigned queue_io
; /* Must we queue all I/O? */
79 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
80 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
81 unsigned pg_init_retries
; /* Number of times to retry pg_init */
82 unsigned pg_init_count
; /* Number of times pg_init called */
84 struct work_struct process_queued_ios
;
85 struct list_head queued_ios
;
88 struct work_struct trigger_event
;
91 * We must use a mempool of dm_mpath_io structs so that we
92 * can resubmit bios on error.
98 * Context information attached to each bio we process.
101 struct pgpath
*pgpath
;
105 typedef int (*action_fn
) (struct pgpath
*pgpath
);
107 #define MIN_IOS 256 /* Mempool size */
109 static struct kmem_cache
*_mpio_cache
;
111 static struct workqueue_struct
*kmultipathd
, *kmpath_handlerd
;
112 static void process_queued_ios(struct work_struct
*work
);
113 static void trigger_event(struct work_struct
*work
);
114 static void activate_path(struct work_struct
*work
);
117 /*-----------------------------------------------
118 * Allocation routines
119 *-----------------------------------------------*/
121 static struct pgpath
*alloc_pgpath(void)
123 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
126 pgpath
->is_active
= 1;
127 INIT_WORK(&pgpath
->activate_path
, activate_path
);
133 static void free_pgpath(struct pgpath
*pgpath
)
138 static struct priority_group
*alloc_priority_group(void)
140 struct priority_group
*pg
;
142 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
145 INIT_LIST_HEAD(&pg
->pgpaths
);
150 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
152 struct pgpath
*pgpath
, *tmp
;
153 struct multipath
*m
= ti
->private;
155 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
156 list_del(&pgpath
->list
);
157 if (m
->hw_handler_name
)
158 scsi_dh_detach(bdev_get_queue(pgpath
->path
.dev
->bdev
));
159 dm_put_device(ti
, pgpath
->path
.dev
);
164 static void free_priority_group(struct priority_group
*pg
,
165 struct dm_target
*ti
)
167 struct path_selector
*ps
= &pg
->ps
;
170 ps
->type
->destroy(ps
);
171 dm_put_path_selector(ps
->type
);
174 free_pgpaths(&pg
->pgpaths
, ti
);
178 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
182 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
184 INIT_LIST_HEAD(&m
->priority_groups
);
185 INIT_LIST_HEAD(&m
->queued_ios
);
186 spin_lock_init(&m
->lock
);
188 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
189 INIT_WORK(&m
->trigger_event
, trigger_event
);
190 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
202 static void free_multipath(struct multipath
*m
)
204 struct priority_group
*pg
, *tmp
;
206 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
208 free_priority_group(pg
, m
->ti
);
211 kfree(m
->hw_handler_name
);
212 kfree(m
->hw_handler_params
);
213 mempool_destroy(m
->mpio_pool
);
218 /*-----------------------------------------------
220 *-----------------------------------------------*/
222 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
224 m
->current_pg
= pgpath
->pg
;
226 /* Must we initialise the PG first, and queue I/O till it's ready? */
227 if (m
->hw_handler_name
) {
228 m
->pg_init_required
= 1;
231 m
->pg_init_required
= 0;
235 m
->pg_init_count
= 0;
238 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
,
241 struct dm_path
*path
;
243 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
, nr_bytes
);
247 m
->current_pgpath
= path_to_pgpath(path
);
249 if (m
->current_pg
!= pg
)
250 __switch_pg(m
, m
->current_pgpath
);
255 static void __choose_pgpath(struct multipath
*m
, size_t nr_bytes
)
257 struct priority_group
*pg
;
258 unsigned bypassed
= 1;
260 if (!m
->nr_valid_paths
)
263 /* Were we instructed to switch PG? */
267 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
271 /* Don't change PG until it has no remaining paths */
272 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
, nr_bytes
))
276 * Loop through priority groups until we find a valid path.
277 * First time we skip PGs marked 'bypassed'.
278 * Second time we only try the ones we skipped.
281 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
282 if (pg
->bypassed
== bypassed
)
284 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
287 } while (bypassed
--);
290 m
->current_pgpath
= NULL
;
291 m
->current_pg
= NULL
;
295 * Check whether bios must be queued in the device-mapper core rather
296 * than here in the target.
298 * m->lock must be held on entry.
300 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
301 * same value then we are not between multipath_presuspend()
302 * and multipath_resume() calls and we have no need to check
303 * for the DMF_NOFLUSH_SUSPENDING flag.
305 static int __must_push_back(struct multipath
*m
)
307 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
308 dm_noflush_suspending(m
->ti
));
311 static int map_io(struct multipath
*m
, struct request
*clone
,
312 struct dm_mpath_io
*mpio
, unsigned was_queued
)
314 int r
= DM_MAPIO_REMAPPED
;
315 size_t nr_bytes
= blk_rq_bytes(clone
);
317 struct pgpath
*pgpath
;
318 struct block_device
*bdev
;
320 spin_lock_irqsave(&m
->lock
, flags
);
322 /* Do we need to select a new pgpath? */
323 if (!m
->current_pgpath
||
324 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
325 __choose_pgpath(m
, nr_bytes
);
327 pgpath
= m
->current_pgpath
;
332 if ((pgpath
&& m
->queue_io
) ||
333 (!pgpath
&& m
->queue_if_no_path
)) {
334 /* Queue for the daemon to resubmit */
335 list_add_tail(&clone
->queuelist
, &m
->queued_ios
);
337 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
339 queue_work(kmultipathd
, &m
->process_queued_ios
);
341 r
= DM_MAPIO_SUBMITTED
;
343 bdev
= pgpath
->path
.dev
->bdev
;
344 clone
->q
= bdev_get_queue(bdev
);
345 clone
->rq_disk
= bdev
->bd_disk
;
346 } else if (__must_push_back(m
))
347 r
= DM_MAPIO_REQUEUE
;
349 r
= -EIO
; /* Failed */
351 mpio
->pgpath
= pgpath
;
352 mpio
->nr_bytes
= nr_bytes
;
354 if (r
== DM_MAPIO_REMAPPED
&& pgpath
->pg
->ps
.type
->start_io
)
355 pgpath
->pg
->ps
.type
->start_io(&pgpath
->pg
->ps
, &pgpath
->path
,
358 spin_unlock_irqrestore(&m
->lock
, flags
);
364 * If we run out of usable paths, should we queue I/O or error it?
366 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
367 unsigned save_old_value
)
371 spin_lock_irqsave(&m
->lock
, flags
);
374 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
376 m
->saved_queue_if_no_path
= queue_if_no_path
;
377 m
->queue_if_no_path
= queue_if_no_path
;
378 if (!m
->queue_if_no_path
&& m
->queue_size
)
379 queue_work(kmultipathd
, &m
->process_queued_ios
);
381 spin_unlock_irqrestore(&m
->lock
, flags
);
386 /*-----------------------------------------------------------------
387 * The multipath daemon is responsible for resubmitting queued ios.
388 *---------------------------------------------------------------*/
390 static void dispatch_queued_ios(struct multipath
*m
)
394 struct dm_mpath_io
*mpio
;
395 union map_info
*info
;
396 struct request
*clone
, *n
;
399 spin_lock_irqsave(&m
->lock
, flags
);
400 list_splice_init(&m
->queued_ios
, &cl
);
401 spin_unlock_irqrestore(&m
->lock
, flags
);
403 list_for_each_entry_safe(clone
, n
, &cl
, queuelist
) {
404 list_del_init(&clone
->queuelist
);
406 info
= dm_get_rq_mapinfo(clone
);
409 r
= map_io(m
, clone
, mpio
, 1);
411 mempool_free(mpio
, m
->mpio_pool
);
412 dm_kill_unmapped_request(clone
, r
);
413 } else if (r
== DM_MAPIO_REMAPPED
)
414 dm_dispatch_request(clone
);
415 else if (r
== DM_MAPIO_REQUEUE
) {
416 mempool_free(mpio
, m
->mpio_pool
);
417 dm_requeue_unmapped_request(clone
);
422 static void process_queued_ios(struct work_struct
*work
)
424 struct multipath
*m
=
425 container_of(work
, struct multipath
, process_queued_ios
);
426 struct pgpath
*pgpath
= NULL
, *tmp
;
427 unsigned must_queue
= 1;
430 spin_lock_irqsave(&m
->lock
, flags
);
435 if (!m
->current_pgpath
)
436 __choose_pgpath(m
, 0);
438 pgpath
= m
->current_pgpath
;
440 if ((pgpath
&& !m
->queue_io
) ||
441 (!pgpath
&& !m
->queue_if_no_path
))
444 if (m
->pg_init_required
&& !m
->pg_init_in_progress
&& pgpath
) {
446 m
->pg_init_required
= 0;
447 list_for_each_entry(tmp
, &pgpath
->pg
->pgpaths
, list
) {
448 if (queue_work(kmpath_handlerd
, &tmp
->activate_path
))
449 m
->pg_init_in_progress
++;
453 spin_unlock_irqrestore(&m
->lock
, flags
);
455 dispatch_queued_ios(m
);
459 * An event is triggered whenever a path is taken out of use.
460 * Includes path failure and PG bypass.
462 static void trigger_event(struct work_struct
*work
)
464 struct multipath
*m
=
465 container_of(work
, struct multipath
, trigger_event
);
467 dm_table_event(m
->ti
->table
);
470 /*-----------------------------------------------------------------
471 * Constructor/argument parsing:
472 * <#multipath feature args> [<arg>]*
473 * <#hw_handler args> [hw_handler [<arg>]*]
475 * <initial priority group>
476 * [<selector> <#selector args> [<arg>]*
477 * <#paths> <#per-path selector args>
478 * [<path> [<arg>]* ]+ ]+
479 *---------------------------------------------------------------*/
486 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
489 (sscanf(str
, "%u", v
) != 1) ||
492 *error
= param
->error
;
504 static char *shift(struct arg_set
*as
)
518 static void consume(struct arg_set
*as
, unsigned n
)
520 BUG_ON (as
->argc
< n
);
525 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
526 struct dm_target
*ti
)
529 struct path_selector_type
*pst
;
532 static struct param _params
[] = {
533 {0, 1024, "invalid number of path selector args"},
536 pst
= dm_get_path_selector(shift(as
));
538 ti
->error
= "unknown path selector type";
542 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
544 dm_put_path_selector(pst
);
548 if (ps_argc
> as
->argc
) {
549 dm_put_path_selector(pst
);
550 ti
->error
= "not enough arguments for path selector";
554 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
556 dm_put_path_selector(pst
);
557 ti
->error
= "path selector constructor failed";
562 consume(as
, ps_argc
);
567 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
568 struct dm_target
*ti
)
572 struct multipath
*m
= ti
->private;
574 /* we need at least a path arg */
576 ti
->error
= "no device given";
577 return ERR_PTR(-EINVAL
);
582 return ERR_PTR(-ENOMEM
);
584 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
585 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
587 ti
->error
= "error getting device";
591 if (m
->hw_handler_name
) {
592 struct request_queue
*q
= bdev_get_queue(p
->path
.dev
->bdev
);
594 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
597 * Already attached to different hw_handler,
598 * try to reattach with correct one.
601 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
605 ti
->error
= "error attaching hardware handler";
606 dm_put_device(ti
, p
->path
.dev
);
610 if (m
->hw_handler_params
) {
611 r
= scsi_dh_set_params(q
, m
->hw_handler_params
);
613 ti
->error
= "unable to set hardware "
614 "handler parameters";
616 dm_put_device(ti
, p
->path
.dev
);
622 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
624 dm_put_device(ti
, p
->path
.dev
);
635 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
638 static struct param _params
[] = {
639 {1, 1024, "invalid number of paths"},
640 {0, 1024, "invalid number of selector args"}
644 unsigned i
, nr_selector_args
, nr_params
;
645 struct priority_group
*pg
;
646 struct dm_target
*ti
= m
->ti
;
650 ti
->error
= "not enough priority group arguments";
651 return ERR_PTR(-EINVAL
);
654 pg
= alloc_priority_group();
656 ti
->error
= "couldn't allocate priority group";
657 return ERR_PTR(-ENOMEM
);
661 r
= parse_path_selector(as
, pg
, ti
);
668 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
672 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
676 nr_params
= 1 + nr_selector_args
;
677 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
678 struct pgpath
*pgpath
;
679 struct arg_set path_args
;
681 if (as
->argc
< nr_params
) {
682 ti
->error
= "not enough path parameters";
687 path_args
.argc
= nr_params
;
688 path_args
.argv
= as
->argv
;
690 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
691 if (IS_ERR(pgpath
)) {
697 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
698 consume(as
, nr_params
);
704 free_priority_group(pg
, ti
);
708 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
712 struct dm_target
*ti
= m
->ti
;
714 static struct param _params
[] = {
715 {0, 1024, "invalid number of hardware handler args"},
718 if (read_param(_params
, shift(as
), &hw_argc
, &ti
->error
))
724 if (hw_argc
> as
->argc
) {
725 ti
->error
= "not enough arguments for hardware handler";
729 m
->hw_handler_name
= kstrdup(shift(as
), GFP_KERNEL
);
730 request_module("scsi_dh_%s", m
->hw_handler_name
);
731 if (scsi_dh_handler_exist(m
->hw_handler_name
) == 0) {
732 ti
->error
= "unknown hardware handler type";
741 for (i
= 0; i
<= hw_argc
- 2; i
++)
742 len
+= strlen(as
->argv
[i
]) + 1;
743 p
= m
->hw_handler_params
= kzalloc(len
, GFP_KERNEL
);
745 ti
->error
= "memory allocation failed";
749 j
= sprintf(p
, "%d", hw_argc
- 1);
750 for (i
= 0, p
+=j
+1; i
<= hw_argc
- 2; i
++, p
+=j
+1)
751 j
= sprintf(p
, "%s", as
->argv
[i
]);
753 consume(as
, hw_argc
- 1);
757 kfree(m
->hw_handler_name
);
758 m
->hw_handler_name
= NULL
;
762 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
766 struct dm_target
*ti
= m
->ti
;
767 const char *param_name
;
769 static struct param _params
[] = {
770 {0, 3, "invalid number of feature args"},
771 {1, 50, "pg_init_retries must be between 1 and 50"},
774 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
781 if (argc
> as
->argc
) {
782 ti
->error
= "not enough arguments for features";
787 param_name
= shift(as
);
790 if (!strnicmp(param_name
, MESG_STR("queue_if_no_path"))) {
791 r
= queue_if_no_path(m
, 1, 0);
795 if (!strnicmp(param_name
, MESG_STR("pg_init_retries")) &&
797 r
= read_param(_params
+ 1, shift(as
),
798 &m
->pg_init_retries
, &ti
->error
);
803 ti
->error
= "Unrecognised multipath feature request";
805 } while (argc
&& !r
);
810 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
813 /* target parameters */
814 static struct param _params
[] = {
815 {1, 1024, "invalid number of priority groups"},
816 {1, 1024, "invalid initial priority group number"},
822 unsigned pg_count
= 0;
823 unsigned next_pg_num
;
828 m
= alloc_multipath(ti
);
830 ti
->error
= "can't allocate multipath";
834 r
= parse_features(&as
, m
);
838 r
= parse_hw_handler(&as
, m
);
842 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
846 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
850 /* parse the priority groups */
852 struct priority_group
*pg
;
854 pg
= parse_priority_group(&as
, m
);
860 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
861 list_add_tail(&pg
->list
, &m
->priority_groups
);
863 pg
->pg_num
= pg_count
;
868 if (pg_count
!= m
->nr_priority_groups
) {
869 ti
->error
= "priority group count mismatch";
874 ti
->num_flush_requests
= 1;
883 static void multipath_dtr(struct dm_target
*ti
)
885 struct multipath
*m
= (struct multipath
*) ti
->private;
887 flush_workqueue(kmpath_handlerd
);
888 flush_workqueue(kmultipathd
);
889 flush_scheduled_work();
894 * Map cloned requests
896 static int multipath_map(struct dm_target
*ti
, struct request
*clone
,
897 union map_info
*map_context
)
900 struct dm_mpath_io
*mpio
;
901 struct multipath
*m
= (struct multipath
*) ti
->private;
903 mpio
= mempool_alloc(m
->mpio_pool
, GFP_ATOMIC
);
905 /* ENOMEM, requeue */
906 return DM_MAPIO_REQUEUE
;
907 memset(mpio
, 0, sizeof(*mpio
));
909 map_context
->ptr
= mpio
;
910 clone
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
911 r
= map_io(m
, clone
, mpio
, 0);
912 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
913 mempool_free(mpio
, m
->mpio_pool
);
919 * Take a path out of use.
921 static int fail_path(struct pgpath
*pgpath
)
924 struct multipath
*m
= pgpath
->pg
->m
;
926 spin_lock_irqsave(&m
->lock
, flags
);
928 if (!pgpath
->is_active
)
931 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
933 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
934 pgpath
->is_active
= 0;
935 pgpath
->fail_count
++;
939 if (pgpath
== m
->current_pgpath
)
940 m
->current_pgpath
= NULL
;
942 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
943 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
945 schedule_work(&m
->trigger_event
);
948 spin_unlock_irqrestore(&m
->lock
, flags
);
954 * Reinstate a previously-failed path
956 static int reinstate_path(struct pgpath
*pgpath
)
960 struct multipath
*m
= pgpath
->pg
->m
;
962 spin_lock_irqsave(&m
->lock
, flags
);
964 if (pgpath
->is_active
)
967 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
968 DMWARN("Reinstate path not supported by path selector %s",
969 pgpath
->pg
->ps
.type
->name
);
974 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
978 pgpath
->is_active
= 1;
980 if (!m
->nr_valid_paths
++ && m
->queue_size
) {
981 m
->current_pgpath
= NULL
;
982 queue_work(kmultipathd
, &m
->process_queued_ios
);
983 } else if (m
->hw_handler_name
&& (m
->current_pg
== pgpath
->pg
)) {
984 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
))
985 m
->pg_init_in_progress
++;
988 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
989 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
991 schedule_work(&m
->trigger_event
);
994 spin_unlock_irqrestore(&m
->lock
, flags
);
1000 * Fail or reinstate all paths that match the provided struct dm_dev.
1002 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
1006 struct pgpath
*pgpath
;
1007 struct priority_group
*pg
;
1009 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1010 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
1011 if (pgpath
->path
.dev
== dev
)
1020 * Temporarily try to avoid having to use the specified PG
1022 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
1025 unsigned long flags
;
1027 spin_lock_irqsave(&m
->lock
, flags
);
1029 pg
->bypassed
= bypassed
;
1030 m
->current_pgpath
= NULL
;
1031 m
->current_pg
= NULL
;
1033 spin_unlock_irqrestore(&m
->lock
, flags
);
1035 schedule_work(&m
->trigger_event
);
1039 * Switch to using the specified PG from the next I/O that gets mapped
1041 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
1043 struct priority_group
*pg
;
1045 unsigned long flags
;
1047 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1048 (pgnum
> m
->nr_priority_groups
)) {
1049 DMWARN("invalid PG number supplied to switch_pg_num");
1053 spin_lock_irqsave(&m
->lock
, flags
);
1054 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1059 m
->current_pgpath
= NULL
;
1060 m
->current_pg
= NULL
;
1063 spin_unlock_irqrestore(&m
->lock
, flags
);
1065 schedule_work(&m
->trigger_event
);
1070 * Set/clear bypassed status of a PG.
1071 * PGs are numbered upwards from 1 in the order they were declared.
1073 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
1075 struct priority_group
*pg
;
1078 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1079 (pgnum
> m
->nr_priority_groups
)) {
1080 DMWARN("invalid PG number supplied to bypass_pg");
1084 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1089 bypass_pg(m
, pg
, bypassed
);
1094 * Should we retry pg_init immediately?
1096 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1098 unsigned long flags
;
1099 int limit_reached
= 0;
1101 spin_lock_irqsave(&m
->lock
, flags
);
1103 if (m
->pg_init_count
<= m
->pg_init_retries
)
1104 m
->pg_init_required
= 1;
1108 spin_unlock_irqrestore(&m
->lock
, flags
);
1110 return limit_reached
;
1113 static void pg_init_done(struct dm_path
*path
, int errors
)
1115 struct pgpath
*pgpath
= path_to_pgpath(path
);
1116 struct priority_group
*pg
= pgpath
->pg
;
1117 struct multipath
*m
= pg
->m
;
1118 unsigned long flags
;
1120 /* device or driver problems */
1125 if (!m
->hw_handler_name
) {
1129 DMERR("Cannot failover device because scsi_dh_%s was not "
1130 "loaded.", m
->hw_handler_name
);
1132 * Fail path for now, so we do not ping pong
1136 case SCSI_DH_DEV_TEMP_BUSY
:
1138 * Probably doing something like FW upgrade on the
1139 * controller so try the other pg.
1141 bypass_pg(m
, pg
, 1);
1143 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1145 case SCSI_DH_IMM_RETRY
:
1146 case SCSI_DH_RES_TEMP_UNAVAIL
:
1147 if (pg_init_limit_reached(m
, pgpath
))
1153 * We probably do not want to fail the path for a device
1154 * error, but this is what the old dm did. In future
1155 * patches we can do more advanced handling.
1160 spin_lock_irqsave(&m
->lock
, flags
);
1162 if (pgpath
== m
->current_pgpath
) {
1163 DMERR("Could not failover device. Error %d.", errors
);
1164 m
->current_pgpath
= NULL
;
1165 m
->current_pg
= NULL
;
1167 } else if (!m
->pg_init_required
) {
1172 m
->pg_init_in_progress
--;
1173 if (!m
->pg_init_in_progress
)
1174 queue_work(kmultipathd
, &m
->process_queued_ios
);
1175 spin_unlock_irqrestore(&m
->lock
, flags
);
1178 static void activate_path(struct work_struct
*work
)
1181 struct pgpath
*pgpath
=
1182 container_of(work
, struct pgpath
, activate_path
);
1184 ret
= scsi_dh_activate(bdev_get_queue(pgpath
->path
.dev
->bdev
));
1185 pg_init_done(&pgpath
->path
, ret
);
1191 static int do_end_io(struct multipath
*m
, struct request
*clone
,
1192 int error
, struct dm_mpath_io
*mpio
)
1195 * We don't queue any clone request inside the multipath target
1196 * during end I/O handling, since those clone requests don't have
1197 * bio clones. If we queue them inside the multipath target,
1198 * we need to make bio clones, that requires memory allocation.
1199 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1200 * don't have bio clones.)
1201 * Instead of queueing the clone request here, we queue the original
1202 * request into dm core, which will remake a clone request and
1203 * clone bios for it and resubmit it later.
1205 int r
= DM_ENDIO_REQUEUE
;
1206 unsigned long flags
;
1208 if (!error
&& !clone
->errors
)
1209 return 0; /* I/O complete */
1211 if (error
== -EOPNOTSUPP
)
1215 fail_path(mpio
->pgpath
);
1217 spin_lock_irqsave(&m
->lock
, flags
);
1218 if (!m
->nr_valid_paths
&& !m
->queue_if_no_path
&& !__must_push_back(m
))
1220 spin_unlock_irqrestore(&m
->lock
, flags
);
1225 static int multipath_end_io(struct dm_target
*ti
, struct request
*clone
,
1226 int error
, union map_info
*map_context
)
1228 struct multipath
*m
= ti
->private;
1229 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1230 struct pgpath
*pgpath
= mpio
->pgpath
;
1231 struct path_selector
*ps
;
1234 r
= do_end_io(m
, clone
, error
, mpio
);
1236 ps
= &pgpath
->pg
->ps
;
1237 if (ps
->type
->end_io
)
1238 ps
->type
->end_io(ps
, &pgpath
->path
, mpio
->nr_bytes
);
1240 mempool_free(mpio
, m
->mpio_pool
);
1246 * Suspend can't complete until all the I/O is processed so if
1247 * the last path fails we must error any remaining I/O.
1248 * Note that if the freeze_bdev fails while suspending, the
1249 * queue_if_no_path state is lost - userspace should reset it.
1251 static void multipath_presuspend(struct dm_target
*ti
)
1253 struct multipath
*m
= (struct multipath
*) ti
->private;
1255 queue_if_no_path(m
, 0, 1);
1259 * Restore the queue_if_no_path setting.
1261 static void multipath_resume(struct dm_target
*ti
)
1263 struct multipath
*m
= (struct multipath
*) ti
->private;
1264 unsigned long flags
;
1266 spin_lock_irqsave(&m
->lock
, flags
);
1267 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1268 spin_unlock_irqrestore(&m
->lock
, flags
);
1272 * Info output has the following format:
1273 * num_multipath_feature_args [multipath_feature_args]*
1274 * num_handler_status_args [handler_status_args]*
1275 * num_groups init_group_number
1276 * [A|D|E num_ps_status_args [ps_status_args]*
1277 * num_paths num_selector_args
1278 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1280 * Table output has the following format (identical to the constructor string):
1281 * num_feature_args [features_args]*
1282 * num_handler_args hw_handler [hw_handler_args]*
1283 * num_groups init_group_number
1284 * [priority selector-name num_ps_args [ps_args]*
1285 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1287 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1288 char *result
, unsigned int maxlen
)
1291 unsigned long flags
;
1292 struct multipath
*m
= (struct multipath
*) ti
->private;
1293 struct priority_group
*pg
;
1298 spin_lock_irqsave(&m
->lock
, flags
);
1301 if (type
== STATUSTYPE_INFO
)
1302 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1304 DMEMIT("%u ", m
->queue_if_no_path
+
1305 (m
->pg_init_retries
> 0) * 2);
1306 if (m
->queue_if_no_path
)
1307 DMEMIT("queue_if_no_path ");
1308 if (m
->pg_init_retries
)
1309 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1312 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1315 DMEMIT("1 %s ", m
->hw_handler_name
);
1317 DMEMIT("%u ", m
->nr_priority_groups
);
1320 pg_num
= m
->next_pg
->pg_num
;
1321 else if (m
->current_pg
)
1322 pg_num
= m
->current_pg
->pg_num
;
1326 DMEMIT("%u ", pg_num
);
1329 case STATUSTYPE_INFO
:
1330 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1332 state
= 'D'; /* Disabled */
1333 else if (pg
== m
->current_pg
)
1334 state
= 'A'; /* Currently Active */
1336 state
= 'E'; /* Enabled */
1338 DMEMIT("%c ", state
);
1340 if (pg
->ps
.type
->status
)
1341 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1347 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1348 pg
->ps
.type
->info_args
);
1350 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1351 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1352 p
->is_active
? "A" : "F",
1354 if (pg
->ps
.type
->status
)
1355 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1356 &p
->path
, type
, result
+ sz
,
1362 case STATUSTYPE_TABLE
:
1363 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1364 DMEMIT("%s ", pg
->ps
.type
->name
);
1366 if (pg
->ps
.type
->status
)
1367 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1373 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1374 pg
->ps
.type
->table_args
);
1376 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1377 DMEMIT("%s ", p
->path
.dev
->name
);
1378 if (pg
->ps
.type
->status
)
1379 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1380 &p
->path
, type
, result
+ sz
,
1387 spin_unlock_irqrestore(&m
->lock
, flags
);
1392 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1396 struct multipath
*m
= (struct multipath
*) ti
->private;
1400 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path")))
1401 return queue_if_no_path(m
, 1, 0);
1402 else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path")))
1403 return queue_if_no_path(m
, 0, 0);
1409 if (!strnicmp(argv
[0], MESG_STR("disable_group")))
1410 return bypass_pg_num(m
, argv
[1], 1);
1411 else if (!strnicmp(argv
[0], MESG_STR("enable_group")))
1412 return bypass_pg_num(m
, argv
[1], 0);
1413 else if (!strnicmp(argv
[0], MESG_STR("switch_group")))
1414 return switch_pg_num(m
, argv
[1]);
1415 else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1416 action
= reinstate_path
;
1417 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1422 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1423 dm_table_get_mode(ti
->table
), &dev
);
1425 DMWARN("message: error getting device %s",
1430 r
= action_dev(m
, dev
, action
);
1432 dm_put_device(ti
, dev
);
1437 DMWARN("Unrecognised multipath message received.");
1441 static int multipath_ioctl(struct dm_target
*ti
, unsigned int cmd
,
1444 struct multipath
*m
= (struct multipath
*) ti
->private;
1445 struct block_device
*bdev
= NULL
;
1447 unsigned long flags
;
1450 spin_lock_irqsave(&m
->lock
, flags
);
1452 if (!m
->current_pgpath
)
1453 __choose_pgpath(m
, 0);
1455 if (m
->current_pgpath
) {
1456 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1457 mode
= m
->current_pgpath
->path
.dev
->mode
;
1465 spin_unlock_irqrestore(&m
->lock
, flags
);
1467 return r
? : __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
1470 static int multipath_iterate_devices(struct dm_target
*ti
,
1471 iterate_devices_callout_fn fn
, void *data
)
1473 struct multipath
*m
= ti
->private;
1474 struct priority_group
*pg
;
1478 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1479 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1480 ret
= fn(ti
, p
->path
.dev
, ti
->begin
, ti
->len
, data
);
1490 static int __pgpath_busy(struct pgpath
*pgpath
)
1492 struct request_queue
*q
= bdev_get_queue(pgpath
->path
.dev
->bdev
);
1494 return dm_underlying_device_busy(q
);
1498 * We return "busy", only when we can map I/Os but underlying devices
1499 * are busy (so even if we map I/Os now, the I/Os will wait on
1500 * the underlying queue).
1501 * In other words, if we want to kill I/Os or queue them inside us
1502 * due to map unavailability, we don't return "busy". Otherwise,
1503 * dm core won't give us the I/Os and we can't do what we want.
1505 static int multipath_busy(struct dm_target
*ti
)
1507 int busy
= 0, has_active
= 0;
1508 struct multipath
*m
= ti
->private;
1509 struct priority_group
*pg
;
1510 struct pgpath
*pgpath
;
1511 unsigned long flags
;
1513 spin_lock_irqsave(&m
->lock
, flags
);
1515 /* Guess which priority_group will be used at next mapping time */
1516 if (unlikely(!m
->current_pgpath
&& m
->next_pg
))
1518 else if (likely(m
->current_pg
))
1522 * We don't know which pg will be used at next mapping time.
1523 * We don't call __choose_pgpath() here to avoid to trigger
1524 * pg_init just by busy checking.
1525 * So we don't know whether underlying devices we will be using
1526 * at next mapping time are busy or not. Just try mapping.
1531 * If there is one non-busy active path at least, the path selector
1532 * will be able to select it. So we consider such a pg as not busy.
1535 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
)
1536 if (pgpath
->is_active
) {
1539 if (!__pgpath_busy(pgpath
)) {
1547 * No active path in this pg, so this pg won't be used and
1548 * the current_pg will be changed at next mapping time.
1549 * We need to try mapping to determine it.
1554 spin_unlock_irqrestore(&m
->lock
, flags
);
1559 /*-----------------------------------------------------------------
1561 *---------------------------------------------------------------*/
1562 static struct target_type multipath_target
= {
1563 .name
= "multipath",
1564 .version
= {1, 1, 0},
1565 .module
= THIS_MODULE
,
1566 .ctr
= multipath_ctr
,
1567 .dtr
= multipath_dtr
,
1568 .map_rq
= multipath_map
,
1569 .rq_end_io
= multipath_end_io
,
1570 .presuspend
= multipath_presuspend
,
1571 .resume
= multipath_resume
,
1572 .status
= multipath_status
,
1573 .message
= multipath_message
,
1574 .ioctl
= multipath_ioctl
,
1575 .iterate_devices
= multipath_iterate_devices
,
1576 .busy
= multipath_busy
,
1579 static int __init
dm_multipath_init(void)
1583 /* allocate a slab for the dm_ios */
1584 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1588 r
= dm_register_target(&multipath_target
);
1590 DMERR("register failed %d", r
);
1591 kmem_cache_destroy(_mpio_cache
);
1595 kmultipathd
= create_workqueue("kmpathd");
1597 DMERR("failed to create workqueue kmpathd");
1598 dm_unregister_target(&multipath_target
);
1599 kmem_cache_destroy(_mpio_cache
);
1604 * A separate workqueue is used to handle the device handlers
1605 * to avoid overloading existing workqueue. Overloading the
1606 * old workqueue would also create a bottleneck in the
1607 * path of the storage hardware device activation.
1609 kmpath_handlerd
= create_singlethread_workqueue("kmpath_handlerd");
1610 if (!kmpath_handlerd
) {
1611 DMERR("failed to create workqueue kmpath_handlerd");
1612 destroy_workqueue(kmultipathd
);
1613 dm_unregister_target(&multipath_target
);
1614 kmem_cache_destroy(_mpio_cache
);
1618 DMINFO("version %u.%u.%u loaded",
1619 multipath_target
.version
[0], multipath_target
.version
[1],
1620 multipath_target
.version
[2]);
1625 static void __exit
dm_multipath_exit(void)
1627 destroy_workqueue(kmpath_handlerd
);
1628 destroy_workqueue(kmultipathd
);
1630 dm_unregister_target(&multipath_target
);
1631 kmem_cache_destroy(_mpio_cache
);
1634 module_init(dm_multipath_init
);
1635 module_exit(dm_multipath_exit
);
1637 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1638 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1639 MODULE_LICENSE("GPL");