blob: 98da8eee2d2c308156c124ed18dcd3d3a16090b8 [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? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070075 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
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) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700307 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* 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
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700336 m->saved_queue_if_no_path = m->queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 m->queue_if_no_path = queue_if_no_path;
338 if (!m->queue_if_no_path)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700339 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 spin_unlock_irqrestore(&m->lock, flags);
342
343 return 0;
344}
345
346/*-----------------------------------------------------------------
347 * The multipath daemon is responsible for resubmitting queued ios.
348 *---------------------------------------------------------------*/
349
350static void dispatch_queued_ios(struct multipath *m)
351{
352 int r;
353 unsigned long flags;
354 struct bio *bio = NULL, *next;
355 struct mpath_io *mpio;
356 union map_info *info;
357
358 spin_lock_irqsave(&m->lock, flags);
359 bio = bio_list_get(&m->queued_ios);
360 spin_unlock_irqrestore(&m->lock, flags);
361
362 while (bio) {
363 next = bio->bi_next;
364 bio->bi_next = NULL;
365
366 info = dm_get_mapinfo(bio);
367 mpio = info->ptr;
368
369 r = map_io(m, bio, mpio, 1);
370 if (r < 0)
371 bio_endio(bio, bio->bi_size, r);
372 else if (r == 1)
373 generic_make_request(bio);
374
375 bio = next;
376 }
377}
378
379static void process_queued_ios(void *data)
380{
381 struct multipath *m = (struct multipath *) data;
382 struct hw_handler *hwh = &m->hw_handler;
383 struct pgpath *pgpath;
384 unsigned init_required, must_queue = 0;
385 unsigned long flags;
386
387 spin_lock_irqsave(&m->lock, flags);
388
389 if (!m->current_pgpath)
390 __choose_pgpath(m);
391
392 pgpath = m->current_pgpath;
393
394 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700395 (!pgpath && m->queue_if_no_path))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 must_queue = 1;
397
398 init_required = m->pg_init_required;
399 if (init_required)
400 m->pg_init_required = 0;
401
402 spin_unlock_irqrestore(&m->lock, flags);
403
404 if (init_required)
405 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
406
407 if (!must_queue)
408 dispatch_queued_ios(m);
409}
410
411/*
412 * An event is triggered whenever a path is taken out of use.
413 * Includes path failure and PG bypass.
414 */
415static void trigger_event(void *data)
416{
417 struct multipath *m = (struct multipath *) data;
418
419 dm_table_event(m->ti->table);
420}
421
422/*-----------------------------------------------------------------
423 * Constructor/argument parsing:
424 * <#multipath feature args> [<arg>]*
425 * <#hw_handler args> [hw_handler [<arg>]*]
426 * <#priority groups>
427 * <initial priority group>
428 * [<selector> <#selector args> [<arg>]*
429 * <#paths> <#per-path selector args>
430 * [<path> [<arg>]* ]+ ]+
431 *---------------------------------------------------------------*/
432struct param {
433 unsigned min;
434 unsigned max;
435 char *error;
436};
437
438#define ESTR(s) ("dm-multipath: " s)
439
440static int read_param(struct param *param, char *str, unsigned *v, char **error)
441{
442 if (!str ||
443 (sscanf(str, "%u", v) != 1) ||
444 (*v < param->min) ||
445 (*v > param->max)) {
446 *error = param->error;
447 return -EINVAL;
448 }
449
450 return 0;
451}
452
453struct arg_set {
454 unsigned argc;
455 char **argv;
456};
457
458static char *shift(struct arg_set *as)
459{
460 char *r;
461
462 if (as->argc) {
463 as->argc--;
464 r = *as->argv;
465 as->argv++;
466 return r;
467 }
468
469 return NULL;
470}
471
472static void consume(struct arg_set *as, unsigned n)
473{
474 BUG_ON (as->argc < n);
475 as->argc -= n;
476 as->argv += n;
477}
478
479static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
480 struct dm_target *ti)
481{
482 int r;
483 struct path_selector_type *pst;
484 unsigned ps_argc;
485
486 static struct param _params[] = {
487 {0, 1024, ESTR("invalid number of path selector args")},
488 };
489
490 pst = dm_get_path_selector(shift(as));
491 if (!pst) {
492 ti->error = ESTR("unknown path selector type");
493 return -EINVAL;
494 }
495
496 r = read_param(_params, shift(as), &ps_argc, &ti->error);
497 if (r)
498 return -EINVAL;
499
500 r = pst->create(&pg->ps, ps_argc, as->argv);
501 if (r) {
502 dm_put_path_selector(pst);
503 ti->error = ESTR("path selector constructor failed");
504 return r;
505 }
506
507 pg->ps.type = pst;
508 consume(as, ps_argc);
509
510 return 0;
511}
512
513static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
514 struct dm_target *ti)
515{
516 int r;
517 struct pgpath *p;
518
519 /* we need at least a path arg */
520 if (as->argc < 1) {
521 ti->error = ESTR("no device given");
522 return NULL;
523 }
524
525 p = alloc_pgpath();
526 if (!p)
527 return NULL;
528
529 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
530 dm_table_get_mode(ti->table), &p->path.dev);
531 if (r) {
532 ti->error = ESTR("error getting device");
533 goto bad;
534 }
535
536 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
537 if (r) {
538 dm_put_device(ti, p->path.dev);
539 goto bad;
540 }
541
542 return p;
543
544 bad:
545 free_pgpath(p);
546 return NULL;
547}
548
549static struct priority_group *parse_priority_group(struct arg_set *as,
550 struct multipath *m,
551 struct dm_target *ti)
552{
553 static struct param _params[] = {
554 {1, 1024, ESTR("invalid number of paths")},
555 {0, 1024, ESTR("invalid number of selector args")}
556 };
557
558 int r;
559 unsigned i, nr_selector_args, nr_params;
560 struct priority_group *pg;
561
562 if (as->argc < 2) {
563 as->argc = 0;
564 ti->error = ESTR("not enough priority group aruments");
565 return NULL;
566 }
567
568 pg = alloc_priority_group();
569 if (!pg) {
570 ti->error = ESTR("couldn't allocate priority group");
571 return NULL;
572 }
573 pg->m = m;
574
575 r = parse_path_selector(as, pg, ti);
576 if (r)
577 goto bad;
578
579 /*
580 * read the paths
581 */
582 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
583 if (r)
584 goto bad;
585
586 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
587 if (r)
588 goto bad;
589
590 nr_params = 1 + nr_selector_args;
591 for (i = 0; i < pg->nr_pgpaths; i++) {
592 struct pgpath *pgpath;
593 struct arg_set path_args;
594
595 if (as->argc < nr_params)
596 goto bad;
597
598 path_args.argc = nr_params;
599 path_args.argv = as->argv;
600
601 pgpath = parse_path(&path_args, &pg->ps, ti);
602 if (!pgpath)
603 goto bad;
604
605 pgpath->pg = pg;
606 list_add_tail(&pgpath->list, &pg->pgpaths);
607 consume(as, nr_params);
608 }
609
610 return pg;
611
612 bad:
613 free_priority_group(pg, ti);
614 return NULL;
615}
616
617static int parse_hw_handler(struct arg_set *as, struct multipath *m,
618 struct dm_target *ti)
619{
620 int r;
621 struct hw_handler_type *hwht;
622 unsigned hw_argc;
623
624 static struct param _params[] = {
625 {0, 1024, ESTR("invalid number of hardware handler args")},
626 };
627
628 r = read_param(_params, shift(as), &hw_argc, &ti->error);
629 if (r)
630 return -EINVAL;
631
632 if (!hw_argc)
633 return 0;
634
635 hwht = dm_get_hw_handler(shift(as));
636 if (!hwht) {
637 ti->error = ESTR("unknown hardware handler type");
638 return -EINVAL;
639 }
640
641 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
642 if (r) {
643 dm_put_hw_handler(hwht);
644 ti->error = ESTR("hardware handler constructor failed");
645 return r;
646 }
647
648 m->hw_handler.type = hwht;
649 consume(as, hw_argc - 1);
650
651 return 0;
652}
653
654static int parse_features(struct arg_set *as, struct multipath *m,
655 struct dm_target *ti)
656{
657 int r;
658 unsigned argc;
659
660 static struct param _params[] = {
661 {0, 1, ESTR("invalid number of feature args")},
662 };
663
664 r = read_param(_params, shift(as), &argc, &ti->error);
665 if (r)
666 return -EINVAL;
667
668 if (!argc)
669 return 0;
670
671 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
672 return queue_if_no_path(m, 1);
673 else {
674 ti->error = "Unrecognised multipath feature request";
675 return -EINVAL;
676 }
677}
678
679static int multipath_ctr(struct dm_target *ti, unsigned int argc,
680 char **argv)
681{
682 /* target parameters */
683 static struct param _params[] = {
684 {1, 1024, ESTR("invalid number of priority groups")},
685 {1, 1024, ESTR("invalid initial priority group number")},
686 };
687
688 int r;
689 struct multipath *m;
690 struct arg_set as;
691 unsigned pg_count = 0;
692 unsigned next_pg_num;
693
694 as.argc = argc;
695 as.argv = argv;
696
697 m = alloc_multipath();
698 if (!m) {
699 ti->error = ESTR("can't allocate multipath");
700 return -EINVAL;
701 }
702
703 r = parse_features(&as, m, ti);
704 if (r)
705 goto bad;
706
707 r = parse_hw_handler(&as, m, ti);
708 if (r)
709 goto bad;
710
711 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
712 if (r)
713 goto bad;
714
715 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
716 if (r)
717 goto bad;
718
719 /* parse the priority groups */
720 while (as.argc) {
721 struct priority_group *pg;
722
723 pg = parse_priority_group(&as, m, ti);
724 if (!pg) {
725 r = -EINVAL;
726 goto bad;
727 }
728
729 m->nr_valid_paths += pg->nr_pgpaths;
730 list_add_tail(&pg->list, &m->priority_groups);
731 pg_count++;
732 pg->pg_num = pg_count;
733 if (!--next_pg_num)
734 m->next_pg = pg;
735 }
736
737 if (pg_count != m->nr_priority_groups) {
738 ti->error = ESTR("priority group count mismatch");
739 r = -EINVAL;
740 goto bad;
741 }
742
743 ti->private = m;
744 m->ti = ti;
745
746 return 0;
747
748 bad:
749 free_multipath(m);
750 return r;
751}
752
753static void multipath_dtr(struct dm_target *ti)
754{
755 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700756
757 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 free_multipath(m);
759}
760
761/*
762 * Map bios, recording original fields for later in case we have to resubmit
763 */
764static int multipath_map(struct dm_target *ti, struct bio *bio,
765 union map_info *map_context)
766{
767 int r;
768 struct mpath_io *mpio;
769 struct multipath *m = (struct multipath *) ti->private;
770
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700771 if (bio_barrier(bio))
772 return -EOPNOTSUPP;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
775 dm_bio_record(&mpio->details, bio);
776
777 map_context->ptr = mpio;
778 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
779 r = map_io(m, bio, mpio, 0);
780 if (r < 0)
781 mempool_free(mpio, m->mpio_pool);
782
783 return r;
784}
785
786/*
787 * Take a path out of use.
788 */
789static int fail_path(struct pgpath *pgpath)
790{
791 unsigned long flags;
792 struct multipath *m = pgpath->pg->m;
793
794 spin_lock_irqsave(&m->lock, flags);
795
796 if (!pgpath->path.is_active)
797 goto out;
798
799 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
800
801 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
802 pgpath->path.is_active = 0;
803 pgpath->fail_count++;
804
805 m->nr_valid_paths--;
806
807 if (pgpath == m->current_pgpath)
808 m->current_pgpath = NULL;
809
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700810 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812out:
813 spin_unlock_irqrestore(&m->lock, flags);
814
815 return 0;
816}
817
818/*
819 * Reinstate a previously-failed path
820 */
821static int reinstate_path(struct pgpath *pgpath)
822{
823 int r = 0;
824 unsigned long flags;
825 struct multipath *m = pgpath->pg->m;
826
827 spin_lock_irqsave(&m->lock, flags);
828
829 if (pgpath->path.is_active)
830 goto out;
831
832 if (!pgpath->pg->ps.type) {
833 DMWARN("Reinstate path not supported by path selector %s",
834 pgpath->pg->ps.type->name);
835 r = -EINVAL;
836 goto out;
837 }
838
839 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
840 if (r)
841 goto out;
842
843 pgpath->path.is_active = 1;
844
845 m->current_pgpath = NULL;
846 if (!m->nr_valid_paths++)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700847 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700849 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851out:
852 spin_unlock_irqrestore(&m->lock, flags);
853
854 return r;
855}
856
857/*
858 * Fail or reinstate all paths that match the provided struct dm_dev.
859 */
860static int action_dev(struct multipath *m, struct dm_dev *dev,
861 action_fn action)
862{
863 int r = 0;
864 struct pgpath *pgpath;
865 struct priority_group *pg;
866
867 list_for_each_entry(pg, &m->priority_groups, list) {
868 list_for_each_entry(pgpath, &pg->pgpaths, list) {
869 if (pgpath->path.dev == dev)
870 r = action(pgpath);
871 }
872 }
873
874 return r;
875}
876
877/*
878 * Temporarily try to avoid having to use the specified PG
879 */
880static void bypass_pg(struct multipath *m, struct priority_group *pg,
881 int bypassed)
882{
883 unsigned long flags;
884
885 spin_lock_irqsave(&m->lock, flags);
886
887 pg->bypassed = bypassed;
888 m->current_pgpath = NULL;
889 m->current_pg = NULL;
890
891 spin_unlock_irqrestore(&m->lock, flags);
892
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700893 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * Switch to using the specified PG from the next I/O that gets mapped
898 */
899static int switch_pg_num(struct multipath *m, const char *pgstr)
900{
901 struct priority_group *pg;
902 unsigned pgnum;
903 unsigned long flags;
904
905 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
906 (pgnum > m->nr_priority_groups)) {
907 DMWARN("invalid PG number supplied to switch_pg_num");
908 return -EINVAL;
909 }
910
911 spin_lock_irqsave(&m->lock, flags);
912 list_for_each_entry(pg, &m->priority_groups, list) {
913 pg->bypassed = 0;
914 if (--pgnum)
915 continue;
916
917 m->current_pgpath = NULL;
918 m->current_pg = NULL;
919 m->next_pg = pg;
920 }
921 spin_unlock_irqrestore(&m->lock, flags);
922
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700923 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
927/*
928 * Set/clear bypassed status of a PG.
929 * PGs are numbered upwards from 1 in the order they were declared.
930 */
931static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
932{
933 struct priority_group *pg;
934 unsigned pgnum;
935
936 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
937 (pgnum > m->nr_priority_groups)) {
938 DMWARN("invalid PG number supplied to bypass_pg");
939 return -EINVAL;
940 }
941
942 list_for_each_entry(pg, &m->priority_groups, list) {
943 if (!--pgnum)
944 break;
945 }
946
947 bypass_pg(m, pg, bypassed);
948 return 0;
949}
950
951/*
952 * pg_init must call this when it has completed its initialisation
953 */
954void dm_pg_init_complete(struct path *path, unsigned err_flags)
955{
956 struct pgpath *pgpath = path_to_pgpath(path);
957 struct priority_group *pg = pgpath->pg;
958 struct multipath *m = pg->m;
959 unsigned long flags;
960
961 /* We insist on failing the path if the PG is already bypassed. */
962 if (err_flags && pg->bypassed)
963 err_flags |= MP_FAIL_PATH;
964
965 if (err_flags & MP_FAIL_PATH)
966 fail_path(pgpath);
967
968 if (err_flags & MP_BYPASS_PG)
969 bypass_pg(m, pg, 1);
970
971 spin_lock_irqsave(&m->lock, flags);
972 if (!err_flags)
973 m->queue_io = 0;
974 else {
975 m->current_pgpath = NULL;
976 m->current_pg = NULL;
977 }
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700978 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 spin_unlock_irqrestore(&m->lock, flags);
980}
981
982/*
983 * end_io handling
984 */
985static int do_end_io(struct multipath *m, struct bio *bio,
986 int error, struct mpath_io *mpio)
987{
988 struct hw_handler *hwh = &m->hw_handler;
989 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
990
991 if (!error)
992 return 0; /* I/O complete */
993
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -0700994 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
995 return error;
996
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700997 if (error == -EOPNOTSUPP)
998 return error;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 spin_lock(&m->lock);
1001 if (!m->nr_valid_paths) {
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001002 if (!m->queue_if_no_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 spin_unlock(&m->lock);
1004 return -EIO;
1005 } else {
1006 spin_unlock(&m->lock);
1007 goto requeue;
1008 }
1009 }
1010 spin_unlock(&m->lock);
1011
1012 if (hwh->type && hwh->type->error)
1013 err_flags = hwh->type->error(hwh, bio);
1014
1015 if (mpio->pgpath) {
1016 if (err_flags & MP_FAIL_PATH)
1017 fail_path(mpio->pgpath);
1018
1019 if (err_flags & MP_BYPASS_PG)
1020 bypass_pg(m, mpio->pgpath->pg, 1);
1021 }
1022
1023 if (err_flags & MP_ERROR_IO)
1024 return -EIO;
1025
1026 requeue:
1027 dm_bio_restore(&mpio->details, bio);
1028
1029 /* queue for the daemon to resubmit or fail */
1030 spin_lock(&m->lock);
1031 bio_list_add(&m->queued_ios, bio);
1032 m->queue_size++;
1033 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001034 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 spin_unlock(&m->lock);
1036
1037 return 1; /* io not complete */
1038}
1039
1040static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1041 int error, union map_info *map_context)
1042{
1043 struct multipath *m = (struct multipath *) ti->private;
1044 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1045 struct pgpath *pgpath = mpio->pgpath;
1046 struct path_selector *ps;
1047 int r;
1048
1049 r = do_end_io(m, bio, error, mpio);
1050 if (pgpath) {
1051 ps = &pgpath->pg->ps;
1052 if (ps->type->end_io)
1053 ps->type->end_io(ps, &pgpath->path);
1054 }
1055 if (r <= 0)
1056 mempool_free(mpio, m->mpio_pool);
1057
1058 return r;
1059}
1060
1061/*
1062 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001063 * the last path fails we must error any remaining I/O.
1064 * Note that if the freeze_bdev fails while suspending, the
1065 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
1067static void multipath_presuspend(struct dm_target *ti)
1068{
1069 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001071 queue_if_no_path(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001074/*
1075 * Restore the queue_if_no_path setting.
1076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static void multipath_resume(struct dm_target *ti)
1078{
1079 struct multipath *m = (struct multipath *) ti->private;
1080 unsigned long flags;
1081
1082 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001083 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_unlock_irqrestore(&m->lock, flags);
1085}
1086
1087/*
1088 * Info output has the following format:
1089 * num_multipath_feature_args [multipath_feature_args]*
1090 * num_handler_status_args [handler_status_args]*
1091 * num_groups init_group_number
1092 * [A|D|E num_ps_status_args [ps_status_args]*
1093 * num_paths num_selector_args
1094 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1095 *
1096 * Table output has the following format (identical to the constructor string):
1097 * num_feature_args [features_args]*
1098 * num_handler_args hw_handler [hw_handler_args]*
1099 * num_groups init_group_number
1100 * [priority selector-name num_ps_args [ps_args]*
1101 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1102 */
1103static int multipath_status(struct dm_target *ti, status_type_t type,
1104 char *result, unsigned int maxlen)
1105{
1106 int sz = 0;
1107 unsigned long flags;
1108 struct multipath *m = (struct multipath *) ti->private;
1109 struct hw_handler *hwh = &m->hw_handler;
1110 struct priority_group *pg;
1111 struct pgpath *p;
1112 unsigned pg_num;
1113 char state;
1114
1115 spin_lock_irqsave(&m->lock, flags);
1116
1117 /* Features */
1118 if (type == STATUSTYPE_INFO)
1119 DMEMIT("1 %u ", m->queue_size);
1120 else if (m->queue_if_no_path)
1121 DMEMIT("1 queue_if_no_path ");
1122 else
1123 DMEMIT("0 ");
1124
1125 if (hwh->type && hwh->type->status)
1126 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1127 else if (!hwh->type || type == STATUSTYPE_INFO)
1128 DMEMIT("0 ");
1129 else
1130 DMEMIT("1 %s ", hwh->type->name);
1131
1132 DMEMIT("%u ", m->nr_priority_groups);
1133
1134 if (m->next_pg)
1135 pg_num = m->next_pg->pg_num;
1136 else if (m->current_pg)
1137 pg_num = m->current_pg->pg_num;
1138 else
1139 pg_num = 1;
1140
1141 DMEMIT("%u ", pg_num);
1142
1143 switch (type) {
1144 case STATUSTYPE_INFO:
1145 list_for_each_entry(pg, &m->priority_groups, list) {
1146 if (pg->bypassed)
1147 state = 'D'; /* Disabled */
1148 else if (pg == m->current_pg)
1149 state = 'A'; /* Currently Active */
1150 else
1151 state = 'E'; /* Enabled */
1152
1153 DMEMIT("%c ", state);
1154
1155 if (pg->ps.type->status)
1156 sz += pg->ps.type->status(&pg->ps, NULL, type,
1157 result + sz,
1158 maxlen - sz);
1159 else
1160 DMEMIT("0 ");
1161
1162 DMEMIT("%u %u ", pg->nr_pgpaths,
1163 pg->ps.type->info_args);
1164
1165 list_for_each_entry(p, &pg->pgpaths, list) {
1166 DMEMIT("%s %s %u ", p->path.dev->name,
1167 p->path.is_active ? "A" : "F",
1168 p->fail_count);
1169 if (pg->ps.type->status)
1170 sz += pg->ps.type->status(&pg->ps,
1171 &p->path, type, result + sz,
1172 maxlen - sz);
1173 }
1174 }
1175 break;
1176
1177 case STATUSTYPE_TABLE:
1178 list_for_each_entry(pg, &m->priority_groups, list) {
1179 DMEMIT("%s ", pg->ps.type->name);
1180
1181 if (pg->ps.type->status)
1182 sz += pg->ps.type->status(&pg->ps, NULL, type,
1183 result + sz,
1184 maxlen - sz);
1185 else
1186 DMEMIT("0 ");
1187
1188 DMEMIT("%u %u ", pg->nr_pgpaths,
1189 pg->ps.type->table_args);
1190
1191 list_for_each_entry(p, &pg->pgpaths, list) {
1192 DMEMIT("%s ", p->path.dev->name);
1193 if (pg->ps.type->status)
1194 sz += pg->ps.type->status(&pg->ps,
1195 &p->path, type, result + sz,
1196 maxlen - sz);
1197 }
1198 }
1199 break;
1200 }
1201
1202 spin_unlock_irqrestore(&m->lock, flags);
1203
1204 return 0;
1205}
1206
1207static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1208{
1209 int r;
1210 struct dm_dev *dev;
1211 struct multipath *m = (struct multipath *) ti->private;
1212 action_fn action;
1213
1214 if (argc == 1) {
1215 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1216 return queue_if_no_path(m, 1);
1217 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1218 return queue_if_no_path(m, 0);
1219 }
1220
1221 if (argc != 2)
1222 goto error;
1223
1224 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1225 return bypass_pg_num(m, argv[1], 1);
1226 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1227 return bypass_pg_num(m, argv[1], 0);
1228 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1229 return switch_pg_num(m, argv[1]);
1230 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1231 action = reinstate_path;
1232 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1233 action = fail_path;
1234 else
1235 goto error;
1236
1237 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1238 dm_table_get_mode(ti->table), &dev);
1239 if (r) {
1240 DMWARN("dm-multipath message: error getting device %s",
1241 argv[1]);
1242 return -EINVAL;
1243 }
1244
1245 r = action_dev(m, dev, action);
1246
1247 dm_put_device(ti, dev);
1248
1249 return r;
1250
1251error:
1252 DMWARN("Unrecognised multipath message received.");
1253 return -EINVAL;
1254}
1255
1256/*-----------------------------------------------------------------
1257 * Module setup
1258 *---------------------------------------------------------------*/
1259static struct target_type multipath_target = {
1260 .name = "multipath",
1261 .version = {1, 0, 4},
1262 .module = THIS_MODULE,
1263 .ctr = multipath_ctr,
1264 .dtr = multipath_dtr,
1265 .map = multipath_map,
1266 .end_io = multipath_end_io,
1267 .presuspend = multipath_presuspend,
1268 .resume = multipath_resume,
1269 .status = multipath_status,
1270 .message = multipath_message,
1271};
1272
1273static int __init dm_multipath_init(void)
1274{
1275 int r;
1276
1277 /* allocate a slab for the dm_ios */
1278 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1279 0, 0, NULL, NULL);
1280 if (!_mpio_cache)
1281 return -ENOMEM;
1282
1283 r = dm_register_target(&multipath_target);
1284 if (r < 0) {
1285 DMERR("%s: register failed %d", multipath_target.name, r);
1286 kmem_cache_destroy(_mpio_cache);
1287 return -EINVAL;
1288 }
1289
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001290 kmultipathd = create_workqueue("kmpathd");
1291 if (!kmultipathd) {
1292 DMERR("%s: failed to create workqueue kmpathd",
1293 multipath_target.name);
1294 dm_unregister_target(&multipath_target);
1295 kmem_cache_destroy(_mpio_cache);
1296 return -ENOMEM;
1297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 DMINFO("dm-multipath version %u.%u.%u loaded",
1300 multipath_target.version[0], multipath_target.version[1],
1301 multipath_target.version[2]);
1302
1303 return r;
1304}
1305
1306static void __exit dm_multipath_exit(void)
1307{
1308 int r;
1309
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001310 destroy_workqueue(kmultipathd);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 r = dm_unregister_target(&multipath_target);
1313 if (r < 0)
1314 DMERR("%s: target unregister failed %d",
1315 multipath_target.name, r);
1316 kmem_cache_destroy(_mpio_cache);
1317}
1318
1319EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1320
1321module_init(dm_multipath_init);
1322module_exit(dm_multipath_exit);
1323
1324MODULE_DESCRIPTION(DM_NAME " multipath target");
1325MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1326MODULE_LICENSE("GPL");