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