1
2
3
4
5
6
7
8#include <linux/device-mapper.h>
9
10#include "dm-path-selector.h"
11#include "dm-bio-record.h"
12#include "dm-uevent.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 <scsi/scsi_dh.h>
23#include <asm/atomic.h>
24
25#define DM_MSG_PREFIX "multipath"
26#define MESG_STR(x) x, sizeof(x)
27
28
29struct pgpath {
30 struct list_head list;
31
32 struct priority_group *pg;
33 unsigned is_active;
34 unsigned fail_count;
35
36 struct dm_path path;
37 struct work_struct deactivate_path;
38};
39
40#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41
42
43
44
45
46struct priority_group {
47 struct list_head list;
48
49 struct multipath *m;
50 struct path_selector ps;
51
52 unsigned pg_num;
53 unsigned bypassed;
54
55 unsigned nr_pgpaths;
56 struct list_head pgpaths;
57};
58
59
60struct multipath {
61 struct list_head list;
62 struct dm_target *ti;
63
64 spinlock_t lock;
65
66 const char *hw_handler_name;
67 struct work_struct activate_path;
68 struct pgpath *pgpath_to_activate;
69 unsigned nr_priority_groups;
70 struct list_head priority_groups;
71 unsigned pg_init_required;
72 unsigned pg_init_in_progress;
73
74 unsigned nr_valid_paths;
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg;
78 unsigned repeat_count;
79
80 unsigned queue_io;
81 unsigned queue_if_no_path;
82 unsigned saved_queue_if_no_path;
83 unsigned pg_init_retries;
84 unsigned pg_init_count;
85
86 struct work_struct process_queued_ios;
87 struct bio_list queued_ios;
88 unsigned queue_size;
89
90 struct work_struct trigger_event;
91
92
93
94
95
96 mempool_t *mpio_pool;
97};
98
99
100
101
102struct dm_mpath_io {
103 struct pgpath *pgpath;
104 struct dm_bio_details details;
105};
106
107typedef int (*action_fn) (struct pgpath *pgpath);
108
109#define MIN_IOS 256
110
111static struct kmem_cache *_mpio_cache;
112
113static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
114static void process_queued_ios(struct work_struct *work);
115static void trigger_event(struct work_struct *work);
116static void activate_path(struct work_struct *work);
117static void deactivate_path(struct work_struct *work);
118
119
120
121
122
123
124static struct pgpath *alloc_pgpath(void)
125{
126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
127
128 if (pgpath) {
129 pgpath->is_active = 1;
130 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
131 }
132
133 return pgpath;
134}
135
136static void free_pgpath(struct pgpath *pgpath)
137{
138 kfree(pgpath);
139}
140
141static void deactivate_path(struct work_struct *work)
142{
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
145
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
147}
148
149static struct priority_group *alloc_priority_group(void)
150{
151 struct priority_group *pg;
152
153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
154
155 if (pg)
156 INIT_LIST_HEAD(&pg->pgpaths);
157
158 return pg;
159}
160
161static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
162{
163 unsigned long flags;
164 struct pgpath *pgpath, *tmp;
165 struct multipath *m = ti->private;
166
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
171 dm_put_device(ti, pgpath->path.dev);
172 spin_lock_irqsave(&m->lock, flags);
173 if (m->pgpath_to_activate == pgpath)
174 m->pgpath_to_activate = NULL;
175 spin_unlock_irqrestore(&m->lock, flags);
176 free_pgpath(pgpath);
177 }
178}
179
180static void free_priority_group(struct priority_group *pg,
181 struct dm_target *ti)
182{
183 struct path_selector *ps = &pg->ps;
184
185 if (ps->type) {
186 ps->type->destroy(ps);
187 dm_put_path_selector(ps->type);
188 }
189
190 free_pgpaths(&pg->pgpaths, ti);
191 kfree(pg);
192}
193
194static struct multipath *alloc_multipath(struct dm_target *ti)
195{
196 struct multipath *m;
197
198 m = kzalloc(sizeof(*m), GFP_KERNEL);
199 if (m) {
200 INIT_LIST_HEAD(&m->priority_groups);
201 spin_lock_init(&m->lock);
202 m->queue_io = 1;
203 INIT_WORK(&m->process_queued_ios, process_queued_ios);
204 INIT_WORK(&m->trigger_event, trigger_event);
205 INIT_WORK(&m->activate_path, activate_path);
206 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
207 if (!m->mpio_pool) {
208 kfree(m);
209 return NULL;
210 }
211 m->ti = ti;
212 ti->private = m;
213 }
214
215 return m;
216}
217
218static void free_multipath(struct multipath *m)
219{
220 struct priority_group *pg, *tmp;
221
222 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
223 list_del(&pg->list);
224 free_priority_group(pg, m->ti);
225 }
226
227 kfree(m->hw_handler_name);
228 mempool_destroy(m->mpio_pool);
229 kfree(m);
230}
231
232
233
234
235
236
237static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
238{
239 m->current_pg = pgpath->pg;
240
241
242 if (m->hw_handler_name) {
243 m->pg_init_required = 1;
244 m->queue_io = 1;
245 } else {
246 m->pg_init_required = 0;
247 m->queue_io = 0;
248 }
249
250 m->pg_init_count = 0;
251}
252
253static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
254{
255 struct dm_path *path;
256
257 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
258 if (!path)
259 return -ENXIO;
260
261 m->current_pgpath = path_to_pgpath(path);
262
263 if (m->current_pg != pg)
264 __switch_pg(m, m->current_pgpath);
265
266 return 0;
267}
268
269static void __choose_pgpath(struct multipath *m)
270{
271 struct priority_group *pg;
272 unsigned bypassed = 1;
273
274 if (!m->nr_valid_paths)
275 goto failed;
276
277
278 if (m->next_pg) {
279 pg = m->next_pg;
280 m->next_pg = NULL;
281 if (!__choose_path_in_pg(m, pg))
282 return;
283 }
284
285
286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
287 return;
288
289
290
291
292
293
294 do {
295 list_for_each_entry(pg, &m->priority_groups, list) {
296 if (pg->bypassed == bypassed)
297 continue;
298 if (!__choose_path_in_pg(m, pg))
299 return;
300 }
301 } while (bypassed--);
302
303failed:
304 m->current_pgpath = NULL;
305 m->current_pg = NULL;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319static int __must_push_back(struct multipath *m)
320{
321 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
322 dm_noflush_suspending(m->ti));
323}
324
325static int map_io(struct multipath *m, struct bio *bio,
326 struct dm_mpath_io *mpio, unsigned was_queued)
327{
328 int r = DM_MAPIO_REMAPPED;
329 unsigned long flags;
330 struct pgpath *pgpath;
331
332 spin_lock_irqsave(&m->lock, flags);
333
334
335 if (!m->current_pgpath ||
336 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
337 __choose_pgpath(m);
338
339 pgpath = m->current_pgpath;
340
341 if (was_queued)
342 m->queue_size--;
343
344 if ((pgpath && m->queue_io) ||
345 (!pgpath && m->queue_if_no_path)) {
346
347 bio_list_add(&m->queued_ios, bio);
348 m->queue_size++;
349 if ((m->pg_init_required && !m->pg_init_in_progress) ||
350 !m->queue_io)
351 queue_work(kmultipathd, &m->process_queued_ios);
352 pgpath = NULL;
353 r = DM_MAPIO_SUBMITTED;
354 } else if (pgpath)
355 bio->bi_bdev = pgpath->path.dev->bdev;
356 else if (__must_push_back(m))
357 r = DM_MAPIO_REQUEUE;
358 else
359 r = -EIO;
360
361 mpio->pgpath = pgpath;
362
363 spin_unlock_irqrestore(&m->lock, flags);
364
365 return r;
366}
367
368
369
370
371static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
372 unsigned save_old_value)
373{
374 unsigned long flags;
375
376 spin_lock_irqsave(&m->lock, flags);
377
378 if (save_old_value)
379 m->saved_queue_if_no_path = m->queue_if_no_path;
380 else
381 m->saved_queue_if_no_path = queue_if_no_path;
382 m->queue_if_no_path = queue_if_no_path;
383 if (!m->queue_if_no_path && m->queue_size)
384 queue_work(kmultipathd, &m->process_queued_ios);
385
386 spin_unlock_irqrestore(&m->lock, flags);
387
388 return 0;
389}
390
391
392
393
394
395static void dispatch_queued_ios(struct multipath *m)
396{
397 int r;
398 unsigned long flags;
399 struct bio *bio = NULL, *next;
400 struct dm_mpath_io *mpio;
401 union map_info *info;
402
403 spin_lock_irqsave(&m->lock, flags);
404 bio = bio_list_get(&m->queued_ios);
405 spin_unlock_irqrestore(&m->lock, flags);
406
407 while (bio) {
408 next = bio->bi_next;
409 bio->bi_next = NULL;
410
411 info = dm_get_mapinfo(bio);
412 mpio = info->ptr;
413
414 r = map_io(m, bio, mpio, 1);
415 if (r < 0)
416 bio_endio(bio, r);
417 else if (r == DM_MAPIO_REMAPPED)
418 generic_make_request(bio);
419 else if (r == DM_MAPIO_REQUEUE)
420 bio_endio(bio, -EIO);
421
422 bio = next;
423 }
424}
425
426static void process_queued_ios(struct work_struct *work)
427{
428 struct multipath *m =
429 container_of(work, struct multipath, process_queued_ios);
430 struct pgpath *pgpath = NULL;
431 unsigned init_required = 0, must_queue = 1;
432 unsigned long flags;
433
434 spin_lock_irqsave(&m->lock, flags);
435
436 if (!m->queue_size)
437 goto out;
438
439 if (!m->current_pgpath)
440 __choose_pgpath(m);
441
442 pgpath = m->current_pgpath;
443
444 if ((pgpath && !m->queue_io) ||
445 (!pgpath && !m->queue_if_no_path))
446 must_queue = 0;
447
448 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
449 m->pgpath_to_activate = pgpath;
450 m->pg_init_count++;
451 m->pg_init_required = 0;
452 m->pg_init_in_progress = 1;
453 init_required = 1;
454 }
455
456out:
457 spin_unlock_irqrestore(&m->lock, flags);
458
459 if (init_required)
460 queue_work(kmpath_handlerd, &m->activate_path);
461
462 if (!must_queue)
463 dispatch_queued_ios(m);
464}
465
466
467
468
469
470static void trigger_event(struct work_struct *work)
471{
472 struct multipath *m =
473 container_of(work, struct multipath, trigger_event);
474
475 dm_table_event(m->ti->table);
476}
477
478
479
480
481
482
483
484
485
486
487
488struct param {
489 unsigned min;
490 unsigned max;
491 char *error;
492};
493
494static int read_param(struct param *param, char *str, unsigned *v, char **error)
495{
496 if (!str ||
497 (sscanf(str, "%u", v) != 1) ||
498 (*v < param->min) ||
499 (*v > param->max)) {
500 *error = param->error;
501 return -EINVAL;
502 }
503
504 return 0;
505}
506
507struct arg_set {
508 unsigned argc;
509 char **argv;
510};
511
512static char *shift(struct arg_set *as)
513{
514 char *r;
515
516 if (as->argc) {
517 as->argc--;
518 r = *as->argv;
519 as->argv++;
520 return r;
521 }
522
523 return NULL;
524}
525
526static void consume(struct arg_set *as, unsigned n)
527{
528 BUG_ON (as->argc < n);
529 as->argc -= n;
530 as->argv += n;
531}
532
533static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
534 struct dm_target *ti)
535{
536 int r;
537 struct path_selector_type *pst;
538 unsigned ps_argc;
539
540 static struct param _params[] = {
541 {0, 1024, "invalid number of path selector args"},
542 };
543
544 pst = dm_get_path_selector(shift(as));
545 if (!pst) {
546 ti->error = "unknown path selector type";
547 return -EINVAL;
548 }
549
550 r = read_param(_params, shift(as), &ps_argc, &ti->error);
551 if (r) {
552 dm_put_path_selector(pst);
553 return -EINVAL;
554 }
555
556 if (ps_argc > as->argc) {
557 dm_put_path_selector(pst);
558 ti->error = "not enough arguments for path selector";
559 return -EINVAL;
560 }
561
562 r = pst->create(&pg->ps, ps_argc, as->argv);
563 if (r) {
564 dm_put_path_selector(pst);
565 ti->error = "path selector constructor failed";
566 return r;
567 }
568
569 pg->ps.type = pst;
570 consume(as, ps_argc);
571
572 return 0;
573}
574
575static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
576 struct dm_target *ti)
577{
578 int r;
579 struct pgpath *p;
580 struct multipath *m = ti->private;
581
582
583 if (as->argc < 1) {
584 ti->error = "no device given";
585 return ERR_PTR(-EINVAL);
586 }
587
588 p = alloc_pgpath();
589 if (!p)
590 return ERR_PTR(-ENOMEM);
591
592 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
593 dm_table_get_mode(ti->table), &p->path.dev);
594 if (r) {
595 ti->error = "error getting device";
596 goto bad;
597 }
598
599 if (m->hw_handler_name) {
600 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
601 m->hw_handler_name);
602 if (r < 0) {
603 dm_put_device(ti, p->path.dev);
604 goto bad;
605 }
606 }
607
608 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
609 if (r) {
610 dm_put_device(ti, p->path.dev);
611 goto bad;
612 }
613
614 return p;
615
616 bad:
617 free_pgpath(p);
618 return ERR_PTR(r);
619}
620
621static struct priority_group *parse_priority_group(struct arg_set *as,
622 struct multipath *m)
623{
624 static struct param _params[] = {
625 {1, 1024, "invalid number of paths"},
626 {0, 1024, "invalid number of selector args"}
627 };
628
629 int r;
630 unsigned i, nr_selector_args, nr_params;
631 struct priority_group *pg;
632 struct dm_target *ti = m->ti;
633
634 if (as->argc < 2) {
635 as->argc = 0;
636 ti->error = "not enough priority group arguments";
637 return ERR_PTR(-EINVAL);
638 }
639
640 pg = alloc_priority_group();
641 if (!pg) {
642 ti->error = "couldn't allocate priority group";
643 return ERR_PTR(-ENOMEM);
644 }
645 pg->m = m;
646
647 r = parse_path_selector(as, pg, ti);
648 if (r)
649 goto bad;
650
651
652
653
654 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
655 if (r)
656 goto bad;
657
658 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
659 if (r)
660 goto bad;
661
662 nr_params = 1 + nr_selector_args;
663 for (i = 0; i < pg->nr_pgpaths; i++) {
664 struct pgpath *pgpath;
665 struct arg_set path_args;
666
667 if (as->argc < nr_params) {
668 ti->error = "not enough path parameters";
669 goto bad;
670 }
671
672 path_args.argc = nr_params;
673 path_args.argv = as->argv;
674
675 pgpath = parse_path(&path_args, &pg->ps, ti);
676 if (IS_ERR(pgpath)) {
677 r = PTR_ERR(pgpath);
678 goto bad;
679 }
680
681 pgpath->pg = pg;
682 list_add_tail(&pgpath->list, &pg->pgpaths);
683 consume(as, nr_params);
684 }
685
686 return pg;
687
688 bad:
689 free_priority_group(pg, ti);
690 return ERR_PTR(r);
691}
692
693static int parse_hw_handler(struct arg_set *as, struct multipath *m)
694{
695 unsigned hw_argc;
696 struct dm_target *ti = m->ti;
697
698 static struct param _params[] = {
699 {0, 1024, "invalid number of hardware handler args"},
700 };
701
702 if (read_param(_params, shift(as), &hw_argc, &ti->error))
703 return -EINVAL;
704
705 if (!hw_argc)
706 return 0;
707
708 if (hw_argc > as->argc) {
709 ti->error = "not enough arguments for hardware handler";
710 return -EINVAL;
711 }
712
713 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
714 request_module("scsi_dh_%s", m->hw_handler_name);
715 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
716 ti->error = "unknown hardware handler type";
717 kfree(m->hw_handler_name);
718 m->hw_handler_name = NULL;
719 return -EINVAL;
720 }
721
722 if (hw_argc > 1)
723 DMWARN("Ignoring user-specified arguments for "
724 "hardware handler \"%s\"", m->hw_handler_name);
725 consume(as, hw_argc - 1);
726
727 return 0;
728}
729
730static int parse_features(struct arg_set *as, struct multipath *m)
731{
732 int r;
733 unsigned argc;
734 struct dm_target *ti = m->ti;
735 const char *param_name;
736
737 static struct param _params[] = {
738 {0, 3, "invalid number of feature args"},
739 {1, 50, "pg_init_retries must be between 1 and 50"},
740 };
741
742 r = read_param(_params, shift(as), &argc, &ti->error);
743 if (r)
744 return -EINVAL;
745
746 if (!argc)
747 return 0;
748
749 do {
750 param_name = shift(as);
751 argc--;
752
753 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
754 r = queue_if_no_path(m, 1, 0);
755 continue;
756 }
757
758 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
759 (argc >= 1)) {
760 r = read_param(_params + 1, shift(as),
761 &m->pg_init_retries, &ti->error);
762 argc--;
763 continue;
764 }
765
766 ti->error = "Unrecognised multipath feature request";
767 r = -EINVAL;
768 } while (argc && !r);
769
770 return r;
771}
772
773static int multipath_ctr(struct dm_target *ti, unsigned int argc,
774 char **argv)
775{
776
777 static struct param _params[] = {
778 {1, 1024, "invalid number of priority groups"},
779 {1, 1024, "invalid initial priority group number"},
780 };
781
782 int r;
783 struct multipath *m;
784 struct arg_set as;
785 unsigned pg_count = 0;
786 unsigned next_pg_num;
787
788 as.argc = argc;
789 as.argv = argv;
790
791 m = alloc_multipath(ti);
792 if (!m) {
793 ti->error = "can't allocate multipath";
794 return -EINVAL;
795 }
796
797 r = parse_features(&as, m);
798 if (r)
799 goto bad;
800
801 r = parse_hw_handler(&as, m);
802 if (r)
803 goto bad;
804
805 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
806 if (r)
807 goto bad;
808
809 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
810 if (r)
811 goto bad;
812
813
814 while (as.argc) {
815 struct priority_group *pg;
816
817 pg = parse_priority_group(&as, m);
818 if (IS_ERR(pg)) {
819 r = PTR_ERR(pg);
820 goto bad;
821 }
822
823 m->nr_valid_paths += pg->nr_pgpaths;
824 list_add_tail(&pg->list, &m->priority_groups);
825 pg_count++;
826 pg->pg_num = pg_count;
827 if (!--next_pg_num)
828 m->next_pg = pg;
829 }
830
831 if (pg_count != m->nr_priority_groups) {
832 ti->error = "priority group count mismatch";
833 r = -EINVAL;
834 goto bad;
835 }
836
837 return 0;
838
839 bad:
840 free_multipath(m);
841 return r;
842}
843
844static void multipath_dtr(struct dm_target *ti)
845{
846 struct multipath *m = (struct multipath *) ti->private;
847
848 flush_workqueue(kmpath_handlerd);
849 flush_workqueue(kmultipathd);
850 flush_scheduled_work();
851 free_multipath(m);
852}
853
854
855
856
857static int multipath_map(struct dm_target *ti, struct bio *bio,
858 union map_info *map_context)
859{
860 int r;
861 struct dm_mpath_io *mpio;
862 struct multipath *m = (struct multipath *) ti->private;
863
864 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
865 dm_bio_record(&mpio->details, bio);
866
867 map_context->ptr = mpio;
868 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
869 r = map_io(m, bio, mpio, 0);
870 if (r < 0 || r == DM_MAPIO_REQUEUE)
871 mempool_free(mpio, m->mpio_pool);
872
873 return r;
874}
875
876
877
878
879static int fail_path(struct pgpath *pgpath)
880{
881 unsigned long flags;
882 struct multipath *m = pgpath->pg->m;
883
884 spin_lock_irqsave(&m->lock, flags);
885
886 if (!pgpath->is_active)
887 goto out;
888
889 DMWARN("Failing path %s.", pgpath->path.dev->name);
890
891 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
892 pgpath->is_active = 0;
893 pgpath->fail_count++;
894
895 m->nr_valid_paths--;
896
897 if (pgpath == m->current_pgpath)
898 m->current_pgpath = NULL;
899
900 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
901 pgpath->path.dev->name, m->nr_valid_paths);
902
903 schedule_work(&m->trigger_event);
904 queue_work(kmultipathd, &pgpath->deactivate_path);
905
906out:
907 spin_unlock_irqrestore(&m->lock, flags);
908
909 return 0;
910}
911
912
913
914
915static int reinstate_path(struct pgpath *pgpath)
916{
917 int r = 0;
918 unsigned long flags;
919 struct multipath *m = pgpath->pg->m;
920
921 spin_lock_irqsave(&m->lock, flags);
922
923 if (pgpath->is_active)
924 goto out;
925
926 if (!pgpath->pg->ps.type->reinstate_path) {
927 DMWARN("Reinstate path not supported by path selector %s",
928 pgpath->pg->ps.type->name);
929 r = -EINVAL;
930 goto out;
931 }
932
933 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
934 if (r)
935 goto out;
936
937 pgpath->is_active = 1;
938
939 m->current_pgpath = NULL;
940 if (!m->nr_valid_paths++ && m->queue_size)
941 queue_work(kmultipathd, &m->process_queued_ios);
942
943 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
944 pgpath->path.dev->name, m->nr_valid_paths);
945
946 schedule_work(&m->trigger_event);
947
948out:
949 spin_unlock_irqrestore(&m->lock, flags);
950
951 return r;
952}
953
954
955
956
957static int action_dev(struct multipath *m, struct dm_dev *dev,
958 action_fn action)
959{
960 int r = 0;
961 struct pgpath *pgpath;
962 struct priority_group *pg;
963
964 list_for_each_entry(pg, &m->priority_groups, list) {
965 list_for_each_entry(pgpath, &pg->pgpaths, list) {
966 if (pgpath->path.dev == dev)
967 r = action(pgpath);
968 }
969 }
970
971 return r;
972}
973
974
975
976
977static void bypass_pg(struct multipath *m, struct priority_group *pg,
978 int bypassed)
979{
980 unsigned long flags;
981
982 spin_lock_irqsave(&m->lock, flags);
983
984 pg->bypassed = bypassed;
985 m->current_pgpath = NULL;
986 m->current_pg = NULL;
987
988 spin_unlock_irqrestore(&m->lock, flags);
989
990 schedule_work(&m->trigger_event);
991}
992
993
994
995
996static int switch_pg_num(struct multipath *m, const char *pgstr)
997{
998 struct priority_group *pg;
999 unsigned pgnum;
1000 unsigned long flags;
1001
1002 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1003 (pgnum > m->nr_priority_groups)) {
1004 DMWARN("invalid PG number supplied to switch_pg_num");
1005 return -EINVAL;
1006 }
1007
1008 spin_lock_irqsave(&m->lock, flags);
1009 list_for_each_entry(pg, &m->priority_groups, list) {
1010 pg->bypassed = 0;
1011 if (--pgnum)
1012 continue;
1013
1014 m->current_pgpath = NULL;
1015 m->current_pg = NULL;
1016 m->next_pg = pg;
1017 }
1018 spin_unlock_irqrestore(&m->lock, flags);
1019
1020 schedule_work(&m->trigger_event);
1021 return 0;
1022}
1023
1024
1025
1026
1027
1028static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1029{
1030 struct priority_group *pg;
1031 unsigned pgnum;
1032
1033 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1034 (pgnum > m->nr_priority_groups)) {
1035 DMWARN("invalid PG number supplied to bypass_pg");
1036 return -EINVAL;
1037 }
1038
1039 list_for_each_entry(pg, &m->priority_groups, list) {
1040 if (!--pgnum)
1041 break;
1042 }
1043
1044 bypass_pg(m, pg, bypassed);
1045 return 0;
1046}
1047
1048
1049
1050
1051static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1052{
1053 unsigned long flags;
1054 int limit_reached = 0;
1055
1056 spin_lock_irqsave(&m->lock, flags);
1057
1058 if (m->pg_init_count <= m->pg_init_retries)
1059 m->pg_init_required = 1;
1060 else
1061 limit_reached = 1;
1062
1063 spin_unlock_irqrestore(&m->lock, flags);
1064
1065 return limit_reached;
1066}
1067
1068static void pg_init_done(struct dm_path *path, int errors)
1069{
1070 struct pgpath *pgpath = path_to_pgpath(path);
1071 struct priority_group *pg = pgpath->pg;
1072 struct multipath *m = pg->m;
1073 unsigned long flags;
1074
1075
1076 switch (errors) {
1077 case SCSI_DH_OK:
1078 break;
1079 case SCSI_DH_NOSYS:
1080 if (!m->hw_handler_name) {
1081 errors = 0;
1082 break;
1083 }
1084 DMERR("Cannot failover device because scsi_dh_%s was not "
1085 "loaded.", m->hw_handler_name);
1086
1087
1088
1089 fail_path(pgpath);
1090 break;
1091 case SCSI_DH_DEV_TEMP_BUSY:
1092
1093
1094
1095
1096 bypass_pg(m, pg, 1);
1097 break;
1098
1099 case SCSI_DH_RETRY:
1100 case SCSI_DH_IMM_RETRY:
1101 case SCSI_DH_RES_TEMP_UNAVAIL:
1102 if (pg_init_limit_reached(m, pgpath))
1103 fail_path(pgpath);
1104 errors = 0;
1105 break;
1106 default:
1107
1108
1109
1110
1111
1112 fail_path(pgpath);
1113 }
1114
1115 spin_lock_irqsave(&m->lock, flags);
1116 if (errors) {
1117 DMERR("Could not failover device. Error %d.", errors);
1118 m->current_pgpath = NULL;
1119 m->current_pg = NULL;
1120 } else if (!m->pg_init_required) {
1121 m->queue_io = 0;
1122 pg->bypassed = 0;
1123 }
1124
1125 m->pg_init_in_progress = 0;
1126 queue_work(kmultipathd, &m->process_queued_ios);
1127 spin_unlock_irqrestore(&m->lock, flags);
1128}
1129
1130static void activate_path(struct work_struct *work)
1131{
1132 int ret;
1133 struct multipath *m =
1134 container_of(work, struct multipath, activate_path);
1135 struct dm_path *path;
1136 unsigned long flags;
1137
1138 spin_lock_irqsave(&m->lock, flags);
1139 path = &m->pgpath_to_activate->path;
1140 m->pgpath_to_activate = NULL;
1141 spin_unlock_irqrestore(&m->lock, flags);
1142 if (!path)
1143 return;
1144 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1145 pg_init_done(path, ret);
1146}
1147
1148
1149
1150
1151static int do_end_io(struct multipath *m, struct bio *bio,
1152 int error, struct dm_mpath_io *mpio)
1153{
1154 unsigned long flags;
1155
1156 if (!error)
1157 return 0;
1158
1159 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1160 return error;
1161
1162 if (error == -EOPNOTSUPP)
1163 return error;
1164
1165 spin_lock_irqsave(&m->lock, flags);
1166 if (!m->nr_valid_paths) {
1167 if (__must_push_back(m)) {
1168 spin_unlock_irqrestore(&m->lock, flags);
1169 return DM_ENDIO_REQUEUE;
1170 } else if (!m->queue_if_no_path) {
1171 spin_unlock_irqrestore(&m->lock, flags);
1172 return -EIO;
1173 } else {
1174 spin_unlock_irqrestore(&m->lock, flags);
1175 goto requeue;
1176 }
1177 }
1178 spin_unlock_irqrestore(&m->lock, flags);
1179
1180 if (mpio->pgpath)
1181 fail_path(mpio->pgpath);
1182
1183 requeue:
1184 dm_bio_restore(&mpio->details, bio);
1185
1186
1187 spin_lock_irqsave(&m->lock, flags);
1188 bio_list_add(&m->queued_ios, bio);
1189 m->queue_size++;
1190 if (!m->queue_io)
1191 queue_work(kmultipathd, &m->process_queued_ios);
1192 spin_unlock_irqrestore(&m->lock, flags);
1193
1194 return DM_ENDIO_INCOMPLETE;
1195}
1196
1197static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1198 int error, union map_info *map_context)
1199{
1200 struct multipath *m = ti->private;
1201 struct dm_mpath_io *mpio = map_context->ptr;
1202 struct pgpath *pgpath = mpio->pgpath;
1203 struct path_selector *ps;
1204 int r;
1205
1206 r = do_end_io(m, bio, error, mpio);
1207 if (pgpath) {
1208 ps = &pgpath->pg->ps;
1209 if (ps->type->end_io)
1210 ps->type->end_io(ps, &pgpath->path);
1211 }
1212 if (r != DM_ENDIO_INCOMPLETE)
1213 mempool_free(mpio, m->mpio_pool);
1214
1215 return r;
1216}
1217
1218
1219
1220
1221
1222
1223
1224static void multipath_presuspend(struct dm_target *ti)
1225{
1226 struct multipath *m = (struct multipath *) ti->private;
1227
1228 queue_if_no_path(m, 0, 1);
1229}
1230
1231
1232
1233
1234static void multipath_resume(struct dm_target *ti)
1235{
1236 struct multipath *m = (struct multipath *) ti->private;
1237 unsigned long flags;
1238
1239 spin_lock_irqsave(&m->lock, flags);
1240 m->queue_if_no_path = m->saved_queue_if_no_path;
1241 spin_unlock_irqrestore(&m->lock, flags);
1242}
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260static int multipath_status(struct dm_target *ti, status_type_t type,
1261 char *result, unsigned int maxlen)
1262{
1263 int sz = 0;
1264 unsigned long flags;
1265 struct multipath *m = (struct multipath *) ti->private;
1266 struct priority_group *pg;
1267 struct pgpath *p;
1268 unsigned pg_num;
1269 char state;
1270
1271 spin_lock_irqsave(&m->lock, flags);
1272
1273
1274 if (type == STATUSTYPE_INFO)
1275 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1276 else {
1277 DMEMIT("%u ", m->queue_if_no_path +
1278 (m->pg_init_retries > 0) * 2);
1279 if (m->queue_if_no_path)
1280 DMEMIT("queue_if_no_path ");
1281 if (m->pg_init_retries)
1282 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1283 }
1284
1285 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1286 DMEMIT("0 ");
1287 else
1288 DMEMIT("1 %s ", m->hw_handler_name);
1289
1290 DMEMIT("%u ", m->nr_priority_groups);
1291
1292 if (m->next_pg)
1293 pg_num = m->next_pg->pg_num;
1294 else if (m->current_pg)
1295 pg_num = m->current_pg->pg_num;
1296 else
1297 pg_num = 1;
1298
1299 DMEMIT("%u ", pg_num);
1300
1301 switch (type) {
1302 case STATUSTYPE_INFO:
1303 list_for_each_entry(pg, &m->priority_groups, list) {
1304 if (pg->bypassed)
1305 state = 'D';
1306 else if (pg == m->current_pg)
1307 state = 'A';
1308 else
1309 state = 'E';
1310
1311 DMEMIT("%c ", state);
1312
1313 if (pg->ps.type->status)
1314 sz += pg->ps.type->status(&pg->ps, NULL, type,
1315 result + sz,
1316 maxlen - sz);
1317 else
1318 DMEMIT("0 ");
1319
1320 DMEMIT("%u %u ", pg->nr_pgpaths,
1321 pg->ps.type->info_args);
1322
1323 list_for_each_entry(p, &pg->pgpaths, list) {
1324 DMEMIT("%s %s %u ", p->path.dev->name,
1325 p->is_active ? "A" : "F",
1326 p->fail_count);
1327 if (pg->ps.type->status)
1328 sz += pg->ps.type->status(&pg->ps,
1329 &p->path, type, result + sz,
1330 maxlen - sz);
1331 }
1332 }
1333 break;
1334
1335 case STATUSTYPE_TABLE:
1336 list_for_each_entry(pg, &m->priority_groups, list) {
1337 DMEMIT("%s ", pg->ps.type->name);
1338
1339 if (pg->ps.type->status)
1340 sz += pg->ps.type->status(&pg->ps, NULL, type,
1341 result + sz,
1342 maxlen - sz);
1343 else
1344 DMEMIT("0 ");
1345
1346 DMEMIT("%u %u ", pg->nr_pgpaths,
1347 pg->ps.type->table_args);
1348
1349 list_for_each_entry(p, &pg->pgpaths, list) {
1350 DMEMIT("%s ", p->path.dev->name);
1351 if (pg->ps.type->status)
1352 sz += pg->ps.type->status(&pg->ps,
1353 &p->path, type, result + sz,
1354 maxlen - sz);
1355 }
1356 }
1357 break;
1358 }
1359
1360 spin_unlock_irqrestore(&m->lock, flags);
1361
1362 return 0;
1363}
1364
1365static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1366{
1367 int r;
1368 struct dm_dev *dev;
1369 struct multipath *m = (struct multipath *) ti->private;
1370 action_fn action;
1371
1372 if (argc == 1) {
1373 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1374 return queue_if_no_path(m, 1, 0);
1375 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1376 return queue_if_no_path(m, 0, 0);
1377 }
1378
1379 if (argc != 2)
1380 goto error;
1381
1382 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1383 return bypass_pg_num(m, argv[1], 1);
1384 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1385 return bypass_pg_num(m, argv[1], 0);
1386 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1387 return switch_pg_num(m, argv[1]);
1388 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1389 action = reinstate_path;
1390 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1391 action = fail_path;
1392 else
1393 goto error;
1394
1395 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1396 dm_table_get_mode(ti->table), &dev);
1397 if (r) {
1398 DMWARN("message: error getting device %s",
1399 argv[1]);
1400 return -EINVAL;
1401 }
1402
1403 r = action_dev(m, dev, action);
1404
1405 dm_put_device(ti, dev);
1406
1407 return r;
1408
1409error:
1410 DMWARN("Unrecognised multipath message received.");
1411 return -EINVAL;
1412}
1413
1414static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1415 unsigned long arg)
1416{
1417 struct multipath *m = (struct multipath *) ti->private;
1418 struct block_device *bdev = NULL;
1419 fmode_t mode = 0;
1420 unsigned long flags;
1421 int r = 0;
1422
1423 spin_lock_irqsave(&m->lock, flags);
1424
1425 if (!m->current_pgpath)
1426 __choose_pgpath(m);
1427
1428 if (m->current_pgpath) {
1429 bdev = m->current_pgpath->path.dev->bdev;
1430 mode = m->current_pgpath->path.dev->mode;
1431 }
1432
1433 if (m->queue_io)
1434 r = -EAGAIN;
1435 else if (!bdev)
1436 r = -EIO;
1437
1438 spin_unlock_irqrestore(&m->lock, flags);
1439
1440 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1441}
1442
1443
1444
1445
1446static struct target_type multipath_target = {
1447 .name = "multipath",
1448 .version = {1, 0, 5},
1449 .module = THIS_MODULE,
1450 .ctr = multipath_ctr,
1451 .dtr = multipath_dtr,
1452 .map = multipath_map,
1453 .end_io = multipath_end_io,
1454 .presuspend = multipath_presuspend,
1455 .resume = multipath_resume,
1456 .status = multipath_status,
1457 .message = multipath_message,
1458 .ioctl = multipath_ioctl,
1459};
1460
1461static int __init dm_multipath_init(void)
1462{
1463 int r;
1464
1465
1466 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1467 if (!_mpio_cache)
1468 return -ENOMEM;
1469
1470 r = dm_register_target(&multipath_target);
1471 if (r < 0) {
1472 DMERR("register failed %d", r);
1473 kmem_cache_destroy(_mpio_cache);
1474 return -EINVAL;
1475 }
1476
1477 kmultipathd = create_workqueue("kmpathd");
1478 if (!kmultipathd) {
1479 DMERR("failed to create workqueue kmpathd");
1480 dm_unregister_target(&multipath_target);
1481 kmem_cache_destroy(_mpio_cache);
1482 return -ENOMEM;
1483 }
1484
1485
1486
1487
1488
1489
1490
1491 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1492 if (!kmpath_handlerd) {
1493 DMERR("failed to create workqueue kmpath_handlerd");
1494 destroy_workqueue(kmultipathd);
1495 dm_unregister_target(&multipath_target);
1496 kmem_cache_destroy(_mpio_cache);
1497 return -ENOMEM;
1498 }
1499
1500 DMINFO("version %u.%u.%u loaded",
1501 multipath_target.version[0], multipath_target.version[1],
1502 multipath_target.version[2]);
1503
1504 return r;
1505}
1506
1507static void __exit dm_multipath_exit(void)
1508{
1509 destroy_workqueue(kmpath_handlerd);
1510 destroy_workqueue(kmultipathd);
1511
1512 dm_unregister_target(&multipath_target);
1513 kmem_cache_destroy(_mpio_cache);
1514}
1515
1516module_init(dm_multipath_init);
1517module_exit(dm_multipath_exit);
1518
1519MODULE_DESCRIPTION(DM_NAME " multipath target");
1520MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1521MODULE_LICENSE("GPL");
1522