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