blob: 84cdb700a247040d4e72a7f79c03df12a7598ced [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
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"
13
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>
23
24#define MESG_STR(x) x, sizeof(x)
25
26/* Path properties */
27struct pgpath {
28 struct list_head list;
29
30 struct priority_group *pg; /* Owning PG */
31 unsigned fail_count; /* Cumulative failure count */
32
33 struct path path;
34};
35
36#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
37
38/*
39 * Paths are grouped into Priority Groups and numbered from 1 upwards.
40 * Each has a path selector which controls which path gets used.
41 */
42struct priority_group {
43 struct list_head list;
44
45 struct multipath *m; /* Owning multipath instance */
46 struct path_selector ps;
47
48 unsigned pg_num; /* Reference number */
49 unsigned bypassed; /* Temporarily bypass this PG? */
50
51 unsigned nr_pgpaths; /* Number of paths in PG */
52 struct list_head pgpaths;
53};
54
55/* Multipath context */
56struct multipath {
57 struct list_head list;
58 struct dm_target *ti;
59
60 spinlock_t lock;
61
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? */
66
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 */
72
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? */
76
77 struct work_struct process_queued_ios;
78 struct bio_list queued_ios;
79 unsigned queue_size;
80
81 struct work_struct trigger_event;
82
83 /*
84 * We must use a mempool of mpath_io structs so that we
85 * can resubmit bios on error.
86 */
87 mempool_t *mpio_pool;
88};
89
90/*
91 * Context information attached to each bio we process.
92 */
93struct mpath_io {
94 struct pgpath *pgpath;
95 struct dm_bio_details details;
96};
97
98typedef int (*action_fn) (struct pgpath *pgpath);
99
100#define MIN_IOS 256 /* Mempool size */
101
102static kmem_cache_t *_mpio_cache;
103
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700104struct workqueue_struct *kmultipathd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void process_queued_ios(void *data);
106static void trigger_event(void *data);
107
108
109/*-----------------------------------------------
110 * Allocation routines
111 *-----------------------------------------------*/
112
113static struct pgpath *alloc_pgpath(void)
114{
115 struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
116
117 if (pgpath) {
118 memset(pgpath, 0, sizeof(*pgpath));
119 pgpath->path.is_active = 1;
120 }
121
122 return pgpath;
123}
124
125static inline void free_pgpath(struct pgpath *pgpath)
126{
127 kfree(pgpath);
128}
129
130static struct priority_group *alloc_priority_group(void)
131{
132 struct priority_group *pg;
133
134 pg = kmalloc(sizeof(*pg), GFP_KERNEL);
135 if (!pg)
136 return NULL;
137
138 memset(pg, 0, sizeof(*pg));
139 INIT_LIST_HEAD(&pg->pgpaths);
140
141 return pg;
142}
143
144static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
145{
146 struct pgpath *pgpath, *tmp;
147
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);
152 }
153}
154
155static void free_priority_group(struct priority_group *pg,
156 struct dm_target *ti)
157{
158 struct path_selector *ps = &pg->ps;
159
160 if (ps->type) {
161 ps->type->destroy(ps);
162 dm_put_path_selector(ps->type);
163 }
164
165 free_pgpaths(&pg->pgpaths, ti);
166 kfree(pg);
167}
168
169static struct multipath *alloc_multipath(void)
170{
171 struct multipath *m;
172
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;
186 }
187 }
188
189 return m;
190}
191
192static void free_multipath(struct multipath *m)
193{
194 struct priority_group *pg, *tmp;
195 struct hw_handler *hwh = &m->hw_handler;
196
197 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
198 list_del(&pg->list);
199 free_priority_group(pg, m->ti);
200 }
201
202 if (hwh->type) {
203 hwh->type->destroy(hwh);
204 dm_put_hw_handler(hwh->type);
205 }
206
207 mempool_destroy(m->mpio_pool);
208 kfree(m);
209}
210
211
212/*-----------------------------------------------
213 * Path selection
214 *-----------------------------------------------*/
215
216static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
217{
218 struct hw_handler *hwh = &m->hw_handler;
219
220 m->current_pg = pgpath->pg;
221
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;
229 }
230}
231
232static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
233{
234 struct path *path;
235
236 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
237 if (!path)
238 return -ENXIO;
239
240 m->current_pgpath = path_to_pgpath(path);
241
242 if (m->current_pg != pg)
243 __switch_pg(m, m->current_pgpath);
244
245 return 0;
246}
247
248static void __choose_pgpath(struct multipath *m)
249{
250 struct priority_group *pg;
251 unsigned bypassed = 1;
252
253 if (!m->nr_valid_paths)
254 goto failed;
255
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;
262 }
263
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;
267
268 /*
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.
272 */
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;
279 }
280 } while (bypassed--);
281
282failed:
283 m->current_pgpath = NULL;
284 m->current_pg = NULL;
285}
286
287static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
288 unsigned was_queued)
289{
290 int r = 1;
291 unsigned long flags;
292 struct pgpath *pgpath;
293
294 spin_lock_irqsave(&m->lock, flags);
295
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);
300
301 pgpath = m->current_pgpath;
302
303 if (was_queued)
304 m->queue_size--;
305
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)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700312 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 pgpath = NULL;
314 r = 0;
315 } else if (!pgpath)
316 r = -EIO; /* Failed */
317 else
318 bio->bi_bdev = pgpath->path.dev->bdev;
319
320 mpio->pgpath = pgpath;
321
322 spin_unlock_irqrestore(&m->lock, flags);
323
324 return r;
325}
326
327/*
328 * If we run out of usable paths, should we queue I/O or error it?
329 */
330static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path)
331{
332 unsigned long flags;
333
334 spin_lock_irqsave(&m->lock, flags);
335
336 m->queue_if_no_path = queue_if_no_path;
337 if (!m->queue_if_no_path)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700338 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 spin_unlock_irqrestore(&m->lock, flags);
341
342 return 0;
343}
344
345/*-----------------------------------------------------------------
346 * The multipath daemon is responsible for resubmitting queued ios.
347 *---------------------------------------------------------------*/
348
349static void dispatch_queued_ios(struct multipath *m)
350{
351 int r;
352 unsigned long flags;
353 struct bio *bio = NULL, *next;
354 struct mpath_io *mpio;
355 union map_info *info;
356
357 spin_lock_irqsave(&m->lock, flags);
358 bio = bio_list_get(&m->queued_ios);
359 spin_unlock_irqrestore(&m->lock, flags);
360
361 while (bio) {
362 next = bio->bi_next;
363 bio->bi_next = NULL;
364
365 info = dm_get_mapinfo(bio);
366 mpio = info->ptr;
367
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);
373
374 bio = next;
375 }
376}
377
378static void process_queued_ios(void *data)
379{
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;
385
386 spin_lock_irqsave(&m->lock, flags);
387
388 if (!m->current_pgpath)
389 __choose_pgpath(m);
390
391 pgpath = m->current_pgpath;
392
393 if ((pgpath && m->queue_io) ||
394 (!pgpath && m->queue_if_no_path && !m->suspended))
395 must_queue = 1;
396
397 init_required = m->pg_init_required;
398 if (init_required)
399 m->pg_init_required = 0;
400
401 spin_unlock_irqrestore(&m->lock, flags);
402
403 if (init_required)
404 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
405
406 if (!must_queue)
407 dispatch_queued_ios(m);
408}
409
410/*
411 * An event is triggered whenever a path is taken out of use.
412 * Includes path failure and PG bypass.
413 */
414static void trigger_event(void *data)
415{
416 struct multipath *m = (struct multipath *) data;
417
418 dm_table_event(m->ti->table);
419}
420
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 *---------------------------------------------------------------*/
431struct param {
432 unsigned min;
433 unsigned max;
434 char *error;
435};
436
437#define ESTR(s) ("dm-multipath: " s)
438
439static int read_param(struct param *param, char *str, unsigned *v, char **error)
440{
441 if (!str ||
442 (sscanf(str, "%u", v) != 1) ||
443 (*v < param->min) ||
444 (*v > param->max)) {
445 *error = param->error;
446 return -EINVAL;
447 }
448
449 return 0;
450}
451
452struct arg_set {
453 unsigned argc;
454 char **argv;
455};
456
457static char *shift(struct arg_set *as)
458{
459 char *r;
460
461 if (as->argc) {
462 as->argc--;
463 r = *as->argv;
464 as->argv++;
465 return r;
466 }
467
468 return NULL;
469}
470
471static void consume(struct arg_set *as, unsigned n)
472{
473 BUG_ON (as->argc < n);
474 as->argc -= n;
475 as->argv += n;
476}
477
478static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
479 struct dm_target *ti)
480{
481 int r;
482 struct path_selector_type *pst;
483 unsigned ps_argc;
484
485 static struct param _params[] = {
486 {0, 1024, ESTR("invalid number of path selector args")},
487 };
488
489 pst = dm_get_path_selector(shift(as));
490 if (!pst) {
491 ti->error = ESTR("unknown path selector type");
492 return -EINVAL;
493 }
494
495 r = read_param(_params, shift(as), &ps_argc, &ti->error);
496 if (r)
497 return -EINVAL;
498
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;
504 }
505
506 pg->ps.type = pst;
507 consume(as, ps_argc);
508
509 return 0;
510}
511
512static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
513 struct dm_target *ti)
514{
515 int r;
516 struct pgpath *p;
517
518 /* we need at least a path arg */
519 if (as->argc < 1) {
520 ti->error = ESTR("no device given");
521 return NULL;
522 }
523
524 p = alloc_pgpath();
525 if (!p)
526 return NULL;
527
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;
533 }
534
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;
539 }
540
541 return p;
542
543 bad:
544 free_pgpath(p);
545 return NULL;
546}
547
548static struct priority_group *parse_priority_group(struct arg_set *as,
549 struct multipath *m,
550 struct dm_target *ti)
551{
552 static struct param _params[] = {
553 {1, 1024, ESTR("invalid number of paths")},
554 {0, 1024, ESTR("invalid number of selector args")}
555 };
556
557 int r;
558 unsigned i, nr_selector_args, nr_params;
559 struct priority_group *pg;
560
561 if (as->argc < 2) {
562 as->argc = 0;
563 ti->error = ESTR("not enough priority group aruments");
564 return NULL;
565 }
566
567 pg = alloc_priority_group();
568 if (!pg) {
569 ti->error = ESTR("couldn't allocate priority group");
570 return NULL;
571 }
572 pg->m = m;
573
574 r = parse_path_selector(as, pg, ti);
575 if (r)
576 goto bad;
577
578 /*
579 * read the paths
580 */
581 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
582 if (r)
583 goto bad;
584
585 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
586 if (r)
587 goto bad;
588
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;
593
594 if (as->argc < nr_params)
595 goto bad;
596
597 path_args.argc = nr_params;
598 path_args.argv = as->argv;
599
600 pgpath = parse_path(&path_args, &pg->ps, ti);
601 if (!pgpath)
602 goto bad;
603
604 pgpath->pg = pg;
605 list_add_tail(&pgpath->list, &pg->pgpaths);
606 consume(as, nr_params);
607 }
608
609 return pg;
610
611 bad:
612 free_priority_group(pg, ti);
613 return NULL;
614}
615
616static int parse_hw_handler(struct arg_set *as, struct multipath *m,
617 struct dm_target *ti)
618{
619 int r;
620 struct hw_handler_type *hwht;
621 unsigned hw_argc;
622
623 static struct param _params[] = {
624 {0, 1024, ESTR("invalid number of hardware handler args")},
625 };
626
627 r = read_param(_params, shift(as), &hw_argc, &ti->error);
628 if (r)
629 return -EINVAL;
630
631 if (!hw_argc)
632 return 0;
633
634 hwht = dm_get_hw_handler(shift(as));
635 if (!hwht) {
636 ti->error = ESTR("unknown hardware handler type");
637 return -EINVAL;
638 }
639
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;
645 }
646
647 m->hw_handler.type = hwht;
648 consume(as, hw_argc - 1);
649
650 return 0;
651}
652
653static int parse_features(struct arg_set *as, struct multipath *m,
654 struct dm_target *ti)
655{
656 int r;
657 unsigned argc;
658
659 static struct param _params[] = {
660 {0, 1, ESTR("invalid number of feature args")},
661 };
662
663 r = read_param(_params, shift(as), &argc, &ti->error);
664 if (r)
665 return -EINVAL;
666
667 if (!argc)
668 return 0;
669
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;
675 }
676}
677
678static int multipath_ctr(struct dm_target *ti, unsigned int argc,
679 char **argv)
680{
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")},
685 };
686
687 int r;
688 struct multipath *m;
689 struct arg_set as;
690 unsigned pg_count = 0;
691 unsigned next_pg_num;
692
693 as.argc = argc;
694 as.argv = argv;
695
696 m = alloc_multipath();
697 if (!m) {
698 ti->error = ESTR("can't allocate multipath");
699 return -EINVAL;
700 }
701
702 r = parse_features(&as, m, ti);
703 if (r)
704 goto bad;
705
706 r = parse_hw_handler(&as, m, ti);
707 if (r)
708 goto bad;
709
710 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
711 if (r)
712 goto bad;
713
714 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
715 if (r)
716 goto bad;
717
718 /* parse the priority groups */
719 while (as.argc) {
720 struct priority_group *pg;
721
722 pg = parse_priority_group(&as, m, ti);
723 if (!pg) {
724 r = -EINVAL;
725 goto bad;
726 }
727
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;
734 }
735
736 if (pg_count != m->nr_priority_groups) {
737 ti->error = ESTR("priority group count mismatch");
738 r = -EINVAL;
739 goto bad;
740 }
741
742 ti->private = m;
743 m->ti = ti;
744
745 return 0;
746
747 bad:
748 free_multipath(m);
749 return r;
750}
751
752static void multipath_dtr(struct dm_target *ti)
753{
754 struct multipath *m = (struct multipath *) ti->private;
755 free_multipath(m);
756}
757
758/*
759 * Map bios, recording original fields for later in case we have to resubmit
760 */
761static int multipath_map(struct dm_target *ti, struct bio *bio,
762 union map_info *map_context)
763{
764 int r;
765 struct mpath_io *mpio;
766 struct multipath *m = (struct multipath *) ti->private;
767
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700768 if (bio_barrier(bio))
769 return -EOPNOTSUPP;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
772 dm_bio_record(&mpio->details, bio);
773
774 map_context->ptr = mpio;
775 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
776 r = map_io(m, bio, mpio, 0);
777 if (r < 0)
778 mempool_free(mpio, m->mpio_pool);
779
780 return r;
781}
782
783/*
784 * Take a path out of use.
785 */
786static int fail_path(struct pgpath *pgpath)
787{
788 unsigned long flags;
789 struct multipath *m = pgpath->pg->m;
790
791 spin_lock_irqsave(&m->lock, flags);
792
793 if (!pgpath->path.is_active)
794 goto out;
795
796 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
797
798 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
799 pgpath->path.is_active = 0;
800 pgpath->fail_count++;
801
802 m->nr_valid_paths--;
803
804 if (pgpath == m->current_pgpath)
805 m->current_pgpath = NULL;
806
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700807 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809out:
810 spin_unlock_irqrestore(&m->lock, flags);
811
812 return 0;
813}
814
815/*
816 * Reinstate a previously-failed path
817 */
818static int reinstate_path(struct pgpath *pgpath)
819{
820 int r = 0;
821 unsigned long flags;
822 struct multipath *m = pgpath->pg->m;
823
824 spin_lock_irqsave(&m->lock, flags);
825
826 if (pgpath->path.is_active)
827 goto out;
828
829 if (!pgpath->pg->ps.type) {
830 DMWARN("Reinstate path not supported by path selector %s",
831 pgpath->pg->ps.type->name);
832 r = -EINVAL;
833 goto out;
834 }
835
836 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
837 if (r)
838 goto out;
839
840 pgpath->path.is_active = 1;
841
842 m->current_pgpath = NULL;
843 if (!m->nr_valid_paths++)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700844 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700846 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848out:
849 spin_unlock_irqrestore(&m->lock, flags);
850
851 return r;
852}
853
854/*
855 * Fail or reinstate all paths that match the provided struct dm_dev.
856 */
857static int action_dev(struct multipath *m, struct dm_dev *dev,
858 action_fn action)
859{
860 int r = 0;
861 struct pgpath *pgpath;
862 struct priority_group *pg;
863
864 list_for_each_entry(pg, &m->priority_groups, list) {
865 list_for_each_entry(pgpath, &pg->pgpaths, list) {
866 if (pgpath->path.dev == dev)
867 r = action(pgpath);
868 }
869 }
870
871 return r;
872}
873
874/*
875 * Temporarily try to avoid having to use the specified PG
876 */
877static void bypass_pg(struct multipath *m, struct priority_group *pg,
878 int bypassed)
879{
880 unsigned long flags;
881
882 spin_lock_irqsave(&m->lock, flags);
883
884 pg->bypassed = bypassed;
885 m->current_pgpath = NULL;
886 m->current_pg = NULL;
887
888 spin_unlock_irqrestore(&m->lock, flags);
889
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700890 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * Switch to using the specified PG from the next I/O that gets mapped
895 */
896static int switch_pg_num(struct multipath *m, const char *pgstr)
897{
898 struct priority_group *pg;
899 unsigned pgnum;
900 unsigned long flags;
901
902 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
903 (pgnum > m->nr_priority_groups)) {
904 DMWARN("invalid PG number supplied to switch_pg_num");
905 return -EINVAL;
906 }
907
908 spin_lock_irqsave(&m->lock, flags);
909 list_for_each_entry(pg, &m->priority_groups, list) {
910 pg->bypassed = 0;
911 if (--pgnum)
912 continue;
913
914 m->current_pgpath = NULL;
915 m->current_pg = NULL;
916 m->next_pg = pg;
917 }
918 spin_unlock_irqrestore(&m->lock, flags);
919
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700920 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
924/*
925 * Set/clear bypassed status of a PG.
926 * PGs are numbered upwards from 1 in the order they were declared.
927 */
928static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
929{
930 struct priority_group *pg;
931 unsigned pgnum;
932
933 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
934 (pgnum > m->nr_priority_groups)) {
935 DMWARN("invalid PG number supplied to bypass_pg");
936 return -EINVAL;
937 }
938
939 list_for_each_entry(pg, &m->priority_groups, list) {
940 if (!--pgnum)
941 break;
942 }
943
944 bypass_pg(m, pg, bypassed);
945 return 0;
946}
947
948/*
949 * pg_init must call this when it has completed its initialisation
950 */
951void dm_pg_init_complete(struct path *path, unsigned err_flags)
952{
953 struct pgpath *pgpath = path_to_pgpath(path);
954 struct priority_group *pg = pgpath->pg;
955 struct multipath *m = pg->m;
956 unsigned long flags;
957
958 /* We insist on failing the path if the PG is already bypassed. */
959 if (err_flags && pg->bypassed)
960 err_flags |= MP_FAIL_PATH;
961
962 if (err_flags & MP_FAIL_PATH)
963 fail_path(pgpath);
964
965 if (err_flags & MP_BYPASS_PG)
966 bypass_pg(m, pg, 1);
967
968 spin_lock_irqsave(&m->lock, flags);
969 if (!err_flags)
970 m->queue_io = 0;
971 else {
972 m->current_pgpath = NULL;
973 m->current_pg = NULL;
974 }
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700975 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 spin_unlock_irqrestore(&m->lock, flags);
977}
978
979/*
980 * end_io handling
981 */
982static int do_end_io(struct multipath *m, struct bio *bio,
983 int error, struct mpath_io *mpio)
984{
985 struct hw_handler *hwh = &m->hw_handler;
986 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
987
988 if (!error)
989 return 0; /* I/O complete */
990
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -0700991 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
992 return error;
993
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700994 if (error == -EOPNOTSUPP)
995 return error;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_lock(&m->lock);
998 if (!m->nr_valid_paths) {
999 if (!m->queue_if_no_path || m->suspended) {
1000 spin_unlock(&m->lock);
1001 return -EIO;
1002 } else {
1003 spin_unlock(&m->lock);
1004 goto requeue;
1005 }
1006 }
1007 spin_unlock(&m->lock);
1008
1009 if (hwh->type && hwh->type->error)
1010 err_flags = hwh->type->error(hwh, bio);
1011
1012 if (mpio->pgpath) {
1013 if (err_flags & MP_FAIL_PATH)
1014 fail_path(mpio->pgpath);
1015
1016 if (err_flags & MP_BYPASS_PG)
1017 bypass_pg(m, mpio->pgpath->pg, 1);
1018 }
1019
1020 if (err_flags & MP_ERROR_IO)
1021 return -EIO;
1022
1023 requeue:
1024 dm_bio_restore(&mpio->details, bio);
1025
1026 /* queue for the daemon to resubmit or fail */
1027 spin_lock(&m->lock);
1028 bio_list_add(&m->queued_ios, bio);
1029 m->queue_size++;
1030 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001031 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_unlock(&m->lock);
1033
1034 return 1; /* io not complete */
1035}
1036
1037static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1038 int error, union map_info *map_context)
1039{
1040 struct multipath *m = (struct multipath *) ti->private;
1041 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1042 struct pgpath *pgpath = mpio->pgpath;
1043 struct path_selector *ps;
1044 int r;
1045
1046 r = do_end_io(m, bio, error, mpio);
1047 if (pgpath) {
1048 ps = &pgpath->pg->ps;
1049 if (ps->type->end_io)
1050 ps->type->end_io(ps, &pgpath->path);
1051 }
1052 if (r <= 0)
1053 mempool_free(mpio, m->mpio_pool);
1054
1055 return r;
1056}
1057
1058/*
1059 * Suspend can't complete until all the I/O is processed so if
1060 * the last path failed we will now error any queued I/O.
1061 */
1062static void multipath_presuspend(struct dm_target *ti)
1063{
1064 struct multipath *m = (struct multipath *) ti->private;
1065 unsigned long flags;
1066
1067 spin_lock_irqsave(&m->lock, flags);
1068 m->suspended = 1;
1069 if (m->queue_if_no_path)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001070 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 spin_unlock_irqrestore(&m->lock, flags);
1072}
1073
1074static void multipath_resume(struct dm_target *ti)
1075{
1076 struct multipath *m = (struct multipath *) ti->private;
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&m->lock, flags);
1080 m->suspended = 0;
1081 spin_unlock_irqrestore(&m->lock, flags);
1082}
1083
1084/*
1085 * Info output has the following format:
1086 * num_multipath_feature_args [multipath_feature_args]*
1087 * num_handler_status_args [handler_status_args]*
1088 * num_groups init_group_number
1089 * [A|D|E num_ps_status_args [ps_status_args]*
1090 * num_paths num_selector_args
1091 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1092 *
1093 * Table output has the following format (identical to the constructor string):
1094 * num_feature_args [features_args]*
1095 * num_handler_args hw_handler [hw_handler_args]*
1096 * num_groups init_group_number
1097 * [priority selector-name num_ps_args [ps_args]*
1098 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1099 */
1100static int multipath_status(struct dm_target *ti, status_type_t type,
1101 char *result, unsigned int maxlen)
1102{
1103 int sz = 0;
1104 unsigned long flags;
1105 struct multipath *m = (struct multipath *) ti->private;
1106 struct hw_handler *hwh = &m->hw_handler;
1107 struct priority_group *pg;
1108 struct pgpath *p;
1109 unsigned pg_num;
1110 char state;
1111
1112 spin_lock_irqsave(&m->lock, flags);
1113
1114 /* Features */
1115 if (type == STATUSTYPE_INFO)
1116 DMEMIT("1 %u ", m->queue_size);
1117 else if (m->queue_if_no_path)
1118 DMEMIT("1 queue_if_no_path ");
1119 else
1120 DMEMIT("0 ");
1121
1122 if (hwh->type && hwh->type->status)
1123 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1124 else if (!hwh->type || type == STATUSTYPE_INFO)
1125 DMEMIT("0 ");
1126 else
1127 DMEMIT("1 %s ", hwh->type->name);
1128
1129 DMEMIT("%u ", m->nr_priority_groups);
1130
1131 if (m->next_pg)
1132 pg_num = m->next_pg->pg_num;
1133 else if (m->current_pg)
1134 pg_num = m->current_pg->pg_num;
1135 else
1136 pg_num = 1;
1137
1138 DMEMIT("%u ", pg_num);
1139
1140 switch (type) {
1141 case STATUSTYPE_INFO:
1142 list_for_each_entry(pg, &m->priority_groups, list) {
1143 if (pg->bypassed)
1144 state = 'D'; /* Disabled */
1145 else if (pg == m->current_pg)
1146 state = 'A'; /* Currently Active */
1147 else
1148 state = 'E'; /* Enabled */
1149
1150 DMEMIT("%c ", state);
1151
1152 if (pg->ps.type->status)
1153 sz += pg->ps.type->status(&pg->ps, NULL, type,
1154 result + sz,
1155 maxlen - sz);
1156 else
1157 DMEMIT("0 ");
1158
1159 DMEMIT("%u %u ", pg->nr_pgpaths,
1160 pg->ps.type->info_args);
1161
1162 list_for_each_entry(p, &pg->pgpaths, list) {
1163 DMEMIT("%s %s %u ", p->path.dev->name,
1164 p->path.is_active ? "A" : "F",
1165 p->fail_count);
1166 if (pg->ps.type->status)
1167 sz += pg->ps.type->status(&pg->ps,
1168 &p->path, type, result + sz,
1169 maxlen - sz);
1170 }
1171 }
1172 break;
1173
1174 case STATUSTYPE_TABLE:
1175 list_for_each_entry(pg, &m->priority_groups, list) {
1176 DMEMIT("%s ", pg->ps.type->name);
1177
1178 if (pg->ps.type->status)
1179 sz += pg->ps.type->status(&pg->ps, NULL, type,
1180 result + sz,
1181 maxlen - sz);
1182 else
1183 DMEMIT("0 ");
1184
1185 DMEMIT("%u %u ", pg->nr_pgpaths,
1186 pg->ps.type->table_args);
1187
1188 list_for_each_entry(p, &pg->pgpaths, list) {
1189 DMEMIT("%s ", p->path.dev->name);
1190 if (pg->ps.type->status)
1191 sz += pg->ps.type->status(&pg->ps,
1192 &p->path, type, result + sz,
1193 maxlen - sz);
1194 }
1195 }
1196 break;
1197 }
1198
1199 spin_unlock_irqrestore(&m->lock, flags);
1200
1201 return 0;
1202}
1203
1204static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1205{
1206 int r;
1207 struct dm_dev *dev;
1208 struct multipath *m = (struct multipath *) ti->private;
1209 action_fn action;
1210
1211 if (argc == 1) {
1212 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1213 return queue_if_no_path(m, 1);
1214 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1215 return queue_if_no_path(m, 0);
1216 }
1217
1218 if (argc != 2)
1219 goto error;
1220
1221 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1222 return bypass_pg_num(m, argv[1], 1);
1223 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1224 return bypass_pg_num(m, argv[1], 0);
1225 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1226 return switch_pg_num(m, argv[1]);
1227 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1228 action = reinstate_path;
1229 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1230 action = fail_path;
1231 else
1232 goto error;
1233
1234 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1235 dm_table_get_mode(ti->table), &dev);
1236 if (r) {
1237 DMWARN("dm-multipath message: error getting device %s",
1238 argv[1]);
1239 return -EINVAL;
1240 }
1241
1242 r = action_dev(m, dev, action);
1243
1244 dm_put_device(ti, dev);
1245
1246 return r;
1247
1248error:
1249 DMWARN("Unrecognised multipath message received.");
1250 return -EINVAL;
1251}
1252
1253/*-----------------------------------------------------------------
1254 * Module setup
1255 *---------------------------------------------------------------*/
1256static struct target_type multipath_target = {
1257 .name = "multipath",
1258 .version = {1, 0, 4},
1259 .module = THIS_MODULE,
1260 .ctr = multipath_ctr,
1261 .dtr = multipath_dtr,
1262 .map = multipath_map,
1263 .end_io = multipath_end_io,
1264 .presuspend = multipath_presuspend,
1265 .resume = multipath_resume,
1266 .status = multipath_status,
1267 .message = multipath_message,
1268};
1269
1270static int __init dm_multipath_init(void)
1271{
1272 int r;
1273
1274 /* allocate a slab for the dm_ios */
1275 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1276 0, 0, NULL, NULL);
1277 if (!_mpio_cache)
1278 return -ENOMEM;
1279
1280 r = dm_register_target(&multipath_target);
1281 if (r < 0) {
1282 DMERR("%s: register failed %d", multipath_target.name, r);
1283 kmem_cache_destroy(_mpio_cache);
1284 return -EINVAL;
1285 }
1286
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001287 kmultipathd = create_workqueue("kmpathd");
1288 if (!kmultipathd) {
1289 DMERR("%s: failed to create workqueue kmpathd",
1290 multipath_target.name);
1291 dm_unregister_target(&multipath_target);
1292 kmem_cache_destroy(_mpio_cache);
1293 return -ENOMEM;
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 DMINFO("dm-multipath version %u.%u.%u loaded",
1297 multipath_target.version[0], multipath_target.version[1],
1298 multipath_target.version[2]);
1299
1300 return r;
1301}
1302
1303static void __exit dm_multipath_exit(void)
1304{
1305 int r;
1306
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001307 destroy_workqueue(kmultipathd);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 r = dm_unregister_target(&multipath_target);
1310 if (r < 0)
1311 DMERR("%s: target unregister failed %d",
1312 multipath_target.name, r);
1313 kmem_cache_destroy(_mpio_cache);
1314}
1315
1316EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1317
1318module_init(dm_multipath_init);
1319module_exit(dm_multipath_exit);
1320
1321MODULE_DESCRIPTION(DM_NAME " multipath target");
1322MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1323MODULE_LICENSE("GPL");