Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6
[deliverable/linux.git] / drivers / md / dm-mpath.c
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
24 #define DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
26
27 /* Path properties */
28 struct 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 */
43 struct 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 */
57 struct 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? */
67 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
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? */
77 unsigned saved_queue_if_no_path;/* Saved state during suspension */
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 */
95 struct mpath_io {
96 struct pgpath *pgpath;
97 struct dm_bio_details details;
98 };
99
100 typedef int (*action_fn) (struct pgpath *pgpath);
101
102 #define MIN_IOS 256 /* Mempool size */
103
104 static kmem_cache_t *_mpio_cache;
105
106 struct workqueue_struct *kmultipathd;
107 static void process_queued_ios(void *data);
108 static void trigger_event(void *data);
109
110
111 /*-----------------------------------------------
112 * Allocation routines
113 *-----------------------------------------------*/
114
115 static struct pgpath *alloc_pgpath(void)
116 {
117 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
118
119 if (pgpath)
120 pgpath->path.is_active = 1;
121
122 return pgpath;
123 }
124
125 static inline void free_pgpath(struct pgpath *pgpath)
126 {
127 kfree(pgpath);
128 }
129
130 static struct priority_group *alloc_priority_group(void)
131 {
132 struct priority_group *pg;
133
134 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
135
136 if (pg)
137 INIT_LIST_HEAD(&pg->pgpaths);
138
139 return pg;
140 }
141
142 static 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
153 static 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
167 static struct multipath *alloc_multipath(struct dm_target *ti)
168 {
169 struct multipath *m;
170
171 m = kzalloc(sizeof(*m), GFP_KERNEL);
172 if (m) {
173 INIT_LIST_HEAD(&m->priority_groups);
174 spin_lock_init(&m->lock);
175 m->queue_io = 1;
176 INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
177 INIT_WORK(&m->trigger_event, trigger_event, m);
178 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
179 if (!m->mpio_pool) {
180 kfree(m);
181 return NULL;
182 }
183 m->ti = ti;
184 ti->private = m;
185 }
186
187 return m;
188 }
189
190 static 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
214 static 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
230 static 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
246 static 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
280 failed:
281 m->current_pgpath = NULL;
282 m->current_pg = NULL;
283 }
284
285 static 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) ||
305 (!pgpath && m->queue_if_no_path)) {
306 /* Queue for the daemon to resubmit */
307 bio_list_add(&m->queued_ios, bio);
308 m->queue_size++;
309 if ((m->pg_init_required && !m->pg_init_in_progress) ||
310 !m->queue_io)
311 queue_work(kmultipathd, &m->process_queued_ios);
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 */
329 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
330 unsigned save_old_value)
331 {
332 unsigned long flags;
333
334 spin_lock_irqsave(&m->lock, flags);
335
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;
340 m->queue_if_no_path = queue_if_no_path;
341 if (!m->queue_if_no_path && m->queue_size)
342 queue_work(kmultipathd, &m->process_queued_ios);
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
353 static 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
382 static void process_queued_ios(void *data)
383 {
384 struct multipath *m = (struct multipath *) data;
385 struct hw_handler *hwh = &m->hw_handler;
386 struct pgpath *pgpath = NULL;
387 unsigned init_required = 0, must_queue = 1;
388 unsigned long flags;
389
390 spin_lock_irqsave(&m->lock, flags);
391
392 if (!m->queue_size)
393 goto out;
394
395 if (!m->current_pgpath)
396 __choose_pgpath(m);
397
398 pgpath = m->current_pgpath;
399
400 if ((pgpath && !m->queue_io) ||
401 (!pgpath && !m->queue_if_no_path))
402 must_queue = 0;
403
404 if (m->pg_init_required && !m->pg_init_in_progress) {
405 m->pg_init_required = 0;
406 m->pg_init_in_progress = 1;
407 init_required = 1;
408 }
409
410 out:
411 spin_unlock_irqrestore(&m->lock, flags);
412
413 if (init_required)
414 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
415
416 if (!must_queue)
417 dispatch_queued_ios(m);
418 }
419
420 /*
421 * An event is triggered whenever a path is taken out of use.
422 * Includes path failure and PG bypass.
423 */
424 static void trigger_event(void *data)
425 {
426 struct multipath *m = (struct multipath *) data;
427
428 dm_table_event(m->ti->table);
429 }
430
431 /*-----------------------------------------------------------------
432 * Constructor/argument parsing:
433 * <#multipath feature args> [<arg>]*
434 * <#hw_handler args> [hw_handler [<arg>]*]
435 * <#priority groups>
436 * <initial priority group>
437 * [<selector> <#selector args> [<arg>]*
438 * <#paths> <#per-path selector args>
439 * [<path> [<arg>]* ]+ ]+
440 *---------------------------------------------------------------*/
441 struct param {
442 unsigned min;
443 unsigned max;
444 char *error;
445 };
446
447 static int read_param(struct param *param, char *str, unsigned *v, char **error)
448 {
449 if (!str ||
450 (sscanf(str, "%u", v) != 1) ||
451 (*v < param->min) ||
452 (*v > param->max)) {
453 *error = param->error;
454 return -EINVAL;
455 }
456
457 return 0;
458 }
459
460 struct arg_set {
461 unsigned argc;
462 char **argv;
463 };
464
465 static char *shift(struct arg_set *as)
466 {
467 char *r;
468
469 if (as->argc) {
470 as->argc--;
471 r = *as->argv;
472 as->argv++;
473 return r;
474 }
475
476 return NULL;
477 }
478
479 static void consume(struct arg_set *as, unsigned n)
480 {
481 BUG_ON (as->argc < n);
482 as->argc -= n;
483 as->argv += n;
484 }
485
486 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
487 struct dm_target *ti)
488 {
489 int r;
490 struct path_selector_type *pst;
491 unsigned ps_argc;
492
493 static struct param _params[] = {
494 {0, 1024, "invalid number of path selector args"},
495 };
496
497 pst = dm_get_path_selector(shift(as));
498 if (!pst) {
499 ti->error = "unknown path selector type";
500 return -EINVAL;
501 }
502
503 r = read_param(_params, shift(as), &ps_argc, &ti->error);
504 if (r)
505 return -EINVAL;
506
507 r = pst->create(&pg->ps, ps_argc, as->argv);
508 if (r) {
509 dm_put_path_selector(pst);
510 ti->error = "path selector constructor failed";
511 return r;
512 }
513
514 pg->ps.type = pst;
515 consume(as, ps_argc);
516
517 return 0;
518 }
519
520 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
521 struct dm_target *ti)
522 {
523 int r;
524 struct pgpath *p;
525
526 /* we need at least a path arg */
527 if (as->argc < 1) {
528 ti->error = "no device given";
529 return NULL;
530 }
531
532 p = alloc_pgpath();
533 if (!p)
534 return NULL;
535
536 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
537 dm_table_get_mode(ti->table), &p->path.dev);
538 if (r) {
539 ti->error = "error getting device";
540 goto bad;
541 }
542
543 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
544 if (r) {
545 dm_put_device(ti, p->path.dev);
546 goto bad;
547 }
548
549 return p;
550
551 bad:
552 free_pgpath(p);
553 return NULL;
554 }
555
556 static struct priority_group *parse_priority_group(struct arg_set *as,
557 struct multipath *m)
558 {
559 static struct param _params[] = {
560 {1, 1024, "invalid number of paths"},
561 {0, 1024, "invalid number of selector args"}
562 };
563
564 int r;
565 unsigned i, nr_selector_args, nr_params;
566 struct priority_group *pg;
567 struct dm_target *ti = m->ti;
568
569 if (as->argc < 2) {
570 as->argc = 0;
571 ti->error = "not enough priority group aruments";
572 return NULL;
573 }
574
575 pg = alloc_priority_group();
576 if (!pg) {
577 ti->error = "couldn't allocate priority group";
578 return NULL;
579 }
580 pg->m = m;
581
582 r = parse_path_selector(as, pg, ti);
583 if (r)
584 goto bad;
585
586 /*
587 * read the paths
588 */
589 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
590 if (r)
591 goto bad;
592
593 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
594 if (r)
595 goto bad;
596
597 nr_params = 1 + nr_selector_args;
598 for (i = 0; i < pg->nr_pgpaths; i++) {
599 struct pgpath *pgpath;
600 struct arg_set path_args;
601
602 if (as->argc < nr_params)
603 goto bad;
604
605 path_args.argc = nr_params;
606 path_args.argv = as->argv;
607
608 pgpath = parse_path(&path_args, &pg->ps, ti);
609 if (!pgpath)
610 goto bad;
611
612 pgpath->pg = pg;
613 list_add_tail(&pgpath->list, &pg->pgpaths);
614 consume(as, nr_params);
615 }
616
617 return pg;
618
619 bad:
620 free_priority_group(pg, ti);
621 return NULL;
622 }
623
624 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
625 {
626 int r;
627 struct hw_handler_type *hwht;
628 unsigned hw_argc;
629 struct dm_target *ti = m->ti;
630
631 static struct param _params[] = {
632 {0, 1024, "invalid number of hardware handler args"},
633 };
634
635 r = read_param(_params, shift(as), &hw_argc, &ti->error);
636 if (r)
637 return -EINVAL;
638
639 if (!hw_argc)
640 return 0;
641
642 hwht = dm_get_hw_handler(shift(as));
643 if (!hwht) {
644 ti->error = "unknown hardware handler type";
645 return -EINVAL;
646 }
647
648 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
649 if (r) {
650 dm_put_hw_handler(hwht);
651 ti->error = "hardware handler constructor failed";
652 return r;
653 }
654
655 m->hw_handler.type = hwht;
656 consume(as, hw_argc - 1);
657
658 return 0;
659 }
660
661 static int parse_features(struct arg_set *as, struct multipath *m)
662 {
663 int r;
664 unsigned argc;
665 struct dm_target *ti = m->ti;
666
667 static struct param _params[] = {
668 {0, 1, "invalid number of feature args"},
669 };
670
671 r = read_param(_params, shift(as), &argc, &ti->error);
672 if (r)
673 return -EINVAL;
674
675 if (!argc)
676 return 0;
677
678 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
679 return queue_if_no_path(m, 1, 0);
680 else {
681 ti->error = "Unrecognised multipath feature request";
682 return -EINVAL;
683 }
684 }
685
686 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
687 char **argv)
688 {
689 /* target parameters */
690 static struct param _params[] = {
691 {1, 1024, "invalid number of priority groups"},
692 {1, 1024, "invalid initial priority group number"},
693 };
694
695 int r;
696 struct multipath *m;
697 struct arg_set as;
698 unsigned pg_count = 0;
699 unsigned next_pg_num;
700
701 as.argc = argc;
702 as.argv = argv;
703
704 m = alloc_multipath(ti);
705 if (!m) {
706 ti->error = "can't allocate multipath";
707 return -EINVAL;
708 }
709
710 r = parse_features(&as, m);
711 if (r)
712 goto bad;
713
714 r = parse_hw_handler(&as, m);
715 if (r)
716 goto bad;
717
718 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
719 if (r)
720 goto bad;
721
722 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
723 if (r)
724 goto bad;
725
726 /* parse the priority groups */
727 while (as.argc) {
728 struct priority_group *pg;
729
730 pg = parse_priority_group(&as, m);
731 if (!pg) {
732 r = -EINVAL;
733 goto bad;
734 }
735
736 m->nr_valid_paths += pg->nr_pgpaths;
737 list_add_tail(&pg->list, &m->priority_groups);
738 pg_count++;
739 pg->pg_num = pg_count;
740 if (!--next_pg_num)
741 m->next_pg = pg;
742 }
743
744 if (pg_count != m->nr_priority_groups) {
745 ti->error = "priority group count mismatch";
746 r = -EINVAL;
747 goto bad;
748 }
749
750 return 0;
751
752 bad:
753 free_multipath(m);
754 return r;
755 }
756
757 static void multipath_dtr(struct dm_target *ti)
758 {
759 struct multipath *m = (struct multipath *) ti->private;
760
761 flush_workqueue(kmultipathd);
762 free_multipath(m);
763 }
764
765 /*
766 * Map bios, recording original fields for later in case we have to resubmit
767 */
768 static int multipath_map(struct dm_target *ti, struct bio *bio,
769 union map_info *map_context)
770 {
771 int r;
772 struct mpath_io *mpio;
773 struct multipath *m = (struct multipath *) ti->private;
774
775 if (bio_barrier(bio))
776 return -EOPNOTSUPP;
777
778 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
779 dm_bio_record(&mpio->details, bio);
780
781 map_context->ptr = mpio;
782 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
783 r = map_io(m, bio, mpio, 0);
784 if (r < 0)
785 mempool_free(mpio, m->mpio_pool);
786
787 return r;
788 }
789
790 /*
791 * Take a path out of use.
792 */
793 static int fail_path(struct pgpath *pgpath)
794 {
795 unsigned long flags;
796 struct multipath *m = pgpath->pg->m;
797
798 spin_lock_irqsave(&m->lock, flags);
799
800 if (!pgpath->path.is_active)
801 goto out;
802
803 DMWARN("Failing path %s.", pgpath->path.dev->name);
804
805 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
806 pgpath->path.is_active = 0;
807 pgpath->fail_count++;
808
809 m->nr_valid_paths--;
810
811 if (pgpath == m->current_pgpath)
812 m->current_pgpath = NULL;
813
814 queue_work(kmultipathd, &m->trigger_event);
815
816 out:
817 spin_unlock_irqrestore(&m->lock, flags);
818
819 return 0;
820 }
821
822 /*
823 * Reinstate a previously-failed path
824 */
825 static int reinstate_path(struct pgpath *pgpath)
826 {
827 int r = 0;
828 unsigned long flags;
829 struct multipath *m = pgpath->pg->m;
830
831 spin_lock_irqsave(&m->lock, flags);
832
833 if (pgpath->path.is_active)
834 goto out;
835
836 if (!pgpath->pg->ps.type) {
837 DMWARN("Reinstate path not supported by path selector %s",
838 pgpath->pg->ps.type->name);
839 r = -EINVAL;
840 goto out;
841 }
842
843 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
844 if (r)
845 goto out;
846
847 pgpath->path.is_active = 1;
848
849 m->current_pgpath = NULL;
850 if (!m->nr_valid_paths++ && m->queue_size)
851 queue_work(kmultipathd, &m->process_queued_ios);
852
853 queue_work(kmultipathd, &m->trigger_event);
854
855 out:
856 spin_unlock_irqrestore(&m->lock, flags);
857
858 return r;
859 }
860
861 /*
862 * Fail or reinstate all paths that match the provided struct dm_dev.
863 */
864 static int action_dev(struct multipath *m, struct dm_dev *dev,
865 action_fn action)
866 {
867 int r = 0;
868 struct pgpath *pgpath;
869 struct priority_group *pg;
870
871 list_for_each_entry(pg, &m->priority_groups, list) {
872 list_for_each_entry(pgpath, &pg->pgpaths, list) {
873 if (pgpath->path.dev == dev)
874 r = action(pgpath);
875 }
876 }
877
878 return r;
879 }
880
881 /*
882 * Temporarily try to avoid having to use the specified PG
883 */
884 static void bypass_pg(struct multipath *m, struct priority_group *pg,
885 int bypassed)
886 {
887 unsigned long flags;
888
889 spin_lock_irqsave(&m->lock, flags);
890
891 pg->bypassed = bypassed;
892 m->current_pgpath = NULL;
893 m->current_pg = NULL;
894
895 spin_unlock_irqrestore(&m->lock, flags);
896
897 queue_work(kmultipathd, &m->trigger_event);
898 }
899
900 /*
901 * Switch to using the specified PG from the next I/O that gets mapped
902 */
903 static int switch_pg_num(struct multipath *m, const char *pgstr)
904 {
905 struct priority_group *pg;
906 unsigned pgnum;
907 unsigned long flags;
908
909 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
910 (pgnum > m->nr_priority_groups)) {
911 DMWARN("invalid PG number supplied to switch_pg_num");
912 return -EINVAL;
913 }
914
915 spin_lock_irqsave(&m->lock, flags);
916 list_for_each_entry(pg, &m->priority_groups, list) {
917 pg->bypassed = 0;
918 if (--pgnum)
919 continue;
920
921 m->current_pgpath = NULL;
922 m->current_pg = NULL;
923 m->next_pg = pg;
924 }
925 spin_unlock_irqrestore(&m->lock, flags);
926
927 queue_work(kmultipathd, &m->trigger_event);
928 return 0;
929 }
930
931 /*
932 * Set/clear bypassed status of a PG.
933 * PGs are numbered upwards from 1 in the order they were declared.
934 */
935 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
936 {
937 struct priority_group *pg;
938 unsigned pgnum;
939
940 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
941 (pgnum > m->nr_priority_groups)) {
942 DMWARN("invalid PG number supplied to bypass_pg");
943 return -EINVAL;
944 }
945
946 list_for_each_entry(pg, &m->priority_groups, list) {
947 if (!--pgnum)
948 break;
949 }
950
951 bypass_pg(m, pg, bypassed);
952 return 0;
953 }
954
955 /*
956 * pg_init must call this when it has completed its initialisation
957 */
958 void dm_pg_init_complete(struct path *path, unsigned err_flags)
959 {
960 struct pgpath *pgpath = path_to_pgpath(path);
961 struct priority_group *pg = pgpath->pg;
962 struct multipath *m = pg->m;
963 unsigned long flags;
964
965 /* We insist on failing the path if the PG is already bypassed. */
966 if (err_flags && pg->bypassed)
967 err_flags |= MP_FAIL_PATH;
968
969 if (err_flags & MP_FAIL_PATH)
970 fail_path(pgpath);
971
972 if (err_flags & MP_BYPASS_PG)
973 bypass_pg(m, pg, 1);
974
975 spin_lock_irqsave(&m->lock, flags);
976 if (err_flags) {
977 m->current_pgpath = NULL;
978 m->current_pg = NULL;
979 } else if (!m->pg_init_required)
980 m->queue_io = 0;
981
982 m->pg_init_in_progress = 0;
983 queue_work(kmultipathd, &m->process_queued_ios);
984 spin_unlock_irqrestore(&m->lock, flags);
985 }
986
987 /*
988 * end_io handling
989 */
990 static int do_end_io(struct multipath *m, struct bio *bio,
991 int error, struct mpath_io *mpio)
992 {
993 struct hw_handler *hwh = &m->hw_handler;
994 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
995 unsigned long flags;
996
997 if (!error)
998 return 0; /* I/O complete */
999
1000 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1001 return error;
1002
1003 if (error == -EOPNOTSUPP)
1004 return error;
1005
1006 spin_lock_irqsave(&m->lock, flags);
1007 if (!m->nr_valid_paths) {
1008 if (!m->queue_if_no_path) {
1009 spin_unlock_irqrestore(&m->lock, flags);
1010 return -EIO;
1011 } else {
1012 spin_unlock_irqrestore(&m->lock, flags);
1013 goto requeue;
1014 }
1015 }
1016 spin_unlock_irqrestore(&m->lock, flags);
1017
1018 if (hwh->type && hwh->type->error)
1019 err_flags = hwh->type->error(hwh, bio);
1020
1021 if (mpio->pgpath) {
1022 if (err_flags & MP_FAIL_PATH)
1023 fail_path(mpio->pgpath);
1024
1025 if (err_flags & MP_BYPASS_PG)
1026 bypass_pg(m, mpio->pgpath->pg, 1);
1027 }
1028
1029 if (err_flags & MP_ERROR_IO)
1030 return -EIO;
1031
1032 requeue:
1033 dm_bio_restore(&mpio->details, bio);
1034
1035 /* queue for the daemon to resubmit or fail */
1036 spin_lock_irqsave(&m->lock, flags);
1037 bio_list_add(&m->queued_ios, bio);
1038 m->queue_size++;
1039 if (!m->queue_io)
1040 queue_work(kmultipathd, &m->process_queued_ios);
1041 spin_unlock_irqrestore(&m->lock, flags);
1042
1043 return 1; /* io not complete */
1044 }
1045
1046 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1047 int error, union map_info *map_context)
1048 {
1049 struct multipath *m = (struct multipath *) ti->private;
1050 struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
1051 struct pgpath *pgpath = mpio->pgpath;
1052 struct path_selector *ps;
1053 int r;
1054
1055 r = do_end_io(m, bio, error, mpio);
1056 if (pgpath) {
1057 ps = &pgpath->pg->ps;
1058 if (ps->type->end_io)
1059 ps->type->end_io(ps, &pgpath->path);
1060 }
1061 if (r <= 0)
1062 mempool_free(mpio, m->mpio_pool);
1063
1064 return r;
1065 }
1066
1067 /*
1068 * Suspend can't complete until all the I/O is processed so if
1069 * the last path fails we must error any remaining I/O.
1070 * Note that if the freeze_bdev fails while suspending, the
1071 * queue_if_no_path state is lost - userspace should reset it.
1072 */
1073 static void multipath_presuspend(struct dm_target *ti)
1074 {
1075 struct multipath *m = (struct multipath *) ti->private;
1076
1077 queue_if_no_path(m, 0, 1);
1078 }
1079
1080 /*
1081 * Restore the queue_if_no_path setting.
1082 */
1083 static void multipath_resume(struct dm_target *ti)
1084 {
1085 struct multipath *m = (struct multipath *) ti->private;
1086 unsigned long flags;
1087
1088 spin_lock_irqsave(&m->lock, flags);
1089 m->queue_if_no_path = m->saved_queue_if_no_path;
1090 spin_unlock_irqrestore(&m->lock, flags);
1091 }
1092
1093 /*
1094 * Info output has the following format:
1095 * num_multipath_feature_args [multipath_feature_args]*
1096 * num_handler_status_args [handler_status_args]*
1097 * num_groups init_group_number
1098 * [A|D|E num_ps_status_args [ps_status_args]*
1099 * num_paths num_selector_args
1100 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1101 *
1102 * Table output has the following format (identical to the constructor string):
1103 * num_feature_args [features_args]*
1104 * num_handler_args hw_handler [hw_handler_args]*
1105 * num_groups init_group_number
1106 * [priority selector-name num_ps_args [ps_args]*
1107 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1108 */
1109 static int multipath_status(struct dm_target *ti, status_type_t type,
1110 char *result, unsigned int maxlen)
1111 {
1112 int sz = 0;
1113 unsigned long flags;
1114 struct multipath *m = (struct multipath *) ti->private;
1115 struct hw_handler *hwh = &m->hw_handler;
1116 struct priority_group *pg;
1117 struct pgpath *p;
1118 unsigned pg_num;
1119 char state;
1120
1121 spin_lock_irqsave(&m->lock, flags);
1122
1123 /* Features */
1124 if (type == STATUSTYPE_INFO)
1125 DMEMIT("1 %u ", m->queue_size);
1126 else if (m->queue_if_no_path)
1127 DMEMIT("1 queue_if_no_path ");
1128 else
1129 DMEMIT("0 ");
1130
1131 if (hwh->type && hwh->type->status)
1132 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1133 else if (!hwh->type || type == STATUSTYPE_INFO)
1134 DMEMIT("0 ");
1135 else
1136 DMEMIT("1 %s ", hwh->type->name);
1137
1138 DMEMIT("%u ", m->nr_priority_groups);
1139
1140 if (m->next_pg)
1141 pg_num = m->next_pg->pg_num;
1142 else if (m->current_pg)
1143 pg_num = m->current_pg->pg_num;
1144 else
1145 pg_num = 1;
1146
1147 DMEMIT("%u ", pg_num);
1148
1149 switch (type) {
1150 case STATUSTYPE_INFO:
1151 list_for_each_entry(pg, &m->priority_groups, list) {
1152 if (pg->bypassed)
1153 state = 'D'; /* Disabled */
1154 else if (pg == m->current_pg)
1155 state = 'A'; /* Currently Active */
1156 else
1157 state = 'E'; /* Enabled */
1158
1159 DMEMIT("%c ", state);
1160
1161 if (pg->ps.type->status)
1162 sz += pg->ps.type->status(&pg->ps, NULL, type,
1163 result + sz,
1164 maxlen - sz);
1165 else
1166 DMEMIT("0 ");
1167
1168 DMEMIT("%u %u ", pg->nr_pgpaths,
1169 pg->ps.type->info_args);
1170
1171 list_for_each_entry(p, &pg->pgpaths, list) {
1172 DMEMIT("%s %s %u ", p->path.dev->name,
1173 p->path.is_active ? "A" : "F",
1174 p->fail_count);
1175 if (pg->ps.type->status)
1176 sz += pg->ps.type->status(&pg->ps,
1177 &p->path, type, result + sz,
1178 maxlen - sz);
1179 }
1180 }
1181 break;
1182
1183 case STATUSTYPE_TABLE:
1184 list_for_each_entry(pg, &m->priority_groups, list) {
1185 DMEMIT("%s ", pg->ps.type->name);
1186
1187 if (pg->ps.type->status)
1188 sz += pg->ps.type->status(&pg->ps, NULL, type,
1189 result + sz,
1190 maxlen - sz);
1191 else
1192 DMEMIT("0 ");
1193
1194 DMEMIT("%u %u ", pg->nr_pgpaths,
1195 pg->ps.type->table_args);
1196
1197 list_for_each_entry(p, &pg->pgpaths, list) {
1198 DMEMIT("%s ", p->path.dev->name);
1199 if (pg->ps.type->status)
1200 sz += pg->ps.type->status(&pg->ps,
1201 &p->path, type, result + sz,
1202 maxlen - sz);
1203 }
1204 }
1205 break;
1206 }
1207
1208 spin_unlock_irqrestore(&m->lock, flags);
1209
1210 return 0;
1211 }
1212
1213 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1214 {
1215 int r;
1216 struct dm_dev *dev;
1217 struct multipath *m = (struct multipath *) ti->private;
1218 action_fn action;
1219
1220 if (argc == 1) {
1221 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1222 return queue_if_no_path(m, 1, 0);
1223 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1224 return queue_if_no_path(m, 0, 0);
1225 }
1226
1227 if (argc != 2)
1228 goto error;
1229
1230 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1231 return bypass_pg_num(m, argv[1], 1);
1232 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1233 return bypass_pg_num(m, argv[1], 0);
1234 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1235 return switch_pg_num(m, argv[1]);
1236 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1237 action = reinstate_path;
1238 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1239 action = fail_path;
1240 else
1241 goto error;
1242
1243 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1244 dm_table_get_mode(ti->table), &dev);
1245 if (r) {
1246 DMWARN("message: error getting device %s",
1247 argv[1]);
1248 return -EINVAL;
1249 }
1250
1251 r = action_dev(m, dev, action);
1252
1253 dm_put_device(ti, dev);
1254
1255 return r;
1256
1257 error:
1258 DMWARN("Unrecognised multipath message received.");
1259 return -EINVAL;
1260 }
1261
1262 static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1263 struct file *filp, unsigned int cmd,
1264 unsigned long arg)
1265 {
1266 struct multipath *m = (struct multipath *) ti->private;
1267 struct block_device *bdev = NULL;
1268 unsigned long flags;
1269 struct file fake_file = {};
1270 struct dentry fake_dentry = {};
1271 int r = 0;
1272
1273 fake_file.f_dentry = &fake_dentry;
1274
1275 spin_lock_irqsave(&m->lock, flags);
1276
1277 if (!m->current_pgpath)
1278 __choose_pgpath(m);
1279
1280 if (m->current_pgpath) {
1281 bdev = m->current_pgpath->path.dev->bdev;
1282 fake_dentry.d_inode = bdev->bd_inode;
1283 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1284 }
1285
1286 if (m->queue_io)
1287 r = -EAGAIN;
1288 else if (!bdev)
1289 r = -EIO;
1290
1291 spin_unlock_irqrestore(&m->lock, flags);
1292
1293 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1294 bdev->bd_disk, cmd, arg);
1295 }
1296
1297 /*-----------------------------------------------------------------
1298 * Module setup
1299 *---------------------------------------------------------------*/
1300 static struct target_type multipath_target = {
1301 .name = "multipath",
1302 .version = {1, 0, 5},
1303 .module = THIS_MODULE,
1304 .ctr = multipath_ctr,
1305 .dtr = multipath_dtr,
1306 .map = multipath_map,
1307 .end_io = multipath_end_io,
1308 .presuspend = multipath_presuspend,
1309 .resume = multipath_resume,
1310 .status = multipath_status,
1311 .message = multipath_message,
1312 .ioctl = multipath_ioctl,
1313 };
1314
1315 static int __init dm_multipath_init(void)
1316 {
1317 int r;
1318
1319 /* allocate a slab for the dm_ios */
1320 _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
1321 0, 0, NULL, NULL);
1322 if (!_mpio_cache)
1323 return -ENOMEM;
1324
1325 r = dm_register_target(&multipath_target);
1326 if (r < 0) {
1327 DMERR("%s: register failed %d", multipath_target.name, r);
1328 kmem_cache_destroy(_mpio_cache);
1329 return -EINVAL;
1330 }
1331
1332 kmultipathd = create_workqueue("kmpathd");
1333 if (!kmultipathd) {
1334 DMERR("%s: failed to create workqueue kmpathd",
1335 multipath_target.name);
1336 dm_unregister_target(&multipath_target);
1337 kmem_cache_destroy(_mpio_cache);
1338 return -ENOMEM;
1339 }
1340
1341 DMINFO("version %u.%u.%u loaded",
1342 multipath_target.version[0], multipath_target.version[1],
1343 multipath_target.version[2]);
1344
1345 return r;
1346 }
1347
1348 static void __exit dm_multipath_exit(void)
1349 {
1350 int r;
1351
1352 destroy_workqueue(kmultipathd);
1353
1354 r = dm_unregister_target(&multipath_target);
1355 if (r < 0)
1356 DMERR("%s: target unregister failed %d",
1357 multipath_target.name, r);
1358 kmem_cache_destroy(_mpio_cache);
1359 }
1360
1361 EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1362
1363 module_init(dm_multipath_init);
1364 module_exit(dm_multipath_exit);
1365
1366 MODULE_DESCRIPTION(DM_NAME " multipath target");
1367 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1368 MODULE_LICENSE("GPL");
This page took 0.056603 seconds and 6 git commands to generate.