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