Fix gcc 4.5.1 miscompiling drivers/char/i8k.c (again)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-mpath.c
blobd43e186114317f501c48d52449e001ee63b60e4e
1 /*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include <linux/device-mapper.h>
10 #include "dm-path-selector.h"
11 #include "dm-uevent.h"
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>
24 #define DM_MSG_PREFIX "multipath"
25 #define MESG_STR(x) x, sizeof(x)
27 /* Path properties */
28 struct pgpath {
29 struct list_head list;
31 struct priority_group *pg; /* Owning PG */
32 unsigned is_active; /* Path status */
33 unsigned fail_count; /* Cumulative failure count */
35 struct dm_path path;
36 struct work_struct activate_path;
39 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
45 struct priority_group {
46 struct list_head list;
48 struct multipath *m; /* Owning multipath instance */
49 struct path_selector ps;
51 unsigned pg_num; /* Reference number */
52 unsigned bypassed; /* Temporarily bypass this PG? */
54 unsigned nr_pgpaths; /* Number of paths in PG */
55 struct list_head pgpaths;
58 /* Multipath context */
59 struct multipath {
60 struct list_head list;
61 struct dm_target *ti;
63 spinlock_t lock;
65 const char *hw_handler_name;
66 char *hw_handler_params;
67 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
70 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
72 unsigned nr_valid_paths; /* Total number of usable paths */
73 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
76 unsigned repeat_count; /* I/Os left before calling PS again */
78 unsigned queue_io; /* Must we queue all I/O? */
79 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
80 unsigned saved_queue_if_no_path;/* Saved state during suspension */
81 unsigned pg_init_retries; /* Number of times to retry pg_init */
82 unsigned pg_init_count; /* Number of times pg_init called */
84 struct work_struct process_queued_ios;
85 struct list_head queued_ios;
86 unsigned queue_size;
88 struct work_struct trigger_event;
91 * We must use a mempool of dm_mpath_io structs so that we
92 * can resubmit bios on error.
94 mempool_t *mpio_pool;
96 struct mutex work_mutex;
98 unsigned suspended; /* Don't create new I/O internally when set. */
102 * Context information attached to each bio we process.
104 struct dm_mpath_io {
105 struct pgpath *pgpath;
106 size_t nr_bytes;
109 typedef int (*action_fn) (struct pgpath *pgpath);
111 #define MIN_IOS 256 /* Mempool size */
113 static struct kmem_cache *_mpio_cache;
115 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
116 static void process_queued_ios(struct work_struct *work);
117 static void trigger_event(struct work_struct *work);
118 static void activate_path(struct work_struct *work);
121 /*-----------------------------------------------
122 * Allocation routines
123 *-----------------------------------------------*/
125 static struct pgpath *alloc_pgpath(void)
127 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
129 if (pgpath) {
130 pgpath->is_active = 1;
131 INIT_WORK(&pgpath->activate_path, activate_path);
134 return pgpath;
137 static void free_pgpath(struct pgpath *pgpath)
139 kfree(pgpath);
142 static struct priority_group *alloc_priority_group(void)
144 struct priority_group *pg;
146 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
148 if (pg)
149 INIT_LIST_HEAD(&pg->pgpaths);
151 return pg;
154 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
156 struct pgpath *pgpath, *tmp;
157 struct multipath *m = ti->private;
159 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
160 list_del(&pgpath->list);
161 if (m->hw_handler_name)
162 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
163 dm_put_device(ti, pgpath->path.dev);
164 free_pgpath(pgpath);
168 static void free_priority_group(struct priority_group *pg,
169 struct dm_target *ti)
171 struct path_selector *ps = &pg->ps;
173 if (ps->type) {
174 ps->type->destroy(ps);
175 dm_put_path_selector(ps->type);
178 free_pgpaths(&pg->pgpaths, ti);
179 kfree(pg);
182 static struct multipath *alloc_multipath(struct dm_target *ti)
184 struct multipath *m;
186 m = kzalloc(sizeof(*m), GFP_KERNEL);
187 if (m) {
188 INIT_LIST_HEAD(&m->priority_groups);
189 INIT_LIST_HEAD(&m->queued_ios);
190 spin_lock_init(&m->lock);
191 m->queue_io = 1;
192 INIT_WORK(&m->process_queued_ios, process_queued_ios);
193 INIT_WORK(&m->trigger_event, trigger_event);
194 mutex_init(&m->work_mutex);
195 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
196 if (!m->mpio_pool) {
197 kfree(m);
198 return NULL;
200 m->ti = ti;
201 ti->private = m;
204 return m;
207 static void free_multipath(struct multipath *m)
209 struct priority_group *pg, *tmp;
211 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
212 list_del(&pg->list);
213 free_priority_group(pg, m->ti);
216 kfree(m->hw_handler_name);
217 kfree(m->hw_handler_params);
218 mempool_destroy(m->mpio_pool);
219 kfree(m);
223 /*-----------------------------------------------
224 * Path selection
225 *-----------------------------------------------*/
227 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
229 m->current_pg = pgpath->pg;
231 /* Must we initialise the PG first, and queue I/O till it's ready? */
232 if (m->hw_handler_name) {
233 m->pg_init_required = 1;
234 m->queue_io = 1;
235 } else {
236 m->pg_init_required = 0;
237 m->queue_io = 0;
240 m->pg_init_count = 0;
243 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
244 size_t nr_bytes)
246 struct dm_path *path;
248 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
249 if (!path)
250 return -ENXIO;
252 m->current_pgpath = path_to_pgpath(path);
254 if (m->current_pg != pg)
255 __switch_pg(m, m->current_pgpath);
257 return 0;
260 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
262 struct priority_group *pg;
263 unsigned bypassed = 1;
265 if (!m->nr_valid_paths)
266 goto failed;
268 /* Were we instructed to switch PG? */
269 if (m->next_pg) {
270 pg = m->next_pg;
271 m->next_pg = NULL;
272 if (!__choose_path_in_pg(m, pg, nr_bytes))
273 return;
276 /* Don't change PG until it has no remaining paths */
277 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
278 return;
281 * Loop through priority groups until we find a valid path.
282 * First time we skip PGs marked 'bypassed'.
283 * Second time we only try the ones we skipped.
285 do {
286 list_for_each_entry(pg, &m->priority_groups, list) {
287 if (pg->bypassed == bypassed)
288 continue;
289 if (!__choose_path_in_pg(m, pg, nr_bytes))
290 return;
292 } while (bypassed--);
294 failed:
295 m->current_pgpath = NULL;
296 m->current_pg = NULL;
300 * Check whether bios must be queued in the device-mapper core rather
301 * than here in the target.
303 * m->lock must be held on entry.
305 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
306 * same value then we are not between multipath_presuspend()
307 * and multipath_resume() calls and we have no need to check
308 * for the DMF_NOFLUSH_SUSPENDING flag.
310 static int __must_push_back(struct multipath *m)
312 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
313 dm_noflush_suspending(m->ti));
316 static int map_io(struct multipath *m, struct request *clone,
317 struct dm_mpath_io *mpio, unsigned was_queued)
319 int r = DM_MAPIO_REMAPPED;
320 size_t nr_bytes = blk_rq_bytes(clone);
321 unsigned long flags;
322 struct pgpath *pgpath;
323 struct block_device *bdev;
325 spin_lock_irqsave(&m->lock, flags);
327 /* Do we need to select a new pgpath? */
328 if (!m->current_pgpath ||
329 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
330 __choose_pgpath(m, nr_bytes);
332 pgpath = m->current_pgpath;
334 if (was_queued)
335 m->queue_size--;
337 if ((pgpath && m->queue_io) ||
338 (!pgpath && m->queue_if_no_path)) {
339 /* Queue for the daemon to resubmit */
340 list_add_tail(&clone->queuelist, &m->queued_ios);
341 m->queue_size++;
342 if ((m->pg_init_required && !m->pg_init_in_progress) ||
343 !m->queue_io)
344 queue_work(kmultipathd, &m->process_queued_ios);
345 pgpath = NULL;
346 r = DM_MAPIO_SUBMITTED;
347 } else if (pgpath) {
348 bdev = pgpath->path.dev->bdev;
349 clone->q = bdev_get_queue(bdev);
350 clone->rq_disk = bdev->bd_disk;
351 } else if (__must_push_back(m))
352 r = DM_MAPIO_REQUEUE;
353 else
354 r = -EIO; /* Failed */
356 mpio->pgpath = pgpath;
357 mpio->nr_bytes = nr_bytes;
359 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
360 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
361 nr_bytes);
363 spin_unlock_irqrestore(&m->lock, flags);
365 return r;
369 * If we run out of usable paths, should we queue I/O or error it?
371 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
372 unsigned save_old_value)
374 unsigned long flags;
376 spin_lock_irqsave(&m->lock, flags);
378 if (save_old_value)
379 m->saved_queue_if_no_path = m->queue_if_no_path;
380 else
381 m->saved_queue_if_no_path = queue_if_no_path;
382 m->queue_if_no_path = queue_if_no_path;
383 if (!m->queue_if_no_path && m->queue_size)
384 queue_work(kmultipathd, &m->process_queued_ios);
386 spin_unlock_irqrestore(&m->lock, flags);
388 return 0;
391 /*-----------------------------------------------------------------
392 * The multipath daemon is responsible for resubmitting queued ios.
393 *---------------------------------------------------------------*/
395 static void dispatch_queued_ios(struct multipath *m)
397 int r;
398 unsigned long flags;
399 struct dm_mpath_io *mpio;
400 union map_info *info;
401 struct request *clone, *n;
402 LIST_HEAD(cl);
404 spin_lock_irqsave(&m->lock, flags);
405 list_splice_init(&m->queued_ios, &cl);
406 spin_unlock_irqrestore(&m->lock, flags);
408 list_for_each_entry_safe(clone, n, &cl, queuelist) {
409 list_del_init(&clone->queuelist);
411 info = dm_get_rq_mapinfo(clone);
412 mpio = info->ptr;
414 r = map_io(m, clone, mpio, 1);
415 if (r < 0) {
416 mempool_free(mpio, m->mpio_pool);
417 dm_kill_unmapped_request(clone, r);
418 } else if (r == DM_MAPIO_REMAPPED)
419 dm_dispatch_request(clone);
420 else if (r == DM_MAPIO_REQUEUE) {
421 mempool_free(mpio, m->mpio_pool);
422 dm_requeue_unmapped_request(clone);
427 static void process_queued_ios(struct work_struct *work)
429 struct multipath *m =
430 container_of(work, struct multipath, process_queued_ios);
431 struct pgpath *pgpath = NULL, *tmp;
432 unsigned must_queue = 1;
433 unsigned long flags;
435 spin_lock_irqsave(&m->lock, flags);
437 if (!m->queue_size)
438 goto out;
440 if (!m->current_pgpath)
441 __choose_pgpath(m, 0);
443 pgpath = m->current_pgpath;
445 if ((pgpath && !m->queue_io) ||
446 (!pgpath && !m->queue_if_no_path))
447 must_queue = 0;
449 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
450 m->pg_init_count++;
451 m->pg_init_required = 0;
452 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
453 if (queue_work(kmpath_handlerd, &tmp->activate_path))
454 m->pg_init_in_progress++;
457 out:
458 spin_unlock_irqrestore(&m->lock, flags);
459 if (!must_queue)
460 dispatch_queued_ios(m);
464 * An event is triggered whenever a path is taken out of use.
465 * Includes path failure and PG bypass.
467 static void trigger_event(struct work_struct *work)
469 struct multipath *m =
470 container_of(work, struct multipath, trigger_event);
472 dm_table_event(m->ti->table);
475 /*-----------------------------------------------------------------
476 * Constructor/argument parsing:
477 * <#multipath feature args> [<arg>]*
478 * <#hw_handler args> [hw_handler [<arg>]*]
479 * <#priority groups>
480 * <initial priority group>
481 * [<selector> <#selector args> [<arg>]*
482 * <#paths> <#per-path selector args>
483 * [<path> [<arg>]* ]+ ]+
484 *---------------------------------------------------------------*/
485 struct param {
486 unsigned min;
487 unsigned max;
488 char *error;
491 static int read_param(struct param *param, char *str, unsigned *v, char **error)
493 if (!str ||
494 (sscanf(str, "%u", v) != 1) ||
495 (*v < param->min) ||
496 (*v > param->max)) {
497 *error = param->error;
498 return -EINVAL;
501 return 0;
504 struct arg_set {
505 unsigned argc;
506 char **argv;
509 static char *shift(struct arg_set *as)
511 char *r;
513 if (as->argc) {
514 as->argc--;
515 r = *as->argv;
516 as->argv++;
517 return r;
520 return NULL;
523 static void consume(struct arg_set *as, unsigned n)
525 BUG_ON (as->argc < n);
526 as->argc -= n;
527 as->argv += n;
530 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
531 struct dm_target *ti)
533 int r;
534 struct path_selector_type *pst;
535 unsigned ps_argc;
537 static struct param _params[] = {
538 {0, 1024, "invalid number of path selector args"},
541 pst = dm_get_path_selector(shift(as));
542 if (!pst) {
543 ti->error = "unknown path selector type";
544 return -EINVAL;
547 r = read_param(_params, shift(as), &ps_argc, &ti->error);
548 if (r) {
549 dm_put_path_selector(pst);
550 return -EINVAL;
553 if (ps_argc > as->argc) {
554 dm_put_path_selector(pst);
555 ti->error = "not enough arguments for path selector";
556 return -EINVAL;
559 r = pst->create(&pg->ps, ps_argc, as->argv);
560 if (r) {
561 dm_put_path_selector(pst);
562 ti->error = "path selector constructor failed";
563 return r;
566 pg->ps.type = pst;
567 consume(as, ps_argc);
569 return 0;
572 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
573 struct dm_target *ti)
575 int r;
576 struct pgpath *p;
577 struct multipath *m = ti->private;
579 /* we need at least a path arg */
580 if (as->argc < 1) {
581 ti->error = "no device given";
582 return ERR_PTR(-EINVAL);
585 p = alloc_pgpath();
586 if (!p)
587 return ERR_PTR(-ENOMEM);
589 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
590 dm_table_get_mode(ti->table), &p->path.dev);
591 if (r) {
592 ti->error = "error getting device";
593 goto bad;
596 if (m->hw_handler_name) {
597 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
599 r = scsi_dh_attach(q, m->hw_handler_name);
600 if (r == -EBUSY) {
602 * Already attached to different hw_handler,
603 * try to reattach with correct one.
605 scsi_dh_detach(q);
606 r = scsi_dh_attach(q, m->hw_handler_name);
609 if (r < 0) {
610 ti->error = "error attaching hardware handler";
611 dm_put_device(ti, p->path.dev);
612 goto bad;
615 if (m->hw_handler_params) {
616 r = scsi_dh_set_params(q, m->hw_handler_params);
617 if (r < 0) {
618 ti->error = "unable to set hardware "
619 "handler parameters";
620 scsi_dh_detach(q);
621 dm_put_device(ti, p->path.dev);
622 goto bad;
627 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
628 if (r) {
629 dm_put_device(ti, p->path.dev);
630 goto bad;
633 return p;
635 bad:
636 free_pgpath(p);
637 return ERR_PTR(r);
640 static struct priority_group *parse_priority_group(struct arg_set *as,
641 struct multipath *m)
643 static struct param _params[] = {
644 {1, 1024, "invalid number of paths"},
645 {0, 1024, "invalid number of selector args"}
648 int r;
649 unsigned i, nr_selector_args, nr_params;
650 struct priority_group *pg;
651 struct dm_target *ti = m->ti;
653 if (as->argc < 2) {
654 as->argc = 0;
655 ti->error = "not enough priority group arguments";
656 return ERR_PTR(-EINVAL);
659 pg = alloc_priority_group();
660 if (!pg) {
661 ti->error = "couldn't allocate priority group";
662 return ERR_PTR(-ENOMEM);
664 pg->m = m;
666 r = parse_path_selector(as, pg, ti);
667 if (r)
668 goto bad;
671 * read the paths
673 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
674 if (r)
675 goto bad;
677 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
678 if (r)
679 goto bad;
681 nr_params = 1 + nr_selector_args;
682 for (i = 0; i < pg->nr_pgpaths; i++) {
683 struct pgpath *pgpath;
684 struct arg_set path_args;
686 if (as->argc < nr_params) {
687 ti->error = "not enough path parameters";
688 goto bad;
691 path_args.argc = nr_params;
692 path_args.argv = as->argv;
694 pgpath = parse_path(&path_args, &pg->ps, ti);
695 if (IS_ERR(pgpath)) {
696 r = PTR_ERR(pgpath);
697 goto bad;
700 pgpath->pg = pg;
701 list_add_tail(&pgpath->list, &pg->pgpaths);
702 consume(as, nr_params);
705 return pg;
707 bad:
708 free_priority_group(pg, ti);
709 return ERR_PTR(r);
712 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
714 unsigned hw_argc;
715 int ret;
716 struct dm_target *ti = m->ti;
718 static struct param _params[] = {
719 {0, 1024, "invalid number of hardware handler args"},
722 if (read_param(_params, shift(as), &hw_argc, &ti->error))
723 return -EINVAL;
725 if (!hw_argc)
726 return 0;
728 if (hw_argc > as->argc) {
729 ti->error = "not enough arguments for hardware handler";
730 return -EINVAL;
733 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
734 request_module("scsi_dh_%s", m->hw_handler_name);
735 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
736 ti->error = "unknown hardware handler type";
737 ret = -EINVAL;
738 goto fail;
741 if (hw_argc > 1) {
742 char *p;
743 int i, j, len = 4;
745 for (i = 0; i <= hw_argc - 2; i++)
746 len += strlen(as->argv[i]) + 1;
747 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
748 if (!p) {
749 ti->error = "memory allocation failed";
750 ret = -ENOMEM;
751 goto fail;
753 j = sprintf(p, "%d", hw_argc - 1);
754 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
755 j = sprintf(p, "%s", as->argv[i]);
757 consume(as, hw_argc - 1);
759 return 0;
760 fail:
761 kfree(m->hw_handler_name);
762 m->hw_handler_name = NULL;
763 return ret;
766 static int parse_features(struct arg_set *as, struct multipath *m)
768 int r;
769 unsigned argc;
770 struct dm_target *ti = m->ti;
771 const char *param_name;
773 static struct param _params[] = {
774 {0, 3, "invalid number of feature args"},
775 {1, 50, "pg_init_retries must be between 1 and 50"},
778 r = read_param(_params, shift(as), &argc, &ti->error);
779 if (r)
780 return -EINVAL;
782 if (!argc)
783 return 0;
785 do {
786 param_name = shift(as);
787 argc--;
789 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
790 r = queue_if_no_path(m, 1, 0);
791 continue;
794 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
795 (argc >= 1)) {
796 r = read_param(_params + 1, shift(as),
797 &m->pg_init_retries, &ti->error);
798 argc--;
799 continue;
802 ti->error = "Unrecognised multipath feature request";
803 r = -EINVAL;
804 } while (argc && !r);
806 return r;
809 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
810 char **argv)
812 /* target parameters */
813 static struct param _params[] = {
814 {1, 1024, "invalid number of priority groups"},
815 {1, 1024, "invalid initial priority group number"},
818 int r;
819 struct multipath *m;
820 struct arg_set as;
821 unsigned pg_count = 0;
822 unsigned next_pg_num;
824 as.argc = argc;
825 as.argv = argv;
827 m = alloc_multipath(ti);
828 if (!m) {
829 ti->error = "can't allocate multipath";
830 return -EINVAL;
833 r = parse_features(&as, m);
834 if (r)
835 goto bad;
837 r = parse_hw_handler(&as, m);
838 if (r)
839 goto bad;
841 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
842 if (r)
843 goto bad;
845 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
846 if (r)
847 goto bad;
849 /* parse the priority groups */
850 while (as.argc) {
851 struct priority_group *pg;
853 pg = parse_priority_group(&as, m);
854 if (IS_ERR(pg)) {
855 r = PTR_ERR(pg);
856 goto bad;
859 m->nr_valid_paths += pg->nr_pgpaths;
860 list_add_tail(&pg->list, &m->priority_groups);
861 pg_count++;
862 pg->pg_num = pg_count;
863 if (!--next_pg_num)
864 m->next_pg = pg;
867 if (pg_count != m->nr_priority_groups) {
868 ti->error = "priority group count mismatch";
869 r = -EINVAL;
870 goto bad;
873 ti->num_flush_requests = 1;
875 return 0;
877 bad:
878 free_multipath(m);
879 return r;
882 static void flush_multipath_work(void)
884 flush_workqueue(kmpath_handlerd);
885 flush_workqueue(kmultipathd);
886 flush_scheduled_work();
889 static void multipath_dtr(struct dm_target *ti)
891 struct multipath *m = ti->private;
893 flush_multipath_work();
894 free_multipath(m);
898 * Map cloned requests
900 static int multipath_map(struct dm_target *ti, struct request *clone,
901 union map_info *map_context)
903 int r;
904 struct dm_mpath_io *mpio;
905 struct multipath *m = (struct multipath *) ti->private;
907 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
908 if (!mpio)
909 /* ENOMEM, requeue */
910 return DM_MAPIO_REQUEUE;
911 memset(mpio, 0, sizeof(*mpio));
913 map_context->ptr = mpio;
914 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
915 r = map_io(m, clone, mpio, 0);
916 if (r < 0 || r == DM_MAPIO_REQUEUE)
917 mempool_free(mpio, m->mpio_pool);
919 return r;
923 * Take a path out of use.
925 static int fail_path(struct pgpath *pgpath)
927 unsigned long flags;
928 struct multipath *m = pgpath->pg->m;
930 spin_lock_irqsave(&m->lock, flags);
932 if (!pgpath->is_active)
933 goto out;
935 DMWARN("Failing path %s.", pgpath->path.dev->name);
937 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
938 pgpath->is_active = 0;
939 pgpath->fail_count++;
941 m->nr_valid_paths--;
943 if (pgpath == m->current_pgpath)
944 m->current_pgpath = NULL;
946 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
947 pgpath->path.dev->name, m->nr_valid_paths);
949 schedule_work(&m->trigger_event);
951 out:
952 spin_unlock_irqrestore(&m->lock, flags);
954 return 0;
958 * Reinstate a previously-failed path
960 static int reinstate_path(struct pgpath *pgpath)
962 int r = 0;
963 unsigned long flags;
964 struct multipath *m = pgpath->pg->m;
966 spin_lock_irqsave(&m->lock, flags);
968 if (pgpath->is_active)
969 goto out;
971 if (!pgpath->pg->ps.type->reinstate_path) {
972 DMWARN("Reinstate path not supported by path selector %s",
973 pgpath->pg->ps.type->name);
974 r = -EINVAL;
975 goto out;
978 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
979 if (r)
980 goto out;
982 pgpath->is_active = 1;
984 if (!m->nr_valid_paths++ && m->queue_size) {
985 m->current_pgpath = NULL;
986 queue_work(kmultipathd, &m->process_queued_ios);
987 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
988 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
989 m->pg_init_in_progress++;
992 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
993 pgpath->path.dev->name, m->nr_valid_paths);
995 schedule_work(&m->trigger_event);
997 out:
998 spin_unlock_irqrestore(&m->lock, flags);
1000 return r;
1004 * Fail or reinstate all paths that match the provided struct dm_dev.
1006 static int action_dev(struct multipath *m, struct dm_dev *dev,
1007 action_fn action)
1009 int r = 0;
1010 struct pgpath *pgpath;
1011 struct priority_group *pg;
1013 list_for_each_entry(pg, &m->priority_groups, list) {
1014 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1015 if (pgpath->path.dev == dev)
1016 r = action(pgpath);
1020 return r;
1024 * Temporarily try to avoid having to use the specified PG
1026 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1027 int bypassed)
1029 unsigned long flags;
1031 spin_lock_irqsave(&m->lock, flags);
1033 pg->bypassed = bypassed;
1034 m->current_pgpath = NULL;
1035 m->current_pg = NULL;
1037 spin_unlock_irqrestore(&m->lock, flags);
1039 schedule_work(&m->trigger_event);
1043 * Switch to using the specified PG from the next I/O that gets mapped
1045 static int switch_pg_num(struct multipath *m, const char *pgstr)
1047 struct priority_group *pg;
1048 unsigned pgnum;
1049 unsigned long flags;
1051 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1052 (pgnum > m->nr_priority_groups)) {
1053 DMWARN("invalid PG number supplied to switch_pg_num");
1054 return -EINVAL;
1057 spin_lock_irqsave(&m->lock, flags);
1058 list_for_each_entry(pg, &m->priority_groups, list) {
1059 pg->bypassed = 0;
1060 if (--pgnum)
1061 continue;
1063 m->current_pgpath = NULL;
1064 m->current_pg = NULL;
1065 m->next_pg = pg;
1067 spin_unlock_irqrestore(&m->lock, flags);
1069 schedule_work(&m->trigger_event);
1070 return 0;
1074 * Set/clear bypassed status of a PG.
1075 * PGs are numbered upwards from 1 in the order they were declared.
1077 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1079 struct priority_group *pg;
1080 unsigned pgnum;
1082 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1083 (pgnum > m->nr_priority_groups)) {
1084 DMWARN("invalid PG number supplied to bypass_pg");
1085 return -EINVAL;
1088 list_for_each_entry(pg, &m->priority_groups, list) {
1089 if (!--pgnum)
1090 break;
1093 bypass_pg(m, pg, bypassed);
1094 return 0;
1098 * Should we retry pg_init immediately?
1100 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1102 unsigned long flags;
1103 int limit_reached = 0;
1105 spin_lock_irqsave(&m->lock, flags);
1107 if (m->pg_init_count <= m->pg_init_retries)
1108 m->pg_init_required = 1;
1109 else
1110 limit_reached = 1;
1112 spin_unlock_irqrestore(&m->lock, flags);
1114 return limit_reached;
1117 static void pg_init_done(void *data, int errors)
1119 struct dm_path *path = data;
1120 struct pgpath *pgpath = path_to_pgpath(path);
1121 struct priority_group *pg = pgpath->pg;
1122 struct multipath *m = pg->m;
1123 unsigned long flags;
1125 /* device or driver problems */
1126 switch (errors) {
1127 case SCSI_DH_OK:
1128 break;
1129 case SCSI_DH_NOSYS:
1130 if (!m->hw_handler_name) {
1131 errors = 0;
1132 break;
1134 DMERR("Cannot failover device because scsi_dh_%s was not "
1135 "loaded.", m->hw_handler_name);
1137 * Fail path for now, so we do not ping pong
1139 fail_path(pgpath);
1140 break;
1141 case SCSI_DH_DEV_TEMP_BUSY:
1143 * Probably doing something like FW upgrade on the
1144 * controller so try the other pg.
1146 bypass_pg(m, pg, 1);
1147 break;
1148 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1149 case SCSI_DH_RETRY:
1150 case SCSI_DH_IMM_RETRY:
1151 case SCSI_DH_RES_TEMP_UNAVAIL:
1152 if (pg_init_limit_reached(m, pgpath))
1153 fail_path(pgpath);
1154 errors = 0;
1155 break;
1156 default:
1158 * We probably do not want to fail the path for a device
1159 * error, but this is what the old dm did. In future
1160 * patches we can do more advanced handling.
1162 fail_path(pgpath);
1165 spin_lock_irqsave(&m->lock, flags);
1166 if (errors) {
1167 if (pgpath == m->current_pgpath) {
1168 DMERR("Could not failover device. Error %d.", errors);
1169 m->current_pgpath = NULL;
1170 m->current_pg = NULL;
1172 } else if (!m->pg_init_required) {
1173 m->queue_io = 0;
1174 pg->bypassed = 0;
1177 m->pg_init_in_progress--;
1178 if (!m->pg_init_in_progress)
1179 queue_work(kmultipathd, &m->process_queued_ios);
1180 spin_unlock_irqrestore(&m->lock, flags);
1183 static void activate_path(struct work_struct *work)
1185 struct pgpath *pgpath =
1186 container_of(work, struct pgpath, activate_path);
1188 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1189 pg_init_done, &pgpath->path);
1193 * end_io handling
1195 static int do_end_io(struct multipath *m, struct request *clone,
1196 int error, struct dm_mpath_io *mpio)
1199 * We don't queue any clone request inside the multipath target
1200 * during end I/O handling, since those clone requests don't have
1201 * bio clones. If we queue them inside the multipath target,
1202 * we need to make bio clones, that requires memory allocation.
1203 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1204 * don't have bio clones.)
1205 * Instead of queueing the clone request here, we queue the original
1206 * request into dm core, which will remake a clone request and
1207 * clone bios for it and resubmit it later.
1209 int r = DM_ENDIO_REQUEUE;
1210 unsigned long flags;
1212 if (!error && !clone->errors)
1213 return 0; /* I/O complete */
1215 if (error == -EOPNOTSUPP)
1216 return error;
1218 if (mpio->pgpath)
1219 fail_path(mpio->pgpath);
1221 spin_lock_irqsave(&m->lock, flags);
1222 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1223 r = -EIO;
1224 spin_unlock_irqrestore(&m->lock, flags);
1226 return r;
1229 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1230 int error, union map_info *map_context)
1232 struct multipath *m = ti->private;
1233 struct dm_mpath_io *mpio = map_context->ptr;
1234 struct pgpath *pgpath = mpio->pgpath;
1235 struct path_selector *ps;
1236 int r;
1238 r = do_end_io(m, clone, error, mpio);
1239 if (pgpath) {
1240 ps = &pgpath->pg->ps;
1241 if (ps->type->end_io)
1242 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1244 mempool_free(mpio, m->mpio_pool);
1246 return r;
1250 * Suspend can't complete until all the I/O is processed so if
1251 * the last path fails we must error any remaining I/O.
1252 * Note that if the freeze_bdev fails while suspending, the
1253 * queue_if_no_path state is lost - userspace should reset it.
1255 static void multipath_presuspend(struct dm_target *ti)
1257 struct multipath *m = (struct multipath *) ti->private;
1259 queue_if_no_path(m, 0, 1);
1262 static void multipath_postsuspend(struct dm_target *ti)
1264 struct multipath *m = ti->private;
1266 mutex_lock(&m->work_mutex);
1267 m->suspended = 1;
1268 flush_multipath_work();
1269 mutex_unlock(&m->work_mutex);
1273 * Restore the queue_if_no_path setting.
1275 static void multipath_resume(struct dm_target *ti)
1277 struct multipath *m = (struct multipath *) ti->private;
1278 unsigned long flags;
1280 mutex_lock(&m->work_mutex);
1281 m->suspended = 0;
1282 mutex_unlock(&m->work_mutex);
1284 spin_lock_irqsave(&m->lock, flags);
1285 m->queue_if_no_path = m->saved_queue_if_no_path;
1286 spin_unlock_irqrestore(&m->lock, flags);
1290 * Info output has the following format:
1291 * num_multipath_feature_args [multipath_feature_args]*
1292 * num_handler_status_args [handler_status_args]*
1293 * num_groups init_group_number
1294 * [A|D|E num_ps_status_args [ps_status_args]*
1295 * num_paths num_selector_args
1296 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1298 * Table output has the following format (identical to the constructor string):
1299 * num_feature_args [features_args]*
1300 * num_handler_args hw_handler [hw_handler_args]*
1301 * num_groups init_group_number
1302 * [priority selector-name num_ps_args [ps_args]*
1303 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1305 static int multipath_status(struct dm_target *ti, status_type_t type,
1306 char *result, unsigned int maxlen)
1308 int sz = 0;
1309 unsigned long flags;
1310 struct multipath *m = (struct multipath *) ti->private;
1311 struct priority_group *pg;
1312 struct pgpath *p;
1313 unsigned pg_num;
1314 char state;
1316 spin_lock_irqsave(&m->lock, flags);
1318 /* Features */
1319 if (type == STATUSTYPE_INFO)
1320 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1321 else {
1322 DMEMIT("%u ", m->queue_if_no_path +
1323 (m->pg_init_retries > 0) * 2);
1324 if (m->queue_if_no_path)
1325 DMEMIT("queue_if_no_path ");
1326 if (m->pg_init_retries)
1327 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1330 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1331 DMEMIT("0 ");
1332 else
1333 DMEMIT("1 %s ", m->hw_handler_name);
1335 DMEMIT("%u ", m->nr_priority_groups);
1337 if (m->next_pg)
1338 pg_num = m->next_pg->pg_num;
1339 else if (m->current_pg)
1340 pg_num = m->current_pg->pg_num;
1341 else
1342 pg_num = 1;
1344 DMEMIT("%u ", pg_num);
1346 switch (type) {
1347 case STATUSTYPE_INFO:
1348 list_for_each_entry(pg, &m->priority_groups, list) {
1349 if (pg->bypassed)
1350 state = 'D'; /* Disabled */
1351 else if (pg == m->current_pg)
1352 state = 'A'; /* Currently Active */
1353 else
1354 state = 'E'; /* Enabled */
1356 DMEMIT("%c ", state);
1358 if (pg->ps.type->status)
1359 sz += pg->ps.type->status(&pg->ps, NULL, type,
1360 result + sz,
1361 maxlen - sz);
1362 else
1363 DMEMIT("0 ");
1365 DMEMIT("%u %u ", pg->nr_pgpaths,
1366 pg->ps.type->info_args);
1368 list_for_each_entry(p, &pg->pgpaths, list) {
1369 DMEMIT("%s %s %u ", p->path.dev->name,
1370 p->is_active ? "A" : "F",
1371 p->fail_count);
1372 if (pg->ps.type->status)
1373 sz += pg->ps.type->status(&pg->ps,
1374 &p->path, type, result + sz,
1375 maxlen - sz);
1378 break;
1380 case STATUSTYPE_TABLE:
1381 list_for_each_entry(pg, &m->priority_groups, list) {
1382 DMEMIT("%s ", pg->ps.type->name);
1384 if (pg->ps.type->status)
1385 sz += pg->ps.type->status(&pg->ps, NULL, type,
1386 result + sz,
1387 maxlen - sz);
1388 else
1389 DMEMIT("0 ");
1391 DMEMIT("%u %u ", pg->nr_pgpaths,
1392 pg->ps.type->table_args);
1394 list_for_each_entry(p, &pg->pgpaths, list) {
1395 DMEMIT("%s ", p->path.dev->name);
1396 if (pg->ps.type->status)
1397 sz += pg->ps.type->status(&pg->ps,
1398 &p->path, type, result + sz,
1399 maxlen - sz);
1402 break;
1405 spin_unlock_irqrestore(&m->lock, flags);
1407 return 0;
1410 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1412 int r = -EINVAL;
1413 struct dm_dev *dev;
1414 struct multipath *m = (struct multipath *) ti->private;
1415 action_fn action;
1417 mutex_lock(&m->work_mutex);
1419 if (m->suspended) {
1420 r = -EBUSY;
1421 goto out;
1424 if (dm_suspended(ti)) {
1425 r = -EBUSY;
1426 goto out;
1429 if (argc == 1) {
1430 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1431 r = queue_if_no_path(m, 1, 0);
1432 goto out;
1433 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1434 r = queue_if_no_path(m, 0, 0);
1435 goto out;
1439 if (argc != 2) {
1440 DMWARN("Unrecognised multipath message received.");
1441 goto out;
1444 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1445 r = bypass_pg_num(m, argv[1], 1);
1446 goto out;
1447 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1448 r = bypass_pg_num(m, argv[1], 0);
1449 goto out;
1450 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1451 r = switch_pg_num(m, argv[1]);
1452 goto out;
1453 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1454 action = reinstate_path;
1455 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1456 action = fail_path;
1457 else {
1458 DMWARN("Unrecognised multipath message received.");
1459 goto out;
1462 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1463 dm_table_get_mode(ti->table), &dev);
1464 if (r) {
1465 DMWARN("message: error getting device %s",
1466 argv[1]);
1467 goto out;
1470 r = action_dev(m, dev, action);
1472 dm_put_device(ti, dev);
1474 out:
1475 mutex_unlock(&m->work_mutex);
1476 return r;
1479 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1480 unsigned long arg)
1482 struct multipath *m = (struct multipath *) ti->private;
1483 struct block_device *bdev = NULL;
1484 fmode_t mode = 0;
1485 unsigned long flags;
1486 int r = 0;
1488 spin_lock_irqsave(&m->lock, flags);
1490 if (!m->current_pgpath)
1491 __choose_pgpath(m, 0);
1493 if (m->current_pgpath) {
1494 bdev = m->current_pgpath->path.dev->bdev;
1495 mode = m->current_pgpath->path.dev->mode;
1498 if (m->queue_io)
1499 r = -EAGAIN;
1500 else if (!bdev)
1501 r = -EIO;
1503 spin_unlock_irqrestore(&m->lock, flags);
1505 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1508 static int multipath_iterate_devices(struct dm_target *ti,
1509 iterate_devices_callout_fn fn, void *data)
1511 struct multipath *m = ti->private;
1512 struct priority_group *pg;
1513 struct pgpath *p;
1514 int ret = 0;
1516 list_for_each_entry(pg, &m->priority_groups, list) {
1517 list_for_each_entry(p, &pg->pgpaths, list) {
1518 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1519 if (ret)
1520 goto out;
1524 out:
1525 return ret;
1528 static int __pgpath_busy(struct pgpath *pgpath)
1530 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1532 return dm_underlying_device_busy(q);
1536 * We return "busy", only when we can map I/Os but underlying devices
1537 * are busy (so even if we map I/Os now, the I/Os will wait on
1538 * the underlying queue).
1539 * In other words, if we want to kill I/Os or queue them inside us
1540 * due to map unavailability, we don't return "busy". Otherwise,
1541 * dm core won't give us the I/Os and we can't do what we want.
1543 static int multipath_busy(struct dm_target *ti)
1545 int busy = 0, has_active = 0;
1546 struct multipath *m = ti->private;
1547 struct priority_group *pg;
1548 struct pgpath *pgpath;
1549 unsigned long flags;
1551 spin_lock_irqsave(&m->lock, flags);
1553 /* Guess which priority_group will be used at next mapping time */
1554 if (unlikely(!m->current_pgpath && m->next_pg))
1555 pg = m->next_pg;
1556 else if (likely(m->current_pg))
1557 pg = m->current_pg;
1558 else
1560 * We don't know which pg will be used at next mapping time.
1561 * We don't call __choose_pgpath() here to avoid to trigger
1562 * pg_init just by busy checking.
1563 * So we don't know whether underlying devices we will be using
1564 * at next mapping time are busy or not. Just try mapping.
1566 goto out;
1569 * If there is one non-busy active path at least, the path selector
1570 * will be able to select it. So we consider such a pg as not busy.
1572 busy = 1;
1573 list_for_each_entry(pgpath, &pg->pgpaths, list)
1574 if (pgpath->is_active) {
1575 has_active = 1;
1577 if (!__pgpath_busy(pgpath)) {
1578 busy = 0;
1579 break;
1583 if (!has_active)
1585 * No active path in this pg, so this pg won't be used and
1586 * the current_pg will be changed at next mapping time.
1587 * We need to try mapping to determine it.
1589 busy = 0;
1591 out:
1592 spin_unlock_irqrestore(&m->lock, flags);
1594 return busy;
1597 /*-----------------------------------------------------------------
1598 * Module setup
1599 *---------------------------------------------------------------*/
1600 static struct target_type multipath_target = {
1601 .name = "multipath",
1602 .version = {1, 1, 1},
1603 .module = THIS_MODULE,
1604 .ctr = multipath_ctr,
1605 .dtr = multipath_dtr,
1606 .map_rq = multipath_map,
1607 .rq_end_io = multipath_end_io,
1608 .presuspend = multipath_presuspend,
1609 .postsuspend = multipath_postsuspend,
1610 .resume = multipath_resume,
1611 .status = multipath_status,
1612 .message = multipath_message,
1613 .ioctl = multipath_ioctl,
1614 .iterate_devices = multipath_iterate_devices,
1615 .busy = multipath_busy,
1618 static int __init dm_multipath_init(void)
1620 int r;
1622 /* allocate a slab for the dm_ios */
1623 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1624 if (!_mpio_cache)
1625 return -ENOMEM;
1627 r = dm_register_target(&multipath_target);
1628 if (r < 0) {
1629 DMERR("register failed %d", r);
1630 kmem_cache_destroy(_mpio_cache);
1631 return -EINVAL;
1634 kmultipathd = create_workqueue("kmpathd");
1635 if (!kmultipathd) {
1636 DMERR("failed to create workqueue kmpathd");
1637 dm_unregister_target(&multipath_target);
1638 kmem_cache_destroy(_mpio_cache);
1639 return -ENOMEM;
1643 * A separate workqueue is used to handle the device handlers
1644 * to avoid overloading existing workqueue. Overloading the
1645 * old workqueue would also create a bottleneck in the
1646 * path of the storage hardware device activation.
1648 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1649 if (!kmpath_handlerd) {
1650 DMERR("failed to create workqueue kmpath_handlerd");
1651 destroy_workqueue(kmultipathd);
1652 dm_unregister_target(&multipath_target);
1653 kmem_cache_destroy(_mpio_cache);
1654 return -ENOMEM;
1657 DMINFO("version %u.%u.%u loaded",
1658 multipath_target.version[0], multipath_target.version[1],
1659 multipath_target.version[2]);
1661 return r;
1664 static void __exit dm_multipath_exit(void)
1666 destroy_workqueue(kmpath_handlerd);
1667 destroy_workqueue(kmultipathd);
1669 dm_unregister_target(&multipath_target);
1670 kmem_cache_destroy(_mpio_cache);
1673 module_init(dm_multipath_init);
1674 module_exit(dm_multipath_exit);
1676 MODULE_DESCRIPTION(DM_NAME " multipath target");
1677 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1678 MODULE_LICENSE("GPL");