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 <linux/atomic.h>
24 #define DM_MSG_PREFIX "multipath"
25 #define DM_PG_INIT_DELAY_MSECS 2000
26 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
30 struct list_head list
;
32 struct priority_group
*pg
; /* Owning PG */
33 unsigned is_active
; /* Path status */
34 unsigned fail_count
; /* Cumulative failure count */
37 struct delayed_work 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 #define FEATURE_NO_PARTITIONS 1
61 /* Multipath context */
63 struct list_head list
;
68 const char *hw_handler_name
;
69 char *hw_handler_params
;
71 unsigned nr_priority_groups
;
72 struct list_head priority_groups
;
74 wait_queue_head_t pg_init_wait
; /* Wait for pg_init completion */
76 unsigned pg_init_required
; /* pg_init needs calling? */
77 unsigned pg_init_in_progress
; /* Only one pg_init allowed at once */
78 unsigned pg_init_delay_retry
; /* Delay pg_init retry? */
80 unsigned nr_valid_paths
; /* Total number of usable paths */
81 struct pgpath
*current_pgpath
;
82 struct priority_group
*current_pg
;
83 struct priority_group
*next_pg
; /* Switch to this PG if set */
84 unsigned repeat_count
; /* I/Os left before calling PS again */
86 unsigned queue_io
; /* Must we queue all I/O? */
87 unsigned queue_if_no_path
; /* Queue I/O if last path fails? */
88 unsigned saved_queue_if_no_path
;/* Saved state during suspension */
89 unsigned pg_init_retries
; /* Number of times to retry pg_init */
90 unsigned pg_init_count
; /* Number of times pg_init called */
91 unsigned pg_init_delay_msecs
; /* Number of msecs before pg_init retry */
92 unsigned features
; /* Additional selected features */
94 struct work_struct process_queued_ios
;
95 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
;
166 list_for_each_entry_safe(pgpath
, tmp
, pgpaths
, list
) {
167 list_del(&pgpath
->list
);
168 dm_put_device(ti
, pgpath
->path
.dev
);
173 static void free_priority_group(struct priority_group
*pg
,
174 struct dm_target
*ti
)
176 struct path_selector
*ps
= &pg
->ps
;
179 ps
->type
->destroy(ps
);
180 dm_put_path_selector(ps
->type
);
183 free_pgpaths(&pg
->pgpaths
, ti
);
187 static struct multipath
*alloc_multipath(struct dm_target
*ti
)
191 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
193 INIT_LIST_HEAD(&m
->priority_groups
);
194 INIT_LIST_HEAD(&m
->queued_ios
);
195 spin_lock_init(&m
->lock
);
197 m
->pg_init_delay_msecs
= DM_PG_INIT_DELAY_DEFAULT
;
198 INIT_WORK(&m
->process_queued_ios
, process_queued_ios
);
199 INIT_WORK(&m
->trigger_event
, trigger_event
);
200 init_waitqueue_head(&m
->pg_init_wait
);
201 mutex_init(&m
->work_mutex
);
202 m
->mpio_pool
= mempool_create_slab_pool(MIN_IOS
, _mpio_cache
);
214 static void free_multipath(struct multipath
*m
)
216 struct priority_group
*pg
, *tmp
;
218 list_for_each_entry_safe(pg
, tmp
, &m
->priority_groups
, list
) {
220 free_priority_group(pg
, m
->ti
);
223 kfree(m
->hw_handler_name
);
224 kfree(m
->hw_handler_params
);
225 mempool_destroy(m
->mpio_pool
);
230 /*-----------------------------------------------
232 *-----------------------------------------------*/
234 static void __pg_init_all_paths(struct multipath
*m
)
236 struct pgpath
*pgpath
;
237 unsigned long pg_init_delay
= 0;
240 m
->pg_init_required
= 0;
241 if (m
->pg_init_delay_retry
)
242 pg_init_delay
= msecs_to_jiffies(m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
?
243 m
->pg_init_delay_msecs
: DM_PG_INIT_DELAY_MSECS
);
244 list_for_each_entry(pgpath
, &m
->current_pg
->pgpaths
, list
) {
245 /* Skip failed paths */
246 if (!pgpath
->is_active
)
248 if (queue_delayed_work(kmpath_handlerd
, &pgpath
->activate_path
,
250 m
->pg_init_in_progress
++;
254 static void __switch_pg(struct multipath
*m
, struct pgpath
*pgpath
)
256 m
->current_pg
= pgpath
->pg
;
258 /* Must we initialise the PG first, and queue I/O till it's ready? */
259 if (m
->hw_handler_name
) {
260 m
->pg_init_required
= 1;
263 m
->pg_init_required
= 0;
267 m
->pg_init_count
= 0;
270 static int __choose_path_in_pg(struct multipath
*m
, struct priority_group
*pg
,
273 struct dm_path
*path
;
275 path
= pg
->ps
.type
->select_path(&pg
->ps
, &m
->repeat_count
, nr_bytes
);
279 m
->current_pgpath
= path_to_pgpath(path
);
281 if (!m
->current_pgpath
->path
.dev
) {
282 m
->current_pgpath
= NULL
;
286 if (m
->current_pg
!= pg
)
287 __switch_pg(m
, m
->current_pgpath
);
292 static void __choose_pgpath(struct multipath
*m
, size_t nr_bytes
)
294 struct priority_group
*pg
;
295 unsigned bypassed
= 1;
297 if (!m
->nr_valid_paths
)
300 /* Were we instructed to switch PG? */
304 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
308 /* Don't change PG until it has no remaining paths */
309 if (m
->current_pg
&& !__choose_path_in_pg(m
, m
->current_pg
, nr_bytes
))
313 * Loop through priority groups until we find a valid path.
314 * First time we skip PGs marked 'bypassed'.
315 * Second time we only try the ones we skipped.
318 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
319 if (pg
->bypassed
== bypassed
)
321 if (!__choose_path_in_pg(m
, pg
, nr_bytes
))
324 } while (bypassed
--);
327 m
->current_pgpath
= NULL
;
328 m
->current_pg
= NULL
;
332 * Check whether bios must be queued in the device-mapper core rather
333 * than here in the target.
335 * m->lock must be held on entry.
337 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
338 * same value then we are not between multipath_presuspend()
339 * and multipath_resume() calls and we have no need to check
340 * for the DMF_NOFLUSH_SUSPENDING flag.
342 static int __must_push_back(struct multipath
*m
)
344 return (m
->queue_if_no_path
!= m
->saved_queue_if_no_path
&&
345 dm_noflush_suspending(m
->ti
));
348 static int map_io(struct multipath
*m
, struct request
*clone
,
349 struct dm_mpath_io
*mpio
, unsigned was_queued
)
351 int r
= DM_MAPIO_REMAPPED
;
352 size_t nr_bytes
= blk_rq_bytes(clone
);
354 struct pgpath
*pgpath
;
355 struct block_device
*bdev
;
357 spin_lock_irqsave(&m
->lock
, flags
);
359 /* Do we need to select a new pgpath? */
360 if (!m
->current_pgpath
||
361 (!m
->queue_io
&& (m
->repeat_count
&& --m
->repeat_count
== 0)))
362 __choose_pgpath(m
, nr_bytes
);
364 pgpath
= m
->current_pgpath
;
369 if ((pgpath
&& m
->queue_io
) ||
370 (!pgpath
&& m
->queue_if_no_path
)) {
371 /* Queue for the daemon to resubmit */
372 list_add_tail(&clone
->queuelist
, &m
->queued_ios
);
374 if ((m
->pg_init_required
&& !m
->pg_init_in_progress
) ||
376 queue_work(kmultipathd
, &m
->process_queued_ios
);
378 r
= DM_MAPIO_SUBMITTED
;
380 bdev
= pgpath
->path
.dev
->bdev
;
381 clone
->q
= bdev_get_queue(bdev
);
382 clone
->rq_disk
= bdev
->bd_disk
;
383 } else if (__must_push_back(m
))
384 r
= DM_MAPIO_REQUEUE
;
386 r
= -EIO
; /* Failed */
388 mpio
->pgpath
= pgpath
;
389 mpio
->nr_bytes
= nr_bytes
;
391 if (r
== DM_MAPIO_REMAPPED
&& pgpath
->pg
->ps
.type
->start_io
)
392 pgpath
->pg
->ps
.type
->start_io(&pgpath
->pg
->ps
, &pgpath
->path
,
395 spin_unlock_irqrestore(&m
->lock
, flags
);
401 * If we run out of usable paths, should we queue I/O or error it?
403 static int queue_if_no_path(struct multipath
*m
, unsigned queue_if_no_path
,
404 unsigned save_old_value
)
408 spin_lock_irqsave(&m
->lock
, flags
);
411 m
->saved_queue_if_no_path
= m
->queue_if_no_path
;
413 m
->saved_queue_if_no_path
= queue_if_no_path
;
414 m
->queue_if_no_path
= queue_if_no_path
;
415 if (!m
->queue_if_no_path
&& m
->queue_size
)
416 queue_work(kmultipathd
, &m
->process_queued_ios
);
418 spin_unlock_irqrestore(&m
->lock
, flags
);
423 /*-----------------------------------------------------------------
424 * The multipath daemon is responsible for resubmitting queued ios.
425 *---------------------------------------------------------------*/
427 static void dispatch_queued_ios(struct multipath
*m
)
431 struct dm_mpath_io
*mpio
;
432 union map_info
*info
;
433 struct request
*clone
, *n
;
436 spin_lock_irqsave(&m
->lock
, flags
);
437 list_splice_init(&m
->queued_ios
, &cl
);
438 spin_unlock_irqrestore(&m
->lock
, flags
);
440 list_for_each_entry_safe(clone
, n
, &cl
, queuelist
) {
441 list_del_init(&clone
->queuelist
);
443 info
= dm_get_rq_mapinfo(clone
);
446 r
= map_io(m
, clone
, mpio
, 1);
448 mempool_free(mpio
, m
->mpio_pool
);
449 dm_kill_unmapped_request(clone
, r
);
450 } else if (r
== DM_MAPIO_REMAPPED
)
451 dm_dispatch_request(clone
);
452 else if (r
== DM_MAPIO_REQUEUE
) {
453 mempool_free(mpio
, m
->mpio_pool
);
454 dm_requeue_unmapped_request(clone
);
459 static void process_queued_ios(struct work_struct
*work
)
461 struct multipath
*m
=
462 container_of(work
, struct multipath
, process_queued_ios
);
463 struct pgpath
*pgpath
= NULL
;
464 unsigned must_queue
= 1;
467 spin_lock_irqsave(&m
->lock
, flags
);
472 if (!m
->current_pgpath
)
473 __choose_pgpath(m
, 0);
475 pgpath
= m
->current_pgpath
;
477 if ((pgpath
&& !m
->queue_io
) ||
478 (!pgpath
&& !m
->queue_if_no_path
))
481 if (m
->pg_init_required
&& !m
->pg_init_in_progress
&& pgpath
)
482 __pg_init_all_paths(m
);
485 spin_unlock_irqrestore(&m
->lock
, flags
);
487 dispatch_queued_ios(m
);
491 * An event is triggered whenever a path is taken out of use.
492 * Includes path failure and PG bypass.
494 static void trigger_event(struct work_struct
*work
)
496 struct multipath
*m
=
497 container_of(work
, struct multipath
, trigger_event
);
499 dm_table_event(m
->ti
->table
);
502 /*-----------------------------------------------------------------
503 * Constructor/argument parsing:
504 * <#multipath feature args> [<arg>]*
505 * <#hw_handler args> [hw_handler [<arg>]*]
507 * <initial priority group>
508 * [<selector> <#selector args> [<arg>]*
509 * <#paths> <#per-path selector args>
510 * [<path> [<arg>]* ]+ ]+
511 *---------------------------------------------------------------*/
512 static int parse_path_selector(struct dm_arg_set
*as
, struct priority_group
*pg
,
513 struct dm_target
*ti
)
516 struct path_selector_type
*pst
;
519 static struct dm_arg _args
[] = {
520 {0, 1024, "invalid number of path selector args"},
523 pst
= dm_get_path_selector(dm_shift_arg(as
));
525 ti
->error
= "unknown path selector type";
529 r
= dm_read_arg_group(_args
, as
, &ps_argc
, &ti
->error
);
531 dm_put_path_selector(pst
);
535 r
= pst
->create(&pg
->ps
, ps_argc
, as
->argv
);
537 dm_put_path_selector(pst
);
538 ti
->error
= "path selector constructor failed";
543 dm_consume_args(as
, ps_argc
);
548 static struct pgpath
*parse_path(struct dm_arg_set
*as
, struct path_selector
*ps
,
549 struct dm_target
*ti
)
554 struct multipath
*m
= ti
->private;
556 /* we need at least a path arg */
558 ti
->error
= "no device given";
559 return ERR_PTR(-EINVAL
);
564 return ERR_PTR(-ENOMEM
);
566 path
= dm_shift_arg(as
);
567 r
= dm_get_device(ti
, path
, dm_table_get_mode(ti
->table
),
570 unsigned major
, minor
;
572 /* Try to add a failed device */
573 if (r
== -ENXIO
&& sscanf(path
, "%u:%u", &major
, &minor
) == 2) {
576 /* Extract the major/minor numbers */
577 dev
= MKDEV(major
, minor
);
578 if (MAJOR(dev
) != major
|| MINOR(dev
) != minor
) {
579 /* Nice try, didn't work */
580 DMWARN("Invalid device path %s", path
);
581 ti
->error
= "error converting devnum";
584 DMWARN("adding disabled device %d:%d", major
, minor
);
586 format_dev_t(p
->path
.pdev
, dev
);
589 ti
->error
= "error getting device";
593 memcpy(p
->path
.pdev
, p
->path
.dev
->name
, 16);
597 struct request_queue
*q
= bdev_get_queue(p
->path
.dev
->bdev
);
599 if (m
->hw_handler_name
) {
600 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
603 * Already attached to different hw_handler,
604 * try to reattach with correct one.
607 r
= scsi_dh_attach(q
, m
->hw_handler_name
);
610 ti
->error
= "error attaching hardware handler";
611 dm_put_device(ti
, p
->path
.dev
);
615 /* Play safe and detach hardware handler */
619 if (m
->hw_handler_params
) {
620 r
= scsi_dh_set_params(q
, m
->hw_handler_params
);
622 ti
->error
= "unable to set hardware "
623 "handler parameters";
625 dm_put_device(ti
, p
->path
.dev
);
631 r
= ps
->type
->add_path(ps
, &p
->path
, as
->argc
, as
->argv
, &ti
->error
);
633 dm_put_device(ti
, p
->path
.dev
);
638 ps
->type
->fail_path(ps
, &p
->path
);
649 static struct priority_group
*parse_priority_group(struct dm_arg_set
*as
,
652 static struct dm_arg _args
[] = {
653 {1, 1024, "invalid number of paths"},
654 {0, 1024, "invalid number of selector args"}
658 unsigned i
, nr_selector_args
, nr_args
;
659 struct priority_group
*pg
;
660 struct dm_target
*ti
= m
->ti
;
664 ti
->error
= "not enough priority group arguments";
665 return ERR_PTR(-EINVAL
);
668 pg
= alloc_priority_group();
670 ti
->error
= "couldn't allocate priority group";
671 return ERR_PTR(-ENOMEM
);
675 r
= parse_path_selector(as
, pg
, ti
);
682 r
= dm_read_arg(_args
, as
, &pg
->nr_pgpaths
, &ti
->error
);
686 r
= dm_read_arg(_args
+ 1, as
, &nr_selector_args
, &ti
->error
);
690 nr_args
= 1 + nr_selector_args
;
691 for (i
= 0; i
< pg
->nr_pgpaths
; i
++) {
692 struct pgpath
*pgpath
;
693 struct dm_arg_set path_args
;
695 if (as
->argc
< nr_args
) {
696 ti
->error
= "not enough path parameters";
701 path_args
.argc
= nr_args
;
702 path_args
.argv
= as
->argv
;
704 pgpath
= parse_path(&path_args
, &pg
->ps
, ti
);
705 if (IS_ERR(pgpath
)) {
711 list_add_tail(&pgpath
->list
, &pg
->pgpaths
);
712 dm_consume_args(as
, nr_args
);
718 free_priority_group(pg
, ti
);
722 static int parse_hw_handler(struct dm_arg_set
*as
, struct multipath
*m
)
726 struct dm_target
*ti
= m
->ti
;
728 static struct dm_arg _args
[] = {
729 {0, 1024, "invalid number of hardware handler args"},
732 if (dm_read_arg_group(_args
, as
, &hw_argc
, &ti
->error
))
738 m
->hw_handler_name
= kstrdup(dm_shift_arg(as
), GFP_KERNEL
);
739 if (!try_then_request_module(scsi_dh_handler_exist(m
->hw_handler_name
),
740 "scsi_dh_%s", m
->hw_handler_name
)) {
741 ti
->error
= "unknown hardware handler type";
750 for (i
= 0; i
<= hw_argc
- 2; i
++)
751 len
+= strlen(as
->argv
[i
]) + 1;
752 p
= m
->hw_handler_params
= kzalloc(len
, GFP_KERNEL
);
754 ti
->error
= "memory allocation failed";
758 j
= sprintf(p
, "%d", hw_argc
- 1);
759 for (i
= 0, p
+=j
+1; i
<= hw_argc
- 2; i
++, p
+=j
+1)
760 j
= sprintf(p
, "%s", as
->argv
[i
]);
762 dm_consume_args(as
, hw_argc
- 1);
766 kfree(m
->hw_handler_name
);
767 m
->hw_handler_name
= NULL
;
771 static int parse_features(struct dm_arg_set
*as
, struct multipath
*m
)
775 struct dm_target
*ti
= m
->ti
;
776 const char *arg_name
;
778 static struct dm_arg _args
[] = {
779 {0, 5, "invalid number of feature args"},
780 {1, 50, "pg_init_retries must be between 1 and 50"},
781 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
784 r
= dm_read_arg_group(_args
, as
, &argc
, &ti
->error
);
792 arg_name
= dm_shift_arg(as
);
795 if (!strcasecmp(arg_name
, "queue_if_no_path")) {
796 r
= queue_if_no_path(m
, 1, 0);
800 if (!strcasecmp(arg_name
, "no_partitions")) {
801 m
->features
|= FEATURE_NO_PARTITIONS
;
804 if (!strcasecmp(arg_name
, "pg_init_retries") &&
806 r
= dm_read_arg(_args
+ 1, as
, &m
->pg_init_retries
, &ti
->error
);
811 if (!strcasecmp(arg_name
, "pg_init_delay_msecs") &&
813 r
= dm_read_arg(_args
+ 2, as
, &m
->pg_init_delay_msecs
, &ti
->error
);
818 ti
->error
= "Unrecognised multipath feature request";
820 } while (argc
&& !r
);
825 static int multipath_ctr(struct dm_target
*ti
, unsigned int argc
,
828 /* target arguments */
829 static struct dm_arg _args
[] = {
830 {0, 1024, "invalid number of priority groups"},
831 {0, 1024, "invalid initial priority group number"},
836 struct dm_arg_set as
;
837 unsigned pg_count
= 0;
838 unsigned next_pg_num
;
843 m
= alloc_multipath(ti
);
845 ti
->error
= "can't allocate multipath";
849 r
= parse_features(&as
, m
);
853 r
= parse_hw_handler(&as
, m
);
857 r
= dm_read_arg(_args
, &as
, &m
->nr_priority_groups
, &ti
->error
);
861 r
= dm_read_arg(_args
+ 1, &as
, &next_pg_num
, &ti
->error
);
865 if ((!m
->nr_priority_groups
&& next_pg_num
) ||
866 (m
->nr_priority_groups
&& !next_pg_num
)) {
867 ti
->error
= "invalid initial priority group";
872 /* parse the priority groups */
874 struct priority_group
*pg
;
876 pg
= parse_priority_group(&as
, m
);
882 m
->nr_valid_paths
+= pg
->nr_pgpaths
;
883 list_add_tail(&pg
->list
, &m
->priority_groups
);
885 pg
->pg_num
= pg_count
;
890 if (pg_count
!= m
->nr_priority_groups
) {
891 ti
->error
= "priority group count mismatch";
896 ti
->num_flush_requests
= 1;
897 ti
->num_discard_requests
= 1;
906 static void multipath_wait_for_pg_init_completion(struct multipath
*m
)
908 DECLARE_WAITQUEUE(wait
, current
);
911 add_wait_queue(&m
->pg_init_wait
, &wait
);
914 set_current_state(TASK_UNINTERRUPTIBLE
);
916 spin_lock_irqsave(&m
->lock
, flags
);
917 if (!m
->pg_init_in_progress
) {
918 spin_unlock_irqrestore(&m
->lock
, flags
);
921 spin_unlock_irqrestore(&m
->lock
, flags
);
925 set_current_state(TASK_RUNNING
);
927 remove_wait_queue(&m
->pg_init_wait
, &wait
);
930 static void flush_multipath_work(struct multipath
*m
)
932 flush_workqueue(kmpath_handlerd
);
933 multipath_wait_for_pg_init_completion(m
);
934 flush_workqueue(kmultipathd
);
935 flush_work_sync(&m
->trigger_event
);
938 static void multipath_dtr(struct dm_target
*ti
)
940 struct multipath
*m
= ti
->private;
942 flush_multipath_work(m
);
947 * Map cloned requests
949 static int multipath_map(struct dm_target
*ti
, struct request
*clone
,
950 union map_info
*map_context
)
953 struct dm_mpath_io
*mpio
;
954 struct multipath
*m
= (struct multipath
*) ti
->private;
956 mpio
= mempool_alloc(m
->mpio_pool
, GFP_ATOMIC
);
958 /* ENOMEM, requeue */
959 return DM_MAPIO_REQUEUE
;
960 memset(mpio
, 0, sizeof(*mpio
));
962 map_context
->ptr
= mpio
;
963 clone
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
964 r
= map_io(m
, clone
, mpio
, 0);
965 if (r
< 0 || r
== DM_MAPIO_REQUEUE
)
966 mempool_free(mpio
, m
->mpio_pool
);
972 * Take a path out of use.
974 static int fail_path(struct pgpath
*pgpath
)
977 struct multipath
*m
= pgpath
->pg
->m
;
979 spin_lock_irqsave(&m
->lock
, flags
);
981 if (!pgpath
->is_active
)
984 DMWARN("Failing path %s.", pgpath
->path
.pdev
);
986 pgpath
->pg
->ps
.type
->fail_path(&pgpath
->pg
->ps
, &pgpath
->path
);
987 pgpath
->is_active
= 0;
988 pgpath
->fail_count
++;
992 if (pgpath
== m
->current_pgpath
)
993 m
->current_pgpath
= NULL
;
995 dm_path_uevent(DM_UEVENT_PATH_FAILED
, m
->ti
,
996 pgpath
->path
.pdev
, m
->nr_valid_paths
);
998 schedule_work(&m
->trigger_event
);
1001 spin_unlock_irqrestore(&m
->lock
, flags
);
1007 * Reinstate a previously-failed path
1009 static int reinstate_path(struct pgpath
*pgpath
)
1012 unsigned long flags
;
1013 struct multipath
*m
= pgpath
->pg
->m
;
1015 spin_lock_irqsave(&m
->lock
, flags
);
1017 if (pgpath
->is_active
)
1020 if (!pgpath
->path
.dev
) {
1021 DMWARN("Cannot reinstate disabled path %s", pgpath
->path
.pdev
);
1026 if (!pgpath
->pg
->ps
.type
->reinstate_path
) {
1027 DMWARN("Reinstate path not supported by path selector %s",
1028 pgpath
->pg
->ps
.type
->name
);
1033 r
= pgpath
->pg
->ps
.type
->reinstate_path(&pgpath
->pg
->ps
, &pgpath
->path
);
1037 pgpath
->is_active
= 1;
1039 if (!m
->nr_valid_paths
++ && m
->queue_size
) {
1040 m
->current_pgpath
= NULL
;
1041 queue_work(kmultipathd
, &m
->process_queued_ios
);
1042 } else if (m
->hw_handler_name
&& (m
->current_pg
== pgpath
->pg
)) {
1043 if (queue_work(kmpath_handlerd
, &pgpath
->activate_path
.work
))
1044 m
->pg_init_in_progress
++;
1047 dm_path_uevent(DM_UEVENT_PATH_REINSTATED
, m
->ti
,
1048 pgpath
->path
.pdev
, m
->nr_valid_paths
);
1050 schedule_work(&m
->trigger_event
);
1053 spin_unlock_irqrestore(&m
->lock
, flags
);
1059 * Fail or reinstate all paths that match the provided struct dm_dev.
1061 static int action_dev(struct multipath
*m
, struct dm_dev
*dev
,
1065 struct pgpath
*pgpath
;
1066 struct priority_group
*pg
;
1071 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1072 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
) {
1073 if (pgpath
->path
.dev
== dev
)
1082 * Temporarily try to avoid having to use the specified PG
1084 static void bypass_pg(struct multipath
*m
, struct priority_group
*pg
,
1087 unsigned long flags
;
1089 spin_lock_irqsave(&m
->lock
, flags
);
1091 pg
->bypassed
= bypassed
;
1092 m
->current_pgpath
= NULL
;
1093 m
->current_pg
= NULL
;
1095 spin_unlock_irqrestore(&m
->lock
, flags
);
1097 schedule_work(&m
->trigger_event
);
1101 * Switch to using the specified PG from the next I/O that gets mapped
1103 static int switch_pg_num(struct multipath
*m
, const char *pgstr
)
1105 struct priority_group
*pg
;
1107 unsigned long flags
;
1109 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 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
;
1140 if (!pgstr
|| (sscanf(pgstr
, "%u", &pgnum
) != 1) || !pgnum
||
1141 (pgnum
> m
->nr_priority_groups
)) {
1142 DMWARN("invalid PG number supplied to bypass_pg");
1146 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1151 bypass_pg(m
, pg
, bypassed
);
1156 * Should we retry pg_init immediately?
1158 static int pg_init_limit_reached(struct multipath
*m
, struct pgpath
*pgpath
)
1160 unsigned long flags
;
1161 int limit_reached
= 0;
1163 spin_lock_irqsave(&m
->lock
, flags
);
1165 if (m
->pg_init_count
<= m
->pg_init_retries
)
1166 m
->pg_init_required
= 1;
1170 spin_unlock_irqrestore(&m
->lock
, flags
);
1172 return limit_reached
;
1175 static void pg_init_done(void *data
, int errors
)
1177 struct pgpath
*pgpath
= data
;
1178 struct priority_group
*pg
= pgpath
->pg
;
1179 struct multipath
*m
= pg
->m
;
1180 unsigned long flags
;
1181 unsigned delay_retry
= 0;
1183 /* device or driver problems */
1188 if (!m
->hw_handler_name
) {
1192 DMERR("Count not failover device %s: Handler scsi_dh_%s "
1193 "was not loaded.", pgpath
->path
.dev
->name
,
1194 m
->hw_handler_name
);
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);
1207 case SCSI_DH_DEV_OFFLINED
:
1208 DMWARN("Device %s offlined.", pgpath
->path
.dev
->name
);
1212 /* Wait before retrying. */
1214 case SCSI_DH_IMM_RETRY
:
1215 case SCSI_DH_RES_TEMP_UNAVAIL
:
1216 if (pg_init_limit_reached(m
, pgpath
))
1222 * We probably do not want to fail the path for a device
1223 * error, but this is what the old dm did. In future
1224 * patches we can do more advanced handling.
1229 spin_lock_irqsave(&m
->lock
, flags
);
1231 if (pgpath
== m
->current_pgpath
) {
1232 DMERR("Could not failover device %s, error %d.",
1233 pgpath
->path
.dev
->name
, errors
);
1234 m
->current_pgpath
= NULL
;
1235 m
->current_pg
= NULL
;
1237 } else if (!m
->pg_init_required
)
1240 if (--m
->pg_init_in_progress
)
1241 /* Activations of other paths are still on going */
1244 if (!m
->pg_init_required
)
1247 m
->pg_init_delay_retry
= delay_retry
;
1248 queue_work(kmultipathd
, &m
->process_queued_ios
);
1251 * Wake up any thread waiting to suspend.
1253 wake_up(&m
->pg_init_wait
);
1256 spin_unlock_irqrestore(&m
->lock
, flags
);
1259 static void activate_path(struct work_struct
*work
)
1261 struct pgpath
*pgpath
=
1262 container_of(work
, struct pgpath
, activate_path
.work
);
1264 if (pgpath
->path
.dev
)
1265 scsi_dh_activate(bdev_get_queue(pgpath
->path
.dev
->bdev
),
1266 pg_init_done
, pgpath
);
1272 static int do_end_io(struct multipath
*m
, struct request
*clone
,
1273 int error
, struct dm_mpath_io
*mpio
)
1276 * We don't queue any clone request inside the multipath target
1277 * during end I/O handling, since those clone requests don't have
1278 * bio clones. If we queue them inside the multipath target,
1279 * we need to make bio clones, that requires memory allocation.
1280 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1281 * don't have bio clones.)
1282 * Instead of queueing the clone request here, we queue the original
1283 * request into dm core, which will remake a clone request and
1284 * clone bios for it and resubmit it later.
1286 int r
= DM_ENDIO_REQUEUE
;
1287 unsigned long flags
;
1289 if (!error
&& !clone
->errors
)
1290 return 0; /* I/O complete */
1292 if (error
== -EOPNOTSUPP
|| error
== -EREMOTEIO
|| error
== -EILSEQ
)
1296 fail_path(mpio
->pgpath
);
1298 spin_lock_irqsave(&m
->lock
, flags
);
1299 if (!m
->nr_valid_paths
) {
1300 if (!m
->queue_if_no_path
) {
1301 if (!__must_push_back(m
))
1304 if (error
== -EBADE
)
1308 spin_unlock_irqrestore(&m
->lock
, flags
);
1313 static int multipath_end_io(struct dm_target
*ti
, struct request
*clone
,
1314 int error
, union map_info
*map_context
)
1316 struct multipath
*m
= ti
->private;
1317 struct dm_mpath_io
*mpio
= map_context
->ptr
;
1318 struct pgpath
*pgpath
= mpio
->pgpath
;
1319 struct path_selector
*ps
;
1322 r
= do_end_io(m
, clone
, error
, mpio
);
1324 ps
= &pgpath
->pg
->ps
;
1325 if (ps
->type
->end_io
)
1326 ps
->type
->end_io(ps
, &pgpath
->path
, mpio
->nr_bytes
);
1328 mempool_free(mpio
, m
->mpio_pool
);
1334 * Suspend can't complete until all the I/O is processed so if
1335 * the last path fails we must error any remaining I/O.
1336 * Note that if the freeze_bdev fails while suspending, the
1337 * queue_if_no_path state is lost - userspace should reset it.
1339 static void multipath_presuspend(struct dm_target
*ti
)
1341 struct multipath
*m
= (struct multipath
*) ti
->private;
1343 queue_if_no_path(m
, 0, 1);
1346 static void multipath_postsuspend(struct dm_target
*ti
)
1348 struct multipath
*m
= ti
->private;
1350 mutex_lock(&m
->work_mutex
);
1351 flush_multipath_work(m
);
1352 mutex_unlock(&m
->work_mutex
);
1356 * Restore the queue_if_no_path setting.
1358 static void multipath_resume(struct dm_target
*ti
)
1360 struct multipath
*m
= (struct multipath
*) ti
->private;
1361 unsigned long flags
;
1363 spin_lock_irqsave(&m
->lock
, flags
);
1364 m
->queue_if_no_path
= m
->saved_queue_if_no_path
;
1365 spin_unlock_irqrestore(&m
->lock
, flags
);
1369 * Info output has the following format:
1370 * num_multipath_feature_args [multipath_feature_args]*
1371 * num_handler_status_args [handler_status_args]*
1372 * num_groups init_group_number
1373 * [A|D|E num_ps_status_args [ps_status_args]*
1374 * num_paths num_selector_args
1375 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1377 * Table output has the following format (identical to the constructor string):
1378 * num_feature_args [features_args]*
1379 * num_handler_args hw_handler [hw_handler_args]*
1380 * num_groups init_group_number
1381 * [priority selector-name num_ps_args [ps_args]*
1382 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1384 static int multipath_status(struct dm_target
*ti
, status_type_t type
,
1385 char *result
, unsigned int maxlen
)
1388 unsigned long flags
;
1389 struct multipath
*m
= (struct multipath
*) ti
->private;
1390 struct priority_group
*pg
;
1395 spin_lock_irqsave(&m
->lock
, flags
);
1398 if (type
== STATUSTYPE_INFO
)
1399 DMEMIT("2 %u %u ", m
->queue_size
, m
->pg_init_count
);
1401 DMEMIT("%u ", m
->queue_if_no_path
+
1402 (m
->pg_init_retries
> 0) * 2 +
1403 (m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
) * 2 +
1404 (m
->features
& FEATURE_NO_PARTITIONS
));
1405 if (m
->queue_if_no_path
)
1406 DMEMIT("queue_if_no_path ");
1407 if (m
->pg_init_retries
)
1408 DMEMIT("pg_init_retries %u ", m
->pg_init_retries
);
1409 if (m
->features
& FEATURE_NO_PARTITIONS
)
1410 DMEMIT("no_partitions ");
1411 if (m
->pg_init_delay_msecs
!= DM_PG_INIT_DELAY_DEFAULT
)
1412 DMEMIT("pg_init_delay_msecs %u ", m
->pg_init_delay_msecs
);
1415 if (!m
->hw_handler_name
|| type
== STATUSTYPE_INFO
)
1418 DMEMIT("1 %s ", m
->hw_handler_name
);
1420 DMEMIT("%u ", m
->nr_priority_groups
);
1423 pg_num
= m
->next_pg
->pg_num
;
1424 else if (m
->current_pg
)
1425 pg_num
= m
->current_pg
->pg_num
;
1427 pg_num
= (m
->nr_priority_groups
? 1 : 0);
1429 DMEMIT("%u ", pg_num
);
1432 case STATUSTYPE_INFO
:
1433 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1435 state
= 'D'; /* Disabled */
1436 else if (pg
== m
->current_pg
)
1437 state
= 'A'; /* Currently Active */
1439 state
= 'E'; /* Enabled */
1441 DMEMIT("%c ", state
);
1443 if (pg
->ps
.type
->status
)
1444 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1450 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1451 pg
->ps
.type
->info_args
);
1453 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1454 DMEMIT("%s %s %u ", p
->path
.pdev
,
1455 p
->is_active
? "A" : "F",
1457 if (pg
->ps
.type
->status
)
1458 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1459 &p
->path
, type
, result
+ sz
,
1465 case STATUSTYPE_TABLE
:
1466 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1467 DMEMIT("%s ", pg
->ps
.type
->name
);
1469 if (pg
->ps
.type
->status
)
1470 sz
+= pg
->ps
.type
->status(&pg
->ps
, NULL
, type
,
1476 DMEMIT("%u %u ", pg
->nr_pgpaths
,
1477 pg
->ps
.type
->table_args
);
1479 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1480 DMEMIT("%s ", p
->path
.pdev
);
1481 if (pg
->ps
.type
->status
)
1482 sz
+= pg
->ps
.type
->status(&pg
->ps
,
1483 &p
->path
, type
, result
+ sz
,
1490 spin_unlock_irqrestore(&m
->lock
, flags
);
1495 static int multipath_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1499 struct multipath
*m
= (struct multipath
*) ti
->private;
1502 mutex_lock(&m
->work_mutex
);
1504 if (dm_suspended(ti
)) {
1510 if (!strcasecmp(argv
[0], "queue_if_no_path")) {
1511 r
= queue_if_no_path(m
, 1, 0);
1513 } else if (!strcasecmp(argv
[0], "fail_if_no_path")) {
1514 r
= queue_if_no_path(m
, 0, 0);
1520 DMWARN("Unrecognised multipath message received.");
1524 if (!strcasecmp(argv
[0], "disable_group")) {
1525 r
= bypass_pg_num(m
, argv
[1], 1);
1527 } else if (!strcasecmp(argv
[0], "enable_group")) {
1528 r
= bypass_pg_num(m
, argv
[1], 0);
1530 } else if (!strcasecmp(argv
[0], "switch_group")) {
1531 r
= switch_pg_num(m
, argv
[1]);
1533 } else if (!strcasecmp(argv
[0], "reinstate_path"))
1534 action
= reinstate_path
;
1535 else if (!strcasecmp(argv
[0], "fail_path"))
1538 DMWARN("Unrecognised multipath message received.");
1542 r
= dm_get_device(ti
, argv
[1], dm_table_get_mode(ti
->table
), &dev
);
1544 DMWARN("message: error getting device %s",
1549 r
= action_dev(m
, dev
, action
);
1551 dm_put_device(ti
, dev
);
1554 mutex_unlock(&m
->work_mutex
);
1558 static int multipath_ioctl(struct dm_target
*ti
, unsigned int cmd
,
1561 struct multipath
*m
= (struct multipath
*) ti
->private;
1562 struct block_device
*bdev
= NULL
;
1564 unsigned long flags
;
1567 spin_lock_irqsave(&m
->lock
, flags
);
1569 if (!m
->current_pgpath
)
1570 __choose_pgpath(m
, 0);
1572 if (m
->current_pgpath
&& m
->current_pgpath
->path
.dev
) {
1573 bdev
= m
->current_pgpath
->path
.dev
->bdev
;
1574 mode
= m
->current_pgpath
->path
.dev
->mode
;
1582 spin_unlock_irqrestore(&m
->lock
, flags
);
1585 * Only pass ioctls through if the device sizes match exactly.
1587 if (!r
&& ti
->len
!= i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
)
1588 r
= scsi_verify_blk_ioctl(NULL
, cmd
);
1590 return r
? : __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
1593 static int multipath_iterate_devices(struct dm_target
*ti
,
1594 iterate_devices_callout_fn fn
, void *data
)
1596 struct multipath
*m
= ti
->private;
1597 struct priority_group
*pg
;
1601 list_for_each_entry(pg
, &m
->priority_groups
, list
) {
1602 list_for_each_entry(p
, &pg
->pgpaths
, list
) {
1603 ret
= fn(ti
, p
->path
.dev
, ti
->begin
, ti
->len
, data
);
1613 static int __pgpath_busy(struct pgpath
*pgpath
)
1615 struct request_queue
*q
= bdev_get_queue(pgpath
->path
.dev
->bdev
);
1617 return dm_underlying_device_busy(q
);
1621 * We return "busy", only when we can map I/Os but underlying devices
1622 * are busy (so even if we map I/Os now, the I/Os will wait on
1623 * the underlying queue).
1624 * In other words, if we want to kill I/Os or queue them inside us
1625 * due to map unavailability, we don't return "busy". Otherwise,
1626 * dm core won't give us the I/Os and we can't do what we want.
1628 static int multipath_busy(struct dm_target
*ti
)
1630 int busy
= 0, has_active
= 0;
1631 struct multipath
*m
= ti
->private;
1632 struct priority_group
*pg
;
1633 struct pgpath
*pgpath
;
1634 unsigned long flags
;
1636 spin_lock_irqsave(&m
->lock
, flags
);
1638 /* Guess which priority_group will be used at next mapping time */
1639 if (unlikely(!m
->current_pgpath
&& m
->next_pg
))
1641 else if (likely(m
->current_pg
))
1645 * We don't know which pg will be used at next mapping time.
1646 * We don't call __choose_pgpath() here to avoid to trigger
1647 * pg_init just by busy checking.
1648 * So we don't know whether underlying devices we will be using
1649 * at next mapping time are busy or not. Just try mapping.
1654 * If there is one non-busy active path at least, the path selector
1655 * will be able to select it. So we consider such a pg as not busy.
1658 list_for_each_entry(pgpath
, &pg
->pgpaths
, list
)
1659 if (pgpath
->is_active
) {
1662 if (!__pgpath_busy(pgpath
)) {
1670 * No active path in this pg, so this pg won't be used and
1671 * the current_pg will be changed at next mapping time.
1672 * We need to try mapping to determine it.
1677 spin_unlock_irqrestore(&m
->lock
, flags
);
1682 /*-----------------------------------------------------------------
1684 *---------------------------------------------------------------*/
1685 static struct target_type multipath_target
= {
1686 .name
= "multipath",
1687 .version
= {1, 3, 0},
1688 .module
= THIS_MODULE
,
1689 .ctr
= multipath_ctr
,
1690 .dtr
= multipath_dtr
,
1691 .map_rq
= multipath_map
,
1692 .rq_end_io
= multipath_end_io
,
1693 .presuspend
= multipath_presuspend
,
1694 .postsuspend
= multipath_postsuspend
,
1695 .resume
= multipath_resume
,
1696 .status
= multipath_status
,
1697 .message
= multipath_message
,
1698 .ioctl
= multipath_ioctl
,
1699 .iterate_devices
= multipath_iterate_devices
,
1700 .busy
= multipath_busy
,
1703 static int __init
dm_multipath_init(void)
1707 /* allocate a slab for the dm_ios */
1708 _mpio_cache
= KMEM_CACHE(dm_mpath_io
, 0);
1712 r
= dm_register_target(&multipath_target
);
1714 DMERR("register failed %d", r
);
1715 kmem_cache_destroy(_mpio_cache
);
1719 kmultipathd
= alloc_workqueue("kmpathd", WQ_MEM_RECLAIM
, 0);
1721 DMERR("failed to create workqueue kmpathd");
1722 dm_unregister_target(&multipath_target
);
1723 kmem_cache_destroy(_mpio_cache
);
1728 * A separate workqueue is used to handle the device handlers
1729 * to avoid overloading existing workqueue. Overloading the
1730 * old workqueue would also create a bottleneck in the
1731 * path of the storage hardware device activation.
1733 kmpath_handlerd
= alloc_ordered_workqueue("kmpath_handlerd",
1735 if (!kmpath_handlerd
) {
1736 DMERR("failed to create workqueue kmpath_handlerd");
1737 destroy_workqueue(kmultipathd
);
1738 dm_unregister_target(&multipath_target
);
1739 kmem_cache_destroy(_mpio_cache
);
1743 DMINFO("version %u.%u.%u loaded",
1744 multipath_target
.version
[0], multipath_target
.version
[1],
1745 multipath_target
.version
[2]);
1750 static void __exit
dm_multipath_exit(void)
1752 destroy_workqueue(kmpath_handlerd
);
1753 destroy_workqueue(kmultipathd
);
1755 dm_unregister_target(&multipath_target
);
1756 kmem_cache_destroy(_mpio_cache
);
1759 module_init(dm_multipath_init
);
1760 module_exit(dm_multipath_exit
);
1762 MODULE_DESCRIPTION(DM_NAME
" multipath target");
1763 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1764 MODULE_LICENSE("GPL");