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