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