xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 55a62eef8d1b50ceff3b7bf46851103bdcc7e5b0)
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>
2135991652SMikulas Patocka #include <linux/delay.h>
22cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2360063497SArun Sharma #include <linux/atomic.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
264e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
274e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /* Path properties */
301da177e4SLinus Torvalds struct pgpath {
311da177e4SLinus Torvalds 	struct list_head list;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
346680073dSKiyoshi Ueda 	unsigned is_active;		/* Path status */
351da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
361da177e4SLinus Torvalds 
37c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
384e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
391da177e4SLinus Torvalds };
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
451da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds struct priority_group {
481da177e4SLinus Torvalds 	struct list_head list;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
511da177e4SLinus Torvalds 	struct path_selector ps;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
541da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
571da177e4SLinus Torvalds 	struct list_head pgpaths;
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* Multipath context */
611da177e4SLinus Torvalds struct multipath {
621da177e4SLinus Torvalds 	struct list_head list;
631da177e4SLinus Torvalds 	struct dm_target *ti;
641da177e4SLinus Torvalds 
65cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
662bfd2e13SChandra Seetharaman 	char *hw_handler_params;
674e2d19e4SChandra Seetharaman 
681fbdd2b3SMike Snitzer 	spinlock_t lock;
691fbdd2b3SMike Snitzer 
701da177e4SLinus Torvalds 	unsigned nr_priority_groups;
711da177e4SLinus Torvalds 	struct list_head priority_groups;
724e2d19e4SChandra Seetharaman 
734e2d19e4SChandra Seetharaman 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
744e2d19e4SChandra Seetharaman 
751da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
76c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
774e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_retry;	/* Delay pg_init retry? */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
801da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
811da177e4SLinus Torvalds 	struct priority_group *current_pg;
821da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
831da177e4SLinus Torvalds 	unsigned repeat_count;		/* I/Os left before calling PS again */
841da177e4SLinus Torvalds 
851fbdd2b3SMike Snitzer 	unsigned queue_io:1;		/* Must we queue all I/O? */
861fbdd2b3SMike Snitzer 	unsigned queue_if_no_path:1;	/* Queue I/O if last path fails? */
871fbdd2b3SMike Snitzer 	unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
88a58a935dSMike Snitzer 	unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
891fbdd2b3SMike Snitzer 
90c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
91c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
924e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
931da177e4SLinus Torvalds 
941fbdd2b3SMike Snitzer 	unsigned queue_size;
951da177e4SLinus Torvalds 	struct work_struct process_queued_ios;
96f40c67f0SKiyoshi Ueda 	struct list_head queued_ios;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	struct work_struct trigger_event;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	/*
101028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
1021da177e4SLinus Torvalds 	 * can resubmit bios on error.
1031da177e4SLinus Torvalds 	 */
1041da177e4SLinus Torvalds 	mempool_t *mpio_pool;
1056380f26fSMike Anderson 
1066380f26fSMike Anderson 	struct mutex work_mutex;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * Context information attached to each bio we process.
1111da177e4SLinus Torvalds  */
112028867acSAlasdair G Kergon struct dm_mpath_io {
1131da177e4SLinus Torvalds 	struct pgpath *pgpath;
11402ab823fSKiyoshi Ueda 	size_t nr_bytes;
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define MIN_IOS 256	/* Mempool size */
1201da177e4SLinus Torvalds 
121e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1221da177e4SLinus Torvalds 
123bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
124c4028958SDavid Howells static void process_queued_ios(struct work_struct *work);
125c4028958SDavid Howells static void trigger_event(struct work_struct *work);
126bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*-----------------------------------------------
1301da177e4SLinus Torvalds  * Allocation routines
1311da177e4SLinus Torvalds  *-----------------------------------------------*/
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1341da177e4SLinus Torvalds {
135e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1361da177e4SLinus Torvalds 
137224cb3e9SMike Anderson 	if (pgpath) {
1386680073dSKiyoshi Ueda 		pgpath->is_active = 1;
1394e2d19e4SChandra Seetharaman 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
140224cb3e9SMike Anderson 	}
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return pgpath;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
145028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	kfree(pgpath);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct priority_group *pg;
1531da177e4SLinus Torvalds 
154e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1551da177e4SLinus Torvalds 
156e69fae56SMicha³ Miros³aw 	if (pg)
1571da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	return pg;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
165ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1681da177e4SLinus Torvalds 		list_del(&pgpath->list);
169ae11b1b3SHannes Reinecke 		if (m->hw_handler_name)
170ae11b1b3SHannes Reinecke 			scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
1711da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1721da177e4SLinus Torvalds 		free_pgpath(pgpath);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1771da177e4SLinus Torvalds 				struct dm_target *ti)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	if (ps->type) {
1821da177e4SLinus Torvalds 		ps->type->destroy(ps);
1831da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1871da177e4SLinus Torvalds 	kfree(pg);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
19028f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct multipath *m;
1931da177e4SLinus Torvalds 
194e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1951da177e4SLinus Torvalds 	if (m) {
1961da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
197f40c67f0SKiyoshi Ueda 		INIT_LIST_HEAD(&m->queued_ios);
1981da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1991da177e4SLinus Torvalds 		m->queue_io = 1;
2004e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
201c4028958SDavid Howells 		INIT_WORK(&m->process_queued_ios, process_queued_ios);
202c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
2032bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
2046380f26fSMike Anderson 		mutex_init(&m->work_mutex);
20593d2341cSMatthew Dobson 		m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
2061da177e4SLinus Torvalds 		if (!m->mpio_pool) {
2071da177e4SLinus Torvalds 			kfree(m);
2081da177e4SLinus Torvalds 			return NULL;
2091da177e4SLinus Torvalds 		}
21028f16c20SMicha³ Miros³aw 		m->ti = ti;
21128f16c20SMicha³ Miros³aw 		ti->private = m;
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	return m;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2221da177e4SLinus Torvalds 		list_del(&pg->list);
2231da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
226cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2272bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2281da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2291da177e4SLinus Torvalds 	kfree(m);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
232466891f9SJun'ichi Nomura static int set_mapinfo(struct multipath *m, union map_info *info)
233466891f9SJun'ichi Nomura {
234466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio;
235466891f9SJun'ichi Nomura 
236466891f9SJun'ichi Nomura 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
237466891f9SJun'ichi Nomura 	if (!mpio)
238466891f9SJun'ichi Nomura 		return -ENOMEM;
239466891f9SJun'ichi Nomura 
240466891f9SJun'ichi Nomura 	memset(mpio, 0, sizeof(*mpio));
241466891f9SJun'ichi Nomura 	info->ptr = mpio;
242466891f9SJun'ichi Nomura 
243466891f9SJun'ichi Nomura 	return 0;
244466891f9SJun'ichi Nomura }
245466891f9SJun'ichi Nomura 
246466891f9SJun'ichi Nomura static void clear_mapinfo(struct multipath *m, union map_info *info)
247466891f9SJun'ichi Nomura {
248466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio = info->ptr;
249466891f9SJun'ichi Nomura 
250466891f9SJun'ichi Nomura 	info->ptr = NULL;
251466891f9SJun'ichi Nomura 	mempool_free(mpio, m->mpio_pool);
252466891f9SJun'ichi Nomura }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*-----------------------------------------------
2551da177e4SLinus Torvalds  * Path selection
2561da177e4SLinus Torvalds  *-----------------------------------------------*/
2571da177e4SLinus Torvalds 
258fb612642SKiyoshi Ueda static void __pg_init_all_paths(struct multipath *m)
259fb612642SKiyoshi Ueda {
260fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2614e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
262fb612642SKiyoshi Ueda 
263fb612642SKiyoshi Ueda 	m->pg_init_count++;
264fb612642SKiyoshi Ueda 	m->pg_init_required = 0;
2654e2d19e4SChandra Seetharaman 	if (m->pg_init_delay_retry)
2664e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
2674e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
268fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
269fb612642SKiyoshi Ueda 		/* Skip failed paths */
270fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
271fb612642SKiyoshi Ueda 			continue;
2724e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
2734e2d19e4SChandra Seetharaman 				       pg_init_delay))
274fb612642SKiyoshi Ueda 			m->pg_init_in_progress++;
275fb612642SKiyoshi Ueda 	}
276fb612642SKiyoshi Ueda }
277fb612642SKiyoshi Ueda 
2781da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
283cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
2841da177e4SLinus Torvalds 		m->pg_init_required = 1;
2851da177e4SLinus Torvalds 		m->queue_io = 1;
2861da177e4SLinus Torvalds 	} else {
2871da177e4SLinus Torvalds 		m->pg_init_required = 0;
2881da177e4SLinus Torvalds 		m->queue_io = 0;
2891da177e4SLinus Torvalds 	}
290c9e45581SDave Wysochanski 
291c9e45581SDave Wysochanski 	m->pg_init_count = 0;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29402ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
29502ab823fSKiyoshi Ueda 			       size_t nr_bytes)
2961da177e4SLinus Torvalds {
297c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
2981da177e4SLinus Torvalds 
29902ab823fSKiyoshi Ueda 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
3001da177e4SLinus Torvalds 	if (!path)
3011da177e4SLinus Torvalds 		return -ENXIO;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	if (m->current_pg != pg)
3061da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return 0;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
31102ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct priority_group *pg;
3141da177e4SLinus Torvalds 	unsigned bypassed = 1;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	if (!m->nr_valid_paths)
3171da177e4SLinus Torvalds 		goto failed;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3201da177e4SLinus Torvalds 	if (m->next_pg) {
3211da177e4SLinus Torvalds 		pg = m->next_pg;
3221da177e4SLinus Torvalds 		m->next_pg = NULL;
32302ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
3241da177e4SLinus Torvalds 			return;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
32802ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
3291da177e4SLinus Torvalds 		return;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/*
3321da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
3331da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
334f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
335f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
3361da177e4SLinus Torvalds 	 */
3371da177e4SLinus Torvalds 	do {
3381da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
3391da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
3401da177e4SLinus Torvalds 				continue;
341f220fd4eSMike Christie 			if (!__choose_path_in_pg(m, pg, nr_bytes)) {
342f220fd4eSMike Christie 				if (!bypassed)
343f220fd4eSMike Christie 					m->pg_init_delay_retry = 1;
3441da177e4SLinus Torvalds 				return;
3451da177e4SLinus Torvalds 			}
346f220fd4eSMike Christie 		}
3471da177e4SLinus Torvalds 	} while (bypassed--);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds failed:
3501da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3511da177e4SLinus Torvalds 	m->current_pg = NULL;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
35445e15720SKiyoshi Ueda /*
35545e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
35645e15720SKiyoshi Ueda  * than here in the target.
35745e15720SKiyoshi Ueda  *
35845e15720SKiyoshi Ueda  * m->lock must be held on entry.
35945e15720SKiyoshi Ueda  *
36045e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
36145e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
36245e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
36345e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
36445e15720SKiyoshi Ueda  */
36545e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
36645e15720SKiyoshi Ueda {
36745e15720SKiyoshi Ueda 	return (m->queue_if_no_path != m->saved_queue_if_no_path &&
36845e15720SKiyoshi Ueda 		dm_noflush_suspending(m->ti));
36945e15720SKiyoshi Ueda }
37045e15720SKiyoshi Ueda 
371f40c67f0SKiyoshi Ueda static int map_io(struct multipath *m, struct request *clone,
372466891f9SJun'ichi Nomura 		  union map_info *map_context, unsigned was_queued)
3731da177e4SLinus Torvalds {
374d2a7ad29SKiyoshi Ueda 	int r = DM_MAPIO_REMAPPED;
375f40c67f0SKiyoshi Ueda 	size_t nr_bytes = blk_rq_bytes(clone);
3761da177e4SLinus Torvalds 	unsigned long flags;
3771da177e4SLinus Torvalds 	struct pgpath *pgpath;
378f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
379466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio = map_context->ptr;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
3841da177e4SLinus Torvalds 	if (!m->current_pgpath ||
3851da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
38602ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (was_queued)
3911da177e4SLinus Torvalds 		m->queue_size--;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
394436d4108SAlasdair G Kergon 	    (!pgpath && m->queue_if_no_path)) {
3951da177e4SLinus Torvalds 		/* Queue for the daemon to resubmit */
396f40c67f0SKiyoshi Ueda 		list_add_tail(&clone->queuelist, &m->queued_ios);
3971da177e4SLinus Torvalds 		m->queue_size++;
398c3cd4f6bSAlasdair G Kergon 		if ((m->pg_init_required && !m->pg_init_in_progress) ||
399c3cd4f6bSAlasdair G Kergon 		    !m->queue_io)
400c557308eSAlasdair G Kergon 			queue_work(kmultipathd, &m->process_queued_ios);
4011da177e4SLinus Torvalds 		pgpath = NULL;
402d2a7ad29SKiyoshi Ueda 		r = DM_MAPIO_SUBMITTED;
403f40c67f0SKiyoshi Ueda 	} else if (pgpath) {
404f40c67f0SKiyoshi Ueda 		bdev = pgpath->path.dev->bdev;
405f40c67f0SKiyoshi Ueda 		clone->q = bdev_get_queue(bdev);
406f40c67f0SKiyoshi Ueda 		clone->rq_disk = bdev->bd_disk;
407f40c67f0SKiyoshi Ueda 	} else if (__must_push_back(m))
40845e15720SKiyoshi Ueda 		r = DM_MAPIO_REQUEUE;
40945e15720SKiyoshi Ueda 	else
41045e15720SKiyoshi Ueda 		r = -EIO;	/* Failed */
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	mpio->pgpath = pgpath;
41302ab823fSKiyoshi Ueda 	mpio->nr_bytes = nr_bytes;
41402ab823fSKiyoshi Ueda 
41502ab823fSKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
41602ab823fSKiyoshi Ueda 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
41702ab823fSKiyoshi Ueda 					      nr_bytes);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	return r;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /*
4251da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
4261da177e4SLinus Torvalds  */
427485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
428485ef69eSAlasdair G Kergon 			    unsigned save_old_value)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	unsigned long flags;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4331da177e4SLinus Torvalds 
434485ef69eSAlasdair G Kergon 	if (save_old_value)
435436d4108SAlasdair G Kergon 		m->saved_queue_if_no_path = m->queue_if_no_path;
436485ef69eSAlasdair G Kergon 	else
437485ef69eSAlasdair G Kergon 		m->saved_queue_if_no_path = queue_if_no_path;
4381da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
439c3cd4f6bSAlasdair G Kergon 	if (!m->queue_if_no_path && m->queue_size)
440c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	return 0;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*-----------------------------------------------------------------
4481da177e4SLinus Torvalds  * The multipath daemon is responsible for resubmitting queued ios.
4491da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	int r;
4541da177e4SLinus Torvalds 	unsigned long flags;
4551da177e4SLinus Torvalds 	union map_info *info;
456f40c67f0SKiyoshi Ueda 	struct request *clone, *n;
457f40c67f0SKiyoshi Ueda 	LIST_HEAD(cl);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
460f40c67f0SKiyoshi Ueda 	list_splice_init(&m->queued_ios, &cl);
4611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4621da177e4SLinus Torvalds 
463f40c67f0SKiyoshi Ueda 	list_for_each_entry_safe(clone, n, &cl, queuelist) {
464f40c67f0SKiyoshi Ueda 		list_del_init(&clone->queuelist);
4651da177e4SLinus Torvalds 
466f40c67f0SKiyoshi Ueda 		info = dm_get_rq_mapinfo(clone);
4671da177e4SLinus Torvalds 
468466891f9SJun'ichi Nomura 		r = map_io(m, clone, info, 1);
469f40c67f0SKiyoshi Ueda 		if (r < 0) {
470466891f9SJun'ichi Nomura 			clear_mapinfo(m, info);
471f40c67f0SKiyoshi Ueda 			dm_kill_unmapped_request(clone, r);
472f40c67f0SKiyoshi Ueda 		} else if (r == DM_MAPIO_REMAPPED)
473f40c67f0SKiyoshi Ueda 			dm_dispatch_request(clone);
474f40c67f0SKiyoshi Ueda 		else if (r == DM_MAPIO_REQUEUE) {
475466891f9SJun'ichi Nomura 			clear_mapinfo(m, info);
476f40c67f0SKiyoshi Ueda 			dm_requeue_unmapped_request(clone);
477f40c67f0SKiyoshi Ueda 		}
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
481c4028958SDavid Howells static void process_queued_ios(struct work_struct *work)
4821da177e4SLinus Torvalds {
483c4028958SDavid Howells 	struct multipath *m =
484c4028958SDavid Howells 		container_of(work, struct multipath, process_queued_ios);
485fb612642SKiyoshi Ueda 	struct pgpath *pgpath = NULL;
486e54f77ddSChandra Seetharaman 	unsigned must_queue = 1;
4871da177e4SLinus Torvalds 	unsigned long flags;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (!m->current_pgpath)
49202ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
4951da177e4SLinus Torvalds 
496c3cd4f6bSAlasdair G Kergon 	if ((pgpath && !m->queue_io) ||
497c3cd4f6bSAlasdair G Kergon 	    (!pgpath && !m->queue_if_no_path))
498c3cd4f6bSAlasdair G Kergon 		must_queue = 0;
4991da177e4SLinus Torvalds 
500fb612642SKiyoshi Ueda 	if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
501fb612642SKiyoshi Ueda 		__pg_init_all_paths(m);
502fb612642SKiyoshi Ueda 
5031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
5041da177e4SLinus Torvalds 	if (!must_queue)
5051da177e4SLinus Torvalds 		dispatch_queued_ios(m);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds /*
5091da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
5101da177e4SLinus Torvalds  * Includes path failure and PG bypass.
5111da177e4SLinus Torvalds  */
512c4028958SDavid Howells static void trigger_event(struct work_struct *work)
5131da177e4SLinus Torvalds {
514c4028958SDavid Howells 	struct multipath *m =
515c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds /*-----------------------------------------------------------------
5211da177e4SLinus Torvalds  * Constructor/argument parsing:
5221da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
5231da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
5241da177e4SLinus Torvalds  * <#priority groups>
5251da177e4SLinus Torvalds  * <initial priority group>
5261da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
5271da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
5281da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
5291da177e4SLinus Torvalds  *---------------------------------------------------------------*/
530498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
5311da177e4SLinus Torvalds 			       struct dm_target *ti)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	int r;
5341da177e4SLinus Torvalds 	struct path_selector_type *pst;
5351da177e4SLinus Torvalds 	unsigned ps_argc;
5361da177e4SLinus Torvalds 
537498f0103SMike Snitzer 	static struct dm_arg _args[] = {
53872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
5391da177e4SLinus Torvalds 	};
5401da177e4SLinus Torvalds 
541498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
5421da177e4SLinus Torvalds 	if (!pst) {
54372d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
5441da177e4SLinus Torvalds 		return -EINVAL;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
547498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
548371b2e34SMikulas Patocka 	if (r) {
549371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5501da177e4SLinus Torvalds 		return -EINVAL;
551371b2e34SMikulas Patocka 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5541da177e4SLinus Torvalds 	if (r) {
5551da177e4SLinus Torvalds 		dm_put_path_selector(pst);
55672d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5571da177e4SLinus Torvalds 		return r;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	pg->ps.type = pst;
561498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	return 0;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
566498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
5671da177e4SLinus Torvalds 			       struct dm_target *ti)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	int r;
5701da177e4SLinus Torvalds 	struct pgpath *p;
571ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
572a58a935dSMike Snitzer 	struct request_queue *q = NULL;
573a58a935dSMike Snitzer 	const char *attached_handler_name;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* we need at least a path arg */
5761da177e4SLinus Torvalds 	if (as->argc < 1) {
57772d94861SAlasdair G Kergon 		ti->error = "no device given";
57801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	p = alloc_pgpath();
5821da177e4SLinus Torvalds 	if (!p)
58301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
5841da177e4SLinus Torvalds 
585498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
5868215d6ecSNikanth Karthikesan 			  &p->path.dev);
5871da177e4SLinus Torvalds 	if (r) {
58872d94861SAlasdair G Kergon 		ti->error = "error getting device";
5891da177e4SLinus Torvalds 		goto bad;
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
592a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler || m->hw_handler_name)
593a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
594a0cf7ea9SHannes Reinecke 
595a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler) {
596a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
597a58a935dSMike Snitzer 		if (attached_handler_name) {
598a58a935dSMike Snitzer 			/*
599a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
600a58a935dSMike Snitzer 			 * and clear any hw_handler_params associated with the
601a58a935dSMike Snitzer 			 * ignored handler.
602a58a935dSMike Snitzer 			 *
603a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
604a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
605a58a935dSMike Snitzer 			 */
606a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
607a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
608a58a935dSMike Snitzer 
609a58a935dSMike Snitzer 			kfree(m->hw_handler_params);
610a58a935dSMike Snitzer 			m->hw_handler_params = NULL;
611a58a935dSMike Snitzer 		}
612a58a935dSMike Snitzer 	}
613a58a935dSMike Snitzer 
614a58a935dSMike Snitzer 	if (m->hw_handler_name) {
615a58a935dSMike Snitzer 		/*
616a58a935dSMike Snitzer 		 * Increments scsi_dh reference, even when using an
617a58a935dSMike Snitzer 		 * already-attached handler.
618a58a935dSMike Snitzer 		 */
619a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
620a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
621a0cf7ea9SHannes Reinecke 			/*
622a58a935dSMike Snitzer 			 * Already attached to different hw_handler:
623a0cf7ea9SHannes Reinecke 			 * try to reattach with correct one.
624a0cf7ea9SHannes Reinecke 			 */
625a0cf7ea9SHannes Reinecke 			scsi_dh_detach(q);
626a0cf7ea9SHannes Reinecke 			r = scsi_dh_attach(q, m->hw_handler_name);
627a0cf7ea9SHannes Reinecke 		}
628a0cf7ea9SHannes Reinecke 
629ae11b1b3SHannes Reinecke 		if (r < 0) {
630a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
631ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
632ae11b1b3SHannes Reinecke 			goto bad;
633ae11b1b3SHannes Reinecke 		}
6342bfd2e13SChandra Seetharaman 
6352bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
6362bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
6372bfd2e13SChandra Seetharaman 			if (r < 0) {
6382bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
6392bfd2e13SChandra Seetharaman 							"handler parameters";
6402bfd2e13SChandra Seetharaman 				scsi_dh_detach(q);
6412bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
6422bfd2e13SChandra Seetharaman 				goto bad;
6432bfd2e13SChandra Seetharaman 			}
6442bfd2e13SChandra Seetharaman 		}
645ae11b1b3SHannes Reinecke 	}
646ae11b1b3SHannes Reinecke 
6471da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6481da177e4SLinus Torvalds 	if (r) {
6491da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6501da177e4SLinus Torvalds 		goto bad;
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	return p;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds  bad:
6561da177e4SLinus Torvalds 	free_pgpath(p);
65701460f35SBenjamin Marzinski 	return ERR_PTR(r);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
660498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
66128f16c20SMicha³ Miros³aw 						   struct multipath *m)
6621da177e4SLinus Torvalds {
663498f0103SMike Snitzer 	static struct dm_arg _args[] = {
66472d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
66572d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6661da177e4SLinus Torvalds 	};
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	int r;
669498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
6701da177e4SLinus Torvalds 	struct priority_group *pg;
67128f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	if (as->argc < 2) {
6741da177e4SLinus Torvalds 		as->argc = 0;
67501460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
67601460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	pg = alloc_priority_group();
6801da177e4SLinus Torvalds 	if (!pg) {
68172d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
68201460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	pg->m = m;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
6871da177e4SLinus Torvalds 	if (r)
6881da177e4SLinus Torvalds 		goto bad;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	/*
6911da177e4SLinus Torvalds 	 * read the paths
6921da177e4SLinus Torvalds 	 */
693498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
6941da177e4SLinus Torvalds 	if (r)
6951da177e4SLinus Torvalds 		goto bad;
6961da177e4SLinus Torvalds 
697498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
6981da177e4SLinus Torvalds 	if (r)
6991da177e4SLinus Torvalds 		goto bad;
7001da177e4SLinus Torvalds 
701498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
7021da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
7031da177e4SLinus Torvalds 		struct pgpath *pgpath;
704498f0103SMike Snitzer 		struct dm_arg_set path_args;
7051da177e4SLinus Torvalds 
706498f0103SMike Snitzer 		if (as->argc < nr_args) {
707148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
7086bbf79a1SAlasdair G Kergon 			r = -EINVAL;
7091da177e4SLinus Torvalds 			goto bad;
710148acff6SMikulas Patocka 		}
7111da177e4SLinus Torvalds 
712498f0103SMike Snitzer 		path_args.argc = nr_args;
7131da177e4SLinus Torvalds 		path_args.argv = as->argv;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
71601460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
71701460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
7181da177e4SLinus Torvalds 			goto bad;
71901460f35SBenjamin Marzinski 		}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		pgpath->pg = pg;
7221da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
723498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	return pg;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds  bad:
7291da177e4SLinus Torvalds 	free_priority_group(pg, ti);
73001460f35SBenjamin Marzinski 	return ERR_PTR(r);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
733498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	unsigned hw_argc;
7362bfd2e13SChandra Seetharaman 	int ret;
73728f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
7381da177e4SLinus Torvalds 
739498f0103SMike Snitzer 	static struct dm_arg _args[] = {
74072d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
7411da177e4SLinus Torvalds 	};
7421da177e4SLinus Torvalds 
743498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
7441da177e4SLinus Torvalds 		return -EINVAL;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (!hw_argc)
7471da177e4SLinus Torvalds 		return 0;
7481da177e4SLinus Torvalds 
749498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
750510193a2SMike Snitzer 	if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
751510193a2SMike Snitzer 				     "scsi_dh_%s", m->hw_handler_name)) {
75272d94861SAlasdair G Kergon 		ti->error = "unknown hardware handler type";
7532bfd2e13SChandra Seetharaman 		ret = -EINVAL;
7542bfd2e13SChandra Seetharaman 		goto fail;
7551da177e4SLinus Torvalds 	}
75614e98c5cSChandra Seetharaman 
7572bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
7582bfd2e13SChandra Seetharaman 		char *p;
7592bfd2e13SChandra Seetharaman 		int i, j, len = 4;
7602bfd2e13SChandra Seetharaman 
7612bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
7622bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
7632bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
7642bfd2e13SChandra Seetharaman 		if (!p) {
7652bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
7662bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
7672bfd2e13SChandra Seetharaman 			goto fail;
7682bfd2e13SChandra Seetharaman 		}
7692bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
7702bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
7712bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
7722bfd2e13SChandra Seetharaman 	}
773498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	return 0;
7762bfd2e13SChandra Seetharaman fail:
7772bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
7782bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
7792bfd2e13SChandra Seetharaman 	return ret;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
782498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	int r;
7851da177e4SLinus Torvalds 	unsigned argc;
78628f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
787498f0103SMike Snitzer 	const char *arg_name;
7881da177e4SLinus Torvalds 
789498f0103SMike Snitzer 	static struct dm_arg _args[] = {
790a58a935dSMike Snitzer 		{0, 6, "invalid number of feature args"},
791c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
7924e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
7931da177e4SLinus Torvalds 	};
7941da177e4SLinus Torvalds 
795498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
7961da177e4SLinus Torvalds 	if (r)
7971da177e4SLinus Torvalds 		return -EINVAL;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	if (!argc)
8001da177e4SLinus Torvalds 		return 0;
8011da177e4SLinus Torvalds 
802c9e45581SDave Wysochanski 	do {
803498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
804c9e45581SDave Wysochanski 		argc--;
805c9e45581SDave Wysochanski 
806498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
807c9e45581SDave Wysochanski 			r = queue_if_no_path(m, 1, 0);
808c9e45581SDave Wysochanski 			continue;
8091da177e4SLinus Torvalds 		}
810c9e45581SDave Wysochanski 
811a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
812a58a935dSMike Snitzer 			m->retain_attached_hw_handler = 1;
813a58a935dSMike Snitzer 			continue;
814a58a935dSMike Snitzer 		}
815a58a935dSMike Snitzer 
816498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
817c9e45581SDave Wysochanski 		    (argc >= 1)) {
818498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
819c9e45581SDave Wysochanski 			argc--;
820c9e45581SDave Wysochanski 			continue;
821c9e45581SDave Wysochanski 		}
822c9e45581SDave Wysochanski 
823498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
8244e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
825498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
8264e2d19e4SChandra Seetharaman 			argc--;
8274e2d19e4SChandra Seetharaman 			continue;
8284e2d19e4SChandra Seetharaman 		}
8294e2d19e4SChandra Seetharaman 
830c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
831c9e45581SDave Wysochanski 		r = -EINVAL;
832c9e45581SDave Wysochanski 	} while (argc && !r);
833c9e45581SDave Wysochanski 
834c9e45581SDave Wysochanski 	return r;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
8381da177e4SLinus Torvalds 			 char **argv)
8391da177e4SLinus Torvalds {
840498f0103SMike Snitzer 	/* target arguments */
841498f0103SMike Snitzer 	static struct dm_arg _args[] = {
842a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
843a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
8441da177e4SLinus Torvalds 	};
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	int r;
8471da177e4SLinus Torvalds 	struct multipath *m;
848498f0103SMike Snitzer 	struct dm_arg_set as;
8491da177e4SLinus Torvalds 	unsigned pg_count = 0;
8501da177e4SLinus Torvalds 	unsigned next_pg_num;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	as.argc = argc;
8531da177e4SLinus Torvalds 	as.argv = argv;
8541da177e4SLinus Torvalds 
85528f16c20SMicha³ Miros³aw 	m = alloc_multipath(ti);
8561da177e4SLinus Torvalds 	if (!m) {
85772d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
8581da177e4SLinus Torvalds 		return -EINVAL;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
86128f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
8621da177e4SLinus Torvalds 	if (r)
8631da177e4SLinus Torvalds 		goto bad;
8641da177e4SLinus Torvalds 
86528f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
8661da177e4SLinus Torvalds 	if (r)
8671da177e4SLinus Torvalds 		goto bad;
8681da177e4SLinus Torvalds 
869498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
8701da177e4SLinus Torvalds 	if (r)
8711da177e4SLinus Torvalds 		goto bad;
8721da177e4SLinus Torvalds 
873498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
8741da177e4SLinus Torvalds 	if (r)
8751da177e4SLinus Torvalds 		goto bad;
8761da177e4SLinus Torvalds 
877a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
878a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
879a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
880a490a07aSMike Snitzer 		r = -EINVAL;
881a490a07aSMike Snitzer 		goto bad;
882a490a07aSMike Snitzer 	}
883a490a07aSMike Snitzer 
8841da177e4SLinus Torvalds 	/* parse the priority groups */
8851da177e4SLinus Torvalds 	while (as.argc) {
8861da177e4SLinus Torvalds 		struct priority_group *pg;
8871da177e4SLinus Torvalds 
88828f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
88901460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
89001460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
8911da177e4SLinus Torvalds 			goto bad;
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
8951da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
8961da177e4SLinus Torvalds 		pg_count++;
8971da177e4SLinus Torvalds 		pg->pg_num = pg_count;
8981da177e4SLinus Torvalds 		if (!--next_pg_num)
8991da177e4SLinus Torvalds 			m->next_pg = pg;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
90372d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
9041da177e4SLinus Torvalds 		r = -EINVAL;
9051da177e4SLinus Torvalds 		goto bad;
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
908*55a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
909*55a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
9108627921fSMikulas Patocka 
9111da177e4SLinus Torvalds 	return 0;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds  bad:
9141da177e4SLinus Torvalds 	free_multipath(m);
9151da177e4SLinus Torvalds 	return r;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
9182bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
9192bded7bdSKiyoshi Ueda {
9202bded7bdSKiyoshi Ueda 	DECLARE_WAITQUEUE(wait, current);
9212bded7bdSKiyoshi Ueda 	unsigned long flags;
9222bded7bdSKiyoshi Ueda 
9232bded7bdSKiyoshi Ueda 	add_wait_queue(&m->pg_init_wait, &wait);
9242bded7bdSKiyoshi Ueda 
9252bded7bdSKiyoshi Ueda 	while (1) {
9262bded7bdSKiyoshi Ueda 		set_current_state(TASK_UNINTERRUPTIBLE);
9272bded7bdSKiyoshi Ueda 
9282bded7bdSKiyoshi Ueda 		spin_lock_irqsave(&m->lock, flags);
9292bded7bdSKiyoshi Ueda 		if (!m->pg_init_in_progress) {
9302bded7bdSKiyoshi Ueda 			spin_unlock_irqrestore(&m->lock, flags);
9312bded7bdSKiyoshi Ueda 			break;
9322bded7bdSKiyoshi Ueda 		}
9332bded7bdSKiyoshi Ueda 		spin_unlock_irqrestore(&m->lock, flags);
9342bded7bdSKiyoshi Ueda 
9352bded7bdSKiyoshi Ueda 		io_schedule();
9362bded7bdSKiyoshi Ueda 	}
9372bded7bdSKiyoshi Ueda 	set_current_state(TASK_RUNNING);
9382bded7bdSKiyoshi Ueda 
9392bded7bdSKiyoshi Ueda 	remove_wait_queue(&m->pg_init_wait, &wait);
9402bded7bdSKiyoshi Ueda }
9412bded7bdSKiyoshi Ueda 
9422bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
9431da177e4SLinus Torvalds {
944bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
9452bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
946a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
94743829731STejun Heo 	flush_work(&m->trigger_event);
9486df400abSKiyoshi Ueda }
9496df400abSKiyoshi Ueda 
9506df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
9516df400abSKiyoshi Ueda {
9526df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
9536df400abSKiyoshi Ueda 
9542bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
9551da177e4SLinus Torvalds 	free_multipath(m);
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /*
959f40c67f0SKiyoshi Ueda  * Map cloned requests
9601da177e4SLinus Torvalds  */
961f40c67f0SKiyoshi Ueda static int multipath_map(struct dm_target *ti, struct request *clone,
9621da177e4SLinus Torvalds 			 union map_info *map_context)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	int r;
9651da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
9661da177e4SLinus Torvalds 
967466891f9SJun'ichi Nomura 	if (set_mapinfo(m, map_context) < 0)
968f40c67f0SKiyoshi Ueda 		/* ENOMEM, requeue */
969f40c67f0SKiyoshi Ueda 		return DM_MAPIO_REQUEUE;
9701da177e4SLinus Torvalds 
971f40c67f0SKiyoshi Ueda 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
972466891f9SJun'ichi Nomura 	r = map_io(m, clone, map_context, 0);
97345e15720SKiyoshi Ueda 	if (r < 0 || r == DM_MAPIO_REQUEUE)
974466891f9SJun'ichi Nomura 		clear_mapinfo(m, map_context);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	return r;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds /*
9801da177e4SLinus Torvalds  * Take a path out of use.
9811da177e4SLinus Torvalds  */
9821da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	unsigned long flags;
9851da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9881da177e4SLinus Torvalds 
9896680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9901da177e4SLinus Torvalds 		goto out;
9911da177e4SLinus Torvalds 
99272d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9956680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9961da177e4SLinus Torvalds 	pgpath->fail_count++;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	m->nr_valid_paths--;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
10011da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10021da177e4SLinus Torvalds 
1003b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1004b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1005b15546f9SMike Anderson 
1006fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds out:
10091da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	return 0;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds /*
10151da177e4SLinus Torvalds  * Reinstate a previously-failed path
10161da177e4SLinus Torvalds  */
10171da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	int r = 0;
10201da177e4SLinus Torvalds 	unsigned long flags;
10211da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10241da177e4SLinus Torvalds 
10256680073dSKiyoshi Ueda 	if (pgpath->is_active)
10261da177e4SLinus Torvalds 		goto out;
10271da177e4SLinus Torvalds 
1028def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
10291da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
10301da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
10311da177e4SLinus Torvalds 		r = -EINVAL;
10321da177e4SLinus Torvalds 		goto out;
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
10361da177e4SLinus Torvalds 	if (r)
10371da177e4SLinus Torvalds 		goto out;
10381da177e4SLinus Torvalds 
10396680073dSKiyoshi Ueda 	pgpath->is_active = 1;
10401da177e4SLinus Torvalds 
1041e54f77ddSChandra Seetharaman 	if (!m->nr_valid_paths++ && m->queue_size) {
10421da177e4SLinus Torvalds 		m->current_pgpath = NULL;
1043c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
1044e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
10454e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1046e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
1047e54f77ddSChandra Seetharaman 	}
10481da177e4SLinus Torvalds 
1049b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1050b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1051b15546f9SMike Anderson 
1052fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds out:
10551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	return r;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds /*
10611da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10621da177e4SLinus Torvalds  */
10631da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10641da177e4SLinus Torvalds 		      action_fn action)
10651da177e4SLinus Torvalds {
106619040c0bSMike Snitzer 	int r = -EINVAL;
10671da177e4SLinus Torvalds 	struct pgpath *pgpath;
10681da177e4SLinus Torvalds 	struct priority_group *pg;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10711da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10721da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10731da177e4SLinus Torvalds 				r = action(pgpath);
10741da177e4SLinus Torvalds 		}
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	return r;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10821da177e4SLinus Torvalds  */
10831da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10841da177e4SLinus Torvalds 		      int bypassed)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	unsigned long flags;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10911da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10921da177e4SLinus Torvalds 	m->current_pg = NULL;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10951da177e4SLinus Torvalds 
1096fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds /*
11001da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	struct priority_group *pg;
11051da177e4SLinus Torvalds 	unsigned pgnum;
11061da177e4SLinus Torvalds 	unsigned long flags;
110731998ef1SMikulas Patocka 	char dummy;
11081da177e4SLinus Torvalds 
110931998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11101da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11111da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
11121da177e4SLinus Torvalds 		return -EINVAL;
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11161da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11171da177e4SLinus Torvalds 		pg->bypassed = 0;
11181da177e4SLinus Torvalds 		if (--pgnum)
11191da177e4SLinus Torvalds 			continue;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 		m->current_pgpath = NULL;
11221da177e4SLinus Torvalds 		m->current_pg = NULL;
11231da177e4SLinus Torvalds 		m->next_pg = pg;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
11261da177e4SLinus Torvalds 
1127fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11281da177e4SLinus Torvalds 	return 0;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds /*
11321da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
11331da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
11341da177e4SLinus Torvalds  */
11351da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	struct priority_group *pg;
11381da177e4SLinus Torvalds 	unsigned pgnum;
113931998ef1SMikulas Patocka 	char dummy;
11401da177e4SLinus Torvalds 
114131998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11421da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11431da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
11441da177e4SLinus Torvalds 		return -EINVAL;
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11481da177e4SLinus Torvalds 		if (!--pgnum)
11491da177e4SLinus Torvalds 			break;
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
11531da177e4SLinus Torvalds 	return 0;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /*
1157c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1158c9e45581SDave Wysochanski  */
1159c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1160c9e45581SDave Wysochanski {
1161c9e45581SDave Wysochanski 	unsigned long flags;
1162c9e45581SDave Wysochanski 	int limit_reached = 0;
1163c9e45581SDave Wysochanski 
1164c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1165c9e45581SDave Wysochanski 
1166c9e45581SDave Wysochanski 	if (m->pg_init_count <= m->pg_init_retries)
1167c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1168c9e45581SDave Wysochanski 	else
1169c9e45581SDave Wysochanski 		limit_reached = 1;
1170c9e45581SDave Wysochanski 
1171c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1172c9e45581SDave Wysochanski 
1173c9e45581SDave Wysochanski 	return limit_reached;
1174c9e45581SDave Wysochanski }
1175c9e45581SDave Wysochanski 
11763ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1177cfae5c9bSChandra Seetharaman {
117883c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1179cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1180cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1181cfae5c9bSChandra Seetharaman 	unsigned long flags;
11824e2d19e4SChandra Seetharaman 	unsigned delay_retry = 0;
1183cfae5c9bSChandra Seetharaman 
1184cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1185cfae5c9bSChandra Seetharaman 	switch (errors) {
1186cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1187cfae5c9bSChandra Seetharaman 		break;
1188cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1189cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1190cfae5c9bSChandra Seetharaman 			errors = 0;
1191cfae5c9bSChandra Seetharaman 			break;
1192cfae5c9bSChandra Seetharaman 		}
1193f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1194f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1195cfae5c9bSChandra Seetharaman 		/*
1196cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1197cfae5c9bSChandra Seetharaman 		 */
1198cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1199cfae5c9bSChandra Seetharaman 		break;
1200cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1201cfae5c9bSChandra Seetharaman 		/*
1202cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1203cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1204cfae5c9bSChandra Seetharaman 		 */
1205cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1206cfae5c9bSChandra Seetharaman 		break;
1207cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
12084e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
12094e2d19e4SChandra Seetharaman 		delay_retry = 1;
1210cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1211cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1212cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1213cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1214cfae5c9bSChandra Seetharaman 		errors = 0;
1215cfae5c9bSChandra Seetharaman 		break;
1216cfae5c9bSChandra Seetharaman 	default:
1217cfae5c9bSChandra Seetharaman 		/*
1218cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1219cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1220cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1221cfae5c9bSChandra Seetharaman 		 */
1222cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1223cfae5c9bSChandra Seetharaman 	}
1224cfae5c9bSChandra Seetharaman 
1225cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1226cfae5c9bSChandra Seetharaman 	if (errors) {
1227e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1228cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1229cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1230cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1231e54f77ddSChandra Seetharaman 		}
1232d0259bf0SKiyoshi Ueda 	} else if (!m->pg_init_required)
1233cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1234cfae5c9bSChandra Seetharaman 
1235d0259bf0SKiyoshi Ueda 	if (--m->pg_init_in_progress)
1236d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1237d0259bf0SKiyoshi Ueda 		goto out;
1238d0259bf0SKiyoshi Ueda 
1239d0259bf0SKiyoshi Ueda 	if (!m->pg_init_required)
1240d0259bf0SKiyoshi Ueda 		m->queue_io = 0;
1241d0259bf0SKiyoshi Ueda 
12424e2d19e4SChandra Seetharaman 	m->pg_init_delay_retry = delay_retry;
1243cfae5c9bSChandra Seetharaman 	queue_work(kmultipathd, &m->process_queued_ios);
1244d0259bf0SKiyoshi Ueda 
12452bded7bdSKiyoshi Ueda 	/*
12462bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
12472bded7bdSKiyoshi Ueda 	 */
12482bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
12492bded7bdSKiyoshi Ueda 
1250d0259bf0SKiyoshi Ueda out:
1251cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1252cfae5c9bSChandra Seetharaman }
1253cfae5c9bSChandra Seetharaman 
1254bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1255bab7cfc7SChandra Seetharaman {
1256e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
12574e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1258bab7cfc7SChandra Seetharaman 
12593ae31f6aSChandra Seetharaman 	scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
126083c0d5d5SMoger, Babu 				pg_init_done, pgpath);
1261bab7cfc7SChandra Seetharaman }
1262bab7cfc7SChandra Seetharaman 
12631da177e4SLinus Torvalds /*
12641da177e4SLinus Torvalds  * end_io handling
12651da177e4SLinus Torvalds  */
1266f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1267028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12681da177e4SLinus Torvalds {
1269f40c67f0SKiyoshi Ueda 	/*
1270f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1271f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1272f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1273f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1274f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1275f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1276f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1277f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1278f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1279f40c67f0SKiyoshi Ueda 	 */
1280f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1281640eb3b0SStefan Bader 	unsigned long flags;
12821da177e4SLinus Torvalds 
1283f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
12841da177e4SLinus Torvalds 		return 0;	/* I/O complete */
12851da177e4SLinus Torvalds 
12866f13f6fbSMartin K. Petersen 	if (error == -EOPNOTSUPP || error == -EREMOTEIO || error == -EILSEQ)
1287959eb4e5SMike Snitzer 		return error;
1288959eb4e5SMike Snitzer 
1289cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
12901da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
12911da177e4SLinus Torvalds 
1292640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1293751b2a7dSHannes Reinecke 	if (!m->nr_valid_paths) {
1294751b2a7dSHannes Reinecke 		if (!m->queue_if_no_path) {
1295751b2a7dSHannes Reinecke 			if (!__must_push_back(m))
1296f40c67f0SKiyoshi Ueda 				r = -EIO;
1297751b2a7dSHannes Reinecke 		} else {
1298751b2a7dSHannes Reinecke 			if (error == -EBADE)
1299751b2a7dSHannes Reinecke 				r = error;
1300751b2a7dSHannes Reinecke 		}
1301751b2a7dSHannes Reinecke 	}
1302640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
13031da177e4SLinus Torvalds 
1304f40c67f0SKiyoshi Ueda 	return r;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
1307f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
13081da177e4SLinus Torvalds 			    int error, union map_info *map_context)
13091da177e4SLinus Torvalds {
1310028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1311028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
1312a71a261fSWei Yongjun 	struct pgpath *pgpath;
13131da177e4SLinus Torvalds 	struct path_selector *ps;
13141da177e4SLinus Torvalds 	int r;
13151da177e4SLinus Torvalds 
1316466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1317466891f9SJun'ichi Nomura 
1318f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
1319a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
13201da177e4SLinus Torvalds 	if (pgpath) {
13211da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
13221da177e4SLinus Torvalds 		if (ps->type->end_io)
132302ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
13241da177e4SLinus Torvalds 	}
1325466891f9SJun'ichi Nomura 	clear_mapinfo(m, map_context);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	return r;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds /*
13311da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1332436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1333436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1334436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13391da177e4SLinus Torvalds 
1340485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
13436df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
13446df400abSKiyoshi Ueda {
13456380f26fSMike Anderson 	struct multipath *m = ti->private;
13466380f26fSMike Anderson 
13476380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
13482bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
13496380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
13506df400abSKiyoshi Ueda }
13516df400abSKiyoshi Ueda 
1352436d4108SAlasdair G Kergon /*
1353436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1354436d4108SAlasdair G Kergon  */
13551da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13581da177e4SLinus Torvalds 	unsigned long flags;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1361436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
13621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds /*
13661da177e4SLinus Torvalds  * Info output has the following format:
13671da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
13681da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
13691da177e4SLinus Torvalds  * num_groups init_group_number
13701da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
13711da177e4SLinus Torvalds  *             num_paths num_selector_args
13721da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
13731da177e4SLinus Torvalds  *
13741da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
13751da177e4SLinus Torvalds  * num_feature_args [features_args]*
13761da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
13771da177e4SLinus Torvalds  * num_groups init_group_number
13781da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
13791da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
13801da177e4SLinus Torvalds  */
1381fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
13821f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	int sz = 0;
13851da177e4SLinus Torvalds 	unsigned long flags;
13861da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13871da177e4SLinus Torvalds 	struct priority_group *pg;
13881da177e4SLinus Torvalds 	struct pgpath *p;
13891da177e4SLinus Torvalds 	unsigned pg_num;
13901da177e4SLinus Torvalds 	char state;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	/* Features */
13951da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1396c9e45581SDave Wysochanski 		DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1397c9e45581SDave Wysochanski 	else {
1398c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
13994e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1400a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1401a58a935dSMike Snitzer 			      m->retain_attached_hw_handler);
1402c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1403c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1404c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1405c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
14064e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
14074e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1408a58a935dSMike Snitzer 		if (m->retain_attached_hw_handler)
1409a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1410c9e45581SDave Wysochanski 	}
14111da177e4SLinus Torvalds 
1412cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
14131da177e4SLinus Torvalds 		DMEMIT("0 ");
14141da177e4SLinus Torvalds 	else
1415cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	if (m->next_pg)
14201da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
14211da177e4SLinus Torvalds 	else if (m->current_pg)
14221da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
14231da177e4SLinus Torvalds 	else
1424a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	switch (type) {
14291da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
14301da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14311da177e4SLinus Torvalds 			if (pg->bypassed)
14321da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
14331da177e4SLinus Torvalds 			else if (pg == m->current_pg)
14341da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
14351da177e4SLinus Torvalds 			else
14361da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 			DMEMIT("%c ", state);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 			if (pg->ps.type->status)
14411da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14421da177e4SLinus Torvalds 							  result + sz,
14431da177e4SLinus Torvalds 							  maxlen - sz);
14441da177e4SLinus Torvalds 			else
14451da177e4SLinus Torvalds 				DMEMIT("0 ");
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14481da177e4SLinus Torvalds 			       pg->ps.type->info_args);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14511da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
14526680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
14531da177e4SLinus Torvalds 				       p->fail_count);
14541da177e4SLinus Torvalds 				if (pg->ps.type->status)
14551da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14561da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14571da177e4SLinus Torvalds 					      maxlen - sz);
14581da177e4SLinus Torvalds 			}
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 		break;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
14631da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14641da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 			if (pg->ps.type->status)
14671da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14681da177e4SLinus Torvalds 							  result + sz,
14691da177e4SLinus Torvalds 							  maxlen - sz);
14701da177e4SLinus Torvalds 			else
14711da177e4SLinus Torvalds 				DMEMIT("0 ");
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14741da177e4SLinus Torvalds 			       pg->ps.type->table_args);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14771da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
14781da177e4SLinus Torvalds 				if (pg->ps.type->status)
14791da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14801da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14811da177e4SLinus Torvalds 					      maxlen - sz);
14821da177e4SLinus Torvalds 			}
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 		break;
14851da177e4SLinus Torvalds 	}
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
14911da177e4SLinus Torvalds {
14926380f26fSMike Anderson 	int r = -EINVAL;
14931da177e4SLinus Torvalds 	struct dm_dev *dev;
14941da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14951da177e4SLinus Torvalds 	action_fn action;
14961da177e4SLinus Torvalds 
14976380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
14986380f26fSMike Anderson 
1499c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1500c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1501c2f3d24bSKiyoshi Ueda 		goto out;
1502c2f3d24bSKiyoshi Ueda 	}
1503c2f3d24bSKiyoshi Ueda 
15041da177e4SLinus Torvalds 	if (argc == 1) {
1505498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
15066380f26fSMike Anderson 			r = queue_if_no_path(m, 1, 0);
15076380f26fSMike Anderson 			goto out;
1508498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
15096380f26fSMike Anderson 			r = queue_if_no_path(m, 0, 0);
15106380f26fSMike Anderson 			goto out;
15116380f26fSMike Anderson 		}
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 
15146380f26fSMike Anderson 	if (argc != 2) {
15156380f26fSMike Anderson 		DMWARN("Unrecognised multipath message received.");
15166380f26fSMike Anderson 		goto out;
15176380f26fSMike Anderson 	}
15181da177e4SLinus Torvalds 
1519498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
15206380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 1);
15216380f26fSMike Anderson 		goto out;
1522498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
15236380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 0);
15246380f26fSMike Anderson 		goto out;
1525498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
15266380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
15276380f26fSMike Anderson 		goto out;
1528498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
15291da177e4SLinus Torvalds 		action = reinstate_path;
1530498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
15311da177e4SLinus Torvalds 		action = fail_path;
15326380f26fSMike Anderson 	else {
15336380f26fSMike Anderson 		DMWARN("Unrecognised multipath message received.");
15346380f26fSMike Anderson 		goto out;
15356380f26fSMike Anderson 	}
15361da177e4SLinus Torvalds 
15378215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
15381da177e4SLinus Torvalds 	if (r) {
153972d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
15401da177e4SLinus Torvalds 		       argv[1]);
15416380f26fSMike Anderson 		goto out;
15421da177e4SLinus Torvalds 	}
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	dm_put_device(ti, dev);
15471da177e4SLinus Torvalds 
15486380f26fSMike Anderson out:
15496380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15501da177e4SLinus Torvalds 	return r;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
1553647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
15549af4aa30SMilan Broz 			   unsigned long arg)
15559af4aa30SMilan Broz {
155635991652SMikulas Patocka 	struct multipath *m = ti->private;
15577ba10aa6SMike Snitzer 	struct pgpath *pgpath;
155835991652SMikulas Patocka 	struct block_device *bdev;
155935991652SMikulas Patocka 	fmode_t mode;
15609af4aa30SMilan Broz 	unsigned long flags;
156135991652SMikulas Patocka 	int r;
156235991652SMikulas Patocka 
156335991652SMikulas Patocka again:
156435991652SMikulas Patocka 	bdev = NULL;
156535991652SMikulas Patocka 	mode = 0;
156635991652SMikulas Patocka 	r = 0;
15679af4aa30SMilan Broz 
15689af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
15699af4aa30SMilan Broz 
15709af4aa30SMilan Broz 	if (!m->current_pgpath)
157102ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
15729af4aa30SMilan Broz 
15737ba10aa6SMike Snitzer 	pgpath = m->current_pgpath;
15747ba10aa6SMike Snitzer 
15757ba10aa6SMike Snitzer 	if (pgpath) {
15767ba10aa6SMike Snitzer 		bdev = pgpath->path.dev->bdev;
15777ba10aa6SMike Snitzer 		mode = pgpath->path.dev->mode;
1578e90dae1fSMilan Broz 	}
15799af4aa30SMilan Broz 
15807ba10aa6SMike Snitzer 	if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
15819af4aa30SMilan Broz 		r = -EAGAIN;
15829af4aa30SMilan Broz 	else if (!bdev)
15839af4aa30SMilan Broz 		r = -EIO;
15849af4aa30SMilan Broz 
15859af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
15869af4aa30SMilan Broz 
1587ec8013beSPaolo Bonzini 	/*
1588ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
1589ec8013beSPaolo Bonzini 	 */
1590ec8013beSPaolo Bonzini 	if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
1591ec8013beSPaolo Bonzini 		r = scsi_verify_blk_ioctl(NULL, cmd);
1592ec8013beSPaolo Bonzini 
159335991652SMikulas Patocka 	if (r == -EAGAIN && !fatal_signal_pending(current)) {
159435991652SMikulas Patocka 		queue_work(kmultipathd, &m->process_queued_ios);
159535991652SMikulas Patocka 		msleep(10);
159635991652SMikulas Patocka 		goto again;
159735991652SMikulas Patocka 	}
159835991652SMikulas Patocka 
1599633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
16009af4aa30SMilan Broz }
16019af4aa30SMilan Broz 
1602af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1603af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1604af4874e0SMike Snitzer {
1605af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1606af4874e0SMike Snitzer 	struct priority_group *pg;
1607af4874e0SMike Snitzer 	struct pgpath *p;
1608af4874e0SMike Snitzer 	int ret = 0;
1609af4874e0SMike Snitzer 
1610af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1611af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
16125dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1613af4874e0SMike Snitzer 			if (ret)
1614af4874e0SMike Snitzer 				goto out;
1615af4874e0SMike Snitzer 		}
1616af4874e0SMike Snitzer 	}
1617af4874e0SMike Snitzer 
1618af4874e0SMike Snitzer out:
1619af4874e0SMike Snitzer 	return ret;
1620af4874e0SMike Snitzer }
1621af4874e0SMike Snitzer 
1622f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1623f40c67f0SKiyoshi Ueda {
1624f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1625f40c67f0SKiyoshi Ueda 
1626f40c67f0SKiyoshi Ueda 	return dm_underlying_device_busy(q);
1627f40c67f0SKiyoshi Ueda }
1628f40c67f0SKiyoshi Ueda 
1629f40c67f0SKiyoshi Ueda /*
1630f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1631f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1632f40c67f0SKiyoshi Ueda  * the underlying queue).
1633f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1634f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1635f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1636f40c67f0SKiyoshi Ueda  */
1637f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1638f40c67f0SKiyoshi Ueda {
1639f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1640f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1641f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1642f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1643f40c67f0SKiyoshi Ueda 	unsigned long flags;
1644f40c67f0SKiyoshi Ueda 
1645f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1646f40c67f0SKiyoshi Ueda 
1647f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1648f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1649f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1650f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1651f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1652f40c67f0SKiyoshi Ueda 	else
1653f40c67f0SKiyoshi Ueda 		/*
1654f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1655f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1656f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1657f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1658f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1659f40c67f0SKiyoshi Ueda 		 */
1660f40c67f0SKiyoshi Ueda 		goto out;
1661f40c67f0SKiyoshi Ueda 
1662f40c67f0SKiyoshi Ueda 	/*
1663f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1664f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1665f40c67f0SKiyoshi Ueda 	 */
1666f40c67f0SKiyoshi Ueda 	busy = 1;
1667f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1668f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1669f40c67f0SKiyoshi Ueda 			has_active = 1;
1670f40c67f0SKiyoshi Ueda 
1671f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1672f40c67f0SKiyoshi Ueda 				busy = 0;
1673f40c67f0SKiyoshi Ueda 				break;
1674f40c67f0SKiyoshi Ueda 			}
1675f40c67f0SKiyoshi Ueda 		}
1676f40c67f0SKiyoshi Ueda 
1677f40c67f0SKiyoshi Ueda 	if (!has_active)
1678f40c67f0SKiyoshi Ueda 		/*
1679f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1680f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1681f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1682f40c67f0SKiyoshi Ueda 		 */
1683f40c67f0SKiyoshi Ueda 		busy = 0;
1684f40c67f0SKiyoshi Ueda 
1685f40c67f0SKiyoshi Ueda out:
1686f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1687f40c67f0SKiyoshi Ueda 
1688f40c67f0SKiyoshi Ueda 	return busy;
1689f40c67f0SKiyoshi Ueda }
1690f40c67f0SKiyoshi Ueda 
16911da177e4SLinus Torvalds /*-----------------------------------------------------------------
16921da177e4SLinus Torvalds  * Module setup
16931da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16941da177e4SLinus Torvalds static struct target_type multipath_target = {
16951da177e4SLinus Torvalds 	.name = "multipath",
1696fd7c092eSMikulas Patocka 	.version = {1, 5, 1},
16971da177e4SLinus Torvalds 	.module = THIS_MODULE,
16981da177e4SLinus Torvalds 	.ctr = multipath_ctr,
16991da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1700f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1701f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
17021da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
17036df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
17041da177e4SLinus Torvalds 	.resume = multipath_resume,
17051da177e4SLinus Torvalds 	.status = multipath_status,
17061da177e4SLinus Torvalds 	.message = multipath_message,
17079af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1708af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1709f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
17101da177e4SLinus Torvalds };
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds static int __init dm_multipath_init(void)
17131da177e4SLinus Torvalds {
17141da177e4SLinus Torvalds 	int r;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1717028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
17181da177e4SLinus Torvalds 	if (!_mpio_cache)
17191da177e4SLinus Torvalds 		return -ENOMEM;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
17221da177e4SLinus Torvalds 	if (r < 0) {
17230cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
17241da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
17251da177e4SLinus Torvalds 		return -EINVAL;
17261da177e4SLinus Torvalds 	}
17271da177e4SLinus Torvalds 
17284d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1729c557308eSAlasdair G Kergon 	if (!kmultipathd) {
17300cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1731c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1732c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1733c557308eSAlasdair G Kergon 		return -ENOMEM;
1734c557308eSAlasdair G Kergon 	}
1735c557308eSAlasdair G Kergon 
1736bab7cfc7SChandra Seetharaman 	/*
1737bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1738bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1739bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1740bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1741bab7cfc7SChandra Seetharaman 	 */
17424d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
17434d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1744bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1745bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1746bab7cfc7SChandra Seetharaman 		destroy_workqueue(kmultipathd);
1747bab7cfc7SChandra Seetharaman 		dm_unregister_target(&multipath_target);
1748bab7cfc7SChandra Seetharaman 		kmem_cache_destroy(_mpio_cache);
1749bab7cfc7SChandra Seetharaman 		return -ENOMEM;
1750bab7cfc7SChandra Seetharaman 	}
1751bab7cfc7SChandra Seetharaman 
175272d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
17531da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
17541da177e4SLinus Torvalds 	       multipath_target.version[2]);
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	return r;
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
17601da177e4SLinus Torvalds {
1761bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1762c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1763c557308eSAlasdair G Kergon 
176410d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
17651da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds module_init(dm_multipath_init);
17691da177e4SLinus Torvalds module_exit(dm_multipath_exit);
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
17721da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
17731da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1774