xref: /openbmc/linux/drivers/md/dm-mpath.c (revision cc9d3c382bc1674884c2e5e468d51230a9503dee)
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 
90855a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
90955a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
910042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
9118627921fSMikulas Patocka 
9121da177e4SLinus Torvalds 	return 0;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds  bad:
9151da177e4SLinus Torvalds 	free_multipath(m);
9161da177e4SLinus Torvalds 	return r;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9192bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
9202bded7bdSKiyoshi Ueda {
9212bded7bdSKiyoshi Ueda 	DECLARE_WAITQUEUE(wait, current);
9222bded7bdSKiyoshi Ueda 	unsigned long flags;
9232bded7bdSKiyoshi Ueda 
9242bded7bdSKiyoshi Ueda 	add_wait_queue(&m->pg_init_wait, &wait);
9252bded7bdSKiyoshi Ueda 
9262bded7bdSKiyoshi Ueda 	while (1) {
9272bded7bdSKiyoshi Ueda 		set_current_state(TASK_UNINTERRUPTIBLE);
9282bded7bdSKiyoshi Ueda 
9292bded7bdSKiyoshi Ueda 		spin_lock_irqsave(&m->lock, flags);
9302bded7bdSKiyoshi Ueda 		if (!m->pg_init_in_progress) {
9312bded7bdSKiyoshi Ueda 			spin_unlock_irqrestore(&m->lock, flags);
9322bded7bdSKiyoshi Ueda 			break;
9332bded7bdSKiyoshi Ueda 		}
9342bded7bdSKiyoshi Ueda 		spin_unlock_irqrestore(&m->lock, flags);
9352bded7bdSKiyoshi Ueda 
9362bded7bdSKiyoshi Ueda 		io_schedule();
9372bded7bdSKiyoshi Ueda 	}
9382bded7bdSKiyoshi Ueda 	set_current_state(TASK_RUNNING);
9392bded7bdSKiyoshi Ueda 
9402bded7bdSKiyoshi Ueda 	remove_wait_queue(&m->pg_init_wait, &wait);
9412bded7bdSKiyoshi Ueda }
9422bded7bdSKiyoshi Ueda 
9432bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
9441da177e4SLinus Torvalds {
945bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
9462bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
947a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
94843829731STejun Heo 	flush_work(&m->trigger_event);
9496df400abSKiyoshi Ueda }
9506df400abSKiyoshi Ueda 
9516df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
9526df400abSKiyoshi Ueda {
9536df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
9546df400abSKiyoshi Ueda 
9552bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
9561da177e4SLinus Torvalds 	free_multipath(m);
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds /*
960f40c67f0SKiyoshi Ueda  * Map cloned requests
9611da177e4SLinus Torvalds  */
962f40c67f0SKiyoshi Ueda static int multipath_map(struct dm_target *ti, struct request *clone,
9631da177e4SLinus Torvalds 			 union map_info *map_context)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	int r;
9661da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
9671da177e4SLinus Torvalds 
968466891f9SJun'ichi Nomura 	if (set_mapinfo(m, map_context) < 0)
969f40c67f0SKiyoshi Ueda 		/* ENOMEM, requeue */
970f40c67f0SKiyoshi Ueda 		return DM_MAPIO_REQUEUE;
9711da177e4SLinus Torvalds 
972f40c67f0SKiyoshi Ueda 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
973466891f9SJun'ichi Nomura 	r = map_io(m, clone, map_context, 0);
97445e15720SKiyoshi Ueda 	if (r < 0 || r == DM_MAPIO_REQUEUE)
975466891f9SJun'ichi Nomura 		clear_mapinfo(m, map_context);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	return r;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds /*
9811da177e4SLinus Torvalds  * Take a path out of use.
9821da177e4SLinus Torvalds  */
9831da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	unsigned long flags;
9861da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9891da177e4SLinus Torvalds 
9906680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9911da177e4SLinus Torvalds 		goto out;
9921da177e4SLinus Torvalds 
99372d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9966680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9971da177e4SLinus Torvalds 	pgpath->fail_count++;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	m->nr_valid_paths--;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
10021da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10031da177e4SLinus Torvalds 
1004b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1005b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1006b15546f9SMike Anderson 
1007fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds out:
10101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	return 0;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /*
10161da177e4SLinus Torvalds  * Reinstate a previously-failed path
10171da177e4SLinus Torvalds  */
10181da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	int r = 0;
10211da177e4SLinus Torvalds 	unsigned long flags;
10221da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10251da177e4SLinus Torvalds 
10266680073dSKiyoshi Ueda 	if (pgpath->is_active)
10271da177e4SLinus Torvalds 		goto out;
10281da177e4SLinus Torvalds 
1029def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
10301da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
10311da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
10321da177e4SLinus Torvalds 		r = -EINVAL;
10331da177e4SLinus Torvalds 		goto out;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
10371da177e4SLinus Torvalds 	if (r)
10381da177e4SLinus Torvalds 		goto out;
10391da177e4SLinus Torvalds 
10406680073dSKiyoshi Ueda 	pgpath->is_active = 1;
10411da177e4SLinus Torvalds 
1042e54f77ddSChandra Seetharaman 	if (!m->nr_valid_paths++ && m->queue_size) {
10431da177e4SLinus Torvalds 		m->current_pgpath = NULL;
1044c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
1045e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
10464e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1047e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
1048e54f77ddSChandra Seetharaman 	}
10491da177e4SLinus Torvalds 
1050b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1051b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1052b15546f9SMike Anderson 
1053fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds out:
10561da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	return r;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds /*
10621da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10631da177e4SLinus Torvalds  */
10641da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10651da177e4SLinus Torvalds 		      action_fn action)
10661da177e4SLinus Torvalds {
106719040c0bSMike Snitzer 	int r = -EINVAL;
10681da177e4SLinus Torvalds 	struct pgpath *pgpath;
10691da177e4SLinus Torvalds 	struct priority_group *pg;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10721da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10731da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10741da177e4SLinus Torvalds 				r = action(pgpath);
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	return r;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds /*
10821da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10831da177e4SLinus Torvalds  */
10841da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10851da177e4SLinus Torvalds 		      int bypassed)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	unsigned long flags;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10921da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10931da177e4SLinus Torvalds 	m->current_pg = NULL;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10961da177e4SLinus Torvalds 
1097fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds /*
11011da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
11021da177e4SLinus Torvalds  */
11031da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
11041da177e4SLinus Torvalds {
11051da177e4SLinus Torvalds 	struct priority_group *pg;
11061da177e4SLinus Torvalds 	unsigned pgnum;
11071da177e4SLinus Torvalds 	unsigned long flags;
110831998ef1SMikulas Patocka 	char dummy;
11091da177e4SLinus Torvalds 
111031998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11111da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11121da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
11131da177e4SLinus Torvalds 		return -EINVAL;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11171da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11181da177e4SLinus Torvalds 		pg->bypassed = 0;
11191da177e4SLinus Torvalds 		if (--pgnum)
11201da177e4SLinus Torvalds 			continue;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 		m->current_pgpath = NULL;
11231da177e4SLinus Torvalds 		m->current_pg = NULL;
11241da177e4SLinus Torvalds 		m->next_pg = pg;
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
11271da177e4SLinus Torvalds 
1128fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11291da177e4SLinus Torvalds 	return 0;
11301da177e4SLinus Torvalds }
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds /*
11331da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
11341da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
11351da177e4SLinus Torvalds  */
11361da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	struct priority_group *pg;
11391da177e4SLinus Torvalds 	unsigned pgnum;
114031998ef1SMikulas Patocka 	char dummy;
11411da177e4SLinus Torvalds 
114231998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11431da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11441da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
11451da177e4SLinus Torvalds 		return -EINVAL;
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11491da177e4SLinus Torvalds 		if (!--pgnum)
11501da177e4SLinus Torvalds 			break;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
11541da177e4SLinus Torvalds 	return 0;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds /*
1158c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1159c9e45581SDave Wysochanski  */
1160c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1161c9e45581SDave Wysochanski {
1162c9e45581SDave Wysochanski 	unsigned long flags;
1163c9e45581SDave Wysochanski 	int limit_reached = 0;
1164c9e45581SDave Wysochanski 
1165c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1166c9e45581SDave Wysochanski 
1167c9e45581SDave Wysochanski 	if (m->pg_init_count <= m->pg_init_retries)
1168c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1169c9e45581SDave Wysochanski 	else
1170c9e45581SDave Wysochanski 		limit_reached = 1;
1171c9e45581SDave Wysochanski 
1172c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1173c9e45581SDave Wysochanski 
1174c9e45581SDave Wysochanski 	return limit_reached;
1175c9e45581SDave Wysochanski }
1176c9e45581SDave Wysochanski 
11773ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1178cfae5c9bSChandra Seetharaman {
117983c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1180cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1181cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1182cfae5c9bSChandra Seetharaman 	unsigned long flags;
11834e2d19e4SChandra Seetharaman 	unsigned delay_retry = 0;
1184cfae5c9bSChandra Seetharaman 
1185cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1186cfae5c9bSChandra Seetharaman 	switch (errors) {
1187cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1188cfae5c9bSChandra Seetharaman 		break;
1189cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1190cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1191cfae5c9bSChandra Seetharaman 			errors = 0;
1192cfae5c9bSChandra Seetharaman 			break;
1193cfae5c9bSChandra Seetharaman 		}
1194f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1195f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1196cfae5c9bSChandra Seetharaman 		/*
1197cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1198cfae5c9bSChandra Seetharaman 		 */
1199cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1200cfae5c9bSChandra Seetharaman 		break;
1201cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1202cfae5c9bSChandra Seetharaman 		/*
1203cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1204cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1205cfae5c9bSChandra Seetharaman 		 */
1206cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1207cfae5c9bSChandra Seetharaman 		break;
1208cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
12094e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
12104e2d19e4SChandra Seetharaman 		delay_retry = 1;
1211cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1212cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1213cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1214cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1215cfae5c9bSChandra Seetharaman 		errors = 0;
1216cfae5c9bSChandra Seetharaman 		break;
1217cfae5c9bSChandra Seetharaman 	default:
1218cfae5c9bSChandra Seetharaman 		/*
1219cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1220cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1221cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1222cfae5c9bSChandra Seetharaman 		 */
1223cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1224cfae5c9bSChandra Seetharaman 	}
1225cfae5c9bSChandra Seetharaman 
1226cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1227cfae5c9bSChandra Seetharaman 	if (errors) {
1228e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1229cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1230cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1231cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1232e54f77ddSChandra Seetharaman 		}
1233d0259bf0SKiyoshi Ueda 	} else if (!m->pg_init_required)
1234cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1235cfae5c9bSChandra Seetharaman 
1236d0259bf0SKiyoshi Ueda 	if (--m->pg_init_in_progress)
1237d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1238d0259bf0SKiyoshi Ueda 		goto out;
1239d0259bf0SKiyoshi Ueda 
1240d0259bf0SKiyoshi Ueda 	if (!m->pg_init_required)
1241d0259bf0SKiyoshi Ueda 		m->queue_io = 0;
1242d0259bf0SKiyoshi Ueda 
12434e2d19e4SChandra Seetharaman 	m->pg_init_delay_retry = delay_retry;
1244cfae5c9bSChandra Seetharaman 	queue_work(kmultipathd, &m->process_queued_ios);
1245d0259bf0SKiyoshi Ueda 
12462bded7bdSKiyoshi Ueda 	/*
12472bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
12482bded7bdSKiyoshi Ueda 	 */
12492bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
12502bded7bdSKiyoshi Ueda 
1251d0259bf0SKiyoshi Ueda out:
1252cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1253cfae5c9bSChandra Seetharaman }
1254cfae5c9bSChandra Seetharaman 
1255bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1256bab7cfc7SChandra Seetharaman {
1257e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
12584e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1259bab7cfc7SChandra Seetharaman 
12603ae31f6aSChandra Seetharaman 	scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
126183c0d5d5SMoger, Babu 				pg_init_done, pgpath);
1262bab7cfc7SChandra Seetharaman }
1263bab7cfc7SChandra Seetharaman 
12647e782af5SHannes Reinecke static int noretry_error(int error)
12657e782af5SHannes Reinecke {
12667e782af5SHannes Reinecke 	switch (error) {
12677e782af5SHannes Reinecke 	case -EOPNOTSUPP:
12687e782af5SHannes Reinecke 	case -EREMOTEIO:
12697e782af5SHannes Reinecke 	case -EILSEQ:
12707e782af5SHannes Reinecke 	case -ENODATA:
1271*cc9d3c38SJun'ichi Nomura 	case -ENOSPC:
12727e782af5SHannes Reinecke 		return 1;
12737e782af5SHannes Reinecke 	}
12747e782af5SHannes Reinecke 
12757e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
12767e782af5SHannes Reinecke 	return 0;
12777e782af5SHannes Reinecke }
12787e782af5SHannes Reinecke 
12791da177e4SLinus Torvalds /*
12801da177e4SLinus Torvalds  * end_io handling
12811da177e4SLinus Torvalds  */
1282f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1283028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12841da177e4SLinus Torvalds {
1285f40c67f0SKiyoshi Ueda 	/*
1286f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1287f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1288f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1289f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1290f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1291f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1292f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1293f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1294f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1295f40c67f0SKiyoshi Ueda 	 */
1296f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1297640eb3b0SStefan Bader 	unsigned long flags;
12981da177e4SLinus Torvalds 
1299f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
13001da177e4SLinus Torvalds 		return 0;	/* I/O complete */
13011da177e4SLinus Torvalds 
13027e782af5SHannes Reinecke 	if (noretry_error(error))
1303959eb4e5SMike Snitzer 		return error;
1304959eb4e5SMike Snitzer 
1305cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
13061da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
13071da177e4SLinus Torvalds 
1308640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1309751b2a7dSHannes Reinecke 	if (!m->nr_valid_paths) {
1310751b2a7dSHannes Reinecke 		if (!m->queue_if_no_path) {
1311751b2a7dSHannes Reinecke 			if (!__must_push_back(m))
1312f40c67f0SKiyoshi Ueda 				r = -EIO;
1313751b2a7dSHannes Reinecke 		} else {
1314751b2a7dSHannes Reinecke 			if (error == -EBADE)
1315751b2a7dSHannes Reinecke 				r = error;
1316751b2a7dSHannes Reinecke 		}
1317751b2a7dSHannes Reinecke 	}
1318640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
13191da177e4SLinus Torvalds 
1320f40c67f0SKiyoshi Ueda 	return r;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
1323f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
13241da177e4SLinus Torvalds 			    int error, union map_info *map_context)
13251da177e4SLinus Torvalds {
1326028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1327028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
1328a71a261fSWei Yongjun 	struct pgpath *pgpath;
13291da177e4SLinus Torvalds 	struct path_selector *ps;
13301da177e4SLinus Torvalds 	int r;
13311da177e4SLinus Torvalds 
1332466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1333466891f9SJun'ichi Nomura 
1334f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
1335a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
13361da177e4SLinus Torvalds 	if (pgpath) {
13371da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
13381da177e4SLinus Torvalds 		if (ps->type->end_io)
133902ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
13401da177e4SLinus Torvalds 	}
1341466891f9SJun'ichi Nomura 	clear_mapinfo(m, map_context);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	return r;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds /*
13471da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1348436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1349436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1350436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
13511da177e4SLinus Torvalds  */
13521da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13551da177e4SLinus Torvalds 
1356485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
13596df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
13606df400abSKiyoshi Ueda {
13616380f26fSMike Anderson 	struct multipath *m = ti->private;
13626380f26fSMike Anderson 
13636380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
13642bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
13656380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
13666df400abSKiyoshi Ueda }
13676df400abSKiyoshi Ueda 
1368436d4108SAlasdair G Kergon /*
1369436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1370436d4108SAlasdair G Kergon  */
13711da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13741da177e4SLinus Torvalds 	unsigned long flags;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1377436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
13781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds /*
13821da177e4SLinus Torvalds  * Info output has the following format:
13831da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
13841da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
13851da177e4SLinus Torvalds  * num_groups init_group_number
13861da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
13871da177e4SLinus Torvalds  *             num_paths num_selector_args
13881da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
13891da177e4SLinus Torvalds  *
13901da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
13911da177e4SLinus Torvalds  * num_feature_args [features_args]*
13921da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
13931da177e4SLinus Torvalds  * num_groups init_group_number
13941da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
13951da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
13961da177e4SLinus Torvalds  */
1397fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
13981f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	int sz = 0;
14011da177e4SLinus Torvalds 	unsigned long flags;
14021da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14031da177e4SLinus Torvalds 	struct priority_group *pg;
14041da177e4SLinus Torvalds 	struct pgpath *p;
14051da177e4SLinus Torvalds 	unsigned pg_num;
14061da177e4SLinus Torvalds 	char state;
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	/* Features */
14111da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1412c9e45581SDave Wysochanski 		DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1413c9e45581SDave Wysochanski 	else {
1414c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
14154e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1416a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1417a58a935dSMike Snitzer 			      m->retain_attached_hw_handler);
1418c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1419c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1420c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1421c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
14224e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
14234e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1424a58a935dSMike Snitzer 		if (m->retain_attached_hw_handler)
1425a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1426c9e45581SDave Wysochanski 	}
14271da177e4SLinus Torvalds 
1428cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
14291da177e4SLinus Torvalds 		DMEMIT("0 ");
14301da177e4SLinus Torvalds 	else
1431cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	if (m->next_pg)
14361da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
14371da177e4SLinus Torvalds 	else if (m->current_pg)
14381da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
14391da177e4SLinus Torvalds 	else
1440a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	switch (type) {
14451da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
14461da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14471da177e4SLinus Torvalds 			if (pg->bypassed)
14481da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
14491da177e4SLinus Torvalds 			else if (pg == m->current_pg)
14501da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
14511da177e4SLinus Torvalds 			else
14521da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 			DMEMIT("%c ", state);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 			if (pg->ps.type->status)
14571da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14581da177e4SLinus Torvalds 							  result + sz,
14591da177e4SLinus Torvalds 							  maxlen - sz);
14601da177e4SLinus Torvalds 			else
14611da177e4SLinus Torvalds 				DMEMIT("0 ");
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14641da177e4SLinus Torvalds 			       pg->ps.type->info_args);
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14671da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
14686680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
14691da177e4SLinus Torvalds 				       p->fail_count);
14701da177e4SLinus Torvalds 				if (pg->ps.type->status)
14711da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14721da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14731da177e4SLinus Torvalds 					      maxlen - sz);
14741da177e4SLinus Torvalds 			}
14751da177e4SLinus Torvalds 		}
14761da177e4SLinus Torvalds 		break;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
14791da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14801da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 			if (pg->ps.type->status)
14831da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14841da177e4SLinus Torvalds 							  result + sz,
14851da177e4SLinus Torvalds 							  maxlen - sz);
14861da177e4SLinus Torvalds 			else
14871da177e4SLinus Torvalds 				DMEMIT("0 ");
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14901da177e4SLinus Torvalds 			       pg->ps.type->table_args);
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14931da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
14941da177e4SLinus Torvalds 				if (pg->ps.type->status)
14951da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14961da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14971da177e4SLinus Torvalds 					      maxlen - sz);
14981da177e4SLinus Torvalds 			}
14991da177e4SLinus Torvalds 		}
15001da177e4SLinus Torvalds 		break;
15011da177e4SLinus Torvalds 	}
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
15071da177e4SLinus Torvalds {
15086380f26fSMike Anderson 	int r = -EINVAL;
15091da177e4SLinus Torvalds 	struct dm_dev *dev;
15101da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
15111da177e4SLinus Torvalds 	action_fn action;
15121da177e4SLinus Torvalds 
15136380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
15146380f26fSMike Anderson 
1515c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1516c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1517c2f3d24bSKiyoshi Ueda 		goto out;
1518c2f3d24bSKiyoshi Ueda 	}
1519c2f3d24bSKiyoshi Ueda 
15201da177e4SLinus Torvalds 	if (argc == 1) {
1521498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
15226380f26fSMike Anderson 			r = queue_if_no_path(m, 1, 0);
15236380f26fSMike Anderson 			goto out;
1524498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
15256380f26fSMike Anderson 			r = queue_if_no_path(m, 0, 0);
15266380f26fSMike Anderson 			goto out;
15276380f26fSMike Anderson 		}
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 
15306380f26fSMike Anderson 	if (argc != 2) {
15316380f26fSMike Anderson 		DMWARN("Unrecognised multipath message received.");
15326380f26fSMike Anderson 		goto out;
15336380f26fSMike Anderson 	}
15341da177e4SLinus Torvalds 
1535498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
15366380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 1);
15376380f26fSMike Anderson 		goto out;
1538498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
15396380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 0);
15406380f26fSMike Anderson 		goto out;
1541498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
15426380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
15436380f26fSMike Anderson 		goto out;
1544498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
15451da177e4SLinus Torvalds 		action = reinstate_path;
1546498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
15471da177e4SLinus Torvalds 		action = fail_path;
15486380f26fSMike Anderson 	else {
15496380f26fSMike Anderson 		DMWARN("Unrecognised multipath message received.");
15506380f26fSMike Anderson 		goto out;
15516380f26fSMike Anderson 	}
15521da177e4SLinus Torvalds 
15538215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
15541da177e4SLinus Torvalds 	if (r) {
155572d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
15561da177e4SLinus Torvalds 		       argv[1]);
15576380f26fSMike Anderson 		goto out;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	dm_put_device(ti, dev);
15631da177e4SLinus Torvalds 
15646380f26fSMike Anderson out:
15656380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15661da177e4SLinus Torvalds 	return r;
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
1569647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
15709af4aa30SMilan Broz 			   unsigned long arg)
15719af4aa30SMilan Broz {
157235991652SMikulas Patocka 	struct multipath *m = ti->private;
15737ba10aa6SMike Snitzer 	struct pgpath *pgpath;
157435991652SMikulas Patocka 	struct block_device *bdev;
157535991652SMikulas Patocka 	fmode_t mode;
15769af4aa30SMilan Broz 	unsigned long flags;
157735991652SMikulas Patocka 	int r;
157835991652SMikulas Patocka 
157935991652SMikulas Patocka 	bdev = NULL;
158035991652SMikulas Patocka 	mode = 0;
158135991652SMikulas Patocka 	r = 0;
15829af4aa30SMilan Broz 
15839af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
15849af4aa30SMilan Broz 
15859af4aa30SMilan Broz 	if (!m->current_pgpath)
158602ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
15879af4aa30SMilan Broz 
15887ba10aa6SMike Snitzer 	pgpath = m->current_pgpath;
15897ba10aa6SMike Snitzer 
15907ba10aa6SMike Snitzer 	if (pgpath) {
15917ba10aa6SMike Snitzer 		bdev = pgpath->path.dev->bdev;
15927ba10aa6SMike Snitzer 		mode = pgpath->path.dev->mode;
1593e90dae1fSMilan Broz 	}
15949af4aa30SMilan Broz 
15957ba10aa6SMike Snitzer 	if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
15966c182cd8SHannes Reinecke 		r = -ENOTCONN;
15979af4aa30SMilan Broz 	else if (!bdev)
15989af4aa30SMilan Broz 		r = -EIO;
15999af4aa30SMilan Broz 
16009af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
16019af4aa30SMilan Broz 
1602ec8013beSPaolo Bonzini 	/*
1603ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
1604ec8013beSPaolo Bonzini 	 */
1605ec8013beSPaolo Bonzini 	if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
1606ec8013beSPaolo Bonzini 		r = scsi_verify_blk_ioctl(NULL, cmd);
1607ec8013beSPaolo Bonzini 
16086c182cd8SHannes Reinecke 	if (r == -ENOTCONN && !fatal_signal_pending(current))
160935991652SMikulas Patocka 		queue_work(kmultipathd, &m->process_queued_ios);
161035991652SMikulas Patocka 
1611633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
16129af4aa30SMilan Broz }
16139af4aa30SMilan Broz 
1614af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1615af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1616af4874e0SMike Snitzer {
1617af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1618af4874e0SMike Snitzer 	struct priority_group *pg;
1619af4874e0SMike Snitzer 	struct pgpath *p;
1620af4874e0SMike Snitzer 	int ret = 0;
1621af4874e0SMike Snitzer 
1622af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1623af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
16245dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1625af4874e0SMike Snitzer 			if (ret)
1626af4874e0SMike Snitzer 				goto out;
1627af4874e0SMike Snitzer 		}
1628af4874e0SMike Snitzer 	}
1629af4874e0SMike Snitzer 
1630af4874e0SMike Snitzer out:
1631af4874e0SMike Snitzer 	return ret;
1632af4874e0SMike Snitzer }
1633af4874e0SMike Snitzer 
1634f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1635f40c67f0SKiyoshi Ueda {
1636f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1637f40c67f0SKiyoshi Ueda 
1638f40c67f0SKiyoshi Ueda 	return dm_underlying_device_busy(q);
1639f40c67f0SKiyoshi Ueda }
1640f40c67f0SKiyoshi Ueda 
1641f40c67f0SKiyoshi Ueda /*
1642f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1643f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1644f40c67f0SKiyoshi Ueda  * the underlying queue).
1645f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1646f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1647f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1648f40c67f0SKiyoshi Ueda  */
1649f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1650f40c67f0SKiyoshi Ueda {
1651f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1652f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1653f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1654f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1655f40c67f0SKiyoshi Ueda 	unsigned long flags;
1656f40c67f0SKiyoshi Ueda 
1657f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1658f40c67f0SKiyoshi Ueda 
1659f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1660f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1661f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1662f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1663f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1664f40c67f0SKiyoshi Ueda 	else
1665f40c67f0SKiyoshi Ueda 		/*
1666f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1667f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1668f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1669f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1670f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1671f40c67f0SKiyoshi Ueda 		 */
1672f40c67f0SKiyoshi Ueda 		goto out;
1673f40c67f0SKiyoshi Ueda 
1674f40c67f0SKiyoshi Ueda 	/*
1675f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1676f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1677f40c67f0SKiyoshi Ueda 	 */
1678f40c67f0SKiyoshi Ueda 	busy = 1;
1679f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1680f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1681f40c67f0SKiyoshi Ueda 			has_active = 1;
1682f40c67f0SKiyoshi Ueda 
1683f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1684f40c67f0SKiyoshi Ueda 				busy = 0;
1685f40c67f0SKiyoshi Ueda 				break;
1686f40c67f0SKiyoshi Ueda 			}
1687f40c67f0SKiyoshi Ueda 		}
1688f40c67f0SKiyoshi Ueda 
1689f40c67f0SKiyoshi Ueda 	if (!has_active)
1690f40c67f0SKiyoshi Ueda 		/*
1691f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1692f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1693f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1694f40c67f0SKiyoshi Ueda 		 */
1695f40c67f0SKiyoshi Ueda 		busy = 0;
1696f40c67f0SKiyoshi Ueda 
1697f40c67f0SKiyoshi Ueda out:
1698f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1699f40c67f0SKiyoshi Ueda 
1700f40c67f0SKiyoshi Ueda 	return busy;
1701f40c67f0SKiyoshi Ueda }
1702f40c67f0SKiyoshi Ueda 
17031da177e4SLinus Torvalds /*-----------------------------------------------------------------
17041da177e4SLinus Torvalds  * Module setup
17051da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17061da177e4SLinus Torvalds static struct target_type multipath_target = {
17071da177e4SLinus Torvalds 	.name = "multipath",
1708fd7c092eSMikulas Patocka 	.version = {1, 5, 1},
17091da177e4SLinus Torvalds 	.module = THIS_MODULE,
17101da177e4SLinus Torvalds 	.ctr = multipath_ctr,
17111da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1712f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1713f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
17141da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
17156df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
17161da177e4SLinus Torvalds 	.resume = multipath_resume,
17171da177e4SLinus Torvalds 	.status = multipath_status,
17181da177e4SLinus Torvalds 	.message = multipath_message,
17199af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1720af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1721f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
17221da177e4SLinus Torvalds };
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds static int __init dm_multipath_init(void)
17251da177e4SLinus Torvalds {
17261da177e4SLinus Torvalds 	int r;
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1729028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
17301da177e4SLinus Torvalds 	if (!_mpio_cache)
17311da177e4SLinus Torvalds 		return -ENOMEM;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
17341da177e4SLinus Torvalds 	if (r < 0) {
17350cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
17361da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
17371da177e4SLinus Torvalds 		return -EINVAL;
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds 
17404d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1741c557308eSAlasdair G Kergon 	if (!kmultipathd) {
17420cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1743c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1744c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1745c557308eSAlasdair G Kergon 		return -ENOMEM;
1746c557308eSAlasdair G Kergon 	}
1747c557308eSAlasdair G Kergon 
1748bab7cfc7SChandra Seetharaman 	/*
1749bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1750bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1751bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1752bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1753bab7cfc7SChandra Seetharaman 	 */
17544d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
17554d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1756bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1757bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1758bab7cfc7SChandra Seetharaman 		destroy_workqueue(kmultipathd);
1759bab7cfc7SChandra Seetharaman 		dm_unregister_target(&multipath_target);
1760bab7cfc7SChandra Seetharaman 		kmem_cache_destroy(_mpio_cache);
1761bab7cfc7SChandra Seetharaman 		return -ENOMEM;
1762bab7cfc7SChandra Seetharaman 	}
1763bab7cfc7SChandra Seetharaman 
176472d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
17651da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
17661da177e4SLinus Torvalds 	       multipath_target.version[2]);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	return r;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
17721da177e4SLinus Torvalds {
1773bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1774c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1775c557308eSAlasdair G Kergon 
177610d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
17771da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds module_init(dm_multipath_init);
17811da177e4SLinus Torvalds module_exit(dm_multipath_exit);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
17841da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
17851da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1786