xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 518257b13276d07a19e6ae0608b8e5ee73383ce4)
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 
10f4790826SMike Snitzer #include "dm.h"
111da177e4SLinus Torvalds #include "dm-path-selector.h"
12b15546f9SMike Anderson #include "dm-uevent.h"
131da177e4SLinus Torvalds 
14e5863d9aSMike Snitzer #include <linux/blkdev.h>
151da177e4SLinus Torvalds #include <linux/ctype.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/time.h>
221da177e4SLinus Torvalds #include <linux/workqueue.h>
2335991652SMikulas Patocka #include <linux/delay.h>
24cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2560063497SArun Sharma #include <linux/atomic.h>
2678ce23b5SMike Snitzer #include <linux/blk-mq.h>
271da177e4SLinus Torvalds 
2872d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
294e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
304e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /* Path properties */
331da177e4SLinus Torvalds struct pgpath {
341da177e4SLinus Torvalds 	struct list_head list;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
371da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
381da177e4SLinus Torvalds 
39c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
404e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
41be7d31ccSMike Snitzer 
42be7d31ccSMike Snitzer 	bool is_active:1;		/* Path status */
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*
481da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
491da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds struct priority_group {
521da177e4SLinus Torvalds 	struct list_head list;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
551da177e4SLinus Torvalds 	struct path_selector ps;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
581da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
591da177e4SLinus Torvalds 	struct list_head pgpaths;
60be7d31ccSMike Snitzer 
61be7d31ccSMike Snitzer 	bool bypassed:1;		/* Temporarily bypass this PG? */
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Multipath context */
651da177e4SLinus Torvalds struct multipath {
661da177e4SLinus Torvalds 	struct list_head list;
671da177e4SLinus Torvalds 	struct dm_target *ti;
681da177e4SLinus Torvalds 
69cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
702bfd2e13SChandra Seetharaman 	char *hw_handler_params;
714e2d19e4SChandra Seetharaman 
721fbdd2b3SMike Snitzer 	spinlock_t lock;
731fbdd2b3SMike Snitzer 
741da177e4SLinus Torvalds 	unsigned nr_priority_groups;
751da177e4SLinus Torvalds 	struct list_head priority_groups;
764e2d19e4SChandra Seetharaman 
774e2d19e4SChandra Seetharaman 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
784e2d19e4SChandra Seetharaman 
79c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
821da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
831da177e4SLinus Torvalds 	struct priority_group *current_pg;
841da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
851da177e4SLinus Torvalds 
86*518257b1SMike Snitzer 	unsigned long flags;		/* Multipath state flags */
871fbdd2b3SMike Snitzer 
88c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
89c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
904e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	struct work_struct trigger_event;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/*
95028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
961da177e4SLinus Torvalds 	 * can resubmit bios on error.
971da177e4SLinus Torvalds 	 */
981da177e4SLinus Torvalds 	mempool_t *mpio_pool;
996380f26fSMike Anderson 
1006380f26fSMike Anderson 	struct mutex work_mutex;
1011da177e4SLinus Torvalds };
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Context information attached to each bio we process.
1051da177e4SLinus Torvalds  */
106028867acSAlasdair G Kergon struct dm_mpath_io {
1071da177e4SLinus Torvalds 	struct pgpath *pgpath;
10802ab823fSKiyoshi Ueda 	size_t nr_bytes;
1091da177e4SLinus Torvalds };
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1121da177e4SLinus Torvalds 
113e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1141da177e4SLinus Torvalds 
115bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
116c4028958SDavid Howells static void trigger_event(struct work_struct *work);
117bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
1181da177e4SLinus Torvalds 
119*518257b1SMike Snitzer /*-----------------------------------------------
120*518257b1SMike Snitzer  * Multipath state flags.
121*518257b1SMike Snitzer  *-----------------------------------------------*/
122*518257b1SMike Snitzer 
123*518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0			/* Must we queue all I/O? */
124*518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1		/* Queue I/O if last path fails? */
125*518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2		/* Saved state during suspension */
126*518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3	/* If there's already a hw_handler present, don't change it. */
127*518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4		/* pg_init is not currently allowed */
128*518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5		/* pg_init needs calling? */
129*518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6		/* Delay pg_init retry? */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*-----------------------------------------------
1321da177e4SLinus Torvalds  * Allocation routines
1331da177e4SLinus Torvalds  *-----------------------------------------------*/
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1361da177e4SLinus Torvalds {
137e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1381da177e4SLinus Torvalds 
139224cb3e9SMike Anderson 	if (pgpath) {
140be7d31ccSMike Snitzer 		pgpath->is_active = true;
1414e2d19e4SChandra Seetharaman 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
142224cb3e9SMike Anderson 	}
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	return pgpath;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	kfree(pgpath);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct priority_group *pg;
1551da177e4SLinus Torvalds 
156e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1571da177e4SLinus Torvalds 
158e69fae56SMicha³ Miros³aw 	if (pg)
1591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	return pg;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1691da177e4SLinus Torvalds 		list_del(&pgpath->list);
1701da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1711da177e4SLinus Torvalds 		free_pgpath(pgpath);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1761da177e4SLinus Torvalds 				struct dm_target *ti)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (ps->type) {
1811da177e4SLinus Torvalds 		ps->type->destroy(ps);
1821da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1861da177e4SLinus Torvalds 	kfree(pg);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1898637a6bfSMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct multipath *m;
1921da177e4SLinus Torvalds 
193e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1941da177e4SLinus Torvalds 	if (m) {
1951da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1961da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
197*518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
1984e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
199c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
2002bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
2016380f26fSMike Anderson 		mutex_init(&m->work_mutex);
2028637a6bfSMike Snitzer 
2038637a6bfSMike Snitzer 		m->mpio_pool = NULL;
2048637a6bfSMike Snitzer 		if (!use_blk_mq) {
2058637a6bfSMike Snitzer 			unsigned min_ios = dm_get_reserved_rq_based_ios();
2068637a6bfSMike Snitzer 
207f4790826SMike Snitzer 			m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
2081da177e4SLinus Torvalds 			if (!m->mpio_pool) {
2091da177e4SLinus Torvalds 				kfree(m);
2101da177e4SLinus Torvalds 				return NULL;
2111da177e4SLinus Torvalds 			}
2128637a6bfSMike Snitzer 		}
2138637a6bfSMike Snitzer 
21428f16c20SMicha³ Miros³aw 		m->ti = ti;
21528f16c20SMicha³ Miros³aw 		ti->private = m;
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	return m;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2261da177e4SLinus Torvalds 		list_del(&pg->list);
2271da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
230cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2312bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2321da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2331da177e4SLinus Torvalds 	kfree(m);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2362eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2372eff1924SMike Snitzer {
2382eff1924SMike Snitzer 	return info->ptr;
2392eff1924SMike Snitzer }
2402eff1924SMike Snitzer 
2412eff1924SMike Snitzer static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
242466891f9SJun'ichi Nomura {
243466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio;
244466891f9SJun'ichi Nomura 
2458637a6bfSMike Snitzer 	if (!m->mpio_pool) {
2468637a6bfSMike Snitzer 		/* Use blk-mq pdu memory requested via per_io_data_size */
2472eff1924SMike Snitzer 		mpio = get_mpio(info);
2488637a6bfSMike Snitzer 		memset(mpio, 0, sizeof(*mpio));
2498637a6bfSMike Snitzer 		return mpio;
2508637a6bfSMike Snitzer 	}
2518637a6bfSMike Snitzer 
252466891f9SJun'ichi Nomura 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
253466891f9SJun'ichi Nomura 	if (!mpio)
2542eff1924SMike Snitzer 		return NULL;
255466891f9SJun'ichi Nomura 
256466891f9SJun'ichi Nomura 	memset(mpio, 0, sizeof(*mpio));
257466891f9SJun'ichi Nomura 	info->ptr = mpio;
258466891f9SJun'ichi Nomura 
2592eff1924SMike Snitzer 	return mpio;
260466891f9SJun'ichi Nomura }
261466891f9SJun'ichi Nomura 
2622eff1924SMike Snitzer static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
263466891f9SJun'ichi Nomura {
2642eff1924SMike Snitzer 	/* Only needed for non blk-mq (.request_fn) multipath */
2658637a6bfSMike Snitzer 	if (m->mpio_pool) {
266466891f9SJun'ichi Nomura 		struct dm_mpath_io *mpio = info->ptr;
267466891f9SJun'ichi Nomura 
268466891f9SJun'ichi Nomura 		info->ptr = NULL;
269466891f9SJun'ichi Nomura 		mempool_free(mpio, m->mpio_pool);
270466891f9SJun'ichi Nomura 	}
2718637a6bfSMike Snitzer }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds /*-----------------------------------------------
2741da177e4SLinus Torvalds  * Path selection
2751da177e4SLinus Torvalds  *-----------------------------------------------*/
2761da177e4SLinus Torvalds 
2773e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
278fb612642SKiyoshi Ueda {
279fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2804e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
281fb612642SKiyoshi Ueda 
282*518257b1SMike Snitzer 	if (m->pg_init_in_progress || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
2833e9f1be1SHannes Reinecke 		return 0;
28417f4ff45SHannes Reinecke 
285fb612642SKiyoshi Ueda 	m->pg_init_count++;
286*518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
2873e9f1be1SHannes Reinecke 
2883e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
2893e9f1be1SHannes Reinecke 	if (!m->current_pg)
2903e9f1be1SHannes Reinecke 		return 0;
2913e9f1be1SHannes Reinecke 
292*518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
2934e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
2944e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
295fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
296fb612642SKiyoshi Ueda 		/* Skip failed paths */
297fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
298fb612642SKiyoshi Ueda 			continue;
2994e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3004e2d19e4SChandra Seetharaman 				       pg_init_delay))
301fb612642SKiyoshi Ueda 			m->pg_init_in_progress++;
302fb612642SKiyoshi Ueda 	}
3033e9f1be1SHannes Reinecke 	return m->pg_init_in_progress;
304fb612642SKiyoshi Ueda }
305fb612642SKiyoshi Ueda 
3061da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
311cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
312*518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
313*518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
3141da177e4SLinus Torvalds 	} else {
315*518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
316*518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3171da177e4SLinus Torvalds 	}
318c9e45581SDave Wysochanski 
319c9e45581SDave Wysochanski 	m->pg_init_count = 0;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
32202ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
32302ab823fSKiyoshi Ueda 			       size_t nr_bytes)
3241da177e4SLinus Torvalds {
325c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3261da177e4SLinus Torvalds 
32790a4323cSMike Snitzer 	path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3281da177e4SLinus Torvalds 	if (!path)
3291da177e4SLinus Torvalds 		return -ENXIO;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (m->current_pg != pg)
3341da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
33902ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct priority_group *pg;
342be7d31ccSMike Snitzer 	bool bypassed = true;
3431da177e4SLinus Torvalds 
3441f271972SBenjamin Marzinski 	if (!m->nr_valid_paths) {
345*518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3461da177e4SLinus Torvalds 		goto failed;
3471f271972SBenjamin Marzinski 	}
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3501da177e4SLinus Torvalds 	if (m->next_pg) {
3511da177e4SLinus Torvalds 		pg = m->next_pg;
3521da177e4SLinus Torvalds 		m->next_pg = NULL;
35302ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
3541da177e4SLinus Torvalds 			return;
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
35802ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
3591da177e4SLinus Torvalds 		return;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/*
3621da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
3631da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
364f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
365f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
3661da177e4SLinus Torvalds 	 */
3671da177e4SLinus Torvalds 	do {
3681da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
3691da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
3701da177e4SLinus Torvalds 				continue;
371f220fd4eSMike Christie 			if (!__choose_path_in_pg(m, pg, nr_bytes)) {
372f220fd4eSMike Christie 				if (!bypassed)
373*518257b1SMike Snitzer 					set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
3741da177e4SLinus Torvalds 				return;
3751da177e4SLinus Torvalds 			}
376f220fd4eSMike Christie 		}
3771da177e4SLinus Torvalds 	} while (bypassed--);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds failed:
3801da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3811da177e4SLinus Torvalds 	m->current_pg = NULL;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
38445e15720SKiyoshi Ueda /*
38545e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
38645e15720SKiyoshi Ueda  * than here in the target.
38745e15720SKiyoshi Ueda  *
38845e15720SKiyoshi Ueda  * m->lock must be held on entry.
38945e15720SKiyoshi Ueda  *
39045e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
39145e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
39245e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
39345e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
39445e15720SKiyoshi Ueda  */
39545e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
39645e15720SKiyoshi Ueda {
397*518257b1SMike Snitzer 	return (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
398*518257b1SMike Snitzer 		((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
399*518257b1SMike Snitzer 		  test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
400e8099177SHannes Reinecke 		 dm_noflush_suspending(m->ti)));
40145e15720SKiyoshi Ueda }
40245e15720SKiyoshi Ueda 
40336fcffccSHannes Reinecke /*
40436fcffccSHannes Reinecke  * Map cloned requests
40536fcffccSHannes Reinecke  */
406e5863d9aSMike Snitzer static int __multipath_map(struct dm_target *ti, struct request *clone,
407e5863d9aSMike Snitzer 			   union map_info *map_context,
408e5863d9aSMike Snitzer 			   struct request *rq, struct request **__clone)
4091da177e4SLinus Torvalds {
4107943bd6dSMike Snitzer 	struct multipath *m = ti->private;
411e3bde04fSHannes Reinecke 	int r = DM_MAPIO_REQUEUE;
412e5863d9aSMike Snitzer 	size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
4131da177e4SLinus Torvalds 	struct pgpath *pgpath;
414f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
415e3bde04fSHannes Reinecke 	struct dm_mpath_io *mpio;
4161da177e4SLinus Torvalds 
4172eb6e1e3SKeith Busch 	spin_lock_irq(&m->lock);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
420*518257b1SMike Snitzer 	if (!m->current_pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
42102ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
4241da177e4SLinus Torvalds 
4259bf59a61SMike Snitzer 	if (!pgpath) {
4269bf59a61SMike Snitzer 		if (!__must_push_back(m))
4279bf59a61SMike Snitzer 			r = -EIO;	/* Failed */
4289bf59a61SMike Snitzer 		goto out_unlock;
429*518257b1SMike Snitzer 	} else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
430*518257b1SMike Snitzer 		   test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
4319bf59a61SMike Snitzer 		__pg_init_all_paths(m);
4329bf59a61SMike Snitzer 		goto out_unlock;
4339bf59a61SMike Snitzer 	}
4346afbc01dSMike Snitzer 
4352eff1924SMike Snitzer 	mpio = set_mpio(m, map_context);
4362eff1924SMike Snitzer 	if (!mpio)
437e3bde04fSHannes Reinecke 		/* ENOMEM, requeue */
438e3bde04fSHannes Reinecke 		goto out_unlock;
439e3bde04fSHannes Reinecke 
440e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
441e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
4422eb6e1e3SKeith Busch 
4432eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
4442eb6e1e3SKeith Busch 
445e5863d9aSMike Snitzer 	spin_unlock_irq(&m->lock);
446e5863d9aSMike Snitzer 
447e5863d9aSMike Snitzer 	if (clone) {
448c5248f79SMike Snitzer 		/*
449c5248f79SMike Snitzer 		 * Old request-based interface: allocated clone is passed in.
450c5248f79SMike Snitzer 		 * Used by: .request_fn stacked on .request_fn path(s).
451c5248f79SMike Snitzer 		 */
4522eb6e1e3SKeith Busch 		clone->q = bdev_get_queue(bdev);
4532eb6e1e3SKeith Busch 		clone->rq_disk = bdev->bd_disk;
4542eb6e1e3SKeith Busch 		clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
455e5863d9aSMike Snitzer 	} else {
456eca7ee6dSMike Snitzer 		/*
457eca7ee6dSMike Snitzer 		 * blk-mq request-based interface; used by both:
458eca7ee6dSMike Snitzer 		 * .request_fn stacked on blk-mq path(s) and
459eca7ee6dSMike Snitzer 		 * blk-mq stacked on blk-mq path(s).
460eca7ee6dSMike Snitzer 		 */
46178ce23b5SMike Snitzer 		*__clone = blk_mq_alloc_request(bdev_get_queue(bdev),
46278ce23b5SMike Snitzer 						rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
4634c6dd53dSMike Snitzer 		if (IS_ERR(*__clone)) {
464e5863d9aSMike Snitzer 			/* ENOMEM, requeue */
4652eff1924SMike Snitzer 			clear_request_fn_mpio(m, map_context);
466e5863d9aSMike Snitzer 			return r;
4674c6dd53dSMike Snitzer 		}
468e5863d9aSMike Snitzer 		(*__clone)->bio = (*__clone)->biotail = NULL;
469e5863d9aSMike Snitzer 		(*__clone)->rq_disk = bdev->bd_disk;
470e5863d9aSMike Snitzer 		(*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
471e5863d9aSMike Snitzer 	}
4722eb6e1e3SKeith Busch 
473e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
474e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
475e8099177SHannes Reinecke 					      &pgpath->path,
476e8099177SHannes Reinecke 					      nr_bytes);
4772eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
4781da177e4SLinus Torvalds 
479e3bde04fSHannes Reinecke out_unlock:
4802eb6e1e3SKeith Busch 	spin_unlock_irq(&m->lock);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	return r;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
485e5863d9aSMike Snitzer static int multipath_map(struct dm_target *ti, struct request *clone,
486e5863d9aSMike Snitzer 			 union map_info *map_context)
487e5863d9aSMike Snitzer {
488e5863d9aSMike Snitzer 	return __multipath_map(ti, clone, map_context, NULL, NULL);
489e5863d9aSMike Snitzer }
490e5863d9aSMike Snitzer 
491e5863d9aSMike Snitzer static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
492e5863d9aSMike Snitzer 				   union map_info *map_context,
493e5863d9aSMike Snitzer 				   struct request **clone)
494e5863d9aSMike Snitzer {
495e5863d9aSMike Snitzer 	return __multipath_map(ti, NULL, map_context, rq, clone);
496e5863d9aSMike Snitzer }
497e5863d9aSMike Snitzer 
498e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone)
499e5863d9aSMike Snitzer {
50078ce23b5SMike Snitzer 	blk_mq_free_request(clone);
501e5863d9aSMike Snitzer }
502e5863d9aSMike Snitzer 
5031da177e4SLinus Torvalds /*
5041da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
5051da177e4SLinus Torvalds  */
506be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
507be7d31ccSMike Snitzer 			    bool save_old_value)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	unsigned long flags;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
5121da177e4SLinus Torvalds 
513*518257b1SMike Snitzer 	if (save_old_value) {
514*518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
515*518257b1SMike Snitzer 			set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
516485ef69eSAlasdair G Kergon 		else
517*518257b1SMike Snitzer 			clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
518*518257b1SMike Snitzer 	} else {
519*518257b1SMike Snitzer 		if (queue_if_no_path)
520*518257b1SMike Snitzer 			set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
521*518257b1SMike Snitzer 		else
522*518257b1SMike Snitzer 			clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
523*518257b1SMike Snitzer 	}
524*518257b1SMike Snitzer 	if (queue_if_no_path)
525*518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
526*518257b1SMike Snitzer 	else
527*518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
528*518257b1SMike Snitzer 
5291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
5301da177e4SLinus Torvalds 
53163d832c3SHannes Reinecke 	if (!queue_if_no_path)
53263d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
53363d832c3SHannes Reinecke 
5341da177e4SLinus Torvalds 	return 0;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds /*
5381da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
5391da177e4SLinus Torvalds  * Includes path failure and PG bypass.
5401da177e4SLinus Torvalds  */
541c4028958SDavid Howells static void trigger_event(struct work_struct *work)
5421da177e4SLinus Torvalds {
543c4028958SDavid Howells 	struct multipath *m =
544c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*-----------------------------------------------------------------
5501da177e4SLinus Torvalds  * Constructor/argument parsing:
5511da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
5521da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
5531da177e4SLinus Torvalds  * <#priority groups>
5541da177e4SLinus Torvalds  * <initial priority group>
5551da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
5561da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
5571da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
5581da177e4SLinus Torvalds  *---------------------------------------------------------------*/
559498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
5601da177e4SLinus Torvalds 			       struct dm_target *ti)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	int r;
5631da177e4SLinus Torvalds 	struct path_selector_type *pst;
5641da177e4SLinus Torvalds 	unsigned ps_argc;
5651da177e4SLinus Torvalds 
566498f0103SMike Snitzer 	static struct dm_arg _args[] = {
56772d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
5681da177e4SLinus Torvalds 	};
5691da177e4SLinus Torvalds 
570498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
5711da177e4SLinus Torvalds 	if (!pst) {
57272d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
5731da177e4SLinus Torvalds 		return -EINVAL;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
576498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
577371b2e34SMikulas Patocka 	if (r) {
578371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5791da177e4SLinus Torvalds 		return -EINVAL;
580371b2e34SMikulas Patocka 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5831da177e4SLinus Torvalds 	if (r) {
5841da177e4SLinus Torvalds 		dm_put_path_selector(pst);
58572d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5861da177e4SLinus Torvalds 		return r;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	pg->ps.type = pst;
590498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	return 0;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
595498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
5961da177e4SLinus Torvalds 			       struct dm_target *ti)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int r;
5991da177e4SLinus Torvalds 	struct pgpath *p;
600ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
601a58a935dSMike Snitzer 	struct request_queue *q = NULL;
602a58a935dSMike Snitzer 	const char *attached_handler_name;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* we need at least a path arg */
6051da177e4SLinus Torvalds 	if (as->argc < 1) {
60672d94861SAlasdair G Kergon 		ti->error = "no device given";
60701460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	p = alloc_pgpath();
6111da177e4SLinus Torvalds 	if (!p)
61201460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6131da177e4SLinus Torvalds 
614498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
6158215d6ecSNikanth Karthikesan 			  &p->path.dev);
6161da177e4SLinus Torvalds 	if (r) {
61772d94861SAlasdair G Kergon 		ti->error = "error getting device";
6181da177e4SLinus Torvalds 		goto bad;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
621*518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
622a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
623a0cf7ea9SHannes Reinecke 
624*518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
6251bab0de0SChristoph Hellwig retain:
626a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
627a58a935dSMike Snitzer 		if (attached_handler_name) {
628a58a935dSMike Snitzer 			/*
629a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
630a58a935dSMike Snitzer 			 * and clear any hw_handler_params associated with the
631a58a935dSMike Snitzer 			 * ignored handler.
632a58a935dSMike Snitzer 			 *
633a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
634a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
635a58a935dSMike Snitzer 			 */
636a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
637a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
638a58a935dSMike Snitzer 
639a58a935dSMike Snitzer 			kfree(m->hw_handler_params);
640a58a935dSMike Snitzer 			m->hw_handler_params = NULL;
641a58a935dSMike Snitzer 		}
642a58a935dSMike Snitzer 	}
643a58a935dSMike Snitzer 
644a58a935dSMike Snitzer 	if (m->hw_handler_name) {
645a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
646a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
6471bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
648a0cf7ea9SHannes Reinecke 
6491bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
6501bab0de0SChristoph Hellwig 				bdevname(p->path.dev->bdev, b));
6511bab0de0SChristoph Hellwig 			goto retain;
6521bab0de0SChristoph Hellwig 		}
653ae11b1b3SHannes Reinecke 		if (r < 0) {
654a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
655ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
656ae11b1b3SHannes Reinecke 			goto bad;
657ae11b1b3SHannes Reinecke 		}
6582bfd2e13SChandra Seetharaman 
6592bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
6602bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
6612bfd2e13SChandra Seetharaman 			if (r < 0) {
6622bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
6632bfd2e13SChandra Seetharaman 							"handler parameters";
6642bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
6652bfd2e13SChandra Seetharaman 				goto bad;
6662bfd2e13SChandra Seetharaman 			}
6672bfd2e13SChandra Seetharaman 		}
668ae11b1b3SHannes Reinecke 	}
669ae11b1b3SHannes Reinecke 
6701da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6711da177e4SLinus Torvalds 	if (r) {
6721da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6731da177e4SLinus Torvalds 		goto bad;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	return p;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds  bad:
6791da177e4SLinus Torvalds 	free_pgpath(p);
68001460f35SBenjamin Marzinski 	return ERR_PTR(r);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
68428f16c20SMicha³ Miros³aw 						   struct multipath *m)
6851da177e4SLinus Torvalds {
686498f0103SMike Snitzer 	static struct dm_arg _args[] = {
68772d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
68872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6891da177e4SLinus Torvalds 	};
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	int r;
692498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
6931da177e4SLinus Torvalds 	struct priority_group *pg;
69428f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if (as->argc < 2) {
6971da177e4SLinus Torvalds 		as->argc = 0;
69801460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
69901460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	pg = alloc_priority_group();
7031da177e4SLinus Torvalds 	if (!pg) {
70472d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
70501460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 	pg->m = m;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
7101da177e4SLinus Torvalds 	if (r)
7111da177e4SLinus Torvalds 		goto bad;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/*
7141da177e4SLinus Torvalds 	 * read the paths
7151da177e4SLinus Torvalds 	 */
716498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
7171da177e4SLinus Torvalds 	if (r)
7181da177e4SLinus Torvalds 		goto bad;
7191da177e4SLinus Torvalds 
720498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
7211da177e4SLinus Torvalds 	if (r)
7221da177e4SLinus Torvalds 		goto bad;
7231da177e4SLinus Torvalds 
724498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
7251da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
7261da177e4SLinus Torvalds 		struct pgpath *pgpath;
727498f0103SMike Snitzer 		struct dm_arg_set path_args;
7281da177e4SLinus Torvalds 
729498f0103SMike Snitzer 		if (as->argc < nr_args) {
730148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
7316bbf79a1SAlasdair G Kergon 			r = -EINVAL;
7321da177e4SLinus Torvalds 			goto bad;
733148acff6SMikulas Patocka 		}
7341da177e4SLinus Torvalds 
735498f0103SMike Snitzer 		path_args.argc = nr_args;
7361da177e4SLinus Torvalds 		path_args.argv = as->argv;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
73901460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
74001460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
7411da177e4SLinus Torvalds 			goto bad;
74201460f35SBenjamin Marzinski 		}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 		pgpath->pg = pg;
7451da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
746498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
7471da177e4SLinus Torvalds 	}
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	return pg;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds  bad:
7521da177e4SLinus Torvalds 	free_priority_group(pg, ti);
75301460f35SBenjamin Marzinski 	return ERR_PTR(r);
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
756498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	unsigned hw_argc;
7592bfd2e13SChandra Seetharaman 	int ret;
76028f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
7611da177e4SLinus Torvalds 
762498f0103SMike Snitzer 	static struct dm_arg _args[] = {
76372d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
7641da177e4SLinus Torvalds 	};
7651da177e4SLinus Torvalds 
766498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
7671da177e4SLinus Torvalds 		return -EINVAL;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (!hw_argc)
7701da177e4SLinus Torvalds 		return 0;
7711da177e4SLinus Torvalds 
772498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
77314e98c5cSChandra Seetharaman 
7742bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
7752bfd2e13SChandra Seetharaman 		char *p;
7762bfd2e13SChandra Seetharaman 		int i, j, len = 4;
7772bfd2e13SChandra Seetharaman 
7782bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
7792bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
7802bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
7812bfd2e13SChandra Seetharaman 		if (!p) {
7822bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
7832bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
7842bfd2e13SChandra Seetharaman 			goto fail;
7852bfd2e13SChandra Seetharaman 		}
7862bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
7872bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
7882bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
7892bfd2e13SChandra Seetharaman 	}
790498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	return 0;
7932bfd2e13SChandra Seetharaman fail:
7942bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
7952bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
7962bfd2e13SChandra Seetharaman 	return ret;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	int r;
8021da177e4SLinus Torvalds 	unsigned argc;
80328f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
804498f0103SMike Snitzer 	const char *arg_name;
8051da177e4SLinus Torvalds 
806498f0103SMike Snitzer 	static struct dm_arg _args[] = {
807a58a935dSMike Snitzer 		{0, 6, "invalid number of feature args"},
808c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
8094e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
8101da177e4SLinus Torvalds 	};
8111da177e4SLinus Torvalds 
812498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
8131da177e4SLinus Torvalds 	if (r)
8141da177e4SLinus Torvalds 		return -EINVAL;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	if (!argc)
8171da177e4SLinus Torvalds 		return 0;
8181da177e4SLinus Torvalds 
819c9e45581SDave Wysochanski 	do {
820498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
821c9e45581SDave Wysochanski 		argc--;
822c9e45581SDave Wysochanski 
823498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
824be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
825c9e45581SDave Wysochanski 			continue;
8261da177e4SLinus Torvalds 		}
827c9e45581SDave Wysochanski 
828a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
829*518257b1SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
830a58a935dSMike Snitzer 			continue;
831a58a935dSMike Snitzer 		}
832a58a935dSMike Snitzer 
833498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
834c9e45581SDave Wysochanski 		    (argc >= 1)) {
835498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
836c9e45581SDave Wysochanski 			argc--;
837c9e45581SDave Wysochanski 			continue;
838c9e45581SDave Wysochanski 		}
839c9e45581SDave Wysochanski 
840498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
8414e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
842498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
8434e2d19e4SChandra Seetharaman 			argc--;
8444e2d19e4SChandra Seetharaman 			continue;
8454e2d19e4SChandra Seetharaman 		}
8464e2d19e4SChandra Seetharaman 
847c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
848c9e45581SDave Wysochanski 		r = -EINVAL;
849c9e45581SDave Wysochanski 	} while (argc && !r);
850c9e45581SDave Wysochanski 
851c9e45581SDave Wysochanski 	return r;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
8551da177e4SLinus Torvalds 			 char **argv)
8561da177e4SLinus Torvalds {
857498f0103SMike Snitzer 	/* target arguments */
858498f0103SMike Snitzer 	static struct dm_arg _args[] = {
859a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
860a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
8611da177e4SLinus Torvalds 	};
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	int r;
8641da177e4SLinus Torvalds 	struct multipath *m;
865498f0103SMike Snitzer 	struct dm_arg_set as;
8661da177e4SLinus Torvalds 	unsigned pg_count = 0;
8671da177e4SLinus Torvalds 	unsigned next_pg_num;
8688637a6bfSMike Snitzer 	bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table));
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	as.argc = argc;
8711da177e4SLinus Torvalds 	as.argv = argv;
8721da177e4SLinus Torvalds 
8738637a6bfSMike Snitzer 	m = alloc_multipath(ti, use_blk_mq);
8741da177e4SLinus Torvalds 	if (!m) {
87572d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
8761da177e4SLinus Torvalds 		return -EINVAL;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
87928f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
8801da177e4SLinus Torvalds 	if (r)
8811da177e4SLinus Torvalds 		goto bad;
8821da177e4SLinus Torvalds 
88328f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
8841da177e4SLinus Torvalds 	if (r)
8851da177e4SLinus Torvalds 		goto bad;
8861da177e4SLinus Torvalds 
887498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
8881da177e4SLinus Torvalds 	if (r)
8891da177e4SLinus Torvalds 		goto bad;
8901da177e4SLinus Torvalds 
891498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
8921da177e4SLinus Torvalds 	if (r)
8931da177e4SLinus Torvalds 		goto bad;
8941da177e4SLinus Torvalds 
895a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
896a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
897a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
898a490a07aSMike Snitzer 		r = -EINVAL;
899a490a07aSMike Snitzer 		goto bad;
900a490a07aSMike Snitzer 	}
901a490a07aSMike Snitzer 
9021da177e4SLinus Torvalds 	/* parse the priority groups */
9031da177e4SLinus Torvalds 	while (as.argc) {
9041da177e4SLinus Torvalds 		struct priority_group *pg;
9051da177e4SLinus Torvalds 
90628f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
90701460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
90801460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
9091da177e4SLinus Torvalds 			goto bad;
9101da177e4SLinus Torvalds 		}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
9131da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
9141da177e4SLinus Torvalds 		pg_count++;
9151da177e4SLinus Torvalds 		pg->pg_num = pg_count;
9161da177e4SLinus Torvalds 		if (!--next_pg_num)
9171da177e4SLinus Torvalds 			m->next_pg = pg;
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
92172d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
9221da177e4SLinus Torvalds 		r = -EINVAL;
9231da177e4SLinus Torvalds 		goto bad;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
92655a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
92755a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
928042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
9298637a6bfSMike Snitzer 	if (use_blk_mq)
9308637a6bfSMike Snitzer 		ti->per_io_data_size = sizeof(struct dm_mpath_io);
9318627921fSMikulas Patocka 
9321da177e4SLinus Torvalds 	return 0;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds  bad:
9351da177e4SLinus Torvalds 	free_multipath(m);
9361da177e4SLinus Torvalds 	return r;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9392bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
9402bded7bdSKiyoshi Ueda {
9412bded7bdSKiyoshi Ueda 	DECLARE_WAITQUEUE(wait, current);
9422bded7bdSKiyoshi Ueda 	unsigned long flags;
9432bded7bdSKiyoshi Ueda 
9442bded7bdSKiyoshi Ueda 	add_wait_queue(&m->pg_init_wait, &wait);
9452bded7bdSKiyoshi Ueda 
9462bded7bdSKiyoshi Ueda 	while (1) {
9472bded7bdSKiyoshi Ueda 		set_current_state(TASK_UNINTERRUPTIBLE);
9482bded7bdSKiyoshi Ueda 
9492bded7bdSKiyoshi Ueda 		spin_lock_irqsave(&m->lock, flags);
9502bded7bdSKiyoshi Ueda 		if (!m->pg_init_in_progress) {
9512bded7bdSKiyoshi Ueda 			spin_unlock_irqrestore(&m->lock, flags);
9522bded7bdSKiyoshi Ueda 			break;
9532bded7bdSKiyoshi Ueda 		}
9542bded7bdSKiyoshi Ueda 		spin_unlock_irqrestore(&m->lock, flags);
9552bded7bdSKiyoshi Ueda 
9562bded7bdSKiyoshi Ueda 		io_schedule();
9572bded7bdSKiyoshi Ueda 	}
9582bded7bdSKiyoshi Ueda 	set_current_state(TASK_RUNNING);
9592bded7bdSKiyoshi Ueda 
9602bded7bdSKiyoshi Ueda 	remove_wait_queue(&m->pg_init_wait, &wait);
9612bded7bdSKiyoshi Ueda }
9622bded7bdSKiyoshi Ueda 
9632bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
9641da177e4SLinus Torvalds {
965*518257b1SMike Snitzer 	set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
966*518257b1SMike Snitzer 	smp_mb__after_atomic();
967954a73d5SShiva Krishna Merla 
968bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
9692bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
970a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
97143829731STejun Heo 	flush_work(&m->trigger_event);
972954a73d5SShiva Krishna Merla 
973*518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
974*518257b1SMike Snitzer 	smp_mb__after_atomic();
9756df400abSKiyoshi Ueda }
9766df400abSKiyoshi Ueda 
9776df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
9786df400abSKiyoshi Ueda {
9796df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
9806df400abSKiyoshi Ueda 
9812bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
9821da177e4SLinus Torvalds 	free_multipath(m);
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds /*
9861da177e4SLinus Torvalds  * Take a path out of use.
9871da177e4SLinus Torvalds  */
9881da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	unsigned long flags;
9911da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9941da177e4SLinus Torvalds 
9956680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9961da177e4SLinus Torvalds 		goto out;
9971da177e4SLinus Torvalds 
99872d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1001be7d31ccSMike Snitzer 	pgpath->is_active = false;
10021da177e4SLinus Torvalds 	pgpath->fail_count++;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	m->nr_valid_paths--;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
10071da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10081da177e4SLinus Torvalds 
1009b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1010b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1011b15546f9SMike Anderson 
1012fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds out:
10151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	return 0;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds /*
10211da177e4SLinus Torvalds  * Reinstate a previously-failed path
10221da177e4SLinus Torvalds  */
10231da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
10241da177e4SLinus Torvalds {
102563d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
10261da177e4SLinus Torvalds 	unsigned long flags;
10271da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10301da177e4SLinus Torvalds 
10316680073dSKiyoshi Ueda 	if (pgpath->is_active)
10321da177e4SLinus Torvalds 		goto out;
10331da177e4SLinus Torvalds 
1034ec31f3f7SMike Snitzer 	DMWARN("Reinstating path %s.", pgpath->path.dev->name);
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 
1040be7d31ccSMike Snitzer 	pgpath->is_active = true;
10411da177e4SLinus Torvalds 
1042e8099177SHannes Reinecke 	if (!m->nr_valid_paths++) {
10431da177e4SLinus Torvalds 		m->current_pgpath = NULL;
104463d832c3SHannes Reinecke 		run_queue = 1;
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);
105763d832c3SHannes Reinecke 	if (run_queue)
105863d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	return r;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds /*
10641da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10651da177e4SLinus Torvalds  */
10661da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10671da177e4SLinus Torvalds 		      action_fn action)
10681da177e4SLinus Torvalds {
106919040c0bSMike Snitzer 	int r = -EINVAL;
10701da177e4SLinus Torvalds 	struct pgpath *pgpath;
10711da177e4SLinus Torvalds 	struct priority_group *pg;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10741da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10751da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10761da177e4SLinus Torvalds 				r = action(pgpath);
10771da177e4SLinus Torvalds 		}
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	return r;
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds /*
10841da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10851da177e4SLinus Torvalds  */
10861da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1087be7d31ccSMike Snitzer 		      bool bypassed)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	unsigned long flags;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10941da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10951da177e4SLinus Torvalds 	m->current_pg = NULL;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10981da177e4SLinus Torvalds 
1099fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds /*
11031da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
11041da177e4SLinus Torvalds  */
11051da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
11061da177e4SLinus Torvalds {
11071da177e4SLinus Torvalds 	struct priority_group *pg;
11081da177e4SLinus Torvalds 	unsigned pgnum;
11091da177e4SLinus Torvalds 	unsigned long flags;
111031998ef1SMikulas Patocka 	char dummy;
11111da177e4SLinus Torvalds 
111231998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11131da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11141da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
11151da177e4SLinus Torvalds 		return -EINVAL;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11191da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
1120be7d31ccSMike Snitzer 		pg->bypassed = false;
11211da177e4SLinus Torvalds 		if (--pgnum)
11221da177e4SLinus Torvalds 			continue;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 		m->current_pgpath = NULL;
11251da177e4SLinus Torvalds 		m->current_pg = NULL;
11261da177e4SLinus Torvalds 		m->next_pg = pg;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
11291da177e4SLinus Torvalds 
1130fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11311da177e4SLinus Torvalds 	return 0;
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /*
11351da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
11361da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
11371da177e4SLinus Torvalds  */
1138be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	struct priority_group *pg;
11411da177e4SLinus Torvalds 	unsigned pgnum;
114231998ef1SMikulas Patocka 	char dummy;
11431da177e4SLinus Torvalds 
114431998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11451da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11461da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
11471da177e4SLinus Torvalds 		return -EINVAL;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11511da177e4SLinus Torvalds 		if (!--pgnum)
11521da177e4SLinus Torvalds 			break;
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
11561da177e4SLinus Torvalds 	return 0;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /*
1160c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1161c9e45581SDave Wysochanski  */
1162be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1163c9e45581SDave Wysochanski {
1164c9e45581SDave Wysochanski 	unsigned long flags;
1165be7d31ccSMike Snitzer 	bool limit_reached = false;
1166c9e45581SDave Wysochanski 
1167c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1168c9e45581SDave Wysochanski 
1169*518257b1SMike Snitzer 	if (m->pg_init_count <= m->pg_init_retries && !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1170*518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1171c9e45581SDave Wysochanski 	else
1172be7d31ccSMike Snitzer 		limit_reached = true;
1173c9e45581SDave Wysochanski 
1174c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1175c9e45581SDave Wysochanski 
1176c9e45581SDave Wysochanski 	return limit_reached;
1177c9e45581SDave Wysochanski }
1178c9e45581SDave Wysochanski 
11793ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1180cfae5c9bSChandra Seetharaman {
118183c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1182cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1183cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1184cfae5c9bSChandra Seetharaman 	unsigned long flags;
1185be7d31ccSMike Snitzer 	bool delay_retry = false;
1186cfae5c9bSChandra Seetharaman 
1187cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1188cfae5c9bSChandra Seetharaman 	switch (errors) {
1189cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1190cfae5c9bSChandra Seetharaman 		break;
1191cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1192cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1193cfae5c9bSChandra Seetharaman 			errors = 0;
1194cfae5c9bSChandra Seetharaman 			break;
1195cfae5c9bSChandra Seetharaman 		}
1196f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1197f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1198cfae5c9bSChandra Seetharaman 		/*
1199cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1200cfae5c9bSChandra Seetharaman 		 */
1201cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1202cfae5c9bSChandra Seetharaman 		break;
1203cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1204cfae5c9bSChandra Seetharaman 		/*
1205cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1206cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1207cfae5c9bSChandra Seetharaman 		 */
1208be7d31ccSMike Snitzer 		bypass_pg(m, pg, true);
1209cfae5c9bSChandra Seetharaman 		break;
1210cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
12114e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
12124e2d19e4SChandra Seetharaman 		delay_retry = 1;
1213cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1214cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1215cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1216cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1217cfae5c9bSChandra Seetharaman 		errors = 0;
1218cfae5c9bSChandra Seetharaman 		break;
1219ec31f3f7SMike Snitzer 	case SCSI_DH_DEV_OFFLINED:
1220cfae5c9bSChandra Seetharaman 	default:
1221cfae5c9bSChandra Seetharaman 		/*
1222cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1223cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1224cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1225cfae5c9bSChandra Seetharaman 		 */
1226cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1227cfae5c9bSChandra Seetharaman 	}
1228cfae5c9bSChandra Seetharaman 
1229cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1230cfae5c9bSChandra Seetharaman 	if (errors) {
1231e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1232cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1233cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1234cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1235e54f77ddSChandra Seetharaman 		}
1236*518257b1SMike Snitzer 	} else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1237be7d31ccSMike Snitzer 		pg->bypassed = false;
1238cfae5c9bSChandra Seetharaman 
1239d0259bf0SKiyoshi Ueda 	if (--m->pg_init_in_progress)
1240d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1241d0259bf0SKiyoshi Ueda 		goto out;
1242d0259bf0SKiyoshi Ueda 
1243*518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1244*518257b1SMike Snitzer 		if (delay_retry)
1245*518257b1SMike Snitzer 			set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1246*518257b1SMike Snitzer 		else
1247*518257b1SMike Snitzer 			clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1248*518257b1SMike Snitzer 
12493e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
12503e9f1be1SHannes Reinecke 			goto out;
12513e9f1be1SHannes Reinecke 	}
1252*518257b1SMike Snitzer 	clear_bit(MPATHF_QUEUE_IO, &m->flags);
1253d0259bf0SKiyoshi Ueda 
12542bded7bdSKiyoshi Ueda 	/*
12552bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
12562bded7bdSKiyoshi Ueda 	 */
12572bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
12582bded7bdSKiyoshi Ueda 
1259d0259bf0SKiyoshi Ueda out:
1260cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1261cfae5c9bSChandra Seetharaman }
1262cfae5c9bSChandra Seetharaman 
1263bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1264bab7cfc7SChandra Seetharaman {
1265e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
12664e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1267bab7cfc7SChandra Seetharaman 
12683a017509SHannes Reinecke 	if (pgpath->is_active)
12693ae31f6aSChandra Seetharaman 		scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
127083c0d5d5SMoger, Babu 				 pg_init_done, pgpath);
12713a017509SHannes Reinecke 	else
12723a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1273bab7cfc7SChandra Seetharaman }
1274bab7cfc7SChandra Seetharaman 
12757e782af5SHannes Reinecke static int noretry_error(int error)
12767e782af5SHannes Reinecke {
12777e782af5SHannes Reinecke 	switch (error) {
12787e782af5SHannes Reinecke 	case -EOPNOTSUPP:
12797e782af5SHannes Reinecke 	case -EREMOTEIO:
12807e782af5SHannes Reinecke 	case -EILSEQ:
12817e782af5SHannes Reinecke 	case -ENODATA:
1282cc9d3c38SJun'ichi Nomura 	case -ENOSPC:
12837e782af5SHannes Reinecke 		return 1;
12847e782af5SHannes Reinecke 	}
12857e782af5SHannes Reinecke 
12867e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
12877e782af5SHannes Reinecke 	return 0;
12887e782af5SHannes Reinecke }
12897e782af5SHannes Reinecke 
12901da177e4SLinus Torvalds /*
12911da177e4SLinus Torvalds  * end_io handling
12921da177e4SLinus Torvalds  */
1293f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1294028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12951da177e4SLinus Torvalds {
1296f40c67f0SKiyoshi Ueda 	/*
1297f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1298f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1299f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1300f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1301f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1302f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1303f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1304f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1305f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1306f40c67f0SKiyoshi Ueda 	 */
1307f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1308640eb3b0SStefan Bader 	unsigned long flags;
13091da177e4SLinus Torvalds 
1310f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
13111da177e4SLinus Torvalds 		return 0;	/* I/O complete */
13121da177e4SLinus Torvalds 
13137eee4ae2SMike Snitzer 	if (noretry_error(error))
1314959eb4e5SMike Snitzer 		return error;
1315959eb4e5SMike Snitzer 
1316cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
13171da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
13181da177e4SLinus Torvalds 
1319640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1320751b2a7dSHannes Reinecke 	if (!m->nr_valid_paths) {
1321*518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1322751b2a7dSHannes Reinecke 			if (!__must_push_back(m))
1323f40c67f0SKiyoshi Ueda 				r = -EIO;
1324751b2a7dSHannes Reinecke 		} else {
1325751b2a7dSHannes Reinecke 			if (error == -EBADE)
1326751b2a7dSHannes Reinecke 				r = error;
1327751b2a7dSHannes Reinecke 		}
1328751b2a7dSHannes Reinecke 	}
1329640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
13301da177e4SLinus Torvalds 
1331f40c67f0SKiyoshi Ueda 	return r;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
1334f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
13351da177e4SLinus Torvalds 			    int error, union map_info *map_context)
13361da177e4SLinus Torvalds {
1337028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
13382eff1924SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio(map_context);
1339a71a261fSWei Yongjun 	struct pgpath *pgpath;
13401da177e4SLinus Torvalds 	struct path_selector *ps;
13411da177e4SLinus Torvalds 	int r;
13421da177e4SLinus Torvalds 
1343466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1344466891f9SJun'ichi Nomura 
1345f40c67f0SKiyoshi Ueda 	r = do_end_io(m, clone, error, mpio);
1346a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
13471da177e4SLinus Torvalds 	if (pgpath) {
13481da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
13491da177e4SLinus Torvalds 		if (ps->type->end_io)
135002ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
13511da177e4SLinus Torvalds 	}
13522eff1924SMike Snitzer 	clear_request_fn_mpio(m, map_context);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	return r;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds /*
13581da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1359436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1360436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1361436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
13621da177e4SLinus Torvalds  */
13631da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
13641da177e4SLinus Torvalds {
13657943bd6dSMike Snitzer 	struct multipath *m = ti->private;
13661da177e4SLinus Torvalds 
1367be7d31ccSMike Snitzer 	queue_if_no_path(m, false, true);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13706df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
13716df400abSKiyoshi Ueda {
13726380f26fSMike Anderson 	struct multipath *m = ti->private;
13736380f26fSMike Anderson 
13746380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
13752bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
13766380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
13776df400abSKiyoshi Ueda }
13786df400abSKiyoshi Ueda 
1379436d4108SAlasdair G Kergon /*
1380436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1381436d4108SAlasdair G Kergon  */
13821da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
13831da177e4SLinus Torvalds {
13847943bd6dSMike Snitzer 	struct multipath *m = ti->private;
13851da177e4SLinus Torvalds 
1386*518257b1SMike Snitzer 	if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1387*518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1388*518257b1SMike Snitzer 	else
1389*518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1390*518257b1SMike Snitzer 	smp_mb__after_atomic();
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /*
13941da177e4SLinus Torvalds  * Info output has the following format:
13951da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
13961da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
13971da177e4SLinus Torvalds  * num_groups init_group_number
13981da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
13991da177e4SLinus Torvalds  *             num_paths num_selector_args
14001da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
14011da177e4SLinus Torvalds  *
14021da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
14031da177e4SLinus Torvalds  * num_feature_args [features_args]*
14041da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
14051da177e4SLinus Torvalds  * num_groups init_group_number
14061da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
14071da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
14081da177e4SLinus Torvalds  */
1409fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
14101f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	int sz = 0;
14131da177e4SLinus Torvalds 	unsigned long flags;
14147943bd6dSMike Snitzer 	struct multipath *m = ti->private;
14151da177e4SLinus Torvalds 	struct priority_group *pg;
14161da177e4SLinus Torvalds 	struct pgpath *p;
14171da177e4SLinus Torvalds 	unsigned pg_num;
14181da177e4SLinus Torvalds 	char state;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	/* Features */
14231da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1424*518257b1SMike Snitzer 		DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), m->pg_init_count);
1425c9e45581SDave Wysochanski 	else {
1426*518257b1SMike Snitzer 		DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
14274e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1428a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1429*518257b1SMike Snitzer 			      test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags));
1430*518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1431c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1432c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1433c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
14344e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
14354e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1436*518257b1SMike Snitzer 		if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1437a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1438c9e45581SDave Wysochanski 	}
14391da177e4SLinus Torvalds 
1440cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
14411da177e4SLinus Torvalds 		DMEMIT("0 ");
14421da177e4SLinus Torvalds 	else
1443cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	if (m->next_pg)
14481da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
14491da177e4SLinus Torvalds 	else if (m->current_pg)
14501da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
14511da177e4SLinus Torvalds 	else
1452a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	switch (type) {
14571da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
14581da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14591da177e4SLinus Torvalds 			if (pg->bypassed)
14601da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
14611da177e4SLinus Torvalds 			else if (pg == m->current_pg)
14621da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
14631da177e4SLinus Torvalds 			else
14641da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 			DMEMIT("%c ", state);
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 			if (pg->ps.type->status)
14691da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14701da177e4SLinus Torvalds 							  result + sz,
14711da177e4SLinus Torvalds 							  maxlen - sz);
14721da177e4SLinus Torvalds 			else
14731da177e4SLinus Torvalds 				DMEMIT("0 ");
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14761da177e4SLinus Torvalds 			       pg->ps.type->info_args);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14791da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
14806680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
14811da177e4SLinus Torvalds 				       p->fail_count);
14821da177e4SLinus Torvalds 				if (pg->ps.type->status)
14831da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14841da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14851da177e4SLinus Torvalds 					      maxlen - sz);
14861da177e4SLinus Torvalds 			}
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 		break;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
14911da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14921da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 			if (pg->ps.type->status)
14951da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14961da177e4SLinus Torvalds 							  result + sz,
14971da177e4SLinus Torvalds 							  maxlen - sz);
14981da177e4SLinus Torvalds 			else
14991da177e4SLinus Torvalds 				DMEMIT("0 ");
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
15021da177e4SLinus Torvalds 			       pg->ps.type->table_args);
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
15051da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
15061da177e4SLinus Torvalds 				if (pg->ps.type->status)
15071da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
15081da177e4SLinus Torvalds 					      &p->path, type, result + sz,
15091da177e4SLinus Torvalds 					      maxlen - sz);
15101da177e4SLinus Torvalds 			}
15111da177e4SLinus Torvalds 		}
15121da177e4SLinus Torvalds 		break;
15131da177e4SLinus Torvalds 	}
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
15191da177e4SLinus Torvalds {
15206380f26fSMike Anderson 	int r = -EINVAL;
15211da177e4SLinus Torvalds 	struct dm_dev *dev;
15227943bd6dSMike Snitzer 	struct multipath *m = ti->private;
15231da177e4SLinus Torvalds 	action_fn action;
15241da177e4SLinus Torvalds 
15256380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
15266380f26fSMike Anderson 
1527c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1528c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1529c2f3d24bSKiyoshi Ueda 		goto out;
1530c2f3d24bSKiyoshi Ueda 	}
1531c2f3d24bSKiyoshi Ueda 
15321da177e4SLinus Torvalds 	if (argc == 1) {
1533498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
1534be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
15356380f26fSMike Anderson 			goto out;
1536498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1537be7d31ccSMike Snitzer 			r = queue_if_no_path(m, false, false);
15386380f26fSMike Anderson 			goto out;
15396380f26fSMike Anderson 		}
15401da177e4SLinus Torvalds 	}
15411da177e4SLinus Torvalds 
15426380f26fSMike Anderson 	if (argc != 2) {
1543a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
15446380f26fSMike Anderson 		goto out;
15456380f26fSMike Anderson 	}
15461da177e4SLinus Torvalds 
1547498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
1548be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], true);
15496380f26fSMike Anderson 		goto out;
1550498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
1551be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], false);
15526380f26fSMike Anderson 		goto out;
1553498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
15546380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
15556380f26fSMike Anderson 		goto out;
1556498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
15571da177e4SLinus Torvalds 		action = reinstate_path;
1558498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
15591da177e4SLinus Torvalds 		action = fail_path;
15606380f26fSMike Anderson 	else {
1561a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
15626380f26fSMike Anderson 		goto out;
15636380f26fSMike Anderson 	}
15641da177e4SLinus Torvalds 
15658215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
15661da177e4SLinus Torvalds 	if (r) {
156772d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
15681da177e4SLinus Torvalds 		       argv[1]);
15696380f26fSMike Anderson 		goto out;
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	dm_put_device(ti, dev);
15751da177e4SLinus Torvalds 
15766380f26fSMike Anderson out:
15776380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15781da177e4SLinus Torvalds 	return r;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
1581e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
1582e56f81e0SChristoph Hellwig 		struct block_device **bdev, fmode_t *mode)
15839af4aa30SMilan Broz {
158435991652SMikulas Patocka 	struct multipath *m = ti->private;
15859af4aa30SMilan Broz 	unsigned long flags;
158635991652SMikulas Patocka 	int r;
158735991652SMikulas Patocka 
15889af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
15899af4aa30SMilan Broz 
15909af4aa30SMilan Broz 	if (!m->current_pgpath)
159102ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
15929af4aa30SMilan Broz 
159343e43c9eSJunichi Nomura 	if (m->current_pgpath) {
1594*518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
159543e43c9eSJunichi Nomura 			*bdev = m->current_pgpath->path.dev->bdev;
159643e43c9eSJunichi Nomura 			*mode = m->current_pgpath->path.dev->mode;
159743e43c9eSJunichi Nomura 			r = 0;
159843e43c9eSJunichi Nomura 		} else {
159943e43c9eSJunichi Nomura 			/* pg_init has not started or completed */
16006c182cd8SHannes Reinecke 			r = -ENOTCONN;
160143e43c9eSJunichi Nomura 		}
160243e43c9eSJunichi Nomura 	} else {
160343e43c9eSJunichi Nomura 		/* No path is available */
1604*518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
160543e43c9eSJunichi Nomura 			r = -ENOTCONN;
160643e43c9eSJunichi Nomura 		else
16079af4aa30SMilan Broz 			r = -EIO;
160843e43c9eSJunichi Nomura 	}
16099af4aa30SMilan Broz 
16109af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
16119af4aa30SMilan Broz 
16125bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN) {
16133e9f1be1SHannes Reinecke 		spin_lock_irqsave(&m->lock, flags);
16143e9f1be1SHannes Reinecke 		if (!m->current_pg) {
16153e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
16163e9f1be1SHannes Reinecke 			__choose_pgpath(m, 0);
16173e9f1be1SHannes Reinecke 		}
1618*518257b1SMike Snitzer 		if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
16193e9f1be1SHannes Reinecke 			__pg_init_all_paths(m);
16204cdd2ad7SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
162163d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
16223e9f1be1SHannes Reinecke 	}
162335991652SMikulas Patocka 
1624e56f81e0SChristoph Hellwig 	/*
1625e56f81e0SChristoph Hellwig 	 * Only pass ioctls through if the device sizes match exactly.
1626e56f81e0SChristoph Hellwig 	 */
1627e56f81e0SChristoph Hellwig 	if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1628e56f81e0SChristoph Hellwig 		return 1;
1629e56f81e0SChristoph Hellwig 	return r;
16309af4aa30SMilan Broz }
16319af4aa30SMilan Broz 
1632af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1633af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1634af4874e0SMike Snitzer {
1635af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1636af4874e0SMike Snitzer 	struct priority_group *pg;
1637af4874e0SMike Snitzer 	struct pgpath *p;
1638af4874e0SMike Snitzer 	int ret = 0;
1639af4874e0SMike Snitzer 
1640af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1641af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
16425dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1643af4874e0SMike Snitzer 			if (ret)
1644af4874e0SMike Snitzer 				goto out;
1645af4874e0SMike Snitzer 		}
1646af4874e0SMike Snitzer 	}
1647af4874e0SMike Snitzer 
1648af4874e0SMike Snitzer out:
1649af4874e0SMike Snitzer 	return ret;
1650af4874e0SMike Snitzer }
1651af4874e0SMike Snitzer 
16529f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
1653f40c67f0SKiyoshi Ueda {
1654f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1655f40c67f0SKiyoshi Ueda 
165652b09914SMike Snitzer 	return blk_lld_busy(q);
1657f40c67f0SKiyoshi Ueda }
1658f40c67f0SKiyoshi Ueda 
1659f40c67f0SKiyoshi Ueda /*
1660f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1661f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1662f40c67f0SKiyoshi Ueda  * the underlying queue).
1663f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1664f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1665f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1666f40c67f0SKiyoshi Ueda  */
1667f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1668f40c67f0SKiyoshi Ueda {
1669be7d31ccSMike Snitzer 	bool busy = false, has_active = false;
1670f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1671f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1672f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1673f40c67f0SKiyoshi Ueda 	unsigned long flags;
1674f40c67f0SKiyoshi Ueda 
1675f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1676f40c67f0SKiyoshi Ueda 
16777a7a3b45SJun'ichi Nomura 	/* pg_init in progress or no paths available */
16787a7a3b45SJun'ichi Nomura 	if (m->pg_init_in_progress ||
1679*518257b1SMike Snitzer 	    (!m->nr_valid_paths && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
1680be7d31ccSMike Snitzer 		busy = true;
1681b63349a7SHannes Reinecke 		goto out;
1682b63349a7SHannes Reinecke 	}
1683f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1684f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1685f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1686f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1687f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1688f40c67f0SKiyoshi Ueda 	else
1689f40c67f0SKiyoshi Ueda 		/*
1690f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1691f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1692f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1693f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1694f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1695f40c67f0SKiyoshi Ueda 		 */
1696f40c67f0SKiyoshi Ueda 		goto out;
1697f40c67f0SKiyoshi Ueda 
1698f40c67f0SKiyoshi Ueda 	/*
1699f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1700f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1701f40c67f0SKiyoshi Ueda 	 */
1702be7d31ccSMike Snitzer 	busy = true;
1703f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1704f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1705be7d31ccSMike Snitzer 			has_active = true;
17069f54cec5SMike Snitzer 			if (!pgpath_busy(pgpath)) {
1707be7d31ccSMike Snitzer 				busy = false;
1708f40c67f0SKiyoshi Ueda 				break;
1709f40c67f0SKiyoshi Ueda 			}
1710f40c67f0SKiyoshi Ueda 		}
1711f40c67f0SKiyoshi Ueda 
1712f40c67f0SKiyoshi Ueda 	if (!has_active)
1713f40c67f0SKiyoshi Ueda 		/*
1714f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1715f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1716f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1717f40c67f0SKiyoshi Ueda 		 */
1718be7d31ccSMike Snitzer 		busy = false;
1719f40c67f0SKiyoshi Ueda 
1720f40c67f0SKiyoshi Ueda out:
1721f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1722f40c67f0SKiyoshi Ueda 
1723f40c67f0SKiyoshi Ueda 	return busy;
1724f40c67f0SKiyoshi Ueda }
1725f40c67f0SKiyoshi Ueda 
17261da177e4SLinus Torvalds /*-----------------------------------------------------------------
17271da177e4SLinus Torvalds  * Module setup
17281da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17291da177e4SLinus Torvalds static struct target_type multipath_target = {
17301da177e4SLinus Torvalds 	.name = "multipath",
173116f12266SMike Snitzer 	.version = {1, 11, 0},
173216f12266SMike Snitzer 	.features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
17331da177e4SLinus Torvalds 	.module = THIS_MODULE,
17341da177e4SLinus Torvalds 	.ctr = multipath_ctr,
17351da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1736f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1737e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
1738e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
1739f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
17401da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
17416df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
17421da177e4SLinus Torvalds 	.resume = multipath_resume,
17431da177e4SLinus Torvalds 	.status = multipath_status,
17441da177e4SLinus Torvalds 	.message = multipath_message,
1745e56f81e0SChristoph Hellwig 	.prepare_ioctl = multipath_prepare_ioctl,
1746af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1747f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
17481da177e4SLinus Torvalds };
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds static int __init dm_multipath_init(void)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	int r;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1755028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
17561da177e4SLinus Torvalds 	if (!_mpio_cache)
17571da177e4SLinus Torvalds 		return -ENOMEM;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
17601da177e4SLinus Torvalds 	if (r < 0) {
17610cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
1762ff658e9cSJohannes Thumshirn 		r = -EINVAL;
1763ff658e9cSJohannes Thumshirn 		goto bad_register_target;
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 
17664d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1767c557308eSAlasdair G Kergon 	if (!kmultipathd) {
17680cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1769ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1770ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
1771c557308eSAlasdair G Kergon 	}
1772c557308eSAlasdair G Kergon 
1773bab7cfc7SChandra Seetharaman 	/*
1774bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1775bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1776bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1777bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1778bab7cfc7SChandra Seetharaman 	 */
17794d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
17804d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1781bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1782bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1783ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1784ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
1785bab7cfc7SChandra Seetharaman 	}
1786bab7cfc7SChandra Seetharaman 
178772d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
17881da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
17891da177e4SLinus Torvalds 	       multipath_target.version[2]);
17901da177e4SLinus Torvalds 
1791ff658e9cSJohannes Thumshirn 	return 0;
1792ff658e9cSJohannes Thumshirn 
1793ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
1794ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
1795ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
1796ff658e9cSJohannes Thumshirn 	dm_unregister_target(&multipath_target);
1797ff658e9cSJohannes Thumshirn bad_register_target:
1798ff658e9cSJohannes Thumshirn 	kmem_cache_destroy(_mpio_cache);
1799ff658e9cSJohannes Thumshirn 
18001da177e4SLinus Torvalds 	return r;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
18041da177e4SLinus Torvalds {
1805bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1806c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1807c557308eSAlasdair G Kergon 
180810d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
18091da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds module_init(dm_multipath_init);
18131da177e4SLinus Torvalds module_exit(dm_multipath_exit);
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
18161da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
18171da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1818