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 <linux/delay.h>
22 #include <scsi/scsi_dh.h>
23 #include <linux/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define DM_PG_INIT_DELAY_MSECS 2000
27 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
31 struct list_head list
;
33 struct priority_group
*pg
; /* Owning PG */
34 unsigned is_active
; /* Path status */
35 unsigned fail_count
; /* Cumulative failure count */
38 struct delayed_work activate_path
;
41 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
47 struct priority_group
{
48 struct list_head list
;
50 struct multipath
*m
; /* Owning multipath instance */
51 struct path_selector ps
;
53 unsigned pg_num
; /* Reference number */
54 unsigned bypassed
; /* Temporarily bypass this PG? */
56 unsigned nr_pgpaths
; /* Number of paths in PG */
57 struct list_head pgpaths
;
60 /* Multipath context */
62 struct list_head list
;
65 const char *hw_handler_name
;
66 char *hw_handler_params
;
70 unsigned nr_priority_groups
;
71 struct list_head priority_groups
;
73 wait_queue_head_t pg_init_wait
; /* Wait for pg_init completion */
75 unsigned pg_init_required
; /* pg_init needs calling? */
76 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
77 unsigned pg_init_delay_retry
; /* Delay pg_init retry? */
79 unsigned nr_valid_paths
; /* Total number of usable paths */
80 struct pgpath
*current_pgpath
;
81 struct priority_group
*current_pg
;
82 struct priority_group
*next_pg
; /* Switch to this PG if set */
83 unsigned repeat_count
; /* I/Os left before calling PS again */
85 unsigned queue_io
:1; /* Must we queue all I/O? */
86 unsigned queue_if_no_path
:1; /* Queue I/O if last path fails? */
87 unsigned saved_queue_if_no_path
:1; /* Saved state during suspension */
88 unsigned retain_attached_hw_handler
:1; /* If there's already a hw_handler present, don't change it. */
90 unsigned pg_init_retries
; /* Number of times to retry pg_init */
91 unsigned pg_init_count
; /* Number of times pg_init called */
92 unsigned pg_init_delay_msecs
; /* Number of msecs before pg_init retry */
95 struct work_struct process_queued_ios
;
96 struct list_head queued_ios
;
98 struct work_struct trigger_event
;
101 * We must use a mempool of dm_mpath_io structs so that we
102 * can resubmit bios on error.
104 mempool_t
*mpio_pool
;
106 struct mutex work_mutex
;
110 * Context information attached to each bio we process.
113 struct pgpath
*pgpath
;
117 typedef int (*action_fn
) (struct pgpath
*pgpath
);
119 #define MIN_IOS 256 /* Mempool size */
121 static struct kmem_cache
*_mpio_cache
;
123 static struct workqueue_struct
*kmultipathd
, *kmpath_handlerd
;
124 static void process_queued_ios(struct work_struct
*work
);
125 static void trigger_event(struct work_struct
*work
);
126 static void activate_path(struct work_struct
*work
);
129 /*-----------------------------------------------
130 * Allocation routines
131 *-----------------------------------------------*/
133 static struct pgpath
*alloc_pgpath(void)
135 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
138 pgpath
->is_active
= 1;
139 INIT_DELAYED_WORK(&pgpath
->activate_path
, activate_path
);
145 static void free_pgpath(struct pgpath
*pgpath
)
150 static struct priority_group
*alloc_priority_group(void)
152 struct priority_group
*pg
;
154 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
157 INIT_LIST_HEAD(&pg
->pgpaths
);
162 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
164 struct pgpath
*pgpath
, *tmp
;
165 struct multipath
*m
= ti
->private;
167 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
168 list_del(&pgpath
->list
);
169 if (m
->hw_handler_name
)
170 scsi_dh_detach(bdev_get_queue(pgpath
->path
.dev
->bdev
));
171 dm_put_device(ti
, pgpath
->path
.dev
);
176 static void free_priority_group(struct priority_group
*pg
,
177 struct dm_target
*ti
)
179 struct path_selector
*ps
= &pg
->ps
;
182 ps
->type
->destroy(ps
);
183 dm_put_path_selector(ps
->type
);
186 free_pgpaths(&pg
->pgpaths
, ti
);
190 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
194 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
196 INIT_LIST_HEAD(&m
->priority_groups
);
197 INIT_LIST_HEAD(&m
->queued_ios
);
198 spin_lock_init(&m
->lock
);
200 m
->pg_init_delay_msecs
= DM_PG_INIT_DELAY_DEFAULT
;
201 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
202 INIT_WORK(&m
->trigger_event
, trigger_event
);
203 init_waitqueue_head(&m
->pg_init_wait
);
204 mutex_init(&m
->work_mutex
);
205 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
217 static void free_multipath(struct multipath
*m
)
219 struct priority_group
*pg
, *tmp
;
221 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
223 free_priority_group(pg
, m
->ti
);
226 kfree(m
->hw_handler_name
);
227 kfree(m
->hw_handler_params
);
228 mempool_destroy(m
->mpio_pool
);
232 static int set_mapinfo(struct multipath
*m
, union map_info
*info
)
234 struct dm_mpath_io
*mpio
;
236 mpio
= mempool_alloc(m
->mpio_pool
, GFP_ATOMIC
);
240 memset(mpio
, 0, sizeof(*mpio
));
246 static void clear_mapinfo(struct multipath
*m
, union map_info
*info
)
248 struct dm_mpath_io
*mpio
= info
->ptr
;
251 mempool_free(mpio
, m
->mpio_pool
);
254 /*-----------------------------------------------
256 *-----------------------------------------------*/
258 static void __pg_init_all_paths(struct multipath
*m
)
260 struct pgpath
*pgpath
;
261 unsigned long pg_init_delay
= 0;
264 m
->pg_init_required
= 0;
265 if (m
->pg_init_delay_retry
)
266 pg_init_delay
= msecs_to_jiffies(m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
?
267 m
->pg_init_delay_msecs
: DM_PG_INIT_DELAY_MSECS
);
268 list_for_each_entry(pgpath
, &m
->current_pg
->pgpaths
, list
) {
269 /* Skip failed paths */
270 if (!pgpath
->is_active
)
272 if (queue_delayed_work(kmpath_handlerd
, &pgpath
->activate_path
,
274 m
->pg_init_in_progress
++;
278 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
280 m
->current_pg
= pgpath
->pg
;
282 /* Must we initialise the PG first, and queue I/O till it's ready? */
283 if (m
->hw_handler_name
) {
284 m
->pg_init_required
= 1;
287 m
->pg_init_required
= 0;
291 m
->pg_init_count
= 0;
294 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
,
297 struct dm_path
*path
;
299 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
, nr_bytes
);
303 m
->current_pgpath
= path_to_pgpath(path
);
305 if (m
->current_pg
!= pg
)
306 __switch_pg(m
, m
->current_pgpath
);
311 static void __choose_pgpath(struct multipath
*m
, size_t nr_bytes
)
313 struct priority_group
*pg
;
314 unsigned bypassed
= 1;
316 if (!m
->nr_valid_paths
)
319 /* Were we instructed to switch PG? */
323 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
327 /* Don't change PG until it has no remaining paths */
328 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
, nr_bytes
))
332 * Loop through priority groups until we find a valid path.
333 * First time we skip PGs marked 'bypassed'.
334 * Second time we only try the ones we skipped, but set
335 * pg_init_delay_retry so we do not hammer controllers.
338 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
339 if (pg
->bypassed
== bypassed
)
341 if (!__choose_path_in_pg(m
, pg
, nr_bytes
)) {
343 m
->pg_init_delay_retry
= 1;
347 } while (bypassed
--);
350 m
->current_pgpath
= NULL
;
351 m
->current_pg
= NULL
;
355 * Check whether bios must be queued in the device-mapper core rather
356 * than here in the target.
358 * m->lock must be held on entry.
360 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
361 * same value then we are not between multipath_presuspend()
362 * and multipath_resume() calls and we have no need to check
363 * for the DMF_NOFLUSH_SUSPENDING flag.
365 static int __must_push_back(struct multipath
*m
)
367 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
368 dm_noflush_suspending(m
->ti
));
371 static int map_io(struct multipath
*m
, struct request
*clone
,
372 union map_info
*map_context
, unsigned was_queued
)
374 int r
= DM_MAPIO_REMAPPED
;
375 size_t nr_bytes
= blk_rq_bytes(clone
);
377 struct pgpath
*pgpath
;
378 struct block_device
*bdev
;
379 struct dm_mpath_io
*mpio
= map_context
->ptr
;
381 spin_lock_irqsave(&m
->lock
, flags
);
383 /* Do we need to select a new pgpath? */
384 if (!m
->current_pgpath
||
385 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
386 __choose_pgpath(m
, nr_bytes
);
388 pgpath
= m
->current_pgpath
;
393 if ((pgpath
&& m
->queue_io
) ||
394 (!pgpath
&& m
->queue_if_no_path
)) {
395 /* Queue for the daemon to resubmit */
396 list_add_tail(&clone
->queuelist
, &m
->queued_ios
);
398 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
400 queue_work(kmultipathd
, &m
->process_queued_ios
);
402 r
= DM_MAPIO_SUBMITTED
;
404 bdev
= pgpath
->path
.dev
->bdev
;
405 clone
->q
= bdev_get_queue(bdev
);
406 clone
->rq_disk
= bdev
->bd_disk
;
407 } else if (__must_push_back(m
))
408 r
= DM_MAPIO_REQUEUE
;
410 r
= -EIO
; /* Failed */
412 mpio
->pgpath
= pgpath
;
413 mpio
->nr_bytes
= nr_bytes
;
415 if (r
== DM_MAPIO_REMAPPED
&& pgpath
->pg
->ps
.type
->start_io
)
416 pgpath
->pg
->ps
.type
->start_io(&pgpath
->pg
->ps
, &pgpath
->path
,
419 spin_unlock_irqrestore(&m
->lock
, flags
);
425 * If we run out of usable paths, should we queue I/O or error it?
427 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
428 unsigned save_old_value
)
432 spin_lock_irqsave(&m
->lock
, flags
);
435 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
437 m
->saved_queue_if_no_path
= queue_if_no_path
;
438 m
->queue_if_no_path
= queue_if_no_path
;
439 if (!m
->queue_if_no_path
&& m
->queue_size
)
440 queue_work(kmultipathd
, &m
->process_queued_ios
);
442 spin_unlock_irqrestore(&m
->lock
, flags
);
447 /*-----------------------------------------------------------------
448 * The multipath daemon is responsible for resubmitting queued ios.
449 *---------------------------------------------------------------*/
451 static void dispatch_queued_ios(struct multipath
*m
)
455 union map_info
*info
;
456 struct request
*clone
, *n
;
459 spin_lock_irqsave(&m
->lock
, flags
);
460 list_splice_init(&m
->queued_ios
, &cl
);
461 spin_unlock_irqrestore(&m
->lock
, flags
);
463 list_for_each_entry_safe(clone
, n
, &cl
, queuelist
) {
464 list_del_init(&clone
->queuelist
);
466 info
= dm_get_rq_mapinfo(clone
);
468 r
= map_io(m
, clone
, info
, 1);
470 clear_mapinfo(m
, info
);
471 dm_kill_unmapped_request(clone
, r
);
472 } else if (r
== DM_MAPIO_REMAPPED
)
473 dm_dispatch_request(clone
);
474 else if (r
== DM_MAPIO_REQUEUE
) {
475 clear_mapinfo(m
, info
);
476 dm_requeue_unmapped_request(clone
);
481 static void process_queued_ios(struct work_struct
*work
)
483 struct multipath
*m
=
484 container_of(work
, struct multipath
, process_queued_ios
);
485 struct pgpath
*pgpath
= NULL
;
486 unsigned must_queue
= 1;
489 spin_lock_irqsave(&m
->lock
, flags
);
491 if (!m
->current_pgpath
)
492 __choose_pgpath(m
, 0);
494 pgpath
= m
->current_pgpath
;
496 if ((pgpath
&& !m
->queue_io
) ||
497 (!pgpath
&& !m
->queue_if_no_path
))
500 if (m
->pg_init_required
&& !m
->pg_init_in_progress
&& pgpath
)
501 __pg_init_all_paths(m
);
503 spin_unlock_irqrestore(&m
->lock
, flags
);
505 dispatch_queued_ios(m
);
509 * An event is triggered whenever a path is taken out of use.
510 * Includes path failure and PG bypass.
512 static void trigger_event(struct work_struct
*work
)
514 struct multipath
*m
=
515 container_of(work
, struct multipath
, trigger_event
);
517 dm_table_event(m
->ti
->table
);
520 /*-----------------------------------------------------------------
521 * Constructor/argument parsing:
522 * <#multipath feature args> [<arg>]*
523 * <#hw_handler args> [hw_handler [<arg>]*]
525 * <initial priority group>
526 * [<selector> <#selector args> [<arg>]*
527 * <#paths> <#per-path selector args>
528 * [<path> [<arg>]* ]+ ]+
529 *---------------------------------------------------------------*/
530 static int parse_path_selector(struct dm_arg_set
*as
, struct priority_group
*pg
,
531 struct dm_target
*ti
)
534 struct path_selector_type
*pst
;
537 static struct dm_arg _args
[] = {
538 {0, 1024, "invalid number of path selector args"},
541 pst
= dm_get_path_selector(dm_shift_arg(as
));
543 ti
->error
= "unknown path selector type";
547 r
= dm_read_arg_group(_args
, as
, &ps_argc
, &ti
->error
);
549 dm_put_path_selector(pst
);
553 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
555 dm_put_path_selector(pst
);
556 ti
->error
= "path selector constructor failed";
561 dm_consume_args(as
, ps_argc
);
566 static struct pgpath
*parse_path(struct dm_arg_set
*as
, struct path_selector
*ps
,
567 struct dm_target
*ti
)
571 struct multipath
*m
= ti
->private;
572 struct request_queue
*q
= NULL
;
573 const char *attached_handler_name
;
575 /* we need at least a path arg */
577 ti
->error
= "no device given";
578 return ERR_PTR(-EINVAL
);
583 return ERR_PTR(-ENOMEM
);
585 r
= dm_get_device(ti
, dm_shift_arg(as
), dm_table_get_mode(ti
->table
),
588 ti
->error
= "error getting device";
592 if (m
->retain_attached_hw_handler
|| m
->hw_handler_name
)
593 q
= bdev_get_queue(p
->path
.dev
->bdev
);
595 if (m
->retain_attached_hw_handler
) {
596 attached_handler_name
= scsi_dh_attached_handler_name(q
, GFP_KERNEL
);
597 if (attached_handler_name
) {
599 * Reset hw_handler_name to match the attached handler
600 * and clear any hw_handler_params associated with the
603 * NB. This modifies the table line to show the actual
604 * handler instead of the original table passed in.
606 kfree(m
->hw_handler_name
);
607 m
->hw_handler_name
= attached_handler_name
;
609 kfree(m
->hw_handler_params
);
610 m
->hw_handler_params
= NULL
;
614 if (m
->hw_handler_name
) {
616 * Increments scsi_dh reference, even when using an
617 * already-attached handler.
619 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
622 * Already attached to different hw_handler:
623 * try to reattach with correct one.
626 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
630 ti
->error
= "error attaching hardware handler";
631 dm_put_device(ti
, p
->path
.dev
);
635 if (m
->hw_handler_params
) {
636 r
= scsi_dh_set_params(q
, m
->hw_handler_params
);
638 ti
->error
= "unable to set hardware "
639 "handler parameters";
641 dm_put_device(ti
, p
->path
.dev
);
647 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
649 dm_put_device(ti
, p
->path
.dev
);
660 static struct priority_group
*parse_priority_group(struct dm_arg_set
*as
,
663 static struct dm_arg _args
[] = {
664 {1, 1024, "invalid number of paths"},
665 {0, 1024, "invalid number of selector args"}
669 unsigned i
, nr_selector_args
, nr_args
;
670 struct priority_group
*pg
;
671 struct dm_target
*ti
= m
->ti
;
675 ti
->error
= "not enough priority group arguments";
676 return ERR_PTR(-EINVAL
);
679 pg
= alloc_priority_group();
681 ti
->error
= "couldn't allocate priority group";
682 return ERR_PTR(-ENOMEM
);
686 r
= parse_path_selector(as
, pg
, ti
);
693 r
= dm_read_arg(_args
, as
, &pg
->nr_pgpaths
, &ti
->error
);
697 r
= dm_read_arg(_args
+ 1, as
, &nr_selector_args
, &ti
->error
);
701 nr_args
= 1 + nr_selector_args
;
702 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
703 struct pgpath
*pgpath
;
704 struct dm_arg_set path_args
;
706 if (as
->argc
< nr_args
) {
707 ti
->error
= "not enough path parameters";
712 path_args
.argc
= nr_args
;
713 path_args
.argv
= as
->argv
;
715 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
716 if (IS_ERR(pgpath
)) {
722 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
723 dm_consume_args(as
, nr_args
);
729 free_priority_group(pg
, ti
);
733 static int parse_hw_handler(struct dm_arg_set
*as
, struct multipath
*m
)
737 struct dm_target
*ti
= m
->ti
;
739 static struct dm_arg _args
[] = {
740 {0, 1024, "invalid number of hardware handler args"},
743 if (dm_read_arg_group(_args
, as
, &hw_argc
, &ti
->error
))
749 m
->hw_handler_name
= kstrdup(dm_shift_arg(as
), GFP_KERNEL
);
750 if (!try_then_request_module(scsi_dh_handler_exist(m
->hw_handler_name
),
751 "scsi_dh_%s", m
->hw_handler_name
)) {
752 ti
->error
= "unknown hardware handler type";
761 for (i
= 0; i
<= hw_argc
- 2; i
++)
762 len
+= strlen(as
->argv
[i
]) + 1;
763 p
= m
->hw_handler_params
= kzalloc(len
, GFP_KERNEL
);
765 ti
->error
= "memory allocation failed";
769 j
= sprintf(p
, "%d", hw_argc
- 1);
770 for (i
= 0, p
+=j
+1; i
<= hw_argc
- 2; i
++, p
+=j
+1)
771 j
= sprintf(p
, "%s", as
->argv
[i
]);
773 dm_consume_args(as
, hw_argc
- 1);
777 kfree(m
->hw_handler_name
);
778 m
->hw_handler_name
= NULL
;
782 static int parse_features(struct dm_arg_set
*as
, struct multipath
*m
)
786 struct dm_target
*ti
= m
->ti
;
787 const char *arg_name
;
789 static struct dm_arg _args
[] = {
790 {0, 6, "invalid number of feature args"},
791 {1, 50, "pg_init_retries must be between 1 and 50"},
792 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
795 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
803 arg_name
= dm_shift_arg(as
);
806 if (!strcasecmp(arg_name
, "queue_if_no_path")) {
807 r
= queue_if_no_path(m
, 1, 0);
811 if (!strcasecmp(arg_name
, "retain_attached_hw_handler")) {
812 m
->retain_attached_hw_handler
= 1;
816 if (!strcasecmp(arg_name
, "pg_init_retries") &&
818 r
= dm_read_arg(_args
+ 1, as
, &m
->pg_init_retries
, &ti
->error
);
823 if (!strcasecmp(arg_name
, "pg_init_delay_msecs") &&
825 r
= dm_read_arg(_args
+ 2, as
, &m
->pg_init_delay_msecs
, &ti
->error
);
830 ti
->error
= "Unrecognised multipath feature request";
832 } while (argc
&& !r
);
837 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
840 /* target arguments */
841 static struct dm_arg _args
[] = {
842 {0, 1024, "invalid number of priority groups"},
843 {0, 1024, "invalid initial priority group number"},
848 struct dm_arg_set as
;
849 unsigned pg_count
= 0;
850 unsigned next_pg_num
;
855 m
= alloc_multipath(ti
);
857 ti
->error
= "can't allocate multipath";
861 r
= parse_features(&as
, m
);
865 r
= parse_hw_handler(&as
, m
);
869 r
= dm_read_arg(_args
, &as
, &m
->nr_priority_groups
, &ti
->error
);
873 r
= dm_read_arg(_args
+ 1, &as
, &next_pg_num
, &ti
->error
);
877 if ((!m
->nr_priority_groups
&& next_pg_num
) ||
878 (m
->nr_priority_groups
&& !next_pg_num
)) {
879 ti
->error
= "invalid initial priority group";
884 /* parse the priority groups */
886 struct priority_group
*pg
;
888 pg
= parse_priority_group(&as
, m
);
894 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
895 list_add_tail(&pg
->list
, &m
->priority_groups
);
897 pg
->pg_num
= pg_count
;
902 if (pg_count
!= m
->nr_priority_groups
) {
903 ti
->error
= "priority group count mismatch";
908 ti
->num_flush_requests
= 1;
909 ti
->num_discard_requests
= 1;
918 static void multipath_wait_for_pg_init_completion(struct multipath
*m
)
920 DECLARE_WAITQUEUE(wait
, current
);
923 add_wait_queue(&m
->pg_init_wait
, &wait
);
926 set_current_state(TASK_UNINTERRUPTIBLE
);
928 spin_lock_irqsave(&m
->lock
, flags
);
929 if (!m
->pg_init_in_progress
) {
930 spin_unlock_irqrestore(&m
->lock
, flags
);
933 spin_unlock_irqrestore(&m
->lock
, flags
);
937 set_current_state(TASK_RUNNING
);
939 remove_wait_queue(&m
->pg_init_wait
, &wait
);
942 static void flush_multipath_work(struct multipath
*m
)
944 flush_workqueue(kmpath_handlerd
);
945 multipath_wait_for_pg_init_completion(m
);
946 flush_workqueue(kmultipathd
);
947 flush_work_sync(&m
->trigger_event
);
950 static void multipath_dtr(struct dm_target
*ti
)
952 struct multipath
*m
= ti
->private;
954 flush_multipath_work(m
);
959 * Map cloned requests
961 static int multipath_map(struct dm_target
*ti
, struct request
*clone
,
962 union map_info
*map_context
)
965 struct multipath
*m
= (struct multipath
*) ti
->private;
967 if (set_mapinfo(m
, map_context
) < 0)
968 /* ENOMEM, requeue */
969 return DM_MAPIO_REQUEUE
;
971 clone
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
972 r
= map_io(m
, clone
, map_context
, 0);
973 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
974 clear_mapinfo(m
, map_context
);
980 * Take a path out of use.
982 static int fail_path(struct pgpath
*pgpath
)
985 struct multipath
*m
= pgpath
->pg
->m
;
987 spin_lock_irqsave(&m
->lock
, flags
);
989 if (!pgpath
->is_active
)
992 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
994 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
995 pgpath
->is_active
= 0;
996 pgpath
->fail_count
++;
1000 if (pgpath
== m
->current_pgpath
)
1001 m
->current_pgpath
= NULL
;
1003 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
1004 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
1006 schedule_work(&m
->trigger_event
);
1009 spin_unlock_irqrestore(&m
->lock
, flags
);
1015 * Reinstate a previously-failed path
1017 static int reinstate_path(struct pgpath
*pgpath
)
1020 unsigned long flags
;
1021 struct multipath
*m
= pgpath
->pg
->m
;
1023 spin_lock_irqsave(&m
->lock
, flags
);
1025 if (pgpath
->is_active
)
1028 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
1029 DMWARN("Reinstate path not supported by path selector %s",
1030 pgpath
->pg
->ps
.type
->name
);
1035 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
1039 pgpath
->is_active
= 1;
1041 if (!m
->nr_valid_paths
++ && m
->queue_size
) {
1042 m
->current_pgpath
= NULL
;
1043 queue_work(kmultipathd
, &m
->process_queued_ios
);
1044 } else if (m
->hw_handler_name
&& (m
->current_pg
== pgpath
->pg
)) {
1045 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
.work
))
1046 m
->pg_init_in_progress
++;
1049 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
1050 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
1052 schedule_work(&m
->trigger_event
);
1055 spin_unlock_irqrestore(&m
->lock
, flags
);
1061 * Fail or reinstate all paths that match the provided struct dm_dev.
1063 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
1067 struct pgpath
*pgpath
;
1068 struct priority_group
*pg
;
1070 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1071 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
1072 if (pgpath
->path
.dev
== dev
)
1081 * Temporarily try to avoid having to use the specified PG
1083 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
1086 unsigned long flags
;
1088 spin_lock_irqsave(&m
->lock
, flags
);
1090 pg
->bypassed
= bypassed
;
1091 m
->current_pgpath
= NULL
;
1092 m
->current_pg
= NULL
;
1094 spin_unlock_irqrestore(&m
->lock
, flags
);
1096 schedule_work(&m
->trigger_event
);
1100 * Switch to using the specified PG from the next I/O that gets mapped
1102 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
1104 struct priority_group
*pg
;
1106 unsigned long flags
;
1109 if (!pgstr
|| (sscanf(pgstr
, "%u%c", &pgnum
, &dummy
) != 1) || !pgnum
||
1110 (pgnum
> m
->nr_priority_groups
)) {
1111 DMWARN("invalid PG number supplied to switch_pg_num");
1115 spin_lock_irqsave(&m
->lock
, flags
);
1116 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1121 m
->current_pgpath
= NULL
;
1122 m
->current_pg
= NULL
;
1125 spin_unlock_irqrestore(&m
->lock
, flags
);
1127 schedule_work(&m
->trigger_event
);
1132 * Set/clear bypassed status of a PG.
1133 * PGs are numbered upwards from 1 in the order they were declared.
1135 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
1137 struct priority_group
*pg
;
1141 if (!pgstr
|| (sscanf(pgstr
, "%u%c", &pgnum
, &dummy
) != 1) || !pgnum
||
1142 (pgnum
> m
->nr_priority_groups
)) {
1143 DMWARN("invalid PG number supplied to bypass_pg");
1147 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1152 bypass_pg(m
, pg
, bypassed
);
1157 * Should we retry pg_init immediately?
1159 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1161 unsigned long flags
;
1162 int limit_reached
= 0;
1164 spin_lock_irqsave(&m
->lock
, flags
);
1166 if (m
->pg_init_count
<= m
->pg_init_retries
)
1167 m
->pg_init_required
= 1;
1171 spin_unlock_irqrestore(&m
->lock
, flags
);
1173 return limit_reached
;
1176 static void pg_init_done(void *data
, int errors
)
1178 struct pgpath
*pgpath
= data
;
1179 struct priority_group
*pg
= pgpath
->pg
;
1180 struct multipath
*m
= pg
->m
;
1181 unsigned long flags
;
1182 unsigned delay_retry
= 0;
1184 /* device or driver problems */
1189 if (!m
->hw_handler_name
) {
1193 DMERR("Could not failover the device: Handler scsi_dh_%s "
1194 "Error %d.", m
->hw_handler_name
, errors
);
1196 * Fail path for now, so we do not ping pong
1200 case SCSI_DH_DEV_TEMP_BUSY
:
1202 * Probably doing something like FW upgrade on the
1203 * controller so try the other pg.
1205 bypass_pg(m
, pg
, 1);
1208 /* Wait before retrying. */
1210 case SCSI_DH_IMM_RETRY
:
1211 case SCSI_DH_RES_TEMP_UNAVAIL
:
1212 if (pg_init_limit_reached(m
, pgpath
))
1218 * We probably do not want to fail the path for a device
1219 * error, but this is what the old dm did. In future
1220 * patches we can do more advanced handling.
1225 spin_lock_irqsave(&m
->lock
, flags
);
1227 if (pgpath
== m
->current_pgpath
) {
1228 DMERR("Could not failover device. Error %d.", errors
);
1229 m
->current_pgpath
= NULL
;
1230 m
->current_pg
= NULL
;
1232 } else if (!m
->pg_init_required
)
1235 if (--m
->pg_init_in_progress
)
1236 /* Activations of other paths are still on going */
1239 if (!m
->pg_init_required
)
1242 m
->pg_init_delay_retry
= delay_retry
;
1243 queue_work(kmultipathd
, &m
->process_queued_ios
);
1246 * Wake up any thread waiting to suspend.
1248 wake_up(&m
->pg_init_wait
);
1251 spin_unlock_irqrestore(&m
->lock
, flags
);
1254 static void activate_path(struct work_struct
*work
)
1256 struct pgpath
*pgpath
=
1257 container_of(work
, struct pgpath
, activate_path
.work
);
1259 scsi_dh_activate(bdev_get_queue(pgpath
->path
.dev
->bdev
),
1260 pg_init_done
, pgpath
);
1266 static int do_end_io(struct multipath
*m
, struct request
*clone
,
1267 int error
, struct dm_mpath_io
*mpio
)
1270 * We don't queue any clone request inside the multipath target
1271 * during end I/O handling, since those clone requests don't have
1272 * bio clones. If we queue them inside the multipath target,
1273 * we need to make bio clones, that requires memory allocation.
1274 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1275 * don't have bio clones.)
1276 * Instead of queueing the clone request here, we queue the original
1277 * request into dm core, which will remake a clone request and
1278 * clone bios for it and resubmit it later.
1280 int r
= DM_ENDIO_REQUEUE
;
1281 unsigned long flags
;
1283 if (!error
&& !clone
->errors
)
1284 return 0; /* I/O complete */
1286 if (error
== -EOPNOTSUPP
|| error
== -EREMOTEIO
|| error
== -EILSEQ
)
1290 fail_path(mpio
->pgpath
);
1292 spin_lock_irqsave(&m
->lock
, flags
);
1293 if (!m
->nr_valid_paths
) {
1294 if (!m
->queue_if_no_path
) {
1295 if (!__must_push_back(m
))
1298 if (error
== -EBADE
)
1302 spin_unlock_irqrestore(&m
->lock
, flags
);
1307 static int multipath_end_io(struct dm_target
*ti
, struct request
*clone
,
1308 int error
, union map_info
*map_context
)
1310 struct multipath
*m
= ti
->private;
1311 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1312 struct pgpath
*pgpath
= mpio
->pgpath
;
1313 struct path_selector
*ps
;
1318 r
= do_end_io(m
, clone
, error
, mpio
);
1320 ps
= &pgpath
->pg
->ps
;
1321 if (ps
->type
->end_io
)
1322 ps
->type
->end_io(ps
, &pgpath
->path
, mpio
->nr_bytes
);
1324 clear_mapinfo(m
, map_context
);
1330 * Suspend can't complete until all the I/O is processed so if
1331 * the last path fails we must error any remaining I/O.
1332 * Note that if the freeze_bdev fails while suspending, the
1333 * queue_if_no_path state is lost - userspace should reset it.
1335 static void multipath_presuspend(struct dm_target
*ti
)
1337 struct multipath
*m
= (struct multipath
*) ti
->private;
1339 queue_if_no_path(m
, 0, 1);
1342 static void multipath_postsuspend(struct dm_target
*ti
)
1344 struct multipath
*m
= ti
->private;
1346 mutex_lock(&m
->work_mutex
);
1347 flush_multipath_work(m
);
1348 mutex_unlock(&m
->work_mutex
);
1352 * Restore the queue_if_no_path setting.
1354 static void multipath_resume(struct dm_target
*ti
)
1356 struct multipath
*m
= (struct multipath
*) ti
->private;
1357 unsigned long flags
;
1359 spin_lock_irqsave(&m
->lock
, flags
);
1360 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1361 spin_unlock_irqrestore(&m
->lock
, flags
);
1365 * Info output has the following format:
1366 * num_multipath_feature_args [multipath_feature_args]*
1367 * num_handler_status_args [handler_status_args]*
1368 * num_groups init_group_number
1369 * [A|D|E num_ps_status_args [ps_status_args]*
1370 * num_paths num_selector_args
1371 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1373 * Table output has the following format (identical to the constructor string):
1374 * num_feature_args [features_args]*
1375 * num_handler_args hw_handler [hw_handler_args]*
1376 * num_groups init_group_number
1377 * [priority selector-name num_ps_args [ps_args]*
1378 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1380 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1381 char *result
, unsigned int maxlen
)
1384 unsigned long flags
;
1385 struct multipath
*m
= (struct multipath
*) ti
->private;
1386 struct priority_group
*pg
;
1391 spin_lock_irqsave(&m
->lock
, flags
);
1394 if (type
== STATUSTYPE_INFO
)
1395 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1397 DMEMIT("%u ", m
->queue_if_no_path
+
1398 (m
->pg_init_retries
> 0) * 2 +
1399 (m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
) * 2 +
1400 m
->retain_attached_hw_handler
);
1401 if (m
->queue_if_no_path
)
1402 DMEMIT("queue_if_no_path ");
1403 if (m
->pg_init_retries
)
1404 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1405 if (m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
)
1406 DMEMIT("pg_init_delay_msecs %u ", m
->pg_init_delay_msecs
);
1407 if (m
->retain_attached_hw_handler
)
1408 DMEMIT("retain_attached_hw_handler ");
1411 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1414 DMEMIT("1 %s ", m
->hw_handler_name
);
1416 DMEMIT("%u ", m
->nr_priority_groups
);
1419 pg_num
= m
->next_pg
->pg_num
;
1420 else if (m
->current_pg
)
1421 pg_num
= m
->current_pg
->pg_num
;
1423 pg_num
= (m
->nr_priority_groups
? 1 : 0);
1425 DMEMIT("%u ", pg_num
);
1428 case STATUSTYPE_INFO
:
1429 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1431 state
= 'D'; /* Disabled */
1432 else if (pg
== m
->current_pg
)
1433 state
= 'A'; /* Currently Active */
1435 state
= 'E'; /* Enabled */
1437 DMEMIT("%c ", state
);
1439 if (pg
->ps
.type
->status
)
1440 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1446 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1447 pg
->ps
.type
->info_args
);
1449 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1450 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1451 p
->is_active
? "A" : "F",
1453 if (pg
->ps
.type
->status
)
1454 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1455 &p
->path
, type
, result
+ sz
,
1461 case STATUSTYPE_TABLE
:
1462 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1463 DMEMIT("%s ", pg
->ps
.type
->name
);
1465 if (pg
->ps
.type
->status
)
1466 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1472 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1473 pg
->ps
.type
->table_args
);
1475 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1476 DMEMIT("%s ", p
->path
.dev
->name
);
1477 if (pg
->ps
.type
->status
)
1478 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1479 &p
->path
, type
, result
+ sz
,
1486 spin_unlock_irqrestore(&m
->lock
, flags
);
1491 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1495 struct multipath
*m
= (struct multipath
*) ti
->private;
1498 mutex_lock(&m
->work_mutex
);
1500 if (dm_suspended(ti
)) {
1506 if (!strcasecmp(argv
[0], "queue_if_no_path")) {
1507 r
= queue_if_no_path(m
, 1, 0);
1509 } else if (!strcasecmp(argv
[0], "fail_if_no_path")) {
1510 r
= queue_if_no_path(m
, 0, 0);
1516 DMWARN("Unrecognised multipath message received.");
1520 if (!strcasecmp(argv
[0], "disable_group")) {
1521 r
= bypass_pg_num(m
, argv
[1], 1);
1523 } else if (!strcasecmp(argv
[0], "enable_group")) {
1524 r
= bypass_pg_num(m
, argv
[1], 0);
1526 } else if (!strcasecmp(argv
[0], "switch_group")) {
1527 r
= switch_pg_num(m
, argv
[1]);
1529 } else if (!strcasecmp(argv
[0], "reinstate_path"))
1530 action
= reinstate_path
;
1531 else if (!strcasecmp(argv
[0], "fail_path"))
1534 DMWARN("Unrecognised multipath message received.");
1538 r
= dm_get_device(ti
, argv
[1], dm_table_get_mode(ti
->table
), &dev
);
1540 DMWARN("message: error getting device %s",
1545 r
= action_dev(m
, dev
, action
);
1547 dm_put_device(ti
, dev
);
1550 mutex_unlock(&m
->work_mutex
);
1554 static int multipath_ioctl(struct dm_target
*ti
, unsigned int cmd
,
1557 struct multipath
*m
= ti
->private;
1558 struct block_device
*bdev
;
1560 unsigned long flags
;
1568 spin_lock_irqsave(&m
->lock
, flags
);
1570 if (!m
->current_pgpath
)
1571 __choose_pgpath(m
, 0);
1573 if (m
->current_pgpath
) {
1574 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1575 mode
= m
->current_pgpath
->path
.dev
->mode
;
1583 spin_unlock_irqrestore(&m
->lock
, flags
);
1586 * Only pass ioctls through if the device sizes match exactly.
1588 if (!r
&& ti
->len
!= i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
)
1589 r
= scsi_verify_blk_ioctl(NULL
, cmd
);
1591 if (r
== -EAGAIN
&& !fatal_signal_pending(current
)) {
1592 queue_work(kmultipathd
, &m
->process_queued_ios
);
1597 return r
? : __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
1600 static int multipath_iterate_devices(struct dm_target
*ti
,
1601 iterate_devices_callout_fn fn
, void *data
)
1603 struct multipath
*m
= ti
->private;
1604 struct priority_group
*pg
;
1608 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1609 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1610 ret
= fn(ti
, p
->path
.dev
, ti
->begin
, ti
->len
, data
);
1620 static int __pgpath_busy(struct pgpath
*pgpath
)
1622 struct request_queue
*q
= bdev_get_queue(pgpath
->path
.dev
->bdev
);
1624 return dm_underlying_device_busy(q
);
1628 * We return "busy", only when we can map I/Os but underlying devices
1629 * are busy (so even if we map I/Os now, the I/Os will wait on
1630 * the underlying queue).
1631 * In other words, if we want to kill I/Os or queue them inside us
1632 * due to map unavailability, we don't return "busy". Otherwise,
1633 * dm core won't give us the I/Os and we can't do what we want.
1635 static int multipath_busy(struct dm_target
*ti
)
1637 int busy
= 0, has_active
= 0;
1638 struct multipath
*m
= ti
->private;
1639 struct priority_group
*pg
;
1640 struct pgpath
*pgpath
;
1641 unsigned long flags
;
1643 spin_lock_irqsave(&m
->lock
, flags
);
1645 /* Guess which priority_group will be used at next mapping time */
1646 if (unlikely(!m
->current_pgpath
&& m
->next_pg
))
1648 else if (likely(m
->current_pg
))
1652 * We don't know which pg will be used at next mapping time.
1653 * We don't call __choose_pgpath() here to avoid to trigger
1654 * pg_init just by busy checking.
1655 * So we don't know whether underlying devices we will be using
1656 * at next mapping time are busy or not. Just try mapping.
1661 * If there is one non-busy active path at least, the path selector
1662 * will be able to select it. So we consider such a pg as not busy.
1665 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
)
1666 if (pgpath
->is_active
) {
1669 if (!__pgpath_busy(pgpath
)) {
1677 * No active path in this pg, so this pg won't be used and
1678 * the current_pg will be changed at next mapping time.
1679 * We need to try mapping to determine it.
1684 spin_unlock_irqrestore(&m
->lock
, flags
);
1689 /*-----------------------------------------------------------------
1691 *---------------------------------------------------------------*/
1692 static struct target_type multipath_target
= {
1693 .name
= "multipath",
1694 .version
= {1, 5, 0},
1695 .module
= THIS_MODULE
,
1696 .ctr
= multipath_ctr
,
1697 .dtr
= multipath_dtr
,
1698 .map_rq
= multipath_map
,
1699 .rq_end_io
= multipath_end_io
,
1700 .presuspend
= multipath_presuspend
,
1701 .postsuspend
= multipath_postsuspend
,
1702 .resume
= multipath_resume
,
1703 .status
= multipath_status
,
1704 .message
= multipath_message
,
1705 .ioctl
= multipath_ioctl
,
1706 .iterate_devices
= multipath_iterate_devices
,
1707 .busy
= multipath_busy
,
1710 static int __init
dm_multipath_init(void)
1714 /* allocate a slab for the dm_ios */
1715 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1719 r
= dm_register_target(&multipath_target
);
1721 DMERR("register failed %d", r
);
1722 kmem_cache_destroy(_mpio_cache
);
1726 kmultipathd
= alloc_workqueue("kmpathd", WQ_MEM_RECLAIM
, 0);
1728 DMERR("failed to create workqueue kmpathd");
1729 dm_unregister_target(&multipath_target
);
1730 kmem_cache_destroy(_mpio_cache
);
1735 * A separate workqueue is used to handle the device handlers
1736 * to avoid overloading existing workqueue. Overloading the
1737 * old workqueue would also create a bottleneck in the
1738 * path of the storage hardware device activation.
1740 kmpath_handlerd
= alloc_ordered_workqueue("kmpath_handlerd",
1742 if (!kmpath_handlerd
) {
1743 DMERR("failed to create workqueue kmpath_handlerd");
1744 destroy_workqueue(kmultipathd
);
1745 dm_unregister_target(&multipath_target
);
1746 kmem_cache_destroy(_mpio_cache
);
1750 DMINFO("version %u.%u.%u loaded",
1751 multipath_target
.version
[0], multipath_target
.version
[1],
1752 multipath_target
.version
[2]);
1757 static void __exit
dm_multipath_exit(void)
1759 destroy_workqueue(kmpath_handlerd
);
1760 destroy_workqueue(kmultipathd
);
1762 dm_unregister_target(&multipath_target
);
1763 kmem_cache_destroy(_mpio_cache
);
1766 module_init(dm_multipath_init
);
1767 module_exit(dm_multipath_exit
);
1769 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1770 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1771 MODULE_LICENSE("GPL");