Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-mpath.c
blob43763a0bd0961f2bae1a52ef0582770fa4600b80
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-hw-handler.h"
11 #include "dm-bio-list.h"
12 #include "dm-bio-record.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 <asm/atomic.h>
24 #define MESG_STR(x) x, sizeof(x)
26 /* Path properties */
27 struct pgpath {
28 struct list_head list;
30 struct priority_group *pg; /* Owning PG */
31 unsigned fail_count; /* Cumulative failure count */
33 struct path path;
36 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
39 * Paths are grouped into Priority Groups and numbered from 1 upwards.
40 * Each has a path selector which controls which path gets used.
42 struct priority_group {
43 struct list_head list;
45 struct multipath *m; /* Owning multipath instance */
46 struct path_selector ps;
48 unsigned pg_num; /* Reference number */
49 unsigned bypassed; /* Temporarily bypass this PG? */
51 unsigned nr_pgpaths; /* Number of paths in PG */
52 struct list_head pgpaths;
55 /* Multipath context */
56 struct multipath {
57 struct list_head list;
58 struct dm_target *ti;
60 spinlock_t lock;
62 struct hw_handler hw_handler;
63 unsigned nr_priority_groups;
64 struct list_head priority_groups;
65 unsigned pg_init_required; /* pg_init needs calling? */
67 unsigned nr_valid_paths; /* Total number of usable paths */
68 struct pgpath *current_pgpath;
69 struct priority_group *current_pg;
70 struct priority_group *next_pg; /* Switch to this PG if set */
71 unsigned repeat_count; /* I/Os left before calling PS again */
73 unsigned queue_io; /* Must we queue all I/O? */
74 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
75 unsigned suspended; /* Has dm core suspended our I/O? */
77 struct work_struct process_queued_ios;
78 struct bio_list queued_ios;
79 unsigned queue_size;
81 struct work_struct trigger_event;
84 * We must use a mempool of mpath_io structs so that we
85 * can resubmit bios on error.
87 mempool_t *mpio_pool;
91 * Context information attached to each bio we process.
93 struct mpath_io {
94 struct pgpath *pgpath;
95 struct dm_bio_details details;
98 typedef int (*action_fn) (struct pgpath *pgpath);
100 #define MIN_IOS 256 /* Mempool size */
102 static kmem_cache_t *_mpio_cache;
104 static void process_queued_ios(void *data);
105 static void trigger_event(void *data);
108 /*-----------------------------------------------
109 * Allocation routines
110 *-----------------------------------------------*/
112 static struct pgpath *alloc_pgpath(void)
114 struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
116 if (pgpath) {
117 memset(pgpath, 0, sizeof(*pgpath));
118 pgpath->path.is_active = 1;
121 return pgpath;
124 static inline void free_pgpath(struct pgpath *pgpath)
126 kfree(pgpath);
129 static struct priority_group *alloc_priority_group(void)
131 struct priority_group *pg;
133 pg = kmalloc(sizeof(*pg), GFP_KERNEL);
134 if (!pg)
135 return NULL;
137 memset(pg, 0, sizeof(*pg));
138 INIT_LIST_HEAD(&pg->pgpaths);
140 return pg;
143 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
145 struct pgpath *pgpath, *tmp;
147 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
148 list_del(&pgpath->list);
149 dm_put_device(ti, pgpath->path.dev);
150 free_pgpath(pgpath);
154 static void free_priority_group(struct priority_group *pg,
155 struct dm_target *ti)
157 struct path_selector *ps = &pg->ps;
159 if (ps->type) {
160 ps->type->destroy(ps);
161 dm_put_path_selector(ps->type);
164 free_pgpaths(&pg->pgpaths, ti);
165 kfree(pg);
168 static struct multipath *alloc_multipath(void)
170 struct multipath *m;
172 m = kmalloc(sizeof(*m), GFP_KERNEL);
173 if (m) {
174 memset(m, 0, sizeof(*m));
175 INIT_LIST_HEAD(&m->priority_groups);
176 spin_lock_init(&m->lock);
177 m->queue_io = 1;
178 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
179 INIT_WORK(&m->trigger_event, trigger_event, m);
180 m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
181 mempool_free_slab, _mpio_cache);
182 if (!m->mpio_pool) {
183 kfree(m);
184 return NULL;
188 return m;
191 static void free_multipath(struct multipath *m)
193 struct priority_group *pg, *tmp;
194 struct hw_handler *hwh = &m->hw_handler;
196 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
197 list_del(&pg->list);
198 free_priority_group(pg, m->ti);
201 if (hwh->type) {
202 hwh->type->destroy(hwh);
203 dm_put_hw_handler(hwh->type);
206 mempool_destroy(m->mpio_pool);
207 kfree(m);
211 /*-----------------------------------------------
212 * Path selection
213 *-----------------------------------------------*/
215 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
217 struct hw_handler *hwh = &m->hw_handler;
219 m->current_pg = pgpath->pg;
221 /* Must we initialise the PG first, and queue I/O till it's ready? */
222 if (hwh->type && hwh->type->pg_init) {
223 m->pg_init_required = 1;
224 m->queue_io = 1;
225 } else {
226 m->pg_init_required = 0;
227 m->queue_io = 0;
231 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
233 struct path *path;
235 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
236 if (!path)
237 return -ENXIO;
239 m->current_pgpath = path_to_pgpath(path);
241 if (m->current_pg != pg)
242 __switch_pg(m, m->current_pgpath);
244 return 0;
247 static void __choose_pgpath(struct multipath *m)
249 struct priority_group *pg;
250 unsigned bypassed = 1;
252 if (!m->nr_valid_paths)
253 goto failed;
255 /* Were we instructed to switch PG? */
256 if (m->next_pg) {
257 pg = m->next_pg;
258 m->next_pg = NULL;
259 if (!__choose_path_in_pg(m, pg))
260 return;
263 /* Don't change PG until it has no remaining paths */
264 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
265 return;
268 * Loop through priority groups until we find a valid path.
269 * First time we skip PGs marked 'bypassed'.
270 * Second time we only try the ones we skipped.
272 do {
273 list_for_each_entry(pg, &m->priority_groups, list) {
274 if (pg->bypassed == bypassed)
275 continue;
276 if (!__choose_path_in_pg(m, pg))
277 return;
279 } while (bypassed--);
281 failed:
282 m->current_pgpath = NULL;
283 m->current_pg = NULL;
286 static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
287 unsigned was_queued)
289 int r = 1;
290 unsigned long flags;
291 struct pgpath *pgpath;
293 spin_lock_irqsave(&m->lock, flags);
295 /* Do we need to select a new pgpath? */
296 if (!m->current_pgpath ||
297 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
298 __choose_pgpath(m);
300 pgpath = m->current_pgpath;
302 if (was_queued)
303 m->queue_size--;
305 if ((pgpath && m->queue_io) ||
306 (!pgpath && m->queue_if_no_path && !m->suspended)) {
307 /* Queue for the daemon to resubmit */
308 bio_list_add(&m->queued_ios, bio);
309 m->queue_size++;
310 if (m->pg_init_required || !m->queue_io)
311 schedule_work(&m->process_queued_ios);
312 pgpath = NULL;
313 r = 0;
314 } else if (!pgpath)
315 r = -EIO; /* Failed */
316 else
317 bio->bi_bdev = pgpath->path.dev->bdev;
319 mpio->pgpath = pgpath;
321 spin_unlock_irqrestore(&m->lock, flags);
323 return r;
327 * If we run out of usable paths, should we queue I/O or error it?
329 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path)
331 unsigned long flags;
333 spin_lock_irqsave(&m->lock, flags);
335 m->queue_if_no_path = queue_if_no_path;
336 if (!m->queue_if_no_path)
337 schedule_work(&m->process_queued_ios);
339 spin_unlock_irqrestore(&m->lock, flags);
341 return 0;
344 /*-----------------------------------------------------------------
345 * The multipath daemon is responsible for resubmitting queued ios.
346 *---------------------------------------------------------------*/
348 static void dispatch_queued_ios(struct multipath *m)
350 int r;
351 unsigned long flags;
352 struct bio *bio = NULL, *next;
353 struct mpath_io *mpio;
354 union map_info *info;
356 spin_lock_irqsave(&m->lock, flags);
357 bio = bio_list_get(&m->queued_ios);
358 spin_unlock_irqrestore(&m->lock, flags);
360 while (bio) {
361 next = bio->bi_next;
362 bio->bi_next = NULL;
364 info = dm_get_mapinfo(bio);
365 mpio = info->ptr;
367 r = map_io(m, bio, mpio, 1);
368 if (r < 0)
369 bio_endio(bio, bio->bi_size, r);
370 else if (r == 1)
371 generic_make_request(bio);
373 bio = next;
377 static void process_queued_ios(void *data)
379 struct multipath *m = (struct multipath *) data;
380 struct hw_handler *hwh = &m->hw_handler;
381 struct pgpath *pgpath;
382 unsigned init_required, must_queue = 0;
383 unsigned long flags;
385 spin_lock_irqsave(&m->lock, flags);
387 if (!m->current_pgpath)
388 __choose_pgpath(m);
390 pgpath = m->current_pgpath;
392 if ((pgpath && m->queue_io) ||
393 (!pgpath && m->queue_if_no_path && !m->suspended))
394 must_queue = 1;
396 init_required = m->pg_init_required;
397 if (init_required)
398 m->pg_init_required = 0;
400 spin_unlock_irqrestore(&m->lock, flags);
402 if (init_required)
403 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
405 if (!must_queue)
406 dispatch_queued_ios(m);
410 * An event is triggered whenever a path is taken out of use.
411 * Includes path failure and PG bypass.
413 static void trigger_event(void *data)
415 struct multipath *m = (struct multipath *) data;
417 dm_table_event(m->ti->table);
420 /*-----------------------------------------------------------------
421 * Constructor/argument parsing:
422 * <#multipath feature args> [<arg>]*
423 * <#hw_handler args> [hw_handler [<arg>]*]
424 * <#priority groups>
425 * <initial priority group>
426 * [<selector> <#selector args> [<arg>]*
427 * <#paths> <#per-path selector args>
428 * [<path> [<arg>]* ]+ ]+
429 *---------------------------------------------------------------*/
430 struct param {
431 unsigned min;
432 unsigned max;
433 char *error;
436 #define ESTR(s) ("dm-multipath: " s)
438 static int read_param(struct param *param, char *str, unsigned *v, char **error)
440 if (!str ||
441 (sscanf(str, "%u", v) != 1) ||
442 (*v < param->min) ||
443 (*v > param->max)) {
444 *error = param->error;
445 return -EINVAL;
448 return 0;
451 struct arg_set {
452 unsigned argc;
453 char **argv;
456 static char *shift(struct arg_set *as)
458 char *r;
460 if (as->argc) {
461 as->argc--;
462 r = *as->argv;
463 as->argv++;
464 return r;
467 return NULL;
470 static void consume(struct arg_set *as, unsigned n)
472 BUG_ON (as->argc < n);
473 as->argc -= n;
474 as->argv += n;
477 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
478 struct dm_target *ti)
480 int r;
481 struct path_selector_type *pst;
482 unsigned ps_argc;
484 static struct param _params[] = {
485 {0, 1024, ESTR("invalid number of path selector args")},
488 pst = dm_get_path_selector(shift(as));
489 if (!pst) {
490 ti->error = ESTR("unknown path selector type");
491 return -EINVAL;
494 r = read_param(_params, shift(as), &ps_argc, &ti->error);
495 if (r)
496 return -EINVAL;
498 r = pst->create(&pg->ps, ps_argc, as->argv);
499 if (r) {
500 dm_put_path_selector(pst);
501 ti->error = ESTR("path selector constructor failed");
502 return r;
505 pg->ps.type = pst;
506 consume(as, ps_argc);
508 return 0;
511 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
512 struct dm_target *ti)
514 int r;
515 struct pgpath *p;
517 /* we need at least a path arg */
518 if (as->argc < 1) {
519 ti->error = ESTR("no device given");
520 return NULL;
523 p = alloc_pgpath();
524 if (!p)
525 return NULL;
527 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
528 dm_table_get_mode(ti->table), &p->path.dev);
529 if (r) {
530 ti->error = ESTR("error getting device");
531 goto bad;
534 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
535 if (r) {
536 dm_put_device(ti, p->path.dev);
537 goto bad;
540 return p;
542 bad:
543 free_pgpath(p);
544 return NULL;
547 static struct priority_group *parse_priority_group(struct arg_set *as,
548 struct multipath *m,
549 struct dm_target *ti)
551 static struct param _params[] = {
552 {1, 1024, ESTR("invalid number of paths")},
553 {0, 1024, ESTR("invalid number of selector args")}
556 int r;
557 unsigned i, nr_selector_args, nr_params;
558 struct priority_group *pg;
560 if (as->argc < 2) {
561 as->argc = 0;
562 ti->error = ESTR("not enough priority group aruments");
563 return NULL;
566 pg = alloc_priority_group();
567 if (!pg) {
568 ti->error = ESTR("couldn't allocate priority group");
569 return NULL;
571 pg->m = m;
573 r = parse_path_selector(as, pg, ti);
574 if (r)
575 goto bad;
578 * read the paths
580 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
581 if (r)
582 goto bad;
584 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
585 if (r)
586 goto bad;
588 nr_params = 1 + nr_selector_args;
589 for (i = 0; i < pg->nr_pgpaths; i++) {
590 struct pgpath *pgpath;
591 struct arg_set path_args;
593 if (as->argc < nr_params)
594 goto bad;
596 path_args.argc = nr_params;
597 path_args.argv = as->argv;
599 pgpath = parse_path(&path_args, &pg->ps, ti);
600 if (!pgpath)
601 goto bad;
603 pgpath->pg = pg;
604 list_add_tail(&pgpath->list, &pg->pgpaths);
605 consume(as, nr_params);
608 return pg;
610 bad:
611 free_priority_group(pg, ti);
612 return NULL;
615 static int parse_hw_handler(struct arg_set *as, struct multipath *m,
616 struct dm_target *ti)
618 int r;
619 struct hw_handler_type *hwht;
620 unsigned hw_argc;
622 static struct param _params[] = {
623 {0, 1024, ESTR("invalid number of hardware handler args")},
626 r = read_param(_params, shift(as), &hw_argc, &ti->error);
627 if (r)
628 return -EINVAL;
630 if (!hw_argc)
631 return 0;
633 hwht = dm_get_hw_handler(shift(as));
634 if (!hwht) {
635 ti->error = ESTR("unknown hardware handler type");
636 return -EINVAL;
639 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
640 if (r) {
641 dm_put_hw_handler(hwht);
642 ti->error = ESTR("hardware handler constructor failed");
643 return r;
646 m->hw_handler.type = hwht;
647 consume(as, hw_argc - 1);
649 return 0;
652 static int parse_features(struct arg_set *as, struct multipath *m,
653 struct dm_target *ti)
655 int r;
656 unsigned argc;
658 static struct param _params[] = {
659 {0, 1, ESTR("invalid number of feature args")},
662 r = read_param(_params, shift(as), &argc, &ti->error);
663 if (r)
664 return -EINVAL;
666 if (!argc)
667 return 0;
669 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
670 return queue_if_no_path(m, 1);
671 else {
672 ti->error = "Unrecognised multipath feature request";
673 return -EINVAL;
677 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
678 char **argv)
680 /* target parameters */
681 static struct param _params[] = {
682 {1, 1024, ESTR("invalid number of priority groups")},
683 {1, 1024, ESTR("invalid initial priority group number")},
686 int r;
687 struct multipath *m;
688 struct arg_set as;
689 unsigned pg_count = 0;
690 unsigned next_pg_num;
692 as.argc = argc;
693 as.argv = argv;
695 m = alloc_multipath();
696 if (!m) {
697 ti->error = ESTR("can't allocate multipath");
698 return -EINVAL;
701 r = parse_features(&as, m, ti);
702 if (r)
703 goto bad;
705 r = parse_hw_handler(&as, m, ti);
706 if (r)
707 goto bad;
709 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
710 if (r)
711 goto bad;
713 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
714 if (r)
715 goto bad;
717 /* parse the priority groups */
718 while (as.argc) {
719 struct priority_group *pg;
721 pg = parse_priority_group(&as, m, ti);
722 if (!pg) {
723 r = -EINVAL;
724 goto bad;
727 m->nr_valid_paths += pg->nr_pgpaths;
728 list_add_tail(&pg->list, &m->priority_groups);
729 pg_count++;
730 pg->pg_num = pg_count;
731 if (!--next_pg_num)
732 m->next_pg = pg;
735 if (pg_count != m->nr_priority_groups) {
736 ti->error = ESTR("priority group count mismatch");
737 r = -EINVAL;
738 goto bad;
741 ti->private = m;
742 m->ti = ti;
744 return 0;
746 bad:
747 free_multipath(m);
748 return r;
751 static void multipath_dtr(struct dm_target *ti)
753 struct multipath *m = (struct multipath *) ti->private;
754 free_multipath(m);
758 * Map bios, recording original fields for later in case we have to resubmit
760 static int multipath_map(struct dm_target *ti, struct bio *bio,
761 union map_info *map_context)
763 int r;
764 struct mpath_io *mpio;
765 struct multipath *m = (struct multipath *) ti->private;
767 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
768 dm_bio_record(&mpio->details, bio);
770 map_context->ptr = mpio;
771 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
772 r = map_io(m, bio, mpio, 0);
773 if (r < 0)
774 mempool_free(mpio, m->mpio_pool);
776 return r;
780 * Take a path out of use.
782 static int fail_path(struct pgpath *pgpath)
784 unsigned long flags;
785 struct multipath *m = pgpath->pg->m;
787 spin_lock_irqsave(&m->lock, flags);
789 if (!pgpath->path.is_active)
790 goto out;
792 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
794 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
795 pgpath->path.is_active = 0;
796 pgpath->fail_count++;
798 m->nr_valid_paths--;
800 if (pgpath == m->current_pgpath)
801 m->current_pgpath = NULL;
803 schedule_work(&m->trigger_event);
805 out:
806 spin_unlock_irqrestore(&m->lock, flags);
808 return 0;
812 * Reinstate a previously-failed path
814 static int reinstate_path(struct pgpath *pgpath)
816 int r = 0;
817 unsigned long flags;
818 struct multipath *m = pgpath->pg->m;
820 spin_lock_irqsave(&m->lock, flags);
822 if (pgpath->path.is_active)
823 goto out;
825 if (!pgpath->pg->ps.type) {
826 DMWARN("Reinstate path not supported by path selector %s",
827 pgpath->pg->ps.type->name);
828 r = -EINVAL;
829 goto out;
832 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
833 if (r)
834 goto out;
836 pgpath->path.is_active = 1;
838 m->current_pgpath = NULL;
839 if (!m->nr_valid_paths++)
840 schedule_work(&m->process_queued_ios);
842 schedule_work(&m->trigger_event);
844 out:
845 spin_unlock_irqrestore(&m->lock, flags);
847 return r;
851 * Fail or reinstate all paths that match the provided struct dm_dev.
853 static int action_dev(struct multipath *m, struct dm_dev *dev,
854 action_fn action)
856 int r = 0;
857 struct pgpath *pgpath;
858 struct priority_group *pg;
860 list_for_each_entry(pg, &m->priority_groups, list) {
861 list_for_each_entry(pgpath, &pg->pgpaths, list) {
862 if (pgpath->path.dev == dev)
863 r = action(pgpath);
867 return r;
871 * Temporarily try to avoid having to use the specified PG
873 static void bypass_pg(struct multipath *m, struct priority_group *pg,
874 int bypassed)
876 unsigned long flags;
878 spin_lock_irqsave(&m->lock, flags);
880 pg->bypassed = bypassed;
881 m->current_pgpath = NULL;
882 m->current_pg = NULL;
884 spin_unlock_irqrestore(&m->lock, flags);
886 schedule_work(&m->trigger_event);
890 * Switch to using the specified PG from the next I/O that gets mapped
892 static int switch_pg_num(struct multipath *m, const char *pgstr)
894 struct priority_group *pg;
895 unsigned pgnum;
896 unsigned long flags;
898 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
899 (pgnum > m->nr_priority_groups)) {
900 DMWARN("invalid PG number supplied to switch_pg_num");
901 return -EINVAL;
904 spin_lock_irqsave(&m->lock, flags);
905 list_for_each_entry(pg, &m->priority_groups, list) {
906 pg->bypassed = 0;
907 if (--pgnum)
908 continue;
910 m->current_pgpath = NULL;
911 m->current_pg = NULL;
912 m->next_pg = pg;
914 spin_unlock_irqrestore(&m->lock, flags);
916 schedule_work(&m->trigger_event);
917 return 0;
921 * Set/clear bypassed status of a PG.
922 * PGs are numbered upwards from 1 in the order they were declared.
924 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
926 struct priority_group *pg;
927 unsigned pgnum;
929 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
930 (pgnum > m->nr_priority_groups)) {
931 DMWARN("invalid PG number supplied to bypass_pg");
932 return -EINVAL;
935 list_for_each_entry(pg, &m->priority_groups, list) {
936 if (!--pgnum)
937 break;
940 bypass_pg(m, pg, bypassed);
941 return 0;
945 * pg_init must call this when it has completed its initialisation
947 void dm_pg_init_complete(struct path *path, unsigned err_flags)
949 struct pgpath *pgpath = path_to_pgpath(path);
950 struct priority_group *pg = pgpath->pg;
951 struct multipath *m = pg->m;
952 unsigned long flags;
954 /* We insist on failing the path if the PG is already bypassed. */
955 if (err_flags && pg->bypassed)
956 err_flags |= MP_FAIL_PATH;
958 if (err_flags & MP_FAIL_PATH)
959 fail_path(pgpath);
961 if (err_flags & MP_BYPASS_PG)
962 bypass_pg(m, pg, 1);
964 spin_lock_irqsave(&m->lock, flags);
965 if (!err_flags)
966 m->queue_io = 0;
967 else {
968 m->current_pgpath = NULL;
969 m->current_pg = NULL;
971 schedule_work(&m->process_queued_ios);
972 spin_unlock_irqrestore(&m->lock, flags);
976 * end_io handling
978 static int do_end_io(struct multipath *m, struct bio *bio,
979 int error, struct mpath_io *mpio)
981 struct hw_handler *hwh = &m->hw_handler;
982 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
984 if (!error)
985 return 0; /* I/O complete */
987 spin_lock(&m->lock);
988 if (!m->nr_valid_paths) {
989 if (!m->queue_if_no_path || m->suspended) {
990 spin_unlock(&m->lock);
991 return -EIO;
992 } else {
993 spin_unlock(&m->lock);
994 goto requeue;
997 spin_unlock(&m->lock);
999 if (hwh->type && hwh->type->error)
1000 err_flags = hwh->type->error(hwh, bio);
1002 if (mpio->pgpath) {
1003 if (err_flags & MP_FAIL_PATH)
1004 fail_path(mpio->pgpath);
1006 if (err_flags & MP_BYPASS_PG)
1007 bypass_pg(m, mpio->pgpath->pg, 1);
1010 if (err_flags & MP_ERROR_IO)
1011 return -EIO;
1013 requeue:
1014 dm_bio_restore(&mpio->details, bio);
1016 /* queue for the daemon to resubmit or fail */
1017 spin_lock(&m->lock);
1018 bio_list_add(&m->queued_ios, bio);
1019 m->queue_size++;
1020 if (!m->queue_io)
1021 schedule_work(&m->process_queued_ios);
1022 spin_unlock(&m->lock);
1024 return 1; /* io not complete */
1027 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1028 int error, union map_info *map_context)
1030 struct multipath *m = (struct multipath *) ti->private;
1031 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1032 struct pgpath *pgpath = mpio->pgpath;
1033 struct path_selector *ps;
1034 int r;
1036 r = do_end_io(m, bio, error, mpio);
1037 if (pgpath) {
1038 ps = &pgpath->pg->ps;
1039 if (ps->type->end_io)
1040 ps->type->end_io(ps, &pgpath->path);
1042 if (r <= 0)
1043 mempool_free(mpio, m->mpio_pool);
1045 return r;
1049 * Suspend can't complete until all the I/O is processed so if
1050 * the last path failed we will now error any queued I/O.
1052 static void multipath_presuspend(struct dm_target *ti)
1054 struct multipath *m = (struct multipath *) ti->private;
1055 unsigned long flags;
1057 spin_lock_irqsave(&m->lock, flags);
1058 m->suspended = 1;
1059 if (m->queue_if_no_path)
1060 schedule_work(&m->process_queued_ios);
1061 spin_unlock_irqrestore(&m->lock, flags);
1064 static void multipath_resume(struct dm_target *ti)
1066 struct multipath *m = (struct multipath *) ti->private;
1067 unsigned long flags;
1069 spin_lock_irqsave(&m->lock, flags);
1070 m->suspended = 0;
1071 spin_unlock_irqrestore(&m->lock, flags);
1075 * Info output has the following format:
1076 * num_multipath_feature_args [multipath_feature_args]*
1077 * num_handler_status_args [handler_status_args]*
1078 * num_groups init_group_number
1079 * [A|D|E num_ps_status_args [ps_status_args]*
1080 * num_paths num_selector_args
1081 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1083 * Table output has the following format (identical to the constructor string):
1084 * num_feature_args [features_args]*
1085 * num_handler_args hw_handler [hw_handler_args]*
1086 * num_groups init_group_number
1087 * [priority selector-name num_ps_args [ps_args]*
1088 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1090 static int multipath_status(struct dm_target *ti, status_type_t type,
1091 char *result, unsigned int maxlen)
1093 int sz = 0;
1094 unsigned long flags;
1095 struct multipath *m = (struct multipath *) ti->private;
1096 struct hw_handler *hwh = &m->hw_handler;
1097 struct priority_group *pg;
1098 struct pgpath *p;
1099 unsigned pg_num;
1100 char state;
1102 spin_lock_irqsave(&m->lock, flags);
1104 /* Features */
1105 if (type == STATUSTYPE_INFO)
1106 DMEMIT("1 %u ", m->queue_size);
1107 else if (m->queue_if_no_path)
1108 DMEMIT("1 queue_if_no_path ");
1109 else
1110 DMEMIT("0 ");
1112 if (hwh->type && hwh->type->status)
1113 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1114 else if (!hwh->type || type == STATUSTYPE_INFO)
1115 DMEMIT("0 ");
1116 else
1117 DMEMIT("1 %s ", hwh->type->name);
1119 DMEMIT("%u ", m->nr_priority_groups);
1121 if (m->next_pg)
1122 pg_num = m->next_pg->pg_num;
1123 else if (m->current_pg)
1124 pg_num = m->current_pg->pg_num;
1125 else
1126 pg_num = 1;
1128 DMEMIT("%u ", pg_num);
1130 switch (type) {
1131 case STATUSTYPE_INFO:
1132 list_for_each_entry(pg, &m->priority_groups, list) {
1133 if (pg->bypassed)
1134 state = 'D'; /* Disabled */
1135 else if (pg == m->current_pg)
1136 state = 'A'; /* Currently Active */
1137 else
1138 state = 'E'; /* Enabled */
1140 DMEMIT("%c ", state);
1142 if (pg->ps.type->status)
1143 sz += pg->ps.type->status(&pg->ps, NULL, type,
1144 result + sz,
1145 maxlen - sz);
1146 else
1147 DMEMIT("0 ");
1149 DMEMIT("%u %u ", pg->nr_pgpaths,
1150 pg->ps.type->info_args);
1152 list_for_each_entry(p, &pg->pgpaths, list) {
1153 DMEMIT("%s %s %u ", p->path.dev->name,
1154 p->path.is_active ? "A" : "F",
1155 p->fail_count);
1156 if (pg->ps.type->status)
1157 sz += pg->ps.type->status(&pg->ps,
1158 &p->path, type, result + sz,
1159 maxlen - sz);
1162 break;
1164 case STATUSTYPE_TABLE:
1165 list_for_each_entry(pg, &m->priority_groups, list) {
1166 DMEMIT("%s ", pg->ps.type->name);
1168 if (pg->ps.type->status)
1169 sz += pg->ps.type->status(&pg->ps, NULL, type,
1170 result + sz,
1171 maxlen - sz);
1172 else
1173 DMEMIT("0 ");
1175 DMEMIT("%u %u ", pg->nr_pgpaths,
1176 pg->ps.type->table_args);
1178 list_for_each_entry(p, &pg->pgpaths, list) {
1179 DMEMIT("%s ", p->path.dev->name);
1180 if (pg->ps.type->status)
1181 sz += pg->ps.type->status(&pg->ps,
1182 &p->path, type, result + sz,
1183 maxlen - sz);
1186 break;
1189 spin_unlock_irqrestore(&m->lock, flags);
1191 return 0;
1194 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1196 int r;
1197 struct dm_dev *dev;
1198 struct multipath *m = (struct multipath *) ti->private;
1199 action_fn action;
1201 if (argc == 1) {
1202 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1203 return queue_if_no_path(m, 1);
1204 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1205 return queue_if_no_path(m, 0);
1208 if (argc != 2)
1209 goto error;
1211 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1212 return bypass_pg_num(m, argv[1], 1);
1213 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1214 return bypass_pg_num(m, argv[1], 0);
1215 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1216 return switch_pg_num(m, argv[1]);
1217 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1218 action = reinstate_path;
1219 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1220 action = fail_path;
1221 else
1222 goto error;
1224 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1225 dm_table_get_mode(ti->table), &dev);
1226 if (r) {
1227 DMWARN("dm-multipath message: error getting device %s",
1228 argv[1]);
1229 return -EINVAL;
1232 r = action_dev(m, dev, action);
1234 dm_put_device(ti, dev);
1236 return r;
1238 error:
1239 DMWARN("Unrecognised multipath message received.");
1240 return -EINVAL;
1243 /*-----------------------------------------------------------------
1244 * Module setup
1245 *---------------------------------------------------------------*/
1246 static struct target_type multipath_target = {
1247 .name = "multipath",
1248 .version = {1, 0, 4},
1249 .module = THIS_MODULE,
1250 .ctr = multipath_ctr,
1251 .dtr = multipath_dtr,
1252 .map = multipath_map,
1253 .end_io = multipath_end_io,
1254 .presuspend = multipath_presuspend,
1255 .resume = multipath_resume,
1256 .status = multipath_status,
1257 .message = multipath_message,
1260 static int __init dm_multipath_init(void)
1262 int r;
1264 /* allocate a slab for the dm_ios */
1265 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1266 0, 0, NULL, NULL);
1267 if (!_mpio_cache)
1268 return -ENOMEM;
1270 r = dm_register_target(&multipath_target);
1271 if (r < 0) {
1272 DMERR("%s: register failed %d", multipath_target.name, r);
1273 kmem_cache_destroy(_mpio_cache);
1274 return -EINVAL;
1277 DMINFO("dm-multipath version %u.%u.%u loaded",
1278 multipath_target.version[0], multipath_target.version[1],
1279 multipath_target.version[2]);
1281 return r;
1284 static void __exit dm_multipath_exit(void)
1286 int r;
1288 r = dm_unregister_target(&multipath_target);
1289 if (r < 0)
1290 DMERR("%s: target unregister failed %d",
1291 multipath_target.name, r);
1292 kmem_cache_destroy(_mpio_cache);
1295 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1297 module_init(dm_multipath_init);
1298 module_exit(dm_multipath_exit);
1300 MODULE_DESCRIPTION(DM_NAME " multipath target");
1301 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1302 MODULE_LICENSE("GPL");