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