dm mpath: validate hw_handler argument count
[linux-2.6/mini2440.git] / drivers / md / dm-mpath.c
blob774cc49239ffdf4872c2946e22e8e5fb0b148a0c
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-bio-list.h"
12 #include "dm-bio-record.h"
13 #include "dm-uevent.h"
15 #include <linux/ctype.h>
16 #include <linux/init.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/slab.h>
21 #include <linux/time.h>
22 #include <linux/workqueue.h>
23 #include <scsi/scsi_dh.h>
24 #include <asm/atomic.h>
26 #define DM_MSG_PREFIX "multipath"
27 #define MESG_STR(x) x, sizeof(x)
29 /* Path properties */
30 struct pgpath {
31 struct list_head list;
33 struct priority_group *pg; /* Owning PG */
34 unsigned is_active; /* Path status */
35 unsigned fail_count; /* Cumulative failure count */
37 struct dm_path path;
38 struct work_struct deactivate_path;
41 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
47 struct priority_group {
48 struct list_head list;
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
60 /* Multipath context */
61 struct multipath {
62 struct list_head list;
63 struct dm_target *ti;
65 spinlock_t lock;
67 const char *hw_handler_name;
68 struct work_struct activate_path;
69 struct pgpath *pgpath_to_activate;
70 unsigned nr_priority_groups;
71 struct list_head priority_groups;
72 unsigned pg_init_required; /* pg_init needs calling? */
73 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
75 unsigned nr_valid_paths; /* Total number of usable paths */
76 struct pgpath *current_pgpath;
77 struct priority_group *current_pg;
78 struct priority_group *next_pg; /* Switch to this PG if set */
79 unsigned repeat_count; /* I/Os left before calling PS again */
81 unsigned queue_io; /* Must we queue all I/O? */
82 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
83 unsigned saved_queue_if_no_path;/* Saved state during suspension */
84 unsigned pg_init_retries; /* Number of times to retry pg_init */
85 unsigned pg_init_count; /* Number of times pg_init called */
87 struct work_struct process_queued_ios;
88 struct bio_list queued_ios;
89 unsigned queue_size;
91 struct work_struct trigger_event;
94 * We must use a mempool of dm_mpath_io structs so that we
95 * can resubmit bios on error.
97 mempool_t *mpio_pool;
101 * Context information attached to each bio we process.
103 struct dm_mpath_io {
104 struct pgpath *pgpath;
105 struct dm_bio_details details;
108 typedef int (*action_fn) (struct pgpath *pgpath);
110 #define MIN_IOS 256 /* Mempool size */
112 static struct kmem_cache *_mpio_cache;
114 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
115 static void process_queued_ios(struct work_struct *work);
116 static void trigger_event(struct work_struct *work);
117 static void activate_path(struct work_struct *work);
118 static void deactivate_path(struct work_struct *work);
121 /*-----------------------------------------------
122 * Allocation routines
123 *-----------------------------------------------*/
125 static struct pgpath *alloc_pgpath(void)
127 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
129 if (pgpath) {
130 pgpath->is_active = 1;
131 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
134 return pgpath;
137 static void free_pgpath(struct pgpath *pgpath)
139 kfree(pgpath);
142 static void deactivate_path(struct work_struct *work)
144 struct pgpath *pgpath =
145 container_of(work, struct pgpath, deactivate_path);
147 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
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 unsigned long flags;
165 struct pgpath *pgpath, *tmp;
166 struct multipath *m = ti->private;
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
170 if (m->hw_handler_name)
171 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
172 dm_put_device(ti, pgpath->path.dev);
173 spin_lock_irqsave(&m->lock, flags);
174 if (m->pgpath_to_activate == pgpath)
175 m->pgpath_to_activate = NULL;
176 spin_unlock_irqrestore(&m->lock, flags);
177 free_pgpath(pgpath);
181 static void free_priority_group(struct priority_group *pg,
182 struct dm_target *ti)
184 struct path_selector *ps = &pg->ps;
186 if (ps->type) {
187 ps->type->destroy(ps);
188 dm_put_path_selector(ps->type);
191 free_pgpaths(&pg->pgpaths, ti);
192 kfree(pg);
195 static struct multipath *alloc_multipath(struct dm_target *ti)
197 struct multipath *m;
199 m = kzalloc(sizeof(*m), GFP_KERNEL);
200 if (m) {
201 INIT_LIST_HEAD(&m->priority_groups);
202 spin_lock_init(&m->lock);
203 m->queue_io = 1;
204 INIT_WORK(&m->process_queued_ios, process_queued_ios);
205 INIT_WORK(&m->trigger_event, trigger_event);
206 INIT_WORK(&m->activate_path, activate_path);
207 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
208 if (!m->mpio_pool) {
209 kfree(m);
210 return NULL;
212 m->ti = ti;
213 ti->private = m;
216 return m;
219 static void free_multipath(struct multipath *m)
221 struct priority_group *pg, *tmp;
223 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
224 list_del(&pg->list);
225 free_priority_group(pg, m->ti);
228 kfree(m->hw_handler_name);
229 mempool_destroy(m->mpio_pool);
230 kfree(m);
234 /*-----------------------------------------------
235 * Path selection
236 *-----------------------------------------------*/
238 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
240 m->current_pg = pgpath->pg;
242 /* Must we initialise the PG first, and queue I/O till it's ready? */
243 if (m->hw_handler_name) {
244 m->pg_init_required = 1;
245 m->queue_io = 1;
246 } else {
247 m->pg_init_required = 0;
248 m->queue_io = 0;
251 m->pg_init_count = 0;
254 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
256 struct dm_path *path;
258 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
259 if (!path)
260 return -ENXIO;
262 m->current_pgpath = path_to_pgpath(path);
264 if (m->current_pg != pg)
265 __switch_pg(m, m->current_pgpath);
267 return 0;
270 static void __choose_pgpath(struct multipath *m)
272 struct priority_group *pg;
273 unsigned bypassed = 1;
275 if (!m->nr_valid_paths)
276 goto failed;
278 /* Were we instructed to switch PG? */
279 if (m->next_pg) {
280 pg = m->next_pg;
281 m->next_pg = NULL;
282 if (!__choose_path_in_pg(m, pg))
283 return;
286 /* Don't change PG until it has no remaining paths */
287 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
288 return;
291 * Loop through priority groups until we find a valid path.
292 * First time we skip PGs marked 'bypassed'.
293 * Second time we only try the ones we skipped.
295 do {
296 list_for_each_entry(pg, &m->priority_groups, list) {
297 if (pg->bypassed == bypassed)
298 continue;
299 if (!__choose_path_in_pg(m, pg))
300 return;
302 } while (bypassed--);
304 failed:
305 m->current_pgpath = NULL;
306 m->current_pg = NULL;
310 * Check whether bios must be queued in the device-mapper core rather
311 * than here in the target.
313 * m->lock must be held on entry.
315 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
316 * same value then we are not between multipath_presuspend()
317 * and multipath_resume() calls and we have no need to check
318 * for the DMF_NOFLUSH_SUSPENDING flag.
320 static int __must_push_back(struct multipath *m)
322 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
323 dm_noflush_suspending(m->ti));
326 static int map_io(struct multipath *m, struct bio *bio,
327 struct dm_mpath_io *mpio, unsigned was_queued)
329 int r = DM_MAPIO_REMAPPED;
330 unsigned long flags;
331 struct pgpath *pgpath;
333 spin_lock_irqsave(&m->lock, flags);
335 /* Do we need to select a new pgpath? */
336 if (!m->current_pgpath ||
337 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
338 __choose_pgpath(m);
340 pgpath = m->current_pgpath;
342 if (was_queued)
343 m->queue_size--;
345 if ((pgpath && m->queue_io) ||
346 (!pgpath && m->queue_if_no_path)) {
347 /* Queue for the daemon to resubmit */
348 bio_list_add(&m->queued_ios, bio);
349 m->queue_size++;
350 if ((m->pg_init_required && !m->pg_init_in_progress) ||
351 !m->queue_io)
352 queue_work(kmultipathd, &m->process_queued_ios);
353 pgpath = NULL;
354 r = DM_MAPIO_SUBMITTED;
355 } else if (pgpath)
356 bio->bi_bdev = pgpath->path.dev->bdev;
357 else if (__must_push_back(m))
358 r = DM_MAPIO_REQUEUE;
359 else
360 r = -EIO; /* Failed */
362 mpio->pgpath = pgpath;
364 spin_unlock_irqrestore(&m->lock, flags);
366 return r;
370 * If we run out of usable paths, should we queue I/O or error it?
372 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
373 unsigned save_old_value)
375 unsigned long flags;
377 spin_lock_irqsave(&m->lock, flags);
379 if (save_old_value)
380 m->saved_queue_if_no_path = m->queue_if_no_path;
381 else
382 m->saved_queue_if_no_path = queue_if_no_path;
383 m->queue_if_no_path = queue_if_no_path;
384 if (!m->queue_if_no_path && m->queue_size)
385 queue_work(kmultipathd, &m->process_queued_ios);
387 spin_unlock_irqrestore(&m->lock, flags);
389 return 0;
392 /*-----------------------------------------------------------------
393 * The multipath daemon is responsible for resubmitting queued ios.
394 *---------------------------------------------------------------*/
396 static void dispatch_queued_ios(struct multipath *m)
398 int r;
399 unsigned long flags;
400 struct bio *bio = NULL, *next;
401 struct dm_mpath_io *mpio;
402 union map_info *info;
404 spin_lock_irqsave(&m->lock, flags);
405 bio = bio_list_get(&m->queued_ios);
406 spin_unlock_irqrestore(&m->lock, flags);
408 while (bio) {
409 next = bio->bi_next;
410 bio->bi_next = NULL;
412 info = dm_get_mapinfo(bio);
413 mpio = info->ptr;
415 r = map_io(m, bio, mpio, 1);
416 if (r < 0)
417 bio_endio(bio, r);
418 else if (r == DM_MAPIO_REMAPPED)
419 generic_make_request(bio);
420 else if (r == DM_MAPIO_REQUEUE)
421 bio_endio(bio, -EIO);
423 bio = next;
427 static void process_queued_ios(struct work_struct *work)
429 struct multipath *m =
430 container_of(work, struct multipath, process_queued_ios);
431 struct pgpath *pgpath = NULL;
432 unsigned init_required = 0, must_queue = 1;
433 unsigned long flags;
435 spin_lock_irqsave(&m->lock, flags);
437 if (!m->queue_size)
438 goto out;
440 if (!m->current_pgpath)
441 __choose_pgpath(m);
443 pgpath = 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 && pgpath) {
450 m->pgpath_to_activate = pgpath;
451 m->pg_init_count++;
452 m->pg_init_required = 0;
453 m->pg_init_in_progress = 1;
454 init_required = 1;
457 out:
458 spin_unlock_irqrestore(&m->lock, flags);
460 if (init_required)
461 queue_work(kmpath_handlerd, &m->activate_path);
463 if (!must_queue)
464 dispatch_queued_ios(m);
468 * An event is triggered whenever a path is taken out of use.
469 * Includes path failure and PG bypass.
471 static void trigger_event(struct work_struct *work)
473 struct multipath *m =
474 container_of(work, struct multipath, trigger_event);
476 dm_table_event(m->ti->table);
479 /*-----------------------------------------------------------------
480 * Constructor/argument parsing:
481 * <#multipath feature args> [<arg>]*
482 * <#hw_handler args> [hw_handler [<arg>]*]
483 * <#priority groups>
484 * <initial priority group>
485 * [<selector> <#selector args> [<arg>]*
486 * <#paths> <#per-path selector args>
487 * [<path> [<arg>]* ]+ ]+
488 *---------------------------------------------------------------*/
489 struct param {
490 unsigned min;
491 unsigned max;
492 char *error;
495 static int read_param(struct param *param, char *str, unsigned *v, char **error)
497 if (!str ||
498 (sscanf(str, "%u", v) != 1) ||
499 (*v < param->min) ||
500 (*v > param->max)) {
501 *error = param->error;
502 return -EINVAL;
505 return 0;
508 struct arg_set {
509 unsigned argc;
510 char **argv;
513 static char *shift(struct arg_set *as)
515 char *r;
517 if (as->argc) {
518 as->argc--;
519 r = *as->argv;
520 as->argv++;
521 return r;
524 return NULL;
527 static void consume(struct arg_set *as, unsigned n)
529 BUG_ON (as->argc < n);
530 as->argc -= n;
531 as->argv += n;
534 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
535 struct dm_target *ti)
537 int r;
538 struct path_selector_type *pst;
539 unsigned ps_argc;
541 static struct param _params[] = {
542 {0, 1024, "invalid number of path selector args"},
545 pst = dm_get_path_selector(shift(as));
546 if (!pst) {
547 ti->error = "unknown path selector type";
548 return -EINVAL;
551 r = read_param(_params, shift(as), &ps_argc, &ti->error);
552 if (r) {
553 dm_put_path_selector(pst);
554 return -EINVAL;
557 if (ps_argc > as->argc) {
558 dm_put_path_selector(pst);
559 ti->error = "not enough arguments for path selector";
560 return -EINVAL;
563 r = pst->create(&pg->ps, ps_argc, as->argv);
564 if (r) {
565 dm_put_path_selector(pst);
566 ti->error = "path selector constructor failed";
567 return r;
570 pg->ps.type = pst;
571 consume(as, ps_argc);
573 return 0;
576 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
577 struct dm_target *ti)
579 int r;
580 struct pgpath *p;
581 struct multipath *m = ti->private;
583 /* we need at least a path arg */
584 if (as->argc < 1) {
585 ti->error = "no device given";
586 return ERR_PTR(-EINVAL);
589 p = alloc_pgpath();
590 if (!p)
591 return ERR_PTR(-ENOMEM);
593 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
594 dm_table_get_mode(ti->table), &p->path.dev);
595 if (r) {
596 ti->error = "error getting device";
597 goto bad;
600 if (m->hw_handler_name) {
601 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
602 m->hw_handler_name);
603 if (r < 0) {
604 dm_put_device(ti, p->path.dev);
605 goto bad;
609 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
610 if (r) {
611 dm_put_device(ti, p->path.dev);
612 goto bad;
615 return p;
617 bad:
618 free_pgpath(p);
619 return ERR_PTR(r);
622 static struct priority_group *parse_priority_group(struct arg_set *as,
623 struct multipath *m)
625 static struct param _params[] = {
626 {1, 1024, "invalid number of paths"},
627 {0, 1024, "invalid number of selector args"}
630 int r;
631 unsigned i, nr_selector_args, nr_params;
632 struct priority_group *pg;
633 struct dm_target *ti = m->ti;
635 if (as->argc < 2) {
636 as->argc = 0;
637 ti->error = "not enough priority group arguments";
638 return ERR_PTR(-EINVAL);
641 pg = alloc_priority_group();
642 if (!pg) {
643 ti->error = "couldn't allocate priority group";
644 return ERR_PTR(-ENOMEM);
646 pg->m = m;
648 r = parse_path_selector(as, pg, ti);
649 if (r)
650 goto bad;
653 * read the paths
655 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
656 if (r)
657 goto bad;
659 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
660 if (r)
661 goto bad;
663 nr_params = 1 + nr_selector_args;
664 for (i = 0; i < pg->nr_pgpaths; i++) {
665 struct pgpath *pgpath;
666 struct arg_set path_args;
668 if (as->argc < nr_params) {
669 ti->error = "not enough path parameters";
670 goto bad;
673 path_args.argc = nr_params;
674 path_args.argv = as->argv;
676 pgpath = parse_path(&path_args, &pg->ps, ti);
677 if (IS_ERR(pgpath)) {
678 r = PTR_ERR(pgpath);
679 goto bad;
682 pgpath->pg = pg;
683 list_add_tail(&pgpath->list, &pg->pgpaths);
684 consume(as, nr_params);
687 return pg;
689 bad:
690 free_priority_group(pg, ti);
691 return ERR_PTR(r);
694 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
696 unsigned hw_argc;
697 struct dm_target *ti = m->ti;
699 static struct param _params[] = {
700 {0, 1024, "invalid number of hardware handler args"},
703 if (read_param(_params, shift(as), &hw_argc, &ti->error))
704 return -EINVAL;
706 if (!hw_argc)
707 return 0;
709 if (hw_argc > as->argc) {
710 ti->error = "not enough arguments for hardware handler";
711 return -EINVAL;
714 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
715 request_module("scsi_dh_%s", m->hw_handler_name);
716 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
717 ti->error = "unknown hardware handler type";
718 kfree(m->hw_handler_name);
719 m->hw_handler_name = NULL;
720 return -EINVAL;
723 if (hw_argc > 1)
724 DMWARN("Ignoring user-specified arguments for "
725 "hardware handler \"%s\"", m->hw_handler_name);
726 consume(as, hw_argc - 1);
728 return 0;
731 static int parse_features(struct arg_set *as, struct multipath *m)
733 int r;
734 unsigned argc;
735 struct dm_target *ti = m->ti;
736 const char *param_name;
738 static struct param _params[] = {
739 {0, 3, "invalid number of feature args"},
740 {1, 50, "pg_init_retries must be between 1 and 50"},
743 r = read_param(_params, shift(as), &argc, &ti->error);
744 if (r)
745 return -EINVAL;
747 if (!argc)
748 return 0;
750 do {
751 param_name = shift(as);
752 argc--;
754 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
755 r = queue_if_no_path(m, 1, 0);
756 continue;
759 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
760 (argc >= 1)) {
761 r = read_param(_params + 1, shift(as),
762 &m->pg_init_retries, &ti->error);
763 argc--;
764 continue;
767 ti->error = "Unrecognised multipath feature request";
768 r = -EINVAL;
769 } while (argc && !r);
771 return r;
774 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
775 char **argv)
777 /* target parameters */
778 static struct param _params[] = {
779 {1, 1024, "invalid number of priority groups"},
780 {1, 1024, "invalid initial priority group number"},
783 int r;
784 struct multipath *m;
785 struct arg_set as;
786 unsigned pg_count = 0;
787 unsigned next_pg_num;
789 as.argc = argc;
790 as.argv = argv;
792 m = alloc_multipath(ti);
793 if (!m) {
794 ti->error = "can't allocate multipath";
795 return -EINVAL;
798 r = parse_features(&as, m);
799 if (r)
800 goto bad;
802 r = parse_hw_handler(&as, m);
803 if (r)
804 goto bad;
806 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
807 if (r)
808 goto bad;
810 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
811 if (r)
812 goto bad;
814 /* parse the priority groups */
815 while (as.argc) {
816 struct priority_group *pg;
818 pg = parse_priority_group(&as, m);
819 if (IS_ERR(pg)) {
820 r = PTR_ERR(pg);
821 goto bad;
824 m->nr_valid_paths += pg->nr_pgpaths;
825 list_add_tail(&pg->list, &m->priority_groups);
826 pg_count++;
827 pg->pg_num = pg_count;
828 if (!--next_pg_num)
829 m->next_pg = pg;
832 if (pg_count != m->nr_priority_groups) {
833 ti->error = "priority group count mismatch";
834 r = -EINVAL;
835 goto bad;
838 return 0;
840 bad:
841 free_multipath(m);
842 return r;
845 static void multipath_dtr(struct dm_target *ti)
847 struct multipath *m = (struct multipath *) ti->private;
849 flush_workqueue(kmpath_handlerd);
850 flush_workqueue(kmultipathd);
851 free_multipath(m);
855 * Map bios, recording original fields for later in case we have to resubmit
857 static int multipath_map(struct dm_target *ti, struct bio *bio,
858 union map_info *map_context)
860 int r;
861 struct dm_mpath_io *mpio;
862 struct multipath *m = (struct multipath *) ti->private;
864 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
865 dm_bio_record(&mpio->details, bio);
867 map_context->ptr = mpio;
868 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
869 r = map_io(m, bio, mpio, 0);
870 if (r < 0 || r == DM_MAPIO_REQUEUE)
871 mempool_free(mpio, m->mpio_pool);
873 return r;
877 * Take a path out of use.
879 static int fail_path(struct pgpath *pgpath)
881 unsigned long flags;
882 struct multipath *m = pgpath->pg->m;
884 spin_lock_irqsave(&m->lock, flags);
886 if (!pgpath->is_active)
887 goto out;
889 DMWARN("Failing path %s.", pgpath->path.dev->name);
891 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
892 pgpath->is_active = 0;
893 pgpath->fail_count++;
895 m->nr_valid_paths--;
897 if (pgpath == m->current_pgpath)
898 m->current_pgpath = NULL;
900 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
901 pgpath->path.dev->name, m->nr_valid_paths);
903 schedule_work(&m->trigger_event);
904 queue_work(kmultipathd, &pgpath->deactivate_path);
906 out:
907 spin_unlock_irqrestore(&m->lock, flags);
909 return 0;
913 * Reinstate a previously-failed path
915 static int reinstate_path(struct pgpath *pgpath)
917 int r = 0;
918 unsigned long flags;
919 struct multipath *m = pgpath->pg->m;
921 spin_lock_irqsave(&m->lock, flags);
923 if (pgpath->is_active)
924 goto out;
926 if (!pgpath->pg->ps.type->reinstate_path) {
927 DMWARN("Reinstate path not supported by path selector %s",
928 pgpath->pg->ps.type->name);
929 r = -EINVAL;
930 goto out;
933 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
934 if (r)
935 goto out;
937 pgpath->is_active = 1;
939 m->current_pgpath = NULL;
940 if (!m->nr_valid_paths++ && m->queue_size)
941 queue_work(kmultipathd, &m->process_queued_ios);
943 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
944 pgpath->path.dev->name, m->nr_valid_paths);
946 schedule_work(&m->trigger_event);
948 out:
949 spin_unlock_irqrestore(&m->lock, flags);
951 return r;
955 * Fail or reinstate all paths that match the provided struct dm_dev.
957 static int action_dev(struct multipath *m, struct dm_dev *dev,
958 action_fn action)
960 int r = 0;
961 struct pgpath *pgpath;
962 struct priority_group *pg;
964 list_for_each_entry(pg, &m->priority_groups, list) {
965 list_for_each_entry(pgpath, &pg->pgpaths, list) {
966 if (pgpath->path.dev == dev)
967 r = action(pgpath);
971 return r;
975 * Temporarily try to avoid having to use the specified PG
977 static void bypass_pg(struct multipath *m, struct priority_group *pg,
978 int bypassed)
980 unsigned long flags;
982 spin_lock_irqsave(&m->lock, flags);
984 pg->bypassed = bypassed;
985 m->current_pgpath = NULL;
986 m->current_pg = NULL;
988 spin_unlock_irqrestore(&m->lock, flags);
990 schedule_work(&m->trigger_event);
994 * Switch to using the specified PG from the next I/O that gets mapped
996 static int switch_pg_num(struct multipath *m, const char *pgstr)
998 struct priority_group *pg;
999 unsigned pgnum;
1000 unsigned long flags;
1002 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1003 (pgnum > m->nr_priority_groups)) {
1004 DMWARN("invalid PG number supplied to switch_pg_num");
1005 return -EINVAL;
1008 spin_lock_irqsave(&m->lock, flags);
1009 list_for_each_entry(pg, &m->priority_groups, list) {
1010 pg->bypassed = 0;
1011 if (--pgnum)
1012 continue;
1014 m->current_pgpath = NULL;
1015 m->current_pg = NULL;
1016 m->next_pg = pg;
1018 spin_unlock_irqrestore(&m->lock, flags);
1020 schedule_work(&m->trigger_event);
1021 return 0;
1025 * Set/clear bypassed status of a PG.
1026 * PGs are numbered upwards from 1 in the order they were declared.
1028 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1030 struct priority_group *pg;
1031 unsigned pgnum;
1033 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1034 (pgnum > m->nr_priority_groups)) {
1035 DMWARN("invalid PG number supplied to bypass_pg");
1036 return -EINVAL;
1039 list_for_each_entry(pg, &m->priority_groups, list) {
1040 if (!--pgnum)
1041 break;
1044 bypass_pg(m, pg, bypassed);
1045 return 0;
1049 * Should we retry pg_init immediately?
1051 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1053 unsigned long flags;
1054 int limit_reached = 0;
1056 spin_lock_irqsave(&m->lock, flags);
1058 if (m->pg_init_count <= m->pg_init_retries)
1059 m->pg_init_required = 1;
1060 else
1061 limit_reached = 1;
1063 spin_unlock_irqrestore(&m->lock, flags);
1065 return limit_reached;
1068 static void pg_init_done(struct dm_path *path, int errors)
1070 struct pgpath *pgpath = path_to_pgpath(path);
1071 struct priority_group *pg = pgpath->pg;
1072 struct multipath *m = pg->m;
1073 unsigned long flags;
1075 /* device or driver problems */
1076 switch (errors) {
1077 case SCSI_DH_OK:
1078 break;
1079 case SCSI_DH_NOSYS:
1080 if (!m->hw_handler_name) {
1081 errors = 0;
1082 break;
1084 DMERR("Cannot failover device because scsi_dh_%s was not "
1085 "loaded.", m->hw_handler_name);
1087 * Fail path for now, so we do not ping pong
1089 fail_path(pgpath);
1090 break;
1091 case SCSI_DH_DEV_TEMP_BUSY:
1093 * Probably doing something like FW upgrade on the
1094 * controller so try the other pg.
1096 bypass_pg(m, pg, 1);
1097 break;
1098 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1099 case SCSI_DH_RETRY:
1100 case SCSI_DH_IMM_RETRY:
1101 case SCSI_DH_RES_TEMP_UNAVAIL:
1102 if (pg_init_limit_reached(m, pgpath))
1103 fail_path(pgpath);
1104 errors = 0;
1105 break;
1106 default:
1108 * We probably do not want to fail the path for a device
1109 * error, but this is what the old dm did. In future
1110 * patches we can do more advanced handling.
1112 fail_path(pgpath);
1115 spin_lock_irqsave(&m->lock, flags);
1116 if (errors) {
1117 DMERR("Could not failover device. Error %d.", errors);
1118 m->current_pgpath = NULL;
1119 m->current_pg = NULL;
1120 } else if (!m->pg_init_required) {
1121 m->queue_io = 0;
1122 pg->bypassed = 0;
1125 m->pg_init_in_progress = 0;
1126 queue_work(kmultipathd, &m->process_queued_ios);
1127 spin_unlock_irqrestore(&m->lock, flags);
1130 static void activate_path(struct work_struct *work)
1132 int ret;
1133 struct multipath *m =
1134 container_of(work, struct multipath, activate_path);
1135 struct dm_path *path;
1136 unsigned long flags;
1138 spin_lock_irqsave(&m->lock, flags);
1139 path = &m->pgpath_to_activate->path;
1140 m->pgpath_to_activate = NULL;
1141 spin_unlock_irqrestore(&m->lock, flags);
1142 if (!path)
1143 return;
1144 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1145 pg_init_done(path, ret);
1149 * end_io handling
1151 static int do_end_io(struct multipath *m, struct bio *bio,
1152 int error, struct dm_mpath_io *mpio)
1154 unsigned long flags;
1156 if (!error)
1157 return 0; /* I/O complete */
1159 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1160 return error;
1162 if (error == -EOPNOTSUPP)
1163 return error;
1165 spin_lock_irqsave(&m->lock, flags);
1166 if (!m->nr_valid_paths) {
1167 if (__must_push_back(m)) {
1168 spin_unlock_irqrestore(&m->lock, flags);
1169 return DM_ENDIO_REQUEUE;
1170 } else if (!m->queue_if_no_path) {
1171 spin_unlock_irqrestore(&m->lock, flags);
1172 return -EIO;
1173 } else {
1174 spin_unlock_irqrestore(&m->lock, flags);
1175 goto requeue;
1178 spin_unlock_irqrestore(&m->lock, flags);
1180 if (mpio->pgpath)
1181 fail_path(mpio->pgpath);
1183 requeue:
1184 dm_bio_restore(&mpio->details, bio);
1186 /* queue for the daemon to resubmit or fail */
1187 spin_lock_irqsave(&m->lock, flags);
1188 bio_list_add(&m->queued_ios, bio);
1189 m->queue_size++;
1190 if (!m->queue_io)
1191 queue_work(kmultipathd, &m->process_queued_ios);
1192 spin_unlock_irqrestore(&m->lock, flags);
1194 return DM_ENDIO_INCOMPLETE; /* io not complete */
1197 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1198 int error, union map_info *map_context)
1200 struct multipath *m = ti->private;
1201 struct dm_mpath_io *mpio = map_context->ptr;
1202 struct pgpath *pgpath = mpio->pgpath;
1203 struct path_selector *ps;
1204 int r;
1206 r = do_end_io(m, bio, error, mpio);
1207 if (pgpath) {
1208 ps = &pgpath->pg->ps;
1209 if (ps->type->end_io)
1210 ps->type->end_io(ps, &pgpath->path);
1212 if (r != DM_ENDIO_INCOMPLETE)
1213 mempool_free(mpio, m->mpio_pool);
1215 return r;
1219 * Suspend can't complete until all the I/O is processed so if
1220 * the last path fails we must error any remaining I/O.
1221 * Note that if the freeze_bdev fails while suspending, the
1222 * queue_if_no_path state is lost - userspace should reset it.
1224 static void multipath_presuspend(struct dm_target *ti)
1226 struct multipath *m = (struct multipath *) ti->private;
1228 queue_if_no_path(m, 0, 1);
1232 * Restore the queue_if_no_path setting.
1234 static void multipath_resume(struct dm_target *ti)
1236 struct multipath *m = (struct multipath *) ti->private;
1237 unsigned long flags;
1239 spin_lock_irqsave(&m->lock, flags);
1240 m->queue_if_no_path = m->saved_queue_if_no_path;
1241 spin_unlock_irqrestore(&m->lock, flags);
1245 * Info output has the following format:
1246 * num_multipath_feature_args [multipath_feature_args]*
1247 * num_handler_status_args [handler_status_args]*
1248 * num_groups init_group_number
1249 * [A|D|E num_ps_status_args [ps_status_args]*
1250 * num_paths num_selector_args
1251 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1253 * Table output has the following format (identical to the constructor string):
1254 * num_feature_args [features_args]*
1255 * num_handler_args hw_handler [hw_handler_args]*
1256 * num_groups init_group_number
1257 * [priority selector-name num_ps_args [ps_args]*
1258 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1260 static int multipath_status(struct dm_target *ti, status_type_t type,
1261 char *result, unsigned int maxlen)
1263 int sz = 0;
1264 unsigned long flags;
1265 struct multipath *m = (struct multipath *) ti->private;
1266 struct priority_group *pg;
1267 struct pgpath *p;
1268 unsigned pg_num;
1269 char state;
1271 spin_lock_irqsave(&m->lock, flags);
1273 /* Features */
1274 if (type == STATUSTYPE_INFO)
1275 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1276 else {
1277 DMEMIT("%u ", m->queue_if_no_path +
1278 (m->pg_init_retries > 0) * 2);
1279 if (m->queue_if_no_path)
1280 DMEMIT("queue_if_no_path ");
1281 if (m->pg_init_retries)
1282 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1285 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1286 DMEMIT("0 ");
1287 else
1288 DMEMIT("1 %s ", m->hw_handler_name);
1290 DMEMIT("%u ", m->nr_priority_groups);
1292 if (m->next_pg)
1293 pg_num = m->next_pg->pg_num;
1294 else if (m->current_pg)
1295 pg_num = m->current_pg->pg_num;
1296 else
1297 pg_num = 1;
1299 DMEMIT("%u ", pg_num);
1301 switch (type) {
1302 case STATUSTYPE_INFO:
1303 list_for_each_entry(pg, &m->priority_groups, list) {
1304 if (pg->bypassed)
1305 state = 'D'; /* Disabled */
1306 else if (pg == m->current_pg)
1307 state = 'A'; /* Currently Active */
1308 else
1309 state = 'E'; /* Enabled */
1311 DMEMIT("%c ", state);
1313 if (pg->ps.type->status)
1314 sz += pg->ps.type->status(&pg->ps, NULL, type,
1315 result + sz,
1316 maxlen - sz);
1317 else
1318 DMEMIT("0 ");
1320 DMEMIT("%u %u ", pg->nr_pgpaths,
1321 pg->ps.type->info_args);
1323 list_for_each_entry(p, &pg->pgpaths, list) {
1324 DMEMIT("%s %s %u ", p->path.dev->name,
1325 p->is_active ? "A" : "F",
1326 p->fail_count);
1327 if (pg->ps.type->status)
1328 sz += pg->ps.type->status(&pg->ps,
1329 &p->path, type, result + sz,
1330 maxlen - sz);
1333 break;
1335 case STATUSTYPE_TABLE:
1336 list_for_each_entry(pg, &m->priority_groups, list) {
1337 DMEMIT("%s ", pg->ps.type->name);
1339 if (pg->ps.type->status)
1340 sz += pg->ps.type->status(&pg->ps, NULL, type,
1341 result + sz,
1342 maxlen - sz);
1343 else
1344 DMEMIT("0 ");
1346 DMEMIT("%u %u ", pg->nr_pgpaths,
1347 pg->ps.type->table_args);
1349 list_for_each_entry(p, &pg->pgpaths, list) {
1350 DMEMIT("%s ", p->path.dev->name);
1351 if (pg->ps.type->status)
1352 sz += pg->ps.type->status(&pg->ps,
1353 &p->path, type, result + sz,
1354 maxlen - sz);
1357 break;
1360 spin_unlock_irqrestore(&m->lock, flags);
1362 return 0;
1365 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1367 int r;
1368 struct dm_dev *dev;
1369 struct multipath *m = (struct multipath *) ti->private;
1370 action_fn action;
1372 if (argc == 1) {
1373 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1374 return queue_if_no_path(m, 1, 0);
1375 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1376 return queue_if_no_path(m, 0, 0);
1379 if (argc != 2)
1380 goto error;
1382 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1383 return bypass_pg_num(m, argv[1], 1);
1384 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1385 return bypass_pg_num(m, argv[1], 0);
1386 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1387 return switch_pg_num(m, argv[1]);
1388 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1389 action = reinstate_path;
1390 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1391 action = fail_path;
1392 else
1393 goto error;
1395 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1396 dm_table_get_mode(ti->table), &dev);
1397 if (r) {
1398 DMWARN("message: error getting device %s",
1399 argv[1]);
1400 return -EINVAL;
1403 r = action_dev(m, dev, action);
1405 dm_put_device(ti, dev);
1407 return r;
1409 error:
1410 DMWARN("Unrecognised multipath message received.");
1411 return -EINVAL;
1414 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1415 unsigned long arg)
1417 struct multipath *m = (struct multipath *) ti->private;
1418 struct block_device *bdev = NULL;
1419 fmode_t mode = 0;
1420 unsigned long flags;
1421 int r = 0;
1423 spin_lock_irqsave(&m->lock, flags);
1425 if (!m->current_pgpath)
1426 __choose_pgpath(m);
1428 if (m->current_pgpath) {
1429 bdev = m->current_pgpath->path.dev->bdev;
1430 mode = m->current_pgpath->path.dev->mode;
1433 if (m->queue_io)
1434 r = -EAGAIN;
1435 else if (!bdev)
1436 r = -EIO;
1438 spin_unlock_irqrestore(&m->lock, flags);
1440 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1443 /*-----------------------------------------------------------------
1444 * Module setup
1445 *---------------------------------------------------------------*/
1446 static struct target_type multipath_target = {
1447 .name = "multipath",
1448 .version = {1, 0, 5},
1449 .module = THIS_MODULE,
1450 .ctr = multipath_ctr,
1451 .dtr = multipath_dtr,
1452 .map = multipath_map,
1453 .end_io = multipath_end_io,
1454 .presuspend = multipath_presuspend,
1455 .resume = multipath_resume,
1456 .status = multipath_status,
1457 .message = multipath_message,
1458 .ioctl = multipath_ioctl,
1461 static int __init dm_multipath_init(void)
1463 int r;
1465 /* allocate a slab for the dm_ios */
1466 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1467 if (!_mpio_cache)
1468 return -ENOMEM;
1470 r = dm_register_target(&multipath_target);
1471 if (r < 0) {
1472 DMERR("register failed %d", r);
1473 kmem_cache_destroy(_mpio_cache);
1474 return -EINVAL;
1477 kmultipathd = create_workqueue("kmpathd");
1478 if (!kmultipathd) {
1479 DMERR("failed to create workqueue kmpathd");
1480 dm_unregister_target(&multipath_target);
1481 kmem_cache_destroy(_mpio_cache);
1482 return -ENOMEM;
1486 * A separate workqueue is used to handle the device handlers
1487 * to avoid overloading existing workqueue. Overloading the
1488 * old workqueue would also create a bottleneck in the
1489 * path of the storage hardware device activation.
1491 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1492 if (!kmpath_handlerd) {
1493 DMERR("failed to create workqueue kmpath_handlerd");
1494 destroy_workqueue(kmultipathd);
1495 dm_unregister_target(&multipath_target);
1496 kmem_cache_destroy(_mpio_cache);
1497 return -ENOMEM;
1500 DMINFO("version %u.%u.%u loaded",
1501 multipath_target.version[0], multipath_target.version[1],
1502 multipath_target.version[2]);
1504 return r;
1507 static void __exit dm_multipath_exit(void)
1509 destroy_workqueue(kmpath_handlerd);
1510 destroy_workqueue(kmultipathd);
1512 dm_unregister_target(&multipath_target);
1513 kmem_cache_destroy(_mpio_cache);
1516 module_init(dm_multipath_init);
1517 module_exit(dm_multipath_exit);
1519 MODULE_DESCRIPTION(DM_NAME " multipath target");
1520 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1521 MODULE_LICENSE("GPL");