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