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 deactivate_path
;
37 struct work_struct activate_path
;
40 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
43 * Paths are grouped into Priority Groups and numbered from 1 upwards.
44 * Each has a path selector which controls which path gets used.
46 struct priority_group
{
47 struct list_head list
;
49 struct multipath
*m
; /* Owning multipath instance */
50 struct path_selector ps
;
52 unsigned pg_num
; /* Reference number */
53 unsigned bypassed
; /* Temporarily bypass this PG? */
55 unsigned nr_pgpaths
; /* Number of paths in PG */
56 struct list_head pgpaths
;
59 /* Multipath context */
61 struct list_head list
;
66 const char *hw_handler_name
;
67 char *hw_handler_params
;
68 unsigned nr_priority_groups
;
69 struct list_head priority_groups
;
70 unsigned pg_init_required
; /* pg_init needs calling? */
71 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
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
;
99 unsigned suspended
; /* Don't create new I/O internally when set. */
103 * Context information attached to each bio we process.
106 struct pgpath
*pgpath
;
110 typedef int (*action_fn
) (struct pgpath
*pgpath
);
112 #define MIN_IOS 256 /* Mempool size */
114 static struct kmem_cache
*_mpio_cache
;
116 static struct workqueue_struct
*kmultipathd
, *kmpath_handlerd
;
117 static void process_queued_ios(struct work_struct
*work
);
118 static void trigger_event(struct work_struct
*work
);
119 static void activate_path(struct work_struct
*work
);
120 static void deactivate_path(struct work_struct
*work
);
123 /*-----------------------------------------------
124 * Allocation routines
125 *-----------------------------------------------*/
127 static struct pgpath
*alloc_pgpath(void)
129 struct pgpath
*pgpath
= kzalloc(sizeof(*pgpath
), GFP_KERNEL
);
132 pgpath
->is_active
= 1;
133 INIT_WORK(&pgpath
->deactivate_path
, deactivate_path
);
134 INIT_WORK(&pgpath
->activate_path
, activate_path
);
140 static void free_pgpath(struct pgpath
*pgpath
)
145 static void deactivate_path(struct work_struct
*work
)
147 struct pgpath
*pgpath
=
148 container_of(work
, struct pgpath
, deactivate_path
);
150 blk_abort_queue(pgpath
->path
.dev
->bdev
->bd_disk
->queue
);
153 static struct priority_group
*alloc_priority_group(void)
155 struct priority_group
*pg
;
157 pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
160 INIT_LIST_HEAD(&pg
->pgpaths
);
165 static void free_pgpaths(struct list_head
*pgpaths
, struct dm_target
*ti
)
167 struct pgpath
*pgpath
, *tmp
;
168 struct multipath
*m
= ti
->private;
170 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
171 list_del(&pgpath
->list
);
172 if (m
->hw_handler_name
)
173 scsi_dh_detach(bdev_get_queue(pgpath
->path
.dev
->bdev
));
174 dm_put_device(ti
, pgpath
->path
.dev
);
179 static void free_priority_group(struct priority_group
*pg
,
180 struct dm_target
*ti
)
182 struct path_selector
*ps
= &pg
->ps
;
185 ps
->type
->destroy(ps
);
186 dm_put_path_selector(ps
->type
);
189 free_pgpaths(&pg
->pgpaths
, ti
);
193 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
197 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
199 INIT_LIST_HEAD(&m
->priority_groups
);
200 INIT_LIST_HEAD(&m
->queued_ios
);
201 spin_lock_init(&m
->lock
);
203 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
204 INIT_WORK(&m
->trigger_event
, trigger_event
);
205 mutex_init(&m
->work_mutex
);
206 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
218 static void free_multipath(struct multipath
*m
)
220 struct priority_group
*pg
, *tmp
;
222 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
224 free_priority_group(pg
, m
->ti
);
227 kfree(m
->hw_handler_name
);
228 kfree(m
->hw_handler_params
);
229 mempool_destroy(m
->mpio_pool
);
234 /*-----------------------------------------------
236 *-----------------------------------------------*/
238 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
240 m
->current_pg
= pgpath
->pg
;
242 /* Must we initialise the PG first, and queue I/O till it's ready? */
243 if (m
->hw_handler_name
) {
244 m
->pg_init_required
= 1;
247 m
->pg_init_required
= 0;
251 m
->pg_init_count
= 0;
254 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
,
257 struct dm_path
*path
;
259 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
, nr_bytes
);
263 m
->current_pgpath
= path_to_pgpath(path
);
265 if (m
->current_pg
!= pg
)
266 __switch_pg(m
, m
->current_pgpath
);
271 static void __choose_pgpath(struct multipath
*m
, size_t nr_bytes
)
273 struct priority_group
*pg
;
274 unsigned bypassed
= 1;
276 if (!m
->nr_valid_paths
)
279 /* Were we instructed to switch PG? */
283 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
287 /* Don't change PG until it has no remaining paths */
288 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
, nr_bytes
))
292 * Loop through priority groups until we find a valid path.
293 * First time we skip PGs marked 'bypassed'.
294 * Second time we only try the ones we skipped.
297 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
298 if (pg
->bypassed
== bypassed
)
300 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
303 } while (bypassed
--);
306 m
->current_pgpath
= NULL
;
307 m
->current_pg
= NULL
;
311 * Check whether bios must be queued in the device-mapper core rather
312 * than here in the target.
314 * m->lock must be held on entry.
316 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
317 * same value then we are not between multipath_presuspend()
318 * and multipath_resume() calls and we have no need to check
319 * for the DMF_NOFLUSH_SUSPENDING flag.
321 static int __must_push_back(struct multipath
*m
)
323 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
324 dm_noflush_suspending(m
->ti
));
327 static int map_io(struct multipath
*m
, struct request
*clone
,
328 struct dm_mpath_io
*mpio
, unsigned was_queued
)
330 int r
= DM_MAPIO_REMAPPED
;
331 size_t nr_bytes
= blk_rq_bytes(clone
);
333 struct pgpath
*pgpath
;
334 struct block_device
*bdev
;
336 spin_lock_irqsave(&m
->lock
, flags
);
338 /* Do we need to select a new pgpath? */
339 if (!m
->current_pgpath
||
340 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
341 __choose_pgpath(m
, nr_bytes
);
343 pgpath
= m
->current_pgpath
;
348 if ((pgpath
&& m
->queue_io
) ||
349 (!pgpath
&& m
->queue_if_no_path
)) {
350 /* Queue for the daemon to resubmit */
351 list_add_tail(&clone
->queuelist
, &m
->queued_ios
);
353 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
355 queue_work(kmultipathd
, &m
->process_queued_ios
);
357 r
= DM_MAPIO_SUBMITTED
;
359 bdev
= pgpath
->path
.dev
->bdev
;
360 clone
->q
= bdev_get_queue(bdev
);
361 clone
->rq_disk
= bdev
->bd_disk
;
362 } else if (__must_push_back(m
))
363 r
= DM_MAPIO_REQUEUE
;
365 r
= -EIO
; /* Failed */
367 mpio
->pgpath
= pgpath
;
368 mpio
->nr_bytes
= nr_bytes
;
370 if (r
== DM_MAPIO_REMAPPED
&& pgpath
->pg
->ps
.type
->start_io
)
371 pgpath
->pg
->ps
.type
->start_io(&pgpath
->pg
->ps
, &pgpath
->path
,
374 spin_unlock_irqrestore(&m
->lock
, flags
);
380 * If we run out of usable paths, should we queue I/O or error it?
382 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
383 unsigned save_old_value
)
387 spin_lock_irqsave(&m
->lock
, flags
);
390 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
392 m
->saved_queue_if_no_path
= queue_if_no_path
;
393 m
->queue_if_no_path
= queue_if_no_path
;
394 if (!m
->queue_if_no_path
&& m
->queue_size
)
395 queue_work(kmultipathd
, &m
->process_queued_ios
);
397 spin_unlock_irqrestore(&m
->lock
, flags
);
402 /*-----------------------------------------------------------------
403 * The multipath daemon is responsible for resubmitting queued ios.
404 *---------------------------------------------------------------*/
406 static void dispatch_queued_ios(struct multipath
*m
)
410 struct dm_mpath_io
*mpio
;
411 union map_info
*info
;
412 struct request
*clone
, *n
;
415 spin_lock_irqsave(&m
->lock
, flags
);
416 list_splice_init(&m
->queued_ios
, &cl
);
417 spin_unlock_irqrestore(&m
->lock
, flags
);
419 list_for_each_entry_safe(clone
, n
, &cl
, queuelist
) {
420 list_del_init(&clone
->queuelist
);
422 info
= dm_get_rq_mapinfo(clone
);
425 r
= map_io(m
, clone
, mpio
, 1);
427 mempool_free(mpio
, m
->mpio_pool
);
428 dm_kill_unmapped_request(clone
, r
);
429 } else if (r
== DM_MAPIO_REMAPPED
)
430 dm_dispatch_request(clone
);
431 else if (r
== DM_MAPIO_REQUEUE
) {
432 mempool_free(mpio
, m
->mpio_pool
);
433 dm_requeue_unmapped_request(clone
);
438 static void process_queued_ios(struct work_struct
*work
)
440 struct multipath
*m
=
441 container_of(work
, struct multipath
, process_queued_ios
);
442 struct pgpath
*pgpath
= NULL
, *tmp
;
443 unsigned must_queue
= 1;
446 spin_lock_irqsave(&m
->lock
, flags
);
451 if (!m
->current_pgpath
)
452 __choose_pgpath(m
, 0);
454 pgpath
= m
->current_pgpath
;
456 if ((pgpath
&& !m
->queue_io
) ||
457 (!pgpath
&& !m
->queue_if_no_path
))
460 if (m
->pg_init_required
&& !m
->pg_init_in_progress
&& pgpath
) {
462 m
->pg_init_required
= 0;
463 list_for_each_entry(tmp
, &pgpath
->pg
->pgpaths
, list
) {
464 if (queue_work(kmpath_handlerd
, &tmp
->activate_path
))
465 m
->pg_init_in_progress
++;
469 spin_unlock_irqrestore(&m
->lock
, flags
);
471 dispatch_queued_ios(m
);
475 * An event is triggered whenever a path is taken out of use.
476 * Includes path failure and PG bypass.
478 static void trigger_event(struct work_struct
*work
)
480 struct multipath
*m
=
481 container_of(work
, struct multipath
, trigger_event
);
483 dm_table_event(m
->ti
->table
);
486 /*-----------------------------------------------------------------
487 * Constructor/argument parsing:
488 * <#multipath feature args> [<arg>]*
489 * <#hw_handler args> [hw_handler [<arg>]*]
491 * <initial priority group>
492 * [<selector> <#selector args> [<arg>]*
493 * <#paths> <#per-path selector args>
494 * [<path> [<arg>]* ]+ ]+
495 *---------------------------------------------------------------*/
502 static int read_param(struct param
*param
, char *str
, unsigned *v
, char **error
)
505 (sscanf(str
, "%u", v
) != 1) ||
508 *error
= param
->error
;
520 static char *shift(struct arg_set
*as
)
534 static void consume(struct arg_set
*as
, unsigned n
)
536 BUG_ON (as
->argc
< n
);
541 static int parse_path_selector(struct arg_set
*as
, struct priority_group
*pg
,
542 struct dm_target
*ti
)
545 struct path_selector_type
*pst
;
548 static struct param _params
[] = {
549 {0, 1024, "invalid number of path selector args"},
552 pst
= dm_get_path_selector(shift(as
));
554 ti
->error
= "unknown path selector type";
558 r
= read_param(_params
, shift(as
), &ps_argc
, &ti
->error
);
560 dm_put_path_selector(pst
);
564 if (ps_argc
> as
->argc
) {
565 dm_put_path_selector(pst
);
566 ti
->error
= "not enough arguments for path selector";
570 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
572 dm_put_path_selector(pst
);
573 ti
->error
= "path selector constructor failed";
578 consume(as
, ps_argc
);
583 static struct pgpath
*parse_path(struct arg_set
*as
, struct path_selector
*ps
,
584 struct dm_target
*ti
)
588 struct multipath
*m
= ti
->private;
590 /* we need at least a path arg */
592 ti
->error
= "no device given";
593 return ERR_PTR(-EINVAL
);
598 return ERR_PTR(-ENOMEM
);
600 r
= dm_get_device(ti
, shift(as
), ti
->begin
, ti
->len
,
601 dm_table_get_mode(ti
->table
), &p
->path
.dev
);
603 ti
->error
= "error getting device";
607 if (m
->hw_handler_name
) {
608 struct request_queue
*q
= bdev_get_queue(p
->path
.dev
->bdev
);
610 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
613 * Already attached to different hw_handler,
614 * try to reattach with correct one.
617 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
621 ti
->error
= "error attaching hardware handler";
622 dm_put_device(ti
, p
->path
.dev
);
626 if (m
->hw_handler_params
) {
627 r
= scsi_dh_set_params(q
, m
->hw_handler_params
);
629 ti
->error
= "unable to set hardware "
630 "handler parameters";
632 dm_put_device(ti
, p
->path
.dev
);
638 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
640 dm_put_device(ti
, p
->path
.dev
);
651 static struct priority_group
*parse_priority_group(struct arg_set
*as
,
654 static struct param _params
[] = {
655 {1, 1024, "invalid number of paths"},
656 {0, 1024, "invalid number of selector args"}
660 unsigned i
, nr_selector_args
, nr_params
;
661 struct priority_group
*pg
;
662 struct dm_target
*ti
= m
->ti
;
666 ti
->error
= "not enough priority group arguments";
667 return ERR_PTR(-EINVAL
);
670 pg
= alloc_priority_group();
672 ti
->error
= "couldn't allocate priority group";
673 return ERR_PTR(-ENOMEM
);
677 r
= parse_path_selector(as
, pg
, ti
);
684 r
= read_param(_params
, shift(as
), &pg
->nr_pgpaths
, &ti
->error
);
688 r
= read_param(_params
+ 1, shift(as
), &nr_selector_args
, &ti
->error
);
692 nr_params
= 1 + nr_selector_args
;
693 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
694 struct pgpath
*pgpath
;
695 struct arg_set path_args
;
697 if (as
->argc
< nr_params
) {
698 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;
893 static void flush_multipath_work(void)
895 flush_workqueue(kmpath_handlerd
);
896 flush_workqueue(kmultipathd
);
897 flush_scheduled_work();
900 static void multipath_dtr(struct dm_target
*ti
)
902 struct multipath
*m
= ti
->private;
904 flush_multipath_work();
909 * Map cloned requests
911 static int multipath_map(struct dm_target
*ti
, struct request
*clone
,
912 union map_info
*map_context
)
915 struct dm_mpath_io
*mpio
;
916 struct multipath
*m
= (struct multipath
*) ti
->private;
918 mpio
= mempool_alloc(m
->mpio_pool
, GFP_ATOMIC
);
920 /* ENOMEM, requeue */
921 return DM_MAPIO_REQUEUE
;
922 memset(mpio
, 0, sizeof(*mpio
));
924 map_context
->ptr
= mpio
;
925 clone
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
926 r
= map_io(m
, clone
, mpio
, 0);
927 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
928 mempool_free(mpio
, m
->mpio_pool
);
934 * Take a path out of use.
936 static int fail_path(struct pgpath
*pgpath
)
939 struct multipath
*m
= pgpath
->pg
->m
;
941 spin_lock_irqsave(&m
->lock
, flags
);
943 if (!pgpath
->is_active
)
946 DMWARN("Failing path %s.", pgpath
->path
.dev
->name
);
948 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
949 pgpath
->is_active
= 0;
950 pgpath
->fail_count
++;
954 if (pgpath
== m
->current_pgpath
)
955 m
->current_pgpath
= NULL
;
957 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
958 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
960 schedule_work(&m
->trigger_event
);
961 queue_work(kmultipathd
, &pgpath
->deactivate_path
);
964 spin_unlock_irqrestore(&m
->lock
, flags
);
970 * Reinstate a previously-failed path
972 static int reinstate_path(struct pgpath
*pgpath
)
976 struct multipath
*m
= pgpath
->pg
->m
;
978 spin_lock_irqsave(&m
->lock
, flags
);
980 if (pgpath
->is_active
)
983 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
984 DMWARN("Reinstate path not supported by path selector %s",
985 pgpath
->pg
->ps
.type
->name
);
990 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
994 pgpath
->is_active
= 1;
996 if (!m
->nr_valid_paths
++ && m
->queue_size
) {
997 m
->current_pgpath
= NULL
;
998 queue_work(kmultipathd
, &m
->process_queued_ios
);
999 } else if (m
->hw_handler_name
&& (m
->current_pg
== pgpath
->pg
)) {
1000 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
))
1001 m
->pg_init_in_progress
++;
1004 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
1005 pgpath
->path
.dev
->name
, m
->nr_valid_paths
);
1007 schedule_work(&m
->trigger_event
);
1010 spin_unlock_irqrestore(&m
->lock
, flags
);
1016 * Fail or reinstate all paths that match the provided struct dm_dev.
1018 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
1022 struct pgpath
*pgpath
;
1023 struct priority_group
*pg
;
1025 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1026 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
1027 if (pgpath
->path
.dev
== dev
)
1036 * Temporarily try to avoid having to use the specified PG
1038 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
1041 unsigned long flags
;
1043 spin_lock_irqsave(&m
->lock
, flags
);
1045 pg
->bypassed
= bypassed
;
1046 m
->current_pgpath
= NULL
;
1047 m
->current_pg
= NULL
;
1049 spin_unlock_irqrestore(&m
->lock
, flags
);
1051 schedule_work(&m
->trigger_event
);
1055 * Switch to using the specified PG from the next I/O that gets mapped
1057 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
1059 struct priority_group
*pg
;
1061 unsigned long flags
;
1063 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1064 (pgnum
> m
->nr_priority_groups
)) {
1065 DMWARN("invalid PG number supplied to switch_pg_num");
1069 spin_lock_irqsave(&m
->lock
, flags
);
1070 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1075 m
->current_pgpath
= NULL
;
1076 m
->current_pg
= NULL
;
1079 spin_unlock_irqrestore(&m
->lock
, flags
);
1081 schedule_work(&m
->trigger_event
);
1086 * Set/clear bypassed status of a PG.
1087 * PGs are numbered upwards from 1 in the order they were declared.
1089 static int bypass_pg_num(struct multipath
*m
, const char *pgstr
, int bypassed
)
1091 struct priority_group
*pg
;
1094 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1095 (pgnum
> m
->nr_priority_groups
)) {
1096 DMWARN("invalid PG number supplied to bypass_pg");
1100 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1105 bypass_pg(m
, pg
, bypassed
);
1110 * Should we retry pg_init immediately?
1112 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1114 unsigned long flags
;
1115 int limit_reached
= 0;
1117 spin_lock_irqsave(&m
->lock
, flags
);
1119 if (m
->pg_init_count
<= m
->pg_init_retries
)
1120 m
->pg_init_required
= 1;
1124 spin_unlock_irqrestore(&m
->lock
, flags
);
1126 return limit_reached
;
1129 static void pg_init_done(void *data
, int errors
)
1131 struct dm_path
*path
= data
;
1132 struct pgpath
*pgpath
= path_to_pgpath(path
);
1133 struct priority_group
*pg
= pgpath
->pg
;
1134 struct multipath
*m
= pg
->m
;
1135 unsigned long flags
;
1137 /* device or driver problems */
1142 if (!m
->hw_handler_name
) {
1146 DMERR("Cannot failover device because scsi_dh_%s was not "
1147 "loaded.", m
->hw_handler_name
);
1149 * Fail path for now, so we do not ping pong
1153 case SCSI_DH_DEV_TEMP_BUSY
:
1155 * Probably doing something like FW upgrade on the
1156 * controller so try the other pg.
1158 bypass_pg(m
, pg
, 1);
1160 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1162 case SCSI_DH_IMM_RETRY
:
1163 case SCSI_DH_RES_TEMP_UNAVAIL
:
1164 if (pg_init_limit_reached(m
, pgpath
))
1170 * We probably do not want to fail the path for a device
1171 * error, but this is what the old dm did. In future
1172 * patches we can do more advanced handling.
1177 spin_lock_irqsave(&m
->lock
, flags
);
1179 if (pgpath
== m
->current_pgpath
) {
1180 DMERR("Could not failover device. Error %d.", errors
);
1181 m
->current_pgpath
= NULL
;
1182 m
->current_pg
= NULL
;
1184 } else if (!m
->pg_init_required
) {
1189 m
->pg_init_in_progress
--;
1190 if (!m
->pg_init_in_progress
)
1191 queue_work(kmultipathd
, &m
->process_queued_ios
);
1192 spin_unlock_irqrestore(&m
->lock
, flags
);
1195 static void activate_path(struct work_struct
*work
)
1197 struct pgpath
*pgpath
=
1198 container_of(work
, struct pgpath
, activate_path
);
1200 scsi_dh_activate(bdev_get_queue(pgpath
->path
.dev
->bdev
),
1201 pg_init_done
, &pgpath
->path
);
1207 static int do_end_io(struct multipath
*m
, struct request
*clone
,
1208 int error
, struct dm_mpath_io
*mpio
)
1211 * We don't queue any clone request inside the multipath target
1212 * during end I/O handling, since those clone requests don't have
1213 * bio clones. If we queue them inside the multipath target,
1214 * we need to make bio clones, that requires memory allocation.
1215 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1216 * don't have bio clones.)
1217 * Instead of queueing the clone request here, we queue the original
1218 * request into dm core, which will remake a clone request and
1219 * clone bios for it and resubmit it later.
1221 int r
= DM_ENDIO_REQUEUE
;
1222 unsigned long flags
;
1224 if (!error
&& !clone
->errors
)
1225 return 0; /* I/O complete */
1227 if (error
== -EOPNOTSUPP
)
1231 fail_path(mpio
->pgpath
);
1233 spin_lock_irqsave(&m
->lock
, flags
);
1234 if (!m
->nr_valid_paths
&& !m
->queue_if_no_path
&& !__must_push_back(m
))
1236 spin_unlock_irqrestore(&m
->lock
, flags
);
1241 static int multipath_end_io(struct dm_target
*ti
, struct request
*clone
,
1242 int error
, union map_info
*map_context
)
1244 struct multipath
*m
= ti
->private;
1245 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1246 struct pgpath
*pgpath
= mpio
->pgpath
;
1247 struct path_selector
*ps
;
1250 r
= do_end_io(m
, clone
, error
, mpio
);
1252 ps
= &pgpath
->pg
->ps
;
1253 if (ps
->type
->end_io
)
1254 ps
->type
->end_io(ps
, &pgpath
->path
, mpio
->nr_bytes
);
1256 mempool_free(mpio
, m
->mpio_pool
);
1262 * Suspend can't complete until all the I/O is processed so if
1263 * the last path fails we must error any remaining I/O.
1264 * Note that if the freeze_bdev fails while suspending, the
1265 * queue_if_no_path state is lost - userspace should reset it.
1267 static void multipath_presuspend(struct dm_target
*ti
)
1269 struct multipath
*m
= (struct multipath
*) ti
->private;
1271 queue_if_no_path(m
, 0, 1);
1274 static void multipath_postsuspend(struct dm_target
*ti
)
1276 struct multipath
*m
= ti
->private;
1278 mutex_lock(&m
->work_mutex
);
1280 flush_multipath_work();
1281 mutex_unlock(&m
->work_mutex
);
1285 * Restore the queue_if_no_path setting.
1287 static void multipath_resume(struct dm_target
*ti
)
1289 struct multipath
*m
= (struct multipath
*) ti
->private;
1290 unsigned long flags
;
1292 mutex_lock(&m
->work_mutex
);
1294 mutex_unlock(&m
->work_mutex
);
1296 spin_lock_irqsave(&m
->lock
, flags
);
1297 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1298 spin_unlock_irqrestore(&m
->lock
, flags
);
1302 * Info output has the following format:
1303 * num_multipath_feature_args [multipath_feature_args]*
1304 * num_handler_status_args [handler_status_args]*
1305 * num_groups init_group_number
1306 * [A|D|E num_ps_status_args [ps_status_args]*
1307 * num_paths num_selector_args
1308 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1310 * Table output has the following format (identical to the constructor string):
1311 * num_feature_args [features_args]*
1312 * num_handler_args hw_handler [hw_handler_args]*
1313 * num_groups init_group_number
1314 * [priority selector-name num_ps_args [ps_args]*
1315 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1317 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1318 char *result
, unsigned int maxlen
)
1321 unsigned long flags
;
1322 struct multipath
*m
= (struct multipath
*) ti
->private;
1323 struct priority_group
*pg
;
1328 spin_lock_irqsave(&m
->lock
, flags
);
1331 if (type
== STATUSTYPE_INFO
)
1332 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1334 DMEMIT("%u ", m
->queue_if_no_path
+
1335 (m
->pg_init_retries
> 0) * 2);
1336 if (m
->queue_if_no_path
)
1337 DMEMIT("queue_if_no_path ");
1338 if (m
->pg_init_retries
)
1339 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1342 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1345 DMEMIT("1 %s ", m
->hw_handler_name
);
1347 DMEMIT("%u ", m
->nr_priority_groups
);
1350 pg_num
= m
->next_pg
->pg_num
;
1351 else if (m
->current_pg
)
1352 pg_num
= m
->current_pg
->pg_num
;
1356 DMEMIT("%u ", pg_num
);
1359 case STATUSTYPE_INFO
:
1360 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1362 state
= 'D'; /* Disabled */
1363 else if (pg
== m
->current_pg
)
1364 state
= 'A'; /* Currently Active */
1366 state
= 'E'; /* Enabled */
1368 DMEMIT("%c ", state
);
1370 if (pg
->ps
.type
->status
)
1371 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1377 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1378 pg
->ps
.type
->info_args
);
1380 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1381 DMEMIT("%s %s %u ", p
->path
.dev
->name
,
1382 p
->is_active
? "A" : "F",
1384 if (pg
->ps
.type
->status
)
1385 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1386 &p
->path
, type
, result
+ sz
,
1392 case STATUSTYPE_TABLE
:
1393 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1394 DMEMIT("%s ", pg
->ps
.type
->name
);
1396 if (pg
->ps
.type
->status
)
1397 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1403 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1404 pg
->ps
.type
->table_args
);
1406 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1407 DMEMIT("%s ", p
->path
.dev
->name
);
1408 if (pg
->ps
.type
->status
)
1409 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1410 &p
->path
, type
, result
+ sz
,
1417 spin_unlock_irqrestore(&m
->lock
, flags
);
1422 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1426 struct multipath
*m
= (struct multipath
*) ti
->private;
1429 mutex_lock(&m
->work_mutex
);
1436 if (dm_suspended(ti
)) {
1442 if (!strnicmp(argv
[0], MESG_STR("queue_if_no_path"))) {
1443 r
= queue_if_no_path(m
, 1, 0);
1445 } else if (!strnicmp(argv
[0], MESG_STR("fail_if_no_path"))) {
1446 r
= queue_if_no_path(m
, 0, 0);
1452 DMWARN("Unrecognised multipath message received.");
1456 if (!strnicmp(argv
[0], MESG_STR("disable_group"))) {
1457 r
= bypass_pg_num(m
, argv
[1], 1);
1459 } else if (!strnicmp(argv
[0], MESG_STR("enable_group"))) {
1460 r
= bypass_pg_num(m
, argv
[1], 0);
1462 } else if (!strnicmp(argv
[0], MESG_STR("switch_group"))) {
1463 r
= switch_pg_num(m
, argv
[1]);
1465 } else if (!strnicmp(argv
[0], MESG_STR("reinstate_path")))
1466 action
= reinstate_path
;
1467 else if (!strnicmp(argv
[0], MESG_STR("fail_path")))
1470 DMWARN("Unrecognised multipath message received.");
1474 r
= dm_get_device(ti
, argv
[1], ti
->begin
, ti
->len
,
1475 dm_table_get_mode(ti
->table
), &dev
);
1477 DMWARN("message: error getting device %s",
1482 r
= action_dev(m
, dev
, action
);
1484 dm_put_device(ti
, dev
);
1487 mutex_unlock(&m
->work_mutex
);
1491 static int multipath_ioctl(struct dm_target
*ti
, unsigned int cmd
,
1494 struct multipath
*m
= (struct multipath
*) ti
->private;
1495 struct block_device
*bdev
= NULL
;
1497 unsigned long flags
;
1500 spin_lock_irqsave(&m
->lock
, flags
);
1502 if (!m
->current_pgpath
)
1503 __choose_pgpath(m
, 0);
1505 if (m
->current_pgpath
) {
1506 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1507 mode
= m
->current_pgpath
->path
.dev
->mode
;
1515 spin_unlock_irqrestore(&m
->lock
, flags
);
1517 return r
? : __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
1520 static int multipath_iterate_devices(struct dm_target
*ti
,
1521 iterate_devices_callout_fn fn
, void *data
)
1523 struct multipath
*m
= ti
->private;
1524 struct priority_group
*pg
;
1528 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1529 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1530 ret
= fn(ti
, p
->path
.dev
, ti
->begin
, ti
->len
, data
);
1540 static int __pgpath_busy(struct pgpath
*pgpath
)
1542 struct request_queue
*q
= bdev_get_queue(pgpath
->path
.dev
->bdev
);
1544 return dm_underlying_device_busy(q
);
1548 * We return "busy", only when we can map I/Os but underlying devices
1549 * are busy (so even if we map I/Os now, the I/Os will wait on
1550 * the underlying queue).
1551 * In other words, if we want to kill I/Os or queue them inside us
1552 * due to map unavailability, we don't return "busy". Otherwise,
1553 * dm core won't give us the I/Os and we can't do what we want.
1555 static int multipath_busy(struct dm_target
*ti
)
1557 int busy
= 0, has_active
= 0;
1558 struct multipath
*m
= ti
->private;
1559 struct priority_group
*pg
;
1560 struct pgpath
*pgpath
;
1561 unsigned long flags
;
1563 spin_lock_irqsave(&m
->lock
, flags
);
1565 /* Guess which priority_group will be used at next mapping time */
1566 if (unlikely(!m
->current_pgpath
&& m
->next_pg
))
1568 else if (likely(m
->current_pg
))
1572 * We don't know which pg will be used at next mapping time.
1573 * We don't call __choose_pgpath() here to avoid to trigger
1574 * pg_init just by busy checking.
1575 * So we don't know whether underlying devices we will be using
1576 * at next mapping time are busy or not. Just try mapping.
1581 * If there is one non-busy active path at least, the path selector
1582 * will be able to select it. So we consider such a pg as not busy.
1585 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
)
1586 if (pgpath
->is_active
) {
1589 if (!__pgpath_busy(pgpath
)) {
1597 * No active path in this pg, so this pg won't be used and
1598 * the current_pg will be changed at next mapping time.
1599 * We need to try mapping to determine it.
1604 spin_unlock_irqrestore(&m
->lock
, flags
);
1609 /*-----------------------------------------------------------------
1611 *---------------------------------------------------------------*/
1612 static struct target_type multipath_target
= {
1613 .name
= "multipath",
1614 .version
= {1, 1, 1},
1615 .module
= THIS_MODULE
,
1616 .ctr
= multipath_ctr
,
1617 .dtr
= multipath_dtr
,
1618 .map_rq
= multipath_map
,
1619 .rq_end_io
= multipath_end_io
,
1620 .presuspend
= multipath_presuspend
,
1621 .postsuspend
= multipath_postsuspend
,
1622 .resume
= multipath_resume
,
1623 .status
= multipath_status
,
1624 .message
= multipath_message
,
1625 .ioctl
= multipath_ioctl
,
1626 .iterate_devices
= multipath_iterate_devices
,
1627 .busy
= multipath_busy
,
1630 static int __init
dm_multipath_init(void)
1634 /* allocate a slab for the dm_ios */
1635 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1639 r
= dm_register_target(&multipath_target
);
1641 DMERR("register failed %d", r
);
1642 kmem_cache_destroy(_mpio_cache
);
1646 kmultipathd
= create_workqueue("kmpathd");
1648 DMERR("failed to create workqueue kmpathd");
1649 dm_unregister_target(&multipath_target
);
1650 kmem_cache_destroy(_mpio_cache
);
1655 * A separate workqueue is used to handle the device handlers
1656 * to avoid overloading existing workqueue. Overloading the
1657 * old workqueue would also create a bottleneck in the
1658 * path of the storage hardware device activation.
1660 kmpath_handlerd
= create_singlethread_workqueue("kmpath_handlerd");
1661 if (!kmpath_handlerd
) {
1662 DMERR("failed to create workqueue kmpath_handlerd");
1663 destroy_workqueue(kmultipathd
);
1664 dm_unregister_target(&multipath_target
);
1665 kmem_cache_destroy(_mpio_cache
);
1669 DMINFO("version %u.%u.%u loaded",
1670 multipath_target
.version
[0], multipath_target
.version
[1],
1671 multipath_target
.version
[2]);
1676 static void __exit
dm_multipath_exit(void)
1678 destroy_workqueue(kmpath_handlerd
);
1679 destroy_workqueue(kmultipathd
);
1681 dm_unregister_target(&multipath_target
);
1682 kmem_cache_destroy(_mpio_cache
);
1685 module_init(dm_multipath_init
);
1686 module_exit(dm_multipath_exit
);
1688 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1689 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1690 MODULE_LICENSE("GPL");