ccwgroup: move attributes to attribute group
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-mpath.c
blobb03cd3971830275f240481eb5434e149abbaee71
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;
98 * Context information attached to each bio we process.
100 struct dm_mpath_io {
101 struct pgpath *pgpath;
102 size_t nr_bytes;
105 typedef int (*action_fn) (struct pgpath *pgpath);
107 #define MIN_IOS 256 /* Mempool size */
109 static struct kmem_cache *_mpio_cache;
111 static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
112 static void process_queued_ios(struct work_struct *work);
113 static void trigger_event(struct work_struct *work);
114 static void activate_path(struct work_struct *work);
117 /*-----------------------------------------------
118 * Allocation routines
119 *-----------------------------------------------*/
121 static struct pgpath *alloc_pgpath(void)
123 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
125 if (pgpath) {
126 pgpath->is_active = 1;
127 INIT_WORK(&pgpath->activate_path, activate_path);
130 return pgpath;
133 static void free_pgpath(struct pgpath *pgpath)
135 kfree(pgpath);
138 static struct priority_group *alloc_priority_group(void)
140 struct priority_group *pg;
142 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
144 if (pg)
145 INIT_LIST_HEAD(&pg->pgpaths);
147 return pg;
150 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
152 struct pgpath *pgpath, *tmp;
153 struct multipath *m = ti->private;
155 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
156 list_del(&pgpath->list);
157 if (m->hw_handler_name)
158 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
159 dm_put_device(ti, pgpath->path.dev);
160 free_pgpath(pgpath);
164 static void free_priority_group(struct priority_group *pg,
165 struct dm_target *ti)
167 struct path_selector *ps = &pg->ps;
169 if (ps->type) {
170 ps->type->destroy(ps);
171 dm_put_path_selector(ps->type);
174 free_pgpaths(&pg->pgpaths, ti);
175 kfree(pg);
178 static struct multipath *alloc_multipath(struct dm_target *ti)
180 struct multipath *m;
182 m = kzalloc(sizeof(*m), GFP_KERNEL);
183 if (m) {
184 INIT_LIST_HEAD(&m->priority_groups);
185 INIT_LIST_HEAD(&m->queued_ios);
186 spin_lock_init(&m->lock);
187 m->queue_io = 1;
188 INIT_WORK(&m->process_queued_ios, process_queued_ios);
189 INIT_WORK(&m->trigger_event, trigger_event);
190 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
191 if (!m->mpio_pool) {
192 kfree(m);
193 return NULL;
195 m->ti = ti;
196 ti->private = m;
199 return m;
202 static void free_multipath(struct multipath *m)
204 struct priority_group *pg, *tmp;
206 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
207 list_del(&pg->list);
208 free_priority_group(pg, m->ti);
211 kfree(m->hw_handler_name);
212 kfree(m->hw_handler_params);
213 mempool_destroy(m->mpio_pool);
214 kfree(m);
218 /*-----------------------------------------------
219 * Path selection
220 *-----------------------------------------------*/
222 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
224 m->current_pg = pgpath->pg;
226 /* Must we initialise the PG first, and queue I/O till it's ready? */
227 if (m->hw_handler_name) {
228 m->pg_init_required = 1;
229 m->queue_io = 1;
230 } else {
231 m->pg_init_required = 0;
232 m->queue_io = 0;
235 m->pg_init_count = 0;
238 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
239 size_t nr_bytes)
241 struct dm_path *path;
243 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
244 if (!path)
245 return -ENXIO;
247 m->current_pgpath = path_to_pgpath(path);
249 if (m->current_pg != pg)
250 __switch_pg(m, m->current_pgpath);
252 return 0;
255 static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
257 struct priority_group *pg;
258 unsigned bypassed = 1;
260 if (!m->nr_valid_paths)
261 goto failed;
263 /* Were we instructed to switch PG? */
264 if (m->next_pg) {
265 pg = m->next_pg;
266 m->next_pg = NULL;
267 if (!__choose_path_in_pg(m, pg, nr_bytes))
268 return;
271 /* Don't change PG until it has no remaining paths */
272 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
273 return;
276 * Loop through priority groups until we find a valid path.
277 * First time we skip PGs marked 'bypassed'.
278 * Second time we only try the ones we skipped.
280 do {
281 list_for_each_entry(pg, &m->priority_groups, list) {
282 if (pg->bypassed == bypassed)
283 continue;
284 if (!__choose_path_in_pg(m, pg, nr_bytes))
285 return;
287 } while (bypassed--);
289 failed:
290 m->current_pgpath = NULL;
291 m->current_pg = NULL;
295 * Check whether bios must be queued in the device-mapper core rather
296 * than here in the target.
298 * m->lock must be held on entry.
300 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
301 * same value then we are not between multipath_presuspend()
302 * and multipath_resume() calls and we have no need to check
303 * for the DMF_NOFLUSH_SUSPENDING flag.
305 static int __must_push_back(struct multipath *m)
307 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
308 dm_noflush_suspending(m->ti));
311 static int map_io(struct multipath *m, struct request *clone,
312 struct dm_mpath_io *mpio, unsigned was_queued)
314 int r = DM_MAPIO_REMAPPED;
315 size_t nr_bytes = blk_rq_bytes(clone);
316 unsigned long flags;
317 struct pgpath *pgpath;
318 struct block_device *bdev;
320 spin_lock_irqsave(&m->lock, flags);
322 /* Do we need to select a new pgpath? */
323 if (!m->current_pgpath ||
324 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
325 __choose_pgpath(m, nr_bytes);
327 pgpath = m->current_pgpath;
329 if (was_queued)
330 m->queue_size--;
332 if ((pgpath && m->queue_io) ||
333 (!pgpath && m->queue_if_no_path)) {
334 /* Queue for the daemon to resubmit */
335 list_add_tail(&clone->queuelist, &m->queued_ios);
336 m->queue_size++;
337 if ((m->pg_init_required && !m->pg_init_in_progress) ||
338 !m->queue_io)
339 queue_work(kmultipathd, &m->process_queued_ios);
340 pgpath = NULL;
341 r = DM_MAPIO_SUBMITTED;
342 } else if (pgpath) {
343 bdev = pgpath->path.dev->bdev;
344 clone->q = bdev_get_queue(bdev);
345 clone->rq_disk = bdev->bd_disk;
346 } else if (__must_push_back(m))
347 r = DM_MAPIO_REQUEUE;
348 else
349 r = -EIO; /* Failed */
351 mpio->pgpath = pgpath;
352 mpio->nr_bytes = nr_bytes;
354 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
355 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
356 nr_bytes);
358 spin_unlock_irqrestore(&m->lock, flags);
360 return r;
364 * If we run out of usable paths, should we queue I/O or error it?
366 static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
367 unsigned save_old_value)
369 unsigned long flags;
371 spin_lock_irqsave(&m->lock, flags);
373 if (save_old_value)
374 m->saved_queue_if_no_path = m->queue_if_no_path;
375 else
376 m->saved_queue_if_no_path = queue_if_no_path;
377 m->queue_if_no_path = queue_if_no_path;
378 if (!m->queue_if_no_path && m->queue_size)
379 queue_work(kmultipathd, &m->process_queued_ios);
381 spin_unlock_irqrestore(&m->lock, flags);
383 return 0;
386 /*-----------------------------------------------------------------
387 * The multipath daemon is responsible for resubmitting queued ios.
388 *---------------------------------------------------------------*/
390 static void dispatch_queued_ios(struct multipath *m)
392 int r;
393 unsigned long flags;
394 struct dm_mpath_io *mpio;
395 union map_info *info;
396 struct request *clone, *n;
397 LIST_HEAD(cl);
399 spin_lock_irqsave(&m->lock, flags);
400 list_splice_init(&m->queued_ios, &cl);
401 spin_unlock_irqrestore(&m->lock, flags);
403 list_for_each_entry_safe(clone, n, &cl, queuelist) {
404 list_del_init(&clone->queuelist);
406 info = dm_get_rq_mapinfo(clone);
407 mpio = info->ptr;
409 r = map_io(m, clone, mpio, 1);
410 if (r < 0) {
411 mempool_free(mpio, m->mpio_pool);
412 dm_kill_unmapped_request(clone, r);
413 } else if (r == DM_MAPIO_REMAPPED)
414 dm_dispatch_request(clone);
415 else if (r == DM_MAPIO_REQUEUE) {
416 mempool_free(mpio, m->mpio_pool);
417 dm_requeue_unmapped_request(clone);
422 static void process_queued_ios(struct work_struct *work)
424 struct multipath *m =
425 container_of(work, struct multipath, process_queued_ios);
426 struct pgpath *pgpath = NULL, *tmp;
427 unsigned must_queue = 1;
428 unsigned long flags;
430 spin_lock_irqsave(&m->lock, flags);
432 if (!m->queue_size)
433 goto out;
435 if (!m->current_pgpath)
436 __choose_pgpath(m, 0);
438 pgpath = m->current_pgpath;
440 if ((pgpath && !m->queue_io) ||
441 (!pgpath && !m->queue_if_no_path))
442 must_queue = 0;
444 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
445 m->pg_init_count++;
446 m->pg_init_required = 0;
447 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
448 if (queue_work(kmpath_handlerd, &tmp->activate_path))
449 m->pg_init_in_progress++;
452 out:
453 spin_unlock_irqrestore(&m->lock, flags);
454 if (!must_queue)
455 dispatch_queued_ios(m);
459 * An event is triggered whenever a path is taken out of use.
460 * Includes path failure and PG bypass.
462 static void trigger_event(struct work_struct *work)
464 struct multipath *m =
465 container_of(work, struct multipath, trigger_event);
467 dm_table_event(m->ti->table);
470 /*-----------------------------------------------------------------
471 * Constructor/argument parsing:
472 * <#multipath feature args> [<arg>]*
473 * <#hw_handler args> [hw_handler [<arg>]*]
474 * <#priority groups>
475 * <initial priority group>
476 * [<selector> <#selector args> [<arg>]*
477 * <#paths> <#per-path selector args>
478 * [<path> [<arg>]* ]+ ]+
479 *---------------------------------------------------------------*/
480 struct param {
481 unsigned min;
482 unsigned max;
483 char *error;
486 static int read_param(struct param *param, char *str, unsigned *v, char **error)
488 if (!str ||
489 (sscanf(str, "%u", v) != 1) ||
490 (*v < param->min) ||
491 (*v > param->max)) {
492 *error = param->error;
493 return -EINVAL;
496 return 0;
499 struct arg_set {
500 unsigned argc;
501 char **argv;
504 static char *shift(struct arg_set *as)
506 char *r;
508 if (as->argc) {
509 as->argc--;
510 r = *as->argv;
511 as->argv++;
512 return r;
515 return NULL;
518 static void consume(struct arg_set *as, unsigned n)
520 BUG_ON (as->argc < n);
521 as->argc -= n;
522 as->argv += n;
525 static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
526 struct dm_target *ti)
528 int r;
529 struct path_selector_type *pst;
530 unsigned ps_argc;
532 static struct param _params[] = {
533 {0, 1024, "invalid number of path selector args"},
536 pst = dm_get_path_selector(shift(as));
537 if (!pst) {
538 ti->error = "unknown path selector type";
539 return -EINVAL;
542 r = read_param(_params, shift(as), &ps_argc, &ti->error);
543 if (r) {
544 dm_put_path_selector(pst);
545 return -EINVAL;
548 if (ps_argc > as->argc) {
549 dm_put_path_selector(pst);
550 ti->error = "not enough arguments for path selector";
551 return -EINVAL;
554 r = pst->create(&pg->ps, ps_argc, as->argv);
555 if (r) {
556 dm_put_path_selector(pst);
557 ti->error = "path selector constructor failed";
558 return r;
561 pg->ps.type = pst;
562 consume(as, ps_argc);
564 return 0;
567 static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
568 struct dm_target *ti)
570 int r;
571 struct pgpath *p;
572 struct multipath *m = ti->private;
574 /* we need at least a path arg */
575 if (as->argc < 1) {
576 ti->error = "no device given";
577 return ERR_PTR(-EINVAL);
580 p = alloc_pgpath();
581 if (!p)
582 return ERR_PTR(-ENOMEM);
584 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
585 dm_table_get_mode(ti->table), &p->path.dev);
586 if (r) {
587 ti->error = "error getting device";
588 goto bad;
591 if (m->hw_handler_name) {
592 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
594 r = scsi_dh_attach(q, m->hw_handler_name);
595 if (r == -EBUSY) {
597 * Already attached to different hw_handler,
598 * try to reattach with correct one.
600 scsi_dh_detach(q);
601 r = scsi_dh_attach(q, m->hw_handler_name);
604 if (r < 0) {
605 ti->error = "error attaching hardware handler";
606 dm_put_device(ti, p->path.dev);
607 goto bad;
610 if (m->hw_handler_params) {
611 r = scsi_dh_set_params(q, m->hw_handler_params);
612 if (r < 0) {
613 ti->error = "unable to set hardware "
614 "handler parameters";
615 scsi_dh_detach(q);
616 dm_put_device(ti, p->path.dev);
617 goto bad;
622 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
623 if (r) {
624 dm_put_device(ti, p->path.dev);
625 goto bad;
628 return p;
630 bad:
631 free_pgpath(p);
632 return ERR_PTR(r);
635 static struct priority_group *parse_priority_group(struct arg_set *as,
636 struct multipath *m)
638 static struct param _params[] = {
639 {1, 1024, "invalid number of paths"},
640 {0, 1024, "invalid number of selector args"}
643 int r;
644 unsigned i, nr_selector_args, nr_params;
645 struct priority_group *pg;
646 struct dm_target *ti = m->ti;
648 if (as->argc < 2) {
649 as->argc = 0;
650 ti->error = "not enough priority group arguments";
651 return ERR_PTR(-EINVAL);
654 pg = alloc_priority_group();
655 if (!pg) {
656 ti->error = "couldn't allocate priority group";
657 return ERR_PTR(-ENOMEM);
659 pg->m = m;
661 r = parse_path_selector(as, pg, ti);
662 if (r)
663 goto bad;
666 * read the paths
668 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
669 if (r)
670 goto bad;
672 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
673 if (r)
674 goto bad;
676 nr_params = 1 + nr_selector_args;
677 for (i = 0; i < pg->nr_pgpaths; i++) {
678 struct pgpath *pgpath;
679 struct arg_set path_args;
681 if (as->argc < nr_params) {
682 ti->error = "not enough path parameters";
683 r = -EINVAL;
684 goto bad;
687 path_args.argc = nr_params;
688 path_args.argv = as->argv;
690 pgpath = parse_path(&path_args, &pg->ps, ti);
691 if (IS_ERR(pgpath)) {
692 r = PTR_ERR(pgpath);
693 goto bad;
696 pgpath->pg = pg;
697 list_add_tail(&pgpath->list, &pg->pgpaths);
698 consume(as, nr_params);
701 return pg;
703 bad:
704 free_priority_group(pg, ti);
705 return ERR_PTR(r);
708 static int parse_hw_handler(struct arg_set *as, struct multipath *m)
710 unsigned hw_argc;
711 int ret;
712 struct dm_target *ti = m->ti;
714 static struct param _params[] = {
715 {0, 1024, "invalid number of hardware handler args"},
718 if (read_param(_params, shift(as), &hw_argc, &ti->error))
719 return -EINVAL;
721 if (!hw_argc)
722 return 0;
724 if (hw_argc > as->argc) {
725 ti->error = "not enough arguments for hardware handler";
726 return -EINVAL;
729 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
730 request_module("scsi_dh_%s", m->hw_handler_name);
731 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
732 ti->error = "unknown hardware handler type";
733 ret = -EINVAL;
734 goto fail;
737 if (hw_argc > 1) {
738 char *p;
739 int i, j, len = 4;
741 for (i = 0; i <= hw_argc - 2; i++)
742 len += strlen(as->argv[i]) + 1;
743 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
744 if (!p) {
745 ti->error = "memory allocation failed";
746 ret = -ENOMEM;
747 goto fail;
749 j = sprintf(p, "%d", hw_argc - 1);
750 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
751 j = sprintf(p, "%s", as->argv[i]);
753 consume(as, hw_argc - 1);
755 return 0;
756 fail:
757 kfree(m->hw_handler_name);
758 m->hw_handler_name = NULL;
759 return ret;
762 static int parse_features(struct arg_set *as, struct multipath *m)
764 int r;
765 unsigned argc;
766 struct dm_target *ti = m->ti;
767 const char *param_name;
769 static struct param _params[] = {
770 {0, 3, "invalid number of feature args"},
771 {1, 50, "pg_init_retries must be between 1 and 50"},
774 r = read_param(_params, shift(as), &argc, &ti->error);
775 if (r)
776 return -EINVAL;
778 if (!argc)
779 return 0;
781 if (argc > as->argc) {
782 ti->error = "not enough arguments for features";
783 return -EINVAL;
786 do {
787 param_name = shift(as);
788 argc--;
790 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
791 r = queue_if_no_path(m, 1, 0);
792 continue;
795 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
796 (argc >= 1)) {
797 r = read_param(_params + 1, shift(as),
798 &m->pg_init_retries, &ti->error);
799 argc--;
800 continue;
803 ti->error = "Unrecognised multipath feature request";
804 r = -EINVAL;
805 } while (argc && !r);
807 return r;
810 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
811 char **argv)
813 /* target parameters */
814 static struct param _params[] = {
815 {1, 1024, "invalid number of priority groups"},
816 {1, 1024, "invalid initial priority group number"},
819 int r;
820 struct multipath *m;
821 struct arg_set as;
822 unsigned pg_count = 0;
823 unsigned next_pg_num;
825 as.argc = argc;
826 as.argv = argv;
828 m = alloc_multipath(ti);
829 if (!m) {
830 ti->error = "can't allocate multipath";
831 return -EINVAL;
834 r = parse_features(&as, m);
835 if (r)
836 goto bad;
838 r = parse_hw_handler(&as, m);
839 if (r)
840 goto bad;
842 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
843 if (r)
844 goto bad;
846 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
847 if (r)
848 goto bad;
850 /* parse the priority groups */
851 while (as.argc) {
852 struct priority_group *pg;
854 pg = parse_priority_group(&as, m);
855 if (IS_ERR(pg)) {
856 r = PTR_ERR(pg);
857 goto bad;
860 m->nr_valid_paths += pg->nr_pgpaths;
861 list_add_tail(&pg->list, &m->priority_groups);
862 pg_count++;
863 pg->pg_num = pg_count;
864 if (!--next_pg_num)
865 m->next_pg = pg;
868 if (pg_count != m->nr_priority_groups) {
869 ti->error = "priority group count mismatch";
870 r = -EINVAL;
871 goto bad;
874 ti->num_flush_requests = 1;
876 return 0;
878 bad:
879 free_multipath(m);
880 return r;
883 static void multipath_dtr(struct dm_target *ti)
885 struct multipath *m = (struct multipath *) ti->private;
887 flush_workqueue(kmpath_handlerd);
888 flush_workqueue(kmultipathd);
889 flush_scheduled_work();
890 free_multipath(m);
894 * Map cloned requests
896 static int multipath_map(struct dm_target *ti, struct request *clone,
897 union map_info *map_context)
899 int r;
900 struct dm_mpath_io *mpio;
901 struct multipath *m = (struct multipath *) ti->private;
903 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
904 if (!mpio)
905 /* ENOMEM, requeue */
906 return DM_MAPIO_REQUEUE;
907 memset(mpio, 0, sizeof(*mpio));
909 map_context->ptr = mpio;
910 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
911 r = map_io(m, clone, mpio, 0);
912 if (r < 0 || r == DM_MAPIO_REQUEUE)
913 mempool_free(mpio, m->mpio_pool);
915 return r;
919 * Take a path out of use.
921 static int fail_path(struct pgpath *pgpath)
923 unsigned long flags;
924 struct multipath *m = pgpath->pg->m;
926 spin_lock_irqsave(&m->lock, flags);
928 if (!pgpath->is_active)
929 goto out;
931 DMWARN("Failing path %s.", pgpath->path.dev->name);
933 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
934 pgpath->is_active = 0;
935 pgpath->fail_count++;
937 m->nr_valid_paths--;
939 if (pgpath == m->current_pgpath)
940 m->current_pgpath = NULL;
942 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
943 pgpath->path.dev->name, m->nr_valid_paths);
945 schedule_work(&m->trigger_event);
947 out:
948 spin_unlock_irqrestore(&m->lock, flags);
950 return 0;
954 * Reinstate a previously-failed path
956 static int reinstate_path(struct pgpath *pgpath)
958 int r = 0;
959 unsigned long flags;
960 struct multipath *m = pgpath->pg->m;
962 spin_lock_irqsave(&m->lock, flags);
964 if (pgpath->is_active)
965 goto out;
967 if (!pgpath->pg->ps.type->reinstate_path) {
968 DMWARN("Reinstate path not supported by path selector %s",
969 pgpath->pg->ps.type->name);
970 r = -EINVAL;
971 goto out;
974 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
975 if (r)
976 goto out;
978 pgpath->is_active = 1;
980 if (!m->nr_valid_paths++ && m->queue_size) {
981 m->current_pgpath = NULL;
982 queue_work(kmultipathd, &m->process_queued_ios);
983 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
984 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
985 m->pg_init_in_progress++;
988 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
989 pgpath->path.dev->name, m->nr_valid_paths);
991 schedule_work(&m->trigger_event);
993 out:
994 spin_unlock_irqrestore(&m->lock, flags);
996 return r;
1000 * Fail or reinstate all paths that match the provided struct dm_dev.
1002 static int action_dev(struct multipath *m, struct dm_dev *dev,
1003 action_fn action)
1005 int r = 0;
1006 struct pgpath *pgpath;
1007 struct priority_group *pg;
1009 list_for_each_entry(pg, &m->priority_groups, list) {
1010 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1011 if (pgpath->path.dev == dev)
1012 r = action(pgpath);
1016 return r;
1020 * Temporarily try to avoid having to use the specified PG
1022 static void bypass_pg(struct multipath *m, struct priority_group *pg,
1023 int bypassed)
1025 unsigned long flags;
1027 spin_lock_irqsave(&m->lock, flags);
1029 pg->bypassed = bypassed;
1030 m->current_pgpath = NULL;
1031 m->current_pg = NULL;
1033 spin_unlock_irqrestore(&m->lock, flags);
1035 schedule_work(&m->trigger_event);
1039 * Switch to using the specified PG from the next I/O that gets mapped
1041 static int switch_pg_num(struct multipath *m, const char *pgstr)
1043 struct priority_group *pg;
1044 unsigned pgnum;
1045 unsigned long flags;
1047 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1048 (pgnum > m->nr_priority_groups)) {
1049 DMWARN("invalid PG number supplied to switch_pg_num");
1050 return -EINVAL;
1053 spin_lock_irqsave(&m->lock, flags);
1054 list_for_each_entry(pg, &m->priority_groups, list) {
1055 pg->bypassed = 0;
1056 if (--pgnum)
1057 continue;
1059 m->current_pgpath = NULL;
1060 m->current_pg = NULL;
1061 m->next_pg = pg;
1063 spin_unlock_irqrestore(&m->lock, flags);
1065 schedule_work(&m->trigger_event);
1066 return 0;
1070 * Set/clear bypassed status of a PG.
1071 * PGs are numbered upwards from 1 in the order they were declared.
1073 static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1075 struct priority_group *pg;
1076 unsigned pgnum;
1078 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1079 (pgnum > m->nr_priority_groups)) {
1080 DMWARN("invalid PG number supplied to bypass_pg");
1081 return -EINVAL;
1084 list_for_each_entry(pg, &m->priority_groups, list) {
1085 if (!--pgnum)
1086 break;
1089 bypass_pg(m, pg, bypassed);
1090 return 0;
1094 * Should we retry pg_init immediately?
1096 static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1098 unsigned long flags;
1099 int limit_reached = 0;
1101 spin_lock_irqsave(&m->lock, flags);
1103 if (m->pg_init_count <= m->pg_init_retries)
1104 m->pg_init_required = 1;
1105 else
1106 limit_reached = 1;
1108 spin_unlock_irqrestore(&m->lock, flags);
1110 return limit_reached;
1113 static void pg_init_done(struct dm_path *path, int errors)
1115 struct pgpath *pgpath = path_to_pgpath(path);
1116 struct priority_group *pg = pgpath->pg;
1117 struct multipath *m = pg->m;
1118 unsigned long flags;
1120 /* device or driver problems */
1121 switch (errors) {
1122 case SCSI_DH_OK:
1123 break;
1124 case SCSI_DH_NOSYS:
1125 if (!m->hw_handler_name) {
1126 errors = 0;
1127 break;
1129 DMERR("Cannot failover device because scsi_dh_%s was not "
1130 "loaded.", m->hw_handler_name);
1132 * Fail path for now, so we do not ping pong
1134 fail_path(pgpath);
1135 break;
1136 case SCSI_DH_DEV_TEMP_BUSY:
1138 * Probably doing something like FW upgrade on the
1139 * controller so try the other pg.
1141 bypass_pg(m, pg, 1);
1142 break;
1143 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1144 case SCSI_DH_RETRY:
1145 case SCSI_DH_IMM_RETRY:
1146 case SCSI_DH_RES_TEMP_UNAVAIL:
1147 if (pg_init_limit_reached(m, pgpath))
1148 fail_path(pgpath);
1149 errors = 0;
1150 break;
1151 default:
1153 * We probably do not want to fail the path for a device
1154 * error, but this is what the old dm did. In future
1155 * patches we can do more advanced handling.
1157 fail_path(pgpath);
1160 spin_lock_irqsave(&m->lock, flags);
1161 if (errors) {
1162 if (pgpath == m->current_pgpath) {
1163 DMERR("Could not failover device. Error %d.", errors);
1164 m->current_pgpath = NULL;
1165 m->current_pg = NULL;
1167 } else if (!m->pg_init_required) {
1168 m->queue_io = 0;
1169 pg->bypassed = 0;
1172 m->pg_init_in_progress--;
1173 if (!m->pg_init_in_progress)
1174 queue_work(kmultipathd, &m->process_queued_ios);
1175 spin_unlock_irqrestore(&m->lock, flags);
1178 static void activate_path(struct work_struct *work)
1180 int ret;
1181 struct pgpath *pgpath =
1182 container_of(work, struct pgpath, activate_path);
1184 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1185 pg_init_done(&pgpath->path, ret);
1189 * end_io handling
1191 static int do_end_io(struct multipath *m, struct request *clone,
1192 int error, struct dm_mpath_io *mpio)
1195 * We don't queue any clone request inside the multipath target
1196 * during end I/O handling, since those clone requests don't have
1197 * bio clones. If we queue them inside the multipath target,
1198 * we need to make bio clones, that requires memory allocation.
1199 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1200 * don't have bio clones.)
1201 * Instead of queueing the clone request here, we queue the original
1202 * request into dm core, which will remake a clone request and
1203 * clone bios for it and resubmit it later.
1205 int r = DM_ENDIO_REQUEUE;
1206 unsigned long flags;
1208 if (!error && !clone->errors)
1209 return 0; /* I/O complete */
1211 if (error == -EOPNOTSUPP)
1212 return error;
1214 if (mpio->pgpath)
1215 fail_path(mpio->pgpath);
1217 spin_lock_irqsave(&m->lock, flags);
1218 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1219 r = -EIO;
1220 spin_unlock_irqrestore(&m->lock, flags);
1222 return r;
1225 static int multipath_end_io(struct dm_target *ti, struct request *clone,
1226 int error, union map_info *map_context)
1228 struct multipath *m = ti->private;
1229 struct dm_mpath_io *mpio = map_context->ptr;
1230 struct pgpath *pgpath = mpio->pgpath;
1231 struct path_selector *ps;
1232 int r;
1234 r = do_end_io(m, clone, error, mpio);
1235 if (pgpath) {
1236 ps = &pgpath->pg->ps;
1237 if (ps->type->end_io)
1238 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1240 mempool_free(mpio, m->mpio_pool);
1242 return r;
1246 * Suspend can't complete until all the I/O is processed so if
1247 * the last path fails we must error any remaining I/O.
1248 * Note that if the freeze_bdev fails while suspending, the
1249 * queue_if_no_path state is lost - userspace should reset it.
1251 static void multipath_presuspend(struct dm_target *ti)
1253 struct multipath *m = (struct multipath *) ti->private;
1255 queue_if_no_path(m, 0, 1);
1259 * Restore the queue_if_no_path setting.
1261 static void multipath_resume(struct dm_target *ti)
1263 struct multipath *m = (struct multipath *) ti->private;
1264 unsigned long flags;
1266 spin_lock_irqsave(&m->lock, flags);
1267 m->queue_if_no_path = m->saved_queue_if_no_path;
1268 spin_unlock_irqrestore(&m->lock, flags);
1272 * Info output has the following format:
1273 * num_multipath_feature_args [multipath_feature_args]*
1274 * num_handler_status_args [handler_status_args]*
1275 * num_groups init_group_number
1276 * [A|D|E num_ps_status_args [ps_status_args]*
1277 * num_paths num_selector_args
1278 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1280 * Table output has the following format (identical to the constructor string):
1281 * num_feature_args [features_args]*
1282 * num_handler_args hw_handler [hw_handler_args]*
1283 * num_groups init_group_number
1284 * [priority selector-name num_ps_args [ps_args]*
1285 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1287 static int multipath_status(struct dm_target *ti, status_type_t type,
1288 char *result, unsigned int maxlen)
1290 int sz = 0;
1291 unsigned long flags;
1292 struct multipath *m = (struct multipath *) ti->private;
1293 struct priority_group *pg;
1294 struct pgpath *p;
1295 unsigned pg_num;
1296 char state;
1298 spin_lock_irqsave(&m->lock, flags);
1300 /* Features */
1301 if (type == STATUSTYPE_INFO)
1302 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1303 else {
1304 DMEMIT("%u ", m->queue_if_no_path +
1305 (m->pg_init_retries > 0) * 2);
1306 if (m->queue_if_no_path)
1307 DMEMIT("queue_if_no_path ");
1308 if (m->pg_init_retries)
1309 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1312 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1313 DMEMIT("0 ");
1314 else
1315 DMEMIT("1 %s ", m->hw_handler_name);
1317 DMEMIT("%u ", m->nr_priority_groups);
1319 if (m->next_pg)
1320 pg_num = m->next_pg->pg_num;
1321 else if (m->current_pg)
1322 pg_num = m->current_pg->pg_num;
1323 else
1324 pg_num = 1;
1326 DMEMIT("%u ", pg_num);
1328 switch (type) {
1329 case STATUSTYPE_INFO:
1330 list_for_each_entry(pg, &m->priority_groups, list) {
1331 if (pg->bypassed)
1332 state = 'D'; /* Disabled */
1333 else if (pg == m->current_pg)
1334 state = 'A'; /* Currently Active */
1335 else
1336 state = 'E'; /* Enabled */
1338 DMEMIT("%c ", state);
1340 if (pg->ps.type->status)
1341 sz += pg->ps.type->status(&pg->ps, NULL, type,
1342 result + sz,
1343 maxlen - sz);
1344 else
1345 DMEMIT("0 ");
1347 DMEMIT("%u %u ", pg->nr_pgpaths,
1348 pg->ps.type->info_args);
1350 list_for_each_entry(p, &pg->pgpaths, list) {
1351 DMEMIT("%s %s %u ", p->path.dev->name,
1352 p->is_active ? "A" : "F",
1353 p->fail_count);
1354 if (pg->ps.type->status)
1355 sz += pg->ps.type->status(&pg->ps,
1356 &p->path, type, result + sz,
1357 maxlen - sz);
1360 break;
1362 case STATUSTYPE_TABLE:
1363 list_for_each_entry(pg, &m->priority_groups, list) {
1364 DMEMIT("%s ", pg->ps.type->name);
1366 if (pg->ps.type->status)
1367 sz += pg->ps.type->status(&pg->ps, NULL, type,
1368 result + sz,
1369 maxlen - sz);
1370 else
1371 DMEMIT("0 ");
1373 DMEMIT("%u %u ", pg->nr_pgpaths,
1374 pg->ps.type->table_args);
1376 list_for_each_entry(p, &pg->pgpaths, list) {
1377 DMEMIT("%s ", p->path.dev->name);
1378 if (pg->ps.type->status)
1379 sz += pg->ps.type->status(&pg->ps,
1380 &p->path, type, result + sz,
1381 maxlen - sz);
1384 break;
1387 spin_unlock_irqrestore(&m->lock, flags);
1389 return 0;
1392 static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1394 int r;
1395 struct dm_dev *dev;
1396 struct multipath *m = (struct multipath *) ti->private;
1397 action_fn action;
1399 if (argc == 1) {
1400 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1401 return queue_if_no_path(m, 1, 0);
1402 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1403 return queue_if_no_path(m, 0, 0);
1406 if (argc != 2)
1407 goto error;
1409 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1410 return bypass_pg_num(m, argv[1], 1);
1411 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1412 return bypass_pg_num(m, argv[1], 0);
1413 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1414 return switch_pg_num(m, argv[1]);
1415 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1416 action = reinstate_path;
1417 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1418 action = fail_path;
1419 else
1420 goto error;
1422 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1423 dm_table_get_mode(ti->table), &dev);
1424 if (r) {
1425 DMWARN("message: error getting device %s",
1426 argv[1]);
1427 return -EINVAL;
1430 r = action_dev(m, dev, action);
1432 dm_put_device(ti, dev);
1434 return r;
1436 error:
1437 DMWARN("Unrecognised multipath message received.");
1438 return -EINVAL;
1441 static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
1442 unsigned long arg)
1444 struct multipath *m = (struct multipath *) ti->private;
1445 struct block_device *bdev = NULL;
1446 fmode_t mode = 0;
1447 unsigned long flags;
1448 int r = 0;
1450 spin_lock_irqsave(&m->lock, flags);
1452 if (!m->current_pgpath)
1453 __choose_pgpath(m, 0);
1455 if (m->current_pgpath) {
1456 bdev = m->current_pgpath->path.dev->bdev;
1457 mode = m->current_pgpath->path.dev->mode;
1460 if (m->queue_io)
1461 r = -EAGAIN;
1462 else if (!bdev)
1463 r = -EIO;
1465 spin_unlock_irqrestore(&m->lock, flags);
1467 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
1470 static int multipath_iterate_devices(struct dm_target *ti,
1471 iterate_devices_callout_fn fn, void *data)
1473 struct multipath *m = ti->private;
1474 struct priority_group *pg;
1475 struct pgpath *p;
1476 int ret = 0;
1478 list_for_each_entry(pg, &m->priority_groups, list) {
1479 list_for_each_entry(p, &pg->pgpaths, list) {
1480 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1481 if (ret)
1482 goto out;
1486 out:
1487 return ret;
1490 static int __pgpath_busy(struct pgpath *pgpath)
1492 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1494 return dm_underlying_device_busy(q);
1498 * We return "busy", only when we can map I/Os but underlying devices
1499 * are busy (so even if we map I/Os now, the I/Os will wait on
1500 * the underlying queue).
1501 * In other words, if we want to kill I/Os or queue them inside us
1502 * due to map unavailability, we don't return "busy". Otherwise,
1503 * dm core won't give us the I/Os and we can't do what we want.
1505 static int multipath_busy(struct dm_target *ti)
1507 int busy = 0, has_active = 0;
1508 struct multipath *m = ti->private;
1509 struct priority_group *pg;
1510 struct pgpath *pgpath;
1511 unsigned long flags;
1513 spin_lock_irqsave(&m->lock, flags);
1515 /* Guess which priority_group will be used at next mapping time */
1516 if (unlikely(!m->current_pgpath && m->next_pg))
1517 pg = m->next_pg;
1518 else if (likely(m->current_pg))
1519 pg = m->current_pg;
1520 else
1522 * We don't know which pg will be used at next mapping time.
1523 * We don't call __choose_pgpath() here to avoid to trigger
1524 * pg_init just by busy checking.
1525 * So we don't know whether underlying devices we will be using
1526 * at next mapping time are busy or not. Just try mapping.
1528 goto out;
1531 * If there is one non-busy active path at least, the path selector
1532 * will be able to select it. So we consider such a pg as not busy.
1534 busy = 1;
1535 list_for_each_entry(pgpath, &pg->pgpaths, list)
1536 if (pgpath->is_active) {
1537 has_active = 1;
1539 if (!__pgpath_busy(pgpath)) {
1540 busy = 0;
1541 break;
1545 if (!has_active)
1547 * No active path in this pg, so this pg won't be used and
1548 * the current_pg will be changed at next mapping time.
1549 * We need to try mapping to determine it.
1551 busy = 0;
1553 out:
1554 spin_unlock_irqrestore(&m->lock, flags);
1556 return busy;
1559 /*-----------------------------------------------------------------
1560 * Module setup
1561 *---------------------------------------------------------------*/
1562 static struct target_type multipath_target = {
1563 .name = "multipath",
1564 .version = {1, 1, 0},
1565 .module = THIS_MODULE,
1566 .ctr = multipath_ctr,
1567 .dtr = multipath_dtr,
1568 .map_rq = multipath_map,
1569 .rq_end_io = multipath_end_io,
1570 .presuspend = multipath_presuspend,
1571 .resume = multipath_resume,
1572 .status = multipath_status,
1573 .message = multipath_message,
1574 .ioctl = multipath_ioctl,
1575 .iterate_devices = multipath_iterate_devices,
1576 .busy = multipath_busy,
1579 static int __init dm_multipath_init(void)
1581 int r;
1583 /* allocate a slab for the dm_ios */
1584 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1585 if (!_mpio_cache)
1586 return -ENOMEM;
1588 r = dm_register_target(&multipath_target);
1589 if (r < 0) {
1590 DMERR("register failed %d", r);
1591 kmem_cache_destroy(_mpio_cache);
1592 return -EINVAL;
1595 kmultipathd = create_workqueue("kmpathd");
1596 if (!kmultipathd) {
1597 DMERR("failed to create workqueue kmpathd");
1598 dm_unregister_target(&multipath_target);
1599 kmem_cache_destroy(_mpio_cache);
1600 return -ENOMEM;
1604 * A separate workqueue is used to handle the device handlers
1605 * to avoid overloading existing workqueue. Overloading the
1606 * old workqueue would also create a bottleneck in the
1607 * path of the storage hardware device activation.
1609 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1610 if (!kmpath_handlerd) {
1611 DMERR("failed to create workqueue kmpath_handlerd");
1612 destroy_workqueue(kmultipathd);
1613 dm_unregister_target(&multipath_target);
1614 kmem_cache_destroy(_mpio_cache);
1615 return -ENOMEM;
1618 DMINFO("version %u.%u.%u loaded",
1619 multipath_target.version[0], multipath_target.version[1],
1620 multipath_target.version[2]);
1622 return r;
1625 static void __exit dm_multipath_exit(void)
1627 destroy_workqueue(kmpath_handlerd);
1628 destroy_workqueue(kmultipathd);
1630 dm_unregister_target(&multipath_target);
1631 kmem_cache_destroy(_mpio_cache);
1634 module_init(dm_multipath_init);
1635 module_exit(dm_multipath_exit);
1637 MODULE_DESCRIPTION(DM_NAME " multipath target");
1638 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1639 MODULE_LICENSE("GPL");