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