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