sync hh.org
[hh.org.git] / drivers / md / dm-mpath.c
blobd754e0bc6e90c09f8ea73311e509e3d0a88f707d
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 DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
27 /* Path properties */
28 struct pgpath {
29 struct list_head list;
31 struct priority_group *pg; /* Owning PG */
32 unsigned fail_count; /* Cumulative failure count */
34 struct path path;
37 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40 * Paths are grouped into Priority Groups and numbered from 1 upwards.
41 * Each has a path selector which controls which path gets used.
43 struct priority_group {
44 struct list_head list;
46 struct multipath *m; /* Owning multipath instance */
47 struct path_selector ps;
49 unsigned pg_num; /* Reference number */
50 unsigned bypassed; /* Temporarily bypass this PG? */
52 unsigned nr_pgpaths; /* Number of paths in PG */
53 struct list_head pgpaths;
56 /* Multipath context */
57 struct multipath {
58 struct list_head list;
59 struct dm_target *ti;
61 spinlock_t lock;
63 struct hw_handler hw_handler;
64 unsigned nr_priority_groups;
65 struct list_head priority_groups;
66 unsigned pg_init_required; /* pg_init needs calling? */
67 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
69 unsigned nr_valid_paths; /* Total number of usable paths */
70 struct pgpath *current_pgpath;
71 struct priority_group *current_pg;
72 struct priority_group *next_pg; /* Switch to this PG if set */
73 unsigned repeat_count; /* I/Os left before calling PS again */
75 unsigned queue_io; /* Must we queue all I/O? */
76 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
77 unsigned saved_queue_if_no_path;/* Saved state during suspension */
79 struct work_struct process_queued_ios;
80 struct bio_list queued_ios;
81 unsigned queue_size;
83 struct work_struct trigger_event;
86 * We must use a mempool of mpath_io structs so that we
87 * can resubmit bios on error.
89 mempool_t *mpio_pool;
93 * Context information attached to each bio we process.
95 struct mpath_io {
96 struct pgpath *pgpath;
97 struct dm_bio_details details;
100 typedef int (*action_fn) (struct pgpath *pgpath);
102 #define MIN_IOS 256 /* Mempool size */
104 static kmem_cache_t *_mpio_cache;
106 struct workqueue_struct *kmultipathd;
107 static void process_queued_ios(void *data);
108 static void trigger_event(void *data);
111 /*-----------------------------------------------
112 * Allocation routines
113 *-----------------------------------------------*/
115 static struct pgpath *alloc_pgpath(void)
117 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
119 if (pgpath)
120 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 = kzalloc(sizeof(*pg), GFP_KERNEL);
136 if (pg)
137 INIT_LIST_HEAD(&pg->pgpaths);
139 return pg;
142 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
144 struct pgpath *pgpath, *tmp;
146 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
147 list_del(&pgpath->list);
148 dm_put_device(ti, pgpath->path.dev);
149 free_pgpath(pgpath);
153 static void free_priority_group(struct priority_group *pg,
154 struct dm_target *ti)
156 struct path_selector *ps = &pg->ps;
158 if (ps->type) {
159 ps->type->destroy(ps);
160 dm_put_path_selector(ps->type);
163 free_pgpaths(&pg->pgpaths, ti);
164 kfree(pg);
167 static struct multipath *alloc_multipath(struct dm_target *ti)
169 struct multipath *m;
171 m = kzalloc(sizeof(*m), GFP_KERNEL);
172 if (m) {
173 INIT_LIST_HEAD(&m->priority_groups);
174 spin_lock_init(&m->lock);
175 m->queue_io = 1;
176 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
177 INIT_WORK(&m->trigger_event, trigger_event, m);
178 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
179 if (!m->mpio_pool) {
180 kfree(m);
181 return NULL;
183 m->ti = ti;
184 ti->private = m;
187 return m;
190 static void free_multipath(struct multipath *m)
192 struct priority_group *pg, *tmp;
193 struct hw_handler *hwh = &m->hw_handler;
195 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
196 list_del(&pg->list);
197 free_priority_group(pg, m->ti);
200 if (hwh->type) {
201 hwh->type->destroy(hwh);
202 dm_put_hw_handler(hwh->type);
205 mempool_destroy(m->mpio_pool);
206 kfree(m);
210 /*-----------------------------------------------
211 * Path selection
212 *-----------------------------------------------*/
214 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
216 struct hw_handler *hwh = &m->hw_handler;
218 m->current_pg = pgpath->pg;
220 /* Must we initialise the PG first, and queue I/O till it's ready? */
221 if (hwh->type && hwh->type->pg_init) {
222 m->pg_init_required = 1;
223 m->queue_io = 1;
224 } else {
225 m->pg_init_required = 0;
226 m->queue_io = 0;
230 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
232 struct path *path;
234 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
235 if (!path)
236 return -ENXIO;
238 m->current_pgpath = path_to_pgpath(path);
240 if (m->current_pg != pg)
241 __switch_pg(m, m->current_pgpath);
243 return 0;
246 static void __choose_pgpath(struct multipath *m)
248 struct priority_group *pg;
249 unsigned bypassed = 1;
251 if (!m->nr_valid_paths)
252 goto failed;
254 /* Were we instructed to switch PG? */
255 if (m->next_pg) {
256 pg = m->next_pg;
257 m->next_pg = NULL;
258 if (!__choose_path_in_pg(m, pg))
259 return;
262 /* Don't change PG until it has no remaining paths */
263 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
264 return;
267 * Loop through priority groups until we find a valid path.
268 * First time we skip PGs marked 'bypassed'.
269 * Second time we only try the ones we skipped.
271 do {
272 list_for_each_entry(pg, &m->priority_groups, list) {
273 if (pg->bypassed == bypassed)
274 continue;
275 if (!__choose_path_in_pg(m, pg))
276 return;
278 } while (bypassed--);
280 failed:
281 m->current_pgpath = NULL;
282 m->current_pg = NULL;
285 static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
286 unsigned was_queued)
288 int r = 1;
289 unsigned long flags;
290 struct pgpath *pgpath;
292 spin_lock_irqsave(&m->lock, flags);
294 /* Do we need to select a new pgpath? */
295 if (!m->current_pgpath ||
296 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
297 __choose_pgpath(m);
299 pgpath = m->current_pgpath;
301 if (was_queued)
302 m->queue_size--;
304 if ((pgpath && m->queue_io) ||
305 (!pgpath && m->queue_if_no_path)) {
306 /* Queue for the daemon to resubmit */
307 bio_list_add(&m->queued_ios, bio);
308 m->queue_size++;
309 if ((m->pg_init_required && !m->pg_init_in_progress) ||
310 !m->queue_io)
311 queue_work(kmultipathd, &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,
330 unsigned save_old_value)
332 unsigned long flags;
334 spin_lock_irqsave(&m->lock, flags);
336 if (save_old_value)
337 m->saved_queue_if_no_path = m->queue_if_no_path;
338 else
339 m->saved_queue_if_no_path = queue_if_no_path;
340 m->queue_if_no_path = queue_if_no_path;
341 if (!m->queue_if_no_path && m->queue_size)
342 queue_work(kmultipathd, &m->process_queued_ios);
344 spin_unlock_irqrestore(&m->lock, flags);
346 return 0;
349 /*-----------------------------------------------------------------
350 * The multipath daemon is responsible for resubmitting queued ios.
351 *---------------------------------------------------------------*/
353 static void dispatch_queued_ios(struct multipath *m)
355 int r;
356 unsigned long flags;
357 struct bio *bio = NULL, *next;
358 struct mpath_io *mpio;
359 union map_info *info;
361 spin_lock_irqsave(&m->lock, flags);
362 bio = bio_list_get(&m->queued_ios);
363 spin_unlock_irqrestore(&m->lock, flags);
365 while (bio) {
366 next = bio->bi_next;
367 bio->bi_next = NULL;
369 info = dm_get_mapinfo(bio);
370 mpio = info->ptr;
372 r = map_io(m, bio, mpio, 1);
373 if (r < 0)
374 bio_endio(bio, bio->bi_size, r);
375 else if (r == 1)
376 generic_make_request(bio);
378 bio = next;
382 static void process_queued_ios(void *data)
384 struct multipath *m = (struct multipath *) data;
385 struct hw_handler *hwh = &m->hw_handler;
386 struct pgpath *pgpath = NULL;
387 unsigned init_required = 0, must_queue = 1;
388 unsigned long flags;
390 spin_lock_irqsave(&m->lock, flags);
392 if (!m->queue_size)
393 goto out;
395 if (!m->current_pgpath)
396 __choose_pgpath(m);
398 pgpath = m->current_pgpath;
400 if ((pgpath && !m->queue_io) ||
401 (!pgpath && !m->queue_if_no_path))
402 must_queue = 0;
404 if (m->pg_init_required && !m->pg_init_in_progress) {
405 m->pg_init_required = 0;
406 m->pg_init_in_progress = 1;
407 init_required = 1;
410 out:
411 spin_unlock_irqrestore(&m->lock, flags);
413 if (init_required)
414 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
416 if (!must_queue)
417 dispatch_queued_ios(m);
421 * An event is triggered whenever a path is taken out of use.
422 * Includes path failure and PG bypass.
424 static void trigger_event(void *data)
426 struct multipath *m = (struct multipath *) data;
428 dm_table_event(m->ti->table);
431 /*-----------------------------------------------------------------
432 * Constructor/argument parsing:
433 * <#multipath feature args> [<arg>]*
434 * <#hw_handler args> [hw_handler [<arg>]*]
435 * <#priority groups>
436 * <initial priority group>
437 * [<selector> <#selector args> [<arg>]*
438 * <#paths> <#per-path selector args>
439 * [<path> [<arg>]* ]+ ]+
440 *---------------------------------------------------------------*/
441 struct param {
442 unsigned min;
443 unsigned max;
444 char *error;
447 static int read_param(struct param *param, char *str, unsigned *v, char **error)
449 if (!str ||
450 (sscanf(str, "%u", v) != 1) ||
451 (*v < param->min) ||
452 (*v > param->max)) {
453 *error = param->error;
454 return -EINVAL;
457 return 0;
460 struct arg_set {
461 unsigned argc;
462 char **argv;
465 static char *shift(struct arg_set *as)
467 char *r;
469 if (as->argc) {
470 as->argc--;
471 r = *as->argv;
472 as->argv++;
473 return r;
476 return NULL;
479 static void consume(struct arg_set *as, unsigned n)
481 BUG_ON (as->argc < n);
482 as->argc -= n;
483 as->argv += n;
486 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
487 struct dm_target *ti)
489 int r;
490 struct path_selector_type *pst;
491 unsigned ps_argc;
493 static struct param _params[] = {
494 {0, 1024, "invalid number of path selector args"},
497 pst = dm_get_path_selector(shift(as));
498 if (!pst) {
499 ti->error = "unknown path selector type";
500 return -EINVAL;
503 r = read_param(_params, shift(as), &ps_argc, &ti->error);
504 if (r)
505 return -EINVAL;
507 r = pst->create(&pg->ps, ps_argc, as->argv);
508 if (r) {
509 dm_put_path_selector(pst);
510 ti->error = "path selector constructor failed";
511 return r;
514 pg->ps.type = pst;
515 consume(as, ps_argc);
517 return 0;
520 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
521 struct dm_target *ti)
523 int r;
524 struct pgpath *p;
526 /* we need at least a path arg */
527 if (as->argc < 1) {
528 ti->error = "no device given";
529 return NULL;
532 p = alloc_pgpath();
533 if (!p)
534 return NULL;
536 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
537 dm_table_get_mode(ti->table), &p->path.dev);
538 if (r) {
539 ti->error = "error getting device";
540 goto bad;
543 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
544 if (r) {
545 dm_put_device(ti, p->path.dev);
546 goto bad;
549 return p;
551 bad:
552 free_pgpath(p);
553 return NULL;
556 static struct priority_group *parse_priority_group(struct arg_set *as,
557 struct multipath *m)
559 static struct param _params[] = {
560 {1, 1024, "invalid number of paths"},
561 {0, 1024, "invalid number of selector args"}
564 int r;
565 unsigned i, nr_selector_args, nr_params;
566 struct priority_group *pg;
567 struct dm_target *ti = m->ti;
569 if (as->argc < 2) {
570 as->argc = 0;
571 ti->error = "not enough priority group aruments";
572 return NULL;
575 pg = alloc_priority_group();
576 if (!pg) {
577 ti->error = "couldn't allocate priority group";
578 return NULL;
580 pg->m = m;
582 r = parse_path_selector(as, pg, ti);
583 if (r)
584 goto bad;
587 * read the paths
589 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
590 if (r)
591 goto bad;
593 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
594 if (r)
595 goto bad;
597 nr_params = 1 + nr_selector_args;
598 for (i = 0; i < pg->nr_pgpaths; i++) {
599 struct pgpath *pgpath;
600 struct arg_set path_args;
602 if (as->argc < nr_params)
603 goto bad;
605 path_args.argc = nr_params;
606 path_args.argv = as->argv;
608 pgpath = parse_path(&path_args, &pg->ps, ti);
609 if (!pgpath)
610 goto bad;
612 pgpath->pg = pg;
613 list_add_tail(&pgpath->list, &pg->pgpaths);
614 consume(as, nr_params);
617 return pg;
619 bad:
620 free_priority_group(pg, ti);
621 return NULL;
624 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
626 int r;
627 struct hw_handler_type *hwht;
628 unsigned hw_argc;
629 struct dm_target *ti = m->ti;
631 static struct param _params[] = {
632 {0, 1024, "invalid number of hardware handler args"},
635 r = read_param(_params, shift(as), &hw_argc, &ti->error);
636 if (r)
637 return -EINVAL;
639 if (!hw_argc)
640 return 0;
642 hwht = dm_get_hw_handler(shift(as));
643 if (!hwht) {
644 ti->error = "unknown hardware handler type";
645 return -EINVAL;
648 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
649 if (r) {
650 dm_put_hw_handler(hwht);
651 ti->error = "hardware handler constructor failed";
652 return r;
655 m->hw_handler.type = hwht;
656 consume(as, hw_argc - 1);
658 return 0;
661 static int parse_features(struct arg_set *as, struct multipath *m)
663 int r;
664 unsigned argc;
665 struct dm_target *ti = m->ti;
667 static struct param _params[] = {
668 {0, 1, "invalid number of feature args"},
671 r = read_param(_params, shift(as), &argc, &ti->error);
672 if (r)
673 return -EINVAL;
675 if (!argc)
676 return 0;
678 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
679 return queue_if_no_path(m, 1, 0);
680 else {
681 ti->error = "Unrecognised multipath feature request";
682 return -EINVAL;
686 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
687 char **argv)
689 /* target parameters */
690 static struct param _params[] = {
691 {1, 1024, "invalid number of priority groups"},
692 {1, 1024, "invalid initial priority group number"},
695 int r;
696 struct multipath *m;
697 struct arg_set as;
698 unsigned pg_count = 0;
699 unsigned next_pg_num;
701 as.argc = argc;
702 as.argv = argv;
704 m = alloc_multipath(ti);
705 if (!m) {
706 ti->error = "can't allocate multipath";
707 return -EINVAL;
710 r = parse_features(&as, m);
711 if (r)
712 goto bad;
714 r = parse_hw_handler(&as, m);
715 if (r)
716 goto bad;
718 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
719 if (r)
720 goto bad;
722 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
723 if (r)
724 goto bad;
726 /* parse the priority groups */
727 while (as.argc) {
728 struct priority_group *pg;
730 pg = parse_priority_group(&as, m);
731 if (!pg) {
732 r = -EINVAL;
733 goto bad;
736 m->nr_valid_paths += pg->nr_pgpaths;
737 list_add_tail(&pg->list, &m->priority_groups);
738 pg_count++;
739 pg->pg_num = pg_count;
740 if (!--next_pg_num)
741 m->next_pg = pg;
744 if (pg_count != m->nr_priority_groups) {
745 ti->error = "priority group count mismatch";
746 r = -EINVAL;
747 goto bad;
750 return 0;
752 bad:
753 free_multipath(m);
754 return r;
757 static void multipath_dtr(struct dm_target *ti)
759 struct multipath *m = (struct multipath *) ti->private;
761 flush_workqueue(kmultipathd);
762 free_multipath(m);
766 * Map bios, recording original fields for later in case we have to resubmit
768 static int multipath_map(struct dm_target *ti, struct bio *bio,
769 union map_info *map_context)
771 int r;
772 struct mpath_io *mpio;
773 struct multipath *m = (struct multipath *) ti->private;
775 if (bio_barrier(bio))
776 return -EOPNOTSUPP;
778 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
779 dm_bio_record(&mpio->details, bio);
781 map_context->ptr = mpio;
782 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
783 r = map_io(m, bio, mpio, 0);
784 if (r < 0)
785 mempool_free(mpio, m->mpio_pool);
787 return r;
791 * Take a path out of use.
793 static int fail_path(struct pgpath *pgpath)
795 unsigned long flags;
796 struct multipath *m = pgpath->pg->m;
798 spin_lock_irqsave(&m->lock, flags);
800 if (!pgpath->path.is_active)
801 goto out;
803 DMWARN("Failing path %s.", pgpath->path.dev->name);
805 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
806 pgpath->path.is_active = 0;
807 pgpath->fail_count++;
809 m->nr_valid_paths--;
811 if (pgpath == m->current_pgpath)
812 m->current_pgpath = NULL;
814 queue_work(kmultipathd, &m->trigger_event);
816 out:
817 spin_unlock_irqrestore(&m->lock, flags);
819 return 0;
823 * Reinstate a previously-failed path
825 static int reinstate_path(struct pgpath *pgpath)
827 int r = 0;
828 unsigned long flags;
829 struct multipath *m = pgpath->pg->m;
831 spin_lock_irqsave(&m->lock, flags);
833 if (pgpath->path.is_active)
834 goto out;
836 if (!pgpath->pg->ps.type) {
837 DMWARN("Reinstate path not supported by path selector %s",
838 pgpath->pg->ps.type->name);
839 r = -EINVAL;
840 goto out;
843 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
844 if (r)
845 goto out;
847 pgpath->path.is_active = 1;
849 m->current_pgpath = NULL;
850 if (!m->nr_valid_paths++ && m->queue_size)
851 queue_work(kmultipathd, &m->process_queued_ios);
853 queue_work(kmultipathd, &m->trigger_event);
855 out:
856 spin_unlock_irqrestore(&m->lock, flags);
858 return r;
862 * Fail or reinstate all paths that match the provided struct dm_dev.
864 static int action_dev(struct multipath *m, struct dm_dev *dev,
865 action_fn action)
867 int r = 0;
868 struct pgpath *pgpath;
869 struct priority_group *pg;
871 list_for_each_entry(pg, &m->priority_groups, list) {
872 list_for_each_entry(pgpath, &pg->pgpaths, list) {
873 if (pgpath->path.dev == dev)
874 r = action(pgpath);
878 return r;
882 * Temporarily try to avoid having to use the specified PG
884 static void bypass_pg(struct multipath *m, struct priority_group *pg,
885 int bypassed)
887 unsigned long flags;
889 spin_lock_irqsave(&m->lock, flags);
891 pg->bypassed = bypassed;
892 m->current_pgpath = NULL;
893 m->current_pg = NULL;
895 spin_unlock_irqrestore(&m->lock, flags);
897 queue_work(kmultipathd, &m->trigger_event);
901 * Switch to using the specified PG from the next I/O that gets mapped
903 static int switch_pg_num(struct multipath *m, const char *pgstr)
905 struct priority_group *pg;
906 unsigned pgnum;
907 unsigned long flags;
909 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
910 (pgnum > m->nr_priority_groups)) {
911 DMWARN("invalid PG number supplied to switch_pg_num");
912 return -EINVAL;
915 spin_lock_irqsave(&m->lock, flags);
916 list_for_each_entry(pg, &m->priority_groups, list) {
917 pg->bypassed = 0;
918 if (--pgnum)
919 continue;
921 m->current_pgpath = NULL;
922 m->current_pg = NULL;
923 m->next_pg = pg;
925 spin_unlock_irqrestore(&m->lock, flags);
927 queue_work(kmultipathd, &m->trigger_event);
928 return 0;
932 * Set/clear bypassed status of a PG.
933 * PGs are numbered upwards from 1 in the order they were declared.
935 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
937 struct priority_group *pg;
938 unsigned pgnum;
940 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
941 (pgnum > m->nr_priority_groups)) {
942 DMWARN("invalid PG number supplied to bypass_pg");
943 return -EINVAL;
946 list_for_each_entry(pg, &m->priority_groups, list) {
947 if (!--pgnum)
948 break;
951 bypass_pg(m, pg, bypassed);
952 return 0;
956 * pg_init must call this when it has completed its initialisation
958 void dm_pg_init_complete(struct path *path, unsigned err_flags)
960 struct pgpath *pgpath = path_to_pgpath(path);
961 struct priority_group *pg = pgpath->pg;
962 struct multipath *m = pg->m;
963 unsigned long flags;
965 /* We insist on failing the path if the PG is already bypassed. */
966 if (err_flags && pg->bypassed)
967 err_flags |= MP_FAIL_PATH;
969 if (err_flags & MP_FAIL_PATH)
970 fail_path(pgpath);
972 if (err_flags & MP_BYPASS_PG)
973 bypass_pg(m, pg, 1);
975 spin_lock_irqsave(&m->lock, flags);
976 if (err_flags) {
977 m->current_pgpath = NULL;
978 m->current_pg = NULL;
979 } else if (!m->pg_init_required)
980 m->queue_io = 0;
982 m->pg_init_in_progress = 0;
983 queue_work(kmultipathd, &m->process_queued_ios);
984 spin_unlock_irqrestore(&m->lock, flags);
988 * end_io handling
990 static int do_end_io(struct multipath *m, struct bio *bio,
991 int error, struct mpath_io *mpio)
993 struct hw_handler *hwh = &m->hw_handler;
994 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
995 unsigned long flags;
997 if (!error)
998 return 0; /* I/O complete */
1000 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1001 return error;
1003 if (error == -EOPNOTSUPP)
1004 return error;
1006 spin_lock_irqsave(&m->lock, flags);
1007 if (!m->nr_valid_paths) {
1008 if (!m->queue_if_no_path) {
1009 spin_unlock_irqrestore(&m->lock, flags);
1010 return -EIO;
1011 } else {
1012 spin_unlock_irqrestore(&m->lock, flags);
1013 goto requeue;
1016 spin_unlock_irqrestore(&m->lock, flags);
1018 if (hwh->type && hwh->type->error)
1019 err_flags = hwh->type->error(hwh, bio);
1021 if (mpio->pgpath) {
1022 if (err_flags & MP_FAIL_PATH)
1023 fail_path(mpio->pgpath);
1025 if (err_flags & MP_BYPASS_PG)
1026 bypass_pg(m, mpio->pgpath->pg, 1);
1029 if (err_flags & MP_ERROR_IO)
1030 return -EIO;
1032 requeue:
1033 dm_bio_restore(&mpio->details, bio);
1035 /* queue for the daemon to resubmit or fail */
1036 spin_lock_irqsave(&m->lock, flags);
1037 bio_list_add(&m->queued_ios, bio);
1038 m->queue_size++;
1039 if (!m->queue_io)
1040 queue_work(kmultipathd, &m->process_queued_ios);
1041 spin_unlock_irqrestore(&m->lock, flags);
1043 return 1; /* io not complete */
1046 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1047 int error, union map_info *map_context)
1049 struct multipath *m = (struct multipath *) ti->private;
1050 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1051 struct pgpath *pgpath = mpio->pgpath;
1052 struct path_selector *ps;
1053 int r;
1055 r = do_end_io(m, bio, error, mpio);
1056 if (pgpath) {
1057 ps = &pgpath->pg->ps;
1058 if (ps->type->end_io)
1059 ps->type->end_io(ps, &pgpath->path);
1061 if (r <= 0)
1062 mempool_free(mpio, m->mpio_pool);
1064 return r;
1068 * Suspend can't complete until all the I/O is processed so if
1069 * the last path fails we must error any remaining I/O.
1070 * Note that if the freeze_bdev fails while suspending, the
1071 * queue_if_no_path state is lost - userspace should reset it.
1073 static void multipath_presuspend(struct dm_target *ti)
1075 struct multipath *m = (struct multipath *) ti->private;
1077 queue_if_no_path(m, 0, 1);
1081 * Restore the queue_if_no_path setting.
1083 static void multipath_resume(struct dm_target *ti)
1085 struct multipath *m = (struct multipath *) ti->private;
1086 unsigned long flags;
1088 spin_lock_irqsave(&m->lock, flags);
1089 m->queue_if_no_path = m->saved_queue_if_no_path;
1090 spin_unlock_irqrestore(&m->lock, flags);
1094 * Info output has the following format:
1095 * num_multipath_feature_args [multipath_feature_args]*
1096 * num_handler_status_args [handler_status_args]*
1097 * num_groups init_group_number
1098 * [A|D|E num_ps_status_args [ps_status_args]*
1099 * num_paths num_selector_args
1100 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1102 * Table output has the following format (identical to the constructor string):
1103 * num_feature_args [features_args]*
1104 * num_handler_args hw_handler [hw_handler_args]*
1105 * num_groups init_group_number
1106 * [priority selector-name num_ps_args [ps_args]*
1107 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1109 static int multipath_status(struct dm_target *ti, status_type_t type,
1110 char *result, unsigned int maxlen)
1112 int sz = 0;
1113 unsigned long flags;
1114 struct multipath *m = (struct multipath *) ti->private;
1115 struct hw_handler *hwh = &m->hw_handler;
1116 struct priority_group *pg;
1117 struct pgpath *p;
1118 unsigned pg_num;
1119 char state;
1121 spin_lock_irqsave(&m->lock, flags);
1123 /* Features */
1124 if (type == STATUSTYPE_INFO)
1125 DMEMIT("1 %u ", m->queue_size);
1126 else if (m->queue_if_no_path)
1127 DMEMIT("1 queue_if_no_path ");
1128 else
1129 DMEMIT("0 ");
1131 if (hwh->type && hwh->type->status)
1132 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1133 else if (!hwh->type || type == STATUSTYPE_INFO)
1134 DMEMIT("0 ");
1135 else
1136 DMEMIT("1 %s ", hwh->type->name);
1138 DMEMIT("%u ", m->nr_priority_groups);
1140 if (m->next_pg)
1141 pg_num = m->next_pg->pg_num;
1142 else if (m->current_pg)
1143 pg_num = m->current_pg->pg_num;
1144 else
1145 pg_num = 1;
1147 DMEMIT("%u ", pg_num);
1149 switch (type) {
1150 case STATUSTYPE_INFO:
1151 list_for_each_entry(pg, &m->priority_groups, list) {
1152 if (pg->bypassed)
1153 state = 'D'; /* Disabled */
1154 else if (pg == m->current_pg)
1155 state = 'A'; /* Currently Active */
1156 else
1157 state = 'E'; /* Enabled */
1159 DMEMIT("%c ", state);
1161 if (pg->ps.type->status)
1162 sz += pg->ps.type->status(&pg->ps, NULL, type,
1163 result + sz,
1164 maxlen - sz);
1165 else
1166 DMEMIT("0 ");
1168 DMEMIT("%u %u ", pg->nr_pgpaths,
1169 pg->ps.type->info_args);
1171 list_for_each_entry(p, &pg->pgpaths, list) {
1172 DMEMIT("%s %s %u ", p->path.dev->name,
1173 p->path.is_active ? "A" : "F",
1174 p->fail_count);
1175 if (pg->ps.type->status)
1176 sz += pg->ps.type->status(&pg->ps,
1177 &p->path, type, result + sz,
1178 maxlen - sz);
1181 break;
1183 case STATUSTYPE_TABLE:
1184 list_for_each_entry(pg, &m->priority_groups, list) {
1185 DMEMIT("%s ", pg->ps.type->name);
1187 if (pg->ps.type->status)
1188 sz += pg->ps.type->status(&pg->ps, NULL, type,
1189 result + sz,
1190 maxlen - sz);
1191 else
1192 DMEMIT("0 ");
1194 DMEMIT("%u %u ", pg->nr_pgpaths,
1195 pg->ps.type->table_args);
1197 list_for_each_entry(p, &pg->pgpaths, list) {
1198 DMEMIT("%s ", p->path.dev->name);
1199 if (pg->ps.type->status)
1200 sz += pg->ps.type->status(&pg->ps,
1201 &p->path, type, result + sz,
1202 maxlen - sz);
1205 break;
1208 spin_unlock_irqrestore(&m->lock, flags);
1210 return 0;
1213 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1215 int r;
1216 struct dm_dev *dev;
1217 struct multipath *m = (struct multipath *) ti->private;
1218 action_fn action;
1220 if (argc == 1) {
1221 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1222 return queue_if_no_path(m, 1, 0);
1223 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1224 return queue_if_no_path(m, 0, 0);
1227 if (argc != 2)
1228 goto error;
1230 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1231 return bypass_pg_num(m, argv[1], 1);
1232 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1233 return bypass_pg_num(m, argv[1], 0);
1234 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1235 return switch_pg_num(m, argv[1]);
1236 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1237 action = reinstate_path;
1238 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1239 action = fail_path;
1240 else
1241 goto error;
1243 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1244 dm_table_get_mode(ti->table), &dev);
1245 if (r) {
1246 DMWARN("message: error getting device %s",
1247 argv[1]);
1248 return -EINVAL;
1251 r = action_dev(m, dev, action);
1253 dm_put_device(ti, dev);
1255 return r;
1257 error:
1258 DMWARN("Unrecognised multipath message received.");
1259 return -EINVAL;
1262 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1263 struct file *filp, unsigned int cmd,
1264 unsigned long arg)
1266 struct multipath *m = (struct multipath *) ti->private;
1267 struct block_device *bdev = NULL;
1268 unsigned long flags;
1269 struct file fake_file = {};
1270 struct dentry fake_dentry = {};
1271 int r = 0;
1273 fake_file.f_dentry = &fake_dentry;
1275 spin_lock_irqsave(&m->lock, flags);
1277 if (!m->current_pgpath)
1278 __choose_pgpath(m);
1280 if (m->current_pgpath) {
1281 bdev = m->current_pgpath->path.dev->bdev;
1282 fake_dentry.d_inode = bdev->bd_inode;
1283 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1286 if (m->queue_io)
1287 r = -EAGAIN;
1288 else if (!bdev)
1289 r = -EIO;
1291 spin_unlock_irqrestore(&m->lock, flags);
1293 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1294 bdev->bd_disk, cmd, arg);
1297 /*-----------------------------------------------------------------
1298 * Module setup
1299 *---------------------------------------------------------------*/
1300 static struct target_type multipath_target = {
1301 .name = "multipath",
1302 .version = {1, 0, 5},
1303 .module = THIS_MODULE,
1304 .ctr = multipath_ctr,
1305 .dtr = multipath_dtr,
1306 .map = multipath_map,
1307 .end_io = multipath_end_io,
1308 .presuspend = multipath_presuspend,
1309 .resume = multipath_resume,
1310 .status = multipath_status,
1311 .message = multipath_message,
1312 .ioctl = multipath_ioctl,
1315 static int __init dm_multipath_init(void)
1317 int r;
1319 /* allocate a slab for the dm_ios */
1320 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1321 0, 0, NULL, NULL);
1322 if (!_mpio_cache)
1323 return -ENOMEM;
1325 r = dm_register_target(&multipath_target);
1326 if (r < 0) {
1327 DMERR("%s: register failed %d", multipath_target.name, r);
1328 kmem_cache_destroy(_mpio_cache);
1329 return -EINVAL;
1332 kmultipathd = create_workqueue("kmpathd");
1333 if (!kmultipathd) {
1334 DMERR("%s: failed to create workqueue kmpathd",
1335 multipath_target.name);
1336 dm_unregister_target(&multipath_target);
1337 kmem_cache_destroy(_mpio_cache);
1338 return -ENOMEM;
1341 DMINFO("version %u.%u.%u loaded",
1342 multipath_target.version[0], multipath_target.version[1],
1343 multipath_target.version[2]);
1345 return r;
1348 static void __exit dm_multipath_exit(void)
1350 int r;
1352 destroy_workqueue(kmultipathd);
1354 r = dm_unregister_target(&multipath_target);
1355 if (r < 0)
1356 DMERR("%s: target unregister failed %d",
1357 multipath_target.name, r);
1358 kmem_cache_destroy(_mpio_cache);
1361 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1363 module_init(dm_multipath_init);
1364 module_exit(dm_multipath_exit);
1366 MODULE_DESCRIPTION(DM_NAME " multipath target");
1367 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1368 MODULE_LICENSE("GPL");