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