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