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 */
71 wait_queue_head_t pg_init_wait
; /* Wait for pg_init completion */
73 unsigned nr_valid_paths
; /* Total number of usable paths */
74 struct pgpath
*current_pgpath
;
75 struct priority_group
*current_pg
;
76 struct priority_group
*next_pg
; /* Switch to this PG if set */
77 unsigned repeat_count
; /* I/Os left before calling PS again */
79 unsigned queue_io
; /* Must we queue all I/O? */
80 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
81 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
82 unsigned pg_init_retries
; /* Number of times to retry pg_init */
83 unsigned pg_init_count
; /* Number of times pg_init called */
85 struct work_struct process_queued_ios
;
86 struct list_head queued_ios
;
89 struct work_struct trigger_event
;
92 * We must use a mempool of dm_mpath_io structs so that we
93 * can resubmit bios on error.
97 struct mutex work_mutex
;
101 * Context information attached to each bio we process.
104 struct pgpath
*pgpath
;
108 typedef int (*action_fn
) (struct pgpath
*pgpath
);
110 #define MIN_IOS 256 /* Mempool size */
112 static struct kmem_cache
*_mpio_cache
;
114 static struct workqueue_struct
*kmultipathd
, *kmpath_handlerd
;
115 static void process_queued_ios(struct work_struct
*work
);
116 static void trigger_event(struct work_struct
*work
);
117 static void activate_path(struct work_struct
*work
);
120 /*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
124 static struct pgpath
*alloc_pgpath(void)
126 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
129 pgpath
->is_active
= 1;
130 INIT_WORK(&pgpath
->activate_path
, activate_path
);
136 static void free_pgpath(struct pgpath
*pgpath
)
141 static struct priority_group
*alloc_priority_group(void)
143 struct priority_group
*pg
;
145 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
148 INIT_LIST_HEAD(&pg
->pgpaths
);
153 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
155 struct pgpath
*pgpath
, *tmp
;
156 struct multipath
*m
= ti
->private;
158 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
159 list_del(&pgpath
->list
);
160 if (m
->hw_handler_name
)
161 scsi_dh_detach(bdev_get_queue(pgpath
->path
.dev
->bdev
));
162 dm_put_device(ti
, pgpath
->path
.dev
);
167 static void free_priority_group(struct priority_group
*pg
,
168 struct dm_target
*ti
)
170 struct path_selector
*ps
= &pg
->ps
;
173 ps
->type
->destroy(ps
);
174 dm_put_path_selector(ps
->type
);
177 free_pgpaths(&pg
->pgpaths
, ti
);
181 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
185 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
187 INIT_LIST_HEAD(&m
->priority_groups
);
188 INIT_LIST_HEAD(&m
->queued_ios
);
189 spin_lock_init(&m
->lock
);
191 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
192 INIT_WORK(&m
->trigger_event
, trigger_event
);
193 init_waitqueue_head(&m
->pg_init_wait
);
194 mutex_init(&m
->work_mutex
);
195 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
207 static void free_multipath(struct multipath
*m
)
209 struct priority_group
*pg
, *tmp
;
211 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
213 free_priority_group(pg
, m
->ti
);
216 kfree(m
->hw_handler_name
);
217 kfree(m
->hw_handler_params
);
218 mempool_destroy(m
->mpio_pool
);
223 /*-----------------------------------------------
225 *-----------------------------------------------*/
227 static void __pg_init_all_paths(struct multipath
*m
)
229 struct pgpath
*pgpath
;
232 m
->pg_init_required
= 0;
233 list_for_each_entry(pgpath
, &m
->current_pg
->pgpaths
, list
) {
234 /* Skip failed paths */
235 if (!pgpath
->is_active
)
237 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
))
238 m
->pg_init_in_progress
++;
242 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
244 m
->current_pg
= pgpath
->pg
;
246 /* Must we initialise the PG first, and queue I/O till it's ready? */
247 if (m
->hw_handler_name
) {
248 m
->pg_init_required
= 1;
251 m
->pg_init_required
= 0;
255 m
->pg_init_count
= 0;
258 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
,
261 struct dm_path
*path
;
263 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
, nr_bytes
);
267 m
->current_pgpath
= path_to_pgpath(path
);
269 if (m
->current_pg
!= pg
)
270 __switch_pg(m
, m
->current_pgpath
);
275 static void __choose_pgpath(struct multipath
*m
, size_t nr_bytes
)
277 struct priority_group
*pg
;
278 unsigned bypassed
= 1;
280 if (!m
->nr_valid_paths
)
283 /* Were we instructed to switch PG? */
287 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
291 /* Don't change PG until it has no remaining paths */
292 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
, nr_bytes
))
296 * Loop through priority groups until we find a valid path.
297 * First time we skip PGs marked 'bypassed'.
298 * Second time we only try the ones we skipped.
301 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
302 if (pg
->bypassed
== bypassed
)
304 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
307 } while (bypassed
--);
310 m
->current_pgpath
= NULL
;
311 m
->current_pg
= NULL
;
315 * Check whether bios must be queued in the device-mapper core rather
316 * than here in the target.
318 * m->lock must be held on entry.
320 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
321 * same value then we are not between multipath_presuspend()
322 * and multipath_resume() calls and we have no need to check
323 * for the DMF_NOFLUSH_SUSPENDING flag.
325 static int __must_push_back(struct multipath
*m
)
327 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
328 dm_noflush_suspending(m
->ti
));
331 static int map_io(struct multipath
*m
, struct request
*clone
,
332 struct dm_mpath_io
*mpio
, unsigned was_queued
)
334 int r
= DM_MAPIO_REMAPPED
;
335 size_t nr_bytes
= blk_rq_bytes(clone
);
337 struct pgpath
*pgpath
;
338 struct block_device
*bdev
;
340 spin_lock_irqsave(&m
->lock
, flags
);
342 /* Do we need to select a new pgpath? */
343 if (!m
->current_pgpath
||
344 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
345 __choose_pgpath(m
, nr_bytes
);
347 pgpath
= m
->current_pgpath
;
352 if ((pgpath
&& m
->queue_io
) ||
353 (!pgpath
&& m
->queue_if_no_path
)) {
354 /* Queue for the daemon to resubmit */
355 list_add_tail(&clone
->queuelist
, &m
->queued_ios
);
357 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
359 queue_work(kmultipathd
, &m
->process_queued_ios
);
361 r
= DM_MAPIO_SUBMITTED
;
363 bdev
= pgpath
->path
.dev
->bdev
;
364 clone
->q
= bdev_get_queue(bdev
);
365 clone
->rq_disk
= bdev
->bd_disk
;
366 } else if (__must_push_back(m
))
367 r
= DM_MAPIO_REQUEUE
;
369 r
= -EIO
; /* Failed */
371 mpio
->pgpath
= pgpath
;
372 mpio
->nr_bytes
= nr_bytes
;
374 if (r
== DM_MAPIO_REMAPPED
&& pgpath
->pg
->ps
.type
->start_io
)
375 pgpath
->pg
->ps
.type
->start_io(&pgpath
->pg
->ps
, &pgpath
->path
,
378 spin_unlock_irqrestore(&m
->lock
, flags
);
384 * If we run out of usable paths, should we queue I/O or error it?
386 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
387 unsigned save_old_value
)
391 spin_lock_irqsave(&m
->lock
, flags
);
394 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
396 m
->saved_queue_if_no_path
= queue_if_no_path
;
397 m
->queue_if_no_path
= queue_if_no_path
;
398 if (!m
->queue_if_no_path
&& m
->queue_size
)
399 queue_work(kmultipathd
, &m
->process_queued_ios
);
401 spin_unlock_irqrestore(&m
->lock
, flags
);
406 /*-----------------------------------------------------------------
407 * The multipath daemon is responsible for resubmitting queued ios.
408 *---------------------------------------------------------------*/
410 static void dispatch_queued_ios(struct multipath
*m
)
414 struct dm_mpath_io
*mpio
;
415 union map_info
*info
;
416 struct request
*clone
, *n
;
419 spin_lock_irqsave(&m
->lock
, flags
);
420 list_splice_init(&m
->queued_ios
, &cl
);
421 spin_unlock_irqrestore(&m
->lock
, flags
);
423 list_for_each_entry_safe(clone
, n
, &cl
, queuelist
) {
424 list_del_init(&clone
->queuelist
);
426 info
= dm_get_rq_mapinfo(clone
);
429 r
= map_io(m
, clone
, mpio
, 1);
431 mempool_free(mpio
, m
->mpio_pool
);
432 dm_kill_unmapped_request(clone
, r
);
433 } else if (r
== DM_MAPIO_REMAPPED
)
434 dm_dispatch_request(clone
);
435 else if (r
== DM_MAPIO_REQUEUE
) {
436 mempool_free(mpio
, m
->mpio_pool
);
437 dm_requeue_unmapped_request(clone
);
442 static void process_queued_ios(struct work_struct
*work
)
444 struct multipath
*m
=
445 container_of(work
, struct multipath
, process_queued_ios
);
446 struct pgpath
*pgpath
= NULL
;
447 unsigned must_queue
= 1;
450 spin_lock_irqsave(&m
->lock
, flags
);
455 if (!m
->current_pgpath
)
456 __choose_pgpath(m
, 0);
458 pgpath
= m
->current_pgpath
;
460 if ((pgpath
&& !m
->queue_io
) ||
461 (!pgpath
&& !m
->queue_if_no_path
))
464 if (m
->pg_init_required
&& !m
->pg_init_in_progress
&& pgpath
)
465 __pg_init_all_paths(m
);
468 spin_unlock_irqrestore(&m
->lock
, flags
);
470 dispatch_queued_ios(m
);
474 * An event is triggered whenever a path is taken out of use.
475 * Includes path failure and PG bypass.
477 static void trigger_event(struct work_struct
*work
)
479 struct multipath
*m
=
480 container_of(work
, struct multipath
, trigger_event
);
482 dm_table_event(m
->ti
->table
);
485 /*-----------------------------------------------------------------
486 * Constructor/argument parsing:
487 * <#multipath feature args> [<arg>]*
488 * <#hw_handler args> [hw_handler [<arg>]*]
490 * <initial priority group>
491 * [<selector> <#selector args> [<arg>]*
492 * <#paths> <#per-path selector args>
493 * [<path> [<arg>]* ]+ ]+
494 *---------------------------------------------------------------*/
501 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
504 (sscanf(str
, "%u", v
) != 1) ||
507 *error
= param
->error
;
519 static char *shift(struct arg_set
*as
)
533 static void consume(struct arg_set
*as
, unsigned n
)
535 BUG_ON (as
->argc
< n
);
540 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
541 struct dm_target
*ti
)
544 struct path_selector_type
*pst
;
547 static struct param _params
[] = {
548 {0, 1024, "invalid number of path selector args"},
551 pst
= dm_get_path_selector(shift(as
));
553 ti
->error
= "unknown path selector type";
557 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
559 dm_put_path_selector(pst
);
563 if (ps_argc
> as
->argc
) {
564 dm_put_path_selector(pst
);
565 ti
->error
= "not enough arguments for path selector";
569 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
571 dm_put_path_selector(pst
);
572 ti
->error
= "path selector constructor failed";
577 consume(as
, ps_argc
);
582 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
583 struct dm_target
*ti
)
587 struct multipath
*m
= ti
->private;
589 /* we need at least a path arg */
591 ti
->error
= "no device given";
592 return ERR_PTR(-EINVAL
);
597 return ERR_PTR(-ENOMEM
);
599 r
= dm_get_device(ti
, shift(as
), dm_table_get_mode(ti
->table
),
602 ti
->error
= "error getting device";
606 if (m
->hw_handler_name
) {
607 struct request_queue
*q
= bdev_get_queue(p
->path
.dev
->bdev
);
609 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
612 * Already attached to different hw_handler,
613 * try to reattach with correct one.
616 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
620 ti
->error
= "error attaching hardware handler";
621 dm_put_device(ti
, p
->path
.dev
);
625 if (m
->hw_handler_params
) {
626 r
= scsi_dh_set_params(q
, m
->hw_handler_params
);
628 ti
->error
= "unable to set hardware "
629 "handler parameters";
631 dm_put_device(ti
, p
->path
.dev
);
637 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
639 dm_put_device(ti
, p
->path
.dev
);
650 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
653 static struct param _params
[] = {
654 {1, 1024, "invalid number of paths"},
655 {0, 1024, "invalid number of selector args"}
659 unsigned i
, nr_selector_args
, nr_params
;
660 struct priority_group
*pg
;
661 struct dm_target
*ti
= m
->ti
;
665 ti
->error
= "not enough priority group arguments";
666 return ERR_PTR(-EINVAL
);
669 pg
= alloc_priority_group();
671 ti
->error
= "couldn't allocate priority group";
672 return ERR_PTR(-ENOMEM
);
676 r
= parse_path_selector(as
, pg
, ti
);
683 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
687 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
691 nr_params
= 1 + nr_selector_args
;
692 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
693 struct pgpath
*pgpath
;
694 struct arg_set path_args
;
696 if (as
->argc
< nr_params
) {
697 ti
->error
= "not enough path parameters";
702 path_args
.argc
= nr_params
;
703 path_args
.argv
= as
->argv
;
705 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
706 if (IS_ERR(pgpath
)) {
712 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
713 consume(as
, nr_params
);
719 free_priority_group(pg
, ti
);
723 static int parse_hw_handler(struct arg_set
*as
, struct multipath
*m
)
727 struct dm_target
*ti
= m
->ti
;
729 static struct param _params
[] = {
730 {0, 1024, "invalid number of hardware handler args"},
733 if (read_param(_params
, shift(as
), &hw_argc
, &ti
->error
))
739 if (hw_argc
> as
->argc
) {
740 ti
->error
= "not enough arguments for hardware handler";
744 m
->hw_handler_name
= kstrdup(shift(as
), GFP_KERNEL
);
745 request_module("scsi_dh_%s", m
->hw_handler_name
);
746 if (scsi_dh_handler_exist(m
->hw_handler_name
) == 0) {
747 ti
->error
= "unknown hardware handler type";
756 for (i
= 0; i
<= hw_argc
- 2; i
++)
757 len
+= strlen(as
->argv
[i
]) + 1;
758 p
= m
->hw_handler_params
= kzalloc(len
, GFP_KERNEL
);
760 ti
->error
= "memory allocation failed";
764 j
= sprintf(p
, "%d", hw_argc
- 1);
765 for (i
= 0, p
+=j
+1; i
<= hw_argc
- 2; i
++, p
+=j
+1)
766 j
= sprintf(p
, "%s", as
->argv
[i
]);
768 consume(as
, hw_argc
- 1);
772 kfree(m
->hw_handler_name
);
773 m
->hw_handler_name
= NULL
;
777 static int parse_features(struct arg_set
*as
, struct multipath
*m
)
781 struct dm_target
*ti
= m
->ti
;
782 const char *param_name
;
784 static struct param _params
[] = {
785 {0, 3, "invalid number of feature args"},
786 {1, 50, "pg_init_retries must be between 1 and 50"},
789 r
= read_param(_params
, shift(as
), &argc
, &ti
->error
);
797 param_name
= shift(as
);
800 if (!strnicmp(param_name
, MESG_STR("queue_if_no_path"))) {
801 r
= queue_if_no_path(m
, 1, 0);
805 if (!strnicmp(param_name
, MESG_STR("pg_init_retries")) &&
807 r
= read_param(_params
+ 1, shift(as
),
808 &m
->pg_init_retries
, &ti
->error
);
813 ti
->error
= "Unrecognised multipath feature request";
815 } while (argc
&& !r
);
820 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
823 /* target parameters */
824 static struct param _params
[] = {
825 {1, 1024, "invalid number of priority groups"},
826 {1, 1024, "invalid initial priority group number"},
832 unsigned pg_count
= 0;
833 unsigned next_pg_num
;
838 m
= alloc_multipath(ti
);
840 ti
->error
= "can't allocate multipath";
844 r
= parse_features(&as
, m
);
848 r
= parse_hw_handler(&as
, m
);
852 r
= read_param(_params
, shift(&as
), &m
->nr_priority_groups
, &ti
->error
);
856 r
= read_param(_params
+ 1, shift(&as
), &next_pg_num
, &ti
->error
);
860 /* parse the priority groups */
862 struct priority_group
*pg
;
864 pg
= parse_priority_group(&as
, m
);
870 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
871 list_add_tail(&pg
->list
, &m
->priority_groups
);
873 pg
->pg_num
= pg_count
;
878 if (pg_count
!= m
->nr_priority_groups
) {
879 ti
->error
= "priority group count mismatch";
884 ti
->num_flush_requests
= 1;
885 ti
->num_discard_requests
= 1;
894 static void multipath_wait_for_pg_init_completion(struct multipath
*m
)
896 DECLARE_WAITQUEUE(wait
, current
);
899 add_wait_queue(&m
->pg_init_wait
, &wait
);
902 set_current_state(TASK_UNINTERRUPTIBLE
);
904 spin_lock_irqsave(&m
->lock
, flags
);
905 if (!m
->pg_init_in_progress
) {
906 spin_unlock_irqrestore(&m
->lock
, flags
);
909 spin_unlock_irqrestore(&m
->lock
, flags
);
913 set_current_state(TASK_RUNNING
);
915 remove_wait_queue(&m
->pg_init_wait
, &wait
);
918 static void flush_multipath_work(struct multipath
*m
)
920 flush_workqueue(kmpath_handlerd
);
921 multipath_wait_for_pg_init_completion(m
);
922 flush_workqueue(kmultipathd
);
923 flush_work_sync(&m
->trigger_event
);
926 static void multipath_dtr(struct dm_target
*ti
)
928 struct multipath
*m
= ti
->private;
930 flush_multipath_work(m
);
935 * Map cloned requests
937 static int multipath_map(struct dm_target
*ti
, struct request
*clone
,
938 union map_info
*map_context
)
941 struct dm_mpath_io
*mpio
;
942 struct multipath
*m
= (struct multipath
*) ti
->private;
944 mpio
= mempool_alloc(m
->mpio_pool
, GFP_ATOMIC
);
946 /* ENOMEM, requeue */
947 return DM_MAPIO_REQUEUE
;
948 memset(mpio
, 0, sizeof(*mpio
));
950 map_context
->ptr
= mpio
;
951 clone
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
952 r
= map_io(m
, clone
, mpio
, 0);
953 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
954 mempool_free(mpio
, m
->mpio_pool
);
960 * Take a path out of use.
962 static int fail_path(struct pgpath
*pgpath
)
965 struct multipath
*m
= pgpath
->pg
->m
;
967 spin_lock_irqsave(&m
->lock
, flags
);
969 if (!pgpath
->is_active
)
972 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
974 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
975 pgpath
->is_active
= 0;
976 pgpath
->fail_count
++;
980 if (pgpath
== m
->current_pgpath
)
981 m
->current_pgpath
= NULL
;
983 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
984 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
986 schedule_work(&m
->trigger_event
);
989 spin_unlock_irqrestore(&m
->lock
, flags
);
995 * Reinstate a previously-failed path
997 static int reinstate_path(struct pgpath
*pgpath
)
1000 unsigned long flags
;
1001 struct multipath
*m
= pgpath
->pg
->m
;
1003 spin_lock_irqsave(&m
->lock
, flags
);
1005 if (pgpath
->is_active
)
1008 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
1009 DMWARN("Reinstate path not supported by path selector %s",
1010 pgpath
->pg
->ps
.type
->name
);
1015 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
1019 pgpath
->is_active
= 1;
1021 if (!m
->nr_valid_paths
++ && m
->queue_size
) {
1022 m
->current_pgpath
= NULL
;
1023 queue_work(kmultipathd
, &m
->process_queued_ios
);
1024 } else if (m
->hw_handler_name
&& (m
->current_pg
== pgpath
->pg
)) {
1025 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
))
1026 m
->pg_init_in_progress
++;
1029 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
1030 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
1032 schedule_work(&m
->trigger_event
);
1035 spin_unlock_irqrestore(&m
->lock
, flags
);
1041 * Fail or reinstate all paths that match the provided struct dm_dev.
1043 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
1047 struct pgpath
*pgpath
;
1048 struct priority_group
*pg
;
1050 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1051 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
1052 if (pgpath
->path
.dev
== dev
)
1061 * Temporarily try to avoid having to use the specified PG
1063 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
1066 unsigned long flags
;
1068 spin_lock_irqsave(&m
->lock
, flags
);
1070 pg
->bypassed
= bypassed
;
1071 m
->current_pgpath
= NULL
;
1072 m
->current_pg
= NULL
;
1074 spin_unlock_irqrestore(&m
->lock
, flags
);
1076 schedule_work(&m
->trigger_event
);
1080 * Switch to using the specified PG from the next I/O that gets mapped
1082 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
1084 struct priority_group
*pg
;
1086 unsigned long flags
;
1088 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1089 (pgnum
> m
->nr_priority_groups
)) {
1090 DMWARN("invalid PG number supplied to switch_pg_num");
1094 spin_lock_irqsave(&m
->lock
, flags
);
1095 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1100 m
->current_pgpath
= NULL
;
1101 m
->current_pg
= NULL
;
1104 spin_unlock_irqrestore(&m
->lock
, flags
);
1106 schedule_work(&m
->trigger_event
);
1111 * Set/clear bypassed status of a PG.
1112 * PGs are numbered upwards from 1 in the order they were declared.
1114 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
1116 struct priority_group
*pg
;
1119 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1120 (pgnum
> m
->nr_priority_groups
)) {
1121 DMWARN("invalid PG number supplied to bypass_pg");
1125 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1130 bypass_pg(m
, pg
, bypassed
);
1135 * Should we retry pg_init immediately?
1137 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1139 unsigned long flags
;
1140 int limit_reached
= 0;
1142 spin_lock_irqsave(&m
->lock
, flags
);
1144 if (m
->pg_init_count
<= m
->pg_init_retries
)
1145 m
->pg_init_required
= 1;
1149 spin_unlock_irqrestore(&m
->lock
, flags
);
1151 return limit_reached
;
1154 static void pg_init_done(void *data
, int errors
)
1156 struct pgpath
*pgpath
= data
;
1157 struct priority_group
*pg
= pgpath
->pg
;
1158 struct multipath
*m
= pg
->m
;
1159 unsigned long flags
;
1161 /* device or driver problems */
1166 if (!m
->hw_handler_name
) {
1170 DMERR("Could not failover the device: Handler scsi_dh_%s "
1171 "Error %d.", m
->hw_handler_name
, errors
);
1173 * Fail path for now, so we do not ping pong
1177 case SCSI_DH_DEV_TEMP_BUSY
:
1179 * Probably doing something like FW upgrade on the
1180 * controller so try the other pg.
1182 bypass_pg(m
, pg
, 1);
1184 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1186 case SCSI_DH_IMM_RETRY
:
1187 case SCSI_DH_RES_TEMP_UNAVAIL
:
1188 if (pg_init_limit_reached(m
, pgpath
))
1194 * We probably do not want to fail the path for a device
1195 * error, but this is what the old dm did. In future
1196 * patches we can do more advanced handling.
1201 spin_lock_irqsave(&m
->lock
, flags
);
1203 if (pgpath
== m
->current_pgpath
) {
1204 DMERR("Could not failover device. Error %d.", errors
);
1205 m
->current_pgpath
= NULL
;
1206 m
->current_pg
= NULL
;
1208 } else if (!m
->pg_init_required
)
1211 if (--m
->pg_init_in_progress
)
1212 /* Activations of other paths are still on going */
1215 if (!m
->pg_init_required
)
1218 queue_work(kmultipathd
, &m
->process_queued_ios
);
1221 * Wake up any thread waiting to suspend.
1223 wake_up(&m
->pg_init_wait
);
1226 spin_unlock_irqrestore(&m
->lock
, flags
);
1229 static void activate_path(struct work_struct
*work
)
1231 struct pgpath
*pgpath
=
1232 container_of(work
, struct pgpath
, activate_path
);
1234 scsi_dh_activate(bdev_get_queue(pgpath
->path
.dev
->bdev
),
1235 pg_init_done
, pgpath
);
1241 static int do_end_io(struct multipath
*m
, struct request
*clone
,
1242 int error
, struct dm_mpath_io
*mpio
)
1245 * We don't queue any clone request inside the multipath target
1246 * during end I/O handling, since those clone requests don't have
1247 * bio clones. If we queue them inside the multipath target,
1248 * we need to make bio clones, that requires memory allocation.
1249 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1250 * don't have bio clones.)
1251 * Instead of queueing the clone request here, we queue the original
1252 * request into dm core, which will remake a clone request and
1253 * clone bios for it and resubmit it later.
1255 int r
= DM_ENDIO_REQUEUE
;
1256 unsigned long flags
;
1258 if (!error
&& !clone
->errors
)
1259 return 0; /* I/O complete */
1261 if (error
== -EOPNOTSUPP
)
1264 if (clone
->cmd_flags
& REQ_DISCARD
)
1266 * Pass all discard request failures up.
1267 * FIXME: only fail_path if the discard failed due to a
1268 * transport problem. This requires precise understanding
1269 * of the underlying failure (e.g. the SCSI sense).
1274 fail_path(mpio
->pgpath
);
1276 spin_lock_irqsave(&m
->lock
, flags
);
1277 if (!m
->nr_valid_paths
&& !m
->queue_if_no_path
&& !__must_push_back(m
))
1279 spin_unlock_irqrestore(&m
->lock
, flags
);
1284 static int multipath_end_io(struct dm_target
*ti
, struct request
*clone
,
1285 int error
, union map_info
*map_context
)
1287 struct multipath
*m
= ti
->private;
1288 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1289 struct pgpath
*pgpath
= mpio
->pgpath
;
1290 struct path_selector
*ps
;
1293 r
= do_end_io(m
, clone
, error
, mpio
);
1295 ps
= &pgpath
->pg
->ps
;
1296 if (ps
->type
->end_io
)
1297 ps
->type
->end_io(ps
, &pgpath
->path
, mpio
->nr_bytes
);
1299 mempool_free(mpio
, m
->mpio_pool
);
1305 * Suspend can't complete until all the I/O is processed so if
1306 * the last path fails we must error any remaining I/O.
1307 * Note that if the freeze_bdev fails while suspending, the
1308 * queue_if_no_path state is lost - userspace should reset it.
1310 static void multipath_presuspend(struct dm_target
*ti
)
1312 struct multipath
*m
= (struct multipath
*) ti
->private;
1314 queue_if_no_path(m
, 0, 1);
1317 static void multipath_postsuspend(struct dm_target
*ti
)
1319 struct multipath
*m
= ti
->private;
1321 mutex_lock(&m
->work_mutex
);
1322 flush_multipath_work(m
);
1323 mutex_unlock(&m
->work_mutex
);
1327 * Restore the queue_if_no_path setting.
1329 static void multipath_resume(struct dm_target
*ti
)
1331 struct multipath
*m
= (struct multipath
*) ti
->private;
1332 unsigned long flags
;
1334 spin_lock_irqsave(&m
->lock
, flags
);
1335 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1336 spin_unlock_irqrestore(&m
->lock
, flags
);
1340 * Info output has the following format:
1341 * num_multipath_feature_args [multipath_feature_args]*
1342 * num_handler_status_args [handler_status_args]*
1343 * num_groups init_group_number
1344 * [A|D|E num_ps_status_args [ps_status_args]*
1345 * num_paths num_selector_args
1346 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1348 * Table output has the following format (identical to the constructor string):
1349 * num_feature_args [features_args]*
1350 * num_handler_args hw_handler [hw_handler_args]*
1351 * num_groups init_group_number
1352 * [priority selector-name num_ps_args [ps_args]*
1353 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1355 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1356 char *result
, unsigned int maxlen
)
1359 unsigned long flags
;
1360 struct multipath
*m
= (struct multipath
*) ti
->private;
1361 struct priority_group
*pg
;
1366 spin_lock_irqsave(&m
->lock
, flags
);
1369 if (type
== STATUSTYPE_INFO
)
1370 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1372 DMEMIT("%u ", m
->queue_if_no_path
+
1373 (m
->pg_init_retries
> 0) * 2);
1374 if (m
->queue_if_no_path
)
1375 DMEMIT("queue_if_no_path ");
1376 if (m
->pg_init_retries
)
1377 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1380 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1383 DMEMIT("1 %s ", m
->hw_handler_name
);
1385 DMEMIT("%u ", m
->nr_priority_groups
);
1388 pg_num
= m
->next_pg
->pg_num
;
1389 else if (m
->current_pg
)
1390 pg_num
= m
->current_pg
->pg_num
;
1394 DMEMIT("%u ", pg_num
);
1397 case STATUSTYPE_INFO
:
1398 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1400 state
= 'D'; /* Disabled */
1401 else if (pg
== m
->current_pg
)
1402 state
= 'A'; /* Currently Active */
1404 state
= 'E'; /* Enabled */
1406 DMEMIT("%c ", state
);
1408 if (pg
->ps
.type
->status
)
1409 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1415 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1416 pg
->ps
.type
->info_args
);
1418 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1419 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1420 p
->is_active
? "A" : "F",
1422 if (pg
->ps
.type
->status
)
1423 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1424 &p
->path
, type
, result
+ sz
,
1430 case STATUSTYPE_TABLE
:
1431 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1432 DMEMIT("%s ", pg
->ps
.type
->name
);
1434 if (pg
->ps
.type
->status
)
1435 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1441 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1442 pg
->ps
.type
->table_args
);
1444 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1445 DMEMIT("%s ", p
->path
.dev
->name
);
1446 if (pg
->ps
.type
->status
)
1447 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1448 &p
->path
, type
, result
+ sz
,
1455 spin_unlock_irqrestore(&m
->lock
, flags
);
1460 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1464 struct multipath
*m
= (struct multipath
*) ti
->private;
1467 mutex_lock(&m
->work_mutex
);
1469 if (dm_suspended(ti
)) {
1475 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path"))) {
1476 r
= queue_if_no_path(m
, 1, 0);
1478 } else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path"))) {
1479 r
= queue_if_no_path(m
, 0, 0);
1485 DMWARN("Unrecognised multipath message received.");
1489 if (!strnicmp(argv
[0], MESG_STR("disable_group"))) {
1490 r
= bypass_pg_num(m
, argv
[1], 1);
1492 } else if (!strnicmp(argv
[0], MESG_STR("enable_group"))) {
1493 r
= bypass_pg_num(m
, argv
[1], 0);
1495 } else if (!strnicmp(argv
[0], MESG_STR("switch_group"))) {
1496 r
= switch_pg_num(m
, argv
[1]);
1498 } else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1499 action
= reinstate_path
;
1500 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1503 DMWARN("Unrecognised multipath message received.");
1507 r
= dm_get_device(ti
, argv
[1], dm_table_get_mode(ti
->table
), &dev
);
1509 DMWARN("message: error getting device %s",
1514 r
= action_dev(m
, dev
, action
);
1516 dm_put_device(ti
, dev
);
1519 mutex_unlock(&m
->work_mutex
);
1523 static int multipath_ioctl(struct dm_target
*ti
, unsigned int cmd
,
1526 struct multipath
*m
= (struct multipath
*) ti
->private;
1527 struct block_device
*bdev
= NULL
;
1529 unsigned long flags
;
1532 spin_lock_irqsave(&m
->lock
, flags
);
1534 if (!m
->current_pgpath
)
1535 __choose_pgpath(m
, 0);
1537 if (m
->current_pgpath
) {
1538 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1539 mode
= m
->current_pgpath
->path
.dev
->mode
;
1547 spin_unlock_irqrestore(&m
->lock
, flags
);
1549 return r
? : __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
1552 static int multipath_iterate_devices(struct dm_target
*ti
,
1553 iterate_devices_callout_fn fn
, void *data
)
1555 struct multipath
*m
= ti
->private;
1556 struct priority_group
*pg
;
1560 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1561 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1562 ret
= fn(ti
, p
->path
.dev
, ti
->begin
, ti
->len
, data
);
1572 static int __pgpath_busy(struct pgpath
*pgpath
)
1574 struct request_queue
*q
= bdev_get_queue(pgpath
->path
.dev
->bdev
);
1576 return dm_underlying_device_busy(q
);
1580 * We return "busy", only when we can map I/Os but underlying devices
1581 * are busy (so even if we map I/Os now, the I/Os will wait on
1582 * the underlying queue).
1583 * In other words, if we want to kill I/Os or queue them inside us
1584 * due to map unavailability, we don't return "busy". Otherwise,
1585 * dm core won't give us the I/Os and we can't do what we want.
1587 static int multipath_busy(struct dm_target
*ti
)
1589 int busy
= 0, has_active
= 0;
1590 struct multipath
*m
= ti
->private;
1591 struct priority_group
*pg
;
1592 struct pgpath
*pgpath
;
1593 unsigned long flags
;
1595 spin_lock_irqsave(&m
->lock
, flags
);
1597 /* Guess which priority_group will be used at next mapping time */
1598 if (unlikely(!m
->current_pgpath
&& m
->next_pg
))
1600 else if (likely(m
->current_pg
))
1604 * We don't know which pg will be used at next mapping time.
1605 * We don't call __choose_pgpath() here to avoid to trigger
1606 * pg_init just by busy checking.
1607 * So we don't know whether underlying devices we will be using
1608 * at next mapping time are busy or not. Just try mapping.
1613 * If there is one non-busy active path at least, the path selector
1614 * will be able to select it. So we consider such a pg as not busy.
1617 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
)
1618 if (pgpath
->is_active
) {
1621 if (!__pgpath_busy(pgpath
)) {
1629 * No active path in this pg, so this pg won't be used and
1630 * the current_pg will be changed at next mapping time.
1631 * We need to try mapping to determine it.
1636 spin_unlock_irqrestore(&m
->lock
, flags
);
1641 /*-----------------------------------------------------------------
1643 *---------------------------------------------------------------*/
1644 static struct target_type multipath_target
= {
1645 .name
= "multipath",
1646 .version
= {1, 1, 1},
1647 .module
= THIS_MODULE
,
1648 .ctr
= multipath_ctr
,
1649 .dtr
= multipath_dtr
,
1650 .map_rq
= multipath_map
,
1651 .rq_end_io
= multipath_end_io
,
1652 .presuspend
= multipath_presuspend
,
1653 .postsuspend
= multipath_postsuspend
,
1654 .resume
= multipath_resume
,
1655 .status
= multipath_status
,
1656 .message
= multipath_message
,
1657 .ioctl
= multipath_ioctl
,
1658 .iterate_devices
= multipath_iterate_devices
,
1659 .busy
= multipath_busy
,
1662 static int __init
dm_multipath_init(void)
1666 /* allocate a slab for the dm_ios */
1667 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1671 r
= dm_register_target(&multipath_target
);
1673 DMERR("register failed %d", r
);
1674 kmem_cache_destroy(_mpio_cache
);
1678 kmultipathd
= alloc_workqueue("kmpathd", WQ_MEM_RECLAIM
, 0);
1680 DMERR("failed to create workqueue kmpathd");
1681 dm_unregister_target(&multipath_target
);
1682 kmem_cache_destroy(_mpio_cache
);
1687 * A separate workqueue is used to handle the device handlers
1688 * to avoid overloading existing workqueue. Overloading the
1689 * old workqueue would also create a bottleneck in the
1690 * path of the storage hardware device activation.
1692 kmpath_handlerd
= alloc_ordered_workqueue("kmpath_handlerd",
1694 if (!kmpath_handlerd
) {
1695 DMERR("failed to create workqueue kmpath_handlerd");
1696 destroy_workqueue(kmultipathd
);
1697 dm_unregister_target(&multipath_target
);
1698 kmem_cache_destroy(_mpio_cache
);
1702 DMINFO("version %u.%u.%u loaded",
1703 multipath_target
.version
[0], multipath_target
.version
[1],
1704 multipath_target
.version
[2]);
1709 static void __exit
dm_multipath_exit(void)
1711 destroy_workqueue(kmpath_handlerd
);
1712 destroy_workqueue(kmultipathd
);
1714 dm_unregister_target(&multipath_target
);
1715 kmem_cache_destroy(_mpio_cache
);
1718 module_init(dm_multipath_init
);
1719 module_exit(dm_multipath_exit
);
1721 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1722 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1723 MODULE_LICENSE("GPL");