xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 6df400ab64c68fc4072a13019fc20fd7e3d51303)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2003 Sistina Software Limited.
31da177e4SLinus Torvalds  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8586e80e6SMikulas Patocka #include <linux/device-mapper.h>
9586e80e6SMikulas Patocka 
101da177e4SLinus Torvalds #include "dm-path-selector.h"
11b15546f9SMike Anderson #include "dm-uevent.h"
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/ctype.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/mempool.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/time.h>
201da177e4SLinus Torvalds #include <linux/workqueue.h>
21cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
221da177e4SLinus Torvalds #include <asm/atomic.h>
231da177e4SLinus Torvalds 
2472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
251da177e4SLinus Torvalds #define MESG_STR(x) x, sizeof(x)
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /* Path properties */
281da177e4SLinus Torvalds struct pgpath {
291da177e4SLinus Torvalds 	struct list_head list;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
326680073dSKiyoshi Ueda 	unsigned is_active;		/* Path status */
331da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
341da177e4SLinus Torvalds 
35c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
36224cb3e9SMike Anderson 	struct work_struct deactivate_path;
37e54f77ddSChandra Seetharaman 	struct work_struct activate_path;
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
441da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds struct priority_group {
471da177e4SLinus Torvalds 	struct list_head list;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
501da177e4SLinus Torvalds 	struct path_selector ps;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
531da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
561da177e4SLinus Torvalds 	struct list_head pgpaths;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Multipath context */
601da177e4SLinus Torvalds struct multipath {
611da177e4SLinus Torvalds 	struct list_head list;
621da177e4SLinus Torvalds 	struct dm_target *ti;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	spinlock_t lock;
651da177e4SLinus Torvalds 
66cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
672bfd2e13SChandra Seetharaman 	char *hw_handler_params;
681da177e4SLinus Torvalds 	unsigned nr_priority_groups;
691da177e4SLinus Torvalds 	struct list_head priority_groups;
701da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
71c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
741da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
751da177e4SLinus Torvalds 	struct priority_group *current_pg;
761da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
771da177e4SLinus Torvalds 	unsigned repeat_count;		/* I/Os left before calling PS again */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	unsigned queue_io;		/* Must we queue all I/O? */
801da177e4SLinus Torvalds 	unsigned queue_if_no_path;	/* Queue I/O if last path fails? */
81436d4108SAlasdair G Kergon 	unsigned saved_queue_if_no_path;/* Saved state during suspension */
82c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
83c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	struct work_struct process_queued_ios;
86f40c67f0SKiyoshi Ueda 	struct list_head queued_ios;
871da177e4SLinus Torvalds 	unsigned queue_size;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	struct work_struct trigger_event;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/*
92028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
931da177e4SLinus Torvalds 	 * can resubmit bios on error.
941da177e4SLinus Torvalds 	 */
951da177e4SLinus Torvalds 	mempool_t *mpio_pool;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Context information attached to each bio we process.
1001da177e4SLinus Torvalds  */
101028867acSAlasdair G Kergon struct dm_mpath_io {
1021da177e4SLinus Torvalds 	struct pgpath *pgpath;
10302ab823fSKiyoshi Ueda 	size_t nr_bytes;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define MIN_IOS 256	/* Mempool size */
1091da177e4SLinus Torvalds 
110e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1111da177e4SLinus Torvalds 
112bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113c4028958SDavid Howells static void process_queued_ios(struct work_struct *work);
114c4028958SDavid Howells static void trigger_event(struct work_struct *work);
115bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
116224cb3e9SMike Anderson static void deactivate_path(struct work_struct *work);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*-----------------------------------------------
1201da177e4SLinus Torvalds  * Allocation routines
1211da177e4SLinus Torvalds  *-----------------------------------------------*/
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1241da177e4SLinus Torvalds {
125e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1261da177e4SLinus Torvalds 
127224cb3e9SMike Anderson 	if (pgpath) {
1286680073dSKiyoshi Ueda 		pgpath->is_active = 1;
129224cb3e9SMike Anderson 		INIT_WORK(&pgpath->deactivate_path, deactivate_path);
130e54f77ddSChandra Seetharaman 		INIT_WORK(&pgpath->activate_path, activate_path);
131224cb3e9SMike Anderson 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	return pgpath;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
136028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	kfree(pgpath);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141224cb3e9SMike Anderson static void deactivate_path(struct work_struct *work)
142224cb3e9SMike Anderson {
143224cb3e9SMike Anderson 	struct pgpath *pgpath =
144224cb3e9SMike Anderson 		container_of(work, struct pgpath, deactivate_path);
145224cb3e9SMike Anderson 
146224cb3e9SMike Anderson 	blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
147224cb3e9SMike Anderson }
148224cb3e9SMike Anderson 
1491da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct priority_group *pg;
1521da177e4SLinus Torvalds 
153e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1541da177e4SLinus Torvalds 
155e69fae56SMicha³ Miros³aw 	if (pg)
1561da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	return pg;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
164ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1671da177e4SLinus Torvalds 		list_del(&pgpath->list);
168ae11b1b3SHannes Reinecke 		if (m->hw_handler_name)
169ae11b1b3SHannes Reinecke 			scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
1701da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1711da177e4SLinus Torvalds 		free_pgpath(pgpath);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1761da177e4SLinus Torvalds 				struct dm_target *ti)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (ps->type) {
1811da177e4SLinus Torvalds 		ps->type->destroy(ps);
1821da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1861da177e4SLinus Torvalds 	kfree(pg);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
18928f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct multipath *m;
1921da177e4SLinus Torvalds 
193e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1941da177e4SLinus Torvalds 	if (m) {
1951da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
196f40c67f0SKiyoshi Ueda 		INIT_LIST_HEAD(&m->queued_ios);
1971da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1981da177e4SLinus Torvalds 		m->queue_io = 1;
199c4028958SDavid Howells 		INIT_WORK(&m->process_queued_ios, process_queued_ios);
200c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
20193d2341cSMatthew Dobson 		m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
2021da177e4SLinus Torvalds 		if (!m->mpio_pool) {
2031da177e4SLinus Torvalds 			kfree(m);
2041da177e4SLinus Torvalds 			return NULL;
2051da177e4SLinus Torvalds 		}
20628f16c20SMicha³ Miros³aw 		m->ti = ti;
20728f16c20SMicha³ Miros³aw 		ti->private = m;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	return m;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2181da177e4SLinus Torvalds 		list_del(&pg->list);
2191da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
222cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2232bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2241da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2251da177e4SLinus Torvalds 	kfree(m);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*-----------------------------------------------
2301da177e4SLinus Torvalds  * Path selection
2311da177e4SLinus Torvalds  *-----------------------------------------------*/
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
238cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
2391da177e4SLinus Torvalds 		m->pg_init_required = 1;
2401da177e4SLinus Torvalds 		m->queue_io = 1;
2411da177e4SLinus Torvalds 	} else {
2421da177e4SLinus Torvalds 		m->pg_init_required = 0;
2431da177e4SLinus Torvalds 		m->queue_io = 0;
2441da177e4SLinus Torvalds 	}
245c9e45581SDave Wysochanski 
246c9e45581SDave Wysochanski 	m->pg_init_count = 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24902ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
25002ab823fSKiyoshi Ueda 			       size_t nr_bytes)
2511da177e4SLinus Torvalds {
252c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
2531da177e4SLinus Torvalds 
25402ab823fSKiyoshi Ueda 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
2551da177e4SLinus Torvalds 	if (!path)
2561da177e4SLinus Torvalds 		return -ENXIO;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (m->current_pg != pg)
2611da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	return 0;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
26602ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct priority_group *pg;
2691da177e4SLinus Torvalds 	unsigned bypassed = 1;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (!m->nr_valid_paths)
2721da177e4SLinus Torvalds 		goto failed;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
2751da177e4SLinus Torvalds 	if (m->next_pg) {
2761da177e4SLinus Torvalds 		pg = m->next_pg;
2771da177e4SLinus Torvalds 		m->next_pg = NULL;
27802ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
2791da177e4SLinus Torvalds 			return;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
28302ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
2841da177e4SLinus Torvalds 		return;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/*
2871da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
2881da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
2891da177e4SLinus Torvalds 	 * Second time we only try the ones we skipped.
2901da177e4SLinus Torvalds 	 */
2911da177e4SLinus Torvalds 	do {
2921da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
2931da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
2941da177e4SLinus Torvalds 				continue;
29502ab823fSKiyoshi Ueda 			if (!__choose_path_in_pg(m, pg, nr_bytes))
2961da177e4SLinus Torvalds 				return;
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 	} while (bypassed--);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds failed:
3011da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3021da177e4SLinus Torvalds 	m->current_pg = NULL;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
30545e15720SKiyoshi Ueda /*
30645e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
30745e15720SKiyoshi Ueda  * than here in the target.
30845e15720SKiyoshi Ueda  *
30945e15720SKiyoshi Ueda  * m->lock must be held on entry.
31045e15720SKiyoshi Ueda  *
31145e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
31245e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
31345e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
31445e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
31545e15720SKiyoshi Ueda  */
31645e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
31745e15720SKiyoshi Ueda {
31845e15720SKiyoshi Ueda 	return (m->queue_if_no_path != m->saved_queue_if_no_path &&
31945e15720SKiyoshi Ueda 		dm_noflush_suspending(m->ti));
32045e15720SKiyoshi Ueda }
32145e15720SKiyoshi Ueda 
322f40c67f0SKiyoshi Ueda static int map_io(struct multipath *m, struct request *clone,
323028867acSAlasdair G Kergon 		  struct dm_mpath_io *mpio, unsigned was_queued)
3241da177e4SLinus Torvalds {
325d2a7ad29SKiyoshi Ueda 	int r = DM_MAPIO_REMAPPED;
326f40c67f0SKiyoshi Ueda 	size_t nr_bytes = blk_rq_bytes(clone);
3271da177e4SLinus Torvalds 	unsigned long flags;
3281da177e4SLinus Torvalds 	struct pgpath *pgpath;
329f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
3341da177e4SLinus Torvalds 	if (!m->current_pgpath ||
3351da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
33602ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (was_queued)
3411da177e4SLinus Torvalds 		m->queue_size--;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
344436d4108SAlasdair G Kergon 	    (!pgpath && m->queue_if_no_path)) {
3451da177e4SLinus Torvalds 		/* Queue for the daemon to resubmit */
346f40c67f0SKiyoshi Ueda 		list_add_tail(&clone->queuelist, &m->queued_ios);
3471da177e4SLinus Torvalds 		m->queue_size++;
348c3cd4f6bSAlasdair G Kergon 		if ((m->pg_init_required && !m->pg_init_in_progress) ||
349c3cd4f6bSAlasdair G Kergon 		    !m->queue_io)
350c557308eSAlasdair G Kergon 			queue_work(kmultipathd, &m->process_queued_ios);
3511da177e4SLinus Torvalds 		pgpath = NULL;
352d2a7ad29SKiyoshi Ueda 		r = DM_MAPIO_SUBMITTED;
353f40c67f0SKiyoshi Ueda 	} else if (pgpath) {
354f40c67f0SKiyoshi Ueda 		bdev = pgpath->path.dev->bdev;
355f40c67f0SKiyoshi Ueda 		clone->q = bdev_get_queue(bdev);
356f40c67f0SKiyoshi Ueda 		clone->rq_disk = bdev->bd_disk;
357f40c67f0SKiyoshi Ueda 	} else if (__must_push_back(m))
35845e15720SKiyoshi Ueda 		r = DM_MAPIO_REQUEUE;
35945e15720SKiyoshi Ueda 	else
36045e15720SKiyoshi Ueda 		r = -EIO;	/* Failed */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	mpio->pgpath = pgpath;
36302ab823fSKiyoshi Ueda 	mpio->nr_bytes = nr_bytes;
36402ab823fSKiyoshi Ueda 
36502ab823fSKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
36602ab823fSKiyoshi Ueda 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
36702ab823fSKiyoshi Ueda 					      nr_bytes);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	return r;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
3761da177e4SLinus Torvalds  */
377485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
378485ef69eSAlasdair G Kergon 			    unsigned save_old_value)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	unsigned long flags;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3831da177e4SLinus Torvalds 
384485ef69eSAlasdair G Kergon 	if (save_old_value)
385436d4108SAlasdair G Kergon 		m->saved_queue_if_no_path = m->queue_if_no_path;
386485ef69eSAlasdair G Kergon 	else
387485ef69eSAlasdair G Kergon 		m->saved_queue_if_no_path = queue_if_no_path;
3881da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
389c3cd4f6bSAlasdair G Kergon 	if (!m->queue_if_no_path && m->queue_size)
390c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return 0;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /*-----------------------------------------------------------------
3981da177e4SLinus Torvalds  * The multipath daemon is responsible for resubmitting queued ios.
3991da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	int r;
4041da177e4SLinus Torvalds 	unsigned long flags;
405028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio;
4061da177e4SLinus Torvalds 	union map_info *info;
407f40c67f0SKiyoshi Ueda 	struct request *clone, *n;
408f40c67f0SKiyoshi Ueda 	LIST_HEAD(cl);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
411f40c67f0SKiyoshi Ueda 	list_splice_init(&m->queued_ios, &cl);
4121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4131da177e4SLinus Torvalds 
414f40c67f0SKiyoshi Ueda 	list_for_each_entry_safe(clone, n, &cl, queuelist) {
415f40c67f0SKiyoshi Ueda 		list_del_init(&clone->queuelist);
4161da177e4SLinus Torvalds 
417f40c67f0SKiyoshi Ueda 		info = dm_get_rq_mapinfo(clone);
4181da177e4SLinus Torvalds 		mpio = info->ptr;
4191da177e4SLinus Torvalds 
420f40c67f0SKiyoshi Ueda 		r = map_io(m, clone, mpio, 1);
421f40c67f0SKiyoshi Ueda 		if (r < 0) {
422f40c67f0SKiyoshi Ueda 			mempool_free(mpio, m->mpio_pool);
423f40c67f0SKiyoshi Ueda 			dm_kill_unmapped_request(clone, r);
424f40c67f0SKiyoshi Ueda 		} else if (r == DM_MAPIO_REMAPPED)
425f40c67f0SKiyoshi Ueda 			dm_dispatch_request(clone);
426f40c67f0SKiyoshi Ueda 		else if (r == DM_MAPIO_REQUEUE) {
427f40c67f0SKiyoshi Ueda 			mempool_free(mpio, m->mpio_pool);
428f40c67f0SKiyoshi Ueda 			dm_requeue_unmapped_request(clone);
429f40c67f0SKiyoshi Ueda 		}
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433c4028958SDavid Howells static void process_queued_ios(struct work_struct *work)
4341da177e4SLinus Torvalds {
435c4028958SDavid Howells 	struct multipath *m =
436c4028958SDavid Howells 		container_of(work, struct multipath, process_queued_ios);
437e54f77ddSChandra Seetharaman 	struct pgpath *pgpath = NULL, *tmp;
438e54f77ddSChandra Seetharaman 	unsigned must_queue = 1;
4391da177e4SLinus Torvalds 	unsigned long flags;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4421da177e4SLinus Torvalds 
443c3cd4f6bSAlasdair G Kergon 	if (!m->queue_size)
444c3cd4f6bSAlasdair G Kergon 		goto out;
445c3cd4f6bSAlasdair G Kergon 
4461da177e4SLinus Torvalds 	if (!m->current_pgpath)
44702ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
4501da177e4SLinus Torvalds 
451c3cd4f6bSAlasdair G Kergon 	if ((pgpath && !m->queue_io) ||
452c3cd4f6bSAlasdair G Kergon 	    (!pgpath && !m->queue_if_no_path))
453c3cd4f6bSAlasdair G Kergon 		must_queue = 0;
4541da177e4SLinus Torvalds 
455b81aa1c7SChandra Seetharaman 	if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
456c9e45581SDave Wysochanski 		m->pg_init_count++;
4571da177e4SLinus Torvalds 		m->pg_init_required = 0;
458e54f77ddSChandra Seetharaman 		list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
459e54f77ddSChandra Seetharaman 			if (queue_work(kmpath_handlerd, &tmp->activate_path))
460e54f77ddSChandra Seetharaman 				m->pg_init_in_progress++;
461c3cd4f6bSAlasdair G Kergon 		}
462e54f77ddSChandra Seetharaman 	}
463c3cd4f6bSAlasdair G Kergon out:
4641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4651da177e4SLinus Torvalds 	if (!must_queue)
4661da177e4SLinus Torvalds 		dispatch_queued_ios(m);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4711da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4721da177e4SLinus Torvalds  */
473c4028958SDavid Howells static void trigger_event(struct work_struct *work)
4741da177e4SLinus Torvalds {
475c4028958SDavid Howells 	struct multipath *m =
476c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds /*-----------------------------------------------------------------
4821da177e4SLinus Torvalds  * Constructor/argument parsing:
4831da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
4841da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
4851da177e4SLinus Torvalds  * <#priority groups>
4861da177e4SLinus Torvalds  * <initial priority group>
4871da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
4881da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
4891da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
4901da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4911da177e4SLinus Torvalds struct param {
4921da177e4SLinus Torvalds 	unsigned min;
4931da177e4SLinus Torvalds 	unsigned max;
4941da177e4SLinus Torvalds 	char *error;
4951da177e4SLinus Torvalds };
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds static int read_param(struct param *param, char *str, unsigned *v, char **error)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	if (!str ||
5001da177e4SLinus Torvalds 	    (sscanf(str, "%u", v) != 1) ||
5011da177e4SLinus Torvalds 	    (*v < param->min) ||
5021da177e4SLinus Torvalds 	    (*v > param->max)) {
5031da177e4SLinus Torvalds 		*error = param->error;
5041da177e4SLinus Torvalds 		return -EINVAL;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	return 0;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds struct arg_set {
5111da177e4SLinus Torvalds 	unsigned argc;
5121da177e4SLinus Torvalds 	char **argv;
5131da177e4SLinus Torvalds };
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static char *shift(struct arg_set *as)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	char *r;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	if (as->argc) {
5201da177e4SLinus Torvalds 		as->argc--;
5211da177e4SLinus Torvalds 		r = *as->argv;
5221da177e4SLinus Torvalds 		as->argv++;
5231da177e4SLinus Torvalds 		return r;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	return NULL;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static void consume(struct arg_set *as, unsigned n)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	BUG_ON (as->argc < n);
5321da177e4SLinus Torvalds 	as->argc -= n;
5331da177e4SLinus Torvalds 	as->argv += n;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
5371da177e4SLinus Torvalds 			       struct dm_target *ti)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	int r;
5401da177e4SLinus Torvalds 	struct path_selector_type *pst;
5411da177e4SLinus Torvalds 	unsigned ps_argc;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	static struct param _params[] = {
54472d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
5451da177e4SLinus Torvalds 	};
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	pst = dm_get_path_selector(shift(as));
5481da177e4SLinus Torvalds 	if (!pst) {
54972d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
5501da177e4SLinus Torvalds 		return -EINVAL;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &ps_argc, &ti->error);
554371b2e34SMikulas Patocka 	if (r) {
555371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5561da177e4SLinus Torvalds 		return -EINVAL;
557371b2e34SMikulas Patocka 	}
5581da177e4SLinus Torvalds 
5590e0497c0SMikulas Patocka 	if (ps_argc > as->argc) {
5600e0497c0SMikulas Patocka 		dm_put_path_selector(pst);
5610e0497c0SMikulas Patocka 		ti->error = "not enough arguments for path selector";
5620e0497c0SMikulas Patocka 		return -EINVAL;
5630e0497c0SMikulas Patocka 	}
5640e0497c0SMikulas Patocka 
5651da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5661da177e4SLinus Torvalds 	if (r) {
5671da177e4SLinus Torvalds 		dm_put_path_selector(pst);
56872d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5691da177e4SLinus Torvalds 		return r;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	pg->ps.type = pst;
5731da177e4SLinus Torvalds 	consume(as, ps_argc);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
5791da177e4SLinus Torvalds 			       struct dm_target *ti)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	int r;
5821da177e4SLinus Torvalds 	struct pgpath *p;
583ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/* we need at least a path arg */
5861da177e4SLinus Torvalds 	if (as->argc < 1) {
58772d94861SAlasdair G Kergon 		ti->error = "no device given";
58801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	p = alloc_pgpath();
5921da177e4SLinus Torvalds 	if (!p)
59301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	r = dm_get_device(ti, shift(as), ti->begin, ti->len,
5961da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &p->path.dev);
5971da177e4SLinus Torvalds 	if (r) {
59872d94861SAlasdair G Kergon 		ti->error = "error getting device";
5991da177e4SLinus Torvalds 		goto bad;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 
602ae11b1b3SHannes Reinecke 	if (m->hw_handler_name) {
603a0cf7ea9SHannes Reinecke 		struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
604a0cf7ea9SHannes Reinecke 
605a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
606a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
607a0cf7ea9SHannes Reinecke 			/*
608a0cf7ea9SHannes Reinecke 			 * Already attached to different hw_handler,
609a0cf7ea9SHannes Reinecke 			 * try to reattach with correct one.
610a0cf7ea9SHannes Reinecke 			 */
611a0cf7ea9SHannes Reinecke 			scsi_dh_detach(q);
612a0cf7ea9SHannes Reinecke 			r = scsi_dh_attach(q, m->hw_handler_name);
613a0cf7ea9SHannes Reinecke 		}
614a0cf7ea9SHannes Reinecke 
615ae11b1b3SHannes Reinecke 		if (r < 0) {
616a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
617ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
618ae11b1b3SHannes Reinecke 			goto bad;
619ae11b1b3SHannes Reinecke 		}
6202bfd2e13SChandra Seetharaman 
6212bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
6222bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
6232bfd2e13SChandra Seetharaman 			if (r < 0) {
6242bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
6252bfd2e13SChandra Seetharaman 							"handler parameters";
6262bfd2e13SChandra Seetharaman 				scsi_dh_detach(q);
6272bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
6282bfd2e13SChandra Seetharaman 				goto bad;
6292bfd2e13SChandra Seetharaman 			}
6302bfd2e13SChandra Seetharaman 		}
631ae11b1b3SHannes Reinecke 	}
632ae11b1b3SHannes Reinecke 
6331da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6341da177e4SLinus Torvalds 	if (r) {
6351da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6361da177e4SLinus Torvalds 		goto bad;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	return p;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds  bad:
6421da177e4SLinus Torvalds 	free_pgpath(p);
64301460f35SBenjamin Marzinski 	return ERR_PTR(r);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds static struct priority_group *parse_priority_group(struct arg_set *as,
64728f16c20SMicha³ Miros³aw 						   struct multipath *m)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	static struct param _params[] = {
65072d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
65172d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6521da177e4SLinus Torvalds 	};
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	int r;
6551da177e4SLinus Torvalds 	unsigned i, nr_selector_args, nr_params;
6561da177e4SLinus Torvalds 	struct priority_group *pg;
65728f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (as->argc < 2) {
6601da177e4SLinus Torvalds 		as->argc = 0;
66101460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
66201460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	pg = alloc_priority_group();
6661da177e4SLinus Torvalds 	if (!pg) {
66772d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
66801460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	pg->m = m;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
6731da177e4SLinus Torvalds 	if (r)
6741da177e4SLinus Torvalds 		goto bad;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	/*
6771da177e4SLinus Torvalds 	 * read the paths
6781da177e4SLinus Torvalds 	 */
6791da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
6801da177e4SLinus Torvalds 	if (r)
6811da177e4SLinus Torvalds 		goto bad;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
6841da177e4SLinus Torvalds 	if (r)
6851da177e4SLinus Torvalds 		goto bad;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	nr_params = 1 + nr_selector_args;
6881da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
6891da177e4SLinus Torvalds 		struct pgpath *pgpath;
6901da177e4SLinus Torvalds 		struct arg_set path_args;
6911da177e4SLinus Torvalds 
692148acff6SMikulas Patocka 		if (as->argc < nr_params) {
693148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
6941da177e4SLinus Torvalds 			goto bad;
695148acff6SMikulas Patocka 		}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		path_args.argc = nr_params;
6981da177e4SLinus Torvalds 		path_args.argv = as->argv;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
70101460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
70201460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
7031da177e4SLinus Torvalds 			goto bad;
70401460f35SBenjamin Marzinski 		}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 		pgpath->pg = pg;
7071da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
7081da177e4SLinus Torvalds 		consume(as, nr_params);
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	return pg;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds  bad:
7141da177e4SLinus Torvalds 	free_priority_group(pg, ti);
71501460f35SBenjamin Marzinski 	return ERR_PTR(r);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
71828f16c20SMicha³ Miros³aw static int parse_hw_handler(struct arg_set *as, struct multipath *m)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	unsigned hw_argc;
7212bfd2e13SChandra Seetharaman 	int ret;
72228f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	static struct param _params[] = {
72572d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
7261da177e4SLinus Torvalds 	};
7271da177e4SLinus Torvalds 
728cfae5c9bSChandra Seetharaman 	if (read_param(_params, shift(as), &hw_argc, &ti->error))
7291da177e4SLinus Torvalds 		return -EINVAL;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	if (!hw_argc)
7321da177e4SLinus Torvalds 		return 0;
7331da177e4SLinus Torvalds 
734e094f4f1SMikulas Patocka 	if (hw_argc > as->argc) {
735e094f4f1SMikulas Patocka 		ti->error = "not enough arguments for hardware handler";
736e094f4f1SMikulas Patocka 		return -EINVAL;
737e094f4f1SMikulas Patocka 	}
738e094f4f1SMikulas Patocka 
739cfae5c9bSChandra Seetharaman 	m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
740cfae5c9bSChandra Seetharaman 	request_module("scsi_dh_%s", m->hw_handler_name);
741cfae5c9bSChandra Seetharaman 	if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
74272d94861SAlasdair G Kergon 		ti->error = "unknown hardware handler type";
7432bfd2e13SChandra Seetharaman 		ret = -EINVAL;
7442bfd2e13SChandra Seetharaman 		goto fail;
7451da177e4SLinus Torvalds 	}
74614e98c5cSChandra Seetharaman 
7472bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
7482bfd2e13SChandra Seetharaman 		char *p;
7492bfd2e13SChandra Seetharaman 		int i, j, len = 4;
7502bfd2e13SChandra Seetharaman 
7512bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
7522bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
7532bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
7542bfd2e13SChandra Seetharaman 		if (!p) {
7552bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
7562bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
7572bfd2e13SChandra Seetharaman 			goto fail;
7582bfd2e13SChandra Seetharaman 		}
7592bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
7602bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
7612bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
7622bfd2e13SChandra Seetharaman 	}
7631da177e4SLinus Torvalds 	consume(as, hw_argc - 1);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	return 0;
7662bfd2e13SChandra Seetharaman fail:
7672bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
7682bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
7692bfd2e13SChandra Seetharaman 	return ret;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
77228f16c20SMicha³ Miros³aw static int parse_features(struct arg_set *as, struct multipath *m)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	int r;
7751da177e4SLinus Torvalds 	unsigned argc;
77628f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
777c9e45581SDave Wysochanski 	const char *param_name;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	static struct param _params[] = {
780c9e45581SDave Wysochanski 		{0, 3, "invalid number of feature args"},
781c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
7821da177e4SLinus Torvalds 	};
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &argc, &ti->error);
7851da177e4SLinus Torvalds 	if (r)
7861da177e4SLinus Torvalds 		return -EINVAL;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (!argc)
7891da177e4SLinus Torvalds 		return 0;
7901da177e4SLinus Torvalds 
791c9e45581SDave Wysochanski 	do {
792c9e45581SDave Wysochanski 		param_name = shift(as);
793c9e45581SDave Wysochanski 		argc--;
794c9e45581SDave Wysochanski 
795c9e45581SDave Wysochanski 		if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
796c9e45581SDave Wysochanski 			r = queue_if_no_path(m, 1, 0);
797c9e45581SDave Wysochanski 			continue;
7981da177e4SLinus Torvalds 		}
799c9e45581SDave Wysochanski 
800c9e45581SDave Wysochanski 		if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
801c9e45581SDave Wysochanski 		    (argc >= 1)) {
802c9e45581SDave Wysochanski 			r = read_param(_params + 1, shift(as),
803c9e45581SDave Wysochanski 				       &m->pg_init_retries, &ti->error);
804c9e45581SDave Wysochanski 			argc--;
805c9e45581SDave Wysochanski 			continue;
806c9e45581SDave Wysochanski 		}
807c9e45581SDave Wysochanski 
808c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
809c9e45581SDave Wysochanski 		r = -EINVAL;
810c9e45581SDave Wysochanski 	} while (argc && !r);
811c9e45581SDave Wysochanski 
812c9e45581SDave Wysochanski 	return r;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
8161da177e4SLinus Torvalds 			 char **argv)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	/* target parameters */
8191da177e4SLinus Torvalds 	static struct param _params[] = {
82072d94861SAlasdair G Kergon 		{1, 1024, "invalid number of priority groups"},
82172d94861SAlasdair G Kergon 		{1, 1024, "invalid initial priority group number"},
8221da177e4SLinus Torvalds 	};
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	int r;
8251da177e4SLinus Torvalds 	struct multipath *m;
8261da177e4SLinus Torvalds 	struct arg_set as;
8271da177e4SLinus Torvalds 	unsigned pg_count = 0;
8281da177e4SLinus Torvalds 	unsigned next_pg_num;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	as.argc = argc;
8311da177e4SLinus Torvalds 	as.argv = argv;
8321da177e4SLinus Torvalds 
83328f16c20SMicha³ Miros³aw 	m = alloc_multipath(ti);
8341da177e4SLinus Torvalds 	if (!m) {
83572d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
8361da177e4SLinus Torvalds 		return -EINVAL;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
83928f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
8401da177e4SLinus Torvalds 	if (r)
8411da177e4SLinus Torvalds 		goto bad;
8421da177e4SLinus Torvalds 
84328f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
8441da177e4SLinus Torvalds 	if (r)
8451da177e4SLinus Torvalds 		goto bad;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
8481da177e4SLinus Torvalds 	if (r)
8491da177e4SLinus Torvalds 		goto bad;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
8521da177e4SLinus Torvalds 	if (r)
8531da177e4SLinus Torvalds 		goto bad;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/* parse the priority groups */
8561da177e4SLinus Torvalds 	while (as.argc) {
8571da177e4SLinus Torvalds 		struct priority_group *pg;
8581da177e4SLinus Torvalds 
85928f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
86001460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
86101460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
8621da177e4SLinus Torvalds 			goto bad;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
8661da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
8671da177e4SLinus Torvalds 		pg_count++;
8681da177e4SLinus Torvalds 		pg->pg_num = pg_count;
8691da177e4SLinus Torvalds 		if (!--next_pg_num)
8701da177e4SLinus Torvalds 			m->next_pg = pg;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
87472d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
8751da177e4SLinus Torvalds 		r = -EINVAL;
8761da177e4SLinus Torvalds 		goto bad;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
8798627921fSMikulas Patocka 	ti->num_flush_requests = 1;
8808627921fSMikulas Patocka 
8811da177e4SLinus Torvalds 	return 0;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds  bad:
8841da177e4SLinus Torvalds 	free_multipath(m);
8851da177e4SLinus Torvalds 	return r;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
888*6df400abSKiyoshi Ueda static void flush_multipath_work(void)
8891da177e4SLinus Torvalds {
890bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
891a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
89253b351f9SMikulas Patocka 	flush_scheduled_work();
893*6df400abSKiyoshi Ueda }
894*6df400abSKiyoshi Ueda 
895*6df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
896*6df400abSKiyoshi Ueda {
897*6df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
898*6df400abSKiyoshi Ueda 
899*6df400abSKiyoshi Ueda 	flush_multipath_work();
9001da177e4SLinus Torvalds 	free_multipath(m);
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /*
904f40c67f0SKiyoshi Ueda  * Map cloned requests
9051da177e4SLinus Torvalds  */
906f40c67f0SKiyoshi Ueda static int multipath_map(struct dm_target *ti, struct request *clone,
9071da177e4SLinus Torvalds 			 union map_info *map_context)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	int r;
910028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio;
9111da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
9121da177e4SLinus Torvalds 
913f40c67f0SKiyoshi Ueda 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
914f40c67f0SKiyoshi Ueda 	if (!mpio)
915f40c67f0SKiyoshi Ueda 		/* ENOMEM, requeue */
916f40c67f0SKiyoshi Ueda 		return DM_MAPIO_REQUEUE;
917f40c67f0SKiyoshi Ueda 	memset(mpio, 0, sizeof(*mpio));
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	map_context->ptr = mpio;
920f40c67f0SKiyoshi Ueda 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
921f40c67f0SKiyoshi Ueda 	r = map_io(m, clone, mpio, 0);
92245e15720SKiyoshi Ueda 	if (r < 0 || r == DM_MAPIO_REQUEUE)
9231da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	return r;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds /*
9291da177e4SLinus Torvalds  * Take a path out of use.
9301da177e4SLinus Torvalds  */
9311da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	unsigned long flags;
9341da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9371da177e4SLinus Torvalds 
9386680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9391da177e4SLinus Torvalds 		goto out;
9401da177e4SLinus Torvalds 
94172d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9446680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9451da177e4SLinus Torvalds 	pgpath->fail_count++;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	m->nr_valid_paths--;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
9501da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9511da177e4SLinus Torvalds 
952b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
953b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
954b15546f9SMike Anderson 
955fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
956224cb3e9SMike Anderson 	queue_work(kmultipathd, &pgpath->deactivate_path);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds out:
9591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	return 0;
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds /*
9651da177e4SLinus Torvalds  * Reinstate a previously-failed path
9661da177e4SLinus Torvalds  */
9671da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	int r = 0;
9701da177e4SLinus Torvalds 	unsigned long flags;
9711da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9741da177e4SLinus Torvalds 
9756680073dSKiyoshi Ueda 	if (pgpath->is_active)
9761da177e4SLinus Torvalds 		goto out;
9771da177e4SLinus Torvalds 
978def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
9791da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
9801da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
9811da177e4SLinus Torvalds 		r = -EINVAL;
9821da177e4SLinus Torvalds 		goto out;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
9861da177e4SLinus Torvalds 	if (r)
9871da177e4SLinus Torvalds 		goto out;
9881da177e4SLinus Torvalds 
9896680073dSKiyoshi Ueda 	pgpath->is_active = 1;
9901da177e4SLinus Torvalds 
991e54f77ddSChandra Seetharaman 	if (!m->nr_valid_paths++ && m->queue_size) {
9921da177e4SLinus Torvalds 		m->current_pgpath = NULL;
993c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
994e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
995e54f77ddSChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path))
996e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
997e54f77ddSChandra Seetharaman 	}
9981da177e4SLinus Torvalds 
999b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1000b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1001b15546f9SMike Anderson 
1002fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds out:
10051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	return r;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds /*
10111da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10121da177e4SLinus Torvalds  */
10131da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10141da177e4SLinus Torvalds 		      action_fn action)
10151da177e4SLinus Torvalds {
10161da177e4SLinus Torvalds 	int r = 0;
10171da177e4SLinus Torvalds 	struct pgpath *pgpath;
10181da177e4SLinus Torvalds 	struct priority_group *pg;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10211da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10221da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10231da177e4SLinus Torvalds 				r = action(pgpath);
10241da177e4SLinus Torvalds 		}
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	return r;
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds /*
10311da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10341da177e4SLinus Torvalds 		      int bypassed)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	unsigned long flags;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10411da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10421da177e4SLinus Torvalds 	m->current_pg = NULL;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10451da177e4SLinus Torvalds 
1046fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
10511da177e4SLinus Torvalds  */
10521da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	struct priority_group *pg;
10551da177e4SLinus Torvalds 	unsigned pgnum;
10561da177e4SLinus Torvalds 	unsigned long flags;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
10591da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10601da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
10611da177e4SLinus Torvalds 		return -EINVAL;
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10651da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10661da177e4SLinus Torvalds 		pg->bypassed = 0;
10671da177e4SLinus Torvalds 		if (--pgnum)
10681da177e4SLinus Torvalds 			continue;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10711da177e4SLinus Torvalds 		m->current_pg = NULL;
10721da177e4SLinus Torvalds 		m->next_pg = pg;
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10751da177e4SLinus Torvalds 
1076fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10771da177e4SLinus Torvalds 	return 0;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
10821da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
10831da177e4SLinus Torvalds  */
10841da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	struct priority_group *pg;
10871da177e4SLinus Torvalds 	unsigned pgnum;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
10901da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10911da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
10921da177e4SLinus Torvalds 		return -EINVAL;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10961da177e4SLinus Torvalds 		if (!--pgnum)
10971da177e4SLinus Torvalds 			break;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
11011da177e4SLinus Torvalds 	return 0;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds /*
1105c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1106c9e45581SDave Wysochanski  */
1107c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1108c9e45581SDave Wysochanski {
1109c9e45581SDave Wysochanski 	unsigned long flags;
1110c9e45581SDave Wysochanski 	int limit_reached = 0;
1111c9e45581SDave Wysochanski 
1112c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1113c9e45581SDave Wysochanski 
1114c9e45581SDave Wysochanski 	if (m->pg_init_count <= m->pg_init_retries)
1115c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1116c9e45581SDave Wysochanski 	else
1117c9e45581SDave Wysochanski 		limit_reached = 1;
1118c9e45581SDave Wysochanski 
1119c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1120c9e45581SDave Wysochanski 
1121c9e45581SDave Wysochanski 	return limit_reached;
1122c9e45581SDave Wysochanski }
1123c9e45581SDave Wysochanski 
11243ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1125cfae5c9bSChandra Seetharaman {
11263ae31f6aSChandra Seetharaman 	struct dm_path *path = data;
1127cfae5c9bSChandra Seetharaman 	struct pgpath *pgpath = path_to_pgpath(path);
1128cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1129cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1130cfae5c9bSChandra Seetharaman 	unsigned long flags;
1131cfae5c9bSChandra Seetharaman 
1132cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1133cfae5c9bSChandra Seetharaman 	switch (errors) {
1134cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1135cfae5c9bSChandra Seetharaman 		break;
1136cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1137cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1138cfae5c9bSChandra Seetharaman 			errors = 0;
1139cfae5c9bSChandra Seetharaman 			break;
1140cfae5c9bSChandra Seetharaman 		}
1141cfae5c9bSChandra Seetharaman 		DMERR("Cannot failover device because scsi_dh_%s was not "
1142cfae5c9bSChandra Seetharaman 		      "loaded.", m->hw_handler_name);
1143cfae5c9bSChandra Seetharaman 		/*
1144cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1145cfae5c9bSChandra Seetharaman 		 */
1146cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1147cfae5c9bSChandra Seetharaman 		break;
1148cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1149cfae5c9bSChandra Seetharaman 		/*
1150cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1151cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1152cfae5c9bSChandra Seetharaman 		 */
1153cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1154cfae5c9bSChandra Seetharaman 		break;
1155cfae5c9bSChandra Seetharaman 	/* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1156cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
1157cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1158cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1159cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1160cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1161cfae5c9bSChandra Seetharaman 		errors = 0;
1162cfae5c9bSChandra Seetharaman 		break;
1163cfae5c9bSChandra Seetharaman 	default:
1164cfae5c9bSChandra Seetharaman 		/*
1165cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1166cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1167cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1168cfae5c9bSChandra Seetharaman 		 */
1169cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1170cfae5c9bSChandra Seetharaman 	}
1171cfae5c9bSChandra Seetharaman 
1172cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1173cfae5c9bSChandra Seetharaman 	if (errors) {
1174e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1175cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1176cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1177cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1178e54f77ddSChandra Seetharaman 		}
1179cfae5c9bSChandra Seetharaman 	} else if (!m->pg_init_required) {
1180cfae5c9bSChandra Seetharaman 		m->queue_io = 0;
1181cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1182cfae5c9bSChandra Seetharaman 	}
1183cfae5c9bSChandra Seetharaman 
1184e54f77ddSChandra Seetharaman 	m->pg_init_in_progress--;
1185e54f77ddSChandra Seetharaman 	if (!m->pg_init_in_progress)
1186cfae5c9bSChandra Seetharaman 		queue_work(kmultipathd, &m->process_queued_ios);
1187cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1188cfae5c9bSChandra Seetharaman }
1189cfae5c9bSChandra Seetharaman 
1190bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1191bab7cfc7SChandra Seetharaman {
1192e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
1193e54f77ddSChandra Seetharaman 		container_of(work, struct pgpath, activate_path);
1194bab7cfc7SChandra Seetharaman 
11953ae31f6aSChandra Seetharaman 	scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
11963ae31f6aSChandra Seetharaman 				pg_init_done, &pgpath->path);
1197bab7cfc7SChandra Seetharaman }
1198bab7cfc7SChandra Seetharaman 
11991da177e4SLinus Torvalds /*
12001da177e4SLinus Torvalds  * end_io handling
12011da177e4SLinus Torvalds  */
1202f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1203028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12041da177e4SLinus Torvalds {
1205f40c67f0SKiyoshi Ueda 	/*
1206f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1207f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1208f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1209f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1210f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1211f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1212f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1213f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1214f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1215f40c67f0SKiyoshi Ueda 	 */
1216f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1217640eb3b0SStefan Bader 	unsigned long flags;
12181da177e4SLinus Torvalds 
1219f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
12201da177e4SLinus Torvalds 		return 0;	/* I/O complete */
12211da177e4SLinus Torvalds 
1222f6a80ea8SAlasdair G Kergon 	if (error == -EOPNOTSUPP)
1223f6a80ea8SAlasdair G Kergon 		return error;
1224f6a80ea8SAlasdair G Kergon 
1225cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
12261da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
12271da177e4SLinus Torvalds 
1228640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1229f40c67f0SKiyoshi Ueda 	if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1230f40c67f0SKiyoshi Ueda 		r = -EIO;
1231640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
12321da177e4SLinus Torvalds 
1233f40c67f0SKiyoshi Ueda 	return r;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
1236f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
12371da177e4SLinus Torvalds 			    int error, union map_info *map_context)
12381da177e4SLinus Torvalds {
1239028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1240028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
12411da177e4SLinus Torvalds 	struct pgpath *pgpath = mpio->pgpath;
12421da177e4SLinus Torvalds 	struct path_selector *ps;
12431da177e4SLinus Torvalds 	int r;
12441da177e4SLinus Torvalds 
1245f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
12461da177e4SLinus Torvalds 	if (pgpath) {
12471da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
12481da177e4SLinus Torvalds 		if (ps->type->end_io)
124902ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds 	mempool_free(mpio, m->mpio_pool);
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	return r;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*
12571da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1258436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1259436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1260436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
12611da177e4SLinus Torvalds  */
12621da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12651da177e4SLinus Torvalds 
1266485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
1269*6df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
1270*6df400abSKiyoshi Ueda {
1271*6df400abSKiyoshi Ueda 	flush_multipath_work();
1272*6df400abSKiyoshi Ueda }
1273*6df400abSKiyoshi Ueda 
1274436d4108SAlasdair G Kergon /*
1275436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1276436d4108SAlasdair G Kergon  */
12771da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
12781da177e4SLinus Torvalds {
12791da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12801da177e4SLinus Torvalds 	unsigned long flags;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1283436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
12841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds /*
12881da177e4SLinus Torvalds  * Info output has the following format:
12891da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
12901da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
12911da177e4SLinus Torvalds  * num_groups init_group_number
12921da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
12931da177e4SLinus Torvalds  *             num_paths num_selector_args
12941da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
12951da177e4SLinus Torvalds  *
12961da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
12971da177e4SLinus Torvalds  * num_feature_args [features_args]*
12981da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
12991da177e4SLinus Torvalds  * num_groups init_group_number
13001da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
13011da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
13021da177e4SLinus Torvalds  */
13031da177e4SLinus Torvalds static int multipath_status(struct dm_target *ti, status_type_t type,
13041da177e4SLinus Torvalds 			    char *result, unsigned int maxlen)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	int sz = 0;
13071da177e4SLinus Torvalds 	unsigned long flags;
13081da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13091da177e4SLinus Torvalds 	struct priority_group *pg;
13101da177e4SLinus Torvalds 	struct pgpath *p;
13111da177e4SLinus Torvalds 	unsigned pg_num;
13121da177e4SLinus Torvalds 	char state;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	/* Features */
13171da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1318c9e45581SDave Wysochanski 		DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1319c9e45581SDave Wysochanski 	else {
1320c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
1321c9e45581SDave Wysochanski 			      (m->pg_init_retries > 0) * 2);
1322c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1323c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1324c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1325c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1326c9e45581SDave Wysochanski 	}
13271da177e4SLinus Torvalds 
1328cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
13291da177e4SLinus Torvalds 		DMEMIT("0 ");
13301da177e4SLinus Torvalds 	else
1331cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	if (m->next_pg)
13361da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
13371da177e4SLinus Torvalds 	else if (m->current_pg)
13381da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
13391da177e4SLinus Torvalds 	else
13401da177e4SLinus Torvalds 			pg_num = 1;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	switch (type) {
13451da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
13461da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
13471da177e4SLinus Torvalds 			if (pg->bypassed)
13481da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
13491da177e4SLinus Torvalds 			else if (pg == m->current_pg)
13501da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
13511da177e4SLinus Torvalds 			else
13521da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 			DMEMIT("%c ", state);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 			if (pg->ps.type->status)
13571da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
13581da177e4SLinus Torvalds 							  result + sz,
13591da177e4SLinus Torvalds 							  maxlen - sz);
13601da177e4SLinus Torvalds 			else
13611da177e4SLinus Torvalds 				DMEMIT("0 ");
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
13641da177e4SLinus Torvalds 			       pg->ps.type->info_args);
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
13671da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
13686680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
13691da177e4SLinus Torvalds 				       p->fail_count);
13701da177e4SLinus Torvalds 				if (pg->ps.type->status)
13711da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
13721da177e4SLinus Torvalds 					      &p->path, type, result + sz,
13731da177e4SLinus Torvalds 					      maxlen - sz);
13741da177e4SLinus Torvalds 			}
13751da177e4SLinus Torvalds 		}
13761da177e4SLinus Torvalds 		break;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
13791da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
13801da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 			if (pg->ps.type->status)
13831da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
13841da177e4SLinus Torvalds 							  result + sz,
13851da177e4SLinus Torvalds 							  maxlen - sz);
13861da177e4SLinus Torvalds 			else
13871da177e4SLinus Torvalds 				DMEMIT("0 ");
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
13901da177e4SLinus Torvalds 			       pg->ps.type->table_args);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
13931da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
13941da177e4SLinus Torvalds 				if (pg->ps.type->status)
13951da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
13961da177e4SLinus Torvalds 					      &p->path, type, result + sz,
13971da177e4SLinus Torvalds 					      maxlen - sz);
13981da177e4SLinus Torvalds 			}
13991da177e4SLinus Torvalds 		}
14001da177e4SLinus Torvalds 		break;
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	return 0;
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
14091da177e4SLinus Torvalds {
14101da177e4SLinus Torvalds 	int r;
14111da177e4SLinus Torvalds 	struct dm_dev *dev;
14121da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14131da177e4SLinus Torvalds 	action_fn action;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	if (argc == 1) {
14161da177e4SLinus Torvalds 		if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1417485ef69eSAlasdair G Kergon 			return queue_if_no_path(m, 1, 0);
14181da177e4SLinus Torvalds 		else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1419485ef69eSAlasdair G Kergon 			return queue_if_no_path(m, 0, 0);
14201da177e4SLinus Torvalds 	}
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	if (argc != 2)
14231da177e4SLinus Torvalds 		goto error;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	if (!strnicmp(argv[0], MESG_STR("disable_group")))
14261da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 1);
14271da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("enable_group")))
14281da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 0);
14291da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("switch_group")))
14301da177e4SLinus Torvalds 		return switch_pg_num(m, argv[1]);
14311da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
14321da177e4SLinus Torvalds 		action = reinstate_path;
14331da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("fail_path")))
14341da177e4SLinus Torvalds 		action = fail_path;
14351da177e4SLinus Torvalds 	else
14361da177e4SLinus Torvalds 		goto error;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	r = dm_get_device(ti, argv[1], ti->begin, ti->len,
14391da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &dev);
14401da177e4SLinus Torvalds 	if (r) {
144172d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
14421da177e4SLinus Torvalds 		       argv[1]);
14431da177e4SLinus Torvalds 		return -EINVAL;
14441da177e4SLinus Torvalds 	}
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	dm_put_device(ti, dev);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	return r;
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds error:
14531da177e4SLinus Torvalds 	DMWARN("Unrecognised multipath message received.");
14541da177e4SLinus Torvalds 	return -EINVAL;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
1457647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
14589af4aa30SMilan Broz 			   unsigned long arg)
14599af4aa30SMilan Broz {
14609af4aa30SMilan Broz 	struct multipath *m = (struct multipath *) ti->private;
14619af4aa30SMilan Broz 	struct block_device *bdev = NULL;
1462633a08b8SAl Viro 	fmode_t mode = 0;
14639af4aa30SMilan Broz 	unsigned long flags;
14649af4aa30SMilan Broz 	int r = 0;
14659af4aa30SMilan Broz 
14669af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
14679af4aa30SMilan Broz 
14689af4aa30SMilan Broz 	if (!m->current_pgpath)
146902ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
14709af4aa30SMilan Broz 
1471e90dae1fSMilan Broz 	if (m->current_pgpath) {
14729af4aa30SMilan Broz 		bdev = m->current_pgpath->path.dev->bdev;
1473633a08b8SAl Viro 		mode = m->current_pgpath->path.dev->mode;
1474e90dae1fSMilan Broz 	}
14759af4aa30SMilan Broz 
14769af4aa30SMilan Broz 	if (m->queue_io)
14779af4aa30SMilan Broz 		r = -EAGAIN;
14789af4aa30SMilan Broz 	else if (!bdev)
14799af4aa30SMilan Broz 		r = -EIO;
14809af4aa30SMilan Broz 
14819af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
14829af4aa30SMilan Broz 
1483633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
14849af4aa30SMilan Broz }
14859af4aa30SMilan Broz 
1486af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1487af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1488af4874e0SMike Snitzer {
1489af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1490af4874e0SMike Snitzer 	struct priority_group *pg;
1491af4874e0SMike Snitzer 	struct pgpath *p;
1492af4874e0SMike Snitzer 	int ret = 0;
1493af4874e0SMike Snitzer 
1494af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1495af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
14965dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1497af4874e0SMike Snitzer 			if (ret)
1498af4874e0SMike Snitzer 				goto out;
1499af4874e0SMike Snitzer 		}
1500af4874e0SMike Snitzer 	}
1501af4874e0SMike Snitzer 
1502af4874e0SMike Snitzer out:
1503af4874e0SMike Snitzer 	return ret;
1504af4874e0SMike Snitzer }
1505af4874e0SMike Snitzer 
1506f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1507f40c67f0SKiyoshi Ueda {
1508f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1509f40c67f0SKiyoshi Ueda 
1510f40c67f0SKiyoshi Ueda 	return dm_underlying_device_busy(q);
1511f40c67f0SKiyoshi Ueda }
1512f40c67f0SKiyoshi Ueda 
1513f40c67f0SKiyoshi Ueda /*
1514f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1515f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1516f40c67f0SKiyoshi Ueda  * the underlying queue).
1517f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1518f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1519f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1520f40c67f0SKiyoshi Ueda  */
1521f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1522f40c67f0SKiyoshi Ueda {
1523f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1524f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1525f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1526f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1527f40c67f0SKiyoshi Ueda 	unsigned long flags;
1528f40c67f0SKiyoshi Ueda 
1529f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1530f40c67f0SKiyoshi Ueda 
1531f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1532f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1533f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1534f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1535f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1536f40c67f0SKiyoshi Ueda 	else
1537f40c67f0SKiyoshi Ueda 		/*
1538f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1539f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1540f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1541f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1542f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1543f40c67f0SKiyoshi Ueda 		 */
1544f40c67f0SKiyoshi Ueda 		goto out;
1545f40c67f0SKiyoshi Ueda 
1546f40c67f0SKiyoshi Ueda 	/*
1547f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1548f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1549f40c67f0SKiyoshi Ueda 	 */
1550f40c67f0SKiyoshi Ueda 	busy = 1;
1551f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1552f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1553f40c67f0SKiyoshi Ueda 			has_active = 1;
1554f40c67f0SKiyoshi Ueda 
1555f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1556f40c67f0SKiyoshi Ueda 				busy = 0;
1557f40c67f0SKiyoshi Ueda 				break;
1558f40c67f0SKiyoshi Ueda 			}
1559f40c67f0SKiyoshi Ueda 		}
1560f40c67f0SKiyoshi Ueda 
1561f40c67f0SKiyoshi Ueda 	if (!has_active)
1562f40c67f0SKiyoshi Ueda 		/*
1563f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1564f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1565f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1566f40c67f0SKiyoshi Ueda 		 */
1567f40c67f0SKiyoshi Ueda 		busy = 0;
1568f40c67f0SKiyoshi Ueda 
1569f40c67f0SKiyoshi Ueda out:
1570f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1571f40c67f0SKiyoshi Ueda 
1572f40c67f0SKiyoshi Ueda 	return busy;
1573f40c67f0SKiyoshi Ueda }
1574f40c67f0SKiyoshi Ueda 
15751da177e4SLinus Torvalds /*-----------------------------------------------------------------
15761da177e4SLinus Torvalds  * Module setup
15771da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15781da177e4SLinus Torvalds static struct target_type multipath_target = {
15791da177e4SLinus Torvalds 	.name = "multipath",
1580*6df400abSKiyoshi Ueda 	.version = {1, 1, 1},
15811da177e4SLinus Torvalds 	.module = THIS_MODULE,
15821da177e4SLinus Torvalds 	.ctr = multipath_ctr,
15831da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1584f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1585f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
15861da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
1587*6df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
15881da177e4SLinus Torvalds 	.resume = multipath_resume,
15891da177e4SLinus Torvalds 	.status = multipath_status,
15901da177e4SLinus Torvalds 	.message = multipath_message,
15919af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1592af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1593f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
15941da177e4SLinus Torvalds };
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds static int __init dm_multipath_init(void)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	int r;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1601028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
16021da177e4SLinus Torvalds 	if (!_mpio_cache)
16031da177e4SLinus Torvalds 		return -ENOMEM;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
16061da177e4SLinus Torvalds 	if (r < 0) {
16070cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
16081da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
16091da177e4SLinus Torvalds 		return -EINVAL;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
1612c557308eSAlasdair G Kergon 	kmultipathd = create_workqueue("kmpathd");
1613c557308eSAlasdair G Kergon 	if (!kmultipathd) {
16140cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1615c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1616c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1617c557308eSAlasdair G Kergon 		return -ENOMEM;
1618c557308eSAlasdair G Kergon 	}
1619c557308eSAlasdair G Kergon 
1620bab7cfc7SChandra Seetharaman 	/*
1621bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1622bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1623bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1624bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1625bab7cfc7SChandra Seetharaman 	 */
1626bab7cfc7SChandra Seetharaman 	kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1627bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1628bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1629bab7cfc7SChandra Seetharaman 		destroy_workqueue(kmultipathd);
1630bab7cfc7SChandra Seetharaman 		dm_unregister_target(&multipath_target);
1631bab7cfc7SChandra Seetharaman 		kmem_cache_destroy(_mpio_cache);
1632bab7cfc7SChandra Seetharaman 		return -ENOMEM;
1633bab7cfc7SChandra Seetharaman 	}
1634bab7cfc7SChandra Seetharaman 
163572d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
16361da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
16371da177e4SLinus Torvalds 	       multipath_target.version[2]);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	return r;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
16431da177e4SLinus Torvalds {
1644bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1645c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1646c557308eSAlasdair G Kergon 
164710d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
16481da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds module_init(dm_multipath_init);
16521da177e4SLinus Torvalds module_exit(dm_multipath_exit);
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
16551da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
16561da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1657