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