smc911x: Make the driver safer on SMP
[linux-2.6/libata-dev.git] / drivers / md / dm-mpath.c
blob9bf3460c5540b9fdbefbacaed15788f7a6868fb0
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 "dm.h"
9 #include "dm-path-selector.h"
10 #include "dm-bio-list.h"
11 #include "dm-bio-record.h"
12 #include "dm-uevent.h"
14 #include <linux/ctype.h>
15 #include <linux/init.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/time.h>
21 #include <linux/workqueue.h>
22 #include <scsi/scsi_dh.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define MESG_STR(x) x, sizeof(x)
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 work_struct deactivate_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 */
60 struct multipath {
61 struct list_head list;
62 struct dm_target *ti;
64 spinlock_t lock;
66 const char *hw_handler_name;
67 struct work_struct activate_path;
68 struct pgpath *pgpath_to_activate;
69 unsigned nr_priority_groups;
70 struct list_head priority_groups;
71 unsigned pg_init_required; /* pg_init needs calling? */
72 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
74 unsigned nr_valid_paths; /* Total number of usable paths */
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg; /* Switch to this PG if set */
78 unsigned repeat_count; /* I/Os left before calling PS again */
80 unsigned queue_io; /* Must we queue all I/O? */
81 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
82 unsigned saved_queue_if_no_path;/* Saved state during suspension */
83 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_count; /* Number of times pg_init called */
86 struct work_struct process_queued_ios;
87 struct bio_list queued_ios;
88 unsigned queue_size;
90 struct work_struct trigger_event;
93 * We must use a mempool of dm_mpath_io structs so that we
94 * can resubmit bios on error.
96 mempool_t *mpio_pool;
100 * Context information attached to each bio we process.
102 struct dm_mpath_io {
103 struct pgpath *pgpath;
104 struct dm_bio_details details;
107 typedef int (*action_fn) (struct pgpath *pgpath);
109 #define MIN_IOS 256 /* Mempool size */
111 static struct kmem_cache *_mpio_cache;
113 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
114 static void process_queued_ios(struct work_struct *work);
115 static void trigger_event(struct work_struct *work);
116 static void activate_path(struct work_struct *work);
117 static void deactivate_path(struct work_struct *work);
120 /*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
124 static struct pgpath *alloc_pgpath(void)
126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
128 if (pgpath) {
129 pgpath->is_active = 1;
130 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
133 return pgpath;
136 static void free_pgpath(struct pgpath *pgpath)
138 kfree(pgpath);
141 static void deactivate_path(struct work_struct *work)
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
149 static struct priority_group *alloc_priority_group(void)
151 struct priority_group *pg;
153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
155 if (pg)
156 INIT_LIST_HEAD(&pg->pgpaths);
158 return pg;
161 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163 unsigned long flags;
164 struct pgpath *pgpath, *tmp;
165 struct multipath *m = ti->private;
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
171 dm_put_device(ti, pgpath->path.dev);
172 spin_lock_irqsave(&m->lock, flags);
173 if (m->pgpath_to_activate == pgpath)
174 m->pgpath_to_activate = NULL;
175 spin_unlock_irqrestore(&m->lock, flags);
176 free_pgpath(pgpath);
180 static void free_priority_group(struct priority_group *pg,
181 struct dm_target *ti)
183 struct path_selector *ps = &pg->ps;
185 if (ps->type) {
186 ps->type->destroy(ps);
187 dm_put_path_selector(ps->type);
190 free_pgpaths(&pg->pgpaths, ti);
191 kfree(pg);
194 static struct multipath *alloc_multipath(struct dm_target *ti)
196 struct multipath *m;
198 m = kzalloc(sizeof(*m), GFP_KERNEL);
199 if (m) {
200 INIT_LIST_HEAD(&m->priority_groups);
201 spin_lock_init(&m->lock);
202 m->queue_io = 1;
203 INIT_WORK(&m->process_queued_ios, process_queued_ios);
204 INIT_WORK(&m->trigger_event, trigger_event);
205 INIT_WORK(&m->activate_path, activate_path);
206 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
207 if (!m->mpio_pool) {
208 kfree(m);
209 return NULL;
211 m->ti = ti;
212 ti->private = m;
215 return m;
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) {
223 list_del(&pg->list);
224 free_priority_group(pg, m->ti);
227 kfree(m->hw_handler_name);
228 mempool_destroy(m->mpio_pool);
229 kfree(m);
233 /*-----------------------------------------------
234 * Path selection
235 *-----------------------------------------------*/
237 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
239 m->current_pg = pgpath->pg;
241 /* Must we initialise the PG first, and queue I/O till it's ready? */
242 if (m->hw_handler_name) {
243 m->pg_init_required = 1;
244 m->queue_io = 1;
245 } else {
246 m->pg_init_required = 0;
247 m->queue_io = 0;
250 m->pg_init_count = 0;
253 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
255 struct dm_path *path;
257 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
258 if (!path)
259 return -ENXIO;
261 m->current_pgpath = path_to_pgpath(path);
263 if (m->current_pg != pg)
264 __switch_pg(m, m->current_pgpath);
266 return 0;
269 static void __choose_pgpath(struct multipath *m)
271 struct priority_group *pg;
272 unsigned bypassed = 1;
274 if (!m->nr_valid_paths)
275 goto failed;
277 /* Were we instructed to switch PG? */
278 if (m->next_pg) {
279 pg = m->next_pg;
280 m->next_pg = NULL;
281 if (!__choose_path_in_pg(m, pg))
282 return;
285 /* Don't change PG until it has no remaining paths */
286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
287 return;
290 * Loop through priority groups until we find a valid path.
291 * First time we skip PGs marked 'bypassed'.
292 * Second time we only try the ones we skipped.
294 do {
295 list_for_each_entry(pg, &m->priority_groups, list) {
296 if (pg->bypassed == bypassed)
297 continue;
298 if (!__choose_path_in_pg(m, pg))
299 return;
301 } while (bypassed--);
303 failed:
304 m->current_pgpath = NULL;
305 m->current_pg = NULL;
309 * Check whether bios must be queued in the device-mapper core rather
310 * than here in the target.
312 * m->lock must be held on entry.
314 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
315 * same value then we are not between multipath_presuspend()
316 * and multipath_resume() calls and we have no need to check
317 * for the DMF_NOFLUSH_SUSPENDING flag.
319 static int __must_push_back(struct multipath *m)
321 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
322 dm_noflush_suspending(m->ti));
325 static int map_io(struct multipath *m, struct bio *bio,
326 struct dm_mpath_io *mpio, unsigned was_queued)
328 int r = DM_MAPIO_REMAPPED;
329 unsigned long flags;
330 struct pgpath *pgpath;
332 spin_lock_irqsave(&m->lock, flags);
334 /* Do we need to select a new pgpath? */
335 if (!m->current_pgpath ||
336 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
337 __choose_pgpath(m);
339 pgpath = m->current_pgpath;
341 if (was_queued)
342 m->queue_size--;
344 if ((pgpath && m->queue_io) ||
345 (!pgpath && m->queue_if_no_path)) {
346 /* Queue for the daemon to resubmit */
347 bio_list_add(&m->queued_ios, bio);
348 m->queue_size++;
349 if ((m->pg_init_required && !m->pg_init_in_progress) ||
350 !m->queue_io)
351 queue_work(kmultipathd, &m->process_queued_ios);
352 pgpath = NULL;
353 r = DM_MAPIO_SUBMITTED;
354 } else if (pgpath)
355 bio->bi_bdev = pgpath->path.dev->bdev;
356 else if (__must_push_back(m))
357 r = DM_MAPIO_REQUEUE;
358 else
359 r = -EIO; /* Failed */
361 mpio->pgpath = pgpath;
363 spin_unlock_irqrestore(&m->lock, flags);
365 return r;
369 * If we run out of usable paths, should we queue I/O or error it?
371 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
372 unsigned save_old_value)
374 unsigned long flags;
376 spin_lock_irqsave(&m->lock, flags);
378 if (save_old_value)
379 m->saved_queue_if_no_path = m->queue_if_no_path;
380 else
381 m->saved_queue_if_no_path = queue_if_no_path;
382 m->queue_if_no_path = queue_if_no_path;
383 if (!m->queue_if_no_path && m->queue_size)
384 queue_work(kmultipathd, &m->process_queued_ios);
386 spin_unlock_irqrestore(&m->lock, flags);
388 return 0;
391 /*-----------------------------------------------------------------
392 * The multipath daemon is responsible for resubmitting queued ios.
393 *---------------------------------------------------------------*/
395 static void dispatch_queued_ios(struct multipath *m)
397 int r;
398 unsigned long flags;
399 struct bio *bio = NULL, *next;
400 struct dm_mpath_io *mpio;
401 union map_info *info;
403 spin_lock_irqsave(&m->lock, flags);
404 bio = bio_list_get(&m->queued_ios);
405 spin_unlock_irqrestore(&m->lock, flags);
407 while (bio) {
408 next = bio->bi_next;
409 bio->bi_next = NULL;
411 info = dm_get_mapinfo(bio);
412 mpio = info->ptr;
414 r = map_io(m, bio, mpio, 1);
415 if (r < 0)
416 bio_endio(bio, r);
417 else if (r == DM_MAPIO_REMAPPED)
418 generic_make_request(bio);
419 else if (r == DM_MAPIO_REQUEUE)
420 bio_endio(bio, -EIO);
422 bio = next;
426 static void process_queued_ios(struct work_struct *work)
428 struct multipath *m =
429 container_of(work, struct multipath, process_queued_ios);
430 struct pgpath *pgpath = NULL;
431 unsigned init_required = 0, must_queue = 1;
432 unsigned long flags;
434 spin_lock_irqsave(&m->lock, flags);
436 if (!m->queue_size)
437 goto out;
439 if (!m->current_pgpath)
440 __choose_pgpath(m);
442 pgpath = m->current_pgpath;
443 m->pgpath_to_activate = m->current_pgpath;
445 if ((pgpath && !m->queue_io) ||
446 (!pgpath && !m->queue_if_no_path))
447 must_queue = 0;
449 if (m->pg_init_required && !m->pg_init_in_progress) {
450 m->pg_init_count++;
451 m->pg_init_required = 0;
452 m->pg_init_in_progress = 1;
453 init_required = 1;
456 out:
457 spin_unlock_irqrestore(&m->lock, flags);
459 if (init_required)
460 queue_work(kmpath_handlerd, &m->activate_path);
462 if (!must_queue)
463 dispatch_queued_ios(m);
467 * An event is triggered whenever a path is taken out of use.
468 * Includes path failure and PG bypass.
470 static void trigger_event(struct work_struct *work)
472 struct multipath *m =
473 container_of(work, struct multipath, trigger_event);
475 dm_table_event(m->ti->table);
478 /*-----------------------------------------------------------------
479 * Constructor/argument parsing:
480 * <#multipath feature args> [<arg>]*
481 * <#hw_handler args> [hw_handler [<arg>]*]
482 * <#priority groups>
483 * <initial priority group>
484 * [<selector> <#selector args> [<arg>]*
485 * <#paths> <#per-path selector args>
486 * [<path> [<arg>]* ]+ ]+
487 *---------------------------------------------------------------*/
488 struct param {
489 unsigned min;
490 unsigned max;
491 char *error;
494 static int read_param(struct param *param, char *str, unsigned *v, char **error)
496 if (!str ||
497 (sscanf(str, "%u", v) != 1) ||
498 (*v < param->min) ||
499 (*v > param->max)) {
500 *error = param->error;
501 return -EINVAL;
504 return 0;
507 struct arg_set {
508 unsigned argc;
509 char **argv;
512 static char *shift(struct arg_set *as)
514 char *r;
516 if (as->argc) {
517 as->argc--;
518 r = *as->argv;
519 as->argv++;
520 return r;
523 return NULL;
526 static void consume(struct arg_set *as, unsigned n)
528 BUG_ON (as->argc < n);
529 as->argc -= n;
530 as->argv += n;
533 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
534 struct dm_target *ti)
536 int r;
537 struct path_selector_type *pst;
538 unsigned ps_argc;
540 static struct param _params[] = {
541 {0, 1024, "invalid number of path selector args"},
544 pst = dm_get_path_selector(shift(as));
545 if (!pst) {
546 ti->error = "unknown path selector type";
547 return -EINVAL;
550 r = read_param(_params, shift(as), &ps_argc, &ti->error);
551 if (r) {
552 dm_put_path_selector(pst);
553 return -EINVAL;
556 r = pst->create(&pg->ps, ps_argc, as->argv);
557 if (r) {
558 dm_put_path_selector(pst);
559 ti->error = "path selector constructor failed";
560 return r;
563 pg->ps.type = pst;
564 consume(as, ps_argc);
566 return 0;
569 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
570 struct dm_target *ti)
572 int r;
573 struct pgpath *p;
574 struct multipath *m = ti->private;
576 /* we need at least a path arg */
577 if (as->argc < 1) {
578 ti->error = "no device given";
579 return ERR_PTR(-EINVAL);
582 p = alloc_pgpath();
583 if (!p)
584 return ERR_PTR(-ENOMEM);
586 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
587 dm_table_get_mode(ti->table), &p->path.dev);
588 if (r) {
589 ti->error = "error getting device";
590 goto bad;
593 if (m->hw_handler_name) {
594 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
595 m->hw_handler_name);
596 if (r < 0) {
597 dm_put_device(ti, p->path.dev);
598 goto bad;
602 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
603 if (r) {
604 dm_put_device(ti, p->path.dev);
605 goto bad;
608 return p;
610 bad:
611 free_pgpath(p);
612 return ERR_PTR(r);
615 static struct priority_group *parse_priority_group(struct arg_set *as,
616 struct multipath *m)
618 static struct param _params[] = {
619 {1, 1024, "invalid number of paths"},
620 {0, 1024, "invalid number of selector args"}
623 int r;
624 unsigned i, nr_selector_args, nr_params;
625 struct priority_group *pg;
626 struct dm_target *ti = m->ti;
628 if (as->argc < 2) {
629 as->argc = 0;
630 ti->error = "not enough priority group arguments";
631 return ERR_PTR(-EINVAL);
634 pg = alloc_priority_group();
635 if (!pg) {
636 ti->error = "couldn't allocate priority group";
637 return ERR_PTR(-ENOMEM);
639 pg->m = m;
641 r = parse_path_selector(as, pg, ti);
642 if (r)
643 goto bad;
646 * read the paths
648 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
649 if (r)
650 goto bad;
652 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
653 if (r)
654 goto bad;
656 nr_params = 1 + nr_selector_args;
657 for (i = 0; i < pg->nr_pgpaths; i++) {
658 struct pgpath *pgpath;
659 struct arg_set path_args;
661 if (as->argc < nr_params) {
662 ti->error = "not enough path parameters";
663 goto bad;
666 path_args.argc = nr_params;
667 path_args.argv = as->argv;
669 pgpath = parse_path(&path_args, &pg->ps, ti);
670 if (IS_ERR(pgpath)) {
671 r = PTR_ERR(pgpath);
672 goto bad;
675 pgpath->pg = pg;
676 list_add_tail(&pgpath->list, &pg->pgpaths);
677 consume(as, nr_params);
680 return pg;
682 bad:
683 free_priority_group(pg, ti);
684 return ERR_PTR(r);
687 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
689 unsigned hw_argc;
690 struct dm_target *ti = m->ti;
692 static struct param _params[] = {
693 {0, 1024, "invalid number of hardware handler args"},
696 if (read_param(_params, shift(as), &hw_argc, &ti->error))
697 return -EINVAL;
699 if (!hw_argc)
700 return 0;
702 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
703 request_module("scsi_dh_%s", m->hw_handler_name);
704 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
705 ti->error = "unknown hardware handler type";
706 kfree(m->hw_handler_name);
707 m->hw_handler_name = NULL;
708 return -EINVAL;
710 consume(as, hw_argc - 1);
712 return 0;
715 static int parse_features(struct arg_set *as, struct multipath *m)
717 int r;
718 unsigned argc;
719 struct dm_target *ti = m->ti;
720 const char *param_name;
722 static struct param _params[] = {
723 {0, 3, "invalid number of feature args"},
724 {1, 50, "pg_init_retries must be between 1 and 50"},
727 r = read_param(_params, shift(as), &argc, &ti->error);
728 if (r)
729 return -EINVAL;
731 if (!argc)
732 return 0;
734 do {
735 param_name = shift(as);
736 argc--;
738 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
739 r = queue_if_no_path(m, 1, 0);
740 continue;
743 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
744 (argc >= 1)) {
745 r = read_param(_params + 1, shift(as),
746 &m->pg_init_retries, &ti->error);
747 argc--;
748 continue;
751 ti->error = "Unrecognised multipath feature request";
752 r = -EINVAL;
753 } while (argc && !r);
755 return r;
758 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
759 char **argv)
761 /* target parameters */
762 static struct param _params[] = {
763 {1, 1024, "invalid number of priority groups"},
764 {1, 1024, "invalid initial priority group number"},
767 int r;
768 struct multipath *m;
769 struct arg_set as;
770 unsigned pg_count = 0;
771 unsigned next_pg_num;
773 as.argc = argc;
774 as.argv = argv;
776 m = alloc_multipath(ti);
777 if (!m) {
778 ti->error = "can't allocate multipath";
779 return -EINVAL;
782 r = parse_features(&as, m);
783 if (r)
784 goto bad;
786 r = parse_hw_handler(&as, m);
787 if (r)
788 goto bad;
790 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
791 if (r)
792 goto bad;
794 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
795 if (r)
796 goto bad;
798 /* parse the priority groups */
799 while (as.argc) {
800 struct priority_group *pg;
802 pg = parse_priority_group(&as, m);
803 if (IS_ERR(pg)) {
804 r = PTR_ERR(pg);
805 goto bad;
808 m->nr_valid_paths += pg->nr_pgpaths;
809 list_add_tail(&pg->list, &m->priority_groups);
810 pg_count++;
811 pg->pg_num = pg_count;
812 if (!--next_pg_num)
813 m->next_pg = pg;
816 if (pg_count != m->nr_priority_groups) {
817 ti->error = "priority group count mismatch";
818 r = -EINVAL;
819 goto bad;
822 return 0;
824 bad:
825 free_multipath(m);
826 return r;
829 static void multipath_dtr(struct dm_target *ti)
831 struct multipath *m = (struct multipath *) ti->private;
833 flush_workqueue(kmpath_handlerd);
834 flush_workqueue(kmultipathd);
835 free_multipath(m);
839 * Map bios, recording original fields for later in case we have to resubmit
841 static int multipath_map(struct dm_target *ti, struct bio *bio,
842 union map_info *map_context)
844 int r;
845 struct dm_mpath_io *mpio;
846 struct multipath *m = (struct multipath *) ti->private;
848 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
849 dm_bio_record(&mpio->details, bio);
851 map_context->ptr = mpio;
852 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
853 r = map_io(m, bio, mpio, 0);
854 if (r < 0 || r == DM_MAPIO_REQUEUE)
855 mempool_free(mpio, m->mpio_pool);
857 return r;
861 * Take a path out of use.
863 static int fail_path(struct pgpath *pgpath)
865 unsigned long flags;
866 struct multipath *m = pgpath->pg->m;
868 spin_lock_irqsave(&m->lock, flags);
870 if (!pgpath->is_active)
871 goto out;
873 DMWARN("Failing path %s.", pgpath->path.dev->name);
875 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
876 pgpath->is_active = 0;
877 pgpath->fail_count++;
879 m->nr_valid_paths--;
881 if (pgpath == m->current_pgpath)
882 m->current_pgpath = NULL;
884 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
885 pgpath->path.dev->name, m->nr_valid_paths);
887 queue_work(kmultipathd, &m->trigger_event);
888 queue_work(kmultipathd, &pgpath->deactivate_path);
890 out:
891 spin_unlock_irqrestore(&m->lock, flags);
893 return 0;
897 * Reinstate a previously-failed path
899 static int reinstate_path(struct pgpath *pgpath)
901 int r = 0;
902 unsigned long flags;
903 struct multipath *m = pgpath->pg->m;
905 spin_lock_irqsave(&m->lock, flags);
907 if (pgpath->is_active)
908 goto out;
910 if (!pgpath->pg->ps.type->reinstate_path) {
911 DMWARN("Reinstate path not supported by path selector %s",
912 pgpath->pg->ps.type->name);
913 r = -EINVAL;
914 goto out;
917 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
918 if (r)
919 goto out;
921 pgpath->is_active = 1;
923 m->current_pgpath = NULL;
924 if (!m->nr_valid_paths++ && m->queue_size)
925 queue_work(kmultipathd, &m->process_queued_ios);
927 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
928 pgpath->path.dev->name, m->nr_valid_paths);
930 queue_work(kmultipathd, &m->trigger_event);
932 out:
933 spin_unlock_irqrestore(&m->lock, flags);
935 return r;
939 * Fail or reinstate all paths that match the provided struct dm_dev.
941 static int action_dev(struct multipath *m, struct dm_dev *dev,
942 action_fn action)
944 int r = 0;
945 struct pgpath *pgpath;
946 struct priority_group *pg;
948 list_for_each_entry(pg, &m->priority_groups, list) {
949 list_for_each_entry(pgpath, &pg->pgpaths, list) {
950 if (pgpath->path.dev == dev)
951 r = action(pgpath);
955 return r;
959 * Temporarily try to avoid having to use the specified PG
961 static void bypass_pg(struct multipath *m, struct priority_group *pg,
962 int bypassed)
964 unsigned long flags;
966 spin_lock_irqsave(&m->lock, flags);
968 pg->bypassed = bypassed;
969 m->current_pgpath = NULL;
970 m->current_pg = NULL;
972 spin_unlock_irqrestore(&m->lock, flags);
974 queue_work(kmultipathd, &m->trigger_event);
978 * Switch to using the specified PG from the next I/O that gets mapped
980 static int switch_pg_num(struct multipath *m, const char *pgstr)
982 struct priority_group *pg;
983 unsigned pgnum;
984 unsigned long flags;
986 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
987 (pgnum > m->nr_priority_groups)) {
988 DMWARN("invalid PG number supplied to switch_pg_num");
989 return -EINVAL;
992 spin_lock_irqsave(&m->lock, flags);
993 list_for_each_entry(pg, &m->priority_groups, list) {
994 pg->bypassed = 0;
995 if (--pgnum)
996 continue;
998 m->current_pgpath = NULL;
999 m->current_pg = NULL;
1000 m->next_pg = pg;
1002 spin_unlock_irqrestore(&m->lock, flags);
1004 queue_work(kmultipathd, &m->trigger_event);
1005 return 0;
1009 * Set/clear bypassed status of a PG.
1010 * PGs are numbered upwards from 1 in the order they were declared.
1012 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1014 struct priority_group *pg;
1015 unsigned pgnum;
1017 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1018 (pgnum > m->nr_priority_groups)) {
1019 DMWARN("invalid PG number supplied to bypass_pg");
1020 return -EINVAL;
1023 list_for_each_entry(pg, &m->priority_groups, list) {
1024 if (!--pgnum)
1025 break;
1028 bypass_pg(m, pg, bypassed);
1029 return 0;
1033 * Should we retry pg_init immediately?
1035 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1037 unsigned long flags;
1038 int limit_reached = 0;
1040 spin_lock_irqsave(&m->lock, flags);
1042 if (m->pg_init_count <= m->pg_init_retries)
1043 m->pg_init_required = 1;
1044 else
1045 limit_reached = 1;
1047 spin_unlock_irqrestore(&m->lock, flags);
1049 return limit_reached;
1052 static void pg_init_done(struct dm_path *path, int errors)
1054 struct pgpath *pgpath = path_to_pgpath(path);
1055 struct priority_group *pg = pgpath->pg;
1056 struct multipath *m = pg->m;
1057 unsigned long flags;
1059 /* device or driver problems */
1060 switch (errors) {
1061 case SCSI_DH_OK:
1062 break;
1063 case SCSI_DH_NOSYS:
1064 if (!m->hw_handler_name) {
1065 errors = 0;
1066 break;
1068 DMERR("Cannot failover device because scsi_dh_%s was not "
1069 "loaded.", m->hw_handler_name);
1071 * Fail path for now, so we do not ping pong
1073 fail_path(pgpath);
1074 break;
1075 case SCSI_DH_DEV_TEMP_BUSY:
1077 * Probably doing something like FW upgrade on the
1078 * controller so try the other pg.
1080 bypass_pg(m, pg, 1);
1081 break;
1082 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1083 case SCSI_DH_RETRY:
1084 case SCSI_DH_IMM_RETRY:
1085 case SCSI_DH_RES_TEMP_UNAVAIL:
1086 if (pg_init_limit_reached(m, pgpath))
1087 fail_path(pgpath);
1088 errors = 0;
1089 break;
1090 default:
1092 * We probably do not want to fail the path for a device
1093 * error, but this is what the old dm did. In future
1094 * patches we can do more advanced handling.
1096 fail_path(pgpath);
1099 spin_lock_irqsave(&m->lock, flags);
1100 if (errors) {
1101 DMERR("Could not failover device. Error %d.", errors);
1102 m->current_pgpath = NULL;
1103 m->current_pg = NULL;
1104 } else if (!m->pg_init_required) {
1105 m->queue_io = 0;
1106 pg->bypassed = 0;
1109 m->pg_init_in_progress = 0;
1110 queue_work(kmultipathd, &m->process_queued_ios);
1111 spin_unlock_irqrestore(&m->lock, flags);
1114 static void activate_path(struct work_struct *work)
1116 int ret;
1117 struct multipath *m =
1118 container_of(work, struct multipath, activate_path);
1119 struct dm_path *path;
1120 unsigned long flags;
1122 spin_lock_irqsave(&m->lock, flags);
1123 path = &m->pgpath_to_activate->path;
1124 m->pgpath_to_activate = NULL;
1125 spin_unlock_irqrestore(&m->lock, flags);
1126 if (!path)
1127 return;
1128 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1129 pg_init_done(path, ret);
1133 * end_io handling
1135 static int do_end_io(struct multipath *m, struct bio *bio,
1136 int error, struct dm_mpath_io *mpio)
1138 unsigned long flags;
1140 if (!error)
1141 return 0; /* I/O complete */
1143 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1144 return error;
1146 if (error == -EOPNOTSUPP)
1147 return error;
1149 spin_lock_irqsave(&m->lock, flags);
1150 if (!m->nr_valid_paths) {
1151 if (__must_push_back(m)) {
1152 spin_unlock_irqrestore(&m->lock, flags);
1153 return DM_ENDIO_REQUEUE;
1154 } else if (!m->queue_if_no_path) {
1155 spin_unlock_irqrestore(&m->lock, flags);
1156 return -EIO;
1157 } else {
1158 spin_unlock_irqrestore(&m->lock, flags);
1159 goto requeue;
1162 spin_unlock_irqrestore(&m->lock, flags);
1164 if (mpio->pgpath)
1165 fail_path(mpio->pgpath);
1167 requeue:
1168 dm_bio_restore(&mpio->details, bio);
1170 /* queue for the daemon to resubmit or fail */
1171 spin_lock_irqsave(&m->lock, flags);
1172 bio_list_add(&m->queued_ios, bio);
1173 m->queue_size++;
1174 if (!m->queue_io)
1175 queue_work(kmultipathd, &m->process_queued_ios);
1176 spin_unlock_irqrestore(&m->lock, flags);
1178 return DM_ENDIO_INCOMPLETE; /* io not complete */
1181 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1182 int error, union map_info *map_context)
1184 struct multipath *m = ti->private;
1185 struct dm_mpath_io *mpio = map_context->ptr;
1186 struct pgpath *pgpath = mpio->pgpath;
1187 struct path_selector *ps;
1188 int r;
1190 r = do_end_io(m, bio, error, mpio);
1191 if (pgpath) {
1192 ps = &pgpath->pg->ps;
1193 if (ps->type->end_io)
1194 ps->type->end_io(ps, &pgpath->path);
1196 if (r != DM_ENDIO_INCOMPLETE)
1197 mempool_free(mpio, m->mpio_pool);
1199 return r;
1203 * Suspend can't complete until all the I/O is processed so if
1204 * the last path fails we must error any remaining I/O.
1205 * Note that if the freeze_bdev fails while suspending, the
1206 * queue_if_no_path state is lost - userspace should reset it.
1208 static void multipath_presuspend(struct dm_target *ti)
1210 struct multipath *m = (struct multipath *) ti->private;
1212 queue_if_no_path(m, 0, 1);
1216 * Restore the queue_if_no_path setting.
1218 static void multipath_resume(struct dm_target *ti)
1220 struct multipath *m = (struct multipath *) ti->private;
1221 unsigned long flags;
1223 spin_lock_irqsave(&m->lock, flags);
1224 m->queue_if_no_path = m->saved_queue_if_no_path;
1225 spin_unlock_irqrestore(&m->lock, flags);
1229 * Info output has the following format:
1230 * num_multipath_feature_args [multipath_feature_args]*
1231 * num_handler_status_args [handler_status_args]*
1232 * num_groups init_group_number
1233 * [A|D|E num_ps_status_args [ps_status_args]*
1234 * num_paths num_selector_args
1235 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1237 * Table output has the following format (identical to the constructor string):
1238 * num_feature_args [features_args]*
1239 * num_handler_args hw_handler [hw_handler_args]*
1240 * num_groups init_group_number
1241 * [priority selector-name num_ps_args [ps_args]*
1242 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1244 static int multipath_status(struct dm_target *ti, status_type_t type,
1245 char *result, unsigned int maxlen)
1247 int sz = 0;
1248 unsigned long flags;
1249 struct multipath *m = (struct multipath *) ti->private;
1250 struct priority_group *pg;
1251 struct pgpath *p;
1252 unsigned pg_num;
1253 char state;
1255 spin_lock_irqsave(&m->lock, flags);
1257 /* Features */
1258 if (type == STATUSTYPE_INFO)
1259 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1260 else {
1261 DMEMIT("%u ", m->queue_if_no_path +
1262 (m->pg_init_retries > 0) * 2);
1263 if (m->queue_if_no_path)
1264 DMEMIT("queue_if_no_path ");
1265 if (m->pg_init_retries)
1266 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1269 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1270 DMEMIT("0 ");
1271 else
1272 DMEMIT("1 %s ", m->hw_handler_name);
1274 DMEMIT("%u ", m->nr_priority_groups);
1276 if (m->next_pg)
1277 pg_num = m->next_pg->pg_num;
1278 else if (m->current_pg)
1279 pg_num = m->current_pg->pg_num;
1280 else
1281 pg_num = 1;
1283 DMEMIT("%u ", pg_num);
1285 switch (type) {
1286 case STATUSTYPE_INFO:
1287 list_for_each_entry(pg, &m->priority_groups, list) {
1288 if (pg->bypassed)
1289 state = 'D'; /* Disabled */
1290 else if (pg == m->current_pg)
1291 state = 'A'; /* Currently Active */
1292 else
1293 state = 'E'; /* Enabled */
1295 DMEMIT("%c ", state);
1297 if (pg->ps.type->status)
1298 sz += pg->ps.type->status(&pg->ps, NULL, type,
1299 result + sz,
1300 maxlen - sz);
1301 else
1302 DMEMIT("0 ");
1304 DMEMIT("%u %u ", pg->nr_pgpaths,
1305 pg->ps.type->info_args);
1307 list_for_each_entry(p, &pg->pgpaths, list) {
1308 DMEMIT("%s %s %u ", p->path.dev->name,
1309 p->is_active ? "A" : "F",
1310 p->fail_count);
1311 if (pg->ps.type->status)
1312 sz += pg->ps.type->status(&pg->ps,
1313 &p->path, type, result + sz,
1314 maxlen - sz);
1317 break;
1319 case STATUSTYPE_TABLE:
1320 list_for_each_entry(pg, &m->priority_groups, list) {
1321 DMEMIT("%s ", pg->ps.type->name);
1323 if (pg->ps.type->status)
1324 sz += pg->ps.type->status(&pg->ps, NULL, type,
1325 result + sz,
1326 maxlen - sz);
1327 else
1328 DMEMIT("0 ");
1330 DMEMIT("%u %u ", pg->nr_pgpaths,
1331 pg->ps.type->table_args);
1333 list_for_each_entry(p, &pg->pgpaths, list) {
1334 DMEMIT("%s ", p->path.dev->name);
1335 if (pg->ps.type->status)
1336 sz += pg->ps.type->status(&pg->ps,
1337 &p->path, type, result + sz,
1338 maxlen - sz);
1341 break;
1344 spin_unlock_irqrestore(&m->lock, flags);
1346 return 0;
1349 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1351 int r;
1352 struct dm_dev *dev;
1353 struct multipath *m = (struct multipath *) ti->private;
1354 action_fn action;
1356 if (argc == 1) {
1357 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1358 return queue_if_no_path(m, 1, 0);
1359 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1360 return queue_if_no_path(m, 0, 0);
1363 if (argc != 2)
1364 goto error;
1366 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1367 return bypass_pg_num(m, argv[1], 1);
1368 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1369 return bypass_pg_num(m, argv[1], 0);
1370 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1371 return switch_pg_num(m, argv[1]);
1372 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1373 action = reinstate_path;
1374 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1375 action = fail_path;
1376 else
1377 goto error;
1379 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1380 dm_table_get_mode(ti->table), &dev);
1381 if (r) {
1382 DMWARN("message: error getting device %s",
1383 argv[1]);
1384 return -EINVAL;
1387 r = action_dev(m, dev, action);
1389 dm_put_device(ti, dev);
1391 return r;
1393 error:
1394 DMWARN("Unrecognised multipath message received.");
1395 return -EINVAL;
1398 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1399 struct file *filp, unsigned int cmd,
1400 unsigned long arg)
1402 struct multipath *m = (struct multipath *) ti->private;
1403 struct block_device *bdev = NULL;
1404 unsigned long flags;
1405 struct file fake_file = {};
1406 struct dentry fake_dentry = {};
1407 int r = 0;
1409 fake_file.f_path.dentry = &fake_dentry;
1411 spin_lock_irqsave(&m->lock, flags);
1413 if (!m->current_pgpath)
1414 __choose_pgpath(m);
1416 if (m->current_pgpath) {
1417 bdev = m->current_pgpath->path.dev->bdev;
1418 fake_dentry.d_inode = bdev->bd_inode;
1419 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1422 if (m->queue_io)
1423 r = -EAGAIN;
1424 else if (!bdev)
1425 r = -EIO;
1427 spin_unlock_irqrestore(&m->lock, flags);
1429 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1430 bdev->bd_disk, cmd, arg);
1433 /*-----------------------------------------------------------------
1434 * Module setup
1435 *---------------------------------------------------------------*/
1436 static struct target_type multipath_target = {
1437 .name = "multipath",
1438 .version = {1, 0, 5},
1439 .module = THIS_MODULE,
1440 .ctr = multipath_ctr,
1441 .dtr = multipath_dtr,
1442 .map = multipath_map,
1443 .end_io = multipath_end_io,
1444 .presuspend = multipath_presuspend,
1445 .resume = multipath_resume,
1446 .status = multipath_status,
1447 .message = multipath_message,
1448 .ioctl = multipath_ioctl,
1451 static int __init dm_multipath_init(void)
1453 int r;
1455 /* allocate a slab for the dm_ios */
1456 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1457 if (!_mpio_cache)
1458 return -ENOMEM;
1460 r = dm_register_target(&multipath_target);
1461 if (r < 0) {
1462 DMERR("register failed %d", r);
1463 kmem_cache_destroy(_mpio_cache);
1464 return -EINVAL;
1467 kmultipathd = create_workqueue("kmpathd");
1468 if (!kmultipathd) {
1469 DMERR("failed to create workqueue kmpathd");
1470 dm_unregister_target(&multipath_target);
1471 kmem_cache_destroy(_mpio_cache);
1472 return -ENOMEM;
1476 * A separate workqueue is used to handle the device handlers
1477 * to avoid overloading existing workqueue. Overloading the
1478 * old workqueue would also create a bottleneck in the
1479 * path of the storage hardware device activation.
1481 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1482 if (!kmpath_handlerd) {
1483 DMERR("failed to create workqueue kmpath_handlerd");
1484 destroy_workqueue(kmultipathd);
1485 dm_unregister_target(&multipath_target);
1486 kmem_cache_destroy(_mpio_cache);
1487 return -ENOMEM;
1490 DMINFO("version %u.%u.%u loaded",
1491 multipath_target.version[0], multipath_target.version[1],
1492 multipath_target.version[2]);
1494 return r;
1497 static void __exit dm_multipath_exit(void)
1499 int r;
1501 destroy_workqueue(kmpath_handlerd);
1502 destroy_workqueue(kmultipathd);
1504 r = dm_unregister_target(&multipath_target);
1505 if (r < 0)
1506 DMERR("target unregister failed %d", r);
1507 kmem_cache_destroy(_mpio_cache);
1510 module_init(dm_multipath_init);
1511 module_exit(dm_multipath_exit);
1513 MODULE_DESCRIPTION(DM_NAME " multipath target");
1514 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1515 MODULE_LICENSE("GPL");