[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / md / dm-mpath.c
blob1e97b3c12bd5dae147963fa7331ae3512554538d
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 struct workqueue_struct *kmultipathd;
105 static void process_queued_ios(void *data);
106 static void trigger_event(void *data);
109 /*-----------------------------------------------
110 * Allocation routines
111 *-----------------------------------------------*/
113 static struct pgpath *alloc_pgpath(void)
115 struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
117 if (pgpath) {
118 memset(pgpath, 0, sizeof(*pgpath));
119 pgpath->path.is_active = 1;
122 return pgpath;
125 static inline void free_pgpath(struct pgpath *pgpath)
127 kfree(pgpath);
130 static struct priority_group *alloc_priority_group(void)
132 struct priority_group *pg;
134 pg = kmalloc(sizeof(*pg), GFP_KERNEL);
135 if (!pg)
136 return NULL;
138 memset(pg, 0, sizeof(*pg));
139 INIT_LIST_HEAD(&pg->pgpaths);
141 return pg;
144 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
146 struct pgpath *pgpath, *tmp;
148 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
149 list_del(&pgpath->list);
150 dm_put_device(ti, pgpath->path.dev);
151 free_pgpath(pgpath);
155 static void free_priority_group(struct priority_group *pg,
156 struct dm_target *ti)
158 struct path_selector *ps = &pg->ps;
160 if (ps->type) {
161 ps->type->destroy(ps);
162 dm_put_path_selector(ps->type);
165 free_pgpaths(&pg->pgpaths, ti);
166 kfree(pg);
169 static struct multipath *alloc_multipath(void)
171 struct multipath *m;
173 m = kmalloc(sizeof(*m), GFP_KERNEL);
174 if (m) {
175 memset(m, 0, sizeof(*m));
176 INIT_LIST_HEAD(&m->priority_groups);
177 spin_lock_init(&m->lock);
178 m->queue_io = 1;
179 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
180 INIT_WORK(&m->trigger_event, trigger_event, m);
181 m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
182 mempool_free_slab, _mpio_cache);
183 if (!m->mpio_pool) {
184 kfree(m);
185 return NULL;
189 return m;
192 static void free_multipath(struct multipath *m)
194 struct priority_group *pg, *tmp;
195 struct hw_handler *hwh = &m->hw_handler;
197 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
198 list_del(&pg->list);
199 free_priority_group(pg, m->ti);
202 if (hwh->type) {
203 hwh->type->destroy(hwh);
204 dm_put_hw_handler(hwh->type);
207 mempool_destroy(m->mpio_pool);
208 kfree(m);
212 /*-----------------------------------------------
213 * Path selection
214 *-----------------------------------------------*/
216 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
218 struct hw_handler *hwh = &m->hw_handler;
220 m->current_pg = pgpath->pg;
222 /* Must we initialise the PG first, and queue I/O till it's ready? */
223 if (hwh->type && hwh->type->pg_init) {
224 m->pg_init_required = 1;
225 m->queue_io = 1;
226 } else {
227 m->pg_init_required = 0;
228 m->queue_io = 0;
232 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
234 struct path *path;
236 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
237 if (!path)
238 return -ENXIO;
240 m->current_pgpath = path_to_pgpath(path);
242 if (m->current_pg != pg)
243 __switch_pg(m, m->current_pgpath);
245 return 0;
248 static void __choose_pgpath(struct multipath *m)
250 struct priority_group *pg;
251 unsigned bypassed = 1;
253 if (!m->nr_valid_paths)
254 goto failed;
256 /* Were we instructed to switch PG? */
257 if (m->next_pg) {
258 pg = m->next_pg;
259 m->next_pg = NULL;
260 if (!__choose_path_in_pg(m, pg))
261 return;
264 /* Don't change PG until it has no remaining paths */
265 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
266 return;
269 * Loop through priority groups until we find a valid path.
270 * First time we skip PGs marked 'bypassed'.
271 * Second time we only try the ones we skipped.
273 do {
274 list_for_each_entry(pg, &m->priority_groups, list) {
275 if (pg->bypassed == bypassed)
276 continue;
277 if (!__choose_path_in_pg(m, pg))
278 return;
280 } while (bypassed--);
282 failed:
283 m->current_pgpath = NULL;
284 m->current_pg = NULL;
287 static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
288 unsigned was_queued)
290 int r = 1;
291 unsigned long flags;
292 struct pgpath *pgpath;
294 spin_lock_irqsave(&m->lock, flags);
296 /* Do we need to select a new pgpath? */
297 if (!m->current_pgpath ||
298 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
299 __choose_pgpath(m);
301 pgpath = m->current_pgpath;
303 if (was_queued)
304 m->queue_size--;
306 if ((pgpath && m->queue_io) ||
307 (!pgpath && m->queue_if_no_path && !m->suspended)) {
308 /* Queue for the daemon to resubmit */
309 bio_list_add(&m->queued_ios, bio);
310 m->queue_size++;
311 if (m->pg_init_required || !m->queue_io)
312 queue_work(kmultipathd, &m->process_queued_ios);
313 pgpath = NULL;
314 r = 0;
315 } else if (!pgpath)
316 r = -EIO; /* Failed */
317 else
318 bio->bi_bdev = pgpath->path.dev->bdev;
320 mpio->pgpath = pgpath;
322 spin_unlock_irqrestore(&m->lock, flags);
324 return r;
328 * If we run out of usable paths, should we queue I/O or error it?
330 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path)
332 unsigned long flags;
334 spin_lock_irqsave(&m->lock, flags);
336 m->queue_if_no_path = queue_if_no_path;
337 if (!m->queue_if_no_path)
338 queue_work(kmultipathd, &m->process_queued_ios);
340 spin_unlock_irqrestore(&m->lock, flags);
342 return 0;
345 /*-----------------------------------------------------------------
346 * The multipath daemon is responsible for resubmitting queued ios.
347 *---------------------------------------------------------------*/
349 static void dispatch_queued_ios(struct multipath *m)
351 int r;
352 unsigned long flags;
353 struct bio *bio = NULL, *next;
354 struct mpath_io *mpio;
355 union map_info *info;
357 spin_lock_irqsave(&m->lock, flags);
358 bio = bio_list_get(&m->queued_ios);
359 spin_unlock_irqrestore(&m->lock, flags);
361 while (bio) {
362 next = bio->bi_next;
363 bio->bi_next = NULL;
365 info = dm_get_mapinfo(bio);
366 mpio = info->ptr;
368 r = map_io(m, bio, mpio, 1);
369 if (r < 0)
370 bio_endio(bio, bio->bi_size, r);
371 else if (r == 1)
372 generic_make_request(bio);
374 bio = next;
378 static void process_queued_ios(void *data)
380 struct multipath *m = (struct multipath *) data;
381 struct hw_handler *hwh = &m->hw_handler;
382 struct pgpath *pgpath;
383 unsigned init_required, must_queue = 0;
384 unsigned long flags;
386 spin_lock_irqsave(&m->lock, flags);
388 if (!m->current_pgpath)
389 __choose_pgpath(m);
391 pgpath = m->current_pgpath;
393 if ((pgpath && m->queue_io) ||
394 (!pgpath && m->queue_if_no_path && !m->suspended))
395 must_queue = 1;
397 init_required = m->pg_init_required;
398 if (init_required)
399 m->pg_init_required = 0;
401 spin_unlock_irqrestore(&m->lock, flags);
403 if (init_required)
404 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
406 if (!must_queue)
407 dispatch_queued_ios(m);
411 * An event is triggered whenever a path is taken out of use.
412 * Includes path failure and PG bypass.
414 static void trigger_event(void *data)
416 struct multipath *m = (struct multipath *) data;
418 dm_table_event(m->ti->table);
421 /*-----------------------------------------------------------------
422 * Constructor/argument parsing:
423 * <#multipath feature args> [<arg>]*
424 * <#hw_handler args> [hw_handler [<arg>]*]
425 * <#priority groups>
426 * <initial priority group>
427 * [<selector> <#selector args> [<arg>]*
428 * <#paths> <#per-path selector args>
429 * [<path> [<arg>]* ]+ ]+
430 *---------------------------------------------------------------*/
431 struct param {
432 unsigned min;
433 unsigned max;
434 char *error;
437 #define ESTR(s) ("dm-multipath: " s)
439 static int read_param(struct param *param, char *str, unsigned *v, char **error)
441 if (!str ||
442 (sscanf(str, "%u", v) != 1) ||
443 (*v < param->min) ||
444 (*v > param->max)) {
445 *error = param->error;
446 return -EINVAL;
449 return 0;
452 struct arg_set {
453 unsigned argc;
454 char **argv;
457 static char *shift(struct arg_set *as)
459 char *r;
461 if (as->argc) {
462 as->argc--;
463 r = *as->argv;
464 as->argv++;
465 return r;
468 return NULL;
471 static void consume(struct arg_set *as, unsigned n)
473 BUG_ON (as->argc < n);
474 as->argc -= n;
475 as->argv += n;
478 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
479 struct dm_target *ti)
481 int r;
482 struct path_selector_type *pst;
483 unsigned ps_argc;
485 static struct param _params[] = {
486 {0, 1024, ESTR("invalid number of path selector args")},
489 pst = dm_get_path_selector(shift(as));
490 if (!pst) {
491 ti->error = ESTR("unknown path selector type");
492 return -EINVAL;
495 r = read_param(_params, shift(as), &ps_argc, &ti->error);
496 if (r)
497 return -EINVAL;
499 r = pst->create(&pg->ps, ps_argc, as->argv);
500 if (r) {
501 dm_put_path_selector(pst);
502 ti->error = ESTR("path selector constructor failed");
503 return r;
506 pg->ps.type = pst;
507 consume(as, ps_argc);
509 return 0;
512 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
513 struct dm_target *ti)
515 int r;
516 struct pgpath *p;
518 /* we need at least a path arg */
519 if (as->argc < 1) {
520 ti->error = ESTR("no device given");
521 return NULL;
524 p = alloc_pgpath();
525 if (!p)
526 return NULL;
528 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
529 dm_table_get_mode(ti->table), &p->path.dev);
530 if (r) {
531 ti->error = ESTR("error getting device");
532 goto bad;
535 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
536 if (r) {
537 dm_put_device(ti, p->path.dev);
538 goto bad;
541 return p;
543 bad:
544 free_pgpath(p);
545 return NULL;
548 static struct priority_group *parse_priority_group(struct arg_set *as,
549 struct multipath *m,
550 struct dm_target *ti)
552 static struct param _params[] = {
553 {1, 1024, ESTR("invalid number of paths")},
554 {0, 1024, ESTR("invalid number of selector args")}
557 int r;
558 unsigned i, nr_selector_args, nr_params;
559 struct priority_group *pg;
561 if (as->argc < 2) {
562 as->argc = 0;
563 ti->error = ESTR("not enough priority group aruments");
564 return NULL;
567 pg = alloc_priority_group();
568 if (!pg) {
569 ti->error = ESTR("couldn't allocate priority group");
570 return NULL;
572 pg->m = m;
574 r = parse_path_selector(as, pg, ti);
575 if (r)
576 goto bad;
579 * read the paths
581 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
582 if (r)
583 goto bad;
585 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
586 if (r)
587 goto bad;
589 nr_params = 1 + nr_selector_args;
590 for (i = 0; i < pg->nr_pgpaths; i++) {
591 struct pgpath *pgpath;
592 struct arg_set path_args;
594 if (as->argc < nr_params)
595 goto bad;
597 path_args.argc = nr_params;
598 path_args.argv = as->argv;
600 pgpath = parse_path(&path_args, &pg->ps, ti);
601 if (!pgpath)
602 goto bad;
604 pgpath->pg = pg;
605 list_add_tail(&pgpath->list, &pg->pgpaths);
606 consume(as, nr_params);
609 return pg;
611 bad:
612 free_priority_group(pg, ti);
613 return NULL;
616 static int parse_hw_handler(struct arg_set *as, struct multipath *m,
617 struct dm_target *ti)
619 int r;
620 struct hw_handler_type *hwht;
621 unsigned hw_argc;
623 static struct param _params[] = {
624 {0, 1024, ESTR("invalid number of hardware handler args")},
627 r = read_param(_params, shift(as), &hw_argc, &ti->error);
628 if (r)
629 return -EINVAL;
631 if (!hw_argc)
632 return 0;
634 hwht = dm_get_hw_handler(shift(as));
635 if (!hwht) {
636 ti->error = ESTR("unknown hardware handler type");
637 return -EINVAL;
640 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
641 if (r) {
642 dm_put_hw_handler(hwht);
643 ti->error = ESTR("hardware handler constructor failed");
644 return r;
647 m->hw_handler.type = hwht;
648 consume(as, hw_argc - 1);
650 return 0;
653 static int parse_features(struct arg_set *as, struct multipath *m,
654 struct dm_target *ti)
656 int r;
657 unsigned argc;
659 static struct param _params[] = {
660 {0, 1, ESTR("invalid number of feature args")},
663 r = read_param(_params, shift(as), &argc, &ti->error);
664 if (r)
665 return -EINVAL;
667 if (!argc)
668 return 0;
670 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
671 return queue_if_no_path(m, 1);
672 else {
673 ti->error = "Unrecognised multipath feature request";
674 return -EINVAL;
678 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
679 char **argv)
681 /* target parameters */
682 static struct param _params[] = {
683 {1, 1024, ESTR("invalid number of priority groups")},
684 {1, 1024, ESTR("invalid initial priority group number")},
687 int r;
688 struct multipath *m;
689 struct arg_set as;
690 unsigned pg_count = 0;
691 unsigned next_pg_num;
693 as.argc = argc;
694 as.argv = argv;
696 m = alloc_multipath();
697 if (!m) {
698 ti->error = ESTR("can't allocate multipath");
699 return -EINVAL;
702 r = parse_features(&as, m, ti);
703 if (r)
704 goto bad;
706 r = parse_hw_handler(&as, m, ti);
707 if (r)
708 goto bad;
710 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
711 if (r)
712 goto bad;
714 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
715 if (r)
716 goto bad;
718 /* parse the priority groups */
719 while (as.argc) {
720 struct priority_group *pg;
722 pg = parse_priority_group(&as, m, ti);
723 if (!pg) {
724 r = -EINVAL;
725 goto bad;
728 m->nr_valid_paths += pg->nr_pgpaths;
729 list_add_tail(&pg->list, &m->priority_groups);
730 pg_count++;
731 pg->pg_num = pg_count;
732 if (!--next_pg_num)
733 m->next_pg = pg;
736 if (pg_count != m->nr_priority_groups) {
737 ti->error = ESTR("priority group count mismatch");
738 r = -EINVAL;
739 goto bad;
742 ti->private = m;
743 m->ti = ti;
745 return 0;
747 bad:
748 free_multipath(m);
749 return r;
752 static void multipath_dtr(struct dm_target *ti)
754 struct multipath *m = (struct multipath *) ti->private;
755 free_multipath(m);
759 * Map bios, recording original fields for later in case we have to resubmit
761 static int multipath_map(struct dm_target *ti, struct bio *bio,
762 union map_info *map_context)
764 int r;
765 struct mpath_io *mpio;
766 struct multipath *m = (struct multipath *) ti->private;
768 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
769 dm_bio_record(&mpio->details, bio);
771 map_context->ptr = mpio;
772 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
773 r = map_io(m, bio, mpio, 0);
774 if (r < 0)
775 mempool_free(mpio, m->mpio_pool);
777 return r;
781 * Take a path out of use.
783 static int fail_path(struct pgpath *pgpath)
785 unsigned long flags;
786 struct multipath *m = pgpath->pg->m;
788 spin_lock_irqsave(&m->lock, flags);
790 if (!pgpath->path.is_active)
791 goto out;
793 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
795 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
796 pgpath->path.is_active = 0;
797 pgpath->fail_count++;
799 m->nr_valid_paths--;
801 if (pgpath == m->current_pgpath)
802 m->current_pgpath = NULL;
804 queue_work(kmultipathd, &m->trigger_event);
806 out:
807 spin_unlock_irqrestore(&m->lock, flags);
809 return 0;
813 * Reinstate a previously-failed path
815 static int reinstate_path(struct pgpath *pgpath)
817 int r = 0;
818 unsigned long flags;
819 struct multipath *m = pgpath->pg->m;
821 spin_lock_irqsave(&m->lock, flags);
823 if (pgpath->path.is_active)
824 goto out;
826 if (!pgpath->pg->ps.type) {
827 DMWARN("Reinstate path not supported by path selector %s",
828 pgpath->pg->ps.type->name);
829 r = -EINVAL;
830 goto out;
833 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
834 if (r)
835 goto out;
837 pgpath->path.is_active = 1;
839 m->current_pgpath = NULL;
840 if (!m->nr_valid_paths++)
841 queue_work(kmultipathd, &m->process_queued_ios);
843 queue_work(kmultipathd, &m->trigger_event);
845 out:
846 spin_unlock_irqrestore(&m->lock, flags);
848 return r;
852 * Fail or reinstate all paths that match the provided struct dm_dev.
854 static int action_dev(struct multipath *m, struct dm_dev *dev,
855 action_fn action)
857 int r = 0;
858 struct pgpath *pgpath;
859 struct priority_group *pg;
861 list_for_each_entry(pg, &m->priority_groups, list) {
862 list_for_each_entry(pgpath, &pg->pgpaths, list) {
863 if (pgpath->path.dev == dev)
864 r = action(pgpath);
868 return r;
872 * Temporarily try to avoid having to use the specified PG
874 static void bypass_pg(struct multipath *m, struct priority_group *pg,
875 int bypassed)
877 unsigned long flags;
879 spin_lock_irqsave(&m->lock, flags);
881 pg->bypassed = bypassed;
882 m->current_pgpath = NULL;
883 m->current_pg = NULL;
885 spin_unlock_irqrestore(&m->lock, flags);
887 queue_work(kmultipathd, &m->trigger_event);
891 * Switch to using the specified PG from the next I/O that gets mapped
893 static int switch_pg_num(struct multipath *m, const char *pgstr)
895 struct priority_group *pg;
896 unsigned pgnum;
897 unsigned long flags;
899 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
900 (pgnum > m->nr_priority_groups)) {
901 DMWARN("invalid PG number supplied to switch_pg_num");
902 return -EINVAL;
905 spin_lock_irqsave(&m->lock, flags);
906 list_for_each_entry(pg, &m->priority_groups, list) {
907 pg->bypassed = 0;
908 if (--pgnum)
909 continue;
911 m->current_pgpath = NULL;
912 m->current_pg = NULL;
913 m->next_pg = pg;
915 spin_unlock_irqrestore(&m->lock, flags);
917 queue_work(kmultipathd, &m->trigger_event);
918 return 0;
922 * Set/clear bypassed status of a PG.
923 * PGs are numbered upwards from 1 in the order they were declared.
925 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
927 struct priority_group *pg;
928 unsigned pgnum;
930 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
931 (pgnum > m->nr_priority_groups)) {
932 DMWARN("invalid PG number supplied to bypass_pg");
933 return -EINVAL;
936 list_for_each_entry(pg, &m->priority_groups, list) {
937 if (!--pgnum)
938 break;
941 bypass_pg(m, pg, bypassed);
942 return 0;
946 * pg_init must call this when it has completed its initialisation
948 void dm_pg_init_complete(struct path *path, unsigned err_flags)
950 struct pgpath *pgpath = path_to_pgpath(path);
951 struct priority_group *pg = pgpath->pg;
952 struct multipath *m = pg->m;
953 unsigned long flags;
955 /* We insist on failing the path if the PG is already bypassed. */
956 if (err_flags && pg->bypassed)
957 err_flags |= MP_FAIL_PATH;
959 if (err_flags & MP_FAIL_PATH)
960 fail_path(pgpath);
962 if (err_flags & MP_BYPASS_PG)
963 bypass_pg(m, pg, 1);
965 spin_lock_irqsave(&m->lock, flags);
966 if (!err_flags)
967 m->queue_io = 0;
968 else {
969 m->current_pgpath = NULL;
970 m->current_pg = NULL;
972 queue_work(kmultipathd, &m->process_queued_ios);
973 spin_unlock_irqrestore(&m->lock, flags);
977 * end_io handling
979 static int do_end_io(struct multipath *m, struct bio *bio,
980 int error, struct mpath_io *mpio)
982 struct hw_handler *hwh = &m->hw_handler;
983 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
985 if (!error)
986 return 0; /* I/O complete */
988 spin_lock(&m->lock);
989 if (!m->nr_valid_paths) {
990 if (!m->queue_if_no_path || m->suspended) {
991 spin_unlock(&m->lock);
992 return -EIO;
993 } else {
994 spin_unlock(&m->lock);
995 goto requeue;
998 spin_unlock(&m->lock);
1000 if (hwh->type && hwh->type->error)
1001 err_flags = hwh->type->error(hwh, bio);
1003 if (mpio->pgpath) {
1004 if (err_flags & MP_FAIL_PATH)
1005 fail_path(mpio->pgpath);
1007 if (err_flags & MP_BYPASS_PG)
1008 bypass_pg(m, mpio->pgpath->pg, 1);
1011 if (err_flags & MP_ERROR_IO)
1012 return -EIO;
1014 requeue:
1015 dm_bio_restore(&mpio->details, bio);
1017 /* queue for the daemon to resubmit or fail */
1018 spin_lock(&m->lock);
1019 bio_list_add(&m->queued_ios, bio);
1020 m->queue_size++;
1021 if (!m->queue_io)
1022 queue_work(kmultipathd, &m->process_queued_ios);
1023 spin_unlock(&m->lock);
1025 return 1; /* io not complete */
1028 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1029 int error, union map_info *map_context)
1031 struct multipath *m = (struct multipath *) ti->private;
1032 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1033 struct pgpath *pgpath = mpio->pgpath;
1034 struct path_selector *ps;
1035 int r;
1037 r = do_end_io(m, bio, error, mpio);
1038 if (pgpath) {
1039 ps = &pgpath->pg->ps;
1040 if (ps->type->end_io)
1041 ps->type->end_io(ps, &pgpath->path);
1043 if (r <= 0)
1044 mempool_free(mpio, m->mpio_pool);
1046 return r;
1050 * Suspend can't complete until all the I/O is processed so if
1051 * the last path failed we will now error any queued I/O.
1053 static void multipath_presuspend(struct dm_target *ti)
1055 struct multipath *m = (struct multipath *) ti->private;
1056 unsigned long flags;
1058 spin_lock_irqsave(&m->lock, flags);
1059 m->suspended = 1;
1060 if (m->queue_if_no_path)
1061 queue_work(kmultipathd, &m->process_queued_ios);
1062 spin_unlock_irqrestore(&m->lock, flags);
1065 static void multipath_resume(struct dm_target *ti)
1067 struct multipath *m = (struct multipath *) ti->private;
1068 unsigned long flags;
1070 spin_lock_irqsave(&m->lock, flags);
1071 m->suspended = 0;
1072 spin_unlock_irqrestore(&m->lock, flags);
1076 * Info output has the following format:
1077 * num_multipath_feature_args [multipath_feature_args]*
1078 * num_handler_status_args [handler_status_args]*
1079 * num_groups init_group_number
1080 * [A|D|E num_ps_status_args [ps_status_args]*
1081 * num_paths num_selector_args
1082 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1084 * Table output has the following format (identical to the constructor string):
1085 * num_feature_args [features_args]*
1086 * num_handler_args hw_handler [hw_handler_args]*
1087 * num_groups init_group_number
1088 * [priority selector-name num_ps_args [ps_args]*
1089 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1091 static int multipath_status(struct dm_target *ti, status_type_t type,
1092 char *result, unsigned int maxlen)
1094 int sz = 0;
1095 unsigned long flags;
1096 struct multipath *m = (struct multipath *) ti->private;
1097 struct hw_handler *hwh = &m->hw_handler;
1098 struct priority_group *pg;
1099 struct pgpath *p;
1100 unsigned pg_num;
1101 char state;
1103 spin_lock_irqsave(&m->lock, flags);
1105 /* Features */
1106 if (type == STATUSTYPE_INFO)
1107 DMEMIT("1 %u ", m->queue_size);
1108 else if (m->queue_if_no_path)
1109 DMEMIT("1 queue_if_no_path ");
1110 else
1111 DMEMIT("0 ");
1113 if (hwh->type && hwh->type->status)
1114 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1115 else if (!hwh->type || type == STATUSTYPE_INFO)
1116 DMEMIT("0 ");
1117 else
1118 DMEMIT("1 %s ", hwh->type->name);
1120 DMEMIT("%u ", m->nr_priority_groups);
1122 if (m->next_pg)
1123 pg_num = m->next_pg->pg_num;
1124 else if (m->current_pg)
1125 pg_num = m->current_pg->pg_num;
1126 else
1127 pg_num = 1;
1129 DMEMIT("%u ", pg_num);
1131 switch (type) {
1132 case STATUSTYPE_INFO:
1133 list_for_each_entry(pg, &m->priority_groups, list) {
1134 if (pg->bypassed)
1135 state = 'D'; /* Disabled */
1136 else if (pg == m->current_pg)
1137 state = 'A'; /* Currently Active */
1138 else
1139 state = 'E'; /* Enabled */
1141 DMEMIT("%c ", state);
1143 if (pg->ps.type->status)
1144 sz += pg->ps.type->status(&pg->ps, NULL, type,
1145 result + sz,
1146 maxlen - sz);
1147 else
1148 DMEMIT("0 ");
1150 DMEMIT("%u %u ", pg->nr_pgpaths,
1151 pg->ps.type->info_args);
1153 list_for_each_entry(p, &pg->pgpaths, list) {
1154 DMEMIT("%s %s %u ", p->path.dev->name,
1155 p->path.is_active ? "A" : "F",
1156 p->fail_count);
1157 if (pg->ps.type->status)
1158 sz += pg->ps.type->status(&pg->ps,
1159 &p->path, type, result + sz,
1160 maxlen - sz);
1163 break;
1165 case STATUSTYPE_TABLE:
1166 list_for_each_entry(pg, &m->priority_groups, list) {
1167 DMEMIT("%s ", pg->ps.type->name);
1169 if (pg->ps.type->status)
1170 sz += pg->ps.type->status(&pg->ps, NULL, type,
1171 result + sz,
1172 maxlen - sz);
1173 else
1174 DMEMIT("0 ");
1176 DMEMIT("%u %u ", pg->nr_pgpaths,
1177 pg->ps.type->table_args);
1179 list_for_each_entry(p, &pg->pgpaths, list) {
1180 DMEMIT("%s ", p->path.dev->name);
1181 if (pg->ps.type->status)
1182 sz += pg->ps.type->status(&pg->ps,
1183 &p->path, type, result + sz,
1184 maxlen - sz);
1187 break;
1190 spin_unlock_irqrestore(&m->lock, flags);
1192 return 0;
1195 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1197 int r;
1198 struct dm_dev *dev;
1199 struct multipath *m = (struct multipath *) ti->private;
1200 action_fn action;
1202 if (argc == 1) {
1203 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1204 return queue_if_no_path(m, 1);
1205 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1206 return queue_if_no_path(m, 0);
1209 if (argc != 2)
1210 goto error;
1212 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1213 return bypass_pg_num(m, argv[1], 1);
1214 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1215 return bypass_pg_num(m, argv[1], 0);
1216 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1217 return switch_pg_num(m, argv[1]);
1218 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1219 action = reinstate_path;
1220 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1221 action = fail_path;
1222 else
1223 goto error;
1225 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1226 dm_table_get_mode(ti->table), &dev);
1227 if (r) {
1228 DMWARN("dm-multipath message: error getting device %s",
1229 argv[1]);
1230 return -EINVAL;
1233 r = action_dev(m, dev, action);
1235 dm_put_device(ti, dev);
1237 return r;
1239 error:
1240 DMWARN("Unrecognised multipath message received.");
1241 return -EINVAL;
1244 /*-----------------------------------------------------------------
1245 * Module setup
1246 *---------------------------------------------------------------*/
1247 static struct target_type multipath_target = {
1248 .name = "multipath",
1249 .version = {1, 0, 4},
1250 .module = THIS_MODULE,
1251 .ctr = multipath_ctr,
1252 .dtr = multipath_dtr,
1253 .map = multipath_map,
1254 .end_io = multipath_end_io,
1255 .presuspend = multipath_presuspend,
1256 .resume = multipath_resume,
1257 .status = multipath_status,
1258 .message = multipath_message,
1261 static int __init dm_multipath_init(void)
1263 int r;
1265 /* allocate a slab for the dm_ios */
1266 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1267 0, 0, NULL, NULL);
1268 if (!_mpio_cache)
1269 return -ENOMEM;
1271 r = dm_register_target(&multipath_target);
1272 if (r < 0) {
1273 DMERR("%s: register failed %d", multipath_target.name, r);
1274 kmem_cache_destroy(_mpio_cache);
1275 return -EINVAL;
1278 kmultipathd = create_workqueue("kmpathd");
1279 if (!kmultipathd) {
1280 DMERR("%s: failed to create workqueue kmpathd",
1281 multipath_target.name);
1282 dm_unregister_target(&multipath_target);
1283 kmem_cache_destroy(_mpio_cache);
1284 return -ENOMEM;
1287 DMINFO("dm-multipath version %u.%u.%u loaded",
1288 multipath_target.version[0], multipath_target.version[1],
1289 multipath_target.version[2]);
1291 return r;
1294 static void __exit dm_multipath_exit(void)
1296 int r;
1298 destroy_workqueue(kmultipathd);
1300 r = dm_unregister_target(&multipath_target);
1301 if (r < 0)
1302 DMERR("%s: target unregister failed %d",
1303 multipath_target.name, r);
1304 kmem_cache_destroy(_mpio_cache);
1307 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1309 module_init(dm_multipath_init);
1310 module_exit(dm_multipath_exit);
1312 MODULE_DESCRIPTION(DM_NAME " multipath target");
1313 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1314 MODULE_LICENSE("GPL");