blob: f72a82fb943489872b3d91abdffe4cb292c92267 [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? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070066 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 unsigned nr_valid_paths; /* Total number of usable paths */
69 struct pgpath *current_pgpath;
70 struct priority_group *current_pg;
71 struct priority_group *next_pg; /* Switch to this PG if set */
72 unsigned repeat_count; /* I/Os left before calling PS again */
73
74 unsigned queue_io; /* Must we queue all I/O? */
75 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070076 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 struct work_struct process_queued_ios;
79 struct bio_list queued_ios;
80 unsigned queue_size;
81
82 struct work_struct trigger_event;
83
84 /*
85 * We must use a mempool of mpath_io structs so that we
86 * can resubmit bios on error.
87 */
88 mempool_t *mpio_pool;
89};
90
91/*
92 * Context information attached to each bio we process.
93 */
94struct mpath_io {
95 struct pgpath *pgpath;
96 struct dm_bio_details details;
97};
98
99typedef int (*action_fn) (struct pgpath *pgpath);
100
101#define MIN_IOS 256 /* Mempool size */
102
103static kmem_cache_t *_mpio_cache;
104
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700105struct workqueue_struct *kmultipathd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static void process_queued_ios(void *data);
107static void trigger_event(void *data);
108
109
110/*-----------------------------------------------
111 * Allocation routines
112 *-----------------------------------------------*/
113
114static struct pgpath *alloc_pgpath(void)
115{
116 struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
117
118 if (pgpath) {
119 memset(pgpath, 0, sizeof(*pgpath));
120 pgpath->path.is_active = 1;
121 }
122
123 return pgpath;
124}
125
126static inline void free_pgpath(struct pgpath *pgpath)
127{
128 kfree(pgpath);
129}
130
131static struct priority_group *alloc_priority_group(void)
132{
133 struct priority_group *pg;
134
135 pg = kmalloc(sizeof(*pg), GFP_KERNEL);
136 if (!pg)
137 return NULL;
138
139 memset(pg, 0, sizeof(*pg));
140 INIT_LIST_HEAD(&pg->pgpaths);
141
142 return pg;
143}
144
145static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
146{
147 struct pgpath *pgpath, *tmp;
148
149 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
150 list_del(&pgpath->list);
151 dm_put_device(ti, pgpath->path.dev);
152 free_pgpath(pgpath);
153 }
154}
155
156static void free_priority_group(struct priority_group *pg,
157 struct dm_target *ti)
158{
159 struct path_selector *ps = &pg->ps;
160
161 if (ps->type) {
162 ps->type->destroy(ps);
163 dm_put_path_selector(ps->type);
164 }
165
166 free_pgpaths(&pg->pgpaths, ti);
167 kfree(pg);
168}
169
170static struct multipath *alloc_multipath(void)
171{
172 struct multipath *m;
173
174 m = kmalloc(sizeof(*m), GFP_KERNEL);
175 if (m) {
176 memset(m, 0, sizeof(*m));
177 INIT_LIST_HEAD(&m->priority_groups);
178 spin_lock_init(&m->lock);
179 m->queue_io = 1;
180 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
181 INIT_WORK(&m->trigger_event, trigger_event, m);
182 m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
183 mempool_free_slab, _mpio_cache);
184 if (!m->mpio_pool) {
185 kfree(m);
186 return NULL;
187 }
188 }
189
190 return m;
191}
192
193static void free_multipath(struct multipath *m)
194{
195 struct priority_group *pg, *tmp;
196 struct hw_handler *hwh = &m->hw_handler;
197
198 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
199 list_del(&pg->list);
200 free_priority_group(pg, m->ti);
201 }
202
203 if (hwh->type) {
204 hwh->type->destroy(hwh);
205 dm_put_hw_handler(hwh->type);
206 }
207
208 mempool_destroy(m->mpio_pool);
209 kfree(m);
210}
211
212
213/*-----------------------------------------------
214 * Path selection
215 *-----------------------------------------------*/
216
217static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
218{
219 struct hw_handler *hwh = &m->hw_handler;
220
221 m->current_pg = pgpath->pg;
222
223 /* Must we initialise the PG first, and queue I/O till it's ready? */
224 if (hwh->type && hwh->type->pg_init) {
225 m->pg_init_required = 1;
226 m->queue_io = 1;
227 } else {
228 m->pg_init_required = 0;
229 m->queue_io = 0;
230 }
231}
232
233static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
234{
235 struct path *path;
236
237 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
238 if (!path)
239 return -ENXIO;
240
241 m->current_pgpath = path_to_pgpath(path);
242
243 if (m->current_pg != pg)
244 __switch_pg(m, m->current_pgpath);
245
246 return 0;
247}
248
249static void __choose_pgpath(struct multipath *m)
250{
251 struct priority_group *pg;
252 unsigned bypassed = 1;
253
254 if (!m->nr_valid_paths)
255 goto failed;
256
257 /* Were we instructed to switch PG? */
258 if (m->next_pg) {
259 pg = m->next_pg;
260 m->next_pg = NULL;
261 if (!__choose_path_in_pg(m, pg))
262 return;
263 }
264
265 /* Don't change PG until it has no remaining paths */
266 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
267 return;
268
269 /*
270 * Loop through priority groups until we find a valid path.
271 * First time we skip PGs marked 'bypassed'.
272 * Second time we only try the ones we skipped.
273 */
274 do {
275 list_for_each_entry(pg, &m->priority_groups, list) {
276 if (pg->bypassed == bypassed)
277 continue;
278 if (!__choose_path_in_pg(m, pg))
279 return;
280 }
281 } while (bypassed--);
282
283failed:
284 m->current_pgpath = NULL;
285 m->current_pg = NULL;
286}
287
288static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
289 unsigned was_queued)
290{
291 int r = 1;
292 unsigned long flags;
293 struct pgpath *pgpath;
294
295 spin_lock_irqsave(&m->lock, flags);
296
297 /* Do we need to select a new pgpath? */
298 if (!m->current_pgpath ||
299 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
300 __choose_pgpath(m);
301
302 pgpath = m->current_pgpath;
303
304 if (was_queued)
305 m->queue_size--;
306
307 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700308 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Queue for the daemon to resubmit */
310 bio_list_add(&m->queued_ios, bio);
311 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700312 if ((m->pg_init_required && !m->pg_init_in_progress) ||
313 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700314 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 pgpath = NULL;
316 r = 0;
317 } else if (!pgpath)
318 r = -EIO; /* Failed */
319 else
320 bio->bi_bdev = pgpath->path.dev->bdev;
321
322 mpio->pgpath = pgpath;
323
324 spin_unlock_irqrestore(&m->lock, flags);
325
326 return r;
327}
328
329/*
330 * If we run out of usable paths, should we queue I/O or error it?
331 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700332static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
333 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 unsigned long flags;
336
337 spin_lock_irqsave(&m->lock, flags);
338
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700339 if (save_old_value)
340 m->saved_queue_if_no_path = m->queue_if_no_path;
341 else
342 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700344 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700345 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 spin_unlock_irqrestore(&m->lock, flags);
348
349 return 0;
350}
351
352/*-----------------------------------------------------------------
353 * The multipath daemon is responsible for resubmitting queued ios.
354 *---------------------------------------------------------------*/
355
356static void dispatch_queued_ios(struct multipath *m)
357{
358 int r;
359 unsigned long flags;
360 struct bio *bio = NULL, *next;
361 struct mpath_io *mpio;
362 union map_info *info;
363
364 spin_lock_irqsave(&m->lock, flags);
365 bio = bio_list_get(&m->queued_ios);
366 spin_unlock_irqrestore(&m->lock, flags);
367
368 while (bio) {
369 next = bio->bi_next;
370 bio->bi_next = NULL;
371
372 info = dm_get_mapinfo(bio);
373 mpio = info->ptr;
374
375 r = map_io(m, bio, mpio, 1);
376 if (r < 0)
377 bio_endio(bio, bio->bi_size, r);
378 else if (r == 1)
379 generic_make_request(bio);
380
381 bio = next;
382 }
383}
384
385static void process_queued_ios(void *data)
386{
387 struct multipath *m = (struct multipath *) data;
388 struct hw_handler *hwh = &m->hw_handler;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700389 struct pgpath *pgpath = NULL;
390 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned long flags;
392
393 spin_lock_irqsave(&m->lock, flags);
394
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700395 if (!m->queue_size)
396 goto out;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!m->current_pgpath)
399 __choose_pgpath(m);
400
401 pgpath = m->current_pgpath;
402
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700403 if ((pgpath && !m->queue_io) ||
404 (!pgpath && !m->queue_if_no_path))
405 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700407 if (m->pg_init_required && !m->pg_init_in_progress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700409 m->pg_init_in_progress = 1;
410 init_required = 1;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700413out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_unlock_irqrestore(&m->lock, flags);
415
416 if (init_required)
417 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
418
419 if (!must_queue)
420 dispatch_queued_ios(m);
421}
422
423/*
424 * An event is triggered whenever a path is taken out of use.
425 * Includes path failure and PG bypass.
426 */
427static void trigger_event(void *data)
428{
429 struct multipath *m = (struct multipath *) data;
430
431 dm_table_event(m->ti->table);
432}
433
434/*-----------------------------------------------------------------
435 * Constructor/argument parsing:
436 * <#multipath feature args> [<arg>]*
437 * <#hw_handler args> [hw_handler [<arg>]*]
438 * <#priority groups>
439 * <initial priority group>
440 * [<selector> <#selector args> [<arg>]*
441 * <#paths> <#per-path selector args>
442 * [<path> [<arg>]* ]+ ]+
443 *---------------------------------------------------------------*/
444struct param {
445 unsigned min;
446 unsigned max;
447 char *error;
448};
449
450#define ESTR(s) ("dm-multipath: " s)
451
452static int read_param(struct param *param, char *str, unsigned *v, char **error)
453{
454 if (!str ||
455 (sscanf(str, "%u", v) != 1) ||
456 (*v < param->min) ||
457 (*v > param->max)) {
458 *error = param->error;
459 return -EINVAL;
460 }
461
462 return 0;
463}
464
465struct arg_set {
466 unsigned argc;
467 char **argv;
468};
469
470static char *shift(struct arg_set *as)
471{
472 char *r;
473
474 if (as->argc) {
475 as->argc--;
476 r = *as->argv;
477 as->argv++;
478 return r;
479 }
480
481 return NULL;
482}
483
484static void consume(struct arg_set *as, unsigned n)
485{
486 BUG_ON (as->argc < n);
487 as->argc -= n;
488 as->argv += n;
489}
490
491static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
492 struct dm_target *ti)
493{
494 int r;
495 struct path_selector_type *pst;
496 unsigned ps_argc;
497
498 static struct param _params[] = {
499 {0, 1024, ESTR("invalid number of path selector args")},
500 };
501
502 pst = dm_get_path_selector(shift(as));
503 if (!pst) {
504 ti->error = ESTR("unknown path selector type");
505 return -EINVAL;
506 }
507
508 r = read_param(_params, shift(as), &ps_argc, &ti->error);
509 if (r)
510 return -EINVAL;
511
512 r = pst->create(&pg->ps, ps_argc, as->argv);
513 if (r) {
514 dm_put_path_selector(pst);
515 ti->error = ESTR("path selector constructor failed");
516 return r;
517 }
518
519 pg->ps.type = pst;
520 consume(as, ps_argc);
521
522 return 0;
523}
524
525static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
526 struct dm_target *ti)
527{
528 int r;
529 struct pgpath *p;
530
531 /* we need at least a path arg */
532 if (as->argc < 1) {
533 ti->error = ESTR("no device given");
534 return NULL;
535 }
536
537 p = alloc_pgpath();
538 if (!p)
539 return NULL;
540
541 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
542 dm_table_get_mode(ti->table), &p->path.dev);
543 if (r) {
544 ti->error = ESTR("error getting device");
545 goto bad;
546 }
547
548 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
549 if (r) {
550 dm_put_device(ti, p->path.dev);
551 goto bad;
552 }
553
554 return p;
555
556 bad:
557 free_pgpath(p);
558 return NULL;
559}
560
561static struct priority_group *parse_priority_group(struct arg_set *as,
562 struct multipath *m,
563 struct dm_target *ti)
564{
565 static struct param _params[] = {
566 {1, 1024, ESTR("invalid number of paths")},
567 {0, 1024, ESTR("invalid number of selector args")}
568 };
569
570 int r;
571 unsigned i, nr_selector_args, nr_params;
572 struct priority_group *pg;
573
574 if (as->argc < 2) {
575 as->argc = 0;
576 ti->error = ESTR("not enough priority group aruments");
577 return NULL;
578 }
579
580 pg = alloc_priority_group();
581 if (!pg) {
582 ti->error = ESTR("couldn't allocate priority group");
583 return NULL;
584 }
585 pg->m = m;
586
587 r = parse_path_selector(as, pg, ti);
588 if (r)
589 goto bad;
590
591 /*
592 * read the paths
593 */
594 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
595 if (r)
596 goto bad;
597
598 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
599 if (r)
600 goto bad;
601
602 nr_params = 1 + nr_selector_args;
603 for (i = 0; i < pg->nr_pgpaths; i++) {
604 struct pgpath *pgpath;
605 struct arg_set path_args;
606
607 if (as->argc < nr_params)
608 goto bad;
609
610 path_args.argc = nr_params;
611 path_args.argv = as->argv;
612
613 pgpath = parse_path(&path_args, &pg->ps, ti);
614 if (!pgpath)
615 goto bad;
616
617 pgpath->pg = pg;
618 list_add_tail(&pgpath->list, &pg->pgpaths);
619 consume(as, nr_params);
620 }
621
622 return pg;
623
624 bad:
625 free_priority_group(pg, ti);
626 return NULL;
627}
628
629static int parse_hw_handler(struct arg_set *as, struct multipath *m,
630 struct dm_target *ti)
631{
632 int r;
633 struct hw_handler_type *hwht;
634 unsigned hw_argc;
635
636 static struct param _params[] = {
637 {0, 1024, ESTR("invalid number of hardware handler args")},
638 };
639
640 r = read_param(_params, shift(as), &hw_argc, &ti->error);
641 if (r)
642 return -EINVAL;
643
644 if (!hw_argc)
645 return 0;
646
647 hwht = dm_get_hw_handler(shift(as));
648 if (!hwht) {
649 ti->error = ESTR("unknown hardware handler type");
650 return -EINVAL;
651 }
652
653 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
654 if (r) {
655 dm_put_hw_handler(hwht);
656 ti->error = ESTR("hardware handler constructor failed");
657 return r;
658 }
659
660 m->hw_handler.type = hwht;
661 consume(as, hw_argc - 1);
662
663 return 0;
664}
665
666static int parse_features(struct arg_set *as, struct multipath *m,
667 struct dm_target *ti)
668{
669 int r;
670 unsigned argc;
671
672 static struct param _params[] = {
673 {0, 1, ESTR("invalid number of feature args")},
674 };
675
676 r = read_param(_params, shift(as), &argc, &ti->error);
677 if (r)
678 return -EINVAL;
679
680 if (!argc)
681 return 0;
682
683 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700684 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 else {
686 ti->error = "Unrecognised multipath feature request";
687 return -EINVAL;
688 }
689}
690
691static int multipath_ctr(struct dm_target *ti, unsigned int argc,
692 char **argv)
693{
694 /* target parameters */
695 static struct param _params[] = {
696 {1, 1024, ESTR("invalid number of priority groups")},
697 {1, 1024, ESTR("invalid initial priority group number")},
698 };
699
700 int r;
701 struct multipath *m;
702 struct arg_set as;
703 unsigned pg_count = 0;
704 unsigned next_pg_num;
705
706 as.argc = argc;
707 as.argv = argv;
708
709 m = alloc_multipath();
710 if (!m) {
711 ti->error = ESTR("can't allocate multipath");
712 return -EINVAL;
713 }
714
715 r = parse_features(&as, m, ti);
716 if (r)
717 goto bad;
718
719 r = parse_hw_handler(&as, m, ti);
720 if (r)
721 goto bad;
722
723 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
724 if (r)
725 goto bad;
726
727 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
728 if (r)
729 goto bad;
730
731 /* parse the priority groups */
732 while (as.argc) {
733 struct priority_group *pg;
734
735 pg = parse_priority_group(&as, m, ti);
736 if (!pg) {
737 r = -EINVAL;
738 goto bad;
739 }
740
741 m->nr_valid_paths += pg->nr_pgpaths;
742 list_add_tail(&pg->list, &m->priority_groups);
743 pg_count++;
744 pg->pg_num = pg_count;
745 if (!--next_pg_num)
746 m->next_pg = pg;
747 }
748
749 if (pg_count != m->nr_priority_groups) {
750 ti->error = ESTR("priority group count mismatch");
751 r = -EINVAL;
752 goto bad;
753 }
754
755 ti->private = m;
756 m->ti = ti;
757
758 return 0;
759
760 bad:
761 free_multipath(m);
762 return r;
763}
764
765static void multipath_dtr(struct dm_target *ti)
766{
767 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700768
769 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 free_multipath(m);
771}
772
773/*
774 * Map bios, recording original fields for later in case we have to resubmit
775 */
776static int multipath_map(struct dm_target *ti, struct bio *bio,
777 union map_info *map_context)
778{
779 int r;
780 struct mpath_io *mpio;
781 struct multipath *m = (struct multipath *) ti->private;
782
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700783 if (bio_barrier(bio))
784 return -EOPNOTSUPP;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
787 dm_bio_record(&mpio->details, bio);
788
789 map_context->ptr = mpio;
790 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
791 r = map_io(m, bio, mpio, 0);
792 if (r < 0)
793 mempool_free(mpio, m->mpio_pool);
794
795 return r;
796}
797
798/*
799 * Take a path out of use.
800 */
801static int fail_path(struct pgpath *pgpath)
802{
803 unsigned long flags;
804 struct multipath *m = pgpath->pg->m;
805
806 spin_lock_irqsave(&m->lock, flags);
807
808 if (!pgpath->path.is_active)
809 goto out;
810
811 DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
812
813 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
814 pgpath->path.is_active = 0;
815 pgpath->fail_count++;
816
817 m->nr_valid_paths--;
818
819 if (pgpath == m->current_pgpath)
820 m->current_pgpath = NULL;
821
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700822 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824out:
825 spin_unlock_irqrestore(&m->lock, flags);
826
827 return 0;
828}
829
830/*
831 * Reinstate a previously-failed path
832 */
833static int reinstate_path(struct pgpath *pgpath)
834{
835 int r = 0;
836 unsigned long flags;
837 struct multipath *m = pgpath->pg->m;
838
839 spin_lock_irqsave(&m->lock, flags);
840
841 if (pgpath->path.is_active)
842 goto out;
843
844 if (!pgpath->pg->ps.type) {
845 DMWARN("Reinstate path not supported by path selector %s",
846 pgpath->pg->ps.type->name);
847 r = -EINVAL;
848 goto out;
849 }
850
851 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
852 if (r)
853 goto out;
854
855 pgpath->path.is_active = 1;
856
857 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700858 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700859 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700861 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863out:
864 spin_unlock_irqrestore(&m->lock, flags);
865
866 return r;
867}
868
869/*
870 * Fail or reinstate all paths that match the provided struct dm_dev.
871 */
872static int action_dev(struct multipath *m, struct dm_dev *dev,
873 action_fn action)
874{
875 int r = 0;
876 struct pgpath *pgpath;
877 struct priority_group *pg;
878
879 list_for_each_entry(pg, &m->priority_groups, list) {
880 list_for_each_entry(pgpath, &pg->pgpaths, list) {
881 if (pgpath->path.dev == dev)
882 r = action(pgpath);
883 }
884 }
885
886 return r;
887}
888
889/*
890 * Temporarily try to avoid having to use the specified PG
891 */
892static void bypass_pg(struct multipath *m, struct priority_group *pg,
893 int bypassed)
894{
895 unsigned long flags;
896
897 spin_lock_irqsave(&m->lock, flags);
898
899 pg->bypassed = bypassed;
900 m->current_pgpath = NULL;
901 m->current_pg = NULL;
902
903 spin_unlock_irqrestore(&m->lock, flags);
904
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700905 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908/*
909 * Switch to using the specified PG from the next I/O that gets mapped
910 */
911static int switch_pg_num(struct multipath *m, const char *pgstr)
912{
913 struct priority_group *pg;
914 unsigned pgnum;
915 unsigned long flags;
916
917 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
918 (pgnum > m->nr_priority_groups)) {
919 DMWARN("invalid PG number supplied to switch_pg_num");
920 return -EINVAL;
921 }
922
923 spin_lock_irqsave(&m->lock, flags);
924 list_for_each_entry(pg, &m->priority_groups, list) {
925 pg->bypassed = 0;
926 if (--pgnum)
927 continue;
928
929 m->current_pgpath = NULL;
930 m->current_pg = NULL;
931 m->next_pg = pg;
932 }
933 spin_unlock_irqrestore(&m->lock, flags);
934
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700935 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
939/*
940 * Set/clear bypassed status of a PG.
941 * PGs are numbered upwards from 1 in the order they were declared.
942 */
943static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
944{
945 struct priority_group *pg;
946 unsigned pgnum;
947
948 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
949 (pgnum > m->nr_priority_groups)) {
950 DMWARN("invalid PG number supplied to bypass_pg");
951 return -EINVAL;
952 }
953
954 list_for_each_entry(pg, &m->priority_groups, list) {
955 if (!--pgnum)
956 break;
957 }
958
959 bypass_pg(m, pg, bypassed);
960 return 0;
961}
962
963/*
964 * pg_init must call this when it has completed its initialisation
965 */
966void dm_pg_init_complete(struct path *path, unsigned err_flags)
967{
968 struct pgpath *pgpath = path_to_pgpath(path);
969 struct priority_group *pg = pgpath->pg;
970 struct multipath *m = pg->m;
971 unsigned long flags;
972
973 /* We insist on failing the path if the PG is already bypassed. */
974 if (err_flags && pg->bypassed)
975 err_flags |= MP_FAIL_PATH;
976
977 if (err_flags & MP_FAIL_PATH)
978 fail_path(pgpath);
979
980 if (err_flags & MP_BYPASS_PG)
981 bypass_pg(m, pg, 1);
982
983 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700984 if (err_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 m->current_pgpath = NULL;
986 m->current_pg = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700987 } else if (!m->pg_init_required)
988 m->queue_io = 0;
989
990 m->pg_init_in_progress = 0;
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700991 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_unlock_irqrestore(&m->lock, flags);
993}
994
995/*
996 * end_io handling
997 */
998static int do_end_io(struct multipath *m, struct bio *bio,
999 int error, struct mpath_io *mpio)
1000{
1001 struct hw_handler *hwh = &m->hw_handler;
1002 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001003 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (!error)
1006 return 0; /* I/O complete */
1007
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001008 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1009 return error;
1010
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001011 if (error == -EOPNOTSUPP)
1012 return error;
1013
Stefan Bader640eb3b2005-11-21 21:32:35 -08001014 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!m->nr_valid_paths) {
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001016 if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001017 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EIO;
1019 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001020 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto requeue;
1022 }
1023 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001024 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (hwh->type && hwh->type->error)
1027 err_flags = hwh->type->error(hwh, bio);
1028
1029 if (mpio->pgpath) {
1030 if (err_flags & MP_FAIL_PATH)
1031 fail_path(mpio->pgpath);
1032
1033 if (err_flags & MP_BYPASS_PG)
1034 bypass_pg(m, mpio->pgpath->pg, 1);
1035 }
1036
1037 if (err_flags & MP_ERROR_IO)
1038 return -EIO;
1039
1040 requeue:
1041 dm_bio_restore(&mpio->details, bio);
1042
1043 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001044 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 bio_list_add(&m->queued_ios, bio);
1046 m->queue_size++;
1047 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001048 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001049 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 return 1; /* io not complete */
1052}
1053
1054static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1055 int error, union map_info *map_context)
1056{
1057 struct multipath *m = (struct multipath *) ti->private;
1058 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1059 struct pgpath *pgpath = mpio->pgpath;
1060 struct path_selector *ps;
1061 int r;
1062
1063 r = do_end_io(m, bio, error, mpio);
1064 if (pgpath) {
1065 ps = &pgpath->pg->ps;
1066 if (ps->type->end_io)
1067 ps->type->end_io(ps, &pgpath->path);
1068 }
1069 if (r <= 0)
1070 mempool_free(mpio, m->mpio_pool);
1071
1072 return r;
1073}
1074
1075/*
1076 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001077 * the last path fails we must error any remaining I/O.
1078 * Note that if the freeze_bdev fails while suspending, the
1079 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
1081static void multipath_presuspend(struct dm_target *ti)
1082{
1083 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001085 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001088/*
1089 * Restore the queue_if_no_path setting.
1090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static void multipath_resume(struct dm_target *ti)
1092{
1093 struct multipath *m = (struct multipath *) ti->private;
1094 unsigned long flags;
1095
1096 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001097 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 spin_unlock_irqrestore(&m->lock, flags);
1099}
1100
1101/*
1102 * Info output has the following format:
1103 * num_multipath_feature_args [multipath_feature_args]*
1104 * num_handler_status_args [handler_status_args]*
1105 * num_groups init_group_number
1106 * [A|D|E num_ps_status_args [ps_status_args]*
1107 * num_paths num_selector_args
1108 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1109 *
1110 * Table output has the following format (identical to the constructor string):
1111 * num_feature_args [features_args]*
1112 * num_handler_args hw_handler [hw_handler_args]*
1113 * num_groups init_group_number
1114 * [priority selector-name num_ps_args [ps_args]*
1115 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1116 */
1117static int multipath_status(struct dm_target *ti, status_type_t type,
1118 char *result, unsigned int maxlen)
1119{
1120 int sz = 0;
1121 unsigned long flags;
1122 struct multipath *m = (struct multipath *) ti->private;
1123 struct hw_handler *hwh = &m->hw_handler;
1124 struct priority_group *pg;
1125 struct pgpath *p;
1126 unsigned pg_num;
1127 char state;
1128
1129 spin_lock_irqsave(&m->lock, flags);
1130
1131 /* Features */
1132 if (type == STATUSTYPE_INFO)
1133 DMEMIT("1 %u ", m->queue_size);
1134 else if (m->queue_if_no_path)
1135 DMEMIT("1 queue_if_no_path ");
1136 else
1137 DMEMIT("0 ");
1138
1139 if (hwh->type && hwh->type->status)
1140 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1141 else if (!hwh->type || type == STATUSTYPE_INFO)
1142 DMEMIT("0 ");
1143 else
1144 DMEMIT("1 %s ", hwh->type->name);
1145
1146 DMEMIT("%u ", m->nr_priority_groups);
1147
1148 if (m->next_pg)
1149 pg_num = m->next_pg->pg_num;
1150 else if (m->current_pg)
1151 pg_num = m->current_pg->pg_num;
1152 else
1153 pg_num = 1;
1154
1155 DMEMIT("%u ", pg_num);
1156
1157 switch (type) {
1158 case STATUSTYPE_INFO:
1159 list_for_each_entry(pg, &m->priority_groups, list) {
1160 if (pg->bypassed)
1161 state = 'D'; /* Disabled */
1162 else if (pg == m->current_pg)
1163 state = 'A'; /* Currently Active */
1164 else
1165 state = 'E'; /* Enabled */
1166
1167 DMEMIT("%c ", state);
1168
1169 if (pg->ps.type->status)
1170 sz += pg->ps.type->status(&pg->ps, NULL, type,
1171 result + sz,
1172 maxlen - sz);
1173 else
1174 DMEMIT("0 ");
1175
1176 DMEMIT("%u %u ", pg->nr_pgpaths,
1177 pg->ps.type->info_args);
1178
1179 list_for_each_entry(p, &pg->pgpaths, list) {
1180 DMEMIT("%s %s %u ", p->path.dev->name,
1181 p->path.is_active ? "A" : "F",
1182 p->fail_count);
1183 if (pg->ps.type->status)
1184 sz += pg->ps.type->status(&pg->ps,
1185 &p->path, type, result + sz,
1186 maxlen - sz);
1187 }
1188 }
1189 break;
1190
1191 case STATUSTYPE_TABLE:
1192 list_for_each_entry(pg, &m->priority_groups, list) {
1193 DMEMIT("%s ", pg->ps.type->name);
1194
1195 if (pg->ps.type->status)
1196 sz += pg->ps.type->status(&pg->ps, NULL, type,
1197 result + sz,
1198 maxlen - sz);
1199 else
1200 DMEMIT("0 ");
1201
1202 DMEMIT("%u %u ", pg->nr_pgpaths,
1203 pg->ps.type->table_args);
1204
1205 list_for_each_entry(p, &pg->pgpaths, list) {
1206 DMEMIT("%s ", p->path.dev->name);
1207 if (pg->ps.type->status)
1208 sz += pg->ps.type->status(&pg->ps,
1209 &p->path, type, result + sz,
1210 maxlen - sz);
1211 }
1212 }
1213 break;
1214 }
1215
1216 spin_unlock_irqrestore(&m->lock, flags);
1217
1218 return 0;
1219}
1220
1221static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1222{
1223 int r;
1224 struct dm_dev *dev;
1225 struct multipath *m = (struct multipath *) ti->private;
1226 action_fn action;
1227
1228 if (argc == 1) {
1229 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001230 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001232 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 if (argc != 2)
1236 goto error;
1237
1238 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1239 return bypass_pg_num(m, argv[1], 1);
1240 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1241 return bypass_pg_num(m, argv[1], 0);
1242 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1243 return switch_pg_num(m, argv[1]);
1244 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1245 action = reinstate_path;
1246 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1247 action = fail_path;
1248 else
1249 goto error;
1250
1251 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1252 dm_table_get_mode(ti->table), &dev);
1253 if (r) {
1254 DMWARN("dm-multipath message: error getting device %s",
1255 argv[1]);
1256 return -EINVAL;
1257 }
1258
1259 r = action_dev(m, dev, action);
1260
1261 dm_put_device(ti, dev);
1262
1263 return r;
1264
1265error:
1266 DMWARN("Unrecognised multipath message received.");
1267 return -EINVAL;
1268}
1269
1270/*-----------------------------------------------------------------
1271 * Module setup
1272 *---------------------------------------------------------------*/
1273static struct target_type multipath_target = {
1274 .name = "multipath",
1275 .version = {1, 0, 4},
1276 .module = THIS_MODULE,
1277 .ctr = multipath_ctr,
1278 .dtr = multipath_dtr,
1279 .map = multipath_map,
1280 .end_io = multipath_end_io,
1281 .presuspend = multipath_presuspend,
1282 .resume = multipath_resume,
1283 .status = multipath_status,
1284 .message = multipath_message,
1285};
1286
1287static int __init dm_multipath_init(void)
1288{
1289 int r;
1290
1291 /* allocate a slab for the dm_ios */
1292 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1293 0, 0, NULL, NULL);
1294 if (!_mpio_cache)
1295 return -ENOMEM;
1296
1297 r = dm_register_target(&multipath_target);
1298 if (r < 0) {
1299 DMERR("%s: register failed %d", multipath_target.name, r);
1300 kmem_cache_destroy(_mpio_cache);
1301 return -EINVAL;
1302 }
1303
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001304 kmultipathd = create_workqueue("kmpathd");
1305 if (!kmultipathd) {
1306 DMERR("%s: failed to create workqueue kmpathd",
1307 multipath_target.name);
1308 dm_unregister_target(&multipath_target);
1309 kmem_cache_destroy(_mpio_cache);
1310 return -ENOMEM;
1311 }
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 DMINFO("dm-multipath version %u.%u.%u loaded",
1314 multipath_target.version[0], multipath_target.version[1],
1315 multipath_target.version[2]);
1316
1317 return r;
1318}
1319
1320static void __exit dm_multipath_exit(void)
1321{
1322 int r;
1323
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001324 destroy_workqueue(kmultipathd);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 r = dm_unregister_target(&multipath_target);
1327 if (r < 0)
1328 DMERR("%s: target unregister failed %d",
1329 multipath_target.name, r);
1330 kmem_cache_destroy(_mpio_cache);
1331}
1332
1333EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1334
1335module_init(dm_multipath_init);
1336module_exit(dm_multipath_exit);
1337
1338MODULE_DESCRIPTION(DM_NAME " multipath target");
1339MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1340MODULE_LICENSE("GPL");