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