xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 1bab0de0274fbe8c8ac92179e6705584c55ed169)
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>
261da177e4SLinus Torvalds 
2772d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
284e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
294e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /* Path properties */
321da177e4SLinus Torvalds struct pgpath {
331da177e4SLinus Torvalds 	struct list_head list;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
366680073dSKiyoshi Ueda 	unsigned is_active;		/* Path status */
371da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
381da177e4SLinus Torvalds 
39c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
404e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
411da177e4SLinus Torvalds };
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
471da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds struct priority_group {
501da177e4SLinus Torvalds 	struct list_head list;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
531da177e4SLinus Torvalds 	struct path_selector ps;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
561da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
591da177e4SLinus Torvalds 	struct list_head pgpaths;
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* Multipath context */
631da177e4SLinus Torvalds struct multipath {
641da177e4SLinus Torvalds 	struct list_head list;
651da177e4SLinus Torvalds 	struct dm_target *ti;
661da177e4SLinus Torvalds 
67cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
682bfd2e13SChandra Seetharaman 	char *hw_handler_params;
694e2d19e4SChandra Seetharaman 
701fbdd2b3SMike Snitzer 	spinlock_t lock;
711fbdd2b3SMike Snitzer 
721da177e4SLinus Torvalds 	unsigned nr_priority_groups;
731da177e4SLinus Torvalds 	struct list_head priority_groups;
744e2d19e4SChandra Seetharaman 
754e2d19e4SChandra Seetharaman 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
764e2d19e4SChandra Seetharaman 
771da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
78c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
794e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_retry;	/* Delay pg_init retry? */
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 	unsigned repeat_count;		/* I/Os left before calling PS again */
861da177e4SLinus Torvalds 
871fbdd2b3SMike Snitzer 	unsigned queue_io:1;		/* Must we queue all I/O? */
881fbdd2b3SMike Snitzer 	unsigned queue_if_no_path:1;	/* Queue I/O if last path fails? */
891fbdd2b3SMike Snitzer 	unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
90a58a935dSMike Snitzer 	unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
91954a73d5SShiva Krishna Merla 	unsigned pg_init_disabled:1;	/* pg_init is not currently allowed */
921fbdd2b3SMike Snitzer 
93c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
94c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
954e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	struct work_struct trigger_event;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/*
100028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
1011da177e4SLinus Torvalds 	 * can resubmit bios on error.
1021da177e4SLinus Torvalds 	 */
1031da177e4SLinus Torvalds 	mempool_t *mpio_pool;
1046380f26fSMike Anderson 
1056380f26fSMike Anderson 	struct mutex work_mutex;
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Context information attached to each bio we process.
1101da177e4SLinus Torvalds  */
111028867acSAlasdair G Kergon struct dm_mpath_io {
1121da177e4SLinus Torvalds 	struct pgpath *pgpath;
11302ab823fSKiyoshi Ueda 	size_t nr_bytes;
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1171da177e4SLinus Torvalds 
118e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1191da177e4SLinus Torvalds 
120bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
121c4028958SDavid Howells static void trigger_event(struct work_struct *work);
122bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
123e8099177SHannes Reinecke static int __pgpath_busy(struct pgpath *pgpath);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*-----------------------------------------------
1271da177e4SLinus Torvalds  * Allocation routines
1281da177e4SLinus Torvalds  *-----------------------------------------------*/
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1311da177e4SLinus Torvalds {
132e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1331da177e4SLinus Torvalds 
134224cb3e9SMike Anderson 	if (pgpath) {
1356680073dSKiyoshi Ueda 		pgpath->is_active = 1;
1364e2d19e4SChandra Seetharaman 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
137224cb3e9SMike Anderson 	}
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	return pgpath;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
142028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	kfree(pgpath);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct priority_group *pg;
1501da177e4SLinus Torvalds 
151e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1521da177e4SLinus Torvalds 
153e69fae56SMicha³ Miros³aw 	if (pg)
1541da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return pg;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1641da177e4SLinus Torvalds 		list_del(&pgpath->list);
1651da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1661da177e4SLinus Torvalds 		free_pgpath(pgpath);
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1711da177e4SLinus Torvalds 				struct dm_target *ti)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (ps->type) {
1761da177e4SLinus Torvalds 		ps->type->destroy(ps);
1771da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1811da177e4SLinus Torvalds 	kfree(pg);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
18428f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	struct multipath *m;
187f4790826SMike Snitzer 	unsigned min_ios = dm_get_reserved_rq_based_ios();
1881da177e4SLinus Torvalds 
189e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1901da177e4SLinus Torvalds 	if (m) {
1911da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1921da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1931da177e4SLinus Torvalds 		m->queue_io = 1;
1944e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
195c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
1962bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
1976380f26fSMike Anderson 		mutex_init(&m->work_mutex);
198f4790826SMike Snitzer 		m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
1991da177e4SLinus Torvalds 		if (!m->mpio_pool) {
2001da177e4SLinus Torvalds 			kfree(m);
2011da177e4SLinus Torvalds 			return NULL;
2021da177e4SLinus Torvalds 		}
20328f16c20SMicha³ Miros³aw 		m->ti = ti;
20428f16c20SMicha³ Miros³aw 		ti->private = m;
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	return m;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2151da177e4SLinus Torvalds 		list_del(&pg->list);
2161da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 
219cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2202bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2211da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2221da177e4SLinus Torvalds 	kfree(m);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225466891f9SJun'ichi Nomura static int set_mapinfo(struct multipath *m, union map_info *info)
226466891f9SJun'ichi Nomura {
227466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio;
228466891f9SJun'ichi Nomura 
229466891f9SJun'ichi Nomura 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
230466891f9SJun'ichi Nomura 	if (!mpio)
231466891f9SJun'ichi Nomura 		return -ENOMEM;
232466891f9SJun'ichi Nomura 
233466891f9SJun'ichi Nomura 	memset(mpio, 0, sizeof(*mpio));
234466891f9SJun'ichi Nomura 	info->ptr = mpio;
235466891f9SJun'ichi Nomura 
236466891f9SJun'ichi Nomura 	return 0;
237466891f9SJun'ichi Nomura }
238466891f9SJun'ichi Nomura 
239466891f9SJun'ichi Nomura static void clear_mapinfo(struct multipath *m, union map_info *info)
240466891f9SJun'ichi Nomura {
241466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio = info->ptr;
242466891f9SJun'ichi Nomura 
243466891f9SJun'ichi Nomura 	info->ptr = NULL;
244466891f9SJun'ichi Nomura 	mempool_free(mpio, m->mpio_pool);
245466891f9SJun'ichi Nomura }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /*-----------------------------------------------
2481da177e4SLinus Torvalds  * Path selection
2491da177e4SLinus Torvalds  *-----------------------------------------------*/
2501da177e4SLinus Torvalds 
2513e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
252fb612642SKiyoshi Ueda {
253fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2544e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
255fb612642SKiyoshi Ueda 
25617f4ff45SHannes Reinecke 	if (m->pg_init_in_progress || m->pg_init_disabled)
2573e9f1be1SHannes Reinecke 		return 0;
25817f4ff45SHannes Reinecke 
259fb612642SKiyoshi Ueda 	m->pg_init_count++;
260fb612642SKiyoshi Ueda 	m->pg_init_required = 0;
2613e9f1be1SHannes Reinecke 
2623e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
2633e9f1be1SHannes Reinecke 	if (!m->current_pg)
2643e9f1be1SHannes Reinecke 		return 0;
2653e9f1be1SHannes Reinecke 
2664e2d19e4SChandra Seetharaman 	if (m->pg_init_delay_retry)
2674e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
2684e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
269fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
270fb612642SKiyoshi Ueda 		/* Skip failed paths */
271fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
272fb612642SKiyoshi Ueda 			continue;
2734e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
2744e2d19e4SChandra Seetharaman 				       pg_init_delay))
275fb612642SKiyoshi Ueda 			m->pg_init_in_progress++;
276fb612642SKiyoshi Ueda 	}
2773e9f1be1SHannes Reinecke 	return m->pg_init_in_progress;
278fb612642SKiyoshi Ueda }
279fb612642SKiyoshi Ueda 
2801da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
285cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
2861da177e4SLinus Torvalds 		m->pg_init_required = 1;
2871da177e4SLinus Torvalds 		m->queue_io = 1;
2881da177e4SLinus Torvalds 	} else {
2891da177e4SLinus Torvalds 		m->pg_init_required = 0;
2901da177e4SLinus Torvalds 		m->queue_io = 0;
2911da177e4SLinus Torvalds 	}
292c9e45581SDave Wysochanski 
293c9e45581SDave Wysochanski 	m->pg_init_count = 0;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
29602ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
29702ab823fSKiyoshi Ueda 			       size_t nr_bytes)
2981da177e4SLinus Torvalds {
299c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3001da177e4SLinus Torvalds 
30102ab823fSKiyoshi Ueda 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
3021da177e4SLinus Torvalds 	if (!path)
3031da177e4SLinus Torvalds 		return -ENXIO;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (m->current_pg != pg)
3081da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	return 0;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
31302ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct priority_group *pg;
3161da177e4SLinus Torvalds 	unsigned bypassed = 1;
3171da177e4SLinus Torvalds 
3181f271972SBenjamin Marzinski 	if (!m->nr_valid_paths) {
3191f271972SBenjamin Marzinski 		m->queue_io = 0;
3201da177e4SLinus Torvalds 		goto failed;
3211f271972SBenjamin Marzinski 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3241da177e4SLinus Torvalds 	if (m->next_pg) {
3251da177e4SLinus Torvalds 		pg = m->next_pg;
3261da177e4SLinus Torvalds 		m->next_pg = NULL;
32702ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
3281da177e4SLinus Torvalds 			return;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
33202ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
3331da177e4SLinus Torvalds 		return;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/*
3361da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
3371da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
338f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
339f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
3401da177e4SLinus Torvalds 	 */
3411da177e4SLinus Torvalds 	do {
3421da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
3431da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
3441da177e4SLinus Torvalds 				continue;
345f220fd4eSMike Christie 			if (!__choose_path_in_pg(m, pg, nr_bytes)) {
346f220fd4eSMike Christie 				if (!bypassed)
347f220fd4eSMike Christie 					m->pg_init_delay_retry = 1;
3481da177e4SLinus Torvalds 				return;
3491da177e4SLinus Torvalds 			}
350f220fd4eSMike Christie 		}
3511da177e4SLinus Torvalds 	} while (bypassed--);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds failed:
3541da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3551da177e4SLinus Torvalds 	m->current_pg = NULL;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
35845e15720SKiyoshi Ueda /*
35945e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
36045e15720SKiyoshi Ueda  * than here in the target.
36145e15720SKiyoshi Ueda  *
36245e15720SKiyoshi Ueda  * m->lock must be held on entry.
36345e15720SKiyoshi Ueda  *
36445e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
36545e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
36645e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
36745e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
36845e15720SKiyoshi Ueda  */
36945e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
37045e15720SKiyoshi Ueda {
371e8099177SHannes Reinecke 	return (m->queue_if_no_path ||
372e8099177SHannes Reinecke 		(m->queue_if_no_path != m->saved_queue_if_no_path &&
373e8099177SHannes Reinecke 		 dm_noflush_suspending(m->ti)));
37445e15720SKiyoshi Ueda }
37545e15720SKiyoshi Ueda 
37636fcffccSHannes Reinecke /*
37736fcffccSHannes Reinecke  * Map cloned requests
37836fcffccSHannes Reinecke  */
379e5863d9aSMike Snitzer static int __multipath_map(struct dm_target *ti, struct request *clone,
380e5863d9aSMike Snitzer 			   union map_info *map_context,
381e5863d9aSMike Snitzer 			   struct request *rq, struct request **__clone)
3821da177e4SLinus Torvalds {
38336fcffccSHannes Reinecke 	struct multipath *m = (struct multipath *) ti->private;
384e3bde04fSHannes Reinecke 	int r = DM_MAPIO_REQUEUE;
385e5863d9aSMike Snitzer 	size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
3861da177e4SLinus Torvalds 	struct pgpath *pgpath;
387f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
388e3bde04fSHannes Reinecke 	struct dm_mpath_io *mpio;
3891da177e4SLinus Torvalds 
3902eb6e1e3SKeith Busch 	spin_lock_irq(&m->lock);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
3931da177e4SLinus Torvalds 	if (!m->current_pgpath ||
3941da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
39502ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3981da177e4SLinus Torvalds 
3999bf59a61SMike Snitzer 	if (!pgpath) {
4009bf59a61SMike Snitzer 		if (!__must_push_back(m))
4019bf59a61SMike Snitzer 			r = -EIO;	/* Failed */
4029bf59a61SMike Snitzer 		goto out_unlock;
4036afbc01dSMike Snitzer 	} else if (m->queue_io || m->pg_init_required) {
4049bf59a61SMike Snitzer 		__pg_init_all_paths(m);
4059bf59a61SMike Snitzer 		goto out_unlock;
4069bf59a61SMike Snitzer 	}
4076afbc01dSMike Snitzer 
408e3bde04fSHannes Reinecke 	if (set_mapinfo(m, map_context) < 0)
409e3bde04fSHannes Reinecke 		/* ENOMEM, requeue */
410e3bde04fSHannes Reinecke 		goto out_unlock;
411e3bde04fSHannes Reinecke 
412e3bde04fSHannes Reinecke 	mpio = map_context->ptr;
413e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
414e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
4152eb6e1e3SKeith Busch 
4162eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
4172eb6e1e3SKeith Busch 
418e5863d9aSMike Snitzer 	spin_unlock_irq(&m->lock);
419e5863d9aSMike Snitzer 
420e5863d9aSMike Snitzer 	if (clone) {
421e5863d9aSMike Snitzer 		/* Old request-based interface: allocated clone is passed in */
4222eb6e1e3SKeith Busch 		clone->q = bdev_get_queue(bdev);
4232eb6e1e3SKeith Busch 		clone->rq_disk = bdev->bd_disk;
4242eb6e1e3SKeith Busch 		clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
425e5863d9aSMike Snitzer 	} else {
426e5863d9aSMike Snitzer 		/* blk-mq request-based interface */
427e5863d9aSMike Snitzer 		*__clone = blk_get_request(bdev_get_queue(bdev),
42802233342SMike Snitzer 					   rq_data_dir(rq), GFP_ATOMIC);
4294c6dd53dSMike Snitzer 		if (IS_ERR(*__clone)) {
430e5863d9aSMike Snitzer 			/* ENOMEM, requeue */
4314c6dd53dSMike Snitzer 			clear_mapinfo(m, map_context);
432e5863d9aSMike Snitzer 			return r;
4334c6dd53dSMike Snitzer 		}
434e5863d9aSMike Snitzer 		(*__clone)->bio = (*__clone)->biotail = NULL;
435e5863d9aSMike Snitzer 		(*__clone)->rq_disk = bdev->bd_disk;
436e5863d9aSMike Snitzer 		(*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
437e5863d9aSMike Snitzer 	}
4382eb6e1e3SKeith Busch 
439e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
440e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
441e8099177SHannes Reinecke 					      &pgpath->path,
442e8099177SHannes Reinecke 					      nr_bytes);
4432eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
4441da177e4SLinus Torvalds 
445e3bde04fSHannes Reinecke out_unlock:
4462eb6e1e3SKeith Busch 	spin_unlock_irq(&m->lock);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	return r;
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds 
451e5863d9aSMike Snitzer static int multipath_map(struct dm_target *ti, struct request *clone,
452e5863d9aSMike Snitzer 			 union map_info *map_context)
453e5863d9aSMike Snitzer {
454e5863d9aSMike Snitzer 	return __multipath_map(ti, clone, map_context, NULL, NULL);
455e5863d9aSMike Snitzer }
456e5863d9aSMike Snitzer 
457e5863d9aSMike Snitzer static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
458e5863d9aSMike Snitzer 				   union map_info *map_context,
459e5863d9aSMike Snitzer 				   struct request **clone)
460e5863d9aSMike Snitzer {
461e5863d9aSMike Snitzer 	return __multipath_map(ti, NULL, map_context, rq, clone);
462e5863d9aSMike Snitzer }
463e5863d9aSMike Snitzer 
464e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone)
465e5863d9aSMike Snitzer {
466e5863d9aSMike Snitzer 	blk_put_request(clone);
467e5863d9aSMike Snitzer }
468e5863d9aSMike Snitzer 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
4711da177e4SLinus Torvalds  */
472485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
473485ef69eSAlasdair G Kergon 			    unsigned save_old_value)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	unsigned long flags;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4781da177e4SLinus Torvalds 
479485ef69eSAlasdair G Kergon 	if (save_old_value)
480436d4108SAlasdair G Kergon 		m->saved_queue_if_no_path = m->queue_if_no_path;
481485ef69eSAlasdair G Kergon 	else
482485ef69eSAlasdair G Kergon 		m->saved_queue_if_no_path = queue_if_no_path;
4831da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
4841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4851da177e4SLinus Torvalds 
48663d832c3SHannes Reinecke 	if (!queue_if_no_path)
48763d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
48863d832c3SHannes Reinecke 
4891da177e4SLinus Torvalds 	return 0;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /*
4931da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4941da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4951da177e4SLinus Torvalds  */
496c4028958SDavid Howells static void trigger_event(struct work_struct *work)
4971da177e4SLinus Torvalds {
498c4028958SDavid Howells 	struct multipath *m =
499c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /*-----------------------------------------------------------------
5051da177e4SLinus Torvalds  * Constructor/argument parsing:
5061da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
5071da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
5081da177e4SLinus Torvalds  * <#priority groups>
5091da177e4SLinus Torvalds  * <initial priority group>
5101da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
5111da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
5121da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
5131da177e4SLinus Torvalds  *---------------------------------------------------------------*/
514498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
5151da177e4SLinus Torvalds 			       struct dm_target *ti)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	int r;
5181da177e4SLinus Torvalds 	struct path_selector_type *pst;
5191da177e4SLinus Torvalds 	unsigned ps_argc;
5201da177e4SLinus Torvalds 
521498f0103SMike Snitzer 	static struct dm_arg _args[] = {
52272d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
5231da177e4SLinus Torvalds 	};
5241da177e4SLinus Torvalds 
525498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
5261da177e4SLinus Torvalds 	if (!pst) {
52772d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
5281da177e4SLinus Torvalds 		return -EINVAL;
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
531498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
532371b2e34SMikulas Patocka 	if (r) {
533371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5341da177e4SLinus Torvalds 		return -EINVAL;
535371b2e34SMikulas Patocka 	}
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5381da177e4SLinus Torvalds 	if (r) {
5391da177e4SLinus Torvalds 		dm_put_path_selector(pst);
54072d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5411da177e4SLinus Torvalds 		return r;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	pg->ps.type = pst;
545498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	return 0;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
550498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
5511da177e4SLinus Torvalds 			       struct dm_target *ti)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	int r;
5541da177e4SLinus Torvalds 	struct pgpath *p;
555ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
556a58a935dSMike Snitzer 	struct request_queue *q = NULL;
557a58a935dSMike Snitzer 	const char *attached_handler_name;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* we need at least a path arg */
5601da177e4SLinus Torvalds 	if (as->argc < 1) {
56172d94861SAlasdair G Kergon 		ti->error = "no device given";
56201460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	p = alloc_pgpath();
5661da177e4SLinus Torvalds 	if (!p)
56701460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
5681da177e4SLinus Torvalds 
569498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
5708215d6ecSNikanth Karthikesan 			  &p->path.dev);
5711da177e4SLinus Torvalds 	if (r) {
57272d94861SAlasdair G Kergon 		ti->error = "error getting device";
5731da177e4SLinus Torvalds 		goto bad;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
576a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler || m->hw_handler_name)
577a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
578a0cf7ea9SHannes Reinecke 
579a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler) {
580*1bab0de0SChristoph Hellwig retain:
581a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
582a58a935dSMike Snitzer 		if (attached_handler_name) {
583a58a935dSMike Snitzer 			/*
584a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
585a58a935dSMike Snitzer 			 * and clear any hw_handler_params associated with the
586a58a935dSMike Snitzer 			 * ignored handler.
587a58a935dSMike Snitzer 			 *
588a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
589a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
590a58a935dSMike Snitzer 			 */
591a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
592a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
593a58a935dSMike Snitzer 
594a58a935dSMike Snitzer 			kfree(m->hw_handler_params);
595a58a935dSMike Snitzer 			m->hw_handler_params = NULL;
596a58a935dSMike Snitzer 		}
597a58a935dSMike Snitzer 	}
598a58a935dSMike Snitzer 
599a58a935dSMike Snitzer 	if (m->hw_handler_name) {
600a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
601a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
602*1bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
603a0cf7ea9SHannes Reinecke 
604*1bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
605*1bab0de0SChristoph Hellwig 				bdevname(p->path.dev->bdev, b));
606*1bab0de0SChristoph Hellwig 			goto retain;
607*1bab0de0SChristoph Hellwig 		}
608ae11b1b3SHannes Reinecke 		if (r < 0) {
609a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
610ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
611ae11b1b3SHannes Reinecke 			goto bad;
612ae11b1b3SHannes Reinecke 		}
6132bfd2e13SChandra Seetharaman 
6142bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
6152bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
6162bfd2e13SChandra Seetharaman 			if (r < 0) {
6172bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
6182bfd2e13SChandra Seetharaman 							"handler parameters";
6192bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
6202bfd2e13SChandra Seetharaman 				goto bad;
6212bfd2e13SChandra Seetharaman 			}
6222bfd2e13SChandra Seetharaman 		}
623ae11b1b3SHannes Reinecke 	}
624ae11b1b3SHannes Reinecke 
6251da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6261da177e4SLinus Torvalds 	if (r) {
6271da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6281da177e4SLinus Torvalds 		goto bad;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	return p;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds  bad:
6341da177e4SLinus Torvalds 	free_pgpath(p);
63501460f35SBenjamin Marzinski 	return ERR_PTR(r);
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
638498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
63928f16c20SMicha³ Miros³aw 						   struct multipath *m)
6401da177e4SLinus Torvalds {
641498f0103SMike Snitzer 	static struct dm_arg _args[] = {
64272d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
64372d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6441da177e4SLinus Torvalds 	};
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	int r;
647498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
6481da177e4SLinus Torvalds 	struct priority_group *pg;
64928f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (as->argc < 2) {
6521da177e4SLinus Torvalds 		as->argc = 0;
65301460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
65401460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	pg = alloc_priority_group();
6581da177e4SLinus Torvalds 	if (!pg) {
65972d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
66001460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 	pg->m = m;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
6651da177e4SLinus Torvalds 	if (r)
6661da177e4SLinus Torvalds 		goto bad;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	/*
6691da177e4SLinus Torvalds 	 * read the paths
6701da177e4SLinus Torvalds 	 */
671498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
6721da177e4SLinus Torvalds 	if (r)
6731da177e4SLinus Torvalds 		goto bad;
6741da177e4SLinus Torvalds 
675498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
6761da177e4SLinus Torvalds 	if (r)
6771da177e4SLinus Torvalds 		goto bad;
6781da177e4SLinus Torvalds 
679498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
6801da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
6811da177e4SLinus Torvalds 		struct pgpath *pgpath;
682498f0103SMike Snitzer 		struct dm_arg_set path_args;
6831da177e4SLinus Torvalds 
684498f0103SMike Snitzer 		if (as->argc < nr_args) {
685148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
6866bbf79a1SAlasdair G Kergon 			r = -EINVAL;
6871da177e4SLinus Torvalds 			goto bad;
688148acff6SMikulas Patocka 		}
6891da177e4SLinus Torvalds 
690498f0103SMike Snitzer 		path_args.argc = nr_args;
6911da177e4SLinus Torvalds 		path_args.argv = as->argv;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
69401460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
69501460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
6961da177e4SLinus Torvalds 			goto bad;
69701460f35SBenjamin Marzinski 		}
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 		pgpath->pg = pg;
7001da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
701498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	return pg;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds  bad:
7071da177e4SLinus Torvalds 	free_priority_group(pg, ti);
70801460f35SBenjamin Marzinski 	return ERR_PTR(r);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
711498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	unsigned hw_argc;
7142bfd2e13SChandra Seetharaman 	int ret;
71528f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
7161da177e4SLinus Torvalds 
717498f0103SMike Snitzer 	static struct dm_arg _args[] = {
71872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
7191da177e4SLinus Torvalds 	};
7201da177e4SLinus Torvalds 
721498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
7221da177e4SLinus Torvalds 		return -EINVAL;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (!hw_argc)
7251da177e4SLinus Torvalds 		return 0;
7261da177e4SLinus Torvalds 
727498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
728510193a2SMike Snitzer 	if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
729510193a2SMike Snitzer 				     "scsi_dh_%s", m->hw_handler_name)) {
73072d94861SAlasdair G Kergon 		ti->error = "unknown hardware handler type";
7312bfd2e13SChandra Seetharaman 		ret = -EINVAL;
7322bfd2e13SChandra Seetharaman 		goto fail;
7331da177e4SLinus Torvalds 	}
73414e98c5cSChandra Seetharaman 
7352bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
7362bfd2e13SChandra Seetharaman 		char *p;
7372bfd2e13SChandra Seetharaman 		int i, j, len = 4;
7382bfd2e13SChandra Seetharaman 
7392bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
7402bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
7412bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
7422bfd2e13SChandra Seetharaman 		if (!p) {
7432bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
7442bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
7452bfd2e13SChandra Seetharaman 			goto fail;
7462bfd2e13SChandra Seetharaman 		}
7472bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
7482bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
7492bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
7502bfd2e13SChandra Seetharaman 	}
751498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	return 0;
7542bfd2e13SChandra Seetharaman fail:
7552bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
7562bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
7572bfd2e13SChandra Seetharaman 	return ret;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
760498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	int r;
7631da177e4SLinus Torvalds 	unsigned argc;
76428f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
765498f0103SMike Snitzer 	const char *arg_name;
7661da177e4SLinus Torvalds 
767498f0103SMike Snitzer 	static struct dm_arg _args[] = {
768a58a935dSMike Snitzer 		{0, 6, "invalid number of feature args"},
769c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
7704e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
7711da177e4SLinus Torvalds 	};
7721da177e4SLinus Torvalds 
773498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
7741da177e4SLinus Torvalds 	if (r)
7751da177e4SLinus Torvalds 		return -EINVAL;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (!argc)
7781da177e4SLinus Torvalds 		return 0;
7791da177e4SLinus Torvalds 
780c9e45581SDave Wysochanski 	do {
781498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
782c9e45581SDave Wysochanski 		argc--;
783c9e45581SDave Wysochanski 
784498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
785c9e45581SDave Wysochanski 			r = queue_if_no_path(m, 1, 0);
786c9e45581SDave Wysochanski 			continue;
7871da177e4SLinus Torvalds 		}
788c9e45581SDave Wysochanski 
789a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
790a58a935dSMike Snitzer 			m->retain_attached_hw_handler = 1;
791a58a935dSMike Snitzer 			continue;
792a58a935dSMike Snitzer 		}
793a58a935dSMike Snitzer 
794498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
795c9e45581SDave Wysochanski 		    (argc >= 1)) {
796498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
797c9e45581SDave Wysochanski 			argc--;
798c9e45581SDave Wysochanski 			continue;
799c9e45581SDave Wysochanski 		}
800c9e45581SDave Wysochanski 
801498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
8024e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
803498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
8044e2d19e4SChandra Seetharaman 			argc--;
8054e2d19e4SChandra Seetharaman 			continue;
8064e2d19e4SChandra Seetharaman 		}
8074e2d19e4SChandra Seetharaman 
808c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
809c9e45581SDave Wysochanski 		r = -EINVAL;
810c9e45581SDave Wysochanski 	} while (argc && !r);
811c9e45581SDave Wysochanski 
812c9e45581SDave Wysochanski 	return r;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
8161da177e4SLinus Torvalds 			 char **argv)
8171da177e4SLinus Torvalds {
818498f0103SMike Snitzer 	/* target arguments */
819498f0103SMike Snitzer 	static struct dm_arg _args[] = {
820a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
821a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
8221da177e4SLinus Torvalds 	};
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	int r;
8251da177e4SLinus Torvalds 	struct multipath *m;
826498f0103SMike Snitzer 	struct dm_arg_set as;
8271da177e4SLinus Torvalds 	unsigned pg_count = 0;
8281da177e4SLinus Torvalds 	unsigned next_pg_num;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	as.argc = argc;
8311da177e4SLinus Torvalds 	as.argv = argv;
8321da177e4SLinus Torvalds 
83328f16c20SMicha³ Miros³aw 	m = alloc_multipath(ti);
8341da177e4SLinus Torvalds 	if (!m) {
83572d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
8361da177e4SLinus Torvalds 		return -EINVAL;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
83928f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
8401da177e4SLinus Torvalds 	if (r)
8411da177e4SLinus Torvalds 		goto bad;
8421da177e4SLinus Torvalds 
84328f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
8441da177e4SLinus Torvalds 	if (r)
8451da177e4SLinus Torvalds 		goto bad;
8461da177e4SLinus Torvalds 
847498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
8481da177e4SLinus Torvalds 	if (r)
8491da177e4SLinus Torvalds 		goto bad;
8501da177e4SLinus Torvalds 
851498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
8521da177e4SLinus Torvalds 	if (r)
8531da177e4SLinus Torvalds 		goto bad;
8541da177e4SLinus Torvalds 
855a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
856a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
857a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
858a490a07aSMike Snitzer 		r = -EINVAL;
859a490a07aSMike Snitzer 		goto bad;
860a490a07aSMike Snitzer 	}
861a490a07aSMike Snitzer 
8621da177e4SLinus Torvalds 	/* parse the priority groups */
8631da177e4SLinus Torvalds 	while (as.argc) {
8641da177e4SLinus Torvalds 		struct priority_group *pg;
8651da177e4SLinus Torvalds 
86628f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
86701460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
86801460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
8691da177e4SLinus Torvalds 			goto bad;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
8731da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
8741da177e4SLinus Torvalds 		pg_count++;
8751da177e4SLinus Torvalds 		pg->pg_num = pg_count;
8761da177e4SLinus Torvalds 		if (!--next_pg_num)
8771da177e4SLinus Torvalds 			m->next_pg = pg;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
88172d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
8821da177e4SLinus Torvalds 		r = -EINVAL;
8831da177e4SLinus Torvalds 		goto bad;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
88655a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
88755a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
888042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
8898627921fSMikulas Patocka 
8901da177e4SLinus Torvalds 	return 0;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds  bad:
8931da177e4SLinus Torvalds 	free_multipath(m);
8941da177e4SLinus Torvalds 	return r;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8972bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
8982bded7bdSKiyoshi Ueda {
8992bded7bdSKiyoshi Ueda 	DECLARE_WAITQUEUE(wait, current);
9002bded7bdSKiyoshi Ueda 	unsigned long flags;
9012bded7bdSKiyoshi Ueda 
9022bded7bdSKiyoshi Ueda 	add_wait_queue(&m->pg_init_wait, &wait);
9032bded7bdSKiyoshi Ueda 
9042bded7bdSKiyoshi Ueda 	while (1) {
9052bded7bdSKiyoshi Ueda 		set_current_state(TASK_UNINTERRUPTIBLE);
9062bded7bdSKiyoshi Ueda 
9072bded7bdSKiyoshi Ueda 		spin_lock_irqsave(&m->lock, flags);
9082bded7bdSKiyoshi Ueda 		if (!m->pg_init_in_progress) {
9092bded7bdSKiyoshi Ueda 			spin_unlock_irqrestore(&m->lock, flags);
9102bded7bdSKiyoshi Ueda 			break;
9112bded7bdSKiyoshi Ueda 		}
9122bded7bdSKiyoshi Ueda 		spin_unlock_irqrestore(&m->lock, flags);
9132bded7bdSKiyoshi Ueda 
9142bded7bdSKiyoshi Ueda 		io_schedule();
9152bded7bdSKiyoshi Ueda 	}
9162bded7bdSKiyoshi Ueda 	set_current_state(TASK_RUNNING);
9172bded7bdSKiyoshi Ueda 
9182bded7bdSKiyoshi Ueda 	remove_wait_queue(&m->pg_init_wait, &wait);
9192bded7bdSKiyoshi Ueda }
9202bded7bdSKiyoshi Ueda 
9212bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
9221da177e4SLinus Torvalds {
923954a73d5SShiva Krishna Merla 	unsigned long flags;
924954a73d5SShiva Krishna Merla 
925954a73d5SShiva Krishna Merla 	spin_lock_irqsave(&m->lock, flags);
926954a73d5SShiva Krishna Merla 	m->pg_init_disabled = 1;
927954a73d5SShiva Krishna Merla 	spin_unlock_irqrestore(&m->lock, flags);
928954a73d5SShiva Krishna Merla 
929bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
9302bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
931a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
93243829731STejun Heo 	flush_work(&m->trigger_event);
933954a73d5SShiva Krishna Merla 
934954a73d5SShiva Krishna Merla 	spin_lock_irqsave(&m->lock, flags);
935954a73d5SShiva Krishna Merla 	m->pg_init_disabled = 0;
936954a73d5SShiva Krishna Merla 	spin_unlock_irqrestore(&m->lock, flags);
9376df400abSKiyoshi Ueda }
9386df400abSKiyoshi Ueda 
9396df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
9406df400abSKiyoshi Ueda {
9416df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
9426df400abSKiyoshi Ueda 
9432bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
9441da177e4SLinus Torvalds 	free_multipath(m);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds /*
9481da177e4SLinus Torvalds  * Take a path out of use.
9491da177e4SLinus Torvalds  */
9501da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	unsigned long flags;
9531da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9561da177e4SLinus Torvalds 
9576680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9581da177e4SLinus Torvalds 		goto out;
9591da177e4SLinus Torvalds 
96072d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9636680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9641da177e4SLinus Torvalds 	pgpath->fail_count++;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	m->nr_valid_paths--;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
9691da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9701da177e4SLinus Torvalds 
971b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
972b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
973b15546f9SMike Anderson 
974fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds out:
9771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	return 0;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds /*
9831da177e4SLinus Torvalds  * Reinstate a previously-failed path
9841da177e4SLinus Torvalds  */
9851da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
9861da177e4SLinus Torvalds {
98763d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
9881da177e4SLinus Torvalds 	unsigned long flags;
9891da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9921da177e4SLinus Torvalds 
9936680073dSKiyoshi Ueda 	if (pgpath->is_active)
9941da177e4SLinus Torvalds 		goto out;
9951da177e4SLinus Torvalds 
996def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
9971da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
9981da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
9991da177e4SLinus Torvalds 		r = -EINVAL;
10001da177e4SLinus Torvalds 		goto out;
10011da177e4SLinus Torvalds 	}
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
10041da177e4SLinus Torvalds 	if (r)
10051da177e4SLinus Torvalds 		goto out;
10061da177e4SLinus Torvalds 
10076680073dSKiyoshi Ueda 	pgpath->is_active = 1;
10081da177e4SLinus Torvalds 
1009e8099177SHannes Reinecke 	if (!m->nr_valid_paths++) {
10101da177e4SLinus Torvalds 		m->current_pgpath = NULL;
101163d832c3SHannes Reinecke 		run_queue = 1;
1012e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
10134e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
1014e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
1015e54f77ddSChandra Seetharaman 	}
10161da177e4SLinus Torvalds 
1017b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1018b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
1019b15546f9SMike Anderson 
1020fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds out:
10231da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
102463d832c3SHannes Reinecke 	if (run_queue)
102563d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	return r;
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds /*
10311da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10341da177e4SLinus Torvalds 		      action_fn action)
10351da177e4SLinus Torvalds {
103619040c0bSMike Snitzer 	int r = -EINVAL;
10371da177e4SLinus Torvalds 	struct pgpath *pgpath;
10381da177e4SLinus Torvalds 	struct priority_group *pg;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10411da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10421da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10431da177e4SLinus Torvalds 				r = action(pgpath);
10441da177e4SLinus Torvalds 		}
10451da177e4SLinus Torvalds 	}
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	return r;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds /*
10511da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10541da177e4SLinus Torvalds 		      int bypassed)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	unsigned long flags;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10611da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10621da177e4SLinus Torvalds 	m->current_pg = NULL;
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10651da177e4SLinus Torvalds 
1066fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds /*
10701da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
10711da177e4SLinus Torvalds  */
10721da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	struct priority_group *pg;
10751da177e4SLinus Torvalds 	unsigned pgnum;
10761da177e4SLinus Torvalds 	unsigned long flags;
107731998ef1SMikulas Patocka 	char dummy;
10781da177e4SLinus Torvalds 
107931998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
10801da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10811da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
10821da177e4SLinus Torvalds 		return -EINVAL;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10861da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10871da177e4SLinus Torvalds 		pg->bypassed = 0;
10881da177e4SLinus Torvalds 		if (--pgnum)
10891da177e4SLinus Torvalds 			continue;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10921da177e4SLinus Torvalds 		m->current_pg = NULL;
10931da177e4SLinus Torvalds 		m->next_pg = pg;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10961da177e4SLinus Torvalds 
1097fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10981da177e4SLinus Torvalds 	return 0;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds /*
11021da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
11031da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
11041da177e4SLinus Torvalds  */
11051da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
11061da177e4SLinus Torvalds {
11071da177e4SLinus Torvalds 	struct priority_group *pg;
11081da177e4SLinus Torvalds 	unsigned pgnum;
110931998ef1SMikulas Patocka 	char dummy;
11101da177e4SLinus Torvalds 
111131998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
11121da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
11131da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
11141da177e4SLinus Torvalds 		return -EINVAL;
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
11181da177e4SLinus Torvalds 		if (!--pgnum)
11191da177e4SLinus Torvalds 			break;
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
11231da177e4SLinus Torvalds 	return 0;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /*
1127c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1128c9e45581SDave Wysochanski  */
1129c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1130c9e45581SDave Wysochanski {
1131c9e45581SDave Wysochanski 	unsigned long flags;
1132c9e45581SDave Wysochanski 	int limit_reached = 0;
1133c9e45581SDave Wysochanski 
1134c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1135c9e45581SDave Wysochanski 
1136954a73d5SShiva Krishna Merla 	if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
1137c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1138c9e45581SDave Wysochanski 	else
1139c9e45581SDave Wysochanski 		limit_reached = 1;
1140c9e45581SDave Wysochanski 
1141c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1142c9e45581SDave Wysochanski 
1143c9e45581SDave Wysochanski 	return limit_reached;
1144c9e45581SDave Wysochanski }
1145c9e45581SDave Wysochanski 
11463ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1147cfae5c9bSChandra Seetharaman {
114883c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1149cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1150cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1151cfae5c9bSChandra Seetharaman 	unsigned long flags;
11524e2d19e4SChandra Seetharaman 	unsigned delay_retry = 0;
1153cfae5c9bSChandra Seetharaman 
1154cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1155cfae5c9bSChandra Seetharaman 	switch (errors) {
1156cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1157cfae5c9bSChandra Seetharaman 		break;
1158cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1159cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1160cfae5c9bSChandra Seetharaman 			errors = 0;
1161cfae5c9bSChandra Seetharaman 			break;
1162cfae5c9bSChandra Seetharaman 		}
1163f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1164f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1165cfae5c9bSChandra Seetharaman 		/*
1166cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1167cfae5c9bSChandra Seetharaman 		 */
1168cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1169cfae5c9bSChandra Seetharaman 		break;
1170cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1171cfae5c9bSChandra Seetharaman 		/*
1172cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1173cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1174cfae5c9bSChandra Seetharaman 		 */
1175cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1176cfae5c9bSChandra Seetharaman 		break;
1177cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
11784e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
11794e2d19e4SChandra Seetharaman 		delay_retry = 1;
1180cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1181cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1182cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1183cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1184cfae5c9bSChandra Seetharaman 		errors = 0;
1185cfae5c9bSChandra Seetharaman 		break;
1186cfae5c9bSChandra Seetharaman 	default:
1187cfae5c9bSChandra Seetharaman 		/*
1188cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1189cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1190cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1191cfae5c9bSChandra Seetharaman 		 */
1192cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1193cfae5c9bSChandra Seetharaman 	}
1194cfae5c9bSChandra Seetharaman 
1195cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1196cfae5c9bSChandra Seetharaman 	if (errors) {
1197e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1198cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1199cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1200cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1201e54f77ddSChandra Seetharaman 		}
1202d0259bf0SKiyoshi Ueda 	} else if (!m->pg_init_required)
1203cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1204cfae5c9bSChandra Seetharaman 
1205d0259bf0SKiyoshi Ueda 	if (--m->pg_init_in_progress)
1206d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1207d0259bf0SKiyoshi Ueda 		goto out;
1208d0259bf0SKiyoshi Ueda 
12093e9f1be1SHannes Reinecke 	if (m->pg_init_required) {
12104e2d19e4SChandra Seetharaman 		m->pg_init_delay_retry = delay_retry;
12113e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
12123e9f1be1SHannes Reinecke 			goto out;
12133e9f1be1SHannes Reinecke 	}
12143e9f1be1SHannes Reinecke 	m->queue_io = 0;
1215d0259bf0SKiyoshi Ueda 
12162bded7bdSKiyoshi Ueda 	/*
12172bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
12182bded7bdSKiyoshi Ueda 	 */
12192bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
12202bded7bdSKiyoshi Ueda 
1221d0259bf0SKiyoshi Ueda out:
1222cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1223cfae5c9bSChandra Seetharaman }
1224cfae5c9bSChandra Seetharaman 
1225bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1226bab7cfc7SChandra Seetharaman {
1227e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
12284e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1229bab7cfc7SChandra Seetharaman 
12303a017509SHannes Reinecke 	if (pgpath->is_active)
12313ae31f6aSChandra Seetharaman 		scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
123283c0d5d5SMoger, Babu 				 pg_init_done, pgpath);
12333a017509SHannes Reinecke 	else
12343a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1235bab7cfc7SChandra Seetharaman }
1236bab7cfc7SChandra Seetharaman 
12377e782af5SHannes Reinecke static int noretry_error(int error)
12387e782af5SHannes Reinecke {
12397e782af5SHannes Reinecke 	switch (error) {
12407e782af5SHannes Reinecke 	case -EOPNOTSUPP:
12417e782af5SHannes Reinecke 	case -EREMOTEIO:
12427e782af5SHannes Reinecke 	case -EILSEQ:
12437e782af5SHannes Reinecke 	case -ENODATA:
1244cc9d3c38SJun'ichi Nomura 	case -ENOSPC:
12457e782af5SHannes Reinecke 		return 1;
12467e782af5SHannes Reinecke 	}
12477e782af5SHannes Reinecke 
12487e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
12497e782af5SHannes Reinecke 	return 0;
12507e782af5SHannes Reinecke }
12517e782af5SHannes Reinecke 
12521da177e4SLinus Torvalds /*
12531da177e4SLinus Torvalds  * end_io handling
12541da177e4SLinus Torvalds  */
1255f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1256028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12571da177e4SLinus Torvalds {
1258f40c67f0SKiyoshi Ueda 	/*
1259f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1260f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1261f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1262f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1263f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1264f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1265f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1266f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1267f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1268f40c67f0SKiyoshi Ueda 	 */
1269f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1270640eb3b0SStefan Bader 	unsigned long flags;
12711da177e4SLinus Torvalds 
1272f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
12731da177e4SLinus Torvalds 		return 0;	/* I/O complete */
12741da177e4SLinus Torvalds 
12757eee4ae2SMike Snitzer 	if (noretry_error(error))
1276959eb4e5SMike Snitzer 		return error;
1277959eb4e5SMike Snitzer 
1278cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
12791da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
12801da177e4SLinus Torvalds 
1281640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1282751b2a7dSHannes Reinecke 	if (!m->nr_valid_paths) {
1283751b2a7dSHannes Reinecke 		if (!m->queue_if_no_path) {
1284751b2a7dSHannes Reinecke 			if (!__must_push_back(m))
1285f40c67f0SKiyoshi Ueda 				r = -EIO;
1286751b2a7dSHannes Reinecke 		} else {
1287751b2a7dSHannes Reinecke 			if (error == -EBADE)
1288751b2a7dSHannes Reinecke 				r = error;
1289751b2a7dSHannes Reinecke 		}
1290751b2a7dSHannes Reinecke 	}
1291640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
12921da177e4SLinus Torvalds 
1293f40c67f0SKiyoshi Ueda 	return r;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
12971da177e4SLinus Torvalds 			    int error, union map_info *map_context)
12981da177e4SLinus Torvalds {
1299028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1300028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
1301a71a261fSWei Yongjun 	struct pgpath *pgpath;
13021da177e4SLinus Torvalds 	struct path_selector *ps;
13031da177e4SLinus Torvalds 	int r;
13041da177e4SLinus Torvalds 
1305466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1306466891f9SJun'ichi Nomura 
1307f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
1308a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
13091da177e4SLinus Torvalds 	if (pgpath) {
13101da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
13111da177e4SLinus Torvalds 		if (ps->type->end_io)
131202ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
13131da177e4SLinus Torvalds 	}
1314466891f9SJun'ichi Nomura 	clear_mapinfo(m, map_context);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	return r;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds /*
13201da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1321436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1322436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1323436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
13241da177e4SLinus Torvalds  */
13251da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
13261da177e4SLinus Torvalds {
13271da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13281da177e4SLinus Torvalds 
1329485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13326df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
13336df400abSKiyoshi Ueda {
13346380f26fSMike Anderson 	struct multipath *m = ti->private;
13356380f26fSMike Anderson 
13366380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
13372bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
13386380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
13396df400abSKiyoshi Ueda }
13406df400abSKiyoshi Ueda 
1341436d4108SAlasdair G Kergon /*
1342436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1343436d4108SAlasdair G Kergon  */
13441da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13471da177e4SLinus Torvalds 	unsigned long flags;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1350436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
13511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /*
13551da177e4SLinus Torvalds  * Info output has the following format:
13561da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
13571da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
13581da177e4SLinus Torvalds  * num_groups init_group_number
13591da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
13601da177e4SLinus Torvalds  *             num_paths num_selector_args
13611da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
13621da177e4SLinus Torvalds  *
13631da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
13641da177e4SLinus Torvalds  * num_feature_args [features_args]*
13651da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
13661da177e4SLinus Torvalds  * num_groups init_group_number
13671da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
13681da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
13691da177e4SLinus Torvalds  */
1370fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
13711f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	int sz = 0;
13741da177e4SLinus Torvalds 	unsigned long flags;
13751da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13761da177e4SLinus Torvalds 	struct priority_group *pg;
13771da177e4SLinus Torvalds 	struct pgpath *p;
13781da177e4SLinus Torvalds 	unsigned pg_num;
13791da177e4SLinus Torvalds 	char state;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/* Features */
13841da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1385e8099177SHannes Reinecke 		DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
1386c9e45581SDave Wysochanski 	else {
1387c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
13884e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1389a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1390a58a935dSMike Snitzer 			      m->retain_attached_hw_handler);
1391c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1392c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1393c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1394c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
13954e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
13964e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1397a58a935dSMike Snitzer 		if (m->retain_attached_hw_handler)
1398a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1399c9e45581SDave Wysochanski 	}
14001da177e4SLinus Torvalds 
1401cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
14021da177e4SLinus Torvalds 		DMEMIT("0 ");
14031da177e4SLinus Torvalds 	else
1404cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	if (m->next_pg)
14091da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
14101da177e4SLinus Torvalds 	else if (m->current_pg)
14111da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
14121da177e4SLinus Torvalds 	else
1413a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	switch (type) {
14181da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
14191da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14201da177e4SLinus Torvalds 			if (pg->bypassed)
14211da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
14221da177e4SLinus Torvalds 			else if (pg == m->current_pg)
14231da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
14241da177e4SLinus Torvalds 			else
14251da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 			DMEMIT("%c ", state);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 			if (pg->ps.type->status)
14301da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14311da177e4SLinus Torvalds 							  result + sz,
14321da177e4SLinus Torvalds 							  maxlen - sz);
14331da177e4SLinus Torvalds 			else
14341da177e4SLinus Torvalds 				DMEMIT("0 ");
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14371da177e4SLinus Torvalds 			       pg->ps.type->info_args);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14401da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
14416680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
14421da177e4SLinus Torvalds 				       p->fail_count);
14431da177e4SLinus Torvalds 				if (pg->ps.type->status)
14441da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14451da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14461da177e4SLinus Torvalds 					      maxlen - sz);
14471da177e4SLinus Torvalds 			}
14481da177e4SLinus Torvalds 		}
14491da177e4SLinus Torvalds 		break;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
14521da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14531da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 			if (pg->ps.type->status)
14561da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14571da177e4SLinus Torvalds 							  result + sz,
14581da177e4SLinus Torvalds 							  maxlen - sz);
14591da177e4SLinus Torvalds 			else
14601da177e4SLinus Torvalds 				DMEMIT("0 ");
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14631da177e4SLinus Torvalds 			       pg->ps.type->table_args);
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14661da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
14671da177e4SLinus Torvalds 				if (pg->ps.type->status)
14681da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14691da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14701da177e4SLinus Torvalds 					      maxlen - sz);
14711da177e4SLinus Torvalds 			}
14721da177e4SLinus Torvalds 		}
14731da177e4SLinus Torvalds 		break;
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
14801da177e4SLinus Torvalds {
14816380f26fSMike Anderson 	int r = -EINVAL;
14821da177e4SLinus Torvalds 	struct dm_dev *dev;
14831da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14841da177e4SLinus Torvalds 	action_fn action;
14851da177e4SLinus Torvalds 
14866380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
14876380f26fSMike Anderson 
1488c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1489c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1490c2f3d24bSKiyoshi Ueda 		goto out;
1491c2f3d24bSKiyoshi Ueda 	}
1492c2f3d24bSKiyoshi Ueda 
14931da177e4SLinus Torvalds 	if (argc == 1) {
1494498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
14956380f26fSMike Anderson 			r = queue_if_no_path(m, 1, 0);
14966380f26fSMike Anderson 			goto out;
1497498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
14986380f26fSMike Anderson 			r = queue_if_no_path(m, 0, 0);
14996380f26fSMike Anderson 			goto out;
15006380f26fSMike Anderson 		}
15011da177e4SLinus Torvalds 	}
15021da177e4SLinus Torvalds 
15036380f26fSMike Anderson 	if (argc != 2) {
1504a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
15056380f26fSMike Anderson 		goto out;
15066380f26fSMike Anderson 	}
15071da177e4SLinus Torvalds 
1508498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
15096380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 1);
15106380f26fSMike Anderson 		goto out;
1511498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
15126380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 0);
15136380f26fSMike Anderson 		goto out;
1514498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
15156380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
15166380f26fSMike Anderson 		goto out;
1517498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
15181da177e4SLinus Torvalds 		action = reinstate_path;
1519498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
15201da177e4SLinus Torvalds 		action = fail_path;
15216380f26fSMike Anderson 	else {
1522a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
15236380f26fSMike Anderson 		goto out;
15246380f26fSMike Anderson 	}
15251da177e4SLinus Torvalds 
15268215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
15271da177e4SLinus Torvalds 	if (r) {
152872d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
15291da177e4SLinus Torvalds 		       argv[1]);
15306380f26fSMike Anderson 		goto out;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	dm_put_device(ti, dev);
15361da177e4SLinus Torvalds 
15376380f26fSMike Anderson out:
15386380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15391da177e4SLinus Torvalds 	return r;
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
1542647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
15439af4aa30SMilan Broz 			   unsigned long arg)
15449af4aa30SMilan Broz {
154535991652SMikulas Patocka 	struct multipath *m = ti->private;
15467ba10aa6SMike Snitzer 	struct pgpath *pgpath;
154735991652SMikulas Patocka 	struct block_device *bdev;
154835991652SMikulas Patocka 	fmode_t mode;
15499af4aa30SMilan Broz 	unsigned long flags;
155035991652SMikulas Patocka 	int r;
155135991652SMikulas Patocka 
155235991652SMikulas Patocka 	bdev = NULL;
155335991652SMikulas Patocka 	mode = 0;
155435991652SMikulas Patocka 	r = 0;
15559af4aa30SMilan Broz 
15569af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
15579af4aa30SMilan Broz 
15589af4aa30SMilan Broz 	if (!m->current_pgpath)
155902ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
15609af4aa30SMilan Broz 
15617ba10aa6SMike Snitzer 	pgpath = m->current_pgpath;
15627ba10aa6SMike Snitzer 
15637ba10aa6SMike Snitzer 	if (pgpath) {
15647ba10aa6SMike Snitzer 		bdev = pgpath->path.dev->bdev;
15657ba10aa6SMike Snitzer 		mode = pgpath->path.dev->mode;
1566e90dae1fSMilan Broz 	}
15679af4aa30SMilan Broz 
15687ba10aa6SMike Snitzer 	if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
15696c182cd8SHannes Reinecke 		r = -ENOTCONN;
15709af4aa30SMilan Broz 	else if (!bdev)
15719af4aa30SMilan Broz 		r = -EIO;
15729af4aa30SMilan Broz 
15739af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
15749af4aa30SMilan Broz 
1575ec8013beSPaolo Bonzini 	/*
1576ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
1577ec8013beSPaolo Bonzini 	 */
1578a1989b33SHannes Reinecke 	if (!bdev || ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT) {
1579a1989b33SHannes Reinecke 		int err = scsi_verify_blk_ioctl(NULL, cmd);
1580a1989b33SHannes Reinecke 		if (err)
1581a1989b33SHannes Reinecke 			r = err;
1582a1989b33SHannes Reinecke 	}
1583ec8013beSPaolo Bonzini 
15843e9f1be1SHannes Reinecke 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
15853e9f1be1SHannes Reinecke 		spin_lock_irqsave(&m->lock, flags);
15863e9f1be1SHannes Reinecke 		if (!m->current_pg) {
15873e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
15883e9f1be1SHannes Reinecke 			__choose_pgpath(m, 0);
15893e9f1be1SHannes Reinecke 		}
15903e9f1be1SHannes Reinecke 		if (m->pg_init_required)
15913e9f1be1SHannes Reinecke 			__pg_init_all_paths(m);
15924cdd2ad7SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
159363d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
15943e9f1be1SHannes Reinecke 	}
159535991652SMikulas Patocka 
1596633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
15979af4aa30SMilan Broz }
15989af4aa30SMilan Broz 
1599af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1600af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1601af4874e0SMike Snitzer {
1602af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1603af4874e0SMike Snitzer 	struct priority_group *pg;
1604af4874e0SMike Snitzer 	struct pgpath *p;
1605af4874e0SMike Snitzer 	int ret = 0;
1606af4874e0SMike Snitzer 
1607af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1608af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
16095dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1610af4874e0SMike Snitzer 			if (ret)
1611af4874e0SMike Snitzer 				goto out;
1612af4874e0SMike Snitzer 		}
1613af4874e0SMike Snitzer 	}
1614af4874e0SMike Snitzer 
1615af4874e0SMike Snitzer out:
1616af4874e0SMike Snitzer 	return ret;
1617af4874e0SMike Snitzer }
1618af4874e0SMike Snitzer 
1619f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1620f40c67f0SKiyoshi Ueda {
1621f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1622f40c67f0SKiyoshi Ueda 
162352b09914SMike Snitzer 	return blk_lld_busy(q);
1624f40c67f0SKiyoshi Ueda }
1625f40c67f0SKiyoshi Ueda 
1626f40c67f0SKiyoshi Ueda /*
1627f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1628f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1629f40c67f0SKiyoshi Ueda  * the underlying queue).
1630f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1631f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1632f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1633f40c67f0SKiyoshi Ueda  */
1634f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1635f40c67f0SKiyoshi Ueda {
1636f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1637f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1638f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1639f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1640f40c67f0SKiyoshi Ueda 	unsigned long flags;
1641f40c67f0SKiyoshi Ueda 
1642f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1643f40c67f0SKiyoshi Ueda 
16447a7a3b45SJun'ichi Nomura 	/* pg_init in progress or no paths available */
16457a7a3b45SJun'ichi Nomura 	if (m->pg_init_in_progress ||
16467a7a3b45SJun'ichi Nomura 	    (!m->nr_valid_paths && m->queue_if_no_path)) {
1647b63349a7SHannes Reinecke 		busy = 1;
1648b63349a7SHannes Reinecke 		goto out;
1649b63349a7SHannes Reinecke 	}
1650f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1651f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1652f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1653f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1654f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1655f40c67f0SKiyoshi Ueda 	else
1656f40c67f0SKiyoshi Ueda 		/*
1657f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1658f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1659f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1660f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1661f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1662f40c67f0SKiyoshi Ueda 		 */
1663f40c67f0SKiyoshi Ueda 		goto out;
1664f40c67f0SKiyoshi Ueda 
1665f40c67f0SKiyoshi Ueda 	/*
1666f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1667f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1668f40c67f0SKiyoshi Ueda 	 */
1669f40c67f0SKiyoshi Ueda 	busy = 1;
1670f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1671f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1672f40c67f0SKiyoshi Ueda 			has_active = 1;
1673f40c67f0SKiyoshi Ueda 
1674f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1675f40c67f0SKiyoshi Ueda 				busy = 0;
1676f40c67f0SKiyoshi Ueda 				break;
1677f40c67f0SKiyoshi Ueda 			}
1678f40c67f0SKiyoshi Ueda 		}
1679f40c67f0SKiyoshi Ueda 
1680f40c67f0SKiyoshi Ueda 	if (!has_active)
1681f40c67f0SKiyoshi Ueda 		/*
1682f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1683f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1684f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1685f40c67f0SKiyoshi Ueda 		 */
1686f40c67f0SKiyoshi Ueda 		busy = 0;
1687f40c67f0SKiyoshi Ueda 
1688f40c67f0SKiyoshi Ueda out:
1689f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1690f40c67f0SKiyoshi Ueda 
1691f40c67f0SKiyoshi Ueda 	return busy;
1692f40c67f0SKiyoshi Ueda }
1693f40c67f0SKiyoshi Ueda 
16941da177e4SLinus Torvalds /*-----------------------------------------------------------------
16951da177e4SLinus Torvalds  * Module setup
16961da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16971da177e4SLinus Torvalds static struct target_type multipath_target = {
16981da177e4SLinus Torvalds 	.name = "multipath",
1699bfebd1cdSMike Snitzer 	.version = {1, 9, 0},
17001da177e4SLinus Torvalds 	.module = THIS_MODULE,
17011da177e4SLinus Torvalds 	.ctr = multipath_ctr,
17021da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1703f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1704e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
1705e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
1706f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
17071da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
17086df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
17091da177e4SLinus Torvalds 	.resume = multipath_resume,
17101da177e4SLinus Torvalds 	.status = multipath_status,
17111da177e4SLinus Torvalds 	.message = multipath_message,
17129af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1713af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1714f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
17151da177e4SLinus Torvalds };
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds static int __init dm_multipath_init(void)
17181da177e4SLinus Torvalds {
17191da177e4SLinus Torvalds 	int r;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1722028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
17231da177e4SLinus Torvalds 	if (!_mpio_cache)
17241da177e4SLinus Torvalds 		return -ENOMEM;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
17271da177e4SLinus Torvalds 	if (r < 0) {
17280cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
1729ff658e9cSJohannes Thumshirn 		r = -EINVAL;
1730ff658e9cSJohannes Thumshirn 		goto bad_register_target;
17311da177e4SLinus Torvalds 	}
17321da177e4SLinus Torvalds 
17334d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1734c557308eSAlasdair G Kergon 	if (!kmultipathd) {
17350cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1736ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1737ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
1738c557308eSAlasdair G Kergon 	}
1739c557308eSAlasdair G Kergon 
1740bab7cfc7SChandra Seetharaman 	/*
1741bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1742bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1743bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1744bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1745bab7cfc7SChandra Seetharaman 	 */
17464d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
17474d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1748bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1749bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1750ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1751ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
1752bab7cfc7SChandra Seetharaman 	}
1753bab7cfc7SChandra Seetharaman 
175472d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
17551da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
17561da177e4SLinus Torvalds 	       multipath_target.version[2]);
17571da177e4SLinus Torvalds 
1758ff658e9cSJohannes Thumshirn 	return 0;
1759ff658e9cSJohannes Thumshirn 
1760ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
1761ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
1762ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
1763ff658e9cSJohannes Thumshirn 	dm_unregister_target(&multipath_target);
1764ff658e9cSJohannes Thumshirn bad_register_target:
1765ff658e9cSJohannes Thumshirn 	kmem_cache_destroy(_mpio_cache);
1766ff658e9cSJohannes Thumshirn 
17671da177e4SLinus Torvalds 	return r;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
17711da177e4SLinus Torvalds {
1772bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1773c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1774c557308eSAlasdair G Kergon 
177510d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
17761da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds module_init(dm_multipath_init);
17801da177e4SLinus Torvalds module_exit(dm_multipath_exit);
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
17831da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
17841da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1785