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