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