Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / md / dm-mpath.c
blob971ae75b34bf94e50b49b046ea83cf5d1e1fc7e9
1 /*
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.
6 */
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)
28 /* Path properties */
29 struct pgpath {
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 */
36 struct dm_path path;
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 */
62 struct multipath {
63 struct list_head list;
64 struct dm_target *ti;
66 spinlock_t lock;
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;
96 unsigned queue_size;
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.
112 struct dm_mpath_io {
113 struct pgpath *pgpath;
114 size_t nr_bytes;
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);
137 if (pgpath) {
138 pgpath->is_active = 1;
139 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
142 return pgpath;
145 static void free_pgpath(struct pgpath *pgpath)
147 kfree(pgpath);
150 static struct priority_group *alloc_priority_group(void)
152 struct priority_group *pg;
154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
156 if (pg)
157 INIT_LIST_HEAD(&pg->pgpaths);
159 return pg;
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);
169 free_pgpath(pgpath);
173 static void free_priority_group(struct priority_group *pg,
174 struct dm_target *ti)
176 struct path_selector *ps = &pg->ps;
178 if (ps->type) {
179 ps->type->destroy(ps);
180 dm_put_path_selector(ps->type);
183 free_pgpaths(&pg->pgpaths, ti);
184 kfree(pg);
187 static struct multipath *alloc_multipath(struct dm_target *ti)
189 struct multipath *m;
191 m = kzalloc(sizeof(*m), GFP_KERNEL);
192 if (m) {
193 INIT_LIST_HEAD(&m->priority_groups);
194 INIT_LIST_HEAD(&m->queued_ios);
195 spin_lock_init(&m->lock);
196 m->queue_io = 1;
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);
203 if (!m->mpio_pool) {
204 kfree(m);
205 return NULL;
207 m->ti = ti;
208 ti->private = m;
211 return m;
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) {
219 list_del(&pg->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);
226 kfree(m);
230 /*-----------------------------------------------
231 * Path selection
232 *-----------------------------------------------*/
234 static void __pg_init_all_paths(struct multipath *m)
236 struct pgpath *pgpath;
237 unsigned long pg_init_delay = 0;
239 m->pg_init_count++;
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)
247 continue;
248 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
249 pg_init_delay))
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;
261 m->queue_io = 1;
262 } else {
263 m->pg_init_required = 0;
264 m->queue_io = 0;
267 m->pg_init_count = 0;
270 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
271 size_t nr_bytes)
273 struct dm_path *path;
275 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
276 if (!path)
277 return -ENXIO;
279 m->current_pgpath = path_to_pgpath(path);
281 if (!m->current_pgpath->path.dev) {
282 m->current_pgpath = NULL;
283 return -ENODEV;
286 if (m->current_pg != pg)
287 __switch_pg(m, m->current_pgpath);
289 return 0;
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)
298 goto failed;
300 /* Were we instructed to switch PG? */
301 if (m->next_pg) {
302 pg = m->next_pg;
303 m->next_pg = NULL;
304 if (!__choose_path_in_pg(m, pg, nr_bytes))
305 return;
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))
310 return;
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.
317 do {
318 list_for_each_entry(pg, &m->priority_groups, list) {
319 if (pg->bypassed == bypassed)
320 continue;
321 if (!__choose_path_in_pg(m, pg, nr_bytes))
322 return;
324 } while (bypassed--);
326 failed:
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);
353 unsigned long flags;
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;
366 if (was_queued)
367 m->queue_size--;
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);
373 m->queue_size++;
374 if ((m->pg_init_required && !m->pg_init_in_progress) ||
375 !m->queue_io)
376 queue_work(kmultipathd, &m->process_queued_ios);
377 pgpath = NULL;
378 r = DM_MAPIO_SUBMITTED;
379 } else if (pgpath) {
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;
385 else
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,
393 nr_bytes);
395 spin_unlock_irqrestore(&m->lock, flags);
397 return r;
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)
406 unsigned long flags;
408 spin_lock_irqsave(&m->lock, flags);
410 if (save_old_value)
411 m->saved_queue_if_no_path = m->queue_if_no_path;
412 else
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);
420 return 0;
423 /*-----------------------------------------------------------------
424 * The multipath daemon is responsible for resubmitting queued ios.
425 *---------------------------------------------------------------*/
427 static void dispatch_queued_ios(struct multipath *m)
429 int r;
430 unsigned long flags;
431 struct dm_mpath_io *mpio;
432 union map_info *info;
433 struct request *clone, *n;
434 LIST_HEAD(cl);
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);
444 mpio = info->ptr;
446 r = map_io(m, clone, mpio, 1);
447 if (r < 0) {
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;
465 unsigned long flags;
467 spin_lock_irqsave(&m->lock, flags);
469 if (!m->queue_size)
470 goto out;
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))
479 must_queue = 0;
481 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
482 __pg_init_all_paths(m);
484 out:
485 spin_unlock_irqrestore(&m->lock, flags);
486 if (!must_queue)
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>]*]
506 * <#priority groups>
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)
515 int r;
516 struct path_selector_type *pst;
517 unsigned ps_argc;
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));
524 if (!pst) {
525 ti->error = "unknown path selector type";
526 return -EINVAL;
529 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
530 if (r) {
531 dm_put_path_selector(pst);
532 return -EINVAL;
535 r = pst->create(&pg->ps, ps_argc, as->argv);
536 if (r) {
537 dm_put_path_selector(pst);
538 ti->error = "path selector constructor failed";
539 return r;
542 pg->ps.type = pst;
543 dm_consume_args(as, ps_argc);
545 return 0;
548 static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
549 struct dm_target *ti)
551 int r;
552 struct pgpath *p;
553 char *path;
554 struct multipath *m = ti->private;
556 /* we need at least a path arg */
557 if (as->argc < 1) {
558 ti->error = "no device given";
559 return ERR_PTR(-EINVAL);
562 p = alloc_pgpath();
563 if (!p)
564 return ERR_PTR(-ENOMEM);
566 path = dm_shift_arg(as);
567 r = dm_get_device(ti, path, dm_table_get_mode(ti->table),
568 &p->path.dev);
569 if (r) {
570 unsigned major, minor;
572 /* Try to add a failed device */
573 if (r == -ENXIO && sscanf(path, "%u:%u", &major, &minor) == 2) {
574 dev_t dev;
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";
582 goto bad;
584 DMWARN("adding disabled device %d:%d", major, minor);
585 p->path.dev = NULL;
586 format_dev_t(p->path.pdev, dev);
587 p->is_active = 0;
588 } else {
589 ti->error = "error getting device";
590 goto bad;
592 } else {
593 memcpy(p->path.pdev, p->path.dev->name, 16);
596 if (p->path.dev) {
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);
601 if (r == -EBUSY) {
603 * Already attached to different hw_handler,
604 * try to reattach with correct one.
606 scsi_dh_detach(q);
607 r = scsi_dh_attach(q, m->hw_handler_name);
609 if (r < 0) {
610 ti->error = "error attaching hardware handler";
611 dm_put_device(ti, p->path.dev);
612 goto bad;
614 } else {
615 /* Play safe and detach hardware handler */
616 scsi_dh_detach(q);
619 if (m->hw_handler_params) {
620 r = scsi_dh_set_params(q, m->hw_handler_params);
621 if (r < 0) {
622 ti->error = "unable to set hardware "
623 "handler parameters";
624 scsi_dh_detach(q);
625 dm_put_device(ti, p->path.dev);
626 goto bad;
631 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
632 if (r) {
633 dm_put_device(ti, p->path.dev);
634 goto bad;
637 if (!p->is_active) {
638 ps->type->fail_path(ps, &p->path);
639 p->fail_count++;
640 m->nr_valid_paths--;
642 return p;
644 bad:
645 free_pgpath(p);
646 return ERR_PTR(r);
649 static struct priority_group *parse_priority_group(struct dm_arg_set *as,
650 struct multipath *m)
652 static struct dm_arg _args[] = {
653 {1, 1024, "invalid number of paths"},
654 {0, 1024, "invalid number of selector args"}
657 int r;
658 unsigned i, nr_selector_args, nr_args;
659 struct priority_group *pg;
660 struct dm_target *ti = m->ti;
662 if (as->argc < 2) {
663 as->argc = 0;
664 ti->error = "not enough priority group arguments";
665 return ERR_PTR(-EINVAL);
668 pg = alloc_priority_group();
669 if (!pg) {
670 ti->error = "couldn't allocate priority group";
671 return ERR_PTR(-ENOMEM);
673 pg->m = m;
675 r = parse_path_selector(as, pg, ti);
676 if (r)
677 goto bad;
680 * read the paths
682 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
683 if (r)
684 goto bad;
686 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
687 if (r)
688 goto bad;
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";
697 r = -EINVAL;
698 goto bad;
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)) {
706 r = PTR_ERR(pgpath);
707 goto bad;
710 pgpath->pg = pg;
711 list_add_tail(&pgpath->list, &pg->pgpaths);
712 dm_consume_args(as, nr_args);
715 return pg;
717 bad:
718 free_priority_group(pg, ti);
719 return ERR_PTR(r);
722 static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
724 unsigned hw_argc;
725 int ret;
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))
733 return -EINVAL;
735 if (!hw_argc)
736 return 0;
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";
742 ret = -EINVAL;
743 goto fail;
746 if (hw_argc > 1) {
747 char *p;
748 int i, j, len = 4;
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);
753 if (!p) {
754 ti->error = "memory allocation failed";
755 ret = -ENOMEM;
756 goto fail;
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);
764 return 0;
765 fail:
766 kfree(m->hw_handler_name);
767 m->hw_handler_name = NULL;
768 return ret;
771 static int parse_features(struct dm_arg_set *as, struct multipath *m)
773 int r;
774 unsigned argc;
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);
785 if (r)
786 return -EINVAL;
788 if (!argc)
789 return 0;
791 do {
792 arg_name = dm_shift_arg(as);
793 argc--;
795 if (!strcasecmp(arg_name, "queue_if_no_path")) {
796 r = queue_if_no_path(m, 1, 0);
797 continue;
800 if (!strcasecmp(arg_name, "no_partitions")) {
801 m->features |= FEATURE_NO_PARTITIONS;
802 continue;
804 if (!strcasecmp(arg_name, "pg_init_retries") &&
805 (argc >= 1)) {
806 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
807 argc--;
808 continue;
811 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
812 (argc >= 1)) {
813 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
814 argc--;
815 continue;
818 ti->error = "Unrecognised multipath feature request";
819 r = -EINVAL;
820 } while (argc && !r);
822 return r;
825 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
826 char **argv)
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"},
834 int r;
835 struct multipath *m;
836 struct dm_arg_set as;
837 unsigned pg_count = 0;
838 unsigned next_pg_num;
840 as.argc = argc;
841 as.argv = argv;
843 m = alloc_multipath(ti);
844 if (!m) {
845 ti->error = "can't allocate multipath";
846 return -EINVAL;
849 r = parse_features(&as, m);
850 if (r)
851 goto bad;
853 r = parse_hw_handler(&as, m);
854 if (r)
855 goto bad;
857 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
858 if (r)
859 goto bad;
861 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
862 if (r)
863 goto bad;
865 if ((!m->nr_priority_groups && next_pg_num) ||
866 (m->nr_priority_groups && !next_pg_num)) {
867 ti->error = "invalid initial priority group";
868 r = -EINVAL;
869 goto bad;
872 /* parse the priority groups */
873 while (as.argc) {
874 struct priority_group *pg;
876 pg = parse_priority_group(&as, m);
877 if (IS_ERR(pg)) {
878 r = PTR_ERR(pg);
879 goto bad;
882 m->nr_valid_paths += pg->nr_pgpaths;
883 list_add_tail(&pg->list, &m->priority_groups);
884 pg_count++;
885 pg->pg_num = pg_count;
886 if (!--next_pg_num)
887 m->next_pg = pg;
890 if (pg_count != m->nr_priority_groups) {
891 ti->error = "priority group count mismatch";
892 r = -EINVAL;
893 goto bad;
896 ti->num_flush_requests = 1;
897 ti->num_discard_requests = 1;
899 return 0;
901 bad:
902 free_multipath(m);
903 return r;
906 static void multipath_wait_for_pg_init_completion(struct multipath *m)
908 DECLARE_WAITQUEUE(wait, current);
909 unsigned long flags;
911 add_wait_queue(&m->pg_init_wait, &wait);
913 while (1) {
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);
919 break;
921 spin_unlock_irqrestore(&m->lock, flags);
923 io_schedule();
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);
943 free_multipath(m);
947 * Map cloned requests
949 static int multipath_map(struct dm_target *ti, struct request *clone,
950 union map_info *map_context)
952 int r;
953 struct dm_mpath_io *mpio;
954 struct multipath *m = (struct multipath *) ti->private;
956 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
957 if (!mpio)
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);
968 return r;
972 * Take a path out of use.
974 static int fail_path(struct pgpath *pgpath)
976 unsigned long flags;
977 struct multipath *m = pgpath->pg->m;
979 spin_lock_irqsave(&m->lock, flags);
981 if (!pgpath->is_active)
982 goto out;
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++;
990 m->nr_valid_paths--;
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);
1000 out:
1001 spin_unlock_irqrestore(&m->lock, flags);
1003 return 0;
1007 * Reinstate a previously-failed path
1009 static int reinstate_path(struct pgpath *pgpath)
1011 int r = 0;
1012 unsigned long flags;
1013 struct multipath *m = pgpath->pg->m;
1015 spin_lock_irqsave(&m->lock, flags);
1017 if (pgpath->is_active)
1018 goto out;
1020 if (!pgpath->path.dev) {
1021 DMWARN("Cannot reinstate disabled path %s", pgpath->path.pdev);
1022 r = -ENODEV;
1023 goto out;
1026 if (!pgpath->pg->ps.type->reinstate_path) {
1027 DMWARN("Reinstate path not supported by path selector %s",
1028 pgpath->pg->ps.type->name);
1029 r = -EINVAL;
1030 goto out;
1033 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1034 if (r)
1035 goto out;
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);
1052 out:
1053 spin_unlock_irqrestore(&m->lock, flags);
1055 return r;
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,
1062 action_fn action)
1064 int r = -EINVAL;
1065 struct pgpath *pgpath;
1066 struct priority_group *pg;
1068 if (!dev)
1069 return 0;
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)
1074 r = action(pgpath);
1078 return r;
1082 * Temporarily try to avoid having to use the specified PG
1084 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1085 int bypassed)
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;
1106 unsigned pgnum;
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");
1112 return -EINVAL;
1115 spin_lock_irqsave(&m->lock, flags);
1116 list_for_each_entry(pg, &m->priority_groups, list) {
1117 pg->bypassed = 0;
1118 if (--pgnum)
1119 continue;
1121 m->current_pgpath = NULL;
1122 m->current_pg = NULL;
1123 m->next_pg = pg;
1125 spin_unlock_irqrestore(&m->lock, flags);
1127 schedule_work(&m->trigger_event);
1128 return 0;
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;
1138 unsigned pgnum;
1140 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1141 (pgnum > m->nr_priority_groups)) {
1142 DMWARN("invalid PG number supplied to bypass_pg");
1143 return -EINVAL;
1146 list_for_each_entry(pg, &m->priority_groups, list) {
1147 if (!--pgnum)
1148 break;
1151 bypass_pg(m, pg, bypassed);
1152 return 0;
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;
1167 else
1168 limit_reached = 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 */
1184 switch (errors) {
1185 case SCSI_DH_OK:
1186 break;
1187 case SCSI_DH_NOSYS:
1188 if (!m->hw_handler_name) {
1189 errors = 0;
1190 break;
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
1198 fail_path(pgpath);
1199 break;
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);
1206 break;
1207 case SCSI_DH_DEV_OFFLINED:
1208 DMWARN("Device %s offlined.", pgpath->path.dev->name);
1209 errors = 0;
1210 break;
1211 case SCSI_DH_RETRY:
1212 /* Wait before retrying. */
1213 delay_retry = 1;
1214 case SCSI_DH_IMM_RETRY:
1215 case SCSI_DH_RES_TEMP_UNAVAIL:
1216 if (pg_init_limit_reached(m, pgpath))
1217 fail_path(pgpath);
1218 errors = 0;
1219 break;
1220 default:
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.
1226 fail_path(pgpath);
1229 spin_lock_irqsave(&m->lock, flags);
1230 if (errors) {
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)
1238 pg->bypassed = 0;
1240 if (--m->pg_init_in_progress)
1241 /* Activations of other paths are still on going */
1242 goto out;
1244 if (!m->pg_init_required)
1245 m->queue_io = 0;
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);
1255 out:
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);
1270 * end_io handling
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)
1293 return error;
1295 if (mpio->pgpath)
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))
1302 r = -EIO;
1303 } else {
1304 if (error == -EBADE)
1305 r = error;
1308 spin_unlock_irqrestore(&m->lock, flags);
1310 return r;
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;
1320 int r;
1322 r = do_end_io(m, clone, error, mpio);
1323 if (pgpath) {
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);
1330 return r;
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)
1387 int sz = 0;
1388 unsigned long flags;
1389 struct multipath *m = (struct multipath *) ti->private;
1390 struct priority_group *pg;
1391 struct pgpath *p;
1392 unsigned pg_num;
1393 char state;
1395 spin_lock_irqsave(&m->lock, flags);
1397 /* Features */
1398 if (type == STATUSTYPE_INFO)
1399 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1400 else {
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)
1416 DMEMIT("0 ");
1417 else
1418 DMEMIT("1 %s ", m->hw_handler_name);
1420 DMEMIT("%u ", m->nr_priority_groups);
1422 if (m->next_pg)
1423 pg_num = m->next_pg->pg_num;
1424 else if (m->current_pg)
1425 pg_num = m->current_pg->pg_num;
1426 else
1427 pg_num = (m->nr_priority_groups ? 1 : 0);
1429 DMEMIT("%u ", pg_num);
1431 switch (type) {
1432 case STATUSTYPE_INFO:
1433 list_for_each_entry(pg, &m->priority_groups, list) {
1434 if (pg->bypassed)
1435 state = 'D'; /* Disabled */
1436 else if (pg == m->current_pg)
1437 state = 'A'; /* Currently Active */
1438 else
1439 state = 'E'; /* Enabled */
1441 DMEMIT("%c ", state);
1443 if (pg->ps.type->status)
1444 sz += pg->ps.type->status(&pg->ps, NULL, type,
1445 result + sz,
1446 maxlen - sz);
1447 else
1448 DMEMIT("0 ");
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",
1456 p->fail_count);
1457 if (pg->ps.type->status)
1458 sz += pg->ps.type->status(&pg->ps,
1459 &p->path, type, result + sz,
1460 maxlen - sz);
1463 break;
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,
1471 result + sz,
1472 maxlen - sz);
1473 else
1474 DMEMIT("0 ");
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,
1484 maxlen - sz);
1487 break;
1490 spin_unlock_irqrestore(&m->lock, flags);
1492 return 0;
1495 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1497 int r = -EINVAL;
1498 struct dm_dev *dev;
1499 struct multipath *m = (struct multipath *) ti->private;
1500 action_fn action;
1502 mutex_lock(&m->work_mutex);
1504 if (dm_suspended(ti)) {
1505 r = -EBUSY;
1506 goto out;
1509 if (argc == 1) {
1510 if (!strcasecmp(argv[0], "queue_if_no_path")) {
1511 r = queue_if_no_path(m, 1, 0);
1512 goto out;
1513 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1514 r = queue_if_no_path(m, 0, 0);
1515 goto out;
1519 if (argc != 2) {
1520 DMWARN("Unrecognised multipath message received.");
1521 goto out;
1524 if (!strcasecmp(argv[0], "disable_group")) {
1525 r = bypass_pg_num(m, argv[1], 1);
1526 goto out;
1527 } else if (!strcasecmp(argv[0], "enable_group")) {
1528 r = bypass_pg_num(m, argv[1], 0);
1529 goto out;
1530 } else if (!strcasecmp(argv[0], "switch_group")) {
1531 r = switch_pg_num(m, argv[1]);
1532 goto out;
1533 } else if (!strcasecmp(argv[0], "reinstate_path"))
1534 action = reinstate_path;
1535 else if (!strcasecmp(argv[0], "fail_path"))
1536 action = fail_path;
1537 else {
1538 DMWARN("Unrecognised multipath message received.");
1539 goto out;
1542 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1543 if (r) {
1544 DMWARN("message: error getting device %s",
1545 argv[1]);
1546 goto out;
1549 r = action_dev(m, dev, action);
1551 dm_put_device(ti, dev);
1553 out:
1554 mutex_unlock(&m->work_mutex);
1555 return r;
1558 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1559 unsigned long arg)
1561 struct multipath *m = (struct multipath *) ti->private;
1562 struct block_device *bdev = NULL;
1563 fmode_t mode = 0;
1564 unsigned long flags;
1565 int r = 0;
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;
1577 if (m->queue_io)
1578 r = -EAGAIN;
1579 else if (!bdev)
1580 r = -EIO;
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;
1598 struct pgpath *p;
1599 int ret = 0;
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);
1604 if (ret)
1605 goto out;
1609 out:
1610 return ret;
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))
1640 pg = m->next_pg;
1641 else if (likely(m->current_pg))
1642 pg = m->current_pg;
1643 else
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.
1651 goto out;
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.
1657 busy = 1;
1658 list_for_each_entry(pgpath, &pg->pgpaths, list)
1659 if (pgpath->is_active) {
1660 has_active = 1;
1662 if (!__pgpath_busy(pgpath)) {
1663 busy = 0;
1664 break;
1668 if (!has_active)
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.
1674 busy = 0;
1676 out:
1677 spin_unlock_irqrestore(&m->lock, flags);
1679 return busy;
1682 /*-----------------------------------------------------------------
1683 * Module setup
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)
1705 int r;
1707 /* allocate a slab for the dm_ios */
1708 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1709 if (!_mpio_cache)
1710 return -ENOMEM;
1712 r = dm_register_target(&multipath_target);
1713 if (r < 0) {
1714 DMERR("register failed %d", r);
1715 kmem_cache_destroy(_mpio_cache);
1716 return -EINVAL;
1719 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1720 if (!kmultipathd) {
1721 DMERR("failed to create workqueue kmpathd");
1722 dm_unregister_target(&multipath_target);
1723 kmem_cache_destroy(_mpio_cache);
1724 return -ENOMEM;
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",
1734 WQ_MEM_RECLAIM);
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);
1740 return -ENOMEM;
1743 DMINFO("version %u.%u.%u loaded",
1744 multipath_target.version[0], multipath_target.version[1],
1745 multipath_target.version[2]);
1747 return r;
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");